瀏覽代碼

Preparing to run with pixelAnalysis

NIX Worker 1 年之前
父節點
當前提交
e396c17716
共有 6 個文件被更改,包括 104 次插入43 次删除
  1. 0 1
      matlab/analyzePixel.m
  2. 0 1
      matlab/analyzePixelIVF.m
  3. 43 34
      pythonScripts/analysis.py
  4. 18 2
      pythonScripts/config.py
  5. 15 0
      pythonScripts/getData.py
  6. 28 5
      pythonScripts/segmentation.py

+ 0 - 1
matlab/analyzePixel.m

@@ -4,7 +4,6 @@ rng shuffle
 %add nrrd processing capability
 addpath('nrrd_read_write_rensonnet')
 
-path='/home/studen/temp/dynamicSPECT';
 [cax,cm]=loadTime(path,patientID);
 
 segmFile=fullfile(path,patientID,sprintf('%s_Segmentation.txt',patientID));

+ 0 - 1
matlab/analyzePixelIVF.m

@@ -4,7 +4,6 @@ rng shuffle
 %add nrrd processing capability
 addpath('nrrd_read_write_rensonnet')
 
-path='/home/studen/temp/dynamicSPECT';
 [cax,cm]=loadTime(path,patientID);
 
 segmFile=fullfile(path,patientID,sprintf('%s_Segmentation.txt',patientID));

+ 43 - 34
pythonScripts/analysis.py

@@ -95,51 +95,60 @@ def doAnalysisRow(r,xsetup,mode):
 
  
 
-def doPixelAnalysis(db,setup,sigma2,mode='IVF'):
-    
+def doPixelAnalysis(db,fb,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)
+
+def doPixelAnalysisRow(db,fb,r,setup, mode='IVF'):
+   cmds=config.cmdMatlab()
+
+ 
                 
     #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
     #time-response curves from pixels in the sigma2-defined neighborhood of
     #target pixels
-    if mode=='global':
-        runScript='doPixelAnalysis.sh'
-        analysisType=''
+
+   if mode=='global':
+      mScript='analyzePixel'
+      analysisType=''
+   if mode=='IVF':
+      mScript='analzyePixelIVF'
+      analysisType='IVF_'
+    
+
     #in IVF mode, the parameters of input function are taken from the cluster fit 
     #(doAnalysis above, with mode=general). The rest is the same as for global mode
-    if mode=='IVF':
-        runScript='doPixelIVFAnalysis.sh'
-        analysisType='IVF_'
    
-    #either doPixelAnalysis or doPixelAnalysisIVF.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,setup)
+   x=segmentation.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)
+      if os.path.isfile(fName):
+         print('Skipping; {} available.'.format(fName))
+         continue
 
-    try:
-        print('Setting runScript to {}'.format(runScript))
-    except NameError:
-        print('Mode can be one of (global,IVF))')
-        return
-    
-    rows=getData.getPatients(db,setup)
-    for r in rows:
-        dataPath=config.getLocalDir(r,setup)
-        code=config.getCode(r,setup)
-        sName=os.path.join(dataPath,'{}_Segmentation.txt'.format(code))
-        sFile=os.path.join(dataPath,sName)
-        x=numpy.loadtxt(sFile)
-        nc=x.shape[0]
-        for s2 in sigma2:
-            sigmaCode='{:.2f}'.format(s2)
-            sigmaCode=re.sub('\.','p',sigmaCode)
-            fName=os.path.join(dataPath,'{}_{}_{}_Pixel{}_fitParFinal.txt'.format(code,nc,sigmaCode,analysisType))
-            if os.path.isfile(fName):
-                print('Skipping; {} available.'.format(fName))
-                continue
-            subprocess.run([scriptPath,code,str(s2)], \
-                             check=True, stdout=subprocess.PIPE)
-            
+      runCmds=[x for x in cmds]
+      runCmds.append('-r "path=\'{}\'; patientID=\'{}\'; sigma2=\'{}\'; {}"'.format(tempDir,code,s2,mScript))
+
+      
+      subprocess.run(runCmds,check=True, stdout=subprocess.PIPE)
+ 
             
 def getIWeights(r,setup,nclass,realizationId,ic):
     locDir=config.getLocalDir(r,setup)

+ 18 - 2
pythonScripts/config.py

@@ -100,7 +100,6 @@ def getFitParFinalName(code,nc,j,aType):
       fCode='{}_{}'.format(fCode,aType)
    
    return '{}_fitParFinal.txt'.format(fCode)
-   
 
 def printFitParFinalRowNames(r,setup,aType):
    names=[]
@@ -114,5 +113,22 @@ def printFitParFinalRowNames(r,setup,aType):
          #names.append('{}_centerWeight.nrrd'.format(fCode))
    return names
  
-
+def getPixelFitParFinalName(code,nc,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='{}_fitParFinal.txt'.format(fName)
+   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))
+   return names
+ 
 

+ 15 - 0
pythonScripts/getData.py

@@ -100,6 +100,21 @@ def uploadFitParFinal(fb,r,setup,mode):
    #copy files to server
    copyToServer(fb,r,setup,names)
 
+def downloadFitParFinal(fb,r,setup,mode):
+   names=config.printFitParFinalRowNames(r,setup,mode)
+   #copy files to server
+   copyFromServer(fb,r,setup,names)
+
+def uploadPixelFitParFinal(fb,r,xsetup,m='IVF'):
+   sigma2=xsetup['sigma2']
+   #get nc
+   nc=segmentation.getNC(r,xsetup)
+   
+   for s2 in sigma2:
+      names=config.printPixelFitParFinalRowNames(r,xsetup,nc,s2,m)
+      copyToServer(fb,r,setup,names)
+
+
 def copyToServer(fb,r,setup,names):
    tempDir=config.getTempDir(setup)
    code=config.getCode(r,setup)

+ 28 - 5
pythonScripts/segmentation.py

@@ -133,11 +133,12 @@ def updateSegmentation(db,setup,r,pixels):
          entry[q]=x[q]
       db.modifyRows(mode,setup['project'],'study','Segmentation',[entry])
    print('Done')
-     
+    
+def getSegmentationFileName(r,setup):
+   fileName='{}_Segmentation.txt'.format(config.getCode(r,setup))
 
-def writeSegmentation(db,r,setup):
-    locDir=config.getLocalDir(r,setup)
-    fileName='{}_Segmentation.txt'.format(config.getCode(r,setup))
+def writeSegmentation(db,fb,r,setup):
+    fileName=getSegmentationFileName(r,setup)
     idFilter={'variable':'PatientId','value':config.getPatientId(r,setup),'oper':'eq'}
     visitFilter={'variable':'visitName','value':config.getVisitId(r,setup),'oper':'eq'}
     rows=getData.getSegmentation(db,setup,[idFilter,visitFilter])
@@ -149,7 +150,29 @@ def writeSegmentation(db,r,setup):
         v[region,0]=float(qr['z'])
     #for i in range(len(rows)):
     #    print(v[i,:])
-    numpy.savetxt(os.path.join(locDir,fileName),v)
+    numpy.savetxt(getData.getLocalPath(fileName))
+    getData.copyToServer(fb,r,setup,[fileName])
+
+def getNC(r,xsetup):
+   sName=getSegmentationFileName(r,xsetup)
+   fName=getData.getLocalPath(sName)
+   x=numpy.loadtxt(fName)
+   nc=x.shape[0]
+   return nc 
+   
+def loadSegmentation(db,fb,r,setup):
+   sName=getSegmentationFileName(r,setup)
+   fName=getData.getLocalPath(sName)
+   if not os.path.isfile(fName):
+      fURL=getData.getURL(fName)
+      if fb.entryExists(fURL):
+         copyFromServer(fb,r,setup,sName)
+      else:
+         #this creates local and global file
+         writeSegmentation(db,fb,r,setup)
+   return numpy.loadtxt(fName)
+
+
    
 def plotSegmentation(db,fb,r,setup,vmax=1000):    
    copyFields=['PatientId','visitName']