Bladeren bron

Functionality to perfrom doPixelAnalysis.py

NIX Worker 1 jaar geleden
bovenliggende
commit
5e8f30d99f
4 gewijzigde bestanden met toevoegingen van 105 en 26 verwijderingen
  1. 42 19
      pythonScripts/analysis.py
  2. 15 6
      pythonScripts/config.py
  3. 47 0
      pythonScripts/doPixelAnalysis.py
  4. 1 1
      pythonScripts/segmentation.py

+ 42 - 19
pythonScripts/analysis.py

@@ -9,7 +9,7 @@ import SimpleITK
 import numpy
 import re
 import json
-
+import segmentation
 
 def calculateCenters(db,setup):
 
@@ -95,18 +95,19 @@ def doAnalysisRow(r,xsetup,mode):
 
  
 
-def doPixelAnalysis(db,fb,setup,sigma2,mode='IVF'):
+def doPixelAnalysis(db,setup,sigma2,mode='IVF'):
 
     baseDir=os.path.dirname(os.getcwd())#one up
     rows=getData.getPatients(db,setup)
 
     for r in rows:
-       doPixelAnalysisRow(db,fb,r,setup,mode)
+       doPixelAnalysisRow(r,setup,mode)
 
-def doPixelAnalysisRow(db,fb,r,setup, mode='IVF'):
+def doPixelAnalysisRow(r,setup, mode='IVF'):
    cmds=config.cmdMatlab()
 
- 
+   tempDir=config.getTempDir(setup)
+   #
                 
     #in global mode, IVF parameters are inferred together with fits to classes
     #this is essentially repeat of the above, except that classes are taken as
@@ -117,7 +118,7 @@ def doPixelAnalysisRow(db,fb,r,setup, mode='IVF'):
       mScript='analyzePixel'
       analysisType=''
    if mode=='IVF':
-      mScript='analzyePixelIVF'
+      mScript='analyzePixelIVF'
       analysisType='IVF_'
     
 
@@ -131,14 +132,15 @@ def doPixelAnalysisRow(db,fb,r,setup, mode='IVF'):
       return
     
    #code=config.getCode(r,setup)
-   x=segmentation.loadSegmentation(db,fb,r,setup)
-   nc=x.shape[0]
+   nc=segmentation.getNC(r,setup)
+   #loadSegmentation(db,fb,r,setup)
+   #nc=x.shape[0]
 
    sigma2=setup['sigma2']
    code=config.getCode(r,setup)
    for s2 in sigma2:
       f=config.getPixelFitParFinalName(code,nc,s2,mode)
-      fName=getData.getLocalPath(f)
+      fName=getData.getLocalPath(r,setup,f)
       if os.path.isfile(fName):
          print('Skipping; {} available.'.format(fName))
          continue
@@ -217,7 +219,22 @@ def getWeights(db,r,setup,nclass,realizationId,sigma2,na):
 
 
 #gets fitPar for a particular realization in [0..nr-1] range
-def getFitPar(r,setup,nclass,realizationId,mode=''):
+def getPixelFitPar(r,setup,nc,s2,mode):
+   code=config.getCode(r,setup)
+   f=config.getPixelFitParFinalName(code,nc,s2,mode)
+   getData.copyFromServer(r,setup,f)
+   fName=getData.getLocalPath(r,setup,f)
+   return numpy.genFromText(fName,delimiter='\t')
+
+def getFitPar(r,setup,nclass,realizationId,mode):
+   code=config.getCode(r,setup)
+   f=config.getFitParFinalName(code,nclass,realizationId,mode)
+   getData.copyFromServer(r,setup,f)
+   fName=getData.getLocalPath(r,setup,f)
+   return numpy.genFromText(fName,delimiter='\t')
+
+
+def getFitParBackup(r,setup,nclass,realizationId,mode=''):
     #fitGoodnes A tau alpha delay [k1 BVF k2 delay]xNcenters
     allowedModes=['','IVF','Pixel','PixelIVF']
     if mode not in allowedModes:
@@ -335,12 +352,12 @@ def storeSummary(db,setup,summary,sigma2,na):
                     qrow['na']=na
                     getData.updateSummary(db,setup,mode,[qrow])
                     
-def summaryPixelIVF(db,setup,sigma2):
+def summaryPixelIVF(db,fb,setup,sigma2,qfilter=[]):
     #for second type of analysis (pixel based regions)
-    rows=getData.getPatients(db,setup)
+    rows=getData.getPatients(db,setup,qfilter)
     return \
         {config.getCode(r,setup):\
-         {s2:getPixelIVF(db,r,setup,s2) for s2 in sigma2} for r in rows}
+         {s2:getPixelIVF(db,fb,r,setup,s2) for s2 in sigma2} for r in rows}
 
     
 def storeIVF(db,setup,summary):
@@ -371,11 +388,17 @@ def storeIVF(db,setup,summary):
                 qrow['option']='D'
                 getData.updateSummary(db,setup,mode,[qrow])
                 
-def getPixelIVF(db,r,setup,sigma2):
-    idFilter=config.getIdFilter(r,setup)
-    visitFilter=config.getVisitFilter(r,setup)
-    rows=getData.getSegmentation(db,setup,[idFilter,visitFilter])
-    nclassIVF=len(rows)
-    fitPar=getFitPar(r,setup,nclassIVF,sigma2,'PixelIVF')
+def getPixelIVF(db,fb,r,setup,sigma2):
+    #idFilter=config.getIdFilter(r,setup)
+    #visitFilter=config.getVisitFilter(r,setup)
+    #rows=getData.getSegmentation(db,setup,[idFilter,visitFilter])
+    #nclassIVF=len(rows)
+
+    x=segmentation.loadSegmentation(db,fb,r,setup)
+    nclassIVF=x.shape[0]
+    #this assumes segmentation is loaded
+    #nclassIVF=segmentation.getNC(r,setup)
+    #fitPar=getFitPar(r,setup,nclassIVF,sigma2,'PixelIVF')
+    fitPar=getPixelFitPar(r,setup,nclassIVF,sigma2,'IVF')
     k1={r['regionId']:getK1(fitPar,r['regionId']) for r in rows}
     return k1

+ 15 - 6
pythonScripts/config.py

@@ -1,6 +1,6 @@
 import os
 import json
-
+import re
 
 def cmdMatlab():
    pwd=os.path.dirname(os.path.abspath(__file__))
@@ -118,17 +118,26 @@ def getPixelFitParFinalName(code,nc,s2,mode):
    sigmaCode=re.sub('\.','p',sigmaCode)
    fName='{}_{}_{}_Pixel'.format(code,nc,sigmaCode)
    if mode!='general':
-      fName='{}_{}'.format(fName,mode)
+      fName='{}{}'.format(fName,mode)
    fName='{}_fitParFinal.txt'.format(fName)
    return fName
 
+def getPixelFitParFinalPngName(code,nc,i,s2,mode):
+   sigmaCode='{:.2f}'.format(s2)
+   sigmaCode=re.sub('\.','p',sigmaCode)
+   fName='{}_{}_{}_Pixel'.format(code,nc,sigmaCode)
+   if mode!='general':
+      fName='{}{}'.format(fName,mode)
+   fName='{}_centers{}.png'.format(fName,i)
+   return fName
+
+
 def printPixelFitParFinalRowNames(r,setup,nc,s2,mode):
    names=[]
    code=getCode(r,setup)
-   #tempDir=config.getTempDir(setup)
-   for j in range(nr):
-      names.append(getPixelFitParFinalName(code,nc,s2,aType))
-         #names.append('{}_centerWeight.nrrd'.format(fCode))
+   names.append(getPixelFitParFinalName(code,nc,s2,mode))
+   for i in range(nc):
+      names.append(getPixelFitParFinalPngName(code,nc,i+1,s2,mode))
    return names
  
 

+ 47 - 0
pythonScripts/doPixelAnalysis.py

@@ -0,0 +1,47 @@
+import analysis
+import sys
+import os
+import getData
+import json
+import config
+import segmentation
+
+
+def main(configFile):
+   with open(configFile,'r') as f:
+      xsetup=json.load(f)
+   db,fb=getData.connectDB(xsetup['network'])
+
+   try:
+      qfilter=xsetup['patientFilter']
+   except KeyError:
+      qfilter=[]
+   
+   rows=getData.getPatients(db,xsetup,qfilter)
+
+   mode=['global','IVF']
+   for r in rows:
+      segmentation.loadSegmentation(db,fb,r,xsetup)
+      getData.downloadCenters(fb,r,xsetup)
+      for m in mode:
+         getData.downloadFitParFinal(fb,r,xsetup,m)
+
+   pixelFitMode='IVF'
+   for r in rows:
+      code=config.getCode(r,xsetup)
+      nc=segmentation.getNC(r,xsetup)
+      for s2 in xsetup['sigma2']:
+         files=config.printPixelFitParFinalRowNames(r,xsetup,nc,s2,pixelFitMode)
+         fullFiles=[getData.getLocalPath(r,xsetup,f) for f in files]
+         filesPresent=[os.path.isfile(f) for f in fullFiles]
+         #check if we need to run analysis
+         if not all(filesPresent):
+            analysis.doPixelAnalysisRow(r,xsetup)
+
+         getData.copyToServer(fb,r,xsetup,files)
+      #
+      #getData.uploadPixelFitParFinal(fb,r,xsetup,pixelFitMode)
+
+
+if __name__=="__main__":
+   main(sys.argv[1])

+ 1 - 1
pythonScripts/segmentation.py

@@ -156,7 +156,7 @@ def writeSegmentation(db,fb,r,setup):
 
 def getNC(r,xsetup):
    sName=getSegmentationFileName(r,xsetup)
-   fName=getData.getLocalPath(sName)
+   fName=getData.getLocalPath(r,xsetup,sName)
    x=numpy.loadtxt(fName)
    nc=x.shape[0]
    return nc