Browse Source

Adjusting to NRRD based segmentations

Andrej Studen@Labkey-KNM 1 year ago
parent
commit
a65f79315e
2 changed files with 38 additions and 33 deletions
  1. 16 10
      pythonScripts/analysis.py
  2. 22 23
      pythonScripts/segmentation.py

+ 16 - 10
pythonScripts/analysis.py

@@ -88,7 +88,6 @@ def doAnalysisRow(r,xsetup,mode):
             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)
    
@@ -101,9 +100,9 @@ def doPixelAnalysis(db,setup,sigma2,mode='IVF'):
     rows=getData.getPatients(db,setup)
 
     for r in rows:
-       doPixelAnalysisRow(r,setup,mode)
+       doPixelAnalysisRow(db,r,setup,mode)
 
-def doPixelAnalysisRow(r,setup, mode='IVF'):
+def doPixelAnalysisRow(db,r,setup, mode='IVF'):
    cmds=config.cmdMatlab()
 
    tempDir=config.getTempDir(setup)
@@ -138,20 +137,24 @@ def doPixelAnalysisRow(r,setup, mode='IVF'):
 
    sigma2=setup['sigma2']
    code=config.getCode(r,setup)
+
+
+
    for s2 in sigma2:
       f=config.getPixelFitParFinalName(code,nc,s2,mode)
       fName=getData.getLocalPath(r,setup,f)
-      sFile=segmentation.getSegmentationFileName(r,setup)
+      sFile=segmentation.getSegmentationFileName(db,r,setup)
       segmFile=getData.getLocalPath(r,setup,sFile)
       if os.path.isfile(fName):
          print('Skipping; {} available.'.format(fName))
          continue
 
       runCmds=[x for x in cmds]
-      runCmds.append('-r "path=\'{}\'; patientID=\'{}\'; sigma2=\'{}\' segmFile=\'{}\'; {}"'.format(tempDir,code,s2,segmFile,mScript))
-
+      runCmds.append('-r "path=\'{}\'; patientID=\'{}\'; sigma2=\'{}\'; segmFile=\'{}\'; {}"'.format(tempDir,code,s2,segmFile,mScript))
+      print(f'Running with {s2}')
       
-      subprocess.run(runCmds,check=True, stdout=subprocess.PIPE)
+      print(subprocess.run(runCmds, check=True, stdout=subprocess.PIPE).stdout)
+      print(f'Done')
  
             
 def getIWeights(r,setup,nclass,realizationId,ic):
@@ -397,8 +400,10 @@ def storeIVF(db,setup,summary):
 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)
+    nclassIVF=segmentation.getNC(r,setup)
+    regions=[{'regionId':x+1} for x in range(nclassIVF)]
+    #rows=getData.getSegmentation(db,setup,[idFilter,visitFilter])
+    #nclassIVF=len(rows)
 
     #x=segmentation.loadSegmentation(db,fb,r,setup)
     #nclassIVF=x.shape[0]
@@ -407,5 +412,6 @@ def getPixelIVF(db,fb,r,setup,sigma2):
     #fitPar=getFitPar(r,setup,nclassIVF,sigma2,'PixelIVF')
     fitPar=getPixelFitPar(fb,r,setup,nclassIVF,sigma2,'IVF')
     print(fitPar)
-    k1={r['regionId']:getK1(fitPar,r['regionId']) for r in rows}
+    k1={r['regionId']:getK1(fitPar,r['regionId']-1) for r in regions}
+    print(k1)
     return k1

+ 22 - 23
pythonScripts/segmentation.py

@@ -116,7 +116,7 @@ def guessPixelPosition4(sx=-1,sy=-1,sz=-1):
     return [{'regionId':x,'x':pts[x][0],'y':pts[x][1],'z':pts[x][2],'sliceId':sliceCode[x]} for x in pts]
 
 def updateSegmentation(db,setup,r,pixels):
-   copyFields=['PatientId','visitName']
+   copyFields=['PatientId','visitCode']
    for x in pixels:
       for c in copyFields:
          x[c]=r[c]
@@ -134,16 +134,16 @@ def updateSegmentation(db,setup,r,pixels):
       db.modifyRows(mode,setup['project'],'study','Segmentation',[entry])
    print('Done')
     
-def getSegmentationFileName(r,setup):
+def getSegmentationFileName(db,r,setup):
    if setup['segmentationMode']=='TXT':
       return '{}_Segmentation.txt'.format(config.getCode(r,setup))
    if setup['segmentationMode']=='NRRD':
-      copyFields=['PatientId','visitName']
+      copyFields=['PatientId','visitCode']
       qFilter=[{'variable':x,'value':r[x],'oper':'eq'} for x in copyFields]
       qFilter.append({'variable':'User','value':setup['targetUser'],'oper':'eq'})
       rows=getData.getSegmentation(db,setup,qFilter)
       r=rows[0]
-      return r['latestFile']
+      return '/'.join(['Segmentations',r['latestFile']])
  
 
 def writeSegmentation(db,fb,r,setup):
@@ -152,9 +152,9 @@ def writeSegmentation(db,fb,r,setup):
       print('Failed to load segmentation')
       return
    
-   fileName=getSegmentationFileName(r,setup)
+   fileName=getSegmentationFileName(db,r,setup)
    idFilter={'variable':'PatientId','value':config.getPatientId(r,setup),'oper':'eq'}
-   visitFilter={'variable':'visitName','value':config.getVisitId(r,setup),'oper':'eq'}
+   visitFilter={'variable':'visitCode','value':config.getVisitId(r,setup),'oper':'eq'}
    rows=getData.getSegmentation(db,setup,[idFilter,visitFilter])
    v=numpy.zeros((len(rows),3))
    for qr in rows:
@@ -171,32 +171,29 @@ def getNC(r,xsetup):
    if xsetup['segmentationMode']=='TXT':
       getNCTxt(r,xsetup)
    if xsetup['segmentationMode']=='NRRD':
-      return 500
-
-def loadSegmentation(db,fb,r,xsetup):
-   if xsetup['segmentationMode']=='TXT':
-      return loadSegmentationTxt(db,fb,r,xsetup)
-   if xsetup['segmentationMode']=='NRRD':
-      return loadSegmentationNrrd(db,fb,r,xsetup)
-   
+      return xsetup['NC']
 
 def getNCTxt(r,xsetup):
-   sName=getSegmentationFileName(r,xsetup)
+   sName=getSegmentationFileName(db=None,r=r,setup=xsetup)
    fName=getData.getLocalPath(r,xsetup,sName)
    x=numpy.loadtxt(fName)
    nc=x.shape[0]
    return nc 
    
 def loadSegmentation(db,fb,r,setup):
-   sName=getSegmentationFileName(r,setup)
+   
+   sName=getSegmentationFileName(db,r,setup)
+   print(f'Looking for {sName}')
    fName=getData.getLocalPath(r,setup,sName)
+   print(f'Local {fName}')
    if not os.path.isfile(fName):
-      qName=sName
-      if setup['segmentationMode']=='NRRD':
-         qName='/'.join(['Segmentations',sName])
-      fURL=getData.getURL(fb,r,setup,qName)
+      fURL=getData.getURL(fb,r,setup,sName)
       if fb.entryExists(fURL):
-         getData.copyFromServer(fb,r,setup,[qName])
+         getData.copyFromServer(fb,r,setup,[sName])
+         if os.path.isfile(fName):
+            print(f'Copied {fURL} to {fName}') 
+         else: 
+            print(f'Failed to load {fName} from {fURL}')
       else:
          #this creates local and global file
          writeSegmentation(db,fb,r,setup)
@@ -206,13 +203,15 @@ def loadSegmentation(db,fb,r,setup):
 
    
 def plotSegmentation(db,fb,r,setup,vmax=1000):    
-   copyFields=['PatientId','visitName']
+   copyFields=['PatientId','visitCode']
    qFilter=[{'variable':x,'value':r[x],'oper':'eq'} for x in copyFields]
    nim=getData.getPatientNIM(fb,r,setup)
    rows=getData.getSegmentation(db,setup,qFilter)
 
    if len(rows)==0:
-      print('Not found for id={}/{}'.format(pId,visitName))
+      pId=r['PatientId']
+      visitCode=r['visitCode']
+      print(f'Not found for id={pId}/{visitCode}')
       return
    
    fp={}