Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#293 fix to calculate_d_cc_x #294

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading