pyRadPlan 0.0.1__py3-none-any.whl
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.
- examples/carbon_pencilbeam.py +87 -0
- examples/helium_pencilbeam.py +80 -0
- examples/import_matrad_patient.py +102 -0
- examples/photon_pencilbeam.py +66 -0
- examples/proton_pencilbeam.py +80 -0
- pyRadPlan/GUI.py +5 -0
- pyRadPlan/__init__.py +34 -0
- pyRadPlan/core/__init__.py +15 -0
- pyRadPlan/core/_exceptions.py +9 -0
- pyRadPlan/core/_grids.py +231 -0
- pyRadPlan/core/datamodel.py +68 -0
- pyRadPlan/core/np2sitk.py +147 -0
- pyRadPlan/core/resample.py +130 -0
- pyRadPlan/cst/__init__.py +15 -0
- pyRadPlan/cst/_cst.py +307 -0
- pyRadPlan/cst/_voi.py +603 -0
- pyRadPlan/ct/__init__.py +4 -0
- pyRadPlan/ct/_ct.py +525 -0
- pyRadPlan/ct/_hlut.py +27 -0
- pyRadPlan/data/machines/carbon_Generic.mat +0 -0
- pyRadPlan/data/machines/helium_Generic.mat +0 -0
- pyRadPlan/data/machines/photons_Generic.mat +0 -0
- pyRadPlan/data/machines/protons_Generic.mat +0 -0
- pyRadPlan/data/phantoms/TG119.mat +0 -0
- pyRadPlan/data/stf/matRad_stf.mat +0 -0
- pyRadPlan/data/stf/matrad_stf_onebeam.mat +0 -0
- pyRadPlan/dij/__init__.py +4 -0
- pyRadPlan/dij/_dij.py +410 -0
- pyRadPlan/dose/__init__.py +6 -0
- pyRadPlan/dose/_calc_dose.py +64 -0
- pyRadPlan/dose/engines/__init__.py +17 -0
- pyRadPlan/dose/engines/_base.py +591 -0
- pyRadPlan/dose/engines/_base_pencilbeam.py +806 -0
- pyRadPlan/dose/engines/_base_pencilbeam_particle.py +1015 -0
- pyRadPlan/dose/engines/_factory.py +108 -0
- pyRadPlan/dose/engines/_hongpb.py +75 -0
- pyRadPlan/dose/engines/_svdpb.py +275 -0
- pyRadPlan/geometry/__init__.py +7 -0
- pyRadPlan/geometry/lps.py +122 -0
- pyRadPlan/io/.gitkeep +0 -0
- pyRadPlan/io/__init__.py +3 -0
- pyRadPlan/io/matLabFileHandler.py +25 -0
- pyRadPlan/io/matRad.py +22 -0
- pyRadPlan/io/readHLUT.py +17 -0
- pyRadPlan/io/temp_files/cst.mat +0 -0
- pyRadPlan/io/temp_files/ct.mat +0 -0
- pyRadPlan/io/temp_files/pln.mat +0 -0
- pyRadPlan/io/temp_files/stf_with_separate_rays.mat +0 -0
- pyRadPlan/machines/__init__.py +19 -0
- pyRadPlan/machines/_base.py +164 -0
- pyRadPlan/machines/_ions.py +528 -0
- pyRadPlan/machines/_load.py +48 -0
- pyRadPlan/machines/_photons.py +220 -0
- pyRadPlan/machines/_validate.py +61 -0
- pyRadPlan/matRad/.gitkeep +0 -0
- pyRadPlan/matRad/__init__.py +18 -0
- pyRadPlan/matRad/_engine.py +36 -0
- pyRadPlan/matRad/_matlab.py +26 -0
- pyRadPlan/matRad/_octave.py +51 -0
- pyRadPlan/optimization/__init__.py +12 -0
- pyRadPlan/optimization/_fluenceOptimizer.py +312 -0
- pyRadPlan/optimization/_fluence_optimization.py +57 -0
- pyRadPlan/optimization/components/__init__.py +3 -0
- pyRadPlan/optimization/components/_optimizationProblem.py +162 -0
- pyRadPlan/optimization/components/objectives/_EUD.py +93 -0
- pyRadPlan/optimization/components/objectives/__init__.py +19 -0
- pyRadPlan/optimization/components/objectives/_doseUniformity.py +81 -0
- pyRadPlan/optimization/components/objectives/_maxDVH.py +92 -0
- pyRadPlan/optimization/components/objectives/_meanDose.py +80 -0
- pyRadPlan/optimization/components/objectives/_minDVH.py +92 -0
- pyRadPlan/optimization/components/objectives/_objectiveClass.py +98 -0
- pyRadPlan/optimization/components/objectives/_squaredDeviation.py +82 -0
- pyRadPlan/optimization/components/objectives/_squaredOverdosing.py +84 -0
- pyRadPlan/optimization/components/objectives/_squaredUnderdosing.py +84 -0
- pyRadPlan/optimization/projections/__init__.py +5 -0
- pyRadPlan/optimization/projections/_backProjection.py +59 -0
- pyRadPlan/optimization/projections/_constantRBEProjection.py +26 -0
- pyRadPlan/optimization/projections/_doseProjection.py +25 -0
- pyRadPlan/optimization/solvers/__init__.py +18 -0
- pyRadPlan/optimization/solvers/_ipopt.py +89 -0
- pyRadPlan/optimization/solvers/_scipy_solver.py +120 -0
- pyRadPlan/plan/__init__.py +3 -0
- pyRadPlan/plan/_plans.py +360 -0
- pyRadPlan/raytracer/__init__.py +4 -0
- pyRadPlan/raytracer/_base.py +289 -0
- pyRadPlan/raytracer/_perf.py +227 -0
- pyRadPlan/raytracer/_siddon.py +428 -0
- pyRadPlan/scenarios/__init__.py +102 -0
- pyRadPlan/scenarios/_base.py +374 -0
- pyRadPlan/scenarios/_nominal.py +126 -0
- pyRadPlan/stf/__init__.py +29 -0
- pyRadPlan/stf/_base.py +226 -0
- pyRadPlan/stf/_beam.py +195 -0
- pyRadPlan/stf/_beamlet.py +106 -0
- pyRadPlan/stf/_exceptions.py +14 -0
- pyRadPlan/stf/_externalbeam.py +162 -0
- pyRadPlan/stf/_generate_stf.py +23 -0
- pyRadPlan/stf/_ions.py +255 -0
- pyRadPlan/stf/_photons.py +125 -0
- pyRadPlan/stf/_rangeshifter.py +40 -0
- pyRadPlan/stf/_ray.py +201 -0
- pyRadPlan/stf/_steeringinformation.py +181 -0
- pyRadPlan/stf/base_matrad.py +6 -0
- pyRadPlan/stf/hlutLibrary/matRad_default.hlut +10 -0
- pyRadPlan/stf/loadHLUT.py +63 -0
- pyRadPlan/util/__init__.py +0 -0
- pyRadPlan/util/helpers.py +209 -0
- pyRadPlan-0.0.1.dist-info/LICENSE +28 -0
- pyRadPlan-0.0.1.dist-info/METADATA +70 -0
- pyRadPlan-0.0.1.dist-info/RECORD +137 -0
- pyRadPlan-0.0.1.dist-info/WHEEL +5 -0
- pyRadPlan-0.0.1.dist-info/top_level.txt +3 -0
- test/core/test_coordinates.py +46 -0
- test/core/test_np2sitk.py +178 -0
- test/core/test_resample.py +45 -0
- test/cst/conftest.py +57 -0
- test/cst/test_VOI.py +422 -0
- test/cst/test_cst.py +144 -0
- test/ct/test_ct.py +218 -0
- test/ct/test_hlut.py +10 -0
- test/dose/test_dose_particle.py +234 -0
- test/dose/test_dose_photon.py +22 -0
- test/geometry/test_lps.py +96 -0
- test/stf/test_beamlet.py +53 -0
- test/stf/test_stf.py +205 -0
- test/stf/test_stf_gen.py +295 -0
- test/test_dij.py +238 -0
- test/test_fileHandling.py +37 -0
- test/test_grid.py +148 -0
- test/test_helpers.py +65 -0
- test/test_machine_model.py +155 -0
- test/test_matlabEngine.py +30 -0
- test/test_objectives.py +371 -0
- test/test_octaveEngine.py +30 -0
- test/test_plan.py +158 -0
- test/test_raytracersiddon.py +104 -0
- test/test_scenarios.py +198 -0
pyRadPlan/cst/_voi.py
ADDED
|
@@ -0,0 +1,603 @@
|
|
|
1
|
+
from abc import ABC
|
|
2
|
+
from typing import Any, Dict, List, Union
|
|
3
|
+
from typing_extensions import Annotated, Self
|
|
4
|
+
import warnings
|
|
5
|
+
from pydantic import (
|
|
6
|
+
Field,
|
|
7
|
+
field_validator,
|
|
8
|
+
model_validator,
|
|
9
|
+
computed_field,
|
|
10
|
+
StringConstraints,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
import SimpleITK as sitk
|
|
15
|
+
|
|
16
|
+
from pyRadPlan.core import PyRadPlanBaseModel, np2sitk
|
|
17
|
+
from pyRadPlan.ct import CT
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class VOI(PyRadPlanBaseModel, ABC):
|
|
21
|
+
"""
|
|
22
|
+
Represents a Volume of Interest (VOI).
|
|
23
|
+
|
|
24
|
+
Parameters
|
|
25
|
+
----------
|
|
26
|
+
name : str
|
|
27
|
+
The name of the VOI.
|
|
28
|
+
ct_image : CT
|
|
29
|
+
The CT image where the VOI is defined.
|
|
30
|
+
mask : np.ndarray or sitk.Image
|
|
31
|
+
Boolean mask (using 0,1) for referencing of voxels
|
|
32
|
+
(Multiple allocations possible for robust scenarios)
|
|
33
|
+
alpha_x : float, optional
|
|
34
|
+
The alpha_x value. Defaults to 0.1.
|
|
35
|
+
beta_x : float, optional
|
|
36
|
+
The beta_x value. Defaults to 0.05.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
name: str
|
|
40
|
+
ct_image: CT
|
|
41
|
+
mask: sitk.Image
|
|
42
|
+
alpha_x: float = Field(default=0.1)
|
|
43
|
+
beta_x: float = Field(default=0.05)
|
|
44
|
+
|
|
45
|
+
voi_type: Annotated[str, StringConstraints(strip_whitespace=True, to_upper=True)]
|
|
46
|
+
|
|
47
|
+
@field_validator("mask", mode="before")
|
|
48
|
+
@classmethod
|
|
49
|
+
def validate_mask_type(cls, v: Any) -> Any:
|
|
50
|
+
"""
|
|
51
|
+
Validates the mask type.
|
|
52
|
+
|
|
53
|
+
Parameters
|
|
54
|
+
----------
|
|
55
|
+
v : Any
|
|
56
|
+
The mask value to be validated.
|
|
57
|
+
|
|
58
|
+
Returns
|
|
59
|
+
-------
|
|
60
|
+
sitk.Image
|
|
61
|
+
The validated mask.
|
|
62
|
+
|
|
63
|
+
Raises
|
|
64
|
+
------
|
|
65
|
+
ValueError
|
|
66
|
+
If the mask type is not supported.
|
|
67
|
+
"""
|
|
68
|
+
if isinstance(v, np.ndarray):
|
|
69
|
+
if v.dtype in ["bool", "int"]:
|
|
70
|
+
v = v.astype("uint8")
|
|
71
|
+
if v.dtype != "uint8":
|
|
72
|
+
raise ValueError(
|
|
73
|
+
f"{v.dtype} is not supported for index mask. Please use uint8 or boolean mask."
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
if v.ndim == 3:
|
|
77
|
+
return sitk.GetImageFromArray(v, False)
|
|
78
|
+
|
|
79
|
+
if v.ndim == 4:
|
|
80
|
+
mask = []
|
|
81
|
+
for i in range(v.shape[0]):
|
|
82
|
+
mask.append(sitk.GetImageFromArray(v[i], False))
|
|
83
|
+
v = sitk.JoinSeries(mask)
|
|
84
|
+
return v
|
|
85
|
+
|
|
86
|
+
raise ValueError("Dimensionality not supported!")
|
|
87
|
+
|
|
88
|
+
if isinstance(v, sitk.Image):
|
|
89
|
+
if sitk.GetArrayViewFromImage(v).dtype != "uint8":
|
|
90
|
+
raise ValueError(
|
|
91
|
+
f"""{sitk.GetArrayViewFromImage(v).dtype} is not supported
|
|
92
|
+
for index mask. Please use uint8."""
|
|
93
|
+
)
|
|
94
|
+
return v
|
|
95
|
+
|
|
96
|
+
raise ValueError("mask must be either passed as numpy array or SimpleITK image")
|
|
97
|
+
|
|
98
|
+
@model_validator(mode="after")
|
|
99
|
+
def validate_mask(self):
|
|
100
|
+
"""
|
|
101
|
+
Check if the given indices are valid for the CT image.
|
|
102
|
+
|
|
103
|
+
Raises
|
|
104
|
+
------
|
|
105
|
+
ValueError
|
|
106
|
+
If the mask is not a sitk.Image.
|
|
107
|
+
ValueError
|
|
108
|
+
If the dimensions of the mask do not match the CT image.
|
|
109
|
+
"""
|
|
110
|
+
if not isinstance(self.mask, sitk.Image):
|
|
111
|
+
raise ValueError("Sanity check failed - mask is not a SimpleITK image")
|
|
112
|
+
|
|
113
|
+
# check dimensions of sitk image
|
|
114
|
+
dims = self.mask.GetSize()
|
|
115
|
+
if dims != self.ct_image.cube_hu.GetSize():
|
|
116
|
+
raise ValueError(
|
|
117
|
+
f"Mask provided with dimensions {dims}, "
|
|
118
|
+
f"but ct has dimensions {self.ct_image.cube_hu.GetSize()}"
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
# set image parameters for mask in accordance to ct image
|
|
122
|
+
self.mask.SetOrigin(self.ct_image.cube_hu.GetOrigin())
|
|
123
|
+
self.mask.SetSpacing(self.ct_image.cube_hu.GetSpacing())
|
|
124
|
+
self.mask.SetDirection(self.ct_image.cube_hu.GetDirection())
|
|
125
|
+
return self
|
|
126
|
+
|
|
127
|
+
@computed_field
|
|
128
|
+
@property
|
|
129
|
+
def indices(self) -> np.ndarray:
|
|
130
|
+
"""
|
|
131
|
+
Returns the indices of the voxels in the mask using Fortran/SITK
|
|
132
|
+
convention.
|
|
133
|
+
|
|
134
|
+
Returns
|
|
135
|
+
-------
|
|
136
|
+
np.ndarray
|
|
137
|
+
The indices of the voxels.
|
|
138
|
+
"""
|
|
139
|
+
return np2sitk.sitk_mask_to_linear_indices(self.mask, order="sitk")
|
|
140
|
+
|
|
141
|
+
@computed_field
|
|
142
|
+
@property
|
|
143
|
+
def indices_numpy(self) -> np.ndarray:
|
|
144
|
+
"""
|
|
145
|
+
Returns the indices of the voxels in the mask using C/numpy
|
|
146
|
+
convention.
|
|
147
|
+
|
|
148
|
+
Returns
|
|
149
|
+
-------
|
|
150
|
+
np.ndarray
|
|
151
|
+
The indices of the voxels.
|
|
152
|
+
"""
|
|
153
|
+
return np2sitk.sitk_mask_to_linear_indices(self.mask, order="numpy")
|
|
154
|
+
|
|
155
|
+
@computed_field
|
|
156
|
+
@property
|
|
157
|
+
def _numpy_mask(self) -> np.ndarray:
|
|
158
|
+
"""
|
|
159
|
+
Returns the mask as a numpy array.
|
|
160
|
+
|
|
161
|
+
Returns
|
|
162
|
+
-------
|
|
163
|
+
np.ndarray
|
|
164
|
+
The mask as a numpy array.
|
|
165
|
+
"""
|
|
166
|
+
return sitk.GetArrayViewFromImage(self.mask)
|
|
167
|
+
|
|
168
|
+
@computed_field
|
|
169
|
+
@property
|
|
170
|
+
def num_of_scenarios(self) -> int:
|
|
171
|
+
"""
|
|
172
|
+
Returns the number of scenarios.
|
|
173
|
+
|
|
174
|
+
Returns
|
|
175
|
+
-------
|
|
176
|
+
int
|
|
177
|
+
The number of scenarios.
|
|
178
|
+
"""
|
|
179
|
+
|
|
180
|
+
if self.mask.GetDimension() == 4:
|
|
181
|
+
return self.mask.GetSize()[3]
|
|
182
|
+
|
|
183
|
+
return 1
|
|
184
|
+
|
|
185
|
+
def get_indices(self, order="sitk") -> np.ndarray:
|
|
186
|
+
"""
|
|
187
|
+
Returns the indices of the voxels in the mask.
|
|
188
|
+
|
|
189
|
+
Parameters
|
|
190
|
+
----------
|
|
191
|
+
order : str, optional
|
|
192
|
+
The order of the indices. Defaults to "sitk".
|
|
193
|
+
|
|
194
|
+
Returns
|
|
195
|
+
-------
|
|
196
|
+
np.ndarray
|
|
197
|
+
The indices of the voxels.
|
|
198
|
+
"""
|
|
199
|
+
if order == "numpy":
|
|
200
|
+
return self.indices_numpy
|
|
201
|
+
elif order == "sitk":
|
|
202
|
+
return self.indices
|
|
203
|
+
else:
|
|
204
|
+
raise ValueError(f"Unknown order: {order}")
|
|
205
|
+
|
|
206
|
+
def scenario_indices(self, order_type="numpy") -> List[np.ndarray]:
|
|
207
|
+
"""
|
|
208
|
+
Returns the flattened indices of the individual scenarios.
|
|
209
|
+
|
|
210
|
+
Parameters
|
|
211
|
+
----------
|
|
212
|
+
order_type : str, optional
|
|
213
|
+
The order type. Defaults to "numpy".
|
|
214
|
+
|
|
215
|
+
Returns
|
|
216
|
+
-------
|
|
217
|
+
List[np.ndarray]
|
|
218
|
+
The flattened indices of the individual scenarios.
|
|
219
|
+
"""
|
|
220
|
+
if order_type == "numpy":
|
|
221
|
+
_order = "C"
|
|
222
|
+
elif order_type == "sitk":
|
|
223
|
+
_order = "F"
|
|
224
|
+
else:
|
|
225
|
+
raise ValueError(f"Unknown order type: {order_type}")
|
|
226
|
+
|
|
227
|
+
arr = sitk.GetArrayViewFromImage(self.mask)
|
|
228
|
+
if len(arr.shape) == 3:
|
|
229
|
+
return np.ravel_multi_index(np.argwhere(arr).T, dims=arr.shape, order=_order)
|
|
230
|
+
if len(arr.shape) == 4:
|
|
231
|
+
return [
|
|
232
|
+
np.ravel_multi_index(np.argwhere(arr[i]).T, dims=arr[i].shape, order=_order)
|
|
233
|
+
for i in range(arr.shape[0])
|
|
234
|
+
]
|
|
235
|
+
|
|
236
|
+
raise ValueError("Sanity check failed - mask has invalid dimensions")
|
|
237
|
+
|
|
238
|
+
def masked_ct(self, order_type="numpy") -> sitk.Image | np.ndarray:
|
|
239
|
+
"""
|
|
240
|
+
Returns the masked CT image, either as a numpy array or a SimpleITK
|
|
241
|
+
image.
|
|
242
|
+
|
|
243
|
+
Parameters
|
|
244
|
+
----------
|
|
245
|
+
order_type : str, optional
|
|
246
|
+
The order type. Defaults to "numpy".
|
|
247
|
+
|
|
248
|
+
Returns
|
|
249
|
+
-------
|
|
250
|
+
sitk.Image or np.ndarray
|
|
251
|
+
The masked CT image.
|
|
252
|
+
"""
|
|
253
|
+
|
|
254
|
+
if order_type not in ["numpy", "sitk"]:
|
|
255
|
+
raise ValueError(f"Invalid order type requested: {order_type}")
|
|
256
|
+
|
|
257
|
+
if len(self.mask.GetSize()) == 3:
|
|
258
|
+
masked_ct = sitk.Mask(self.ct_image.cube_hu, self.mask)
|
|
259
|
+
elif len(self.mask.GetSize()) == 4:
|
|
260
|
+
masked_ct = [
|
|
261
|
+
sitk.Mask(self.ct_image.cube_hu[:, :, :, i], self.mask[:, :, :, i])
|
|
262
|
+
for i in range(self.mask.GetSize()[-1])
|
|
263
|
+
]
|
|
264
|
+
masked_ct = sitk.JoinSeries(masked_ct)
|
|
265
|
+
else:
|
|
266
|
+
raise ValueError("Sanity check failed - mask has invalid dimensions")
|
|
267
|
+
|
|
268
|
+
if order_type == "numpy":
|
|
269
|
+
return sitk.GetArrayFromImage(masked_ct)
|
|
270
|
+
if order_type == "sitk":
|
|
271
|
+
return masked_ct
|
|
272
|
+
|
|
273
|
+
raise ValueError(f"Sanity check failed -- Invalid order type requested: {order_type}")
|
|
274
|
+
|
|
275
|
+
@computed_field
|
|
276
|
+
@property
|
|
277
|
+
def scenario_ct_data(self) -> list[np.ndarray]:
|
|
278
|
+
"""
|
|
279
|
+
Returns a list of CT data for the individual scenarios.
|
|
280
|
+
|
|
281
|
+
Returns
|
|
282
|
+
-------
|
|
283
|
+
List[np.ndarray]
|
|
284
|
+
The CT data for the individual scenarios.
|
|
285
|
+
"""
|
|
286
|
+
|
|
287
|
+
mask_np = sitk.GetArrayFromImage(self.mask).astype("bool")
|
|
288
|
+
ct_np = sitk.GetArrayFromImage(self.ct_image.cube_hu)
|
|
289
|
+
|
|
290
|
+
if len(self.mask.GetSize()) == 3:
|
|
291
|
+
return ct_np[mask_np]
|
|
292
|
+
|
|
293
|
+
if len(self.mask.GetSize()) == 4:
|
|
294
|
+
return [ct_np[i][mask_np[i]] for i in range(mask_np.shape[0])]
|
|
295
|
+
|
|
296
|
+
raise ValueError("Sanity Check failed -- Unsupported dimensionality of stored mask")
|
|
297
|
+
|
|
298
|
+
def to_matrad(self, context: str = "mat-file") -> Any:
|
|
299
|
+
"""
|
|
300
|
+
Creates an object that can be interpreted by matRad in the given
|
|
301
|
+
context.
|
|
302
|
+
|
|
303
|
+
Returns
|
|
304
|
+
-------
|
|
305
|
+
Any
|
|
306
|
+
VOI as list to write cell arrays.
|
|
307
|
+
"""
|
|
308
|
+
|
|
309
|
+
if context != "mat-file":
|
|
310
|
+
raise ValueError(f"Context {context} not supported")
|
|
311
|
+
|
|
312
|
+
voi_list = [0] # We store an ID which will be changed by cst if exported from there
|
|
313
|
+
voi_list.append(self.name)
|
|
314
|
+
voi_list.append(self.voi_type)
|
|
315
|
+
if self.num_of_scenarios == 1:
|
|
316
|
+
index_lists = [self.indices_numpy.astype(float)]
|
|
317
|
+
else:
|
|
318
|
+
index_lists = self.scenario_indices(order_type="numpy")
|
|
319
|
+
for i, index_list in enumerate(index_lists):
|
|
320
|
+
index_lists[i] = index_list.astype(float)
|
|
321
|
+
|
|
322
|
+
voi_list.append(index_lists)
|
|
323
|
+
|
|
324
|
+
property_dict = {
|
|
325
|
+
"alphaX": self.alpha_x,
|
|
326
|
+
"betaX": self.beta_x,
|
|
327
|
+
}
|
|
328
|
+
voi_list.append(property_dict)
|
|
329
|
+
|
|
330
|
+
# Will not be populated in here but in cst if exported from there
|
|
331
|
+
objective_dict = {}
|
|
332
|
+
voi_list.append(objective_dict)
|
|
333
|
+
|
|
334
|
+
return voi_list
|
|
335
|
+
|
|
336
|
+
def resample_on_new_ct(self, new_ct: CT) -> Self:
|
|
337
|
+
"""
|
|
338
|
+
Resample on new CT image.
|
|
339
|
+
|
|
340
|
+
Parameters
|
|
341
|
+
----------
|
|
342
|
+
new_ct : CT
|
|
343
|
+
The new CT image to resample the VOI on.
|
|
344
|
+
|
|
345
|
+
Returns
|
|
346
|
+
-------
|
|
347
|
+
Self
|
|
348
|
+
The resampled VOI.
|
|
349
|
+
"""
|
|
350
|
+
|
|
351
|
+
if not isinstance(new_ct, CT):
|
|
352
|
+
raise ValueError("new_ct must be a CT object")
|
|
353
|
+
|
|
354
|
+
if self.mask.GetDimension() == 3:
|
|
355
|
+
new_mask = sitk.Resample(self.mask, new_ct.cube_hu)
|
|
356
|
+
elif self.mask.GetDimension() == 4:
|
|
357
|
+
new_mask = []
|
|
358
|
+
for i in range(self.mask.GetSize()[-1]):
|
|
359
|
+
new_mask.append(sitk.Resample(self.mask[:, :, :, i], new_ct.cube_hu))
|
|
360
|
+
new_mask = sitk.JoinSeries(new_mask)
|
|
361
|
+
else:
|
|
362
|
+
raise ValueError("Sanity check failed -- mask has invalid dimensions")
|
|
363
|
+
|
|
364
|
+
resampled_voi = self.model_copy(update={"mask": new_mask, "ct_image": new_ct})
|
|
365
|
+
|
|
366
|
+
if len(resampled_voi.indices) == 0:
|
|
367
|
+
warnings.warn("Resampling created an empty structure")
|
|
368
|
+
|
|
369
|
+
return resampled_voi
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
class OAR(VOI):
|
|
373
|
+
"""
|
|
374
|
+
Represents an organ at risk (OAR).
|
|
375
|
+
|
|
376
|
+
Attributes
|
|
377
|
+
----------
|
|
378
|
+
Inherits all attributes from Plan.
|
|
379
|
+
|
|
380
|
+
Methods
|
|
381
|
+
-------
|
|
382
|
+
voi_type : str
|
|
383
|
+
Returns the voi_type as 'OAR'.
|
|
384
|
+
"""
|
|
385
|
+
|
|
386
|
+
voi_type: str = "OAR"
|
|
387
|
+
|
|
388
|
+
@field_validator("voi_type", mode="after")
|
|
389
|
+
@classmethod
|
|
390
|
+
def validate_voi_type(cls, v: str) -> str:
|
|
391
|
+
"""
|
|
392
|
+
Validates the voi type for an OAR.
|
|
393
|
+
|
|
394
|
+
Parameters
|
|
395
|
+
----------
|
|
396
|
+
v : str
|
|
397
|
+
The voi type to be validated.
|
|
398
|
+
|
|
399
|
+
Returns
|
|
400
|
+
-------
|
|
401
|
+
str
|
|
402
|
+
The validated voi type.
|
|
403
|
+
|
|
404
|
+
Raises
|
|
405
|
+
------
|
|
406
|
+
ValueError
|
|
407
|
+
If the voi type is not "OAR".
|
|
408
|
+
"""
|
|
409
|
+
|
|
410
|
+
if v != "OAR":
|
|
411
|
+
raise ValueError('VOI type for OAR must be "OAR"')
|
|
412
|
+
return v
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
class Target(VOI):
|
|
416
|
+
"""
|
|
417
|
+
Represents a target VOI.
|
|
418
|
+
|
|
419
|
+
Attributes
|
|
420
|
+
----------
|
|
421
|
+
Inherits all attributes from Plan.
|
|
422
|
+
|
|
423
|
+
Methods
|
|
424
|
+
-------
|
|
425
|
+
voi_type : str
|
|
426
|
+
Returns the voi_type as 'TARGET'.
|
|
427
|
+
"""
|
|
428
|
+
|
|
429
|
+
voi_type: str = "TARGET"
|
|
430
|
+
|
|
431
|
+
@field_validator("voi_type", mode="after")
|
|
432
|
+
@classmethod
|
|
433
|
+
def validate_voi_type(cls, v: str) -> str:
|
|
434
|
+
"""
|
|
435
|
+
Validates the voi type for a Target.
|
|
436
|
+
|
|
437
|
+
Parameters
|
|
438
|
+
----------
|
|
439
|
+
v : str
|
|
440
|
+
The voi type to be validated.
|
|
441
|
+
|
|
442
|
+
Returns
|
|
443
|
+
-------
|
|
444
|
+
str
|
|
445
|
+
The validated voi type.
|
|
446
|
+
|
|
447
|
+
Raises
|
|
448
|
+
------
|
|
449
|
+
ValueError
|
|
450
|
+
If the voi type is not "OAR".
|
|
451
|
+
"""
|
|
452
|
+
|
|
453
|
+
if v != "TARGET":
|
|
454
|
+
raise ValueError('VOI type for a Target must be "TARGET"')
|
|
455
|
+
return v
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
class HelperVOI(VOI):
|
|
459
|
+
"""
|
|
460
|
+
Represents a helper VOI.
|
|
461
|
+
|
|
462
|
+
Attributes
|
|
463
|
+
----------
|
|
464
|
+
Inherits all attributes from Plan.
|
|
465
|
+
|
|
466
|
+
Methods
|
|
467
|
+
-------
|
|
468
|
+
voi_type : str
|
|
469
|
+
Returns the voi_type as 'HELPER'.
|
|
470
|
+
"""
|
|
471
|
+
|
|
472
|
+
voi_type: str = "HELPER"
|
|
473
|
+
|
|
474
|
+
@field_validator("voi_type", mode="after")
|
|
475
|
+
@classmethod
|
|
476
|
+
def validate_voi_type(cls, v: str) -> str:
|
|
477
|
+
"""
|
|
478
|
+
Validates the voi type for a HelperVOI.
|
|
479
|
+
|
|
480
|
+
Parameters
|
|
481
|
+
----------
|
|
482
|
+
v : str
|
|
483
|
+
The voi type to be validated.
|
|
484
|
+
|
|
485
|
+
Returns
|
|
486
|
+
-------
|
|
487
|
+
str
|
|
488
|
+
The validated voi type.
|
|
489
|
+
|
|
490
|
+
Raises
|
|
491
|
+
------
|
|
492
|
+
ValueError
|
|
493
|
+
If the voi type is not "HELPER".
|
|
494
|
+
"""
|
|
495
|
+
if v != "HELPER":
|
|
496
|
+
raise ValueError('VOI type for a HelperVOI must be "HELPER"')
|
|
497
|
+
return v
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
class ExternalVOI(VOI):
|
|
501
|
+
"""
|
|
502
|
+
Represents an external contour limiting voxels to be considered for
|
|
503
|
+
planning (EXTERNAL).
|
|
504
|
+
|
|
505
|
+
Attributes
|
|
506
|
+
----------
|
|
507
|
+
Inherits all attributes from Plan.
|
|
508
|
+
|
|
509
|
+
Methods
|
|
510
|
+
-------
|
|
511
|
+
voi_type : str
|
|
512
|
+
Returns the voi_type as 'EXTERNAL'.
|
|
513
|
+
"""
|
|
514
|
+
|
|
515
|
+
voi_type: str = "EXTERNAL"
|
|
516
|
+
|
|
517
|
+
@field_validator("voi_type", mode="after")
|
|
518
|
+
@classmethod
|
|
519
|
+
def validate_voi_type(cls, v: str) -> str:
|
|
520
|
+
"""
|
|
521
|
+
Validates the voi type for an EXTERNAL contour.
|
|
522
|
+
|
|
523
|
+
Parameters
|
|
524
|
+
----------
|
|
525
|
+
v : str
|
|
526
|
+
The voi type to be validated.
|
|
527
|
+
|
|
528
|
+
Returns
|
|
529
|
+
-------
|
|
530
|
+
str
|
|
531
|
+
The validated voi type.
|
|
532
|
+
|
|
533
|
+
Raises
|
|
534
|
+
------
|
|
535
|
+
ValueError
|
|
536
|
+
If the voi type is not "EXTERNAL".
|
|
537
|
+
"""
|
|
538
|
+
|
|
539
|
+
if v != "EXTERNAL":
|
|
540
|
+
raise ValueError('VOI type for EXTERNAL must be "EXTERNAL"')
|
|
541
|
+
return v
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
__VOITYPES__ = {"OAR": OAR, "TARGET": Target, "HELPER": HelperVOI, "EXTERNAL": ExternalVOI}
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
def create_voi(data: Union[Dict[str, Any], VOI, None] = None, **kwargs) -> VOI:
|
|
548
|
+
"""
|
|
549
|
+
Factory function to create a VOI object.
|
|
550
|
+
|
|
551
|
+
Parameters
|
|
552
|
+
----------
|
|
553
|
+
data : Union[Dict[str, Any], VOI, None]
|
|
554
|
+
Dictionary containing the data to create the VOI object.
|
|
555
|
+
**kwargs
|
|
556
|
+
Arbitrary keyword arguments.
|
|
557
|
+
|
|
558
|
+
Returns
|
|
559
|
+
-------
|
|
560
|
+
VOI
|
|
561
|
+
A VOI object.
|
|
562
|
+
"""
|
|
563
|
+
|
|
564
|
+
if data:
|
|
565
|
+
# If data is already a VOI object, return it directly
|
|
566
|
+
if isinstance(data, VOI):
|
|
567
|
+
return data
|
|
568
|
+
|
|
569
|
+
# obtain voi type if we have a dict including camelCase check
|
|
570
|
+
voi_type = data.get("voi_type", data.get("voiType", None))
|
|
571
|
+
|
|
572
|
+
if voi_type in __VOITYPES__:
|
|
573
|
+
return __VOITYPES__[voi_type].model_validate(data)
|
|
574
|
+
|
|
575
|
+
raise ValueError(f"Invalid VOI type: {voi_type}")
|
|
576
|
+
|
|
577
|
+
else:
|
|
578
|
+
voi_type = kwargs.get("voi_type", "")
|
|
579
|
+
|
|
580
|
+
if voi_type in __VOITYPES__:
|
|
581
|
+
return __VOITYPES__[voi_type](**kwargs)
|
|
582
|
+
|
|
583
|
+
raise ValueError(f"Invalid VOI type: {voi_type}")
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
def validate_voi(data: Union[Dict[str, Any], VOI, None] = None, **kwargs) -> VOI:
|
|
587
|
+
"""
|
|
588
|
+
Validates and creates a VOI object.
|
|
589
|
+
Synonym to create_voi but should be used in validation context.
|
|
590
|
+
|
|
591
|
+
Parameters
|
|
592
|
+
----------
|
|
593
|
+
voi : Union[Dict[str, Any], VOI, None], optional
|
|
594
|
+
Dictionary containing the data to create the VOI object, by default None.
|
|
595
|
+
**kwargs
|
|
596
|
+
Arbitrary keyword arguments.
|
|
597
|
+
|
|
598
|
+
Returns
|
|
599
|
+
-------
|
|
600
|
+
VOI
|
|
601
|
+
A validated VOI object.
|
|
602
|
+
"""
|
|
603
|
+
return create_voi(data, **kwargs)
|
pyRadPlan/ct/__init__.py
ADDED