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/ct/_ct.py
ADDED
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
"""Module for handling CT images in pyRadPlan. Defines a CT pydantic model and
|
|
2
|
+
factories.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from abc import ABC
|
|
7
|
+
from typing import Annotated, Any, Union
|
|
8
|
+
from typing_extensions import Self
|
|
9
|
+
from pydantic import (
|
|
10
|
+
Field,
|
|
11
|
+
computed_field,
|
|
12
|
+
model_validator,
|
|
13
|
+
)
|
|
14
|
+
import SimpleITK as sitk
|
|
15
|
+
import numpy as np
|
|
16
|
+
from pyRadPlan.core import resample_image
|
|
17
|
+
|
|
18
|
+
from pyRadPlan.core import PyRadPlanBaseModel, Grid
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class CT(PyRadPlanBaseModel, ABC):
|
|
22
|
+
"""
|
|
23
|
+
A class representing a CT (Computed Tomography) image.
|
|
24
|
+
|
|
25
|
+
This class extends PyRadPlanBaseModel and provides functionality to
|
|
26
|
+
handle CT images, including their properties like resolution, size, origin,
|
|
27
|
+
and direction.
|
|
28
|
+
|
|
29
|
+
Attributes
|
|
30
|
+
----------
|
|
31
|
+
cube_hu : sitk.Image
|
|
32
|
+
The CT image data in Hounsfield Units (HU).
|
|
33
|
+
resolution : dict
|
|
34
|
+
The resolution of the CT image in x, y, and z directions.
|
|
35
|
+
size : tuple
|
|
36
|
+
The size of the CT image in x, y, and z directions.
|
|
37
|
+
origin : tuple
|
|
38
|
+
The origin coordinates of the CT image.
|
|
39
|
+
direction : tuple
|
|
40
|
+
The direction cosines of the CT image.
|
|
41
|
+
|
|
42
|
+
Methods
|
|
43
|
+
-------
|
|
44
|
+
to_matrad
|
|
45
|
+
Converts the CT object to a dictionary compatible with matRad format.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
cube_hu: Annotated[sitk.Image, Field(init=False, alias="cubeHU")]
|
|
49
|
+
|
|
50
|
+
@model_validator(mode="before")
|
|
51
|
+
@classmethod
|
|
52
|
+
def validate_cube_hu(
|
|
53
|
+
cls,
|
|
54
|
+
data: Any,
|
|
55
|
+
) -> Any:
|
|
56
|
+
"""
|
|
57
|
+
Validate and convert input data to SimpleITK image format.
|
|
58
|
+
|
|
59
|
+
This method checks if the input data is in the correct format and
|
|
60
|
+
converts numpy arrays to SimpleITK images if necessary. It also applies
|
|
61
|
+
any specified image properties (origin, direction, spacing) to the
|
|
62
|
+
SimpleITK image.
|
|
63
|
+
|
|
64
|
+
Parameters
|
|
65
|
+
----------
|
|
66
|
+
data : Any
|
|
67
|
+
The input data to be validated and converted.
|
|
68
|
+
|
|
69
|
+
Returns
|
|
70
|
+
-------
|
|
71
|
+
Any
|
|
72
|
+
The validated and converted data.
|
|
73
|
+
|
|
74
|
+
Raises
|
|
75
|
+
------
|
|
76
|
+
ValueError
|
|
77
|
+
If the HU cube is not present in the input dictionary.
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
if isinstance(data, CT):
|
|
81
|
+
return data
|
|
82
|
+
|
|
83
|
+
if isinstance(data, dict): # check if data is dict
|
|
84
|
+
# Check if the cube is contained in the data (either as field name or its alias)
|
|
85
|
+
cube_hu = data.get("cube_hu", None)
|
|
86
|
+
if cube_hu is None:
|
|
87
|
+
cube_fieldname = cls.model_fields.get("cube_hu").validation_alias
|
|
88
|
+
cube_hu = data.pop(cube_fieldname, None)
|
|
89
|
+
|
|
90
|
+
if cube_hu is None:
|
|
91
|
+
raise ValueError("HU cube not present in dictionary.")
|
|
92
|
+
|
|
93
|
+
if isinstance(cube_hu, np.ndarray):
|
|
94
|
+
# Now we do check if it is matRad data
|
|
95
|
+
if data.get("cubeDim", None) is not None:
|
|
96
|
+
permute = True
|
|
97
|
+
else:
|
|
98
|
+
permute = False
|
|
99
|
+
|
|
100
|
+
# We have an array of multiple CT scenarios as numpy arrays
|
|
101
|
+
if cube_hu.dtype == object:
|
|
102
|
+
num_cubes = len(cube_hu)
|
|
103
|
+
|
|
104
|
+
# If there are multiple cubes, we need to store them in a list
|
|
105
|
+
# for later use
|
|
106
|
+
|
|
107
|
+
if permute:
|
|
108
|
+
ct_scenarios = [
|
|
109
|
+
sitk.GetImageFromArray(np.permute_dims(cube_hu[i], (2, 0, 1)), False)
|
|
110
|
+
for i in range(num_cubes)
|
|
111
|
+
]
|
|
112
|
+
else:
|
|
113
|
+
ct_scenarios = [
|
|
114
|
+
sitk.GetImageFromArray(cube_hu[i], False) for i in range(num_cubes)
|
|
115
|
+
]
|
|
116
|
+
|
|
117
|
+
cube_hu = sitk.JoinSeries(ct_scenarios)
|
|
118
|
+
else:
|
|
119
|
+
if permute:
|
|
120
|
+
cube_hu = sitk.GetImageFromArray(
|
|
121
|
+
np.permute_dims(cube_hu, (2, 0, 1)), False
|
|
122
|
+
)
|
|
123
|
+
else:
|
|
124
|
+
cube_hu = sitk.GetImageFromArray(cube_hu, False)
|
|
125
|
+
num_cubes = 1
|
|
126
|
+
|
|
127
|
+
# Reassign the updated cube_hu to the data dictionary
|
|
128
|
+
data["cube_hu"] = cube_hu
|
|
129
|
+
|
|
130
|
+
# From here on we want an sitk.Image
|
|
131
|
+
if not isinstance(data["cube_hu"], sitk.Image):
|
|
132
|
+
raise ValueError(f"Unsupported format of HU cube: {type(data['cube_hu'])}")
|
|
133
|
+
|
|
134
|
+
is4d = cube_hu.GetDimension() == 4
|
|
135
|
+
# Now we parse the rest of the data if additional data is available in the dictionary
|
|
136
|
+
|
|
137
|
+
# TODO: Either set up a Grid or check for x/y/z to do the data management
|
|
138
|
+
if "origin" in data:
|
|
139
|
+
data["cube_hu"].SetOrigin(data["origin"])
|
|
140
|
+
|
|
141
|
+
elif all(key in data for key in ("x", "y", "z")):
|
|
142
|
+
origin = np.array([data["x"][0], data["y"][0], data["z"][0]], dtype=float)
|
|
143
|
+
if is4d:
|
|
144
|
+
origin = np.append(origin, [0.0])
|
|
145
|
+
data["cube_hu"].SetOrigin(origin)
|
|
146
|
+
|
|
147
|
+
if "direction" in data:
|
|
148
|
+
data["cube_hu"].SetDirection(data["direction"])
|
|
149
|
+
|
|
150
|
+
if "resolution" in data and all(key in data["resolution"] for key in ("x", "y", "z")):
|
|
151
|
+
resolution = data["resolution"]
|
|
152
|
+
if cube_hu.GetDimension() == 4:
|
|
153
|
+
cube_hu.SetSpacing([resolution["x"], resolution["y"], resolution["z"], 1.0])
|
|
154
|
+
else:
|
|
155
|
+
cube_hu.SetSpacing([resolution["x"], resolution["y"], resolution["z"]])
|
|
156
|
+
|
|
157
|
+
return data
|
|
158
|
+
|
|
159
|
+
@computed_field
|
|
160
|
+
@property
|
|
161
|
+
def resolution(self) -> dict:
|
|
162
|
+
"""
|
|
163
|
+
Get the resolution of the CT image.
|
|
164
|
+
|
|
165
|
+
Returns
|
|
166
|
+
-------
|
|
167
|
+
dict
|
|
168
|
+
A dictionary containing the resolution in x, y, and z directions.
|
|
169
|
+
"""
|
|
170
|
+
|
|
171
|
+
resolution = self.cube_hu.GetSpacing()
|
|
172
|
+
res = {}
|
|
173
|
+
res["x"] = resolution[0]
|
|
174
|
+
res["y"] = resolution[1]
|
|
175
|
+
res["z"] = resolution[2]
|
|
176
|
+
return res
|
|
177
|
+
|
|
178
|
+
@computed_field
|
|
179
|
+
@property
|
|
180
|
+
def size(self) -> tuple:
|
|
181
|
+
"""
|
|
182
|
+
Get the size of the CT image.
|
|
183
|
+
|
|
184
|
+
Returns
|
|
185
|
+
-------
|
|
186
|
+
tuple
|
|
187
|
+
A tuple containing the size in x, y, and z directions.
|
|
188
|
+
"""
|
|
189
|
+
return self.cube_hu.GetSize()[0:3]
|
|
190
|
+
|
|
191
|
+
@computed_field
|
|
192
|
+
@property
|
|
193
|
+
def cube_dim(self) -> tuple:
|
|
194
|
+
"""
|
|
195
|
+
Get the size of the CT image.
|
|
196
|
+
|
|
197
|
+
Returns
|
|
198
|
+
-------
|
|
199
|
+
tuple
|
|
200
|
+
A tuple containing the size in x, y, and z directions.
|
|
201
|
+
"""
|
|
202
|
+
return self.size[0:3]
|
|
203
|
+
|
|
204
|
+
@computed_field
|
|
205
|
+
@property
|
|
206
|
+
def origin(self) -> tuple:
|
|
207
|
+
"""
|
|
208
|
+
Get the origin of the CT image.
|
|
209
|
+
|
|
210
|
+
Returns
|
|
211
|
+
-------
|
|
212
|
+
tuple
|
|
213
|
+
A tuple containing the origin coordinates.
|
|
214
|
+
"""
|
|
215
|
+
origin = self.cube_hu.GetOrigin()
|
|
216
|
+
return origin[0:3]
|
|
217
|
+
|
|
218
|
+
@computed_field
|
|
219
|
+
@property
|
|
220
|
+
def direction(self) -> tuple:
|
|
221
|
+
"""
|
|
222
|
+
Get the direction of the CT image.
|
|
223
|
+
|
|
224
|
+
Returns
|
|
225
|
+
-------
|
|
226
|
+
tuple
|
|
227
|
+
A tuple containing the direction cosines.
|
|
228
|
+
"""
|
|
229
|
+
direction = self.cube_hu.GetDirection()
|
|
230
|
+
return direction
|
|
231
|
+
|
|
232
|
+
@computed_field
|
|
233
|
+
@property
|
|
234
|
+
def num_of_ct_scen(self) -> int:
|
|
235
|
+
"""
|
|
236
|
+
Get the number of CT scenarios.
|
|
237
|
+
|
|
238
|
+
Returns
|
|
239
|
+
-------
|
|
240
|
+
int
|
|
241
|
+
The number of CT scenarios.
|
|
242
|
+
"""
|
|
243
|
+
if self.cube_hu.GetDimension() == 4:
|
|
244
|
+
return self.cube_hu.GetSize()[3]
|
|
245
|
+
else:
|
|
246
|
+
return 1
|
|
247
|
+
|
|
248
|
+
@computed_field
|
|
249
|
+
@property
|
|
250
|
+
def x(self) -> np.ndarray:
|
|
251
|
+
"""
|
|
252
|
+
Calculate and get the x-vector of the CT image.
|
|
253
|
+
|
|
254
|
+
Returns
|
|
255
|
+
-------
|
|
256
|
+
int
|
|
257
|
+
The corresponding x-vector of the CT image.
|
|
258
|
+
"""
|
|
259
|
+
|
|
260
|
+
x_spacing = self.cube_hu.GetSpacing()[0]
|
|
261
|
+
origin = self.cube_hu.GetOrigin()[0]
|
|
262
|
+
x = np.arange(origin, (self.size[0] * x_spacing + origin), x_spacing)
|
|
263
|
+
return x
|
|
264
|
+
|
|
265
|
+
@computed_field
|
|
266
|
+
@property
|
|
267
|
+
def y(self) -> np.ndarray:
|
|
268
|
+
"""
|
|
269
|
+
Calculate and get the y-vector of the CT image.
|
|
270
|
+
|
|
271
|
+
Returns
|
|
272
|
+
-------
|
|
273
|
+
int
|
|
274
|
+
The corresponding y-vector of the CT image.
|
|
275
|
+
"""
|
|
276
|
+
|
|
277
|
+
y_spacing = self.cube_hu.GetSpacing()[1]
|
|
278
|
+
origin = self.cube_hu.GetOrigin()[1]
|
|
279
|
+
y = np.arange(origin, (self.size[1] * y_spacing + origin), y_spacing)
|
|
280
|
+
return y
|
|
281
|
+
|
|
282
|
+
@computed_field
|
|
283
|
+
@property
|
|
284
|
+
def z(self) -> np.ndarray:
|
|
285
|
+
"""
|
|
286
|
+
Calculate and get the z-vector of the CT image.
|
|
287
|
+
|
|
288
|
+
Returns
|
|
289
|
+
-------
|
|
290
|
+
int
|
|
291
|
+
The corresponding z-vector of the CT image.
|
|
292
|
+
"""
|
|
293
|
+
|
|
294
|
+
z_spacing = self.cube_hu.GetSpacing()[2]
|
|
295
|
+
origin = self.cube_hu.GetOrigin()[2]
|
|
296
|
+
z = np.arange(origin, (self.size[2] * z_spacing + origin), z_spacing)
|
|
297
|
+
return z
|
|
298
|
+
|
|
299
|
+
@property
|
|
300
|
+
def grid(self) -> Grid:
|
|
301
|
+
"""
|
|
302
|
+
Get the grid of the CT image.
|
|
303
|
+
|
|
304
|
+
Returns
|
|
305
|
+
-------
|
|
306
|
+
Grid
|
|
307
|
+
The grid of the CT image.
|
|
308
|
+
"""
|
|
309
|
+
return Grid.from_sitk_image(self.cube_hu)
|
|
310
|
+
|
|
311
|
+
def world_to_cube_coords(self, world_coords: np.ndarray) -> np.ndarray:
|
|
312
|
+
"""
|
|
313
|
+
Convert world coordinates to cube coordinates.
|
|
314
|
+
|
|
315
|
+
Parameters
|
|
316
|
+
----------
|
|
317
|
+
world_coords : np.ndarray
|
|
318
|
+
The world coordinates to convert.
|
|
319
|
+
|
|
320
|
+
Returns
|
|
321
|
+
-------
|
|
322
|
+
np.ndarray
|
|
323
|
+
The converted cube coordinates.
|
|
324
|
+
|
|
325
|
+
Example Usage
|
|
326
|
+
-------------
|
|
327
|
+
>>> worlds_coords = beam.iso_center
|
|
328
|
+
"""
|
|
329
|
+
|
|
330
|
+
return np.array(self.cube_hu.TransformPhysicalPointToIndex(world_coords))
|
|
331
|
+
|
|
332
|
+
def compute_wet(self, hlut: np.ndarray) -> sitk.Image:
|
|
333
|
+
"""
|
|
334
|
+
Compute the water equivalent thickness (WET) from the CT image and
|
|
335
|
+
the
|
|
336
|
+
Hounsfield Look-Up Table (HLUT).
|
|
337
|
+
|
|
338
|
+
Parameters
|
|
339
|
+
----------
|
|
340
|
+
hlut : np.ndarray
|
|
341
|
+
The Hounsfield Look-Up Table (HLUT) to compute the WET.
|
|
342
|
+
|
|
343
|
+
Returns
|
|
344
|
+
-------
|
|
345
|
+
sitk.Image
|
|
346
|
+
The water equivalent thickness (WET) image.
|
|
347
|
+
"""
|
|
348
|
+
|
|
349
|
+
if hlut.shape[1] != 2 and hlut.shape[0] <= 1:
|
|
350
|
+
raise ValueError(
|
|
351
|
+
"HLUT must have 2 columns of values, with the first column being the HU values "
|
|
352
|
+
"and the second column being the WET values with each at least 2 values."
|
|
353
|
+
)
|
|
354
|
+
|
|
355
|
+
# Sort the HLUT by HU values
|
|
356
|
+
ix = np.argsort(hlut[:, 0])
|
|
357
|
+
xp = hlut[ix, 0]
|
|
358
|
+
fp = hlut[ix, 1]
|
|
359
|
+
|
|
360
|
+
# Get the np view of the image
|
|
361
|
+
ct_array = sitk.GetArrayViewFromImage(self.cube_hu)
|
|
362
|
+
|
|
363
|
+
# Convert the WET array back to a SimpleITK image
|
|
364
|
+
wet_image = sitk.GetImageFromArray(np.interp(ct_array, xp, fp))
|
|
365
|
+
wet_image.SetOrigin(self.cube_hu.GetOrigin())
|
|
366
|
+
wet_image.SetSpacing(self.cube_hu.GetSpacing())
|
|
367
|
+
wet_image.SetDirection(self.cube_hu.GetDirection())
|
|
368
|
+
|
|
369
|
+
return wet_image
|
|
370
|
+
|
|
371
|
+
def to_matrad(self, context: str = "mat-file") -> Any:
|
|
372
|
+
"""
|
|
373
|
+
Convert the CT object to a dictionary compatible with matRad format.
|
|
374
|
+
|
|
375
|
+
Returns
|
|
376
|
+
-------
|
|
377
|
+
Any
|
|
378
|
+
A dictionary containing the CT data in matRad format.
|
|
379
|
+
"""
|
|
380
|
+
ct_dict = super().to_matrad(context=context)
|
|
381
|
+
# as cubeHU for the matRad format
|
|
382
|
+
sitk_image = ct_dict["cubeHU"]
|
|
383
|
+
# Flip axes for matRad format from (z, y, x) to (x, y, z)
|
|
384
|
+
numpy_array = np.transpose(sitk.GetArrayFromImage(sitk_image), (2, 1, 0))
|
|
385
|
+
|
|
386
|
+
# We also need to put the image array into a ndarray of objects
|
|
387
|
+
ct_dict["cubeHU"] = np.ndarray(shape=(1,), dtype=object)
|
|
388
|
+
ct_dict["cubeHU"][0] = numpy_array
|
|
389
|
+
|
|
390
|
+
return ct_dict
|
|
391
|
+
|
|
392
|
+
def resample_to_grid(self, grid: Grid) -> Self:
|
|
393
|
+
"""
|
|
394
|
+
Resample the CT image to match the specified grid.
|
|
395
|
+
|
|
396
|
+
Parameters
|
|
397
|
+
----------
|
|
398
|
+
grid : Grid
|
|
399
|
+
The grid to resample the CT image to.
|
|
400
|
+
|
|
401
|
+
Returns
|
|
402
|
+
-------
|
|
403
|
+
Self
|
|
404
|
+
The resampled CT object.
|
|
405
|
+
"""
|
|
406
|
+
|
|
407
|
+
resampler = sitk.ResampleImageFilter()
|
|
408
|
+
resampler.SetInterpolator(sitk.sitkLinear)
|
|
409
|
+
resampler.SetOutputDirection(grid.direction.ravel())
|
|
410
|
+
resampler.SetOutputOrigin(grid.origin)
|
|
411
|
+
resampler.SetOutputSpacing(grid.resolution_vector)
|
|
412
|
+
resampler.SetSize(grid.dimensions)
|
|
413
|
+
|
|
414
|
+
if self.num_of_ct_scen > 1:
|
|
415
|
+
new_ct = []
|
|
416
|
+
for i in range(self.cube_hu.GetSize()[-1]):
|
|
417
|
+
new_ct.append(resampler.Execute(self.cube_hu[:, :, :, i]))
|
|
418
|
+
new_ct = sitk.JoinSeries(new_ct)
|
|
419
|
+
else:
|
|
420
|
+
new_ct = resampler.Execute(self.cube_hu)
|
|
421
|
+
|
|
422
|
+
return self.model_validate({"cube_hu": new_ct})
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
def resample_ct(ct: CT, interpolator=sitk.sitkBSpline, **kwargs) -> CT:
|
|
426
|
+
"""
|
|
427
|
+
Resample CT to a different grid.
|
|
428
|
+
|
|
429
|
+
Returns
|
|
430
|
+
-------
|
|
431
|
+
CT
|
|
432
|
+
A resampled CT object
|
|
433
|
+
"""
|
|
434
|
+
|
|
435
|
+
ct_cube = ct.cube_hu
|
|
436
|
+
|
|
437
|
+
resampled_ct_cube = resample_image(input_image=ct_cube, interpolator=interpolator, **kwargs)
|
|
438
|
+
|
|
439
|
+
return validate_ct(cube_hu=resampled_ct_cube)
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def ct_from_file(file_path: Union[str, os.PathLike]) -> CT:
|
|
443
|
+
"""
|
|
444
|
+
Create a CT object from a file.
|
|
445
|
+
|
|
446
|
+
Parameters
|
|
447
|
+
----------
|
|
448
|
+
file_path : Union[str, os.PathLike]
|
|
449
|
+
The path to the CT image file.
|
|
450
|
+
|
|
451
|
+
Returns
|
|
452
|
+
-------
|
|
453
|
+
CT
|
|
454
|
+
A CT object created from the file.
|
|
455
|
+
|
|
456
|
+
Raises
|
|
457
|
+
------
|
|
458
|
+
ValueError
|
|
459
|
+
If reading DICOM series from folders is attempted (not implemented).
|
|
460
|
+
"""
|
|
461
|
+
if os.path.isfile(file_path):
|
|
462
|
+
sitk_image = sitk.ReadImage(file_path)
|
|
463
|
+
# GetArrayFromImage accounts for the difference between numpy (z, x, y)
|
|
464
|
+
# and SITK (x, y, z) standards
|
|
465
|
+
|
|
466
|
+
else:
|
|
467
|
+
raise ValueError("Reading dicom series from folders not implemented")
|
|
468
|
+
|
|
469
|
+
return create_ct(cube_hu=sitk_image)
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
def create_ct(data: Union[dict[str, Any], CT, os.PathLike, str, None] = None, **kwargs) -> CT:
|
|
473
|
+
"""
|
|
474
|
+
Create a CT object from various input types.
|
|
475
|
+
|
|
476
|
+
Parameters
|
|
477
|
+
----------
|
|
478
|
+
data : Union[dict[str, Any], CT, os.PathLike, str, None], optional
|
|
479
|
+
The input data to create the CT object from. Can be a dictionary,
|
|
480
|
+
existing CT object, file path, or None.
|
|
481
|
+
**kwargs
|
|
482
|
+
Additional keyword arguments to create the CT object.
|
|
483
|
+
|
|
484
|
+
Returns
|
|
485
|
+
-------
|
|
486
|
+
CT
|
|
487
|
+
A CT object created from the input data or keyword arguments.
|
|
488
|
+
"""
|
|
489
|
+
if data: # If data is not None
|
|
490
|
+
# If data is already a CT object return it directly
|
|
491
|
+
if isinstance(data, CT):
|
|
492
|
+
return data
|
|
493
|
+
# If data is in a file get CT object from file
|
|
494
|
+
elif isinstance(data, str) or isinstance(data, os.PathLike):
|
|
495
|
+
return ct_from_file(data)
|
|
496
|
+
else:
|
|
497
|
+
# If data is in a dictionary create an SimpleITK image and then
|
|
498
|
+
# the CT object
|
|
499
|
+
return CT.model_validate(data)
|
|
500
|
+
else:
|
|
501
|
+
# If neither CT object nor dictionary try to
|
|
502
|
+
# get model from keyword arguments
|
|
503
|
+
return CT(**kwargs)
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
def validate_ct(ct: Union[dict[str, Any], CT, os.PathLike, None] = None, **kwargs) -> CT:
|
|
507
|
+
"""
|
|
508
|
+
Validate and create a CT object.
|
|
509
|
+
|
|
510
|
+
This function is a wrapper around create_ct and ensures the returned object
|
|
511
|
+
is a valid CT instance.
|
|
512
|
+
|
|
513
|
+
Parameters
|
|
514
|
+
----------
|
|
515
|
+
ct : Union[dict[str, Any], CT, os.PathLike, None], optional
|
|
516
|
+
The input data to validate and create the CT object from.
|
|
517
|
+
**kwargs
|
|
518
|
+
Additional keyword arguments to create the CT object.
|
|
519
|
+
|
|
520
|
+
Returns
|
|
521
|
+
-------
|
|
522
|
+
CT
|
|
523
|
+
A validated CT object.
|
|
524
|
+
"""
|
|
525
|
+
return create_ct(ct, **kwargs)
|
pyRadPlan/ct/_hlut.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def default_hlut(radiation_mode: str = None) -> np.ndarray:
|
|
5
|
+
"""Default HLUT by radiation mode.
|
|
6
|
+
|
|
7
|
+
Parameters
|
|
8
|
+
----------
|
|
9
|
+
radiation_mode : str
|
|
10
|
+
The radiation mode.
|
|
11
|
+
|
|
12
|
+
Returns
|
|
13
|
+
-------
|
|
14
|
+
np.ndarray
|
|
15
|
+
The default HLUT.
|
|
16
|
+
|
|
17
|
+
Notes
|
|
18
|
+
-----
|
|
19
|
+
Currently, we provide a single hlut independent of the radiation mode.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
hus = [
|
|
23
|
+
[-1024, -999, -90, -45, 0, 100, 350, 3000],
|
|
24
|
+
[0.001, 0.001, 0.95, 0.99, 1, 1.095, 1.199, 2.505],
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
return np.array(hus).transpose()
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|