phantom.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import numpy
  2. import csv
  3. def generatePhantom(nx,ny,N):
  4. im=numpy.zeros([nx,ny])
  5. file='/home/studen/software/build/observer_tools/fisherStudy/phantoms/phantom_0_rw_0p6.out'
  6. rows=[]
  7. with open(file) as fobj:
  8. d=csv.reader(fobj,delimiter=' ')
  9. for r in d:
  10. rows.append(r)
  11. px=0.5*nx
  12. py=0.5*ny
  13. phantomSize=25
  14. sx=phantomSize/px
  15. sy=phantomSize/py
  16. fs=0
  17. for r in rows:
  18. #ring
  19. if r[0]=='1':
  20. fr=float(r[3])
  21. area=fr*fr*float(r[4])
  22. fs+=area
  23. r.append(area)
  24. #rim
  25. if r[0]=='2':
  26. fr2=float(r[2])
  27. fr1=float(r[1])
  28. area=float(r[3])*(fr2*fr2-fr1*fr1)
  29. fs+=area
  30. r.append(area)
  31. print("hotspot lookup done")
  32. for i in numpy.arange(N):
  33. xe1=numpy.random.random()
  34. for r in rows:
  35. print(r)
  36. if r[0]=='1':
  37. q=r[5]/fs
  38. if r[0]=='2':
  39. q=r[4]/fs
  40. if xe1>q:
  41. xe1-=q
  42. continue
  43. if r[0]=='1':
  44. qr=numpy.sqrt(numpy.random.random())*float(r[3])
  45. qphi=2*3.14159*numpy.random.random()
  46. qx=float(r[1])+qr*numpy.cos(qphi)
  47. qy=float(r[2])+qr*numpy.sin(qphi)
  48. if r[0]=='2':
  49. qr=float(r[1])+numpy.sqrt(numpy.random.random())*(float(r[2])-float(r[1]))
  50. qphi=2*3.14159*numpy.random.random()
  51. qx=qr*numpy.cos(qphi)
  52. qy=qr*numpy.sin(qphi)
  53. ix=int((qx+phantomSize)/sx)
  54. iy=int((qy+phantomSize)/sy)
  55. im[ix,iy]+=1.0;
  56. break
  57. print("hotspot lookup done")
  58. print("particle done")
  59. return im