plotData.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import matplotlib.pyplot
  2. import fitData
  3. import fitModel
  4. import numpy
  5. import functools
  6. import geometry
  7. import segmentation
  8. def plotIVF(t,ivf,samples,threshold,file0=None,file1=None):
  9. fit=fitData.getFit(samples,threshold)
  10. t1=numpy.linspace(0,numpy.max(t),300)
  11. w=numpy.ones(t.shape)
  12. fun=functools.partial(fitModel.fDiff,fitModel.fIVF,t,ivf,w)
  13. matplotlib.pyplot.figure(0)
  14. matplotlib.pyplot.scatter(t,ivf)
  15. n=samples.shape[1]
  16. alpha=1/n
  17. alpha1=1
  18. for j in range(samples.shape[1]):
  19. cPar=samples[1:,j]
  20. df=fun(cPar)
  21. chi2=(df*df).sum()
  22. cost=samples[0,j]
  23. if chi2!=cost:
  24. print('{}/{} {}'.format(samples[0,j],chi2,cPar))
  25. fv=fitModel.fIVF(t1,cPar)
  26. if chi2>threshold:
  27. matplotlib.pyplot.figure(0)
  28. matplotlib.pyplot.plot(t1,fv,color='silver',alpha=alpha)
  29. matplotlib.pyplot.figure(1)
  30. matplotlib.pyplot.plot(t,df,color='silver',alpha=alpha)
  31. else:
  32. matplotlib.pyplot.figure(0)
  33. matplotlib.pyplot.plot(t1,fv,alpha=alpha1)
  34. matplotlib.pyplot.figure(1)
  35. matplotlib.pyplot.plot(t,df,alpha=alpha1)
  36. if file0:
  37. matplotlib.pyplot.figure(0)
  38. matplotlib.pyplot.savefig(file0)
  39. if file1:
  40. matplotlib.pyplot.figure(1)
  41. matplotlib.pyplot.savefig(file1)
  42. def plotIVFCenter(centerMapSPECT,centerMapCT,m,data,ct,file0=None,file1=None):
  43. um=centerMapSPECT==m
  44. loc=numpy.nonzero(centerMapSPECT==m)
  45. #print(loc)
  46. #get center of weight of voxels that belong to center indicated as IVF sample
  47. x=[numpy.mean(t) for t in loc]
  48. #print(x)
  49. #convert it to index
  50. iSlice=[int(y) for y in x]
  51. #print(iSlice)
  52. d20=data[...,-1]
  53. fig=matplotlib.pyplot.figure(figsize=(12, 3))
  54. axs = fig.subplots(1, 3)
  55. axs[0].imshow(d20[iSlice[0],...],cmap='gray_r')
  56. axs[0].imshow(um[iSlice[0],...],alpha=0.2,cmap='Reds')
  57. axs[1].imshow(d20[:,iSlice[1],:],cmap='gray_r')
  58. axs[1].imshow(um[:,iSlice[1],:],alpha=0.2,cmap='Reds')
  59. axs[2].imshow(d20[...,iSlice[2]],cmap='gray_r')
  60. axs[2].imshow(um[...,iSlice[2]],alpha=0.2,cmap='Reds')
  61. if file0:
  62. matplotlib.pyplot.savefig(file0)
  63. #overlay it over CT
  64. um1=centerMapCT==m
  65. loc1=numpy.nonzero(um1)
  66. #print(loc1)
  67. x1=[numpy.mean(t) for t in loc1]
  68. #print(x1)
  69. iSlice1=[int(y) for y in x1]
  70. #print(iSlice1)
  71. fig=matplotlib.pyplot.figure(figsize=(12, 3))
  72. axs = fig.subplots(1, 3)
  73. axs[0].imshow(ct[iSlice1[0],...],cmap='gray')
  74. axs[0].imshow(um1[iSlice1[0],...],alpha=0.4,cmap='Reds')
  75. axs[1].imshow(ct[:,iSlice1[1],:],aspect='auto',origin='lower',cmap='gray')
  76. axs[1].imshow(um1[:,iSlice1[1],:],alpha=0.4,aspect='auto',origin='lower',cmap='Reds')
  77. axs[2].imshow(ct[...,iSlice1[2]],aspect='auto',origin='lower',cmap='gray')
  78. axs[2].imshow(um1[...,iSlice1[2]],alpha=0.4,aspect='auto',origin='lower',cmap='Reds')
  79. if file1:
  80. matplotlib.pyplot.savefig(file1)
  81. def plotIVFRealizations(t,ivf,samples,threshold,nplot=50,file=None):
  82. fit=fitData.getFit(samples,threshold)
  83. t1=numpy.linspace(0,numpy.max(t),300)
  84. #cPar=fit.mu
  85. #fv=fitModel.fIVF(t1,cPar)
  86. w=numpy.ones(t.shape)
  87. fun=functools.partial(fitModel.fDiff,fitModel.fIVF,t,ivf,w)
  88. ig=fitData.generateIVF()
  89. par,bounds=ig.generate()
  90. ivfSamples=fitData.generateGauss(fit,bounds,nplot)
  91. matplotlib.pyplot.figure()
  92. matplotlib.pyplot.scatter(t,ivf)
  93. for i in range(nplot):
  94. cPar=ivfSamples[:,i]
  95. fv=fitModel.fIVF(t1,cPar)
  96. #df=fun(cPar)
  97. matplotlib.pyplot.plot(t1,fv,alpha=0.05,color='blue')
  98. #matplotlib.pyplot.plot(t,df)
  99. if file:
  100. matplotlib.pyplot.savefig(file)
  101. def plotSamples(t,evalArray,file0=None,file1=None):
  102. igIVF=fitData.generateIVF()
  103. ig=fitData.generateCModel()
  104. nIVF=igIVF.getN()
  105. nC=ig.getN()
  106. t1=numpy.linspace(0,numpy.max(t),300)
  107. f1=matplotlib.pyplot.figure()
  108. f2=matplotlib.pyplot.figure()
  109. matplotlib.pyplot.figure(f1.number)
  110. for x in evalArray:
  111. matplotlib.pyplot.scatter(t,x[1],color=x[2])
  112. for x in evalArray:
  113. samples=x[0]
  114. color=x[2]
  115. qf=x[1]
  116. chi2=samples[0,:]
  117. median=numpy.median(chi2)
  118. n=chi2.shape[0]
  119. alpha=1/n
  120. alpha1=0.5
  121. for j in range(n):
  122. cPar=samples[1:1+nC,j]
  123. ivfPar=samples[1+nC:,j]
  124. fv=fitModel.fCompartment(ivfPar,t1,cPar)
  125. fc1=functools.partial(fitModel.fCompartment,ivfPar)
  126. fun=functools.partial(fitModel.fDiff,fc1,t,qf,numpy.ones(t.shape))
  127. df=fun(cPar)
  128. cost=chi2[j]
  129. c1=(df*df).sum()
  130. print(f'{cost}/{c1}')
  131. if cost>median:
  132. matplotlib.pyplot.figure(f1.number)
  133. matplotlib.pyplot.plot(t1,fv,color='silver',alpha=alpha)
  134. matplotlib.pyplot.figure(f2.number)
  135. matplotlib.pyplot.plot(t,df,color='silver',alpha=alpha)
  136. else:
  137. matplotlib.pyplot.figure(f1.number)
  138. matplotlib.pyplot.plot(t1,fv,color=color,alpha=alpha1)
  139. matplotlib.pyplot.figure(f2.number)
  140. matplotlib.pyplot.plot(t,df,color=color,alpha=alpha1)
  141. if file0:
  142. matplotlib.pyplot.figure(f1.number)
  143. matplotlib.pyplot.savefig(file0)
  144. if file1:
  145. matplotlib.pyplot.figure(f2.number)
  146. matplotlib.pyplot.savefig(file1)
  147. def plotSegmentation(r,setup,spect,vmax=1000,file0=None,fb=None):
  148. fp={}
  149. for q in rows:
  150. if q['regionId']==0:
  151. slc=[q['x'],q['y'],q['z']]
  152. slc=[int(x) for x in slc]
  153. slices=q['sliceId'].split(';')
  154. for s in slices:
  155. try:
  156. fp[s].append([float(x) for x in [q['x'],q['y'],q['z']]])
  157. except KeyError:
  158. fp[s]=[]
  159. fp[s].append([float(x) for x in [q['x'],q['y'],q['z']]])
  160. cut0=20
  161. w0=20
  162. cut1=20
  163. w1=20
  164. cut2=20
  165. w2=20
  166. vmin=0
  167. nd=3
  168. fig,ax=matplotlib.pyplot.subplots(3,2*nd+1,figsize=(20,12))
  169. for i in numpy.arange(0,2*nd+1):
  170. ax[0,i].set_xlabel('z')
  171. ax[0,i].set_ylabel('x')
  172. ax[0,i].imshow(spect[cut2:cut2+w2,slc[1]-nd+i,cut0:cut0+w0],cmap='gray_r',vmax=vmax,vmin=vmin)
  173. ax[1,i].set_xlabel('x')
  174. ax[1,i].set_ylabel('y')
  175. ax[1,i].imshow(spect[cut2:cut2+w2,cut0:cut0+w0,slc[2]-nd+i].T,cmap='gray_r',vmax=vmax,vmin=vmin)
  176. ax[2,i].set_xlabel('z')
  177. ax[2,i].set_ylabel('y')
  178. ax[2,i].imshow(spect[slc[0]-nd+i,cut1:cut1+w1,cut1:cut1+w1],cmap='gray_r',vmax=vmax,vmin=vmin)
  179. if i==nd:
  180. pt=fp['0']
  181. ax[0,i].scatter([x[2]-cut0 for x in pt],[x[0]-cut2 for x in pt])
  182. pt=fp['1']
  183. ax[1,i].scatter([x[0]-cut2 for x in pt],[x[1]-cut0 for x in pt])
  184. pt=fp['2']
  185. ax[2,i].scatter([x[2]-cut1 for x in pt],[x[1]-cut1 for x in pt])
  186. if i==0:
  187. ax[0,i].text(2,2,pId,fontsize='large')
  188. if file0:
  189. fig.savefig(file0)