pyRadPlan 0.2.3__tar.gz → 0.2.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. {pyradplan-0.2.3/pyRadPlan.egg-info → pyradplan-0.2.4}/PKG-INFO +1 -1
  2. {pyradplan-0.2.3 → pyradplan-0.2.4}/examples/import_matrad_patient.py +1 -0
  3. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/core/np2sitk.py +26 -8
  4. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/dose/engines/_base.py +11 -6
  5. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/dose/engines/_base_pencilbeam.py +107 -28
  6. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/dose/engines/_base_pencilbeam_particle.py +1 -1
  7. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/raytracer/_base.py +24 -12
  8. pyradplan-0.2.4/pyRadPlan/raytracer/_siddon.py +427 -0
  9. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/stf/_beam.py +5 -0
  10. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/stf/_steeringinformation.py +33 -0
  11. {pyradplan-0.2.3 → pyradplan-0.2.4/pyRadPlan.egg-info}/PKG-INFO +1 -1
  12. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyproject.toml +1 -1
  13. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/stf/test_stf.py +59 -0
  14. pyradplan-0.2.3/pyRadPlan/raytracer/_siddon.py +0 -404
  15. {pyradplan-0.2.3 → pyradplan-0.2.4}/LICENSE +0 -0
  16. {pyradplan-0.2.3 → pyradplan-0.2.4}/README.md +0 -0
  17. {pyradplan-0.2.3 → pyradplan-0.2.4}/docs/conf.py +0 -0
  18. {pyradplan-0.2.3 → pyradplan-0.2.4}/examples/carbon_pencilbeam.py +0 -0
  19. {pyradplan-0.2.3 → pyradplan-0.2.4}/examples/helium_pencilbeam.py +0 -0
  20. {pyradplan-0.2.3 → pyradplan-0.2.4}/examples/photon_pencilbeam.py +0 -0
  21. {pyradplan-0.2.3 → pyradplan-0.2.4}/examples/proton_let_optimization.py +0 -0
  22. {pyradplan-0.2.3 → pyradplan-0.2.4}/examples/proton_pencilbeam.py +0 -0
  23. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/__init__.py +0 -0
  24. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/core/__init__.py +0 -0
  25. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/core/_exceptions.py +0 -0
  26. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/core/_grids.py +0 -0
  27. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/core/datamodel.py +0 -0
  28. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/core/resample.py +0 -0
  29. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/cst/__init__.py +0 -0
  30. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/cst/_cst.py +0 -0
  31. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/cst/_voi.py +0 -0
  32. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/ct/__init__.py +0 -0
  33. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/ct/_ct.py +0 -0
  34. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/ct/_hlut.py +0 -0
  35. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/data/machines/carbon_Generic.mat +0 -0
  36. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/data/machines/helium_Generic.mat +0 -0
  37. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/data/machines/photons_Generic.mat +0 -0
  38. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/data/machines/protons_Generic.mat +0 -0
  39. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/data/phantoms/TG119.mat +0 -0
  40. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/data/stf/matRad_stf.mat +0 -0
  41. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/data/stf/matRad_stf_onebeam.mat +0 -0
  42. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/data/stf/matrad_stf_onebeam.mat +0 -0
  43. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/dij/__init__.py +0 -0
  44. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/dij/_compose_beam_dijs.py +0 -0
  45. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/dij/_dij.py +0 -0
  46. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/dose/__init__.py +0 -0
  47. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/dose/_calc_dose.py +0 -0
  48. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/dose/engines/__init__.py +0 -0
  49. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/dose/engines/_factory.py +0 -0
  50. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/dose/engines/_hongpb.py +0 -0
  51. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/dose/engines/_svdpb.py +0 -0
  52. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/geometry/__init__.py +0 -0
  53. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/geometry/lps.py +0 -0
  54. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/io/__init__.py +0 -0
  55. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/io/_matlab_file_handler.py +0 -0
  56. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/io/_patient_loader.py +0 -0
  57. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/io/matfile.py +0 -0
  58. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/machines/__init__.py +0 -0
  59. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/machines/_base.py +0 -0
  60. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/machines/_ions.py +0 -0
  61. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/machines/_load.py +0 -0
  62. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/machines/_photons.py +0 -0
  63. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/machines/_validate.py +0 -0
  64. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/matRad/__init__.py +0 -0
  65. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/matRad/_engine.py +0 -0
  66. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/matRad/_matlab.py +0 -0
  67. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/matRad/_octave.py +0 -0
  68. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/__init__.py +0 -0
  69. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/_fluence_optimization.py +0 -0
  70. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/objectives/__init__.py +0 -0
  71. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/objectives/_eud.py +0 -0
  72. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/objectives/_factory.py +0 -0
  73. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/objectives/_max_dvh.py +0 -0
  74. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/objectives/_mean.py +0 -0
  75. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/objectives/_min_dvh.py +0 -0
  76. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/objectives/_objective.py +0 -0
  77. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/objectives/_squared_dev.py +0 -0
  78. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/objectives/_squared_over.py +0 -0
  79. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/objectives/_squared_under.py +0 -0
  80. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/objectives/_std.py +0 -0
  81. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/problems/__init__.py +0 -0
  82. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/problems/_factory.py +0 -0
  83. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/problems/_nonlin_fluence.py +0 -0
  84. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/problems/_optiprob.py +0 -0
  85. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/problems/_simple_least_squares.py +0 -0
  86. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/solvers/__init__.py +0 -0
  87. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/solvers/_base_solvers.py +0 -0
  88. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/solvers/_factory.py +0 -0
  89. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/solvers/_ipopt.py +0 -0
  90. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/optimization/solvers/_scipy_solver.py +0 -0
  91. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/plan/__init__.py +0 -0
  92. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/plan/_plans.py +0 -0
  93. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/quantities/__init__.py +0 -0
  94. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/quantities/_base.py +0 -0
  95. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/quantities/_dose.py +0 -0
  96. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/quantities/_let_x_dose.py +0 -0
  97. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/raytracer/__init__.py +0 -0
  98. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/raytracer/_perf.py +0 -0
  99. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/scenarios/__init__.py +0 -0
  100. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/scenarios/_base.py +0 -0
  101. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/scenarios/_nominal.py +0 -0
  102. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/stf/__init__.py +0 -0
  103. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/stf/_beamlet.py +0 -0
  104. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/stf/_exceptions.py +0 -0
  105. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/stf/_generate_stf.py +0 -0
  106. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/stf/_rangeshifter.py +0 -0
  107. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/stf/_ray.py +0 -0
  108. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/stf/generators/__init__.py +0 -0
  109. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/stf/generators/_base.py +0 -0
  110. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/stf/generators/_externalbeam.py +0 -0
  111. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/stf/generators/_factory.py +0 -0
  112. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/stf/generators/_ions.py +0 -0
  113. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/stf/generators/_photons.py +0 -0
  114. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/util/__init__.py +0 -0
  115. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/util/helpers.py +0 -0
  116. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/visualization/__init__.py +0 -0
  117. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan/visualization/_plot_slice.py +0 -0
  118. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan.egg-info/SOURCES.txt +0 -0
  119. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan.egg-info/dependency_links.txt +0 -0
  120. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan.egg-info/requires.txt +0 -0
  121. {pyradplan-0.2.3 → pyradplan-0.2.4}/pyRadPlan.egg-info/top_level.txt +0 -0
  122. {pyradplan-0.2.3 → pyradplan-0.2.4}/setup.cfg +0 -0
  123. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/conftest.py +0 -0
  124. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/core/test_coordinates.py +0 -0
  125. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/core/test_datamodel.py +0 -0
  126. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/core/test_np2sitk.py +0 -0
  127. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/core/test_resample.py +0 -0
  128. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/cst/conftest.py +0 -0
  129. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/cst/test_VOI.py +0 -0
  130. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/cst/test_cst.py +0 -0
  131. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/ct/test_ct.py +0 -0
  132. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/ct/test_hlut.py +0 -0
  133. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/data/test_validate_test_data.py +0 -0
  134. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/dose/conftest.py +0 -0
  135. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/dose/test_dose_engine_base.py +0 -0
  136. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/dose/test_factory.py +0 -0
  137. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/dose/test_hongpb.py +0 -0
  138. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/dose/test_svdpb.py +0 -0
  139. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/geometry/test_lps.py +0 -0
  140. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/optimization/test_fluence_optiprob.py +0 -0
  141. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/optimization/test_ipopt_solver.py +0 -0
  142. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/optimization/test_objectives.py +0 -0
  143. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/optimization/test_problem_factory.py +0 -0
  144. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/optimization/test_scipy_solver.py +0 -0
  145. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/optimization/test_solver_factory.py +0 -0
  146. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/quantities/test_dose_quantity.py +0 -0
  147. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/quantities/test_let_dose_quantity.py +0 -0
  148. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/stf/test_beamlet.py +0 -0
  149. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/stf/test_stf_gen.py +0 -0
  150. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/test_dij.py +0 -0
  151. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/test_fileHandling.py +0 -0
  152. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/test_grid.py +0 -0
  153. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/test_helpers.py +0 -0
  154. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/test_machine_model.py +0 -0
  155. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/test_matlabEngine.py +0 -0
  156. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/test_octaveEngine.py +0 -0
  157. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/test_plan.py +0 -0
  158. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/test_raytracersiddon.py +0 -0
  159. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/test_scenarios.py +0 -0
  160. {pyradplan-0.2.3 → pyradplan-0.2.4}/test/visualization/test_plot_slice.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyRadPlan
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Summary: A python interface and reimplementation to matRad
5
5
  Author-email: Niklas Wahl <n.wahl@dkfz-heidelberg.de>, Goran Stanic <goran.stanic@dkfz-heidelberg.de>, Tim Ortkamp <tim.ortkamp@kit.edu>, Tobias Becher <t.becher@dkfz-heidelberg.de>, Lina Bucher <lina.bucher@dkfz-heidelberg.de>, Florian Leininger <florian.leininger@dkfz-heidelberg.de>, Antonios Kontopoulos <antonios.kontopoulos@dkfz-heidelberg.de>
6
6
  Maintainer-email: Niklas Wahl <n.wahl@dkfz-heidelberg.de>, Goran Stanic <goran.stanic@dkfz-heidelberg.de>, Tim Ortkamp <tim.ortkamp@kit.edu>, Tobias Becher <t.becher@dkfz-heidelberg.de>
@@ -73,6 +73,7 @@ stf = stfgen.generate(ct, cst)
73
73
  # For that, we use a default HU->rSP table to convert our CT to water-equivalent thickness and then
74
74
  # call a voxel-wise RayTracing algorithm (proposed by Siddon) to calculate the radiological depth.
75
75
  rt = RayTracerSiddon([ct.compute_wet(default_hlut())])
76
+ rt.debug_core_performance = True
76
77
  rad_depth_cubes = rt.trace_cubes(stf[0])
77
78
 
78
79
  # We could write the image if we want, but we don't do this by default.
@@ -80,7 +80,10 @@ def linear_indices_to_sitk_mask(
80
80
 
81
81
 
82
82
  def linear_indices_to_grid_coordinates(
83
- indices: np.ndarray, grid: Grid, index_type: Literal["numpy", "sitk"] = "numpy"
83
+ indices: np.ndarray,
84
+ grid: Grid,
85
+ index_type: Literal["numpy", "sitk"] = "numpy",
86
+ dtype: np.dtype = np.float64,
84
87
  ) -> np.ndarray:
85
88
  """
86
89
  Convert linear indices to gridcoordinates.
@@ -94,6 +97,8 @@ def linear_indices_to_grid_coordinates(
94
97
  index_type : Literal["numpy", "sitk"], optional
95
98
  The ordering of the indices. Can be 'sitk' for Fortran-like index
96
99
  ordering or 'numpy' for C-like index ordering. Default is 'numpy'.
100
+ dtype : np.dtype, optional
101
+ The data type of the output coordinates. Default is np.float64.
97
102
 
98
103
  Returns
99
104
  -------
@@ -101,17 +106,25 @@ def linear_indices_to_grid_coordinates(
101
106
  A 2D numpy array of image coordinates.
102
107
  """
103
108
 
109
+ # this is a manual reimplementation of np.unravel_index
110
+ # to avoid the overhead of creating a tuple of arrays
104
111
  if index_type == "numpy":
105
- z, y, x = np.unravel_index(indices, grid.dimensions[::-1])
112
+ _, d1, d2 = grid.dimensions[::-1]
113
+ order = [0, 1, 2]
106
114
  elif index_type == "sitk":
107
- x, y, z = np.unravel_index(indices, grid.dimensions)
115
+ _, d1, d2 = grid.dimensions
116
+ order = [2, 1, 0]
108
117
  else:
109
118
  raise ValueError("Invalid index type. Must be 'numpy' or 'sitk'.")
110
119
 
111
- v = np.array([x, y, z])
120
+ v = np.empty((3, np.asarray(indices).size), dtype=dtype)
121
+ tmp, v[order[0]] = np.divmod(indices, d2)
122
+ v[order[2]], v[order[1]] = np.divmod(tmp, d1)
112
123
 
113
- spacing_diag = np.diag([grid.resolution["x"], grid.resolution["y"], grid.resolution["z"]])
114
- origin = grid.origin
124
+ spacing_diag = np.diag(
125
+ [grid.resolution["x"], grid.resolution["y"], grid.resolution["z"]]
126
+ ).astype(dtype)
127
+ origin = grid.origin.astype(dtype)
115
128
 
116
129
  physical_point = origin + np.matmul(np.matmul(grid.direction, spacing_diag), v).T
117
130
 
@@ -119,7 +132,10 @@ def linear_indices_to_grid_coordinates(
119
132
 
120
133
 
121
134
  def linear_indices_to_image_coordinates(
122
- indices: np.ndarray, image: sitk.Image, index_type: Literal["numpy", "sitk"] = "numpy"
135
+ indices: np.ndarray,
136
+ image: sitk.Image,
137
+ index_type: Literal["numpy", "sitk"] = "numpy",
138
+ dtype: np.dtype = np.float64,
123
139
  ) -> np.ndarray:
124
140
  """
125
141
  Convert linear indices to image coordinates.
@@ -133,6 +149,8 @@ def linear_indices_to_image_coordinates(
133
149
  index_type : Literal["numpy", "sitk"], optional
134
150
  The ordering of the indices. Can be 'sitk' for Fortran-like index
135
151
  ordering or 'numpy' for C-like index ordering. Default is 'numpy'.
152
+ dtype : np.dtype, optional
153
+ The data type of the output coordinates. Default is np.float64.
136
154
 
137
155
  Returns
138
156
  -------
@@ -141,4 +159,4 @@ def linear_indices_to_image_coordinates(
141
159
  """
142
160
 
143
161
  grid = Grid.from_sitk_image(image)
144
- return linear_indices_to_grid_coordinates(indices, grid, index_type)
162
+ return linear_indices_to_grid_coordinates(indices, grid, index_type, dtype=dtype)
@@ -470,12 +470,7 @@ class DoseEngineBase(ABC):
470
470
  dij["ct_grid"] = ct.grid
471
471
 
472
472
  if self.dose_grid is None:
473
- logger.info(
474
- "Dose grid not set. Using default resolution: x=%f, y=%f, z=%f",
475
- ct.grid.resolution["x"],
476
- ct.grid.resolution["y"],
477
- ct.grid.resolution["z"],
478
- )
473
+ logger.info("Dose Grid not set. Using default resolution.")
479
474
  self.dose_grid = ct.grid.resample({"x": 5.0, "y": 5.0, "z": 5.0})
480
475
 
481
476
  if not isinstance(self.dose_grid, Grid):
@@ -484,6 +479,16 @@ class DoseEngineBase(ABC):
484
479
  except ValidationError:
485
480
  self.dose_grid = ct.grid.resample(self.dose_grid["resolution"])
486
481
 
482
+ logger.info(
483
+ "Dose Grid has Dimensions (%d,%d,%d) with resolution x=%f, y=%f, z=%f.",
484
+ self.dose_grid.dimensions[0],
485
+ self.dose_grid.dimensions[1],
486
+ self.dose_grid.dimensions[2],
487
+ self.dose_grid.resolution["x"],
488
+ self.dose_grid.resolution["y"],
489
+ self.dose_grid.resolution["z"],
490
+ )
491
+
487
492
  dij["dose_grid"] = self.dose_grid
488
493
 
489
494
  # Meta information for dij #TODO: Change dij Model in future
@@ -1,7 +1,7 @@
1
1
  """Base class for pencil beam dose calculation algorithms."""
2
2
 
3
3
  from abc import abstractmethod
4
- from typing import cast, Any, Literal
4
+ from typing import Any, Literal
5
5
  import warnings
6
6
  import logging
7
7
  import time
@@ -97,6 +97,7 @@ class PencilBeamEngineAbstract(DoseEngineBase):
97
97
  ssd_density_threshold: float
98
98
  use_given_eq_density_cube: bool
99
99
  ignore_outside_densities: bool
100
+ trace_on_dose_grid: bool
100
101
  cube_wed: sitk.Image
101
102
  hlut: np.ndarray
102
103
 
@@ -107,6 +108,7 @@ class PencilBeamEngineAbstract(DoseEngineBase):
107
108
  self.ssd_density_threshold: float = 0.0500
108
109
  self.use_given_eq_density_cube: bool = False
109
110
  self.ignore_outside_densities: bool = True
111
+ self.trace_on_dose_grid: bool = True
110
112
  self.cube_wed = None
111
113
  self.hlut = None
112
114
 
@@ -114,6 +116,7 @@ class PencilBeamEngineAbstract(DoseEngineBase):
114
116
  self._effective_lateral_cutoff = None
115
117
  self._num_of_bixels_container = None
116
118
  self._rad_depth_cubes = []
119
+ self._raytracer = None
117
120
 
118
121
  super().__init__(pln)
119
122
 
@@ -275,6 +278,18 @@ class PencilBeamEngineAbstract(DoseEngineBase):
275
278
  # Allocate memory for quantity containers
276
279
  dij = self._allocate_quantity_matrices(dij, ["physical_dose"])
277
280
 
281
+ # Initialize ray-tracer
282
+ if self.trace_on_dose_grid:
283
+ wed_cube_trace = resample_image(
284
+ input_image=self.cube_wed,
285
+ interpolator=sitk.sitkLinear,
286
+ target_grid=self.dose_grid,
287
+ )
288
+ else:
289
+ wed_cube_trace = self.cube_wed
290
+
291
+ self._raytracer = RayTracerSiddon([wed_cube_trace])
292
+
278
293
  return dij
279
294
 
280
295
  def _allocate_quantity_matrices(self, dij: dict[str, Any], names: list[str]):
@@ -293,14 +308,18 @@ class PencilBeamEngineAbstract(DoseEngineBase):
293
308
  (self.dose_grid.num_voxels, dij["num_of_beams"]), dtype=np.float32
294
309
  )
295
310
  else:
296
- # This could probably be optimized by using direct access to the
297
- # lil_matrix's data structures
298
- # TODO: we could store a single sparse pattern matrix and then only store
299
- # the values for all quantities for better memory management
300
- dij[q_name].flat[i] = sparse.lil_matrix(
301
- (self._num_of_columns_dij, self.dose_grid.num_voxels),
302
- dtype=np.float32,
303
- )
311
+ # We allocate raw csc sparse matrix structures using
312
+ # a rough estimat ebased on number of voxels and
313
+ # beamlets
314
+ est_nnz = np.fix(
315
+ 5e-4 * self.dose_grid.num_voxels * self._num_of_columns_dij
316
+ ).astype(np.int64)
317
+ dij[q_name].flat[i] = {
318
+ "data": np.empty((est_nnz,), dtype=np.float32),
319
+ "indices": np.empty((est_nnz,), dtype=np.int64),
320
+ "indptr": np.zeros((self._num_of_columns_dij + 1,), dtype=np.int64),
321
+ "nnz": 0,
322
+ }
304
323
 
305
324
  self._computed_quantities.append(q_name)
306
325
 
@@ -360,12 +379,11 @@ class PencilBeamEngineAbstract(DoseEngineBase):
360
379
  # Calculate radiological depth cube
361
380
  logger.info("Calculating radiological depth cube...")
362
381
 
363
- ct_cube = self.cube_wed
364
- raytracer = RayTracerSiddon([ct_cube])
365
-
366
382
  start_time = time.time()
367
383
 
368
- rad_depth_cubes = raytracer.trace_cubes(beam_info["beam"])
384
+ self._raytracer.debug_core_performance = True
385
+ rad_depth_cubes = self._raytracer.trace_cubes(beam_info["beam"])
386
+ self._raytracer.debug_core_performance = False
369
387
 
370
388
  # TODO: add universal time-debugging method
371
389
  end_time = time.time()
@@ -376,15 +394,18 @@ class PencilBeamEngineAbstract(DoseEngineBase):
376
394
  beam_info["rad_depths"] = [None] * len(rad_depth_cubes)
377
395
 
378
396
  for c, rad_depth_cube in enumerate(rad_depth_cubes):
379
- resampled_rad_depth_cube = resample_image(
380
- input_image=rad_depth_cube,
381
- interpolator=sitk.sitkLinear,
382
- target_grid=self.dose_grid,
383
- )
397
+ if self.trace_on_dose_grid:
398
+ rad_depth_cube_dose_grid = rad_depth_cube
399
+ else:
400
+ rad_depth_cube_dose_grid = resample_image(
401
+ input_image=rad_depth_cube,
402
+ interpolator=sitk.sitkLinear,
403
+ target_grid=self.dose_grid,
404
+ )
384
405
  if self.keep_rad_depth_cubes:
385
- self._rad_depth_cubes.append(resampled_rad_depth_cube)
406
+ self._rad_depth_cubes.append(rad_depth_cube_dose_grid)
386
407
 
387
- rad_depth_cube_dosegrid = sitk.GetArrayViewFromImage(resampled_rad_depth_cube)
408
+ rad_depth_cube_dosegrid = sitk.GetArrayViewFromImage(rad_depth_cube_dose_grid)
388
409
  rad_depth_vdose_grid = rad_depth_cube_dosegrid.ravel()[self._vdose_grid]
389
410
 
390
411
  # Find valid coordinates
@@ -484,8 +505,7 @@ class PencilBeamEngineAbstract(DoseEngineBase):
484
505
  ray_pos_bev = np.array([ray["ray_pos_bev"] for ray in beam["rays"]])
485
506
  target_points = np.array([ray["target_point"] for ray in beam["rays"]])
486
507
 
487
- raytracer = RayTracerSiddon([self.cube_wed])
488
- alpha, _, rho, d12, _ = raytracer.trace_rays(
508
+ alpha, _, rho, d12, _ = self._raytracer.trace_rays(
489
509
  beam["iso_center"],
490
510
  beam["source_point"].reshape((1, 3)),
491
511
  target_points,
@@ -708,10 +728,44 @@ class PencilBeamEngineAbstract(DoseEngineBase):
708
728
  bixel["weight"] * bixel[q_name]
709
729
  )
710
730
  else:
711
- # We insert into the lil sparse matrix
712
- # This could probably be optimized by using direct access to the lil_matrix's
713
- # data structures
714
- dij[q_name][sub_scen_idx][bixel_counter, bixel["ix"]] = bixel[q_name]
731
+ # first we fill the index pointer array
732
+ dij[q_name][sub_scen_idx]["indptr"][bixel_counter + 1] = (
733
+ dij[q_name][sub_scen_idx]["indptr"][bixel_counter] + bixel["ix"].size
734
+ )
735
+
736
+ # If we haven't preallocated enough, we need to expand the arrays
737
+ if (
738
+ dij[q_name][sub_scen_idx]["data"].size
739
+ < dij[q_name][sub_scen_idx]["nnz"] + bixel["ix"].size
740
+ ):
741
+ logger.debug("Resizing data and indices arrays for %s...", q_name)
742
+
743
+ # We estimate the required size by the remaining
744
+ # number of beamlets / columns in the sparse matrix
745
+ # 1 additional beamlet is added
746
+ shape_resize = (self._num_of_columns_dij - bixel_counter) * (
747
+ bixel["ix"].size + 1
748
+ )
749
+ shape_resize += dij[q_name][sub_scen_idx]["data"].size
750
+ dij[q_name][sub_scen_idx]["data"].resize((shape_resize,), refcheck=False)
751
+ dij[q_name][sub_scen_idx]["indices"].resize(
752
+ (shape_resize,), refcheck=False
753
+ )
754
+
755
+ # Fill the corresponding values and indices using indptr
756
+ dij[q_name][sub_scen_idx]["data"][
757
+ dij[q_name][sub_scen_idx]["indptr"][bixel_counter] : dij[q_name][
758
+ sub_scen_idx
759
+ ]["indptr"][bixel_counter + 1]
760
+ ] = bixel[q_name]
761
+ dij[q_name][sub_scen_idx]["indices"][
762
+ dij[q_name][sub_scen_idx]["indptr"][bixel_counter] : dij[q_name][
763
+ sub_scen_idx
764
+ ]["indptr"][bixel_counter + 1]
765
+ ] = bixel["ix"]
766
+
767
+ # Store how many nnzs we actually have in the matrix
768
+ dij[q_name][sub_scen_idx]["nnz"] += bixel["ix"].size
715
769
 
716
770
  # Bookkeeping of bixel numbers
717
771
  # remember beam and bixel number
@@ -749,9 +803,34 @@ class PencilBeamEngineAbstract(DoseEngineBase):
749
803
  # Loop over all used quantities
750
804
  for q_name in self._computed_quantities:
751
805
  if not self._calc_dose_direct:
752
- tmp_matrix = cast(sparse.lil_matrix, dij[q_name].flat[i])
753
- tmp_matrix = tmp_matrix.tocsr().T
806
+ # tmp_matrix = cast(sparse.lil_matrix, dij[q_name].flat[i])
807
+ # tmp_matrix = tmp_matrix.tocsr().T
808
+ data_dict = dij[q_name].flat[i]
809
+
810
+ # Resize to the actual number of non-zero elements
811
+ data_dict["data"].resize((data_dict["nnz"],), refcheck=False)
812
+ data_dict["indices"].resize((data_dict["nnz"],), refcheck=False)
813
+
814
+ # Create the matrix, avoid copies
815
+ tmp_matrix = sparse.csc_array(
816
+ (data_dict["data"], data_dict["indices"], data_dict["indptr"]),
817
+ shape=(self.dose_grid.num_voxels, self._num_of_columns_dij),
818
+ dtype=np.float32,
819
+ copy=False,
820
+ )
821
+
822
+ # Do we need this?
754
823
  tmp_matrix.eliminate_zeros()
824
+
825
+ # make sure indices are sorted and matrix is canonical
826
+ if not tmp_matrix.has_sorted_indices:
827
+ logger.debug("Sorting indices for %s...", q_name)
828
+ tmp_matrix.sort_indices()
829
+
830
+ if not tmp_matrix.has_canonical_format:
831
+ logger.debug("Matrix is not in canonical format for %s...", q_name)
832
+ tmp_matrix.sum_duplicates()
833
+
755
834
  dij[q_name].flat[i] = tmp_matrix
756
835
 
757
836
  if self.keep_rad_depth_cubes and self._rad_depth_cubes:
@@ -55,7 +55,7 @@ class ParticlePencilBeamEngineAbstract(PencilBeamEngineAbstract):
55
55
  self.calc_let = True
56
56
  self.calc_bio_dose = False
57
57
  self.air_offset_correction = True
58
- self.lateral_model = "auto"
58
+ self.lateral_model = "fastest"
59
59
  self.cut_off_method = "integral"
60
60
 
61
61
  # Protected properties with public get access
@@ -34,11 +34,13 @@ class RayTracerBase(ABC):
34
34
  cubes = [cubes]
35
35
  self._cubes = cubes
36
36
  self._initialize_geometry()
37
+ self._coords = None
37
38
 
38
39
  def __init__(self, cubes: Union[sitk.Image, list[sitk.Image]]):
39
40
  self.lateral_cut_off = 50.0
40
41
  self.precision = np.float32
41
42
  self.cubes = cubes
43
+ self._coords = None
42
44
 
43
45
  def trace_rays(
44
46
  self,
@@ -144,19 +146,20 @@ class RayTracerBase(ABC):
144
146
  if not isinstance(beam, Beam):
145
147
  beam = Beam.model_validate(beam)
146
148
 
147
- logger.debug("Initializing geometry...")
148
149
  t_trace_start = time.perf_counter()
149
- self._initialize_geometry()
150
-
151
- # Obtain coordinates
152
- cube_ix = np.arange(self.cubes[0].GetNumberOfPixels(), dtype=np.int64)
153
- coords = linear_indices_to_image_coordinates(cube_ix, self.cubes[0], index_type="sitk")
150
+ logger.debug("Computing coordinates...")
151
+ if self._coords is None:
152
+ # Obtain coordinates
153
+ cube_ix = np.arange(self.cubes[0].GetNumberOfPixels(), dtype=np.int64)
154
+ self._coords = linear_indices_to_image_coordinates(
155
+ cube_ix, self.cubes[0], index_type="sitk", dtype=self.precision
156
+ )
154
157
 
155
158
  # obtain rotation matrix
156
159
  rot_mat = lps.get_beam_rotation_matrix(beam.gantry_angle, beam.couch_angle)
157
160
 
158
161
  # rotate coordinates
159
- coords = (coords - beam.iso_center) @ rot_mat - beam.source_point_bev
162
+ coords = (self._coords - beam.iso_center) @ rot_mat - beam.source_point_bev
160
163
  t_trace_end = time.perf_counter()
161
164
  logger.debug("took %s seconds!", t_trace_end - t_trace_start)
162
165
 
@@ -228,8 +231,8 @@ class RayTracerBase(ABC):
228
231
  scale_factor = np.zeros_like(ix, dtype=self.precision)
229
232
  scale_factor[valid_ix] = (ray_matrix_bev_y + beam.sad) / coords[ix[valid_ix], 1]
230
233
 
231
- x_dist = np.ones_like(ix, dtype=self.precision) * np.nan
232
- z_dist = np.ones_like(ix, dtype=self.precision) * np.nan
234
+ x_dist = np.full_like(ix, np.nan, dtype=self.precision)
235
+ z_dist = np.full_like(ix, np.nan, dtype=self.precision)
233
236
 
234
237
  x_dist[valid_ix] = coords[ix[valid_ix], 0] * scale_factor[valid_ix]
235
238
  x_dist = x_dist - ray_matrix_bev[:, 0, np.newaxis]
@@ -245,10 +248,12 @@ class RayTracerBase(ABC):
245
248
  & (z_dist > -ray_selection)
246
249
  & (z_dist <= ray_selection)
247
250
  )
251
+ t_remember_end = time.perf_counter()
248
252
 
249
253
  logger.debug(
250
- "Found %d ray indices for radiological depth calculation",
254
+ "Found %d ray indices for radiological depth calculation (took %s seconds)",
251
255
  np.count_nonzero(ix_remember_from_tracing),
256
+ t_remember_end - t_trace_end,
252
257
  )
253
258
  rad_depth_cubes = [
254
259
  np.nan * np.ones_like(sitk.GetArrayViewFromImage(cube), dtype=self.precision)
@@ -256,13 +261,20 @@ class RayTracerBase(ABC):
256
261
  ]
257
262
 
258
263
  for i, cube in enumerate(rad_depth_cubes):
259
- rel_eq_distances = lengths * rho[i]
260
- rel_depths = np.cumsum(rel_eq_distances, axis=1) - rel_eq_distances / 2.0
264
+ rel_depths = lengths * rho[i]
265
+ rel_depths = np.cumsum(rel_depths, axis=1) - rel_depths / 2.0
261
266
  ix_assign = np.unravel_index(ix[ix_remember_from_tracing], cube.shape, order="F")
262
267
  cube[ix_assign] = rel_depths[ix_remember_from_tracing]
263
268
  rad_depth_cubes[i] = sitk.GetImageFromArray(cube)
264
269
  rad_depth_cubes[i].CopyInformation(self.cubes[i])
265
270
 
271
+ t_createcubes_end = time.perf_counter()
272
+
273
+ logger.debug(
274
+ "Radiological depth cube filling took %s seconds",
275
+ t_createcubes_end - t_remember_end,
276
+ )
277
+
266
278
  return rad_depth_cubes
267
279
  # scale_factor[valid_ix] = lengths[valid_ix] / d12[valid_ix]
268
280