Skip to content

Commit

Permalink
Merge pull request #294 from dlbarbee/#293-calculate_d_cc_x-fix
Browse files Browse the repository at this point in the history
#293 fix to calculate_d_cc_x
  • Loading branch information
pchlap authored Nov 19, 2024
2 parents 9911ddd + 37be0b2 commit 86f856a
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions platipy/imaging/dose/dvh.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,36 +202,50 @@ def calculate_v_x(dvh, x, label=None):
return pd.DataFrame(metrics)


def calculate_d_cc_x(dvh, x, label=None):
def calculate_d_cc_x(dvh, x, label=None, index_cols=None):
"""Compute the dose which is received by cc of the volume
Args:
dvh (pandas.DataFrame): DVH DataFrame as produced by calculate_dvh_for_labels
x (float|list): The cc (or list of cc's) to compute the dose at.
label (str, optional): The label to compute the metric for. Computes for all metrics if not
set. Defaults to None.
index_cols (list, optional): List of columns to group by when computing the metric.
Defaults to ["label"].
Returns:
pandas.DataFrame: Data frame with a row for each label containing the metric and value.
"""

# Default to using only label as index_cols
if index_cols is None:
index_cols = ["label"]

if label:
dvh = dvh[dvh.label == label]

if not isinstance(x, list):
x = [x]

metrics = []
for idx in range(len(dvh)):
# Group by struct_hash, dose_hash, and label to ensure unique values per structure
for idx in dvh.groupby(index_cols).groups.keys():

d = dvh.iloc[idx]
m = {"label": d.label}
if isinstance(idx, str):
idx = [idx]

m = {}
group = dvh
for i, col in enumerate(index_cols):
m[col] = idx[i]

group = group[group[col] == idx[i]]

for threshold in x:
cc_at = (threshold / dvh[dvh.label == d.label].cc.iloc[0]) * 100
# Calculate the dose at the specified cc threshold
cc_at = (threshold / group.cc.iloc[0]) * 100
cc_at = min(cc_at, 100)
cc_val = calculate_d_x(dvh[dvh.label == d.label], cc_at)[f"D{cc_at}"].iloc[0]

cc_val = calculate_d_x(group, cc_at)[f"D{cc_at}"].iloc[0]
m[f"D{threshold}cc"] = cc_val

metrics.append(m)
Expand Down

0 comments on commit 86f856a

Please sign in to comment.