Browse Source

Changes from onko-nix

NIX User 4 years ago
parent
commit
6401fe26bc
3 changed files with 91 additions and 25 deletions
  1. 43 6
      DICOMtools/exportDicom.py
  2. 30 18
      DICOMtools/loadDicom.py
  3. 18 1
      labkeyBrowser/slicerNetwork.py

+ 43 - 6
DICOMtools/exportDicom.py

@@ -318,14 +318,25 @@ class exportDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic
        except KeyError:
            pass
 
-
+ 
 
   def generateDicom(self, volumeNode, metadata, fdir):
 
        cliparameters={}
-       tagList=['patientName','patientComments','studyDate','studyTime','studyDescription',
-            'modality','manufacturer','model','seriesDescription',
-            'seriesNumber','seriesDate','seriesTime','contentDate','contentTime']
+       tagList=['patientName',
+               'patientComments',
+               'studyDate',
+               'studyTime',
+               'studyDescription',
+               'modality',
+               'manufacturer',
+               'model',
+               'seriesDescription',
+               'seriesNumber',
+               'seriesDate',
+               'seriesTime',
+               'contentDate',
+               'contentTime']
 
        for tag in tagList:
            self.setTagValue(cliparameters,metadata,tag)
@@ -341,7 +352,10 @@ class exportDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic
 
 
        cliparameters['dicomDirectory']=fdir
-       cliparameters['dicomPrefix']='IMG'
+       try:
+            cliparameters['dicomPrefix']=metadata['fileName']
+       except KeyError:
+            cliparameters['dicomPrefix']='IMG'
        cliparameters['inputVolume']=volumeNode.GetID()
 
        print("[CLI]SeriesInstanceUID: {}").format(cliparameters['seriesInstanceUID'])
@@ -412,4 +426,27 @@ def getTimeCode(x):
     hour=x.strftime("%H")
     hour=re.sub('^0','',hour)
     return hour+x.strftime("%M%S")
-  
+ 
+def generateExportMap():
+    exportMap={}
+    exportMap['patientName']='UNKONWN'
+    exportMap['patientComments']='NONE'
+    exportMap['studyDate']='UNKNOWN'
+    exportMap['studyTime']='UNKNOWN'
+    exportMap['studyDescription']='NONE'
+    exportMap['modality']='UNKNOWN'
+    exportMap['manufacturer']='UNKNOWN'
+    exportMap['model']='UNKNOWN'
+    exportMap['seriesDescription']='NONE'
+    exportMap['seriesNumber']='0'
+    exportMap['seriesDate']='UNKNOWN'
+    exportMap['seriesTime']='UNKNOWN'
+    exportMap['contentDate']='UNKNOWN'
+    exportMap['contentTime']='UNKNOWN'
+    exportMap['patientId']='UNKNOWN'
+    exportMap['studyId']='UNKNOWN'
+    exportMap['seriesInstanceUid']='0.0.0.0'
+    exportMap['studyInstanceUid']='0.0.0.0'
+    exportMap['frameOfReferenceInstanceUid']='0.0.0.0'
+    return exportMap
+

+ 30 - 18
DICOMtools/loadDicom.py

@@ -93,13 +93,15 @@ class loadDicomWidget(slicer.ScriptedLoadableModule.ScriptedLoadableModuleWidget
 
 class loadDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic):
     def __init__(self,parent):
-        slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic.__init__(self, parent)
+        slicer.ScriptedLoadableModule.\
+                ScriptedLoadableModuleLogic.__init__(self, parent)
 
         self.tag={
               'sopInstanceUid' : {'tag':"0008,0018",'VR':'UI'},
               'studyDate': {'tag':"0008,0020",'VR':'DA'},
               'studyTime': {'tag':"0008,0030",'VR':'TM'},
               'seriesTime': {'tag':"0008,0031",'VR':'TM'},
+              'acquisitionTime':{'tag':"0008,0032",'VR':'TM'},
               'modality': {'tag':"0008,0060",'VR':'CS'},
               'presentationIntentType': {'tag':"0008,0068",'VR':'CS'},
               'manufacturer': {'tag':"0008,0070",'VR':'LO'},
@@ -119,10 +121,12 @@ class loadDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic):
               'xRayTubeCurrent': {'tag':"0018,1151",'VR':'IS'},
               'exposure': {'tag':"0018,1152",'VR':'IS'},
               'imagerPixelSpacing': {'tag':"0018,1164",'VR':'DS'},
+              'anodeTargetMaterial':{'tag':"0018,1191",'VR':'CS'},
               'bodyPartThickness': {'tag':"0018,11a0",'VR':'DS'},
               'compressionForce': {'tag':"0018,11a2",'VR':'DS'},
               'viewPosition': {'tag':"0018,5101",'VR':'CS'},
               'fieldOfViewHorizontalFlip': {'tag':"0018,7034",'VR':'CS'},
+              'filterMaterial':{'tag':"0018,7050",'VR':'CS'},
               'studyInstanceUid': {'tag':"0020,000d",'VR':'UI'},
               'seriesInstanceUid': {'tag':"0020,000e",'VR':'UI'},
               'studyId': {'tag':"0020,0010",'VR':'SH'},
@@ -132,6 +136,8 @@ class loadDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic):
               'imageLaterality': {'tag':"0020,0062",'VR':'CS'},
               'imagesInAcquisition': {'tag':"0020,1002",'VR':'IS'},
               'photometricInterpretation': {'tag':"0028,0004",'VR':'CS'},
+              'organDose':{'tag':"0040,0316",'VR':'DS'},
+              'entranceDoseInmGy':{'tag':"0040,8302",'VR':'DS'},
               'reconstructionMethod': {'tag':"0054,1103",'VR':'LO'}
         }
         self.tagPyDicom={
@@ -163,10 +169,6 @@ class loadDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic):
               'instanceNumber': 0x00200013,
               'frameOfReferenceInstanceUid': 0x00200052
               }
-      #new_dict_items={
-      #        0x001811a0: ('DS','BodyPartThickness','Body Part Thickness')
-      #}
-      #dicom.datadict.add_dict_entries(new_dict_items)
         self.local=False
         self.useDicomModify=False
 
@@ -254,7 +256,7 @@ class loadDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic):
 
         return self.applyFilterFile(loadable.files[0],filter,nodeMetadata)
     
-    def applyFilterFile(self,file,filter,nodeMetadata,shell=False):
+    def applyFilterFile(self,file,filter,nodeMetadata,shell=False,debug=False):
         #apply filter to file. Return true if file matches prescribed filter and
         #false otherwise
 
@@ -278,8 +280,9 @@ class loadDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic):
 
             if not filter[key]==None:
                 if not fileValue==filter[key]:
-                    #print("File {} failed for tag {}: {}/{}").format(\
-                    #    file,key,fileValue,filter[key])
+                    if debug:
+                        print("File {} failed for tag {}: {}/{}").format(\
+                            file,key,fileValue,filter[key])
                     filterOK=False
                     break
 
@@ -309,9 +312,10 @@ class loadDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic):
         
 
     def loadVolumes(self,sNet,dir,filter,doRemove=True):
-    #returns all series from the directory, each as a separate node in a node list
-    #filter is a dictionary of speciifed dicom values, if filter(key)=None, that values
-    #get set, if it isn't, the file gets checked for a match
+#returns all series from the directory, each as a separate node in a node list
+#filter is a dictionary of speciifed dicom values, 
+#if filter(key)=None, that value
+#get set, if it isn't, the file gets checked for a match
 
         print("Loading dir {}").format(dir)
         dicomFiles=self.listdir(sNet,dir)
@@ -319,19 +323,25 @@ class loadDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic):
         filelist=[]
         for f in dicomFiles:
             localPath=self.getfile(sNet,f)
+            filelist.append(localPath)
+            if not self.useDicomModify:
+                continue
+            if dicomModify==None:
+                continue
+
             f0=localPath
             f1=f0+"1"
-            if not dicomModify==None:
-                try:
-                    subprocess.call(dicomModify+" "+f0+" "+f1+" && mv "+f1+" "+f0+";", shell=False)
-                except OSError:
-                    print("dicomModify failed")
-            filelist.append(localPath)
+            try:
+                cmd=dicomModify+" "+f0+" "+f1+" && mv "+f1+" "+f0+";"
+                subprocess.call(cmd, shell=False)
+            except OSError:
+                print("dicomModify failed")
 
         try:
             loadables=self.volumePlugin.examineForImport([filelist])
         except AttributeError:
-            self.volumePlugin=slicer.modules.dicomPlugins['DICOMScalarVolumePlugin']()
+            self.volumePlugin=\
+                    slicer.modules.dicomPlugins['DICOMScalarVolumePlugin']()
             loadables=self.volumePlugin.examineForImport([filelist])
 
 
@@ -367,6 +377,8 @@ class loadDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic):
                 volume={'node':volumeNode,'metadata':nodeMetadata}
                 volumeNodes.append(volume)
 
+        self.volumePlugin.loadableCache.clear()
+
         if self.local:
             return volumeNodes
 

+ 18 - 1
labkeyBrowser/slicerNetwork.py

@@ -1,5 +1,6 @@
 import qt
 import urllib2
+import socket
 import ssl
 import cookielib
 import xml.etree.ElementTree as ET
@@ -27,6 +28,12 @@ class MethodRequest(urllib2.Request):
             return self._method
         return urllib2.Request.get_method(self, *args, **kwargs)
 
+
+class ConnectionError(Exception):
+    def __init__(self,message):
+        self.message=message
+
+
 class slicerNetwork(slicer.ScriptedLoadableModule.ScriptedLoadableModule):
     def __init__(self, parent):
         slicer.ScriptedLoadableModule.ScriptedLoadableModule.__init__(self,parent)
@@ -542,7 +549,17 @@ class labkeyURIHandler(slicer.vtkURIHandler):
 
         with open(localPath, 'rb') as f:
             data=f.read()
-        self.put(remotePath,data)
+
+        itry=0
+        while itry<10:
+            try:
+                self.put(remotePath,data)
+                break
+            except (socket.error,urllib2.URLError):
+                print("[{}] Failed to execute put(), retry".format(itry))
+                itry=itry+1
+        if itry==10:
+            raise ConnectionError('Could not copy file, number of tries exceeded') 
 
     #was loadDir
     def DownloadDirToCache(self, relativePath):