Guest User

Untitled

a guest
Jul 18th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.57 KB | None | 0 0
  1. print("\n#####################################################")
  2. print("### Creation of a radiological distance 3D map ####")
  3. print("####################################################")
  4.  
  5. import pickle
  6. import numpy as np
  7. import time
  8. from array import array
  9.  
  10. #The files will be save here:
  11. DVKassocie = "Your Path"
  12. FichierClassementDistanceDVK=DVKassocie+"classementDistanceDVK"
  13.  
  14. # Parameters to choose
  15. TailleFiltre = [49,49,49] #Voxels #Choose a odd number for the voxels number cube
  16. DVKPixelDims = [4.8, 4.8, 4.8] #Size (often in mm for medical DICOM images)
  17. def flatten(x):
  18. result = []
  19. for el in x:
  20. if hasattr(el, "__iter__") and not isinstance(el, str):
  21. result.extend(flatten(el))
  22. else:
  23. result.append(el)
  24. return result
  25.  
  26. ListeDVK1=[]
  27. ListeDVK2=[]
  28.  
  29.  
  30. print("\n## Step 1: 3D map of distance")
  31.  
  32. Center=np.multiply(DVKPixelDims,0.5)
  33. ListeDistanceIndice=[]
  34. temps1=time.time()
  35.  
  36. for z in range(-(TailleFiltre[2]/2-1),TailleFiltre[2]/2):
  37. for y in range(-(TailleFiltre[1]/2-1),TailleFiltre[1]/2):
  38. for x in range(-(TailleFiltre[0]/2-1),TailleFiltre[0]/2):
  39. RayTracing=np.array([(x+0.5)*DVKPixelDims[0],(y+0.5)*DVKPixelDims[1],(z+0.5)*DVKPixelDims[2]])
  40. RayTracingVecteur=RayTracing-Center
  41. DistEtIndice=[np.around(np.linalg.norm(RayTracingVecteur),5), x, y, z]
  42. ListeDistanceIndice.append(DistEtIndice)
  43.  
  44. ###############
  45. ####Rank#
  46. ###############
  47.  
  48. ListeDistanceIndice.sort()
  49. ListeDistanceIndice=ListeDistanceIndice[1:]
  50. ListeDistanceIndiceuh=flatten(ListeDistanceIndice)
  51. temps2=time.time()
  52. print "\nTemps effectif: "+str(temps2-temps1)+" secondes, soit "+str((temps2-temps1)/60)+" minutes."
  53.  
  54.  
  55. myarray = np.asarray(ListeDistanceIndiceuh)
  56. output_file = open(FichierClassementDistanceDVK, 'wb')
  57. float_array = array('d', myarray)
  58. float_array.tofile(output_file)
  59.  
  60. #Reading of distance 3d map previously saved
  61. FichierClassementDistanceDVK=DVKassocie+"classementDistanceDVK"
  62. #Writing of the future output
  63. FichierGrosseListePondDist=DVKassocie+"GrosseListePondDist"
  64.  
  65.  
  66. print("\n## Step 2: 3D map of weighted distance")
  67.  
  68.  
  69. ##############################################
  70. #Fonction intersection
  71.  
  72. def LinePlaneCollision(planeNormal, planePoint, rayDirection, rayPoint, epsilon=1e-6):
  73. ndotu = planeNormal.dot(rayDirection)
  74. if abs(ndotu) < epsilon:
  75. return None,None,None
  76. w = rayPoint - planePoint
  77. si = -planeNormal.dot(w) / ndotu
  78. Psi = w + si * rayDirection + planePoint
  79. if ((Psi[0]-Center[0])**2 + (Psi[1]-Center[1])**2 + (Psi[2]-Center[2])**2)>((rayPoint[0]-Center[0])**2 + (rayPoint[1]-Center[1])**2 + (rayPoint[2]-Center[2])**2) :
  80. return Psi, None
  81. else:
  82. #Index voxel: euclidian division
  83. return Psi, 1
  84.  
  85.  
  86.  
  87. ##########################################################################################################################################
  88. ##########################################################################################################################################
  89. temps1=time.time()
  90.  
  91. ##############################################
  92. #Importation distance matrix
  93. input_file = open(FichierClassementDistanceDVK, 'rb')
  94. ArrayDistanceVoxels = array('d')
  95. ArrayDistanceVoxels.fromstring(input_file.read())
  96. Liste=ArrayDistanceVoxels.tolist()
  97. DistanceVoxels=np.reshape(Liste,(len(Liste)/4,4))
  98.  
  99. Ct=0
  100. GrosseListePondDist=[]
  101. PlaneVectorX=np.array([1.0,0.0,0.0])
  102. PlaneVectorY=np.array([0.0,1.0,0.0])
  103. PlaneVectorZ=np.array([0.0,0.0,1.0])
  104. limXY=(TailleFiltre[2]+2)/2
  105. limYZ=(TailleFiltre[1]+2)/2
  106. limXZ=(TailleFiltre[0]+2)/2
  107. l=0
  108. m=0
  109. n=0
  110. #Path from the closest to the furthest voxel
  111. for XYZ in DistanceVoxels: #[:86]: est un (3,0,0) et [:85]: est un (-3,0,0)
  112. ListeTemp=[]
  113. x=int(XYZ[1])
  114. y=int(XYZ[2])
  115. z=int(XYZ[3])
  116. #No plan at x,y,z=0 neither at x,y,z=max and = max-1 because we need a odd matrix
  117. RayTracingDestination=np.array([(x+0.5)*DVKPixelDims[0],(y+0.5)*DVKPixelDims[1],(z+0.5)*DVKPixelDims[2]])
  118. RayTracingVecteur=RayTracingDestination-Center
  119.  
  120. ListeCoorInterXY=[]
  121. ListeCoorInterXZ=[]
  122. ListeCoorInterYZ=[]
  123. NbXY=0
  124. NbYZ=0
  125. NbXZ=0
  126.  
  127. #Plan XY:
  128.  
  129. for c in range (limXY):
  130. if z>n:#Positive sxis
  131. cc=n
  132. else:#Negative axis
  133. cc=n-2*c-1
  134. PointPlane=([0.0,0.0,(cc+1+c)*DVKPixelDims[2]])
  135. PointIntersection=LinePlaneCollision(PlaneVectorZ,PointPlane,RayTracingVecteur,RayTracingDestination)
  136. if PointIntersection[1] is not None:
  137. NbXY=NbXY+1
  138. ListeCoorInterXY.append(PointIntersection)
  139.  
  140. #Plan YZ:
  141.  
  142. for a in range (limYZ):
  143. if y>m:
  144. aa=m
  145. else:
  146. aa=m-2*a-1
  147. PointPlane=([0.0,(aa+1+a)*DVKPixelDims[1],0.0])
  148. PointIntersection=LinePlaneCollision(PlaneVectorY,PointPlane,RayTracingVecteur,RayTracingDestination)
  149. if PointIntersection[1] is not None:
  150. NbYZ=NbYZ+1
  151. ListeCoorInterYZ.append(PointIntersection)
  152.  
  153.  
  154. #Plan XZ:
  155.  
  156. for b in range (limXZ):
  157. if x>l:
  158. bb=l
  159. else:
  160. bb=l-2*b-1
  161. PointPlane=([(bb+1+b)*DVKPixelDims[0],0.0,0.0])
  162. PointIntersection=LinePlaneCollision(PlaneVectorX,PointPlane,RayTracingVecteur,RayTracingDestination)
  163. if PointIntersection[1] is not None:
  164. NbXZ=NbXZ+1
  165. ListeCoorInterXZ.append(PointIntersection)
  166.  
  167.  
  168.  
  169. ListePondVoxels=[]
  170.  
  171.  
  172. if (NbXY>=NbYZ) and (NbXY>=NbXZ) and (NbXY!=0):
  173. ListeChemin=ListeCoorInterXY
  174. #Addition of distance and coordinates
  175. for Q in range(len(ListeChemin)):
  176. if ListeChemin[Q][1]==None:
  177. ListePondVoxels[Q-1][0]=np.linalg.norm(RayTracingDestination-ListeChemin[Q][0])
  178. break
  179. if cc!=n:#if we are going to negative axis:
  180. ListePondVoxels.append([np.linalg.norm(ListeChemin[Q+2][0]-ListeChemin[Q+1][0]),int(np.round(ListeChemin[Q+1][0][0]/DVKPixelDims[0])),int(np.round(ListeChemin[Q+1][0][1]/DVKPixelDims[1])),int(np.round(ListeChemin[Q+1][0][2]/DVKPixelDims[2]))])
  181. else:
  182. ListePondVoxels.append([np.linalg.norm(ListeChemin[Q+1][0]-ListeChemin[Q][0]),int(np.round(ListeChemin[Q][0][0]/DVKPixelDims[0])),int(np.round(ListeChemin[Q][0][1]/DVKPixelDims[1])),int(np.round(ListeChemin[Q][0][2]/DVKPixelDims[2]))])
  183.  
  184. elif (NbYZ>=NbXY) and (NbYZ>=NbXZ) and (NbYZ!=0):
  185. ListeChemin=ListeCoorInterYZ
  186. for Q in range(len(ListeChemin)):
  187. if ListeChemin[Q][1]==None:
  188. ListePondVoxels[Q-1][0]=np.linalg.norm(RayTracingDestination-ListeChemin[Q][0])
  189. break
  190. if aa!=m:
  191. ListePondVoxels.append([np.linalg.norm(ListeChemin[Q+2][0]-ListeChemin[Q+1][0]),int(np.round(ListeChemin[Q+1][0][0]/DVKPixelDims[0])),int(np.round(ListeChemin[Q+1][0][1]/DVKPixelDims[1])),int(np.round(ListeChemin[Q+1][0][2]/DVKPixelDims[2]))])
  192. else:
  193. ListePondVoxels.append([np.linalg.norm(ListeChemin[Q+1][0]-ListeChemin[Q][0]),int(np.round(ListeChemin[Q][0][0]/DVKPixelDims[0])),int(np.round(ListeChemin[Q][0][1]/DVKPixelDims[1])),int(np.round(ListeChemin[Q][0][2]/DVKPixelDims[2]))])
  194.  
  195. elif (NbXZ>=NbXY) and (NbXZ>=NbYZ) and (NbXZ!=0):
  196. ListeChemin=ListeCoorInterXZ
  197. for Q in range(len(ListeChemin)):
  198.  
  199. if ListeChemin[Q][1]==None:
  200. ListePondVoxels[Q-1][0]=np.linalg.norm(RayTracingDestination-ListeChemin[Q][0])
  201. break
  202. if bb!=l:
  203. ListePondVoxels.append([np.linalg.norm(ListeChemin[Q+2][0]-ListeChemin[Q+1][0]),int(np.round(ListeChemin[Q+1][0][0]/DVKPixelDims[0])),int(np.round(ListeChemin[Q+1][0][1]/DVKPixelDims[1])),int(np.round(ListeChemin[Q+1][0][2]/DVKPixelDims[2]))])
  204. else:
  205. ListePondVoxels.append([np.linalg.norm(ListeChemin[Q+1][0]-ListeChemin[Q][0]),int(np.round(ListeChemin[Q][0][0]/DVKPixelDims[0])),int(np.round(ListeChemin[Q][0][1]/DVKPixelDims[1])),int(np.round(ListeChemin[Q][0][2]/DVKPixelDims[2]))])
  206.  
  207. ListePondVoxels[-1]=[ListePondVoxels[-1][0],int(XYZ[1]),int(XYZ[2]),int(XYZ[3])]
  208. GrosseListePondDist.append(ListePondVoxels)
  209.  
  210.  
  211. temps2=time.time()
  212. print "\nTemps de l'exécution: "+str(temps2-temps1)+" secondes, \n"+" soit "+str((temps2-temps1)/60)+" minutes."
  213.  
  214. output = open(FichierGrosseListePondDist, 'wb')
  215. pickle.dump(GrosseListePondDist,output,-1)
  216. output.close()
  217.  
  218.  
  219. print"\nLast creation: "+str(time.strftime("%A %d %B %Y %H:%M:%S"))
  220.  
  221.  
  222. print "\nEnd"
  223.  
  224. #### For the importation:
  225.  
  226. Badaboum = open(FichierGrosseListePondDist, 'rb')
  227. GrosseListePondDist = pickle.load(Badaboum)
  228.  
  229.  
  230.  
  231. # Some rounding to improve...
  232. # Extreme coordinates as well
Add Comment
Please, Sign In to add comment