Browse Source

Adding interface to labkey

Andrej Studen 7 years ago
parent
commit
0b5a478f6a
2 changed files with 83 additions and 18 deletions
  1. 19 10
      cardiacSPECT/cardiacSPECT.py
  2. 64 8
      parseDicom.py

+ 19 - 10
cardiacSPECT/cardiacSPECT.py

@@ -5,6 +5,7 @@ import vtk, qt, ctk, slicer
 from slicer.ScriptedLoadableModule import *
 import logging
 import vtkInterface as vi
+import parseDicom as pd
 #
 # cardiacSPECT
 #
@@ -50,12 +51,20 @@ class cardiacSPECTWidget(ScriptedLoadableModuleWidget):
     # Layout within the sample collapsible button
     dataFormLayout = qt.QFormLayout(dataButton)
 
+    pathGuess="file://"+os.environ['HOME']+"/SPECT"
+    self.dataPath=qt.QLineEdit(pathGuess)
+    dataFormLayout.addRow("Data location",self.dataPath)
+
+    browseButton = qt.QPushButton("Browse")
+    browseButton.toolTip="Set file location"
+    dataFormLayout.addRow("Change data location",browseButton)
+    browseButton.connect('clicked(bool)',self.onBrowseButtonClicked)
+
     dataLoadButton = qt.QPushButton("Load")
     dataLoadButton.toolTip="Load data from DICOM"
-    dataFormLayout.addWidget(dataLoadButton)
+    dataFormLayout.addRow("Data",dataLoadButton)
     dataLoadButton.connect('clicked(bool)',self.onDataLoadButtonClicked)
 
-    # Set local var as instance attribute
     self.dataLoadButton = dataLoadButton
 
     # Add vertical spacer
@@ -181,6 +190,13 @@ class cardiacSPECTWidget(ScriptedLoadableModuleWidget):
     #logic = cardiacSPECTLogic()
     #imageThreshold = self.imageThresholdSliderWidget.value
 
+  def onBrowseButtonClicked(self):
+      startDir=self.dataPath.text
+      inputDir=qt.QFileDialog.getExistingDirectory(None,
+          'Select DICOM directory',startDir)
+
+      self.dataPath.setText("file://"+inputDir)
+
   def onDataLoadButtonClicked(self):
       self.logic.loadData(self)
 
@@ -280,15 +296,8 @@ class cardiacSPECTLogic(ScriptedLoadableModuleLogic):
   """
 
   def loadData(self,widget):
-    startDir=os.environ['HOME']
-    inputDir=qt.QFileDialog.getExistingDirectory(None,
-        'Select DICOM directory',startDir)
-
-    #use another script from the same file
-    mypath=os.environ['HOME']+'/software/build/dynamicSPECT'
-    sys.path.append(mypath)
-    import parseDicom as pd
 
+    inputDir=str(widget.dataPath.text)
     self.frame_data, self.frame_time, self.frame_origin, \
         self.frame_pixel_size, self.frame_orientation=pd.read_dynamic_SPECT(inputDir)
 

+ 64 - 8
parseDicom.py

@@ -3,6 +3,8 @@ import sys
 import dicom
 import numpy as np
 import re
+import slicer
+
 #rom os import listdir
 #from os.path import isfile, join
 #onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
@@ -28,16 +30,60 @@ class parseDicom:
     """ # replace with organization, grant and thanks.
     self.parent = parent
 
-def read_dynamic_SPECT(mypath):
+def filelist(mypath):
 #mypath=os.environ['PWD']
 #list files
-    onlyfiles = [f for f in os.listdir(mypath)
-            if os.path.isfile(os.path.join(mypath, f))]
+    if mypath.find('labkey://')==0:
+        print("Using labkey")
+        labkeyPath=re.sub('labkey://','',mypath)
+        #not sure if labkey is available, so try it
+        net=slicer.modules.labkeySlicerPythonExtensionWidget.network
+        print("Found network")
+        url=slicer.modules.labkeySlicerPythonExtensionWidget.serverURL.text
+        print("Seting url={}".format(url))
+        files=net.listDir(str(url),labkeyPath)
+        print files
+
+    if mypath.find('file://')==0:
+        print("Using local files")
+        localPath=re.sub('file://','',mypath)
+        files = [os.path.join(localPath,f) for f in os.listdir(localPath)
+            if os.path.isfile(os.path.join(localPath, f))]
+
+    return files
+
+def getfile(origin,f):
+
+    if origin.find('labkey')==0:
+        try:
+            #not sure if labkey is available, but try it
+            net=slicer.modules.labkeySlicerPythonExtensionWidget.network
+            print("Using labkey")
+            url=slicer.modules.labkeySlicerPythonExtensionWidget.serverURL.text
+            print("Sever:{0}, file:{1}".format(url,f))
+            return [net.readFile(str(url),f),1]
+        except:
+            print('Could not access labkey. Exiting')
+            return ['NULL',0]
 
+    if origin.find('file')==0:
+        print("Using local directory")
+        return [f,1]
+
+    return ['NULL',0]
+
+def read_dynamic_SPECT(mypath):
+    origin=re.sub('([^:/])://(.*)$',r'\1',mypath)
+    onlyfiles=filelist(mypath)
     for f in onlyfiles:
         print '{}:'.format(f)
+
+        g,ok=getfile(origin,f)
+        if not(ok):
+                return
+
         try:
-            plan = dicom.read_file(os.path.join(mypath,f))
+            plan = dicom.read_file(g)
         except:
             print ("Not a dicom file")
             continue
@@ -77,8 +123,13 @@ def read_dynamic_SPECT(mypath):
     pixel_size =[0,0,0]
     frame_orientation=[0,0,0,0,0,0]
     for f in onlyfiles:
+
+        g,ok=getfile(origin,f)
+        if not(ok):
+                return
+
         try:
-            plan = dicom.read_file(os.path.join(mypath,f))
+            plan = dicom.read_file(g)
         except:
             print ("Not a dicom file")
             continue
@@ -155,8 +206,8 @@ def read_dynamic_SPECT(mypath):
     return [frame_data,frame_time,center,pixel_size,frame_orientation]
 
 def read_CT(mypath):
-    onlyfiles = [f for f in os.listdir(mypath)
-            if os.path.isfile(os.path.join(mypath, f))]
+    onlyfiles=filelist(mypath)
+    origin=re.sub('([^:/])://(.*)$',r'\1',mypath)
 
     ct_data = []
     ct_idx = []
@@ -166,8 +217,13 @@ def read_CT(mypath):
     ct_orientation=[0,0,0,0,0,0]
     for f in onlyfiles:
         print '{}:'.format(f)
+
+        g,ok=getfile(origin,f)
+        if not(ok):
+                return
+
         try:
-            plan = dicom.read_file(os.path.join(mypath,f))
+            plan = dicom.read_file(g)
         except:
             print ("Not a dicom file")
             continue