segmentation.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import numpy
  2. import config
  3. import SimpleITK
  4. import os
  5. import getData
  6. import matplotlib.pyplot
  7. def guessPixelPosition15(sx=-1,sy=-1,sz=-1):
  8. #guess position of segments
  9. if sx<0:
  10. sx=12
  11. if sy<0:
  12. sy=28
  13. if sz<0:
  14. sz=32
  15. rz=4
  16. oz=0
  17. slc=[sx,sy,sz]
  18. p1=[sx,sy,sz]
  19. pts={
  20. '0':[sx-5,sy,sz],\
  21. '1':[sx-2,sy,sz-rz],\
  22. '2':[sx-2,sy,sz+rz-1],\
  23. '3':[sx-1,sy-rz,sz],\
  24. '4':[sx-1,sy+rz-1,sz],\
  25. '5':[sx,sy-rz+oz,sz],\
  26. '6':[sx,sy-0.3*rz+oz,sz-rz],\
  27. '7':[sx,sy-0.3*rz+oz,sz+rz],\
  28. '8':[sx,sy,sz],\
  29. '9':[sx,sy+0.3*rz+oz,sz-rz],\
  30. '10':[sx,sy+0.3*rz+oz,sz+rz],\
  31. '11':[sx,sy+rz+oz,sz],\
  32. '12':[sx+3,sy-rz,sz],\
  33. '13':[sx+3,sy,sz-rz],\
  34. '14':[sx+3,sy,sz+rz],\
  35. '15':[sx+3,sy+rz,sz]}
  36. slices={'0':['8','0','1','13','2','14'],\
  37. '1':['8','0','3','4','12','15'],\
  38. '2':['8','11','9','6','5','7','10']}
  39. print(slices['0'])
  40. fp={x:[pts[q] for q in slices[x]] for x in slices}
  41. sliceIds={x:[] for x in pts}
  42. for p in pts:
  43. for s in slices:
  44. if p in slices[s]:
  45. sliceIds[p].append(s)
  46. sliceCode={x:';'.join(sliceIds[x]) for x in sliceIds}
  47. print(fp)
  48. print(sliceCode)
  49. return fp
  50. #tip
  51. fp={'0':[\
  52. [sx,sy,sz],
  53. [sx-5,sy,sz],\
  54. [sx-2,sy,sz-rz],\
  55. [sx+3,sy,sz-rz],\
  56. [sx-2,sy,sz+rz-1],\
  57. [sx+3,sy,sz+rz]],\
  58. '1':[\
  59. [sx,sy,sz],
  60. [sx-5,sy,sz],\
  61. [sx-1,sy-rz,sz],\
  62. [sx-1,sy+rz-1,sz],\
  63. [sx+3,sy-rz,sz],\
  64. [sx+3,sy+rz,sz]],\
  65. '2':[\
  66. [sx,sy,sz],
  67. [sx,sy+rz+oz,sz],\
  68. [sx,sy+0.3*rz+oz,sz-rz],\
  69. [sx,sy-0.3*rz+oz,sz-rz],\
  70. [sx,sy-rz+oz,sz],\
  71. [sx,sy-0.3*rz+oz,sz+rz],\
  72. [sx,sy+0.3*rz+oz,sz+rz]]}
  73. return fp
  74. def guessPixelPosition4(sx=-1,sy=-1,sz=-1):
  75. #guess position of segments
  76. if sx<0:
  77. sx=32
  78. if sy<0:
  79. sy=31
  80. if sz<0:
  81. sz=31
  82. rz=4
  83. pts={
  84. '0':[sx,sy,sz],\
  85. '1':[sx,sy,sz-rz],\
  86. '2':[sx,sy,sz+rz],\
  87. '3':[sx,sy-rz,sz],\
  88. '4':[sx,sy+rz,sz]}
  89. slices={
  90. '0':['0','1','2'],\
  91. '1':['0','3','4'],\
  92. '2':['0','1','2','3','4']}
  93. print(slices['0'])
  94. fp={x:[pts[q] for q in slices[x]] for x in slices}
  95. sliceIds={x:[] for x in pts}
  96. for p in pts:
  97. for s in slices:
  98. if p in slices[s]:
  99. sliceIds[p].append(s)
  100. sliceCode={x:';'.join(sliceIds[x]) for x in sliceIds}
  101. print(fp)
  102. print(sliceCode)
  103. return [{'regionId':x,'x':pts[x][0],'y':pts[x][1],'z':pts[x][2],'sliceId':sliceCode[x]} for x in pts]
  104. def updateSegmentation(db,setup,r,pixels):
  105. copyFields=['PatientId','visitName']
  106. for x in pixels:
  107. for c in copyFields:
  108. x[c]=r[c]
  109. x['SequenceNum']=r['SequenceNum']+0.01*int(x['regionId'])
  110. filterVar=['PatientId','SequenceNum']
  111. qFilter=[{'variable':y,'value':'{}'.format(x[y]),'oper':'eq'} for y in filterVar]
  112. ds=db.selectRows(setup['project'],'study','Segmentation',qFilter)
  113. entry={}
  114. mode='insert'
  115. if len(ds['rows'])>0:
  116. entry=ds['rows'][0]
  117. mode='update'
  118. for q in x:
  119. entry[q]=x[q]
  120. db.modifyRows(mode,setup['project'],'study','Segmentation',[entry])
  121. print('Done')
  122. def getSegmentationFileName(r,setup):
  123. fileName='{}_Segmentation.txt'.format(config.getCode(r,setup))
  124. def writeSegmentation(db,fb,r,setup):
  125. fileName=getSegmentationFileName(r,setup)
  126. idFilter={'variable':'PatientId','value':config.getPatientId(r,setup),'oper':'eq'}
  127. visitFilter={'variable':'visitName','value':config.getVisitId(r,setup),'oper':'eq'}
  128. rows=getData.getSegmentation(db,setup,[idFilter,visitFilter])
  129. v=numpy.zeros((len(rows),3))
  130. for qr in rows:
  131. region=int(qr['regionId'])
  132. v[region,2]=float(qr['x'])
  133. v[region,1]=float(qr['y'])
  134. v[region,0]=float(qr['z'])
  135. #for i in range(len(rows)):
  136. # print(v[i,:])
  137. numpy.savetxt(getData.getLocalPath(fileName))
  138. getData.copyToServer(fb,r,setup,[fileName])
  139. def getNC(r,xsetup):
  140. sName=getSegmentationFileName(r,xsetup)
  141. fName=getData.getLocalPath(sName)
  142. x=numpy.loadtxt(fName)
  143. nc=x.shape[0]
  144. return nc
  145. def loadSegmentation(db,fb,r,setup):
  146. sName=getSegmentationFileName(r,setup)
  147. fName=getData.getLocalPath(sName)
  148. if not os.path.isfile(fName):
  149. fURL=getData.getURL(fName)
  150. if fb.entryExists(fURL):
  151. copyFromServer(fb,r,setup,sName)
  152. else:
  153. #this creates local and global file
  154. writeSegmentation(db,fb,r,setup)
  155. return numpy.loadtxt(fName)
  156. def plotSegmentation(db,fb,r,setup,vmax=1000):
  157. copyFields=['PatientId','visitName']
  158. qFilter=[{'variable':x,'value':r[x],'oper':'eq'} for x in copyFields]
  159. nim=getData.getPatientNIM(fb,r,setup)
  160. rows=getData.getSegmentation(db,setup,qFilter)
  161. if len(rows)==0:
  162. print('Not found for id={}/{}'.format(pId,visitName))
  163. return
  164. fp={}
  165. for q in rows:
  166. if q['regionId']==0:
  167. slc=[q['x'],q['y'],q['z']]
  168. slc=[int(x) for x in slc]
  169. slices=q['sliceId'].split(';')
  170. for s in slices:
  171. try:
  172. fp[s].append([float(x) for x in [q['x'],q['y'],q['z']]])
  173. except KeyError:
  174. fp[s]=[]
  175. fp[s].append([float(x) for x in [q['x'],q['y'],q['z']]])
  176. cut0=20
  177. w0=20
  178. cut1=20
  179. w1=20
  180. cut2=20
  181. w2=20
  182. vmin=0
  183. nd=3
  184. fig,ax=matplotlib.pyplot.subplots(3,2*nd+1,figsize=(20,12))
  185. for i in numpy.arange(0,2*nd+1):
  186. ax[0,i].set_xlabel('z')
  187. ax[0,i].set_ylabel('x')
  188. ax[0,i].imshow(nim[cut2:cut2+w2,slc[1]-nd+i,cut0:cut0+w0],cmap='gray_r',vmax=vmax,vmin=vmin)
  189. ax[1,i].set_xlabel('x')
  190. ax[1,i].set_ylabel('y')
  191. ax[1,i].imshow(nim[cut2:cut2+w2,cut0:cut0+w0,slc[2]-nd+i].T,cmap='gray_r',vmax=vmax,vmin=vmin)
  192. ax[2,i].set_xlabel('z')
  193. ax[2,i].set_ylabel('y')
  194. ax[2,i].imshow(nim[slc[0]-nd+i,cut1:cut1+w1,cut1:cut1+w1],cmap='gray_r',vmax=vmax,vmin=vmin)
  195. if i==nd:
  196. pt=fp['0']
  197. ax[0,i].scatter([x[2]-cut0 for x in pt],[x[0]-cut2 for x in pt])
  198. pt=fp['1']
  199. ax[1,i].scatter([x[0]-cut2 for x in pt],[x[1]-cut0 for x in pt])
  200. pt=fp['2']
  201. ax[2,i].scatter([x[2]-cut1 for x in pt],[x[1]-cut1 for x in pt])
  202. if i==0:
  203. ax[0,i].text(2,2,pId,fontsize='large')
  204. name='{}_segmentation.png'.format(config.getCode(r,setup))
  205. fPath=getData.getLocalPath(r,setup,name)
  206. fig.savefig(fPath)
  207. getData.copyToServer(fb,r,setup,[name])