Commit ceea67fa2b82620ef0b4b1e27573d02c6bb2e8bc

Authored by Paulo Henrique Junqueira Amorim
1 parent 2ce60186

ENH: Optimize importation of DICOM files

invesalius/control.py
... ... @@ -328,7 +328,6 @@ class Controller():
328 328 def ImportMedicalImages(self, directory):
329 329 # OPTION 1: DICOM?
330 330 patients_groups = dcm.GetDicomGroups(directory)
331   -
332 331 if len(patients_groups):
333 332 group = dcm.SelectLargerDicomGroup(patients_groups)
334 333 imagedata, dicom = self.OpenDicomGroup(group, 0, gui=True)
... ... @@ -486,7 +485,6 @@ class Controller():
486 485  
487 486 wl = float(dicom.image.level)
488 487 ww = float(dicom.image.window)
489   -
490 488 self.matrix, self.filename = utils.dcm2memmap(filelist, size,
491 489 orientation)
492 490 self.Slice = sl.Slice()
... ...
invesalius/data/imagedata_utils.py
... ... @@ -469,7 +469,7 @@ def dcm2memmap(files, slice_size, orientation):
469 469 shape = slice_size[1], len(files), slice_size[0]
470 470 else:
471 471 shape = len(files), slice_size[1], slice_size[0]
472   -
  472 + print shape
473 473 matrix = numpy.memmap(temp_file, mode='w+', dtype='int16', shape=shape)
474 474 dcm_reader = vtkgdcm.vtkGDCMImageReader()
475 475 for n, f in enumerate(files):
... ...
invesalius/invesalius.py
... ... @@ -31,7 +31,7 @@ if sys.platform == 'win32':
31 31 import _winreg
32 32 else:
33 33 import wxversion
34   - wxversion.ensureMinimal('2.8-unicode', optionsRequired=True)
  34 + wxversion.ensureMinimal('2.8-unicode', optionsRequired=True)
35 35 wxversion.select('2.8-unicode', optionsRequired=True)
36 36  
37 37 import wx
... ...
invesalius/reader/dicom.py
... ... @@ -18,7 +18,7 @@
18 18 # detalhes.
19 19 #---------------------------------------------------------------------
20 20 import time
21   -import gdcm
  21 +#import gdcm
22 22 #import vtkgdcm
23 23 import sys
24 24  
... ... @@ -91,15 +91,14 @@ class Parser():
91 91 def __init__(self):
92 92 self.filename = ""
93 93 self.encoding = ""
94   - #self.vtkgdcm_reader = vtkgdcm.vtkGDCMImageReader()
95 94  
96   - def SetFileName(self, filename):
  95 + #def SetFileName(self, filename):
97 96 """
98 97 Set file name to be parsed given its filename (this should
99 98 include the full path of the file of interest).
100 99  
101 100 Return True/False if file could be read.
102   - """
  101 +
103 102 import os.path as path
104 103  
105 104  
... ... @@ -133,10 +132,15 @@ class Parser():
133 132 #self.vtkgdcm_reader = vtkgdcm_reader
134 133 return True
135 134  
136   - return False
  135 + return False"""
  136 +
  137 + #def GetImageData(self):
  138 + # return None#self.vtkgdcm_reader.GetOutput()
  139 +
137 140  
138   - def GetImageData(self):
139   - return None#self.vtkgdcm_reader.GetOutput()
  141 + def SetDataImage(self, data_image, filename):
  142 + self.data_image = data_image
  143 + self.filename = filename
140 144  
141 145 def __format_time(self,value):
142 146 sp1 = value.split(".")
... ... @@ -181,14 +185,7 @@ class Parser():
181 185 an image. (AXIAL, SAGITTAL, CORONAL,
182 186 OBLIQUE or UNKNOWN)
183 187 """
184   - img = self.gdcm_reader.GetImage()
185   - direc_cosines = img.GetDirectionCosines()
186   - orientation = gdcm.Orientation()
187   - try:
188   - type = orientation.GetType(tuple(direc_cosines))
189   - except TypeError:
190   - type = orientation.GetType(direc_cosines)
191   - label = orientation.GetLabel(type)
  188 + label = self.data_image['invesalius']['orientation_label']
192 189  
193 190 if (label):
194 191 return label
... ... @@ -201,12 +198,7 @@ class Parser():
201 198 to the number of columns on the image.
202 199 Return "" if not defined.
203 200 """
204   - tag = gdcm.Tag(0x0028, 0x0011)
205   - ds = self.gdcm_reader.GetFile().GetDataSet()
206   - sf = gdcm.StringFilter()
207   - sf.SetFile(self.gdcm_reader.GetFile())
208   - data = sf.ToStringPair(tag)[1]
209   -
  201 + data = self.data_image['0028']['0011']
210 202 if (data):
211 203 return int(str(data))
212 204 return ""
... ... @@ -218,12 +210,7 @@ class Parser():
218 210 to the number of rows on the image.
219 211 Return "" if not defined.
220 212 """
221   - tag = gdcm.Tag(0x0028, 0x0010)
222   - ds = self.gdcm_reader.GetFile().GetDataSet()
223   - sf = gdcm.StringFilter()
224   - sf.SetFile(self.gdcm_reader.GetFile())
225   - data = sf.ToStringPair(tag)[1]
226   -
  213 + data = self.data_image['0028']['0010']
227 214 if (data):
228 215 return int(str(data))
229 216 return ""
... ... @@ -309,12 +296,9 @@ class Parser():
309 296 DICOM standard tag (0x0008,0x0022) was used.
310 297 """
311 298 # TODO: internationalize data
312   - tag = gdcm.Tag(0x0008, 0x0022)
313   - ds = self.gdcm_reader.GetFile().GetDataSet()
314   - if ds.FindDataElement(tag):
315   - date = ds.GetDataElement(tag).GetValue()
316   - if (date) and (date != ''):
317   - return self.__format_date(str(date))
  299 + date = self.data_image['0008']['0022']
  300 + if (date) and (date != ''):
  301 + return self.__format_date(str(date))
318 302 return ""
319 303  
320 304 def GetAcquisitionNumber(self):
... ... @@ -324,12 +308,9 @@ class Parser():
324 308  
325 309 DICOM standard tag (0x0020, 0x0012) was used.
326 310 """
327   - tag = gdcm.Tag(0x0020, 0x0012)
328   - ds = self.gdcm_reader.GetFile().GetDataSet()
329   - if ds.FindDataElement(tag):
330   - data = ds.GetDataElement(tag).GetValue()
331   - if (data):
332   - return int(str(data))
  311 + data = self.data_image['0020']['0012']
  312 + if (data):
  313 + return int(str(data))
333 314 return ""
334 315  
335 316 def GetAccessionNumber(self):
... ... @@ -338,16 +319,13 @@ class Parser():
338 319  
339 320 DICOM standard tag (0x0008, 0x0050) was used.
340 321 """
341   - tag = gdcm.Tag(0x0008, 0x0050)
342   - ds = self.gdcm_reader.GetFile().GetDataSet()
343   - if ds.FindDataElement(tag):
344   - data = ds.GetDataElement(tag).GetValue()
345   - if (data):
346   - try:
347   - value = int(str(data))
348   - except(ValueError): #Problem in the other\iCatDanielaProjeto
349   - value = 0
350   - return value
  322 + data = self.data_image['0008']['0050']
  323 + if (data):
  324 + try:
  325 + value = int(str(data))
  326 + except(ValueError): #Problem in the other\iCatDanielaProjeto
  327 + value = 0
  328 + return value
351 329 return ""
352 330  
353 331 def GetAcquisitionTime(self):
... ... @@ -358,12 +336,9 @@ class Parser():
358 336  
359 337 DICOM standard tag (0x0008,0x0032) was used.
360 338 """
361   - tag = gdcm.Tag(0x0008, 0x0032)
362   - ds = self.gdcm_reader.GetFile().GetDataSet()
363   - if ds.FindDataElement(tag):
364   - data = ds.GetDataElement(tag).GetValue()
365   - if (data) and (data != ''):
366   - return self.__format_time(str(data))
  339 + data = self.data_image['0008']['0032']
  340 + if (data) and (data != ''):
  341 + return self.__format_time(str(data))
367 342 return ""
368 343  
369 344 def GetPatientAdmittingDiagnosis(self):
... ... @@ -388,33 +363,28 @@ class Parser():
388 363 Return image window center / level (related to brightness).
389 364 This is an integer or a floating point. If the value can't
390 365 be read, return "".
391   -
392 366 By default, only one level value is returned, according to
393 367 "preset" parameter. If no value is passed, WL_PRESET constant
394 368 is used. In case one wishes to acquire a list with all
395 369 level values, one should set "multiple" parameter to True.
396   -
397 370 Return "" if field is not defined.
398   -
399 371 DICOM standard tag (0x0028,0x1050) was used.
400 372 """
401   -
402   - tag = gdcm.Tag(0x0028, 0x1050)
403   - ds = self.gdcm_reader.GetFile().GetDataSet()
404   -
405   - if ds.FindDataElement(tag):
406   - data = str(ds.GetDataElement(tag).GetValue())
407   - if (data):
408   - # Usually 'data' is a number. However, in some DICOM
409   - # files, there are several values separated by '\'.
410   - # If multiple values are present for the "Window Center"
411   - # we choose only one. As this should be paired to "Window
412   - # Width", it is set based on WL_PRESET
413   - value_list = [float(value) for value in data.split('\\')]
414   - if multiple:
415   - return value_list
416   - else:
417   - return value_list[preset]
  373 + try:
  374 + data = self.data_image['0028']['1050']
  375 + except(KeyError):
  376 + return "300"
  377 + if (data):
  378 + # Usually 'data' is a number. However, in some DICOM
  379 + # files, there are several values separated by '\'.
  380 + # If multiple values are present for the "Window Center"
  381 + # we choose only one. As this should be paired to "Window
  382 + # Width", it is set based on WL_PRESET
  383 + value_list = [float(value) for value in data.split('\\')]
  384 + if multiple:
  385 + return value_list
  386 + else:
  387 + return value_list[preset]
418 388 return "300"
419 389  
420 390 def GetImageWindowWidth(self, preset=WL_PRESET, multiple=WL_MULT):
... ... @@ -432,24 +402,23 @@ class Parser():
432 402  
433 403 DICOM standard tag (0x0028,0x1051) was used.
434 404 """
  405 + try:
  406 + data = self.data_image['0028']['1051']
  407 + except(KeyError):
  408 + return "2000"
435 409  
436   - tag = gdcm.Tag(0x0028,0x1051)
437   - ds = self.gdcm_reader.GetFile().GetDataSet()
438   -
439   - if ds.FindDataElement(tag):
440   - data = str(ds.GetDataElement(tag).GetValue())
441   - if (data):
442   - # Usually 'data' is a number. However, in some DICOM
443   - # files, there are several values separated by '\'.
444   - # If multiple values are present for the "Window Center"
445   - # we choose only one. As this should be paired to "Window
446   - # Width", it is set based on WL_PRESET
447   - value_list = [float(value) for value in data.split('\\')]
448   -
449   - if multiple:
450   - return str(value_list)
451   - else:
452   - return str(value_list[preset])
  410 + if (data):
  411 + # Usually 'data' is a number. However, in some DICOM
  412 + # files, there are several values separated by '\'.
  413 + # If multiple values are present for the "Window Center"
  414 + # we choose only one. As this should be paired to "Window
  415 + # Width", it is set based on WL_PRESET
  416 + value_list = [float(value) for value in data.split('\\')]
  417 +
  418 + if multiple:
  419 + return str(value_list)
  420 + else:
  421 + return str(value_list[preset])
453 422 return "2000"
454 423  
455 424 def GetImagePosition(self):
... ... @@ -462,12 +431,12 @@ class Parser():
462 431  
463 432 DICOM standard tag (0x0020, 0x0032) was used.
464 433 """
465   - tag = gdcm.Tag(0x0020, 0x0032)
466   - ds = self.gdcm_reader.GetFile().GetDataSet()
467   - if ds.FindDataElement(tag):
468   - data = str(ds.GetDataElement(tag).GetValue())
469   - if (data):
470   - return [eval(value) for value in data.split('\\')]
  434 + try:
  435 + data = self.data_image['0020']['0032']
  436 + except(KeyError):
  437 + return ""
  438 + if (data):
  439 + return [eval(value) for value in data.split('\\')]
471 440 return ""
472 441  
473 442 def GetImageLocation(self):
... ... @@ -478,12 +447,9 @@ class Parser():
478 447  
479 448 DICOM standard tag (0x0020, 0x0032) was used.
480 449 """
481   - tag = gdcm.Tag(0x0020, 0x1041)
482   - ds = self.gdcm_reader.GetFile().GetDataSet()
483   - if ds.FindDataElement(tag):
484   - data = str(ds.GetDataElement(tag).GetValue())
485   - if (data):
486   - return eval(data)
  450 + data = self.data_image['0020']['1041']
  451 + if (data):
  452 + return eval(data)
487 453 return ""
488 454  
489 455 def GetImageOffset(self):
... ... @@ -493,12 +459,9 @@ class Parser():
493 459  
494 460 DICOM standard tag (0x7fe0, 0x0010) was used.
495 461 """
496   - tag = gdcm.Tag(0x7fe0, 0x0010)
497   - ds = self.gdcm_reader.GetFile().GetDataSet()
498   - if ds.FindDataElement(tag):
499   - data = str(ds.GetDataElement(tag).GetValue())
500   - if (data):
501   - return int(data.split(':')[1])
  462 + data = self.data_image['7fe0']['0010']
  463 + if (data):
  464 + return int(data.split(':')[1])
502 465 return ""
503 466  
504 467  
... ... @@ -510,12 +473,9 @@ class Parser():
510 473  
511 474 DICOM standard tag (0x0020, 0x0011) was used.
512 475 """
513   - tag = gdcm.Tag(0x0020, 0x0011)
514   - ds = self.gdcm_reader.GetFile().GetDataSet()
515   - if ds.FindDataElement(tag):
516   - data = str(ds.GetDataElement(tag).GetValue())
517   - if (data) and (data != '""') and (data != "None"):
518   - return int(data)
  476 + data = self.data_image['0020']['0011']
  477 + if (data) and (data != '""') and (data != "None"):
  478 + return int(data)
519 479 return ""
520 480  
521 481  
... ... @@ -529,12 +489,9 @@ class Parser():
529 489  
530 490 DICOM standard tag (0x0028, 0x0030) was used.
531 491 """
532   - tag = gdcm.Tag(0x0028, 0x0030)
533   - ds = self.gdcm_reader.GetFile().GetDataSet()
534   - if ds.FindDataElement(tag):
535   - data = str(ds.GetDataElement(tag).GetValue())
536   - if (data):
537   - return [eval(value) for value in data.split('\\')]
  492 + data = self.data_image['0028']['0030']
  493 + if (data):
  494 + return [eval(value) for value in data.split('\\')]
538 495 return ""
539 496  
540 497 def GetPatientWeight(self):
... ... @@ -544,12 +501,9 @@ class Parser():
544 501  
545 502 DICOM standard tag (0x0010, 0x1030) was used.
546 503 """
547   - tag = gdcm.Tag(0x0010, 0x1030)
548   - ds = self.gdcm_reader.GetFile().GetDataSet()
549   - if ds.FindDataElement(tag):
550   - data = str(ds.GetDataElement(tag).GetValue())
551   - if (data):
552   - return float(data)
  504 + data = self.data_image['0010']['1030']
  505 + if (data):
  506 + return float(data)
553 507 return ""
554 508  
555 509 def GetPatientHeight(self):
... ... @@ -559,12 +513,9 @@ class Parser():
559 513  
560 514 DICOM standard tag (0x0010, 0x1030) was used.
561 515 """
562   - tag = gdcm.Tag(0x0010, 0x1020)
563   - ds = self.gdcm_reader.GetFile().GetDataSet()
564   - if ds.FindDataElement(tag):
565   - data = str(ds.GetDataElement(tag).GetValue())
566   - if (data):
567   - return float(data)
  516 + data = self.data_image['0010']['1020']
  517 + if (data):
  518 + return float(data)
568 519 return ""
569 520  
570 521 def GetPatientAddress(self):
... ... @@ -573,12 +524,9 @@ class Parser():
573 524  
574 525 DICOM standard tag (0x0010, 0x1040) was used.
575 526 """
576   - tag = gdcm.Tag(0x0010, 0x1040)
577   - ds = self.gdcm_reader.GetFile().GetDataSet()
578   - if ds.FindDataElement(tag):
579   - data = str(ds.GetDataElement(tag).GetValue())
580   - if (data):
581   - return data
  527 + data = self.data_image['0010']['1040']
  528 + if (data):
  529 + return data
582 530 return ""
583 531  
584 532 def GetPatientMilitarRank(self):
... ... @@ -588,12 +536,9 @@ class Parser():
588 536  
589 537 DICOM standard tag (0x0010, 0x1080) was used.
590 538 """
591   - tag = gdcm.Tag(0x0010,0x1080)
592   - ds = self.gdcm_reader.GetFile().GetDataSet()
593   - if ds.FindDataElement(tag):
594   - data = str(ds.GetDataElement(tag).GetValue())
595   - if (data):
596   - return data
  539 + data = self.data_image['0010']['1080']
  540 + if (data):
  541 + return data
597 542 return ""
598 543  
599 544 def GetPatientMilitarBranch(self):
... ... @@ -605,12 +550,9 @@ class Parser():
605 550  
606 551 DICOM standard tag (0x0010, 0x1081) was used.
607 552 """
608   - tag = gdcm.Tag(0x0010,0x1081)
609   - ds = self.gdcm_reader.GetFile().GetDataSet()
610   - if ds.FindDataElement(tag):
611   - data = str(ds.GetDataElement(tag).GetValue())
612   - if (data):
613   - return data
  553 + data = self.data_image['0010']['1081']
  554 + if (data):
  555 + return data
614 556 return ""
615 557  
616 558 def GetPatientCountry(self):
... ... @@ -621,12 +563,9 @@ class Parser():
621 563  
622 564 DICOM standard tag (0x0010, 0x2150) was used.
623 565 """
624   - tag = gdcm.Tag(0x0010,0x2150)
625   - ds = self.gdcm_reader.GetFile().GetDataSet()
626   - if ds.FindDataElement(tag):
627   - data = str(ds.GetDataElement(tag).GetValue())
628   - if (data):
629   - return data
  566 + data = self.data_image['0010']['2150']
  567 + if (data):
  568 + return data
630 569 return ""
631 570  
632 571 def GetPatientRegion(self):
... ... @@ -637,12 +576,9 @@ class Parser():
637 576  
638 577 DICOM standard tag (0x0010, 0x2152) was used.
639 578 """
640   - tag = gdcm.Tag(0x0010,0x2152)
641   - ds = self.gdcm_reader.GetFile().GetDataSet()
642   - if ds.FindDataElement(tag):
643   - data = str(ds.GetDataElement(tag).GetValue())
644   - if (data):
645   - return data
  579 + data = self.data_image['0010']['2152']
  580 + if (data):
  581 + return data
646 582 return ""
647 583  
648 584 def GetPatientTelephone(self):
... ... @@ -652,12 +588,9 @@ class Parser():
652 588  
653 589 DICOM standard tag (0x0010, 0x2154) was used.
654 590 """
655   - tag = gdcm.Tag(0x0010,0x2154)
656   - ds = self.gdcm_reader.GetFile().GetDataSet()
657   - if ds.FindDataElement(tag):
658   - data = str(ds.GetDataElement(tag).GetValue())
659   - if (data):
660   - return data
  591 + data = self.data_image['0010']['2154']
  592 + if (data):
  593 + return data
661 594 return ""
662 595  
663 596 def GetPatientResponsible(self):
... ... @@ -668,12 +601,9 @@ class Parser():
668 601  
669 602 DICOM standard tag (0x0010, 0x2297) was used.
670 603 """
671   - tag = gdcm.Tag(0x0010,0x2297)
672   - ds = self.gdcm_reader.GetFile().GetDataSet()
673   - if ds.FindDataElement(tag):
674   - data = str(ds.GetDataElement(tag).GetValue())
675   - if (data):
676   - return data
  604 + data = self.data_image['0010']['2297']
  605 + if (data):
  606 + return data
677 607 return ""
678 608  
679 609 def GetPatientResponsibleRole(self):
... ... @@ -684,12 +614,9 @@ class Parser():
684 614  
685 615 DICOM standard tag (0x0010, 0x2298) was used.
686 616 """
687   - tag = gdcm.Tag(0x0010,0x2298)
688   - ds = self.gdcm_reader.GetFile().GetDataSet()
689   - if ds.FindDataElement(tag):
690   - data = str(ds.GetDataElement(tag).GetValue())
691   - if (data):
692   - return data
  617 + data = self.data_image['0010']['2298']
  618 + if (data):
  619 + return data
693 620 return ""
694 621  
695 622 def GetPatientResponsibleOrganization(self):
... ... @@ -700,12 +627,9 @@ class Parser():
700 627  
701 628 DICOM standard tag (0x0010, 0x2299) was used.
702 629 """
703   - tag = gdcm.Tag(0x0010,0x2299)
704   - ds = self.gdcm_reader.GetFile().GetDataSet()
705   - if ds.FindDataElement(tag):
706   - data = str(ds.GetDataElement(tag).GetValue())
707   - if (data):
708   - return data
  630 + data = self.data_image['0010']['2299']
  631 + if (data):
  632 + return data
709 633 return ""
710 634  
711 635 def GetPatientMedicalCondition(self):
... ... @@ -716,12 +640,9 @@ class Parser():
716 640  
717 641 DICOM standard tag (0x0010, 0x2000) was used.
718 642 """
719   - tag = gdcm.Tag(0x0010,0x2000)
720   - ds = self.gdcm_reader.GetFile().GetDataSet()
721   - if ds.FindDataElement(tag):
722   - data = str(ds.GetDataElement(tag).GetValue())
723   - if (data):
724   - return data
  643 + data = self.data_image['0010']['2000']
  644 + if (data):
  645 + return data
725 646 return ""
726 647  
727 648 def GetPatientContrastAllergies(self):
... ... @@ -732,12 +653,9 @@ class Parser():
732 653  
733 654 DICOM standard tag (0x0008, 0x2110) was used.
734 655 """
735   - tag = gdcm.Tag(0x0008, 0x2110)
736   - ds = self.gdcm_reader.GetFile().GetDataSet()
737   - if ds.FindDataElement(tag):
738   - data = str(ds.GetDataElement(tag).GetValue())
739   - if (data):
740   - return data
  656 + data = self.data_image['0008']['2110']
  657 + if (data):
  658 + return data
741 659 return ""
742 660  
743 661  
... ... @@ -749,14 +667,11 @@ class Parser():
749 667  
750 668 DICOM standard tag (0x0008, 0x0090) was used.
751 669 """
752   - tag = gdcm.Tag(0x0008,0x0090)
753   - ds = self.gdcm_reader.GetFile().GetDataSet()
754   - if ds.FindDataElement(tag):
755   - data = str(ds.GetDataElement(tag).GetValue())
756   - if data == "None":
757   - return ""
758   - if (data):
759   - return data
  670 + data = self.data_image['0008']['0090']
  671 + if data == "None":
  672 + return ""
  673 + if (data):
  674 + return data
760 675 return ""
761 676  
762 677  
... ... @@ -767,12 +682,9 @@ class Parser():
767 682  
768 683 DICOM standard tag (0x0008, 0x0092) was used.
769 684 """
770   - tag = gdcm.Tag(0x0008, 0x0092)
771   - ds = self.gdcm_reader.GetFile().GetDataSet()
772   - if ds.FindDataElement(tag):
773   - data = str(ds.GetDataElement(tag).GetValue())
774   - if (data):
775   - return data
  685 + data = self.data_image['0008']['0092']
  686 + if (data):
  687 + return data
776 688 return ""
777 689  
778 690 def GetPhysicianeReferringTelephone(self):
... ... @@ -782,12 +694,9 @@ class Parser():
782 694  
783 695 DICOM standard tag (0x0008, 0x0094) was used.
784 696 """
785   - tag = gdcm.Tag(0x0008, 0x0094)
786   - ds = self.gdcm_reader.GetFile().GetDataSet()
787   - if ds.FindDataElement(tag):
788   - data = str(ds.GetDataElement(tag).GetValue())
789   - if (data):
790   - return data
  697 + data = self.data_image['0008']['0094']
  698 + if (data):
  699 + return data
791 700 return ""
792 701  
793 702 def GetProtocolName(self):
... ... @@ -797,12 +706,9 @@ class Parser():
797 706  
798 707 DICOM standard tag (0x0018, 0x1030) was used.
799 708 """
800   - tag = gdcm.Tag(0x0018, 0x1030)
801   - ds = self.gdcm_reader.GetFile().GetDataSet()
802   - if ds.FindDataElement(tag):
803   - data = str(ds.GetDataElement(tag).GetValue())
804   - if (data):
805   - return data
  709 + data = self.data_image['0018']['1030']
  710 + if (data):
  711 + return data
806 712 return None
807 713  
808 714 def GetImageType(self):
... ... @@ -814,15 +720,12 @@ class Parser():
814 720  
815 721 Critical DICOM tag (0x0008, 0x0008). Cannot be editted.
816 722 """
817   - tag = gdcm.Tag(0x0008, 0x0008)
818   - ds = self.gdcm_reader.GetFile().GetDataSet()
819   - if ds.FindDataElement(tag):
820   - data = str(ds.GetDataElement(tag).GetValue())
821   - if (data):
822   - try:
823   - return data.split('\\')
824   - except(IndexError):
825   - return []
  723 + data = self.data_image['0008']['0008']
  724 + if (data):
  725 + try:
  726 + return data.split('\\')
  727 + except(IndexError):
  728 + return []
826 729 return []
827 730  
828 731 def GetSOPClassUID(self):
... ... @@ -833,12 +736,9 @@ class Parser():
833 736  
834 737 Critical DICOM tag (0x0008, 0x0016). Cannot be edited.
835 738 """
836   - tag = gdcm.Tag(0x0008, 0x0016)
837   - ds = self.gdcm_reader.GetFile().GetDataSet()
838   - if ds.FindDataElement(tag):
839   - data = str(ds.GetDataElement(tag).GetValue())
840   - if (data):
841   - return data
  739 + data = self.data_image['0008']['0016']
  740 + if (data):
  741 + return data
842 742 return ""
843 743  
844 744 def GetSOPInstanceUID(self):
... ... @@ -849,12 +749,9 @@ class Parser():
849 749  
850 750 Critical DICOM tag (0x0008, 0x0018). Cannot be edited.
851 751 """
852   - tag = gdcm.Tag(0x0008, 0x0018)
853   - ds = self.gdcm_reader.GetFile().GetDataSet()
854   - if ds.FindDataElement(tag):
855   - data = str(ds.GetDataElement(tag).GetValue())
856   - if (data):
857   - return data
  752 + data = self.data_image['0008']['0018']
  753 + if (data):
  754 + return data
858 755 return ""
859 756  
860 757 def GetSeriesDescription(self):
... ... @@ -863,12 +760,9 @@ class Parser():
863 760  
864 761 DICOM tag (0x0008, 0x1030). Cannot be edited.
865 762 """
866   - tag = gdcm.Tag(0x0008, 0x1030)
867   - ds = self.gdcm_reader.GetFile().GetDataSet()
868   - if ds.FindDataElement(tag):
869   - data = str(ds.GetDataElement(tag).GetValue())
870   - if (data):
871   - return data
  763 + data = self.data_image['0008']['1030']
  764 + if (data):
  765 + return data
872 766 return ""
873 767  
874 768 def GetStudyInstanceUID(self):
... ... @@ -879,12 +773,9 @@ class Parser():
879 773  
880 774 Critical DICOM Tag (0x0020,0x000D). Cannot be edited.
881 775 """
882   - tag = gdcm.Tag(0x0020,0x000D)
883   - ds = self.gdcm_reader.GetFile().GetDataSet()
884   - if ds.FindDataElement(tag):
885   - data = str(ds.GetDataElement(tag).GetValue())
886   - if (data):
887   - return data
  776 + data = self.data_image['0020']['000D']
  777 + if (data):
  778 + return data
888 779 return ""
889 780  
890 781 def GetImagePatientOrientation(self):
... ... @@ -898,12 +789,9 @@ class Parser():
898 789  
899 790 Critical DICOM tag (0x0020,0x0037). Cannot be edited.
900 791 """
901   - tag = gdcm.Tag(0x0020,0x0037)
902   - ds = self.gdcm_reader.GetFile().GetDataSet()
903   - if ds.FindDataElement(tag):
904   - data = str(ds.GetDataElement(tag).GetValue())
905   - if (data):
906   - return [float(value) for value in data.split('\\')]
  792 + data = self.data_image['0020']['0037']
  793 + if (data):
  794 + return [float(value) for value in data.split('\\')]
907 795 return [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]
908 796  
909 797 def GetImageColumnOrientation(self):
... ... @@ -915,12 +803,9 @@ class Parser():
915 803  
916 804 Critical DICOM tag (0x0020,0x0037). Cannot be edited.
917 805 """
918   - tag = gdcm.Tag(0x0020,0x0037)
919   - ds = self.gdcm_reader.GetFile().GetDataSet()
920   - if ds.FindDataElement(tag):
921   - data = str(ds.GetDataElement(tag).GetValue())
922   - if (data):
923   - return [float(value) for value in data.split('\\')[3:6]]
  806 + data = self.data_image['0020']['0037']
  807 + if (data):
  808 + return [float(value) for value in data.split('\\')[3:6]]
924 809 return [0.0, 1.0, 0.0]
925 810  
926 811 def GetImageRowOrientation(self):
... ... @@ -932,12 +817,9 @@ class Parser():
932 817  
933 818 Critical DICOM tag (0x0020,0x0037). Cannot be edited.
934 819 """
935   - tag = gdcm.Tag(0x0020,0x0037)
936   - ds = self.gdcm_reader.GetFile().GetDataSet()
937   - if ds.FindDataElement(tag):
938   - data = str(ds.GetDataElement(tag).GetValue())
939   - if (data):
940   - return [float(value) for value in data.split('\\')[0:3]]
  820 + data = self.data_image['0020']['0037']
  821 + if (data):
  822 + return [float(value) for value in data.split('\\')[0:3]]
941 823 return [1.0, 0.0, 0.0]
942 824  
943 825 def GetFrameReferenceUID(self):
... ... @@ -947,12 +829,9 @@ class Parser():
947 829  
948 830 Critical DICOM tag (0x0020,0x0052). Cannot be edited.
949 831 """
950   - tag = gdcm.Tag(0x0020,0x0052)
951   - ds = self.gdcm_reader.GetFile().GetDataSet()
952   - if ds.FindDataElement(tag):
953   - data = str(ds.GetDataElement(tag).GetValue())
954   - if (data):
955   - return data
  832 + data = self.data_image['0020']['0052']
  833 + if (data):
  834 + return data
956 835 return ""
957 836  
958 837 def GetImageSamplesPerPixel(self):
... ... @@ -1026,12 +905,12 @@ class Parser():
1026 905  
1027 906 DICOM standard tag (0x0018, 0x1030) was used.
1028 907 """
1029   - tag = gdcm.Tag(0x0018, 0x1030)
1030   - ds = self.gdcm_reader.GetFile().GetDataSet()
1031   - if ds.FindDataElement(tag):
1032   - data = str(ds.GetDataElement(tag).GetValue())
  908 + try:
  909 + data = self.data_image['0018']['1030']
1033 910 if (data):
1034 911 return data
  912 + except(KeyError):
  913 + return ""
1035 914 return ""
1036 915  
1037 916 def GetAcquisionSequence(self):
... ... @@ -1050,12 +929,9 @@ class Parser():
1050 929  
1051 930 Critical DICOM tag (0x0018, 0x0020). Cannot be edited.
1052 931 """
1053   - tag = gdcm.Tag(0x0018, 0x0020)
1054   - ds = self.gdcm_reader.GetFile().GetDataSet()
1055   - if ds.FindDataElement(tag):
1056   - data = str(ds.GetDataElement(tag).GetValue())
1057   - if (data):
1058   - return data
  932 + data = self.data_image['0018']['0020']
  933 + if (data):
  934 + return data
1059 935 return ""
1060 936  
1061 937 def GetInstitutionName(self):
... ... @@ -1065,14 +941,10 @@ class Parser():
1065 941  
1066 942 DICOM standard tag (0x0008, 0x0080) was used.
1067 943 """
1068   - tag = gdcm.Tag(0x0008, 0x0080)
1069   - ds = self.gdcm_reader.GetFile().GetDataSet()
1070   - if ds.FindDataElement(tag):
1071   - data = str(ds.GetDataElement(tag).GetValue())
1072   - if (data):
  944 + data = self.data_image['0008']['0080']
  945 + if (data):
1073 946 return data
1074   -
1075   - 0x0008, 0x0081
  947 + return ""
1076 948  
1077 949 def GetInstitutionAddress(self):
1078 950 """
... ... @@ -1083,12 +955,9 @@ class Parser():
1083 955  
1084 956 DICOM standard tag (0x0008, 0x0081) was used.
1085 957 """
1086   - tag = gdcm.Tag(0x0008, 0x0081)
1087   - ds = self.gdcm_reader.GetFile().GetDataSet()
1088   - if ds.FindDataElement(tag):
1089   - data = str(ds.GetDataElement(tag).GetValue())
1090   - if (data):
1091   - return data
  958 + data = self.data_image['0008']['0081']
  959 + if (data):
  960 + return data
1092 961 return ""
1093 962  
1094 963 def GetStudyInstanceUID(self):
... ... @@ -1099,12 +968,9 @@ class Parser():
1099 968  
1100 969 Critical DICOM tag (0x0020, 0x000D). Cannot be edited.
1101 970 """
1102   - tag = gdcm.Tag(0x0020, 0x000D)
1103   - ds = self.gdcm_reader.GetFile().GetDataSet()
1104   - if ds.FindDataElement(tag):
1105   - data = str(ds.GetDataElement(tag).GetValue())
1106   - if (data):
1107   - return data
  971 + data = self.data_image['0020']['000D']
  972 + if (data):
  973 + return data
1108 974 return ""
1109 975  
1110 976 def GetPatientOccupation(self):
... ... @@ -1114,12 +980,9 @@ class Parser():
1114 980  
1115 981 DICOM standard tag (0x0010,0x2180) was used.
1116 982 """
1117   - tag = gdcm.Tag(0x0010, 0x2180)
1118   - ds = self.gdcm_reader.GetFile().GetDataSet()
1119   - if ds.FindDataElement(tag):
1120   - data = str(ds.GetDataElement(tag).GetValue())
1121   - if (data):
1122   - return data
  983 + data = self.data_image['0010']['2180']
  984 + if (data):
  985 + return data
1123 986 return ""
1124 987  
1125 988 def _GetPixelRepresentation(self):
... ... @@ -1147,13 +1010,13 @@ class Parser():
1147 1010  
1148 1011 DICOM standard tag (0x0028, 0x0100) was used.
1149 1012 """
1150   - tag = gdcm.Tag(0x0028, 0x0100)
1151   - sf = gdcm.StringFilter()
1152   - sf.SetFile(self.gdcm_reader.GetFile())
1153   - res = sf.ToStringPair(tag)
1154   -
1155   - if (res[1]):
1156   - return int(res[1])
  1013 + #tag = gdcm.Tag(0x0028, 0x0100)
  1014 + #sf = gdcm.StringFilter()
  1015 + #sf.SetFile(self.gdcm_reader.GetFile())
  1016 + #res = sf.ToStringPair(tag)
  1017 + data = self.data_image['0028']['0100']
  1018 + if (data):
  1019 + return int(data)
1157 1020 return ""
1158 1021  
1159 1022  
... ... @@ -1166,12 +1029,9 @@ class Parser():
1166 1029 DICOM standard tag (0x0010,0x0030) was used.
1167 1030 """
1168 1031 # TODO: internationalize data
1169   - tag = gdcm.Tag(0x0010, 0x0030)
1170   - ds = self.gdcm_reader.GetFile().GetDataSet()
1171   - if ds.FindDataElement(tag):
1172   - data = ds.GetDataElement(tag).GetValue()
1173   - if (data) and (data != 'None'):
1174   - return self.__format_date(str(data))
  1032 + data = self.data_image['0010']['0030']
  1033 + if (data) and (data != 'None'):
  1034 + return self.__format_date(str(data))
1175 1035 return ""
1176 1036  
1177 1037  
... ... @@ -1182,12 +1042,9 @@ class Parser():
1182 1042  
1183 1043 DICOM standard tag (0x0020,0x0010) was used.
1184 1044 """
1185   - tag = gdcm.Tag(0x0020, 0x0010)
1186   - ds = self.gdcm_reader.GetFile().GetDataSet()
1187   - if ds.FindDataElement(tag):
1188   - data = ds.GetDataElement(tag).GetValue()
1189   - if (data):
1190   - return str(data)
  1045 + data = self.data_image['0020']['0010']
  1046 + if (data):
  1047 + return str(data)
1191 1048 return ""
1192 1049  
1193 1050 def GetAcquisitionGantryTilt(self):
... ... @@ -1198,14 +1055,9 @@ class Parser():
1198 1055  
1199 1056 DICOM standard tag (0x0018,0x1120) was used.
1200 1057 """
1201   - tag = gdcm.Tag(0x0018, 0x1120)
1202   - ds = self.gdcm_reader.GetFile().GetDataSet()
1203   - if ds.FindDataElement(tag):
1204   - data = ds.GetDataElement(tag).GetValue()
1205   -
1206   - if (data):
1207   - return float(str(data))
1208   -
  1058 + data = self.data_image['0018']['1120']
  1059 + if (data):
  1060 + return float(str(data))
1209 1061 return 0.0
1210 1062  
1211 1063 def GetPatientGender(self):
... ... @@ -1218,13 +1070,9 @@ class Parser():
1218 1070  
1219 1071 DICOM standard tag (0x0010,0x0040) was used.
1220 1072 """
1221   - tag = gdcm.Tag(0x0010, 0x0040)
1222   - ds = self.gdcm_reader.GetFile().GetDataSet()
1223   - if ds.FindDataElement(tag):
1224   - data = ds.GetDataElement(tag).GetValue()
1225   -
1226   - if (data):
1227   - return str(data)
  1073 + data = self.data_image['0010']['0040']
  1074 + if (data):
  1075 + return str(data)
1228 1076 return ""
1229 1077  
1230 1078 def GetPatientAge(self):
... ... @@ -1235,17 +1083,13 @@ class Parser():
1235 1083  
1236 1084 DICOM standard tag (0x0010, 0x1010) was used.
1237 1085 """
1238   - tag = gdcm.Tag(0x0010, 0x1010)
1239   - ds = self.gdcm_reader.GetFile().GetDataSet()
1240   - if ds.FindDataElement(tag):
1241   - data = str(ds.GetDataElement(tag).GetValue())
1242   -
1243   - if (data):
1244   - age = (data.split('Y')[0])
1245   - try:
1246   - return int(age)
1247   - except ValueError:
1248   - return age
  1086 + data = self.data_image['0010']['0010']
  1087 + if (data):
  1088 + age = (data.split('Y')[0])
  1089 + try:
  1090 + return int(age)
  1091 + except ValueError:
  1092 + return age
1249 1093 return ""
1250 1094  
1251 1095 def GetPatientName(self):
... ... @@ -1255,16 +1099,12 @@ class Parser():
1255 1099  
1256 1100 DICOM standard tag (0x0010,0x0010) was used.
1257 1101 """
1258   - tag = gdcm.Tag(0x0010, 0x0010)
1259   - ds = self.gdcm_reader.GetFile().GetDataSet()
1260   - if ds.FindDataElement(tag):
1261   - data = str(ds.GetDataElement(tag).GetValue())
1262   -
1263   - if (data):
1264   - name = data.strip()
1265   - encoding = self.GetEncoding()
1266   - # Returns a unicode decoded in the own dicom encoding
1267   - return name.decode(encoding)
  1102 + data = self.data_image['0010']['0010']
  1103 + if (data):
  1104 + name = data.strip()
  1105 + encoding = self.GetEncoding()
  1106 + # Returns a unicode decoded in the own dicom encoding
  1107 + return name.decode(encoding)
1268 1108 return ""
1269 1109  
1270 1110 def GetPatientID(self):
... ... @@ -1275,15 +1115,11 @@ class Parser():
1275 1115  
1276 1116 DICOM standard tag (0x0010,0x0020) was used.
1277 1117 """
1278   - tag = gdcm.Tag(0x0010, 0x0020)
1279   - ds = self.gdcm_reader.GetFile().GetDataSet()
1280   - if ds.FindDataElement(tag):
1281   - data = str(ds.GetDataElement(tag).GetValue())
1282   -
1283   - if (data):
1284   - encoding = self.GetEncoding()
1285   - # Returns a unicode decoded in the own dicom encoding
1286   - return data.decode(encoding)
  1118 + data = self.data_image['0010']['0020']
  1119 + if (data):
  1120 + encoding = self.GetEncoding()
  1121 + # Returns a unicode decoded in the own dicom encoding
  1122 + return data.decode(encoding)
1287 1123 return ""
1288 1124  
1289 1125  
... ... @@ -1295,13 +1131,9 @@ class Parser():
1295 1131  
1296 1132 DICOM standard tag (0x0018,0x1151) was used.
1297 1133 """
1298   - tag = gdcm.Tag(0x0018, 0x1151)
1299   - ds = self.gdcm_reader.GetFile().GetDataSet()
1300   - if ds.FindDataElement(tag):
1301   - data = str(ds.GetDataElement(tag).GetValue())
1302   -
1303   - if (data):
1304   - return data
  1134 + data = self.data_image['0018']['1151']
  1135 + if (data):
  1136 + return data
1305 1137 return ""
1306 1138  
1307 1139 def GetExposureTime(self):
... ... @@ -1312,13 +1144,9 @@ class Parser():
1312 1144  
1313 1145 DICOM standard tag (0x0018, 0x1152) was used.
1314 1146 """
1315   - tag = gdcm.Tag(0x0018, 0x1152)
1316   - ds = self.gdcm_reader.GetFile().GetDataSet()
1317   - if ds.FindDataElement(tag):
1318   - data = str(ds.GetDataElement(tag).GetValue())
1319   -
1320   - if (data):
1321   - return float(data)
  1147 + data = self.data_image['0018']['1152']
  1148 + if (data):
  1149 + return float(data)
1322 1150 return ""
1323 1151  
1324 1152 def GetEquipmentKVP(self):
... ... @@ -1329,13 +1157,9 @@ class Parser():
1329 1157  
1330 1158 DICOM standard tag (0x0018,0x0060) was used.
1331 1159 """
1332   - tag = gdcm.Tag(0x0018, 0x0060)
1333   - ds = self.gdcm_reader.GetFile().GetDataSet()
1334   - if ds.FindDataElement(tag):
1335   - data = str(ds.GetDataElement(tag).GetValue())
1336   -
1337   - if (data):
1338   - return float(data)
  1160 + data = self.data_image['0018']['0060']
  1161 + if (data):
  1162 + return float(data)
1339 1163 return ""
1340 1164  
1341 1165 def GetImageThickness(self):
... ... @@ -1346,12 +1170,9 @@ class Parser():
1346 1170  
1347 1171 DICOM standard tag (0x0018,0x0050) was used.
1348 1172 """
1349   - tag = gdcm.Tag(0x0018, 0x0050)
1350   - ds = self.gdcm_reader.GetFile().GetDataSet()
1351   - if ds.FindDataElement(tag):
1352   - data = str(ds.GetDataElement(tag).GetValue())
1353   - if (data):
1354   - return float(data)
  1173 + data = self.data_image['0018']['0050']
  1174 + if (data):
  1175 + return float(data)
1355 1176 return 0
1356 1177  
1357 1178 def GetImageConvolutionKernel(self):
... ... @@ -1364,13 +1185,9 @@ class Parser():
1364 1185  
1365 1186 DICOM standard tag (0x0018,0x1210) was used.
1366 1187 """
1367   - tag = gdcm.Tag(0x0018, 0x1210)
1368   - ds = self.gdcm_reader.GetFile().GetDataSet()
1369   - if ds.FindDataElement(tag):
1370   - data = str(ds.GetDataElement(tag).GetValue())
1371   -
1372   - if (data):
1373   - return data
  1188 + data = self.data_image['0018']['1210']
  1189 + if (data):
  1190 + return data
1374 1191 return ""
1375 1192  
1376 1193 def GetEquipmentInstitutionName(self):
... ... @@ -1381,13 +1198,9 @@ class Parser():
1381 1198  
1382 1199 DICOM standard tag (0x0008,0x0080) was used.
1383 1200 """
1384   - tag = gdcm.Tag(0x0008, 0x0080)
1385   - ds = self.gdcm_reader.GetFile().GetDataSet()
1386   - if ds.FindDataElement(tag):
1387   - data = str(ds.GetDataElement(tag).GetValue())
1388   -
1389   - if (data):
1390   - return data
  1201 + data = self.data_image['0008']['0080']
  1202 + if (data):
  1203 + return data
1391 1204 return ""
1392 1205  
1393 1206 def GetStationName(self):
... ... @@ -1398,13 +1211,9 @@ class Parser():
1398 1211  
1399 1212 DICOM standard tag (0x0008, 0x1010) was used.
1400 1213 """
1401   - tag = gdcm.Tag(0x0008, 0x1010)
1402   - ds = self.gdcm_reader.GetFile().GetDataSet()
1403   - if ds.FindDataElement(tag):
1404   - data = str(ds.GetDataElement(tag).GetValue())
1405   -
1406   - if (data):
1407   - return data
  1214 + data = self.data_image['0008']['1010']
  1215 + if (data):
  1216 + return data
1408 1217 return ""
1409 1218  
1410 1219 def GetManufacturerModelName(self):
... ... @@ -1415,13 +1224,9 @@ class Parser():
1415 1224  
1416 1225 DICOM standard tag (0x0008,0x1090) was used.
1417 1226 """
1418   - tag = gdcm.Tag(0x0008, 0x1090)
1419   - ds = self.gdcm_reader.GetFile().GetDataSet()
1420   - if ds.FindDataElement(tag):
1421   - data = str(ds.GetDataElement(tag).GetValue())
1422   -
1423   - if (data):
1424   - return data
  1227 + data = self.data_image['0008']['1090']
  1228 + if (data):
  1229 + return data
1425 1230 return ""
1426 1231  
1427 1232 def GetEquipmentManufacturer(self):
... ... @@ -1431,13 +1236,9 @@ class Parser():
1431 1236  
1432 1237 DICOM standard tag (0x0008, 0x1010) was used.
1433 1238 """
1434   - tag = gdcm.Tag(0x0008, 0x1010)
1435   - ds = self.gdcm_reader.GetFile().GetDataSet()
1436   - if ds.FindDataElement(tag):
1437   - data = str(ds.GetDataElement(tag).GetValue())
1438   -
1439   - if (data):
1440   - return data
  1239 + data = self.data_image['0008']['1010']
  1240 + if (data):
  1241 + return data
1441 1242 return ""
1442 1243  
1443 1244 def GetAcquisitionModality(self):
... ... @@ -1449,13 +1250,9 @@ class Parser():
1449 1250  
1450 1251 DICOM standard tag (0x0008,0x0060) was used.
1451 1252 """
1452   - tag = gdcm.Tag(0x0008, 0x0060)
1453   - ds = self.gdcm_reader.GetFile().GetDataSet()
1454   - if ds.FindDataElement(tag):
1455   - data = str(ds.GetDataElement(tag).GetValue())
1456   -
1457   - if (data):
1458   - return data
  1253 + data = self.data_image['0008']['0060']
  1254 + if (data):
  1255 + return data
1459 1256 return ""
1460 1257  
1461 1258  
... ... @@ -1466,13 +1263,13 @@ class Parser():
1466 1263  
1467 1264 DICOM standard tag (0x0020,0x0013) was used.
1468 1265 """
1469   - tag = gdcm.Tag(0x0020, 0x0013)
1470   - ds = self.gdcm_reader.GetFile().GetDataSet()
1471   - if ds.FindDataElement(tag):
1472   - data = str(ds.GetDataElement(tag).GetValue())
  1266 + try:
  1267 + data = self.data_image['0020']['0013']
  1268 + except(KeyError):
  1269 + return ""
1473 1270  
1474   - if (data):
1475   - return int(data)
  1271 + if (data):
  1272 + return int(data)
1476 1273 return ""
1477 1274  
1478 1275 def GetStudyDescription(self):
... ... @@ -1482,15 +1279,13 @@ class Parser():
1482 1279  
1483 1280 DICOM standard tag (0x0008,0x1030) was used.
1484 1281 """
1485   - tag = gdcm.Tag(0x0008, 0x1030)
1486   - ds = self.gdcm_reader.GetFile().GetDataSet()
1487   - if ds.FindDataElement(tag):
1488   - data = str(ds.GetDataElement(tag).GetValue())
1489   -
  1282 + try:
  1283 + data = self.data_image['0008']['1030']
1490 1284 if (data):
1491 1285 encoding = self.GetEncoding()
1492 1286 return data.decode(encoding)
1493   - return ""
  1287 + except(KeyError):
  1288 + return ""
1494 1289  
1495 1290 def GetStudyAdmittingDiagnosis(self):
1496 1291 """
... ... @@ -1515,27 +1310,27 @@ class Parser():
1515 1310 Return a string with a description of the series.
1516 1311 DICOM standard tag (0x0008, 0x103E) was used.
1517 1312 """
1518   - tag = gdcm.Tag(0x0008, 0x103E)
1519   - ds = self.gdcm_reader.GetFile().GetDataSet()
1520   - if ds.FindDataElement(tag):
1521   - data = str(ds.GetDataElement(tag).GetValue())
  1313 + try:
  1314 + data = self.data_image['0008']['103E']
1522 1315 if data == "None":
1523 1316 return _("unnamed")
1524 1317 if (data):
1525 1318 return data
1526   - return _("unnamed")
  1319 + except(KeyError):
  1320 + return _("unnamed")
1527 1321  
1528 1322 def GetImageTime(self):
1529 1323 """
1530 1324 Return the image time.
1531 1325 DICOM standard tag (0x0008,0x0033) was used.
1532 1326 """
1533   - tag = gdcm.Tag(0x0008, 0x0033)
1534   - ds = self.gdcm_reader.GetFile().GetDataSet()
1535   - if ds.FindDataElement(tag):
1536   - date = str(ds.GetDataElement(tag).GetValue())
1537   - if (date) and (date != 'None'):
1538   - return self.__format_time(date)
  1327 + try:
  1328 + data = self.data_image['0008']['0033']
  1329 + except(KeyError):
  1330 + return ""
  1331 +
  1332 + if (data) and (data != 'None'):
  1333 + return self.__format_time(data)
1539 1334 return ""
1540 1335  
1541 1336 def GetAcquisitionTime(self):
... ... @@ -1543,12 +1338,9 @@ class Parser():
1543 1338 Return the acquisition time.
1544 1339 DICOM standard tag (0x0008,0x032) was used.
1545 1340 """
1546   - tag = gdcm.Tag(0x0008, 0x0032)
1547   - ds = self.gdcm_reader.GetFile().GetDataSet()
1548   - if ds.FindDataElement(tag):
1549   - data = str(ds.GetDataElement(tag).GetValue())
1550   - if (data):
1551   - return self.__format_time(data)
  1341 + data = self.data_image['0008']['0032']
  1342 + if (data):
  1343 + return self.__format_time(data)
1552 1344 return ""
1553 1345  
1554 1346 def GetSerieNumber(self):
... ... @@ -1556,12 +1348,9 @@ class Parser():
1556 1348 Return the serie number
1557 1349 DICOM standard tag (0x0020, 0x0011) was used.
1558 1350 """
1559   - tag = gdcm.Tag(0x0020, 0x0011)
1560   - ds = self.gdcm_reader.GetFile().GetDataSet()
1561   - if ds.FindDataElement(tag):
1562   - data = str(ds.GetDataElement(tag).GetValue())
1563   - if (data):
1564   - return data
  1351 + data = self.data_image['0020']['0011']
  1352 + if (data):
  1353 + return data
1565 1354 return ""
1566 1355  
1567 1356 def GetEncoding(self):
... ... @@ -1569,18 +1358,18 @@ class Parser():
1569 1358 Return the dicom encoding
1570 1359 DICOM standard tag (0x0008, 0x0005) was used.
1571 1360 """
1572   - tag = gdcm.Tag(0x0008, 0x0005)
1573   - ds = self.gdcm_reader.GetFile().GetDataSet()
1574   - if ds.FindDataElement(tag):
1575   - encoding = str(ds.GetDataElement(tag).GetValue())
  1361 + try:
  1362 + encoding = self.data_image['0008']['0005']
  1363 + except(KeyError):
  1364 + return 'ISO_IR_100'
1576 1365  
1577   - if encoding != None and encoding != "None":
  1366 + if encoding != None and encoding != "None":
1578 1367  
1579   - #Problem with 0051 anonymized
1580   - if (encoding.split(":"))[0] == "Loaded":
1581   - return 'ISO_IR 100'
1582   - else:
1583   - return encoding
  1368 + #Problem with 0051 anonymized
  1369 + if (encoding.split(":"))[0] == "Loaded":
  1370 + return 'ISO_IR 100'
  1371 + else:
  1372 + return encoding
1584 1373 return 'ISO_IR 100'
1585 1374  
1586 1375  
... ... @@ -1924,7 +1713,7 @@ class Image(object):
1924 1713 self.time = parser.GetImageTime()
1925 1714 self.type = parser.GetImageType()
1926 1715 self.size = (parser.GetDimensionX(), parser.GetDimensionY())
1927   - self.imagedata = parser.GetImageData()
  1716 + #self.imagedata = parser.GetImageData()
1928 1717 self.bits_allocad = parser._GetBitsAllocated()
1929 1718  
1930 1719 if (parser.GetImageThickness()):
... ...
invesalius/reader/dicom_grouper.py
... ... @@ -96,7 +96,7 @@ class DicomGroup:
96 96 return False
97 97 else:
98 98 self.slices_dict[dicom.image.number] = dicom
99   - self.nslices += 1
  99 + self.nslices += 1
100 100 return True
101 101  
102 102 def GetList(self):
... ...
invesalius/reader/dicom_reader.py
... ... @@ -31,6 +31,12 @@ import dicom
31 31 import dicom_grouper
32 32 import session
33 33  
  34 +import glob
  35 +import utils
  36 +
  37 +
  38 +import plistlib
  39 +
34 40 def ReadDicomGroup(dir_):
35 41  
36 42 patient_group = GetDicomGroups(dir_)
... ... @@ -73,6 +79,7 @@ def SortFiles(filelist, dicom):
73 79  
74 80 return filelist
75 81  
  82 +"""
76 83 class LoadDicom(threading.Thread):
77 84 def __init__(self, grouper, q, l):
78 85 threading.Thread.__init__(self)
... ... @@ -87,66 +94,242 @@ class LoadDicom(threading.Thread):
87 94 if not filepath:
88 95 break
89 96 parser = dicom.Parser()
90   - if parser.SetFileName(filepath):
  97 + #if parser.SetFileName(filepath):
91 98 dcm = dicom.Dicom()
92 99 self.l.acquire()
93 100 dcm.SetParser(parser)
94 101 grouper.AddFile(dcm)
95 102 self.l.release()
  103 +"""
  104 +
  105 +"""
  106 +class LoadDicom():
  107 + def __init__(self, grouper, dicom_dict):
  108 + self.grouper = grouper
  109 + self.dicom_dict = dicom_dict
  110 +
  111 + def Start(self):
  112 + images = self.dicom_dict['data']
  113 +
  114 + grouper = self.grouper
  115 +
  116 + for x in images.keys():
  117 + data_image = images[x]
  118 + parser = dicom.Parser()
  119 + parser.SetDataImage(data_image)
  120 +
  121 + dcm = dicom.Dicom()
  122 + dcm.SetParser(parser)
  123 + grouper.AddFile(dcm)
  124 +
  125 +
  126 + #while 1:
  127 + #filepath = path
  128 + #if not filepath:
  129 + # break
  130 + #parser = dicom.Parser()
  131 + #if parser.SetFileName(filepath):
  132 + # dcm = dicom.Dicom()
  133 + # dcm.SetParser(parser)
  134 + # grouper.AddFile(dcm)
  135 +"""
96 136  
97 137  
98   -def yGetDicomGroups(directory, recursive=True, gui=True):
  138 +def GetImageOrientationLabel(filename):
  139 + """
  140 + Return Label regarding the orientation of
  141 + an image. (AXIAL, SAGITTAL, CORONAL,
  142 + OBLIQUE or UNKNOWN)
  143 + """
  144 + gdcm_reader = gdcm.ImageReader()
  145 + gdcm_reader.SetFileName(filename)
  146 +
  147 + img = gdcm_reader.GetImage()
  148 + direc_cosines = img.GetDirectionCosines()
  149 + orientation = gdcm.Orientation()
  150 + try:
  151 + type = orientation.GetType(tuple(direc_cosines))
  152 + except TypeError:
  153 + type = orientation.GetType(direc_cosines)
  154 + label = orientation.GetLabel(type)
  155 +
  156 + if (label):
  157 + return label
  158 + else:
  159 + return ""
  160 +
  161 +
  162 +def GetDicomGroups(directory, recursive=True, gui=True):
99 163 """
100 164 Return all full paths to DICOM files inside given directory.
101 165 """
102 166 nfiles = 0
103 167 # Find total number of files
104 168 if recursive:
105   - for dirpath, dirnames, filenames in os.walk(directory):
106   - nfiles += len(filenames)
107   - else:
108   - dirpath, dirnames, filenames = os.walk(directory)
109   - nfiles = len(filenames)
110   -
111   - counter = 0
112   - grouper = dicom_grouper.DicomPatientGrouper()
113   - q = Queue.Queue()
114   - l = threading.Lock()
115   - threads = []
116   - for i in xrange(cpu_count()):
117   - t = LoadDicom(grouper, q, l)
118   - t.start()
119   - threads.append(t)
120   - # Retrieve only DICOM files, splited into groups
121   - if recursive:
122   - for dirpath, dirnames, filenames in os.walk(directory):
123   - for name in filenames:
124   - filepath = os.path.join(dirpath, name)
125   - counter += 1
126   - if gui:
127   - yield (counter,nfiles)
128   - q.put(filepath)
  169 +
  170 + folders = [f[0] for f in os.walk(directory)]
  171 +
  172 + tag_labels = {}
  173 + main_dict = {}
  174 + dict_file = {}
  175 +
  176 + for folder in folders:
  177 + files = glob.glob(os.path.join(folder, "*"))
  178 +
  179 + for filename in files:
  180 +
  181 + # Read file
  182 + reader = gdcm.Reader()
  183 + reader.SetFileName(filename)
  184 +
  185 + if (reader.Read()):
  186 +
  187 + file = reader.GetFile()
  188 +
  189 + # Retrieve data set
  190 + dataSet = file.GetDataSet()
  191 +
  192 + # Retrieve header
  193 + header = file.GetHeader()
  194 + stf = gdcm.StringFilter()
  195 +
  196 + field_dict = {}
  197 + data_dict = {}
  198 +
  199 +
  200 + tag = gdcm.Tag(0x0008, 0x0005)
  201 + ds = reader.GetFile().GetDataSet()
  202 + if ds.FindDataElement(tag):
  203 + encoding = str(ds.GetDataElement(tag).GetValue())
  204 + if not(encoding != None and encoding != "None" and encoding != "Loaded"):
  205 + encoding = "ISO_IR 100"
  206 + else:
  207 + encoding = "ISO_IR_100"
  208 + # Iterate through the Header
  209 + iterator = header.GetDES().begin()
  210 + while (not iterator.equal(header.GetDES().end())):
  211 + dataElement = iterator.next()
  212 + stf.SetFile(file)
  213 + tag = dataElement.GetTag()
  214 + data = stf.ToStringPair(tag)
  215 + stag = tag.PrintAsPipeSeparatedString()
  216 +
  217 + group = stag.split("|")[0][1:]
  218 + field = stag.split("|")[1][:-1]
  219 + tag_labels[stag] = data[0]
  220 +
  221 + if not group in data_dict.keys():
  222 + data_dict[group] = {}
  223 +
  224 + if not(utils.VerifyInvalidPListCharacter(data[1])):
  225 + data_dict[group][field] = data[1].decode(encoding)
  226 + else:
  227 + data_dict[group][field] = "Invalid Character"
  228 +
  229 +
  230 + # Iterate through the Data set
  231 + iterator = dataSet.GetDES().begin()
  232 + while (not iterator.equal(dataSet.GetDES().end())):
  233 + dataElement = iterator.next()
  234 +
  235 + stf.SetFile(file)
  236 + tag = dataElement.GetTag()
  237 + data = stf.ToStringPair(tag)
  238 + stag = tag.PrintAsPipeSeparatedString()
  239 +
  240 + group = stag.split("|")[0][1:]
  241 + field = stag.split("|")[1][:-1]
  242 + tag_labels[stag] = data[0]
  243 +
  244 + if not group in data_dict.keys():
  245 + data_dict[group] = {}
  246 +
  247 + if not(utils.VerifyInvalidPListCharacter(data[1])):
  248 + data_dict[group][field] = data[1].decode(encoding)
  249 + else:
  250 + data_dict[group][field] = "Invalid Character"
  251 +
  252 +
  253 +
  254 + # ---------- Refactory --------------------------------------
  255 + data_dict['invesalius'] = {'orientation_label' : GetImageOrientationLabel(filename)}
  256 +
  257 + # -------------------------------------------------------------
  258 +
  259 + dict_file[os.path.abspath(filename)] = data_dict
  260 +
  261 + main_dict = dict(
  262 + data = dict_file,
  263 + labels = tag_labels)
  264 +
  265 + #plistlib.writePlist(main_dict, ".//teste.plist")
  266 +
  267 + images = main_dict['data']
  268 + grouper = dicom_grouper.DicomPatientGrouper()
  269 +
  270 + for x in images.keys():
  271 + data_image = images[x]
  272 +
  273 + if (data_image['0002']['0002'] != "1.2.840.10008.1.3.10"): #DICOMDIR
  274 + parser = dicom.Parser()
  275 + parser.SetDataImage(data_image, x)
  276 +
  277 + dcm = dicom.Dicom()
  278 + dcm.SetParser(parser)
  279 + grouper.AddFile(dcm)
  280 +
  281 + grouper.Update()
  282 +
  283 +
  284 +
  285 + return grouper.GetPatientsGroups()
  286 + #print "_____________________________________"
  287 + #print dirpath
  288 + #nfiles += len(filenames)
129 289 else:
130   - dirpath, dirnames, filenames = os.walk(directory)
131   - for name in filenames:
132   - filepath = str(os.path.join(dirpath, name))
133   - counter += 1
134   - if gui:
135   - yield (counter,nfiles)
136   - q.put(filepath)
  290 + pass
  291 + #dirpath, dirnames, filenames = os.walk(directory)
  292 + #nfiles = len(filenames)
137 293  
138   - for t in threads:
139   - q.put(0)
  294 + #counter = 0
  295 + #grouper = dicom_grouper.DicomPatientGrouper()
  296 + #q = Queue.Queue()
  297 + #l = threading.Lock()
  298 + #threads = []
  299 + #for i in xrange(cpu_count()):
  300 + # t = LoadDicom(grouper, q, l)
  301 + # t.start()
  302 + # threads.append(t)
  303 + ## Retrieve only DICOM files, splited into groups
  304 + #if recursive:
  305 + # for dirpath, dirnames, filenames in os.walk(directory):
  306 + # for name in filenames:
  307 + # filepath = os.path.join(dirpath, name)
  308 + # counter += 1
  309 + # if gui:
  310 + # yield (counter,nfiles)
  311 + # q.put(filepath)
  312 + #else:
  313 + # dirpath, dirnames, filenames = os.walk(directory)
  314 + # for name in filenames:
  315 + # filepath = str(os.path.join(dirpath, name))
  316 + # counter += 1
  317 + # if gui:
  318 + # yield (counter,nfiles)
  319 + # q.put(filepath)
140 320  
141   - for t in threads:
142   - t.join()
  321 + #for t in threads:
  322 + # q.put(0)
143 323  
144   - #TODO: Is this commented update necessary?
145   - #grouper.Update()
146   - yield grouper.GetPatientsGroups()
  324 + #for t in threads:
  325 + # t.join()
147 326  
148   -def GetDicomGroups(directory, recursive=True):
149   - return yGetDicomGroups(directory, recursive, gui=False).next()
  327 + ##TODO: Is this commented update necessary?
  328 + ##grouper.Update()
  329 + #yield grouper.GetPatientsGroups()
  330 +
  331 +#def GetDicomGroups(directory, recursive=True):
  332 +# return yGetDicomGroups(directory, recursive, gui=False).next()
150 333  
151 334  
152 335 class ProgressDicomReader:
... ... @@ -158,7 +341,7 @@ class ProgressDicomReader:
158 341 self.stoped = True
159 342  
160 343 def SetWindowEvent(self, frame):
161   - self.frame = frame
  344 + self.frame = frame
162 345  
163 346 def SetDirectoryPath(self, path,recursive=True):
164 347 self.running = True
... ... @@ -193,4 +376,5 @@ class ProgressDicomReader:
193 376 #the load, ensure that dicomdialog is closed
194 377 if(self.stoped):
195 378 self.UpdateLoadFileProgress(None)
196   - self.stoped = False
  379 + self.stoped = False
  380 +
... ...
invesalius/utils.py
... ... @@ -19,7 +19,7 @@
19 19 import platform
20 20 import sigar
21 21 import sys
22   -
  22 +import re
23 23  
24 24 def debug(error_str):
25 25 """
... ... @@ -79,6 +79,21 @@ def next_copy_name(original_name, names_list):
79 79 return next_copy
80 80  
81 81  
  82 +def VerifyInvalidPListCharacter(text):
  83 + #print text
  84 + #text = unicode(text)
  85 +
  86 + _controlCharPat = re.compile(
  87 + r"[\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0b\x0c\x0e\x0f"
  88 + r"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f]")
  89 + m = _controlCharPat.search(text)
  90 +
  91 + if m is not None:
  92 + return True
  93 + else:
  94 + False
  95 +
  96 +
82 97 #http://www.garyrobinson.net/2004/03/python_singleto.html
83 98 # Gary Robinson
84 99 class Singleton(type):
... ...