pyRadPlan 0.2.3__tar.gz → 0.2.5__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 (164) hide show
  1. {pyradplan-0.2.3/pyRadPlan.egg-info → pyradplan-0.2.5}/PKG-INFO +1 -1
  2. {pyradplan-0.2.3 → pyradplan-0.2.5}/examples/import_matrad_patient.py +1 -0
  3. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/__init__.py +3 -0
  4. pyradplan-0.2.5/pyRadPlan/analysis/__init__.py +8 -0
  5. pyradplan-0.2.5/pyRadPlan/analysis/_dvh.py +414 -0
  6. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/core/np2sitk.py +26 -8
  7. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/dose/engines/_base.py +11 -6
  8. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/dose/engines/_base_pencilbeam.py +107 -28
  9. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/dose/engines/_base_pencilbeam_particle.py +1 -1
  10. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/raytracer/_base.py +24 -12
  11. pyradplan-0.2.5/pyRadPlan/raytracer/_siddon.py +427 -0
  12. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/stf/_beam.py +5 -0
  13. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/stf/_steeringinformation.py +33 -0
  14. {pyradplan-0.2.3 → pyradplan-0.2.5/pyRadPlan.egg-info}/PKG-INFO +1 -1
  15. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan.egg-info/SOURCES.txt +4 -0
  16. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyproject.toml +1 -1
  17. pyradplan-0.2.5/test/analysis/test_dvh.py +70 -0
  18. pyradplan-0.2.5/test/analysis/test_qi.py +1 -0
  19. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/stf/test_stf.py +59 -0
  20. pyradplan-0.2.3/pyRadPlan/raytracer/_siddon.py +0 -404
  21. {pyradplan-0.2.3 → pyradplan-0.2.5}/LICENSE +0 -0
  22. {pyradplan-0.2.3 → pyradplan-0.2.5}/README.md +0 -0
  23. {pyradplan-0.2.3 → pyradplan-0.2.5}/docs/conf.py +0 -0
  24. {pyradplan-0.2.3 → pyradplan-0.2.5}/examples/carbon_pencilbeam.py +0 -0
  25. {pyradplan-0.2.3 → pyradplan-0.2.5}/examples/helium_pencilbeam.py +0 -0
  26. {pyradplan-0.2.3 → pyradplan-0.2.5}/examples/photon_pencilbeam.py +0 -0
  27. {pyradplan-0.2.3 → pyradplan-0.2.5}/examples/proton_let_optimization.py +0 -0
  28. {pyradplan-0.2.3 → pyradplan-0.2.5}/examples/proton_pencilbeam.py +0 -0
  29. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/core/__init__.py +0 -0
  30. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/core/_exceptions.py +0 -0
  31. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/core/_grids.py +0 -0
  32. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/core/datamodel.py +0 -0
  33. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/core/resample.py +0 -0
  34. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/cst/__init__.py +0 -0
  35. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/cst/_cst.py +0 -0
  36. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/cst/_voi.py +0 -0
  37. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/ct/__init__.py +0 -0
  38. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/ct/_ct.py +0 -0
  39. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/ct/_hlut.py +0 -0
  40. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/data/machines/carbon_Generic.mat +0 -0
  41. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/data/machines/helium_Generic.mat +0 -0
  42. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/data/machines/photons_Generic.mat +0 -0
  43. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/data/machines/protons_Generic.mat +0 -0
  44. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/data/phantoms/TG119.mat +0 -0
  45. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/data/stf/matRad_stf.mat +0 -0
  46. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/data/stf/matRad_stf_onebeam.mat +0 -0
  47. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/data/stf/matrad_stf_onebeam.mat +0 -0
  48. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/dij/__init__.py +0 -0
  49. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/dij/_compose_beam_dijs.py +0 -0
  50. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/dij/_dij.py +0 -0
  51. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/dose/__init__.py +0 -0
  52. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/dose/_calc_dose.py +0 -0
  53. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/dose/engines/__init__.py +0 -0
  54. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/dose/engines/_factory.py +0 -0
  55. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/dose/engines/_hongpb.py +0 -0
  56. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/dose/engines/_svdpb.py +0 -0
  57. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/geometry/__init__.py +0 -0
  58. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/geometry/lps.py +0 -0
  59. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/io/__init__.py +0 -0
  60. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/io/_matlab_file_handler.py +0 -0
  61. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/io/_patient_loader.py +0 -0
  62. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/io/matfile.py +0 -0
  63. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/machines/__init__.py +0 -0
  64. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/machines/_base.py +0 -0
  65. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/machines/_ions.py +0 -0
  66. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/machines/_load.py +0 -0
  67. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/machines/_photons.py +0 -0
  68. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/machines/_validate.py +0 -0
  69. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/matRad/__init__.py +0 -0
  70. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/matRad/_engine.py +0 -0
  71. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/matRad/_matlab.py +0 -0
  72. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/matRad/_octave.py +0 -0
  73. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/__init__.py +0 -0
  74. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/_fluence_optimization.py +0 -0
  75. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/objectives/__init__.py +0 -0
  76. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/objectives/_eud.py +0 -0
  77. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/objectives/_factory.py +0 -0
  78. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/objectives/_max_dvh.py +0 -0
  79. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/objectives/_mean.py +0 -0
  80. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/objectives/_min_dvh.py +0 -0
  81. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/objectives/_objective.py +0 -0
  82. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/objectives/_squared_dev.py +0 -0
  83. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/objectives/_squared_over.py +0 -0
  84. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/objectives/_squared_under.py +0 -0
  85. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/objectives/_std.py +0 -0
  86. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/problems/__init__.py +0 -0
  87. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/problems/_factory.py +0 -0
  88. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/problems/_nonlin_fluence.py +0 -0
  89. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/problems/_optiprob.py +0 -0
  90. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/problems/_simple_least_squares.py +0 -0
  91. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/solvers/__init__.py +0 -0
  92. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/solvers/_base_solvers.py +0 -0
  93. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/solvers/_factory.py +0 -0
  94. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/solvers/_ipopt.py +0 -0
  95. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/optimization/solvers/_scipy_solver.py +0 -0
  96. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/plan/__init__.py +0 -0
  97. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/plan/_plans.py +0 -0
  98. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/quantities/__init__.py +0 -0
  99. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/quantities/_base.py +0 -0
  100. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/quantities/_dose.py +0 -0
  101. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/quantities/_let_x_dose.py +0 -0
  102. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/raytracer/__init__.py +0 -0
  103. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/raytracer/_perf.py +0 -0
  104. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/scenarios/__init__.py +0 -0
  105. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/scenarios/_base.py +0 -0
  106. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/scenarios/_nominal.py +0 -0
  107. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/stf/__init__.py +0 -0
  108. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/stf/_beamlet.py +0 -0
  109. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/stf/_exceptions.py +0 -0
  110. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/stf/_generate_stf.py +0 -0
  111. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/stf/_rangeshifter.py +0 -0
  112. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/stf/_ray.py +0 -0
  113. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/stf/generators/__init__.py +0 -0
  114. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/stf/generators/_base.py +0 -0
  115. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/stf/generators/_externalbeam.py +0 -0
  116. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/stf/generators/_factory.py +0 -0
  117. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/stf/generators/_ions.py +0 -0
  118. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/stf/generators/_photons.py +0 -0
  119. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/util/__init__.py +0 -0
  120. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/util/helpers.py +0 -0
  121. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/visualization/__init__.py +0 -0
  122. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan/visualization/_plot_slice.py +0 -0
  123. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan.egg-info/dependency_links.txt +0 -0
  124. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan.egg-info/requires.txt +0 -0
  125. {pyradplan-0.2.3 → pyradplan-0.2.5}/pyRadPlan.egg-info/top_level.txt +0 -0
  126. {pyradplan-0.2.3 → pyradplan-0.2.5}/setup.cfg +0 -0
  127. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/conftest.py +0 -0
  128. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/core/test_coordinates.py +0 -0
  129. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/core/test_datamodel.py +0 -0
  130. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/core/test_np2sitk.py +0 -0
  131. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/core/test_resample.py +0 -0
  132. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/cst/conftest.py +0 -0
  133. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/cst/test_VOI.py +0 -0
  134. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/cst/test_cst.py +0 -0
  135. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/ct/test_ct.py +0 -0
  136. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/ct/test_hlut.py +0 -0
  137. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/data/test_validate_test_data.py +0 -0
  138. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/dose/conftest.py +0 -0
  139. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/dose/test_dose_engine_base.py +0 -0
  140. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/dose/test_factory.py +0 -0
  141. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/dose/test_hongpb.py +0 -0
  142. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/dose/test_svdpb.py +0 -0
  143. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/geometry/test_lps.py +0 -0
  144. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/optimization/test_fluence_optiprob.py +0 -0
  145. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/optimization/test_ipopt_solver.py +0 -0
  146. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/optimization/test_objectives.py +0 -0
  147. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/optimization/test_problem_factory.py +0 -0
  148. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/optimization/test_scipy_solver.py +0 -0
  149. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/optimization/test_solver_factory.py +0 -0
  150. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/quantities/test_dose_quantity.py +0 -0
  151. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/quantities/test_let_dose_quantity.py +0 -0
  152. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/stf/test_beamlet.py +0 -0
  153. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/stf/test_stf_gen.py +0 -0
  154. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/test_dij.py +0 -0
  155. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/test_fileHandling.py +0 -0
  156. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/test_grid.py +0 -0
  157. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/test_helpers.py +0 -0
  158. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/test_machine_model.py +0 -0
  159. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/test_matlabEngine.py +0 -0
  160. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/test_octaveEngine.py +0 -0
  161. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/test_plan.py +0 -0
  162. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/test_raytracersiddon.py +0 -0
  163. {pyradplan-0.2.3 → pyradplan-0.2.5}/test/test_scenarios.py +0 -0
  164. {pyradplan-0.2.3 → pyradplan-0.2.5}/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.5
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.
@@ -10,6 +10,7 @@ from .stf._generate_stf import generate_stf
10
10
  from .stf import SteeringInformation, validate_stf
11
11
  from .dose._calc_dose import calc_dose_influence, calc_dose_forward
12
12
  from .optimization._fluence_optimization import fluence_optimization
13
+ from .analysis._dvh import DVH, DVHCollection
13
14
  from .visualization import plot_slice
14
15
  from .io import load_patient, load_tg119
15
16
 
@@ -36,6 +37,8 @@ __all__ = [
36
37
  "calc_dose_influence",
37
38
  "calc_dose_forward",
38
39
  "fluence_optimization",
40
+ "DVH",
41
+ "DVHCollection",
39
42
  "SteeringInformation",
40
43
  "validate_stf",
41
44
  "plot_slice",
@@ -0,0 +1,8 @@
1
+ """Analysis module for pyRadPlan."""
2
+
3
+ from ._dvh import DVH, DVHCollection
4
+
5
+ __all__ = [
6
+ "DVH",
7
+ "DVHCollection",
8
+ ]
@@ -0,0 +1,414 @@
1
+ import logging
2
+ from typing import Union, Optional, Any
3
+ from typing_extensions import Self
4
+ from pydantic import (
5
+ Field,
6
+ computed_field,
7
+ field_validator,
8
+ model_validator,
9
+ )
10
+ from numpydantic import NDArray, Shape
11
+
12
+ import pint
13
+ import numpy as np
14
+ import SimpleITK as sitk
15
+ import matplotlib.pyplot as plt
16
+
17
+ from pyRadPlan.core import PyRadPlanBaseModel
18
+ from pyRadPlan.cst import StructureSet
19
+
20
+ logger = logging.getLogger(__name__)
21
+
22
+ ureg = pint.UnitRegistry()
23
+
24
+
25
+ class DVH(PyRadPlanBaseModel):
26
+ """
27
+ Dose Volume Histogram (DVH) class.
28
+
29
+ Note
30
+ ----
31
+ The DVH class can be used for other quantities as well.
32
+ The bins correspond to the left edge of the bin, thus we have as many bin points as dose points.
33
+
34
+ """
35
+
36
+ diff_volume: NDArray[Shape["*"], np.float64] = Field(
37
+ alias="volumePoints", description="Volume percentages for each point"
38
+ )
39
+
40
+ unit: pint.Unit = Field(default=ureg.gray, description="Unit of the DVH bins")
41
+ name: str = Field(default="DVH", description="Name of the DVH")
42
+
43
+ bin_edges: NDArray[Shape["*"], np.float64] = Field(
44
+ alias="doseGrid", description="Left bin edges for the DVH"
45
+ )
46
+
47
+ @field_validator("unit", mode="before")
48
+ @classmethod
49
+ def validate_unit(cls, v: Any) -> pint.Unit:
50
+ """
51
+ Validate the unit of the DVH bins.
52
+
53
+ Parameters
54
+ ----------
55
+ v : Any
56
+ The unit to validate.
57
+
58
+ Returns
59
+ -------
60
+ pint.Unit: Validated unit of the DVH bins.
61
+ """
62
+
63
+ if isinstance(v, pint.Unit):
64
+ return v
65
+
66
+ try:
67
+ return ureg(v)
68
+ except pint.UndefinedUnitError:
69
+ raise ValueError(f"Invalid unit: {v}")
70
+
71
+ @model_validator(mode="after")
72
+ def check_shapes(self) -> Self:
73
+ """
74
+ Validate the shapes of the DVH data.
75
+
76
+ Returns
77
+ -------
78
+ DVH: The DVH object itself if validation passes.
79
+ """
80
+ if (
81
+ self.diff_volume.ndim != self.bin_edges.ndim
82
+ or self.diff_volume.ndim != 1
83
+ or self.bin_edges.size != self.diff_volume.size + 1
84
+ ):
85
+ raise ValueError("The shape of diff_volume must be consistent with shape of bins.")
86
+ return self
87
+
88
+ @computed_field
89
+ @property
90
+ def cum_volume(self) -> NDArray[Shape["*"], np.float64]:
91
+ """
92
+ Get the cumulative DVH Volume.
93
+
94
+ Returns
95
+ -------
96
+ NDArray: Cumulative DVH volume as a numpy array
97
+ """
98
+ return np.cumsum(self.diff_volume[::-1])[::-1]
99
+
100
+ @computed_field
101
+ @property
102
+ def cumulative(self) -> NDArray[Shape["2,*"], np.float64]:
103
+ """
104
+ Get the cumulative DVH.
105
+
106
+ Gives a 2xnum_points array with the first row being the dose / quantity
107
+ and the second row being the cumulative volume percentages.
108
+
109
+ Returns
110
+ -------
111
+ NDArray: Cumulative DVH
112
+
113
+ Notes
114
+ -----
115
+ The x coordinate (dose/quantity) corresponds to the histograms left
116
+ bin edge, as this corresponds to the meaning: Volume covered by
117
+ "at least" the respective dose/quantity value.
118
+ """
119
+ return np.vstack((self.bins, self.cum_volume))
120
+
121
+ @computed_field
122
+ @property
123
+ def differential(self) -> NDArray[Shape["2,*"], np.float64]:
124
+ """
125
+ Get the differential DVH.
126
+
127
+ Gives a 2xnum_points array with the first row being the dose
128
+ and the second row being the differential volume percentages.
129
+
130
+ Returns
131
+ -------
132
+ NDArray: Differential DVH
133
+
134
+ Notes
135
+ -----
136
+ The x ccoordinate (dose/quantity) corresponds to the histograms
137
+ bin center for the differntial DVH. This is because the differential
138
+ DVH captures the relative volume within a bin, thus the center
139
+ represents the dose/quantity value for that bin best.
140
+ Be aware of this difference to the cumulative DVH.
141
+
142
+ """
143
+ return np.vstack((self.bin_centers, self.diff_volume))
144
+
145
+ @computed_field
146
+ @property
147
+ def num_points(self) -> int:
148
+ """
149
+ Get the number of points in the DVH.
150
+
151
+ Returns
152
+ -------
153
+ int: Number of points in the DVH
154
+ """
155
+ return len(self.diff_volume)
156
+
157
+ @computed_field
158
+ @property
159
+ def bins(self) -> NDArray[Shape["*"], np.float64]:
160
+ """
161
+ Get the DVH bin left edges.
162
+
163
+ This corresponds to the dose / quantity thresholds for the cumulative volume percentages.
164
+
165
+ Returns
166
+ -------
167
+ NDArray: DVH bins as a numpy array
168
+ """
169
+ return self.bin_edges[:-1]
170
+
171
+ @computed_field
172
+ @property
173
+ def has_regular_bins(self) -> bool:
174
+ """
175
+ Check if the DVH has regular bins.
176
+
177
+ Returns
178
+ -------
179
+ bool: True if the DVH has regular bins, False otherwise
180
+ """
181
+ return np.all(np.diff(self.bin_edges) == self.bin_edges[1] - self.bin_edges[0])
182
+
183
+ @computed_field
184
+ @property
185
+ def bin_centers(self) -> NDArray[Shape["*"], np.float64]:
186
+ """
187
+ Get the centers of the DVH bins.
188
+
189
+ Returns
190
+ -------
191
+ NDArray: Centers of the DVH bins as a numpy array
192
+ """
193
+ return (self.bin_edges[:-1] + self.bin_edges[1:]) / 2.0
194
+
195
+ def get_vx(self, x: Union[float, NDArray]) -> float:
196
+ """
197
+ Get the volume percentage for a given dose (or other quantity) value.
198
+
199
+ Parameters
200
+ ----------
201
+ x : float
202
+ The dose/quantity value to get the volume percentage for.
203
+
204
+ Returns
205
+ -------
206
+ float: Volume percentage corresponding to the given dose/quantity value.
207
+ """
208
+ if x < self.bins[0] or x > self.bins[-1]:
209
+ raise ValueError("Dose value is out of bounds of the DVH bins.")
210
+
211
+ # Interpolate to find the volume percentage
212
+ return np.interp(x, self.bins, self.cum_volume)
213
+
214
+ def get_dy(self, y: Union[float, NDArray]) -> float:
215
+ """
216
+ Get the dose (or other quantity) value for a given volume percentage.
217
+
218
+ Parameters
219
+ ----------
220
+ y : float
221
+ The volume percentage to get the dose/quantity value for.
222
+
223
+ Returns
224
+ -------
225
+ float: Dose/quantity value corresponding to the given volume percentage.
226
+ """
227
+ if y < 0 or y > 100:
228
+ raise ValueError("Volume percentage must be between 0 and 100.")
229
+
230
+ # Interpolate to find the dose value
231
+ return np.interp(y / 100, self.cum_volume, self.bins)
232
+
233
+ @classmethod
234
+ def compute(
235
+ cls,
236
+ quantity: Union[sitk.Image, NDArray],
237
+ mask: Optional[Union[sitk.Image, NDArray]] = None,
238
+ num_points: int = 1000,
239
+ max_value: Optional[float] = None,
240
+ min_value: float = 0.0,
241
+ **kwargs,
242
+ ) -> Self:
243
+ """
244
+ Create a DVH from a dose_like quantity and mask.
245
+
246
+ Parameters
247
+ ----------
248
+ quantity : Union[sitk.Image, NDArray]
249
+ The dose-like quantity to update the DVH with. Can be a SimpleITK image or a numpy array.
250
+ mask : Optional[Union[sitk.Image, NDArray]], optional
251
+ The mask to apply to the dose quantity. If None, the whole image / array will be used.
252
+ If provided, it must either have the same shape as the quantity.
253
+ Defaults to None.
254
+ num_points : int, optional
255
+ The number of points in the DVH. Defaults to 100.
256
+ max_value : Optional[float], optional
257
+ The maximum value for the DVH bins. If None, it will be set to 105% of the maximum value in the quantity.
258
+ min_value : float, optional
259
+ The minimum value for the DVH bins. Defaults to 0.0.
260
+ **kwargs: Additional arguments passed to the DVH Model.
261
+
262
+ Returns
263
+ -------
264
+ DVH: DVH Object
265
+ """
266
+
267
+ # manage quantity
268
+ if isinstance(quantity, sitk.Image):
269
+ # Convert SimpleITK image to numpy array
270
+ quantity = sitk.GetArrayFromImage(quantity)
271
+
272
+ # First check if mask is provided and is valid
273
+ if mask is not None:
274
+ if isinstance(mask, sitk.Image):
275
+ # Convert SimpleITK mask to numpy array
276
+ mask = sitk.GetArrayViewFromImage(mask)
277
+
278
+ # Mask has either same dimensionality as quantity, or is a linear mask
279
+ if mask.shape != quantity.shape:
280
+ raise ValueError("Mask must have the same shape as the quantity.")
281
+
282
+ q_array = quantity[mask.astype(np.bool)].flat
283
+ else:
284
+ q_array = quantity.flat
285
+
286
+ if max_value is None:
287
+ max_value = np.max(q_array) * 1.05
288
+
289
+ # Use bin edges that include the last point
290
+ hist, edges = np.histogram(
291
+ q_array, bins=num_points, range=(min_value, max_value), density=False
292
+ )
293
+ # Remove the last bin
294
+ hist = hist / len(q_array) * 100
295
+
296
+ return DVH(diff_volume=hist, bin_edges=edges, **kwargs)
297
+
298
+ def plot(self, ax=None, line_width=2, plot_legend=True, **kwargs):
299
+ """Plot the DVH curve.
300
+
301
+ Parameters
302
+ ----------
303
+ ax : matplotlib.axes.Axes, optional
304
+ Axes to plot on. If None, creates new figure and axes. Defaults to None.
305
+ line_width : float, optional
306
+ Width of the plotted lines. Defaults to 2.
307
+ plot_legend : bool, optional
308
+ Whether to show the legend. Defaults to True.
309
+ **kwargs:
310
+ Additional arguments passed to matplotlib's plot function.
311
+
312
+ Returns
313
+ -------
314
+ matplotlib.axes.Axes: The axes containing the plot
315
+ """
316
+ if ax is None:
317
+ _, ax = plt.subplots()
318
+
319
+ # Plot with or without legend
320
+ if plot_legend:
321
+ ax.plot(
322
+ self.bins,
323
+ self.cum_volume,
324
+ linewidth=line_width,
325
+ label=self.name,
326
+ **kwargs,
327
+ )
328
+ else:
329
+ ax.plot(self.bins, self.cum_volume, linewidth=line_width, **kwargs)
330
+
331
+ # Set labels and legend
332
+ ax.set_xlabel("Dose [Gy]")
333
+ ax.set_ylabel("Volume [%]")
334
+ if plot_legend:
335
+ ax.legend()
336
+
337
+ # Set axis limits with 5% padding
338
+ ax.set_xlim(0, np.max(self.bins) * 1.05)
339
+ ax.set_ylim(0, np.max(self.cumulative) * 1.05)
340
+
341
+ return ax
342
+
343
+
344
+ class DVHCollection(PyRadPlanBaseModel):
345
+ """Collection of DVH curves for multiple structures."""
346
+
347
+ dvhs: list[DVH] = Field(description="List of DVH curves")
348
+
349
+ @classmethod
350
+ def from_structure_set(
351
+ cls,
352
+ cst: StructureSet,
353
+ dose: Union[NDArray, sitk.Image],
354
+ ) -> Self:
355
+ """
356
+ Create DVH collection from a structure set.
357
+
358
+ Parameters
359
+ ----------
360
+ cst : StructureSet
361
+ The structure set containing the VOIs (Volumes of Interest).
362
+ dose : Union[NDArray, sitk.Image]
363
+ The dose-like quantity to compute the DVH from. Can be a SimpleITK image or a numpy array.
364
+
365
+ Returns
366
+ -------
367
+ DVHCollection: Collection of DVH curves for all structures
368
+ """
369
+ dvhs = [DVH.compute(quantity=dose, mask=voi.mask, name=voi.name) for voi in cst.vois]
370
+ return cls(dvhs=dvhs)
371
+
372
+ def plot(
373
+ self,
374
+ structures: Union[list[str], None] = None,
375
+ ax=None,
376
+ line_width=2,
377
+ plot_legend=True,
378
+ **kwargs,
379
+ ):
380
+ """Plot DVH curves for selected or all structures.
381
+
382
+ Parameters
383
+ ----------
384
+ structures : Union[list[str], None], optional
385
+ List of structure names to plot. If None, all structures are plotted. Defaults to None.
386
+ ax : matplotlib.axes.Axes, optional
387
+ Axes to plot on. If None, creates new figure and axes. Defaults to None
388
+ line_width : float, optional
389
+ Width of the plotted lines. Defaults to 2.
390
+ plot_legend : bool, optional
391
+ Whether to show the legend. Defaults to True.
392
+ **kwargs:
393
+ Additional arguments passed to matplotlib's plot function.
394
+
395
+
396
+ Returns
397
+ -------
398
+ matplotlib.axes.Axes: The axes containing the plot
399
+ """
400
+ if ax is None:
401
+ _, ax = plt.subplots()
402
+
403
+ # Filter DVHs based on structure names if provided
404
+ dvhs_to_plot = self.dvhs
405
+ if structures is not None:
406
+ dvhs_to_plot = [dvh for dvh in self.dvhs if dvh.name in structures]
407
+ if not dvhs_to_plot:
408
+ raise ValueError("None of the specified structures found in DVH collection")
409
+
410
+ # Plot each DVH
411
+ for dvh in dvhs_to_plot:
412
+ dvh.plot(ax=ax, line_width=line_width, plot_legend=plot_legend, **kwargs)
413
+
414
+ return ax
@@ -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