Browse Source

Adding update status function to report progress

Andrej Studen@Labkey-KNM 1 year ago
parent
commit
e556aa5b32

+ 1 - 0
pythonScripts/calculateCenters.py

@@ -27,6 +27,7 @@ def main(configFile):
          analysis.calculateRowCenters(r,xsetup)
       #upload centers
       getData.uploadCenters(fb,r,xsetup)
+      getData.updateStatus(db,r,xsetup,'calculateCenters')
 
 if __name__=="__main__":
    main(sys.argv[1])

+ 4 - 1
pythonScripts/config.py

@@ -23,7 +23,10 @@ def getPatientField(xconfig):
    return xconfig['ParticipantField']
 
 def getVisitField(xconfig):
-   return 'visitName'
+   try:
+      return xconfig['visitField']
+   except KeyError:
+      return 'visitName'
 
 def getVisitId(row,xconfig):
    return row[getVisitField(xconfig)]

+ 1 - 0
pythonScripts/doAnalysis.py

@@ -33,6 +33,7 @@ def main(configFile):
          if not all(filesPresent):
             analysis.doAnalysisRow(r,xsetup,m)
          getData.uploadFitParFinal(fb,r,xsetup,m)
+      getData.updateStatus(db,r,xsetup,'doAnalysis')
 
 
 if __name__=="__main__":

+ 22 - 1
pythonScripts/getData.py

@@ -165,5 +165,26 @@ def getPatientNIM(fb,r,setup):
     nim=SimpleITK.GetArrayFromImage(im)
     return nim
 
+def updateStatus(db,r,setup,var,status=True):
+#update status of var to status
+   fVars=['patientCode','visitCode']
+   qFilter=[{'variable':x,'value':'{}'.format(r[x]),'oper':'eq'} for x in fVars]
+   ds=db.selectRows(setup['project'],'study','processingStatus',qFilter)
+   try:
+      rows=ds['rows']
+   except KeyError:
+      #no rows returned, possibly the dataset does not exist
+      return        
+   mode='update'
+   try:
+      entry=rows[0]
+   except IndexError:
+      fVars.extend([config.getPatientField(setup), "SequenceNum"])
+      entry={x:r[x] for x in fVars}
+      mode='insert'
+   entry[var]=status
 
-    
+   db.modifyRows(mode,setup['project'],'study','processingStatus',[entry])
+
+
+ 

+ 77 - 5
slicerScripts/convertToNRRD.py

@@ -38,11 +38,13 @@ def main(configFile):
    pythonScripts=os.path.join(pwdUp,'pythonScripts')
    sys.path.append(pythonScripts)
    import config
+   import getData
    print('Config loaded')
 
    net=labkeyInterface.labkeyInterface()
    fnet=os.path.join(os.path.expanduser('~'),'.labkey',xconfig['network'])
    net.init(fnet)
+      r['CT']=
    net.getCSRF()
    fb=labkeyFileBrowser.labkeyFileBrowser(net)
    db=labkeyDatabaseBrowser.labkeyDB(net)
@@ -98,14 +100,29 @@ def main(configFile):
       for node in nodes:
          print('\t{}'.format(node.GetName()))
          storeNode(fb,r,xconfig,node)
+
+      nodes=slicer.mrmlScene.GetNodesByClass('vtkMRMLTableNode')
+      print('Nodes (table)')
+      for node in nodes:
+         print('\t{}'.format(node.GetName()))
+         storeNode(fb,r,xconfig,node)
+
+
+
       clearNodes(r,xconfig)
+      #addCT and addFrames fill r['ct'] and r['spect']
+      db.modifyRows('update',xconfig['project'],xconfig['schemaName'],xconfig['queryName'],[r])
+      getData.updateStatus(db,r,setup,'convertToNRRD')
 
       
 def clearNodes(row,xconfig):
    nodes=slicer.mrmlScene.GetNodesByClass('vtkMRMLScalarVolumeNode')
    nodes1=slicer.mrmlScene.GetNodesByClass('vtkMRMLDoubleArrayNode')
+   nodes2=slicer.mrmlScene.GetNodesByClass('vtkMRMLTableNode')
    for n in nodes1:
       nodes.AddItem(n)
+   for n in nodes2:
+      nodes.AddItem(n)
 
    res=[slicer.mrmlScene.RemoveNode(f) for f in nodes]
 
@@ -188,6 +205,7 @@ def addCT(r,patient,xconfig):
    vtkData=vtkInterface.numpyToVTK(ct['data'],ct['data'].shape)
    nodeName=config.getNodeName(r,xconfig,'CT')
    addNode(nodeName,vtkData,ct['origin'],ct['pixel_size'],ct['orientation'],0)
+   r['ct']=f'{nodeName}.nrrd'
 
 
 def addFrames(r,patient,xconfig):
@@ -202,7 +220,8 @@ def addFrames(r,patient,xconfig):
       nodeName=config.getNodeName(r,xconfig,'NM',it)
       vtkData=vtkInterface.numpyToVTK(frame_data,frame_data.shape)
       addNode(nodeName,vtkData,nm['origin'],nm['pixel_size'],nm['orientation'],1)
-
+      #last one will be kept
+      r['spect']=f'{nodeName}.nrrd'
 
 
 def addNode(nodeName,v,origin,pixel_size,orientation,dataType):
@@ -241,8 +260,11 @@ def addDummyInputFunction(r,patient,xconfig):
    dnsNodeName=config.getNodeName(r,xconfig,'Dummy')
    dns = slicer.mrmlScene.GetNodesByClassByName('vtkMRMLDoubleArrayNode',dnsNodeName)
    if dns.GetNumberOfItems() == 0:
-      dn = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode())
-      dn.SetName(dnsNodeName)
+      try:
+         dn = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode())
+      except AttributeError:
+         addDummyTable(dnsNodeName,n,nm)
+         return
    else:
       dn = dns.GetItemAsObject(0)
 
@@ -258,12 +280,43 @@ def addDummyInputFunction(r,patient,xconfig):
       dn.SetValue(i, 2, 0)
       print('{} ({},{})'.format(i,fx,fy))
 
+
+
+def addDummyTable(dnsNodeName,n,nm): 
+   
+   #add vtkMRMLTableNodes
+   dns = slicer.mrmlScene.GetNodesByClassByName('vtkMRMLTableNode',dnsNodeName)
+   if dns.GetNumberOfItems() == 0:
+      dn = slicer.mrmlScene.AddNode(slicer.vtkMRMLTableNode())
+      dn.SetName(dnsNodeName)
+   else:
+      dn = dns.GetItemAsObject(0)
+      dn.RemoveAllColumns()
+      
+
+   ft=nm['time']
+   dt=nm['duration']
+   tCol=vtk.vtkDoubleArray()
+   dCol=vtk.vtkDoubleArray()
+   for i in range(n):
+      tCol.InsertNextValue(ft[i])
+      dCol.InsertNextValue(dt[i])
+
+   tcol=dn.AddColumn(tCol)
+   tcol.SetName('time')
+   dcol=dn.AddColumn(dCol)
+   dcol.SetName('duration')
+
 def storeNode(fb,row,xconfig,node):
    import config
 
    suffix=".nrrd"
+   isTable=False
    if node.__class__.__name__=="vtkMRMLDoubleArrayNode":
       suffix=".mcsv"
+   if node.__class__.__name__=="vtkMRMLTableNode":
+      suffix=".mcsv"
+      isTable=True      
    if (node.__class__.__name__=="vtkMRMLTransformNode" or \
          node.__class__.__name__=="vtkMRMLGridTransformNode"):
       suffix=".h5"
@@ -272,7 +325,10 @@ def storeNode(fb,row,xconfig,node):
 
    localPath=os.path.join(config.getLocalDir(row,xconfig),fileName)
 
-   slicer.util.saveNode(node,localPath)
+   if isTable:
+      storeTable(node,localPath)
+   else:
+      slicer.util.saveNode(node,localPath)
    print("Stored to: {}".format(localPath))
    labkeyPath=fb.buildPathURL(xconfig['project'],config.getPathList(row,xconfig))
    print ("Remote: {}".format(labkeyPath))
@@ -281,7 +337,23 @@ def storeNode(fb,row,xconfig,node):
    fb.writeFileToFile(localPath,remoteFile)
 
 
-   
+def storeTable(node,filename):
+   #mimic old vtkMRMLDoubleArray format
+   table=node.GetTable()
+   ft=table.GetColumnByName('time')
+   fd=table.GetColumnByName('duration')
+   n=ft.GetNumberOfValues()
+   print(f'Storing {n} values')
+   with open(filename,'w') as f:
+      f.write(f'# measurement file {filename}\n')
+      f.write('# no labels\n')
+      for i in range(n):
+         _t=ft.GetTuple1(i)
+         _d=fd.GetTuple1(i)
+         print(f'{_t},{_d},0')
+         f.write(f'{_t},{_d},0\n')
+
+