|
@@ -83,155 +83,234 @@ class parseDicomLogic(ScriptedLoadableModuleLogic):
|
|
|
|
|
|
return ['NULL',0]
|
|
|
|
|
|
- def read_dynamic_SPECT(self,mypath):
|
|
|
- axisShift=(2,1,0)
|
|
|
+ def readMasterFile(self,g):
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ try:
|
|
|
+ plan = dicom.read_file(g)
|
|
|
+ except:
|
|
|
+ print ("{}: Not a dicom file").format(g)
|
|
|
+ return False
|
|
|
+
|
|
|
+ try:
|
|
|
+ self.nframe=plan[0x0019,0x10a5].value;
|
|
|
+ except:
|
|
|
+ print ("{}: Not a master file").format(g)
|
|
|
+ return False
|
|
|
+ if not (type(self.nframe) is list) :
|
|
|
+ print("nframe not a list")
|
|
|
+ return False
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ for i in range(1,len(self.nframe)):
|
|
|
+ self.nframe[i]+=self.nframe[i-1]
|
|
|
+
|
|
|
+ self.frame_start=plan[0x0019,0x10a7].value
|
|
|
+ self.frame_stop=plan[0x0019,0x10a8].value
|
|
|
+ self.frame_duration=plan[0x0019,0x10a9].value
|
|
|
+
|
|
|
+ self.frame_time=np.zeros(self.nframe[-1]);
|
|
|
+ self.frame_data=np.empty([1,1,1,self.nframe[-1]])
|
|
|
+ self.center = [0,0,0]
|
|
|
+ self.pixel_size =[0,0,0]
|
|
|
+ self.frame_orientation=[0,0,0,0,0,0]
|
|
|
+ return True
|
|
|
+
|
|
|
+ def readNMFile(self,g):
|
|
|
+
|
|
|
+ try:
|
|
|
+ plan = dicom.read_file(g)
|
|
|
+ except:
|
|
|
+ print ("{}: Not a dicom file").format(g)
|
|
|
+ return False
|
|
|
+
|
|
|
+ try:
|
|
|
+ pf=plan[0x0018,0x5020]
|
|
|
+ except:
|
|
|
+ print("Not a NM file. Exiting")
|
|
|
+ return False
|
|
|
+
|
|
|
+ try:
|
|
|
+ phase=plan[0x0035,0x1005].value
|
|
|
+ cycle=plan[0x0035,0x1004].value
|
|
|
+ except:
|
|
|
+ print("Missing phase/cycle values")
|
|
|
+ return False
|
|
|
+
|
|
|
+
|
|
|
+ off=0
|
|
|
+ if phase > 1:
|
|
|
+ off=self.nframe[phase-2]
|
|
|
+ ifi=off+cycle-1
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ self.frame_time[ifi]=0.5*(self.frame_start[ifi]+self.frame_stop[ifi]);
|
|
|
+
|
|
|
+ print "({},{}) converted to {} at {} for {}".format(
|
|
|
+ phase,cycle,ifi,self.frame_time[ifi],self.frame_duration[ifi])
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if self.frame_data.shape[0] == 1:
|
|
|
+ sh=np.transpose(plan.pixel_array,self.axisShift).shape;
|
|
|
+ sh=list(sh)
|
|
|
+ sh.append(self.nframe[-1])
|
|
|
+ self.frame_data=np.empty(sh)
|
|
|
+ print "Setting frame_data to",sh
|
|
|
+
|
|
|
+
|
|
|
+ pixel_size_read=[plan.PixelSpacing[0],plan.PixelSpacing[1],
|
|
|
+ plan.SliceThickness]
|
|
|
+
|
|
|
+ for i in range(0,3):
|
|
|
+ if self.pixel_size[i] == 0:
|
|
|
+ self.pixel_size[i] = float(pixel_size_read[i])
|
|
|
+ if abs(self.pixel_size[i]-pixel_size_read[i]) > 1e-3:
|
|
|
+ print 'Pixel size mismatch {.2f}/{.2f}'.format(self.pixel_size[i],
|
|
|
+ pixel_size_read[i])
|
|
|
+
|
|
|
+ center_read=plan.DetectorInformationSequence[0].ImagePositionPatient
|
|
|
+ print "Stored center at ({0},{1},{2})".format(self.center[0],self.center[1],self.center[2])
|
|
|
+ print "Read center at ({0},{1},{2})".format(center_read[0],center_read[1],center_read[2])
|
|
|
+ for i in range(0,3):
|
|
|
+ if self.center[i] == 0:
|
|
|
+ self.center[i] = float(center_read[i])
|
|
|
+ if abs(self.center[i]-center_read[i]) > 1e-3:
|
|
|
+ print 'Image center mismatch {.2f}/{.2f}'.format(self.center[i],
|
|
|
+ center_read[i])
|
|
|
+
|
|
|
+ frame_orientation_read=plan.DetectorInformationSequence[0].ImageOrientationPatient
|
|
|
+ for i in range(0,6):
|
|
|
+ if self.frame_orientation[i] == 0:
|
|
|
+ self.frame_orientation[i] = float(frame_orientation_read[i])
|
|
|
+ if abs(self.frame_orientation[i]-frame_orientation_read[i]) > 1e-3:
|
|
|
+ print 'Image orientation mismatch {.2f}/{.2f}'.format(
|
|
|
+ self.frame_rotation[i], frame_orientation_read[i])
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ self.frame_data[:,:,:,ifi]=np.transpose(plan.pixel_array,self.axisShift)
|
|
|
+
|
|
|
+ return True
|
|
|
+
|
|
|
+ def readCTFile(self,g):
|
|
|
+
|
|
|
+ try:
|
|
|
+ plan = dicom.read_file(g)
|
|
|
+ except:
|
|
|
+ print ("{}: Not a dicom file").format(g)
|
|
|
+ return False
|
|
|
+
|
|
|
+
|
|
|
+ if plan.Modality != 'CT':
|
|
|
+ print ('{}: Not a CT file').format(g)
|
|
|
+ return False
|
|
|
|
|
|
- origin=re.sub('([^:/])://(.*)$',r'\1',mypath)
|
|
|
- onlyfiles=self.filelist(mypath)
|
|
|
- for f in onlyfiles:
|
|
|
- print '{}:'.format(f)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ try:
|
|
|
+ iType=plan.ImageType
|
|
|
+ except:
|
|
|
+ print "Image type not found"
|
|
|
+ return False
|
|
|
|
|
|
- g,ok=self.getfile(origin,f)
|
|
|
- if not(ok):
|
|
|
- return
|
|
|
+ if iType[3].find("SPI")<0:
|
|
|
+ print "Not a spiral image"
|
|
|
+ return False
|
|
|
|
|
|
- try:
|
|
|
- plan = dicom.read_file(g)
|
|
|
- except:
|
|
|
- print ("Not a dicom file")
|
|
|
- continue
|
|
|
- try:
|
|
|
- nframe=plan[0x0019,0x10a5].value;
|
|
|
- except:
|
|
|
- print ("Tag not found;")
|
|
|
- continue
|
|
|
- if not (type(nframe) is list) :
|
|
|
- print("nframe not a list")
|
|
|
- continue
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- print('Found master file')
|
|
|
|
|
|
- for i in range(1,len(nframe)):
|
|
|
- nframe[i]+=nframe[i-1]
|
|
|
+ print '.',
|
|
|
+ self.ct_data.append(plan.pixel_array)
|
|
|
+ self.ct_idx.append(plan.InstanceNumber)
|
|
|
+ self.ct_z.append(plan.ImagePositionPatient[2])
|
|
|
+
|
|
|
+ pixel_size_read=[plan.PixelSpacing[0],plan.PixelSpacing[1],
|
|
|
+ plan.SliceThickness]
|
|
|
+
|
|
|
+
|
|
|
+ for i in range(0,3):
|
|
|
+ if self.ct_pixel_size[i] == 0:
|
|
|
+ self.ct_pixel_size[i] = float(pixel_size_read[i])
|
|
|
+ if abs(self.ct_pixel_size[i]-pixel_size_read[i]) > 1e-3:
|
|
|
+ print 'Pixel size mismatch {.2f}/{.2f}'.format(self.ct_pixel_size[i],
|
|
|
+ pixel_size_read[i])
|
|
|
|
|
|
- print(nframe)
|
|
|
+ for i in range(0,2):
|
|
|
+ if self.ct_center[i] == 0:
|
|
|
+ self.ct_center[i] = float(plan.ImagePositionPatient[i])
|
|
|
+ if abs(self.ct_center[i]-plan.ImagePositionPatient[i]) > 1e-3:
|
|
|
+ print 'Image center mismatch {.2f}/{.2f}'.format(self.ct_center[i],
|
|
|
+ plan.ImagePositionPatient[i])
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+ if plan.ImagePositionPatient[2]<self.ct_center[2]:
|
|
|
+ self.ct_center[2]=plan.ImagePositionPatient[2]
|
|
|
|
|
|
- frame_start=plan[0x0019,0x10a7].value
|
|
|
- frame_stop=plan[0x0019,0x10a8].value
|
|
|
- frame_duration=plan[0x0019,0x10a9].value
|
|
|
- break
|
|
|
-
|
|
|
-
|
|
|
+ for i in range(0,6):
|
|
|
+ if self.ct_orientation[i] == 0:
|
|
|
+ self.ct_orientation[i] = float(plan.ImageOrientationPatient[i])
|
|
|
+ if abs(self.ct_orientation[i]-plan.ImageOrientationPatient[i]) > 1e-3:
|
|
|
+ print 'Image orientation mismatch {0:.2f}/{1:.2f}'.format(self.ct_orientation[i],
|
|
|
+ plan.ImageOrientationPatient[i])
|
|
|
|
|
|
-
|
|
|
- frame_time=np.zeros(nframe[-1]);
|
|
|
- frame_data=np.empty([1,1,1,nframe[-1]])
|
|
|
- center = [0,0,0]
|
|
|
- pixel_size =[0,0,0]
|
|
|
- frame_orientation=[0,0,0,0,0,0]
|
|
|
+ return True
|
|
|
+
|
|
|
+ def readMasterDirectory(self,mypath):
|
|
|
+ self.axisShift=(2,1,0)
|
|
|
+
|
|
|
+ origin=re.sub('([^:/])://(.*)$',r'\1',mypath)
|
|
|
+
|
|
|
+ onlyfiles=self.filelist(mypath)
|
|
|
for f in onlyfiles:
|
|
|
+ print '{}:'.format(f)
|
|
|
|
|
|
g,ok=self.getfile(origin,f)
|
|
|
if not(ok):
|
|
|
- continue
|
|
|
+ return
|
|
|
|
|
|
- try:
|
|
|
- plan = dicom.read_file(g)
|
|
|
- except:
|
|
|
- print ("Not a dicom file")
|
|
|
- continue
|
|
|
+ if self.readMasterFile(g):
|
|
|
+ break
|
|
|
|
|
|
- try:
|
|
|
- pf=plan[0x0018,0x5020]
|
|
|
- except:
|
|
|
- print ("ProcessingFunction not found")
|
|
|
- continue
|
|
|
|
|
|
- try:
|
|
|
- phase=plan[0x0035,0x1005].value
|
|
|
- cycle=plan[0x0035,0x1004].value
|
|
|
- except:
|
|
|
- print ("Phase/Cycle tag not found")
|
|
|
- continue
|
|
|
-
|
|
|
-
|
|
|
- off=0
|
|
|
- if phase > 1:
|
|
|
- off=nframe[phase-2]
|
|
|
- ifi=off+cycle-1
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- frame_time[ifi]=0.5*(frame_start[ifi]+frame_stop[ifi]);
|
|
|
-
|
|
|
- print "({},{}) converted to {} at {} for {}".format(
|
|
|
- phase,cycle,ifi,frame_time[ifi],frame_duration[ifi])
|
|
|
-
|
|
|
-
|
|
|
- if frame_data.shape[0] == 1:
|
|
|
- sh=np.transpose(plan.pixel_array,axisShift).shape;
|
|
|
- sh=list(sh)
|
|
|
- sh.append(nframe[-1])
|
|
|
- frame_data=np.empty(sh)
|
|
|
- print "Setting frame_data to",sh
|
|
|
-
|
|
|
-
|
|
|
- pixel_size_read=[plan.PixelSpacing[0],plan.PixelSpacing[1],
|
|
|
- plan.SliceThickness]
|
|
|
-
|
|
|
- for i in range(0,3):
|
|
|
- if pixel_size[i] == 0:
|
|
|
- pixel_size[i] = float(pixel_size_read[i])
|
|
|
- if abs(pixel_size[i]-pixel_size_read[i]) > 1e-3:
|
|
|
- print 'Pixel size mismatch {.2f}/{.2f}'.format(pixel_size[i],
|
|
|
- pixel_size_read[i])
|
|
|
+ def readNMDirectory(self,mypath):
|
|
|
|
|
|
- center_read=plan.DetectorInformationSequence[0].ImagePositionPatient
|
|
|
- print "Stored center at ({0},{1},{2})".format(center[0],center[1],center[2])
|
|
|
- print "Read center at ({0},{1},{2})".format(center_read[0],center_read[1],center_read[2])
|
|
|
- for i in range(0,3):
|
|
|
- if center[i] == 0:
|
|
|
- center[i] = float(center_read[i])
|
|
|
- if abs(center[i]-center_read[i]) > 1e-3:
|
|
|
- print 'Image center mismatch {.2f}/{.2f}'.format(center[i],
|
|
|
- center_read[i])
|
|
|
+ onlyfiles=self.filelist(mypath)
|
|
|
+ origin=re.sub('([^:/])://(.*)$',r'\1',mypath)
|
|
|
+ for f in onlyfiles:
|
|
|
|
|
|
- frame_orientation_read=plan.DetectorInformationSequence[0].ImageOrientationPatient
|
|
|
- for i in range(0,6):
|
|
|
- if frame_orientation[i] == 0:
|
|
|
- frame_orientation[i] = float(frame_orientation_read[i])
|
|
|
- if abs(frame_orientation[i]-frame_orientation_read[i]) > 1e-3:
|
|
|
- print 'Image orientation mismatch {.2f}/{.2f}'.format(
|
|
|
- frame_rotation[i], frame_orientation_read[i])
|
|
|
+ g,ok=self.getfile(origin,f)
|
|
|
+ if not(ok):
|
|
|
+ continue
|
|
|
|
|
|
+ self.readNMFile(g)
|
|
|
|
|
|
|
|
|
|
|
|
- frame_data[:,:,:,ifi]=np.transpose(plan.pixel_array,axisShift)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
|
|
|
- return [frame_data,frame_time,center,pixel_size,frame_orientation]
|
|
|
+ return [self.frame_data,self.frame_time,self.center,
|
|
|
+ self.pixel_size,self.frame_orientation]
|
|
|
|
|
|
- def read_CT(self,mypath):
|
|
|
+ def readCTDirectory(self,mypath):
|
|
|
onlyfiles=self.filelist(mypath)
|
|
|
origin=re.sub('([^:/])://(.*)$',r'\1',mypath)
|
|
|
|
|
|
- ct_data = []
|
|
|
- ct_idx = []
|
|
|
- ct_z = []
|
|
|
- ct_pixel_size = [0,0,0]
|
|
|
- ct_center = [0,0,0]
|
|
|
- ct_center[2]=1e30
|
|
|
- ct_orientation=[0,0,0,0,0,0]
|
|
|
+ self.ct_data = []
|
|
|
+ self.ct_idx = []
|
|
|
+ self.ct_z = []
|
|
|
+ self.ct_pixel_size = [0,0,0]
|
|
|
+ self.ct_center = [0,0,0]
|
|
|
+ self.ct_center[2]=1e30
|
|
|
+ self.ct_orientation=[0,0,0,0,0,0]
|
|
|
for f in onlyfiles:
|
|
|
print '{}:'.format(f)
|
|
|
|
|
@@ -239,78 +318,18 @@ class parseDicomLogic(ScriptedLoadableModuleLogic):
|
|
|
if not(ok):
|
|
|
return
|
|
|
|
|
|
- try:
|
|
|
- plan = dicom.read_file(g)
|
|
|
- except:
|
|
|
- print ("Not a dicom file")
|
|
|
- continue
|
|
|
-
|
|
|
- if plan.Modality != 'CT':
|
|
|
- print ('Not a CT file')
|
|
|
- continue
|
|
|
+ self.readCTFile(g)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- try:
|
|
|
- iType=plan.ImageType
|
|
|
- except:
|
|
|
- print "Image type not found"
|
|
|
- continue;
|
|
|
-
|
|
|
- if iType[3].find("SPI")<0:
|
|
|
- print "Not a spiral image"
|
|
|
- continue;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- print '.',
|
|
|
- ct_data.append(plan.pixel_array)
|
|
|
- ct_idx.append(plan.InstanceNumber)
|
|
|
- ct_z.append(plan.ImagePositionPatient[2])
|
|
|
-
|
|
|
- pixel_size_read=[plan.PixelSpacing[0],plan.PixelSpacing[1],
|
|
|
- plan.SliceThickness]
|
|
|
-
|
|
|
-
|
|
|
- for i in range(0,3):
|
|
|
- if ct_pixel_size[i] == 0:
|
|
|
- ct_pixel_size[i] = float(pixel_size_read[i])
|
|
|
- if abs(ct_pixel_size[i]-pixel_size_read[i]) > 1e-3:
|
|
|
- print 'Pixel size mismatch {.2f}/{.2f}'.format(ct_pixel_size[i],
|
|
|
- pixel_size_read[i])
|
|
|
-
|
|
|
- for i in range(0,2):
|
|
|
- if ct_center[i] == 0:
|
|
|
- ct_center[i] = float(plan.ImagePositionPatient[i])
|
|
|
- if abs(ct_center[i]-plan.ImagePositionPatient[i]) > 1e-3:
|
|
|
- print 'Image center mismatch {.2f}/{.2f}'.format(ct_center[i],
|
|
|
- plan.ImagePositionPatient[i])
|
|
|
-
|
|
|
-
|
|
|
- if plan.ImagePositionPatient[2]<ct_center[2]:
|
|
|
- ct_center[2]=plan.ImagePositionPatient[2]
|
|
|
-
|
|
|
- for i in range(0,6):
|
|
|
- if ct_orientation[i] == 0:
|
|
|
- ct_orientation[i] = float(plan.ImageOrientationPatient[i])
|
|
|
- if abs(ct_orientation[i]-plan.ImageOrientationPatient[i]) > 1e-3:
|
|
|
- print 'Image orientation mismatch {0:.2f}/{1:.2f}'.format(ct_orientation[i],
|
|
|
- plan.ImageOrientationPatient[i])
|
|
|
-
|
|
|
- print
|
|
|
- nz=len(ct_idx)
|
|
|
-
|
|
|
-
|
|
|
- sh=ct_data[-1].shape
|
|
|
+ nz=len(self.ct_idx)
|
|
|
+
|
|
|
+
|
|
|
+ sh=self.ct_data[-1].shape
|
|
|
sh_list=list(sh)
|
|
|
sh_list.append(nz)
|
|
|
data_array=np.zeros(sh_list)
|
|
|
|
|
|
for k in range(0,nz):
|
|
|
- kp=int(np.round((ct_z[k]-ct_center[2])/ct_pixel_size[2]))
|
|
|
- data_array[:,:,kp]=np.transpose(ct_data[k])
|
|
|
+ kp=int(np.round((self.ct_z[k]-self.ct_center[2])/self.ct_pixel_size[2]))
|
|
|
+ data_array[:,:,kp]=np.transpose(self.ct_data[k])
|
|
|
|
|
|
- return data_array,ct_center,ct_pixel_size,ct_orientation
|
|
|
+ return data_array,self.ct_center,self.ct_pixel_size,self.ct_orientation
|