Skip to content

Commit db37d23

Browse files
committed
Update notebooks
1 parent dc9e087 commit db37d23

10 files changed

Lines changed: 629 additions & 490 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ If you are using PortPy for your radiotherapy research, you can apply RMR sparsi
8484
from compress_rtp.utils.get_sparse_only import get_sparse_only
8585

8686
# Apply RMR sparsification to the matrix A
87-
S = get_sparse_only(matrix=A, threshold_perc=10, compression='rmr')
87+
S = get_sparse_only(A=A, threshold_perc=10, compression='rmr')
8888

8989
# Replace the original matrix A with the sparsified matrix S
9090
inf_matrix.A = S

compress_rtp/utils/get_low_dim_basis.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

2-
from typing import TYPE_CHECKING
3-
if TYPE_CHECKING:
4-
from portpy.photon.influence_matrix import InfluenceMatrix
2+
3+
from portpy.photon.influence_matrix import InfluenceMatrix
54
import numpy as np
65
import scipy
76
try:

compress_rtp/utils/get_sparse_only.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
import scipy
44

55

6-
def get_sparse_only(matrix: np.ndarray, threshold_perc: float = 1, compression: str = 'naive'):
6+
def get_sparse_only(A: np.ndarray, threshold_perc: float = 1, compression: str = 'naive'):
77
"""
88
Get sparse matrix using threshold and different methods
9-
:param matrix: matrix to be sparsified
9+
:param A: matrix to be sparsified
1010
:param threshold_perc: threshold for matrix sparsification
1111
:param compression: Method of Sparsification
1212
1313
:return: Sparse influence matrix
1414
"""
15-
threshold = np.max(matrix) * threshold_perc*0.01
15+
threshold = np.max(A) * threshold_perc * 0.01
1616
if compression == 'rmr':
17-
copy_matrix = matrix.copy()
17+
copy_matrix = A.copy()
1818
print('Generating sparse matrix using RMR...')
1919
np.apply_along_axis(row_operation, 1, copy_matrix, threshold)
2020
S = scipy.sparse.csr_matrix(copy_matrix)
2121
else:
22-
S = np.where(matrix >= threshold, matrix, 0)
22+
S = np.where(A >= threshold, A, 0)
2323
S = scipy.sparse.csr_matrix(S)
2424
return S
2525

compress_rtp/utils/get_sparse_plus_low_rank.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
pass
88

99

10-
def get_sparse_plus_low_rank(A: np.ndarray, thresold_perc: float = 1, rank: int = 5):
10+
def get_sparse_plus_low_rank(A: np.ndarray, threshold_perc: float = 1, rank: int = 5):
1111
"""
1212
:param A: dose influence matrix
13-
:param thresold_perc: thresold percentage. Default to 1% of max(A)
13+
:param threshold_perc: thresold percentage. Default to 1% of max(A)
1414
:type rank: rank of L = A-S.
1515
:returns: S, H, W using randomized svd
1616
"""
17-
tol = np.max(A) * thresold_perc * 0.01
17+
tol = np.max(A) * threshold_perc * 0.01
1818
S = np.where(A > tol, A, 0)
1919
if rank == 0:
2020
S = scipy.sparse.csr_matrix(S)

examples/fluence_wavelets.ipynb

Lines changed: 317 additions & 199 deletions
Large diffs are not rendered by default.

examples/fluence_wavelets.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,28 @@ def ex_wavelet():
158158
sol=[sol_no_quad_no_wav, sol_no_quad_with_wav, sol_quad_no_wav, sol_quad_with_wav],
159159
sol_names=['no_quad_no_wav', 'no_quad_with_wav', 'quad_no_wav', 'quad_with_wav'])
160160

161+
'''
162+
7) Visualize the dose dicrepancy between optimized and final deliverable plan using leaf sequencing
163+
'''
164+
# perform leaf sequencing for quadratic objective without and with wavelets using PortPy
165+
leaf_seq_quad_no_wav = pp.leaf_sequencing_siochi(my_plan, sol_quad_no_wav)
166+
leaf_seq_quad_wav = pp.leaf_sequencing_siochi(my_plan, sol_quad_with_wav)
167+
168+
# Visualize the dvh after performing leaf sequencing using the optimal intensities
169+
# plot DVH for the structures in the given list. Default dose_1d is in Gy and volume is in relative scale(%).
170+
struct_names = ['PTV', 'ESOPHAGUS', 'HEART', 'CORD', 'LUNG_L', 'LUNG_R']
171+
num_fractions = my_plan.get_num_of_fractions()
172+
fig, ax = plt.subplots(1, 2, figsize=(20, 7))
173+
ax0 = pp.Visualization.plot_dvh(my_plan, sol=sol_no_quad_no_wav, struct_names=struct_names, style='solid', ax=ax[0])
174+
ax0 = pp.Visualization.plot_dvh(my_plan, dose_1d=inf_matrix.A @ leaf_seq_quad_no_wav['optimal_intensity'] * num_fractions, struct_names=struct_names, style='dashed', ax=ax0)
175+
fig.suptitle('DVH comparison')
176+
ax0.set_title('Without wavelets \n solid: Before leaf sequencing, Dash: After leaf sequencing')
177+
178+
# fig, ax = plt.subplots(figsize=(12, 8))
179+
ax1 = pp.Visualization.plot_dvh(my_plan, sol=sol_quad_with_wav, struct_names=struct_names, style='solid', ax=ax[1])
180+
ax1 = pp.Visualization.plot_dvh(my_plan, dose_1d=inf_matrix.A @ leaf_seq_quad_wav['optimal_intensity'] * num_fractions, struct_names=struct_names, style='dashed', ax=ax1)
181+
ax1.set_title('With wavelets \n solid: Before leaf sequencing, Dash: After leaf sequencing')
182+
plt.show()
161183

162184
if __name__ == "__main__":
163185
ex_wavelet()

examples/matrix_spare_plus_low_rank.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def sparse_plus_low_rank():
7171
# run optimization with naive thresold of 1% of max(A) and no low rank
7272
# create cvxpy problem using the clinical criteria and optimization parameters
7373
A = deepcopy(inf_matrix.A)
74-
S = get_sparse_only(matrix=A, threshold_perc=1)
74+
S = get_sparse_only(A=A, threshold_perc=1)
7575
# Users can also use below method to get sparse matrix using threshold. Rank=0 is equivalent to above method
7676
# S = get_sparse_plus_low_rank(A=A, thresold_perc=1, rank=0)
7777
inf_matrix.A = S
@@ -81,7 +81,7 @@ def sparse_plus_low_rank():
8181

8282
# run optimization with thresold of 1% and rank 5
8383
# create cvxpy problem using the clinical criteria and optimization parameters
84-
S, H, W = get_sparse_plus_low_rank(A=A, thresold_perc=1, rank=5)
84+
S, H, W = get_sparse_plus_low_rank(A=A, threshold_perc=1, rank=5)
8585
opt = CompressRTPOptimization(my_plan, opt_params=opt_params)
8686
opt.create_cvxpy_problem_compressed(S=S, H=H, W=W)
8787

examples/matrix_sparse_only.ipynb

Lines changed: 137 additions & 137 deletions
Large diffs are not rendered by default.

examples/matrix_sparse_only.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ def matrix_sparse_only_rmr():
6969
# run optimization with naive thresold of 1% of max(A) and no low rank
7070
# create cvxpy problem using the clinical criteria and optimization parameters
7171
A = deepcopy(inf_matrix.A)
72-
S_sparse = get_sparse_only(matrix=A, threshold_perc=1)
72+
S_sparse = get_sparse_only(A=A, threshold_perc=1)
7373
inf_matrix.A = S_sparse
7474
opt = pp.Optimization(my_plan, inf_matrix=inf_matrix, opt_params=opt_params)
7575
opt.create_cvxpy_problem()
7676
sol_sparse_naive = opt.solve(solver='MOSEK', verbose=True)
7777

7878
# run optimization with thresold of 1% and sparsifying matrix using RMR method
7979
# create cvxpy problem using the clinical criteria and optimization parameters
80-
S_rmr = get_sparse_only(matrix=A, threshold_perc=10, compression='rmr')
80+
S_rmr = get_sparse_only(A=A, threshold_perc=10, compression='rmr')
8181
inf_matrix.A = S_rmr
8282
opt = pp.Optimization(my_plan, inf_matrix=inf_matrix, opt_params=opt_params)
8383
opt.create_cvxpy_problem()

examples/matrix_sparse_plus_low_rank.ipynb

Lines changed: 138 additions & 138 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)