Skip to content

Commit

Permalink
Update notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
gourav3017 committed Dec 3, 2024
1 parent dc9e087 commit db37d23
Show file tree
Hide file tree
Showing 10 changed files with 629 additions and 490 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ If you are using PortPy for your radiotherapy research, you can apply RMR sparsi
from compress_rtp.utils.get_sparse_only import get_sparse_only

# Apply RMR sparsification to the matrix A
S = get_sparse_only(matrix=A, threshold_perc=10, compression='rmr')
S = get_sparse_only(A=A, threshold_perc=10, compression='rmr')

# Replace the original matrix A with the sparsified matrix S
inf_matrix.A = S
Expand Down
5 changes: 2 additions & 3 deletions compress_rtp/utils/get_low_dim_basis.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

from typing import TYPE_CHECKING
if TYPE_CHECKING:
from portpy.photon.influence_matrix import InfluenceMatrix

from portpy.photon.influence_matrix import InfluenceMatrix
import numpy as np
import scipy
try:
Expand Down
10 changes: 5 additions & 5 deletions compress_rtp/utils/get_sparse_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
import scipy


def get_sparse_only(matrix: np.ndarray, threshold_perc: float = 1, compression: str = 'naive'):
def get_sparse_only(A: np.ndarray, threshold_perc: float = 1, compression: str = 'naive'):
"""
Get sparse matrix using threshold and different methods
:param matrix: matrix to be sparsified
:param A: matrix to be sparsified
:param threshold_perc: threshold for matrix sparsification
:param compression: Method of Sparsification
:return: Sparse influence matrix
"""
threshold = np.max(matrix) * threshold_perc*0.01
threshold = np.max(A) * threshold_perc * 0.01
if compression == 'rmr':
copy_matrix = matrix.copy()
copy_matrix = A.copy()
print('Generating sparse matrix using RMR...')
np.apply_along_axis(row_operation, 1, copy_matrix, threshold)
S = scipy.sparse.csr_matrix(copy_matrix)
else:
S = np.where(matrix >= threshold, matrix, 0)
S = np.where(A >= threshold, A, 0)
S = scipy.sparse.csr_matrix(S)
return S

Expand Down
6 changes: 3 additions & 3 deletions compress_rtp/utils/get_sparse_plus_low_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
pass


def get_sparse_plus_low_rank(A: np.ndarray, thresold_perc: float = 1, rank: int = 5):
def get_sparse_plus_low_rank(A: np.ndarray, threshold_perc: float = 1, rank: int = 5):
"""
:param A: dose influence matrix
:param thresold_perc: thresold percentage. Default to 1% of max(A)
:param threshold_perc: thresold percentage. Default to 1% of max(A)
:type rank: rank of L = A-S.
:returns: S, H, W using randomized svd
"""
tol = np.max(A) * thresold_perc * 0.01
tol = np.max(A) * threshold_perc * 0.01
S = np.where(A > tol, A, 0)
if rank == 0:
S = scipy.sparse.csr_matrix(S)
Expand Down
516 changes: 317 additions & 199 deletions examples/fluence_wavelets.ipynb

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions examples/fluence_wavelets.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,28 @@ def ex_wavelet():
sol=[sol_no_quad_no_wav, sol_no_quad_with_wav, sol_quad_no_wav, sol_quad_with_wav],
sol_names=['no_quad_no_wav', 'no_quad_with_wav', 'quad_no_wav', 'quad_with_wav'])

'''
7) Visualize the dose dicrepancy between optimized and final deliverable plan using leaf sequencing
'''
# perform leaf sequencing for quadratic objective without and with wavelets using PortPy
leaf_seq_quad_no_wav = pp.leaf_sequencing_siochi(my_plan, sol_quad_no_wav)
leaf_seq_quad_wav = pp.leaf_sequencing_siochi(my_plan, sol_quad_with_wav)

# Visualize the dvh after performing leaf sequencing using the optimal intensities
# plot DVH for the structures in the given list. Default dose_1d is in Gy and volume is in relative scale(%).
struct_names = ['PTV', 'ESOPHAGUS', 'HEART', 'CORD', 'LUNG_L', 'LUNG_R']
num_fractions = my_plan.get_num_of_fractions()
fig, ax = plt.subplots(1, 2, figsize=(20, 7))
ax0 = pp.Visualization.plot_dvh(my_plan, sol=sol_no_quad_no_wav, struct_names=struct_names, style='solid', ax=ax[0])
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)
fig.suptitle('DVH comparison')
ax0.set_title('Without wavelets \n solid: Before leaf sequencing, Dash: After leaf sequencing')

# fig, ax = plt.subplots(figsize=(12, 8))
ax1 = pp.Visualization.plot_dvh(my_plan, sol=sol_quad_with_wav, struct_names=struct_names, style='solid', ax=ax[1])
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)
ax1.set_title('With wavelets \n solid: Before leaf sequencing, Dash: After leaf sequencing')
plt.show()

if __name__ == "__main__":
ex_wavelet()
4 changes: 2 additions & 2 deletions examples/matrix_spare_plus_low_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def sparse_plus_low_rank():
# run optimization with naive thresold of 1% of max(A) and no low rank
# create cvxpy problem using the clinical criteria and optimization parameters
A = deepcopy(inf_matrix.A)
S = get_sparse_only(matrix=A, threshold_perc=1)
S = get_sparse_only(A=A, threshold_perc=1)
# Users can also use below method to get sparse matrix using threshold. Rank=0 is equivalent to above method
# S = get_sparse_plus_low_rank(A=A, thresold_perc=1, rank=0)
inf_matrix.A = S
Expand All @@ -81,7 +81,7 @@ def sparse_plus_low_rank():

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

Expand Down
274 changes: 137 additions & 137 deletions examples/matrix_sparse_only.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/matrix_sparse_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ def matrix_sparse_only_rmr():
# run optimization with naive thresold of 1% of max(A) and no low rank
# create cvxpy problem using the clinical criteria and optimization parameters
A = deepcopy(inf_matrix.A)
S_sparse = get_sparse_only(matrix=A, threshold_perc=1)
S_sparse = get_sparse_only(A=A, threshold_perc=1)
inf_matrix.A = S_sparse
opt = pp.Optimization(my_plan, inf_matrix=inf_matrix, opt_params=opt_params)
opt.create_cvxpy_problem()
sol_sparse_naive = opt.solve(solver='MOSEK', verbose=True)

# run optimization with thresold of 1% and sparsifying matrix using RMR method
# create cvxpy problem using the clinical criteria and optimization parameters
S_rmr = get_sparse_only(matrix=A, threshold_perc=10, compression='rmr')
S_rmr = get_sparse_only(A=A, threshold_perc=10, compression='rmr')
inf_matrix.A = S_rmr
opt = pp.Optimization(my_plan, inf_matrix=inf_matrix, opt_params=opt_params)
opt.create_cvxpy_problem()
Expand Down
276 changes: 138 additions & 138 deletions examples/matrix_sparse_plus_low_rank.ipynb

Large diffs are not rendered by default.

0 comments on commit db37d23

Please sign in to comment.