Skip to content

Commit

Permalink
Merge pull request #193 from pyplati/dose-visualisation
Browse files Browse the repository at this point in the history
Add dose visualisation function
  • Loading branch information
pchlap authored Feb 28, 2023
2 parents 4ecceff + e50dfc8 commit 46e8f80
Show file tree
Hide file tree
Showing 2 changed files with 323 additions and 14 deletions.
82 changes: 68 additions & 14 deletions examples/dvh_analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"try:\n",
" import platipy\n",
"except:\n",
" !pip install git+https://github.com/pyplati/platipy.git\n",
" !pip install platipy\n",
" import platipy\n",
"\n",
"import matplotlib.pyplot as plt\n",
Expand All @@ -31,7 +31,8 @@
"from platipy.imaging.tests.data import get_hn_nifti\n",
"from platipy.imaging import ImageVisualiser\n",
"from platipy.imaging.label.utils import get_com\n",
"from platipy.imaging.dose.dvh import calculate_dvh_for_labels, calculate_d_x, calculate_v_x"
"from platipy.imaging.dose.dvh import calculate_dvh_for_labels, calculate_d_x, calculate_v_x\n",
"from platipy.imaging.visualisation.dose import visualise_dose"
]
},
{
Expand Down Expand Up @@ -102,7 +103,7 @@
"source": [
"vis = ImageVisualiser(ct_image, cut=get_com(structures[\"PTV60\"]))\n",
"\n",
"vis.add_scalar_overlay(dose, discrete_levels=20, colormap=plt.cm.get_cmap(\"inferno\"))\n",
"vis.add_scalar_overlay(dose, discrete_levels=20, colormap=plt.cm.get_cmap(\"inferno\"), name=\"Dose (Gy)\")\n",
"vis.add_contour(structures)\n",
"\n",
"fig = vis.show()"
Expand All @@ -128,12 +129,22 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dvh"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plot DVH\n",
"\n",
"using the pandas DataFrame, we plot the DVH here"
"using the pandas DataFrame, we plot the DVH here. The DVH first needs to be reshaped to prepare it for plotting."
]
},
{
Expand All @@ -142,12 +153,16 @@
"metadata": {},
"outputs": [],
"source": [
"plt_dvh = dvh.melt(id_vars=[\"label\", \"cc\", \"mean\"], var_name=\"bin\", value_name=\"dose\")\n",
"fig, ax = plt.subplots()\n",
"# Reshape the DVH\n",
"plt_dvh = dvh\n",
"plt_dvh = plt_dvh.set_index(\"label\")\n",
"plt_dvh = plt_dvh.iloc[:,3:].transpose()\n",
"\n",
"for key, grp in plt_dvh.groupby(['label']):\n",
" ax = grp.plot(ax=ax, kind='line', x='bin', y='dose', label=key)\n",
"# Plot the DVH\n",
"fig, ax = plt.subplots()\n",
"plt_dvh.plot(ax=ax, kind=\"line\", colormap=plt.cm.get_cmap(\"rainbow\"), legend=False)\n",
"\n",
"# Add labels and show plot\n",
"plt.legend(loc='best')\n",
"plt.xlabel(\"Dose (Gy)\")\n",
"plt.ylabel(\"Frequency\")\n",
Expand All @@ -156,12 +171,13 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## DVH Metrics\n",
"\n",
"Finally, we extract commonly used metrics from the DVH, such as D95 or V40."
"Finally, we extract commonly used metrics from the DVH. In the following cells we extract the D0 and D95 as well as the V5 and V20."
]
},
{
Expand All @@ -170,7 +186,7 @@
"metadata": {},
"outputs": [],
"source": [
"df_metrics_d = calculate_d_x(dvh, 95)\n",
"df_metrics_d = calculate_d_x(dvh, [0, 95])\n",
"df_metrics_d"
]
},
Expand All @@ -180,14 +196,52 @@
"metadata": {},
"outputs": [],
"source": [
"df_metrics_v = calculate_v_x(dvh, 40)\n",
"df_metrics_v = calculate_v_x(dvh, [5, 20])\n",
"df_metrics_v"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Dose and DVH visualisation\n",
"\n",
"The `visualise_dose` function can produce a visualisation including the DVH and dose metrics."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, df_metrics = visualise_dose(\n",
" ct_image,\n",
" dose,\n",
" structures,\n",
" dvh=dvh,\n",
" d_points=[0, 95],\n",
" v_points=[5],\n",
" d_cc_points=[10],\n",
" structure_for_limits=dose>5,\n",
" expansion_for_limits=40,\n",
" contour_cmap=plt.cm.get_cmap(\"rainbow\"),\n",
" dose_cmap=plt.cm.get_cmap(\"inferno\"),\n",
" title=\"TCGA_CV_5977 Dose Metrics\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2.7.16 64-bit",
"display_name": "platipy-Ut1mjAmS-py3.8",
"language": "python",
"name": "python3"
},
Expand All @@ -201,11 +255,11 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "2.7.16"
"version": "3.8.0"
},
"vscode": {
"interpreter": {
"hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
"hash": "950d949bcc8447743ba2e2f4f81e1f06919534c5bd1d053b1a2c5f1f73baee7b"
}
}
},
Expand Down
Loading

0 comments on commit 46e8f80

Please sign in to comment.