Browse Source

Making analysis run matlab scripts directly

NIX Worker 1 year ago
parent
commit
e3c0099dab
1 changed files with 79 additions and 50 deletions
  1. 79 50
      pythonScripts/analysis.py

+ 79 - 50
pythonScripts/analysis.py

@@ -8,63 +8,92 @@ import subprocess
 import SimpleITK
 import numpy
 import re
+import json
 
 
-def calculateCenters(db,setup,nclass,nr):
+def calculateCenters(db,setup):
 
     
-    #path of scripts is ../scripts
-    #one up
-    baseDir=os.path.dirname(os.getcwd())
-    #scriptPath
-    scriptPath=os.path.join(baseDir,'scripts','generateCenters.sh')
-    rows=getData.getPatients(db,setup)
-    for r in rows:
-        #download 
-        code=config.getCode(r,setup)
-        for nc in nclass:
-            print('centers: {}/{},{}'.format(code,nc,nr))
-            print(subprocess.run([scriptPath,code,str(nc),str(nr)], \
-                             check=True, stdout=subprocess.PIPE).stdout)
-            
-def doAnalysis(db,setup,nclass,nr,mode):
-    baseDir=os.path.dirname(os.getcwd())#one up
-    #this is if IVF is inferred together with k1 fits
-    #runScript='doAnalysis.sh'
-    #this is if IVF is taken from previous cluster fits
-    #runScript='doAnalysisIVF.sh'
-    
+   #path of scripts is ../scripts
+   #one up
+   #scriptPath
+   rows=getData.getPatients(db,setup)
+
+  # $MATLAB -sd $MATLABSCRIPTDIR -batch "patientID='${PATIENTID}'; nclass='${NCLASS}'; nRealizations='${NR}'; generateCenters" >& ~/logs/dynamicSPECT.log;
+
+   for r in rows:
+
+      #download 
+      calculateRowCenters(r,setup)
+
+def calculateRowCenters(r,setup):
+
+   cmds=config.cmdMatlab()
+   tempDir=config.getTempDir(setup)
+   code=config.getCode(r,setup)
+
+   nr=setup['nr']
+   nclass=setup['nclass']
+
+   for nc in nclass:
+      runCmds=[x for x in cmds]
+      runCmds.append('-r "path=\'{}\'; patientID=\'{}\'; nclass=\'{}\'; nRealizations=\'{}\'; generateCenters"'.format(tempDir,code,nc,nr))
+      print('centers: {}/{},{}'.format(code,nc,nr))
+      #redirect and print output to stdout
+      print(runCmds)
+      print(subprocess.run(runCmds, check=True, stdout=subprocess.PIPE).stdout)
+
+def doAnalysis(db,setup,mode):
+   
+   rows=getData.getPatients(db,setup)
+
+   for r in rows:
+      doAnalysisRow(r,xsetup,mode)
+
+def doAnalysisRow(r,xsetup,mode):
+   cmds=config.cmdMatlab()
+
+   tempDir=config.getTempDir(xsetup)
+   #this is if IVF is inferred together with k1 fits
+   #runScript='doAnalysis.sh'
+   #this is if IVF is taken from previous cluster fits
+   #runScript='doAnalysisIVF.sh'
     
-    if mode=='global':
-        runScript='doAnalysis.sh'
-        analysisType=''
-    if mode=='IVF':
-        runScript='doAnalysisIVF.sh'
-        analysisType='IVF_'
+   if mode=='global':
+      mScript='analyze'
+      analysisType=''
+   if mode=='IVF':
+      mScript='analyzeIVF'
+      analysisType='IVF_'
     
-    try:
-        print('Setting runScript to {}'.format(runScript))
-    except NameError:
-        print('Mode can be one of (global,IVF))')
-        return
-        
-    #either doAnalysis or doAnalysisIVF.sh
-    scriptPath=os.path.join(baseDir,'scripts',runScript)
+   try:
+      print('Setting runScript to {}'.format(mScript))
+   except NameError:
+      print('Mode can be one of (global,IVF))')
+      return
+   
+   code=config.getCode(r,xsetup)
+   nr=xsetup['nr']
+   nclass=xsetup['nclass']
 
-    rows=getData.getPatients(db,setup)
-    for r in rows:
-        dataPath=config.getLocalDir(r,setup)
-        code=config.getCode(r,setup)
-        for nc in nclass:
-            for rId in numpy.arange(nr):
-                print('{} [{} {}/{}]'.format(code,nc,rId+1,nr))
-                #this is a duplicate of path generated in fitCenters.m
-                fName=os.path.join(dataPath,'{}_{}_{}_{}fitParFinal.txt'.format(code,nc,rId+1,analysisType))
-                if os.path.isfile(fName):
-                    print('Skipping; {} available.'.format(fName))
-                    continue
-                subprocess.run([scriptPath,code,str(nc),str(rId+1)],\
-                         check=True, stdout=subprocess.PIPE)
+   for nc in nclass:
+      for j in numpy.arange(nr):
+         print('{} [{} {}/{}]'.format(code,nc,j+1,nr))
+
+         #avoid repetition, only do for new files
+         #this is a duplicate of path generated in fitCenters.m
+         fName=os.path.join(tempDir,code,config.getFitParFinalName(code,nc,j,analysisType))
+         if os.path.isfile(fName):
+            print('Skipping; {} available.'.format(fName))
+            continue
+         runCmds=[x for x in cmds]
+         runCmds.append('-r "path=\'{}\'; patientID=\'{}\'; nclass=\'{}\'; realizationId=\'{}\'; {}"'.format(tempDir,code,nc,j+1,mScript))
+
+      
+         subprocess.run(runCmds,check=True, stdout=subprocess.PIPE)
+   
+
+ 
 
 def doPixelAnalysis(db,setup,sigma2,mode='IVF'):