瀏覽代碼

loadDicom: Separating tag value parsing to getTagValue from dcmdump exec in dicomValue, also controlled removal of downloaded files which is safe-guarded against removing local files on server

Andrej Studen 5 年之前
父節點
當前提交
64a22505ce
共有 1 個文件被更改,包括 66 次插入43 次删除
  1. 66 43
      DICOMtools/loadDicom.py

+ 66 - 43
DICOMtools/loadDicom.py

@@ -242,13 +242,24 @@ class loadDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic):
         #if value is set, a match is attempted and result reported in return value
         #if value is set, a match is attempted and result reported in return value
         #all filters should match for true output
         #all filters should match for true output
 
 
+        return applyFilterFile(self,loadable.files[0],filter,nodeMetadata)
+    
+    def applyFilterFile(self,file,filter,nodeMetadata,shell=False):
+        #apply filter to file. Return true if file matches prescribed filter and
+        #false otherwise
+
+        #filter is a directory with keys equal to pre-specified values listed above
+        #if value associated to key equals None, that value gets set in nodeMetadata
+        #if value is set, a match is attempted and result reported in return value
+        #all filters should match for true output
+
         filterOK=True
         filterOK=True
         
         
         for key in filter:
         for key in filter:
             try:
             try:
-                fileValue=dicomValue(loadable.files[0],self.tag[key]['tag'],self.tag[key]['seqTag'])
+                fileValue=dicomValue(file,self.tag[key]['tag'],self.tag[key]['seqTag'],shell=shell)
             except KeyError:
             except KeyError:
-                fileValue=dicomValue(loadable.files[0],self.tag[key]['tag'])
+                fileValue=dicomValue(file,self.tag[key]['tag'],shell=shell)
 
 
 
 
             if filter[key]=="SeriesLabel":
             if filter[key]=="SeriesLabel":
@@ -258,7 +269,7 @@ class loadDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic):
             if not filter[key]==None:
             if not filter[key]==None:
                 if not fileValue==filter[key]:
                 if not fileValue==filter[key]:
                     print("File {} failed for tag {}: {}/{}").format(
                     print("File {} failed for tag {}: {}/{}").format(
-                        loadable.files[0],key,fileValue,filter[key])
+                        file,key,fileValue,filter[key])
                     filterOK=False
                     filterOK=False
                     break
                     break
 
 
@@ -280,6 +291,11 @@ class loadDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic):
         if self.local:
         if self.local:
             return file
             return file
         return sNet.DownloadFileToCache(file)
         return sNet.DownloadFileToCache(file)
+
+    def removeLocal(self,localFile):
+        if self.local:
+            return
+        os.remove(localFile)
         
         
 
 
     def loadVolumes(self,sNet,dir,filter,doRemove=True):
     def loadVolumes(self,sNet,dir,filter,doRemove=True):
@@ -405,48 +421,55 @@ def dicomValue(file,tag,seqTag=None,shell=False):
     dcmdump=os.path.join(os.environ['SLICER_HOME'],"bin","dcmdump")
     dcmdump=os.path.join(os.environ['SLICER_HOME'],"bin","dcmdump")
     try:
     try:
         out=subprocess.check_output([dcmdump,'+p','+P',tag,file],shell=shell)
         out=subprocess.check_output([dcmdump,'+p','+P',tag,file],shell=shell)
-        if debug:
-            print("Tag {} Line '{}'").format(tag,out)
-        if len(out)==0:
-            return out
-
-        #parse output
-        longTag="^\({}\)".format(tag)
-        #combine sequence and actual tags to long tags
-        if not seqTag==None:
-            if debug:
-                print("Tag:{} seqTag:{}").format(tag,seqTag)
-            longTag="^\({}\).\({}\)".format(seqTag,tag)
-        #parse multi-match outputs which appear as several lines
-        lst=out.split('\n')
-        #pick the values
-        pattern=r'^.*\[(.*)\].*$'
-        #extract tag values
-        rpl=[re.sub(pattern,r'\1',f) for f in lst]
-        #logical whether the line can be matched to typical dcmdump output
-        mtchPattern=[re.match(pattern,f) for f in lst]
-        #find matching tags
-        mtchTag=[re.match(longTag,f) for f in lst]
-        
-        #weed out non-matching lines and lines not matching longTag
-        mtch=[None if x==None or y==None else x \
-                for x,y in zip(mtchTag,mtchPattern)]
-        #set values 
-        out=[x for y,x in zip(mtch,rpl) if not y==None]
-        if len(out)==0:
-            return ''
-        #return first match
-        out=out[0]
-        
-        if debug:
-            print("Tag {} Parsed value {}").format(tag,out)
-        #split output to lists if values are DICOM lists
-        if out.find('\\')>-1:
-            out=out.split('\\')
-
-        return out
     except subprocess.CalledProcessError as e:
     except subprocess.CalledProcessError as e:
         return None
         return None
+    
+    lst=out.split('\n')
+    return getTagValue(lst,tag,seqTag)
+    
+def getTagValue(lst,tag,seqTag=None):
+    #report tag value from a list lst of lines reported by dcmdump        
+    if debug:
+        print("Tag {} Line '{}'").format(tag,out)
+    if len(out)==0:
+        return out
+
+    #parse output
+    longTag="^\({}\)".format(tag)
+    #combine sequence and actual tags to long tags
+    if not seqTag==None:
+        if debug:
+            print("Tag:{} seqTag:{}").format(tag,seqTag)
+        longTag="^\({}\).\({}\)".format(seqTag,tag)
+    #parse multi-match outputs which appear as several lines
+    lst=out.split('\n')
+    #pick the values
+    pattern=r'^.*\[(.*)\].*$'
+    #extract tag values
+    rpl=[re.sub(pattern,r'\1',f) for f in lst]
+    #logical whether the line can be matched to typical dcmdump output
+    mtchPattern=[re.match(pattern,f) for f in lst]
+    #find matching tags
+    mtchTag=[re.match(longTag,f) for f in lst]
+    
+    #weed out non-matching lines and lines not matching longTag
+    mtch=[None if x==None or y==None else x \
+            for x,y in zip(mtchTag,mtchPattern)]
+    #set values 
+    out=[x for y,x in zip(mtch,rpl) if not y==None]
+    if len(out)==0:
+        return ''
+    #return first match
+    out=out[0]
+    
+    if debug:
+        print("Tag {} Parsed value {}").format(tag,out)
+    #split output to lists if values are DICOM lists
+    if out.find('\\')>-1:
+        out=out.split('\\')
+
+    return out
+        
 
 
 def clearNodes():
 def clearNodes():
     nodes=[]
     nodes=[]