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
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
from importlib import resources
|
|
2
|
+
import logging
|
|
3
|
+
import numpy as np
|
|
4
|
+
import SimpleITK as sitk
|
|
5
|
+
import pymatreader
|
|
6
|
+
import matplotlib.pyplot as plt
|
|
7
|
+
|
|
8
|
+
from pyRadPlan import (
|
|
9
|
+
IonPlan,
|
|
10
|
+
validate_ct,
|
|
11
|
+
validate_cst,
|
|
12
|
+
generate_stf,
|
|
13
|
+
calc_dose_influence,
|
|
14
|
+
fluence_optimization,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
logging.basicConfig(level=logging.INFO)
|
|
18
|
+
|
|
19
|
+
# Read patient from provided TG119.mat file and validate data
|
|
20
|
+
path = resources.files("pyRadPlan.data.phantoms").joinpath("TG119.mat")
|
|
21
|
+
tmp = pymatreader.read_mat(path)
|
|
22
|
+
ct = validate_ct(tmp["ct"])
|
|
23
|
+
cst = validate_cst(tmp["cst"], ct=ct)
|
|
24
|
+
|
|
25
|
+
# Create a plan object
|
|
26
|
+
pln = IonPlan(radiation_mode="carbon", machine="Generic")
|
|
27
|
+
pln.prop_stf = {"bixel_width": 4}
|
|
28
|
+
pln.prop_dose_calc = {"calc_bio_dose": True}
|
|
29
|
+
|
|
30
|
+
# Generate Steering Geometry ("stf")
|
|
31
|
+
stf = generate_stf(ct, cst, pln)
|
|
32
|
+
|
|
33
|
+
# Calculate Dose Influence Matrix ("dij")
|
|
34
|
+
dij = calc_dose_influence(ct, cst, stf, pln)
|
|
35
|
+
|
|
36
|
+
# Optimize
|
|
37
|
+
fluence = fluence_optimization(ct, cst, stf, dij, pln)
|
|
38
|
+
|
|
39
|
+
# Result
|
|
40
|
+
result = dij.compute_result_ct_grid(fluence)
|
|
41
|
+
|
|
42
|
+
# Choose a slice to visualize
|
|
43
|
+
view_slice = int(np.round(ct.size[2] / 2))
|
|
44
|
+
|
|
45
|
+
# Visualize
|
|
46
|
+
quantities = ["physical_dose", "effect"]
|
|
47
|
+
for quantity in quantities:
|
|
48
|
+
cube_hu = sitk.GetArrayViewFromImage(ct.cube_hu)
|
|
49
|
+
plt.imshow(cube_hu[view_slice, :, :], cmap="gray")
|
|
50
|
+
|
|
51
|
+
plt.tick_params(
|
|
52
|
+
axis="both",
|
|
53
|
+
which="both",
|
|
54
|
+
bottom=False,
|
|
55
|
+
top=False,
|
|
56
|
+
labelbottom=False,
|
|
57
|
+
left=False,
|
|
58
|
+
right=False,
|
|
59
|
+
labelleft=False,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
# Now let's visualize the VOIs from the StructureSet.
|
|
63
|
+
for v, voi in enumerate(cst.vois):
|
|
64
|
+
mask = sitk.GetArrayViewFromImage(voi.mask)
|
|
65
|
+
color = plt.cm.cool(v / len(cst.vois)) # Select color based on colormap 'cool'
|
|
66
|
+
plt.contour(
|
|
67
|
+
mask[view_slice, :, :],
|
|
68
|
+
levels=[0.5],
|
|
69
|
+
colors=[color],
|
|
70
|
+
linewidths=1,
|
|
71
|
+
)
|
|
72
|
+
dose_array = sitk.GetArrayViewFromImage(result[quantity])
|
|
73
|
+
plt.imshow(
|
|
74
|
+
dose_array[view_slice, :, :],
|
|
75
|
+
cmap="jet",
|
|
76
|
+
interpolation="nearest",
|
|
77
|
+
alpha=0.5 * (dose_array[view_slice, :, :] > 0.02),
|
|
78
|
+
)
|
|
79
|
+
if quantity == "physical_dose":
|
|
80
|
+
plt.colorbar(label="dose [Gy]")
|
|
81
|
+
plt.title(f"Carbon Dose Distribution (Slice z={view_slice})")
|
|
82
|
+
else:
|
|
83
|
+
plt.colorbar(label="effect [1]")
|
|
84
|
+
plt.title(f"Effect [-ln(S)] Distribution (Slice z={view_slice})")
|
|
85
|
+
plt.show()
|
|
86
|
+
plt.savefig(f"carbon_{quantity}.png")
|
|
87
|
+
plt.clf()
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
from importlib import resources
|
|
2
|
+
import logging
|
|
3
|
+
import numpy as np
|
|
4
|
+
import SimpleITK as sitk
|
|
5
|
+
import pymatreader
|
|
6
|
+
import matplotlib.pyplot as plt
|
|
7
|
+
|
|
8
|
+
from pyRadPlan import (
|
|
9
|
+
IonPlan,
|
|
10
|
+
validate_ct,
|
|
11
|
+
validate_cst,
|
|
12
|
+
generate_stf,
|
|
13
|
+
calc_dose_influence,
|
|
14
|
+
fluence_optimization,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
logging.basicConfig(level=logging.INFO)
|
|
18
|
+
|
|
19
|
+
# Read patient from provided TG119.mat file and validate data
|
|
20
|
+
path = resources.files("pyRadPlan.data.phantoms").joinpath("TG119.mat")
|
|
21
|
+
tmp = pymatreader.read_mat(path)
|
|
22
|
+
ct = validate_ct(tmp["ct"])
|
|
23
|
+
cst = validate_cst(tmp["cst"], ct=ct)
|
|
24
|
+
|
|
25
|
+
# Create a plan object
|
|
26
|
+
pln = IonPlan(radiation_mode="helium", machine="Generic")
|
|
27
|
+
pln.prop_stf = {"bixel_width": 4}
|
|
28
|
+
|
|
29
|
+
# Generate Steering Geometry ("stf")
|
|
30
|
+
stf = generate_stf(ct, cst, pln)
|
|
31
|
+
|
|
32
|
+
# Calculate Dose Influence Matrix ("dij")
|
|
33
|
+
dij = calc_dose_influence(ct, cst, stf, pln)
|
|
34
|
+
|
|
35
|
+
# Optimize
|
|
36
|
+
fluence = fluence_optimization(ct, cst, stf, dij, pln)
|
|
37
|
+
|
|
38
|
+
# Result
|
|
39
|
+
result = dij.compute_result_ct_grid(fluence)
|
|
40
|
+
|
|
41
|
+
# Choose a slice to visualize
|
|
42
|
+
view_slice = int(np.round(ct.size[2] / 2))
|
|
43
|
+
|
|
44
|
+
# Visualize
|
|
45
|
+
cube_hu = sitk.GetArrayViewFromImage(ct.cube_hu)
|
|
46
|
+
plt.imshow(cube_hu[view_slice, :, :], cmap="gray")
|
|
47
|
+
|
|
48
|
+
plt.tick_params(
|
|
49
|
+
axis="both",
|
|
50
|
+
which="both",
|
|
51
|
+
bottom=False,
|
|
52
|
+
top=False,
|
|
53
|
+
labelbottom=False,
|
|
54
|
+
left=False,
|
|
55
|
+
right=False,
|
|
56
|
+
labelleft=False,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
# Now let's visualize the VOIs from the StructureSet.
|
|
60
|
+
for v, voi in enumerate(cst.vois):
|
|
61
|
+
mask = sitk.GetArrayViewFromImage(voi.mask)
|
|
62
|
+
color = plt.cm.cool(v / len(cst.vois)) # Select color based on colormap 'cool'
|
|
63
|
+
plt.contour(
|
|
64
|
+
mask[view_slice, :, :],
|
|
65
|
+
levels=[0.5],
|
|
66
|
+
colors=[color],
|
|
67
|
+
linewidths=1,
|
|
68
|
+
)
|
|
69
|
+
dose_array = sitk.GetArrayViewFromImage(result["physical_dose"])
|
|
70
|
+
plt.imshow(
|
|
71
|
+
dose_array[view_slice, :, :],
|
|
72
|
+
cmap="jet",
|
|
73
|
+
interpolation="nearest",
|
|
74
|
+
alpha=0.5 * (dose_array[view_slice, :, :] > 0.02),
|
|
75
|
+
)
|
|
76
|
+
plt.colorbar(label="dose [Gy]")
|
|
77
|
+
plt.title(f"Helium Dose (Slice z={view_slice})")
|
|
78
|
+
plt.show()
|
|
79
|
+
plt.savefig(f"{pln.radiation_mode}_physical_dose.png")
|
|
80
|
+
plt.clf()
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Example of importing a patient from a MatRad file and visualizing the CT,
|
|
3
|
+
VOIs with matplotlib.
|
|
4
|
+
|
|
5
|
+
Also produces a simple beam configuration and visualizes the resulting radiological depth cubes by
|
|
6
|
+
calling the RayTracer.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
# Standard Library Imports
|
|
10
|
+
from importlib import resources
|
|
11
|
+
import logging
|
|
12
|
+
|
|
13
|
+
# Third Party Imports from pyRadPlan requirements
|
|
14
|
+
import pymatreader
|
|
15
|
+
import numpy as np
|
|
16
|
+
import SimpleITK as sitk
|
|
17
|
+
import matplotlib.pyplot as plt
|
|
18
|
+
|
|
19
|
+
# pyRadPlan Module Imports
|
|
20
|
+
from pyRadPlan.ct import validate_ct, default_hlut
|
|
21
|
+
from pyRadPlan.cst import validate_cst
|
|
22
|
+
from pyRadPlan.plan import IonPlan
|
|
23
|
+
from pyRadPlan.stf import StfGeneratorIMPT
|
|
24
|
+
from pyRadPlan.raytracer import RayTracerSiddon
|
|
25
|
+
|
|
26
|
+
# Configure the Logger to show you debug information
|
|
27
|
+
logging.basicConfig(level=logging.INFO)
|
|
28
|
+
logging.getLogger("pyRadPlan").setLevel(logging.DEBUG)
|
|
29
|
+
|
|
30
|
+
# We use importlib resources to load the TG119.mat file from the pyRadPlan.data.phantoms package
|
|
31
|
+
files = resources.files("pyRadPlan.data.phantoms")
|
|
32
|
+
path = files.joinpath("TG119.mat")
|
|
33
|
+
tmp = pymatreader.read_mat(path)
|
|
34
|
+
|
|
35
|
+
# The validation functions for ct and cst allow us to create valid CTs and StructureSets
|
|
36
|
+
ct = validate_ct(tmp["ct"])
|
|
37
|
+
cst = validate_cst(tmp["cst"], ct=ct)
|
|
38
|
+
|
|
39
|
+
# Choose a slice to visualize
|
|
40
|
+
view_slice = int(np.round(ct.size[2] / 2))
|
|
41
|
+
|
|
42
|
+
# We store images as sitk.Image objects, but we can easily convert them to numpy arrays for
|
|
43
|
+
# matplotlib. Taking a "view" here means we do not perform a deep copy of the image data.
|
|
44
|
+
cube_hu = sitk.GetArrayViewFromImage(ct.cube_hu)
|
|
45
|
+
plt.imshow(cube_hu[view_slice, :, :], cmap="gray")
|
|
46
|
+
|
|
47
|
+
# Now let's visualize the VOIs from the StructureSet.
|
|
48
|
+
for v, voi in enumerate(cst.vois):
|
|
49
|
+
mask = sitk.GetArrayViewFromImage(voi.mask)
|
|
50
|
+
plt.imshow(
|
|
51
|
+
(v + 1) * mask[view_slice, :, :],
|
|
52
|
+
cmap="jet",
|
|
53
|
+
alpha=0.5 * (mask[view_slice, :, :] > 0),
|
|
54
|
+
vmin=1,
|
|
55
|
+
vmax=len(cst.vois),
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
# Since we want to do planning, we create a plan object.
|
|
59
|
+
# We choose protons as the radiation mode and a Generic machine included with pyRadPlan
|
|
60
|
+
pln = IonPlan(radiation_mode="protons", machine="Generic")
|
|
61
|
+
|
|
62
|
+
# We use the StfGeneratorIMPT to create a simple beam configuration
|
|
63
|
+
# with a single beam at 0 degrees
|
|
64
|
+
stfgen = StfGeneratorIMPT(pln)
|
|
65
|
+
stfgen.bixel_width = 5.0
|
|
66
|
+
stfgen.gantry_angles = [90.0]
|
|
67
|
+
|
|
68
|
+
# We generate the beam geometry on the CT and CST
|
|
69
|
+
stf = stfgen.generate(ct, cst)
|
|
70
|
+
|
|
71
|
+
# Now let's look at the water equivalent path length for our beam.
|
|
72
|
+
# For that, we use a default HU->rSP table to convert our CT to water-equivalent thickness and then
|
|
73
|
+
# call a voxel-wise RayTracing algorithm (proposed by Siddon) to calculate the radiological depth.
|
|
74
|
+
rt = RayTracerSiddon([ct.compute_wet(default_hlut())])
|
|
75
|
+
rad_depth_cubes = rt.trace_cubes(stf[0])
|
|
76
|
+
|
|
77
|
+
# We could write the image if we want, but we don't do this by default.
|
|
78
|
+
# sitk.WriteImage(ct.compute_wet(default_hlut()), "wet.nrrd")
|
|
79
|
+
# sitk.WriteImage(rad_depth_cubes[0], "rad_depth_cubes.nrrd")
|
|
80
|
+
|
|
81
|
+
# Let's see about our range of radiological depths
|
|
82
|
+
rd_np = sitk.GetArrayFromImage(rad_depth_cubes[0])
|
|
83
|
+
rd_np.ravel()[np.isnan(rd_np.ravel())] = 0
|
|
84
|
+
min_rad_depth = np.min(rd_np)
|
|
85
|
+
max_rad_depth = np.nanmax(rd_np)
|
|
86
|
+
print(f"Minimum rad depth: {min_rad_depth}" f"Maximum rad depth: {max_rad_depth}")
|
|
87
|
+
|
|
88
|
+
# Now we do visualization of the corresponding slice of our WEPL cube
|
|
89
|
+
rad_depth_slice = rd_np[view_slice, :, :]
|
|
90
|
+
|
|
91
|
+
plt.imshow(
|
|
92
|
+
rad_depth_slice,
|
|
93
|
+
cmap="jet",
|
|
94
|
+
alpha=0.5 * (rad_depth_slice > 0.0),
|
|
95
|
+
vmin=min_rad_depth,
|
|
96
|
+
vmax=max_rad_depth,
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
# And finally, output!
|
|
100
|
+
plt.draw()
|
|
101
|
+
plt.pause(0.001)
|
|
102
|
+
plt.show()
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Running the photon pencil-beam dose calculation engine."""
|
|
2
|
+
|
|
3
|
+
# Standard Library Imports
|
|
4
|
+
from importlib import resources
|
|
5
|
+
import logging
|
|
6
|
+
|
|
7
|
+
# Third Party Imports from pyRadPlan requirements
|
|
8
|
+
import pymatreader
|
|
9
|
+
import numpy as np
|
|
10
|
+
import SimpleITK as sitk
|
|
11
|
+
import matplotlib.pyplot as plt
|
|
12
|
+
|
|
13
|
+
# pyRadPlan Module Imports
|
|
14
|
+
from pyRadPlan.ct import validate_ct
|
|
15
|
+
from pyRadPlan.cst import validate_cst
|
|
16
|
+
from pyRadPlan.plan import PhotonPlan
|
|
17
|
+
from pyRadPlan.stf import StfGeneratorPhotonIMRT
|
|
18
|
+
from pyRadPlan.dose import calc_dose_influence
|
|
19
|
+
|
|
20
|
+
# Configure the Logger to show you debug information
|
|
21
|
+
logging.basicConfig(level=logging.INFO)
|
|
22
|
+
logging.getLogger("pyRadPlan").setLevel(logging.DEBUG)
|
|
23
|
+
|
|
24
|
+
# We use importlib resources to load the TG119.mat file from the pyRadPlan.data.phantoms package
|
|
25
|
+
files = resources.files("pyRadPlan.data.phantoms")
|
|
26
|
+
path = files.joinpath("TG119.mat")
|
|
27
|
+
tmp = pymatreader.read_mat(path)
|
|
28
|
+
|
|
29
|
+
# The validation functions for ct and cst allow us to create valid CTs and StructureSets
|
|
30
|
+
ct = validate_ct(tmp["ct"])
|
|
31
|
+
cst = validate_cst(tmp["cst"], ct=ct)
|
|
32
|
+
|
|
33
|
+
# Choose a slice to visualize
|
|
34
|
+
view_slice = int(np.round(ct.size[2] / 2))
|
|
35
|
+
|
|
36
|
+
# We store images as sitk.Image objects, but we can easily convert them to numpy arrays for
|
|
37
|
+
# matplotlib. Taking a "view" here means we do not perform a deep copy of the image data.
|
|
38
|
+
cube_hu = sitk.GetArrayViewFromImage(ct.cube_hu)
|
|
39
|
+
plt.imshow(cube_hu[view_slice, :, :], cmap="gray")
|
|
40
|
+
|
|
41
|
+
# Now let's visualize the VOIs from the StructureSet.
|
|
42
|
+
for v, voi in enumerate(cst.vois):
|
|
43
|
+
mask = sitk.GetArrayViewFromImage(voi.mask)
|
|
44
|
+
plt.imshow(
|
|
45
|
+
(v + 1) * mask[view_slice, :, :],
|
|
46
|
+
cmap="jet",
|
|
47
|
+
alpha=0.5 * (mask[view_slice, :, :] > 0),
|
|
48
|
+
vmin=1,
|
|
49
|
+
vmax=len(cst.vois),
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
# Since we want to do planning, we create a plan object.
|
|
53
|
+
# We choose protons as the radiation mode and a Generic machine included with pyRadPlan
|
|
54
|
+
pln = PhotonPlan(machine="Generic")
|
|
55
|
+
|
|
56
|
+
# We use the StfGeneratorIMPT to create a simple beam configuration
|
|
57
|
+
# with a single beam at 0 degrees
|
|
58
|
+
stfgen = StfGeneratorPhotonIMRT(pln)
|
|
59
|
+
stfgen.bixel_width = 5.0
|
|
60
|
+
stfgen.gantry_angles = [0.0]
|
|
61
|
+
|
|
62
|
+
# We generate the beam geometry on the CT and CST
|
|
63
|
+
stf = stfgen.generate(ct, cst)
|
|
64
|
+
|
|
65
|
+
# Let's calculate the dose influence matrix
|
|
66
|
+
dij = calc_dose_influence(ct, cst, stf, pln)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
from importlib import resources
|
|
2
|
+
import logging
|
|
3
|
+
import numpy as np
|
|
4
|
+
import SimpleITK as sitk
|
|
5
|
+
import pymatreader
|
|
6
|
+
import matplotlib.pyplot as plt
|
|
7
|
+
|
|
8
|
+
from pyRadPlan import (
|
|
9
|
+
IonPlan,
|
|
10
|
+
validate_ct,
|
|
11
|
+
validate_cst,
|
|
12
|
+
generate_stf,
|
|
13
|
+
calc_dose_influence,
|
|
14
|
+
fluence_optimization,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
logging.basicConfig(level=logging.INFO)
|
|
18
|
+
|
|
19
|
+
# Read patient from provided TG119.mat file and validate data
|
|
20
|
+
path = resources.files("pyRadPlan.data.phantoms").joinpath("TG119.mat")
|
|
21
|
+
tmp = pymatreader.read_mat(path)
|
|
22
|
+
ct = validate_ct(tmp["ct"])
|
|
23
|
+
cst = validate_cst(tmp["cst"], ct=ct)
|
|
24
|
+
|
|
25
|
+
# Create a plan object
|
|
26
|
+
pln = IonPlan(radiation_mode="protons", machine="Generic")
|
|
27
|
+
|
|
28
|
+
# Generate Steering Geometry ("stf")
|
|
29
|
+
stf = generate_stf(ct, cst, pln)
|
|
30
|
+
|
|
31
|
+
# Calculate Dose Influence Matrix ("dij")
|
|
32
|
+
dij = calc_dose_influence(ct, cst, stf, pln)
|
|
33
|
+
|
|
34
|
+
# Optimize
|
|
35
|
+
fluence = fluence_optimization(ct, cst, stf, dij, pln)
|
|
36
|
+
|
|
37
|
+
# Result
|
|
38
|
+
result = dij.compute_result_ct_grid(fluence)
|
|
39
|
+
|
|
40
|
+
# Choose a slice to visualize
|
|
41
|
+
view_slice = int(np.round(ct.size[2] / 2))
|
|
42
|
+
|
|
43
|
+
# Visualize
|
|
44
|
+
cube_hu = sitk.GetArrayViewFromImage(ct.cube_hu)
|
|
45
|
+
plt.imshow(cube_hu[view_slice, :, :], cmap="gray")
|
|
46
|
+
|
|
47
|
+
plt.tick_params(
|
|
48
|
+
axis="both",
|
|
49
|
+
which="both",
|
|
50
|
+
bottom=False,
|
|
51
|
+
top=False,
|
|
52
|
+
labelbottom=False,
|
|
53
|
+
left=False,
|
|
54
|
+
right=False,
|
|
55
|
+
labelleft=False,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
# Now let's visualize the VOIs from the StructureSet.
|
|
59
|
+
for v, voi in enumerate(cst.vois):
|
|
60
|
+
mask = sitk.GetArrayViewFromImage(voi.mask)
|
|
61
|
+
cmap = plt.colormaps["cool"]
|
|
62
|
+
color = cmap(v / len(cst.vois)) # Select color based on colormap 'cool'
|
|
63
|
+
plt.contour(
|
|
64
|
+
mask[view_slice, :, :],
|
|
65
|
+
levels=[0.5],
|
|
66
|
+
colors=[color],
|
|
67
|
+
linewidths=1,
|
|
68
|
+
)
|
|
69
|
+
dose_array = sitk.GetArrayViewFromImage(result["physical_dose"])
|
|
70
|
+
plt.imshow(
|
|
71
|
+
dose_array[view_slice, :, :],
|
|
72
|
+
cmap="jet",
|
|
73
|
+
interpolation="nearest",
|
|
74
|
+
alpha=0.5 * (dose_array[view_slice, :, :] > 0.02),
|
|
75
|
+
)
|
|
76
|
+
plt.colorbar(label="dose [Gy]")
|
|
77
|
+
plt.title(f"Proton Dose (Slice z={view_slice})")
|
|
78
|
+
plt.show()
|
|
79
|
+
plt.savefig(f"{pln.radiation_mode}_physical_dose.png")
|
|
80
|
+
plt.clf()
|
pyRadPlan/GUI.py
ADDED
pyRadPlan/__init__.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from importlib.metadata import version, PackageNotFoundError
|
|
2
|
+
import logging
|
|
3
|
+
|
|
4
|
+
from .plan._plans import Plan, validate_pln, IonPlan, PhotonPlan
|
|
5
|
+
from .ct._ct import CT, validate_ct
|
|
6
|
+
from .cst._cst import StructureSet, validate_cst
|
|
7
|
+
from .stf._generate_stf import generate_stf
|
|
8
|
+
from .dose._calc_dose import calc_dose_influence, calc_dose_forward
|
|
9
|
+
from .optimization._fluence_optimization import fluence_optimization
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
__version__ = version(__name__)
|
|
13
|
+
except PackageNotFoundError:
|
|
14
|
+
# package is not installed
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
# Logging is not exposed by default and needs to be configured by the user.
|
|
18
|
+
logging.getLogger(__name__).addHandler(logging.NullHandler())
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"__version__",
|
|
22
|
+
"Plan",
|
|
23
|
+
"IonPlan",
|
|
24
|
+
"PhotonPlan",
|
|
25
|
+
"validate_pln",
|
|
26
|
+
"CT",
|
|
27
|
+
"validate_ct",
|
|
28
|
+
"StructureSet",
|
|
29
|
+
"validate_cst",
|
|
30
|
+
"generate_stf",
|
|
31
|
+
"calc_dose_influence",
|
|
32
|
+
"calc_dose_forward",
|
|
33
|
+
"fluence_optimization",
|
|
34
|
+
]
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""Core module with fundamental classes and functions for pyRadPlan."""
|
|
2
|
+
|
|
3
|
+
from ._exceptions import PyRadPlanException
|
|
4
|
+
from .datamodel import PyRadPlanBaseModel
|
|
5
|
+
from .resample import resample_image, resample_numpy_array
|
|
6
|
+
from ._grids import Grid
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"PyRadPlanException",
|
|
10
|
+
"np2sitk",
|
|
11
|
+
"resample_image",
|
|
12
|
+
"resample_numpy_array",
|
|
13
|
+
"PyRadPlanBaseModel",
|
|
14
|
+
"Grid",
|
|
15
|
+
]
|