|
| 1 | +# %% [markdown] |
| 2 | +"""# Multiple examples on how to use the visualization tools provided by pyRadPlan.""" |
| 3 | +# %% [markdown] |
| 4 | +# This example demonstrates the usage of `plot_slice()`, `plot_distributions()` and `plot_3d()`. |
| 5 | + |
| 6 | +# To display this script in a Jupyter Notebook, you need to install jupytext via pip and run the following command. |
| 7 | +# This will create a .ipynb file in the same directory: |
| 8 | + |
| 9 | +# ```bash |
| 10 | +# pip install jupytext |
| 11 | +# jupytext --to notebook path/to/this/file/utils_plotting.py |
| 12 | + |
| 13 | +# %% |
| 14 | +# Import necessary libraries |
| 15 | +import logging |
| 16 | + |
| 17 | +import matplotlib.pyplot as plt |
| 18 | + |
| 19 | +from pyRadPlan import ( |
| 20 | + IonPlan, |
| 21 | + generate_stf, |
| 22 | + calc_dose_influence, |
| 23 | + fluence_optimization, |
| 24 | + plot_slice, |
| 25 | + plot_multiple_slices, |
| 26 | + load_tg119, |
| 27 | +) |
| 28 | + |
| 29 | +logging.basicConfig(level=logging.INFO) |
| 30 | + |
| 31 | +# %% [markdown] |
| 32 | +# ### Just like in other examples, we need to generate some distributions/quantities to visualize. |
| 33 | +# %% |
| 34 | +# load TG119 |
| 35 | +ct, cst = load_tg119() |
| 36 | + |
| 37 | +# Create a plan object |
| 38 | +pln = IonPlan(radiation_mode="carbon", machine="Generic") |
| 39 | +pln.prop_opt = {"solver": "scipy"} |
| 40 | +# Lets calc the biological dose too |
| 41 | +pln.prop_dose_calc = {"calc_bio_dose": True} |
| 42 | + |
| 43 | +# Generate Steering Geometry ("stf") |
| 44 | +stf = generate_stf(ct, cst, pln) |
| 45 | + |
| 46 | +# Calculate Dose Influence Matrix ("dij") |
| 47 | +dij = calc_dose_influence(ct, cst, stf, pln) |
| 48 | + |
| 49 | +# Run fluence optimization and compute the result |
| 50 | +fluence = fluence_optimization(ct, cst, stf, dij, pln) |
| 51 | +result = dij.compute_result_ct_grid(fluence) |
| 52 | + |
| 53 | +# %% [markdown] |
| 54 | +# ### Visualizing a single slice with `plot_slice()` |
| 55 | +# %% |
| 56 | + |
| 57 | +# Visualize only ct and choosen quantity |
| 58 | +plot_slice(ct=ct, overlay=result["physical_dose"]) |
| 59 | + |
| 60 | +# Visualize ct, cst and choosen quantity |
| 61 | +plot_slice(ct=ct, cst=cst, overlay=result["physical_dose"]) |
| 62 | + |
| 63 | +# %% [markdown] |
| 64 | +# ## Visualze more abstract settings using `plot_slice()` <br> |
| 65 | + |
| 66 | +# `plot_slice()` has multiple tweakable parameters: |
| 67 | +# - **ct**: The CT data to visualize |
| 68 | +# - **cst**: The structure set to visualize (optional) |
| 69 | +# - **overlay**: The quantity to visualize |
| 70 | +# - **view_slice**: Which slice to visualize (default: middle slice) |
| 71 | +# - **plane**: Which plane to visualize (default: "axial") |
| 72 | +# - **overlay_alpha**: Transparency of the overlay (default: 0.5) |
| 73 | +# - **overlay_rel_threshold**: Relative threshold for the overlay (default: 0.01) |
| 74 | +# - **overlay_unit**: The unit of the overlay quantity (default: "Gy") |
| 75 | +# - **save_filename**: If provided, saves the plot to a file |
| 76 | +# - **show_plot**: Whether to show the plot (default: True) |
| 77 | +# - **use_global_max**: If True, uses the global maximum of the overlay for scaling (default: False) |
| 78 | +# - **ax**: If provided, plots on the given axes (useful for subplots) - use of 'plot_distributions()' is recommended |
| 79 | +# %% |
| 80 | +# Feel free to change the parameters to see how they affect the plot. |
| 81 | +plot_slice( |
| 82 | + ct=ct, |
| 83 | + cst=cst, |
| 84 | + overlay=result["physical_dose"], |
| 85 | + view_slice=64, # Visualize slice 10 |
| 86 | + plane="coronal", # axial, coronal or sagittal |
| 87 | + overlay_alpha=0.5, # Transparency of the overlay |
| 88 | + overlay_unit="Gy", # Gy, dimensionless, etc. |
| 89 | + overlay_rel_threshold=0.01, # Relative threshold for the overlay |
| 90 | + contour_line_width=1.0, # Width of the contour lines |
| 91 | + save_filename=None, # alt: path/to/save/plot.png |
| 92 | + show_plot=True, # Show the plot |
| 93 | + use_global_max=False, # Do not use global max for scaling |
| 94 | +) |
| 95 | + |
| 96 | +# %% [markdown] |
| 97 | +# ### Visualizing multiple overlays/quantites with `plot_distributions()` |
| 98 | +# This function might be useful in cases you want to compare multiple overlays/quantities/beams side by side. |
| 99 | + |
| 100 | +# `plot_distributions()` has similar parameters to `plot_slice()`: |
| 101 | +# - **ct**: The CT data to visualize |
| 102 | +# - **cst**: The structure set to visualize (optional) |
| 103 | +# - **overlays**: List of overlay images to visualize |
| 104 | +# - **view_slice**: Which slices to visualize (default: middle slice) |
| 105 | +# - **plane**: Which plane to visualize (default: "axial") |
| 106 | +# - **overlay_alpha**: Transparency of the overlay (default: 0.5) |
| 107 | +# - **overlay_unit**: List of units for each overlay (default: "Gy") |
| 108 | +# - **overlay_rel_threshold**: Relative threshold for the overlay (default: 0.01) |
| 109 | +# - **contour_line_width**: Line width for the contour lines (default: 1.0) |
| 110 | +# - **save_filename**: If provided, saves the plot to a file |
| 111 | +# - **show_plot**: Whether to show the plot (default: True) |
| 112 | +# - **use_global_max**: If True, uses the global maximum of the overlay for scaling |
| 113 | +# - **overlay_titles**: Custom titles for each overlay type (optional) |
| 114 | +# %% |
| 115 | +# Feel free to change the parameters to see how they affect the plot. |
| 116 | +plot_multiple_slices( |
| 117 | + ct=ct, |
| 118 | + cst=cst, |
| 119 | + overlays=[result["physical_dose"], result["effect"]], |
| 120 | + view_slice=[64, 65], # Visualize slices 64 and 65 |
| 121 | + plane="axial", # axial, coronal or sagittal |
| 122 | + overlay_alpha=0.5, # Transparency of the overlay |
| 123 | + overlay_unit=["Gy", "dimensionless"], # Units for each overlay |
| 124 | + overlay_rel_threshold=0.01, # Relative threshold for the overlay |
| 125 | + contour_line_width=1.0, # Width of the contour lines |
| 126 | + save_filename=None, # alt: path/to/save/plot.png |
| 127 | + show_plot=True, # Show the plot |
| 128 | + use_global_max=False, # Do not use global max for scaling |
| 129 | + overlay_titles=["Physical Dose", "Biological Effect"], # Titles for each overlay |
| 130 | +) |
| 131 | + |
| 132 | +# %% [markdown] |
| 133 | +# ### Being more flexible with only `plot_slice()` is also possible: |
| 134 | +# %% |
| 135 | +# Create a figure with subplots for side-by-side comparison |
| 136 | +# 2 rows (physical dose, biological effect) x 2 cols (slice1, slice2) |
| 137 | +slices = [64, 65] # Slices to visualize |
| 138 | +fig, axes = plt.subplots(2, 2, figsize=(12, 10)) |
| 139 | + |
| 140 | +# Plot physical dose for both slices |
| 141 | +for i, slice_idx in enumerate(slices): |
| 142 | + plot_slice( |
| 143 | + ct=ct, |
| 144 | + cst=cst, |
| 145 | + overlay=result["physical_dose"], |
| 146 | + view_slice=slice_idx, # Single slice |
| 147 | + plane="axial", |
| 148 | + overlay_unit="Gy", |
| 149 | + show_plot=False, |
| 150 | + ax=axes[0, i], # Top row |
| 151 | + ) |
| 152 | + axes[0, i].set_title(f"Physical Dose - Slice {slice_idx}") |
| 153 | + |
| 154 | +# Plot biological effect for both slices |
| 155 | +for i, slice_idx in enumerate(slices): |
| 156 | + plot_slice( |
| 157 | + ct=ct, |
| 158 | + cst=cst, |
| 159 | + overlay=result["effect"], |
| 160 | + view_slice=slice_idx, # Single slice |
| 161 | + plane="axial", |
| 162 | + overlay_unit="dimensionless", |
| 163 | + show_plot=False, |
| 164 | + ax=axes[1, i], # Bottom row |
| 165 | + ) |
| 166 | + axes[1, i].set_title(f"Biological Effect - Slice {slice_idx}") |
| 167 | + |
| 168 | +plt.tight_layout() |
| 169 | +plt.show() |
0 commit comments