plotData.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import matplotlib.pyplot
  2. import fitData
  3. import fitModel
  4. import numpy
  5. import functools
  6. import geometry
  7. def plotIVF(t,ivf,samples,threshold,file0=None,file1=None):
  8. fit=fitData.getFit(samples,threshold)
  9. t1=numpy.linspace(0,numpy.max(t),300)
  10. w=numpy.ones(t.shape)
  11. fun=functools.partial(fitModel.fDiff,fitModel.fIVF,t,ivf,w)
  12. matplotlib.pyplot.figure(0)
  13. matplotlib.pyplot.scatter(t,ivf)
  14. n=samples.shape[1]
  15. alpha=1/n
  16. alpha1=1
  17. for j in range(samples.shape[1]):
  18. cPar=samples[1:,j]
  19. df=fun(cPar)
  20. chi2=(df*df).sum()
  21. cost=samples[0,j]
  22. if chi2!=cost:
  23. print('{}/{} {}'.format(samples[0,j],chi2,cPar))
  24. fv=fitModel.fIVF(t1,cPar)
  25. if chi2>threshold:
  26. matplotlib.pyplot.figure(0)
  27. matplotlib.pyplot.plot(t1,fv,color='silver',alpha=alpha)
  28. matplotlib.pyplot.figure(1)
  29. matplotlib.pyplot.plot(t,df,color='silver',alpha=alpha)
  30. else:
  31. matplotlib.pyplot.figure(0)
  32. matplotlib.pyplot.plot(t1,fv,alpha=alpha1)
  33. matplotlib.pyplot.figure(1)
  34. matplotlib.pyplot.plot(t,df,alpha=alpha1)
  35. if file0:
  36. matplotlib.pyplot.figure(0)
  37. matplotlib.pyplot.savefig(file0)
  38. if file1:
  39. matplotlib.pyplot.figure(1)
  40. matplotlib.pyplot.savefig(file1)
  41. def plotIVFCenter(centerMapSPECT,centerMapCT,m,data,ct,file0=None,file1=None):
  42. um=centerMapSPECT==m
  43. loc=numpy.nonzero(centerMapSPECT==m)
  44. #print(loc)
  45. #get center of weight of voxels that belong to center indicated as IVF sample
  46. x=[numpy.mean(t) for t in loc]
  47. #print(x)
  48. #convert it to index
  49. iSlice=[int(y) for y in x]
  50. #print(iSlice)
  51. d20=data[...,-1]
  52. fig=matplotlib.pyplot.figure(figsize=(12, 3))
  53. axs = fig.subplots(1, 3)
  54. axs[0].imshow(d20[iSlice[0],...],cmap='gray_r')
  55. axs[0].imshow(um[iSlice[0],...],alpha=0.2,cmap='Reds')
  56. axs[1].imshow(d20[:,iSlice[1],:],cmap='gray_r')
  57. axs[1].imshow(um[:,iSlice[1],:],alpha=0.2,cmap='Reds')
  58. axs[2].imshow(d20[...,iSlice[2]],cmap='gray_r')
  59. axs[2].imshow(um[...,iSlice[2]],alpha=0.2,cmap='Reds')
  60. if file0:
  61. matplotlib.pyplot.savefig(file0)
  62. #overlay it over CT
  63. um1=centerMapCT==m
  64. loc1=numpy.nonzero(um1)
  65. #print(loc1)
  66. x1=[numpy.mean(t) for t in loc1]
  67. #print(x1)
  68. iSlice1=[int(y) for y in x1]
  69. #print(iSlice1)
  70. fig=matplotlib.pyplot.figure(figsize=(12, 3))
  71. axs = fig.subplots(1, 3)
  72. axs[0].imshow(ct[iSlice1[0],...],cmap='gray')
  73. axs[0].imshow(um1[iSlice1[0],...],alpha=0.4,cmap='Reds')
  74. axs[1].imshow(ct[:,iSlice1[1],:],aspect='auto',origin='lower',cmap='gray')
  75. axs[1].imshow(um1[:,iSlice1[1],:],alpha=0.4,aspect='auto',origin='lower',cmap='Reds')
  76. axs[2].imshow(ct[...,iSlice1[2]],aspect='auto',origin='lower',cmap='gray')
  77. axs[2].imshow(um1[...,iSlice1[2]],alpha=0.4,aspect='auto',origin='lower',cmap='Reds')
  78. if file1:
  79. matplotlib.pyplot.savefig(file1)
  80. def plotIVFRealizations(t,ivf,samples,threshold,nplot=50,file=None):
  81. fit=fitData.getFit(samples,threshold)
  82. t1=numpy.linspace(0,numpy.max(t),300)
  83. #cPar=fit.mu
  84. #fv=fitModel.fIVF(t1,cPar)
  85. w=numpy.ones(t.shape)
  86. fun=functools.partial(fitModel.fDiff,fitModel.fIVF,t,ivf,w)
  87. ig=fitData.generateIVF()
  88. par,bounds=ig.generate()
  89. ivfSamples=fitData.generateGauss(fit,bounds,nplot)
  90. matplotlib.pyplot.figure()
  91. matplotlib.pyplot.scatter(t,ivf)
  92. for i in range(nplot):
  93. cPar=ivfSamples[:,i]
  94. fv=fitModel.fIVF(t1,cPar)
  95. #df=fun(cPar)
  96. matplotlib.pyplot.plot(t1,fv,alpha=0.05,color='blue')
  97. #matplotlib.pyplot.plot(t,df)
  98. if file:
  99. matplotlib.pyplot.savefig(file)
  100. def plotSamples(t,evalArray,file0=None,file1=None):
  101. igIVF=fitData.generateIVF()
  102. ig=fitData.generateCModel()
  103. nIVF=igIVF.getN()
  104. nC=ig.getN()
  105. t1=numpy.linspace(0,numpy.max(t),300)
  106. f1=matplotlib.pyplot.figure()
  107. f2=matplotlib.pyplot.figure()
  108. matplotlib.pyplot.figure(f1.number)
  109. for x in evalArray:
  110. matplotlib.pyplot.scatter(t,x[1],color=x[2])
  111. for x in evalArray:
  112. samples=x[0]
  113. color=x[2]
  114. qf=x[1]
  115. chi2=samples[0,:]
  116. median=numpy.median(chi2)
  117. n=chi2.shape[0]
  118. alpha=1/n
  119. alpha1=0.5
  120. for j in range(n):
  121. cPar=samples[1:1+nC,j]
  122. ivfPar=samples[1+nC:,j]
  123. fv=fitModel.fCompartment(ivfPar,t1,cPar)
  124. fc1=functools.partial(fitModel.fCompartment,ivfPar)
  125. fun=functools.partial(fitModel.fDiff,fc1,t,qf,numpy.ones(t.shape))
  126. df=fun(cPar)
  127. cost=chi2[j]
  128. c1=(df*df).sum()
  129. print(f'{cost}/{c1}')
  130. if cost>median:
  131. matplotlib.pyplot.figure(f1.number)
  132. matplotlib.pyplot.plot(t1,fv,color='silver',alpha=alpha)
  133. matplotlib.pyplot.figure(f2.number)
  134. matplotlib.pyplot.plot(t,df,color='silver',alpha=alpha)
  135. else:
  136. matplotlib.pyplot.figure(f1.number)
  137. matplotlib.pyplot.plot(t1,fv,color=color,alpha=alpha1)
  138. matplotlib.pyplot.figure(f2.number)
  139. matplotlib.pyplot.plot(t,df,color=color,alpha=alpha1)
  140. if file0:
  141. matplotlib.pyplot.figure(f1.number)
  142. matplotlib.pyplot.savefig(file0)
  143. if file1:
  144. matplotlib.pyplot.figure(f2.number)
  145. matplotlib.pyplot.savefig(file1)