소스 검색

Adding plotSegmentation do segmentation.py

Andrej 1 년 전
부모
커밋
0a768d2e5c
2개의 변경된 파일66개의 추가작업 그리고 12개의 파일을 삭제
  1. 6 3
      pythonScripts/segmentation.ipynb
  2. 60 9
      pythonScripts/segmentation.py

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 6 - 3
pythonScripts/segmentation.ipynb


+ 60 - 9
pythonScripts/segmentation.py

@@ -3,6 +3,7 @@ import config
 import SimpleITK
 import os
 import getData
+import matplotlib.pyplot
 
 def guessPixelPosition15(sx=-1,sy=-1,sz=-1):
     #guess position of segments
@@ -134,14 +135,6 @@ def updateSegmentation(db,setup,r,pixels):
    print('Done')
      
 
-def getPatientNIM(r,setup):
-    locDir=config.getLocalDir(r,setup)
-    fileName=config.getNodeName(r,setup,'NM',19)+'.nrrd'
-    f=os.path.join(locDir,fileName)
-    im=SimpleITK.ReadImage(f)
-    nim=SimpleITK.GetArrayFromImage(im)
-    return nim
-
 def writeSegmentation(db,r,setup):
     locDir=config.getLocalDir(r,setup)
     fileName='{}_Segmentation.txt'.format(config.getCode(r,setup))
@@ -157,4 +150,62 @@ def writeSegmentation(db,r,setup):
     #for i in range(len(rows)):
     #    print(v[i,:])
     numpy.savetxt(os.path.join(locDir,fileName),v)
-    
+   
+def plotSegmentation(db,fb,r,setup):    
+   copyFields=['PatientId','visitName']
+   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))
+      return
+   
+   fp={}
+   
+   for q in rows:
+      if q['regionId']==0:
+         slc=[q['x'],q['y'],q['z']]
+         slc=[int(x) for x in slc]
+      slices=q['sliceId'].split(';')
+      for s in slices:
+         try:
+            fp[s].append([float(x) for x in [q['x'],q['y'],q['z']]])
+         except KeyError:
+            fp[s]=[]
+            fp[s].append([float(x) for x in [q['x'],q['y'],q['z']]])
+
+   cut0=20
+   w0=20
+   cut1=20
+   w1=20
+   cut2=20
+   w2=20
+   vmax=1000
+   vmin=0
+   nd=3
+   fig,ax=matplotlib.pyplot.subplots(3,2*nd+1,figsize=(20,12))
+   for i in numpy.arange(0,2*nd+1):
+      ax[0,i].set_xlabel('z')
+      ax[0,i].set_ylabel('x')
+      ax[0,i].imshow(nim[cut2:cut2+w2,slc[1]-nd+i,cut0:cut0+w0],cmap='gray_r',vmax=vmax,vmin=vmin)
+      ax[1,i].set_xlabel('x')
+      ax[1,i].set_ylabel('y')
+      ax[1,i].imshow(nim[cut2:cut2+w2,cut0:cut0+w0,slc[2]-nd+i].T,cmap='gray_r',vmax=vmax,vmin=vmin)
+      ax[2,i].set_xlabel('z')
+      ax[2,i].set_ylabel('y')
+      ax[2,i].imshow(nim[slc[0]-nd+i,cut1:cut1+w1,cut1:cut1+w1],cmap='gray_r',vmax=vmax,vmin=vmin)
+      if i==nd:
+         pt=fp['0']
+         ax[0,i].scatter([x[2]-cut0 for x in pt],[x[0]-cut2 for x in pt])
+         pt=fp['1']
+         ax[1,i].scatter([x[0]-cut2 for x in pt],[x[1]-cut0 for x in pt])
+         pt=fp['2']
+         ax[2,i].scatter([x[2]-cut1 for x in pt],[x[1]-cut1 for x in pt])
+
+         if i==0:
+            ax[0,i].text(2,2,pId,fontsize='large')
+   name='{}_segmentation.png'.format(config.getCode(r,setup))
+   fPath=getData.getLocalPath(r,setup,name)
+   fig.savefig(fPath)
+   getData.copyToServer(fb,r,setup,[name])

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.