This notebook demonstrates sample usage of dicompyler-core.
In [1]:
%matplotlib inline
import os
import numpy as np
from dicompylercore import dicomparser, dvh, dvhcalc
import matplotlib.pyplot as plt
import urllib.request
import os.path
In [2]:
%mkdir -p example_data
repo_url = 'https://github.com/dicompyler/dicompyler-core/blob/master/tests/testdata/file?raw=true'
# files = ['ct.0.dcm', 'rtss.dcm', 'rtplan.dcm', 'rtdose.dcm']
files = ['example_data/{}'.format(y) for y in ['rtss.dcm', 'rtdose.dcm']]
file_urls = [repo_url.replace('file', x) for x in files]
# Only download if the data is not present
[urllib.request.urlretrieve(x, y) for x, y in zip(file_urls, files) if not os.path.exists(y)]
Out[2]:
In [3]:
rtss_dcm = files[0]
rtdose_dcm = files[1]
rtss = dicomparser.DicomParser(rtss_dcm)
rtdose = dicomparser.DicomParser(rtdose_dcm)
In [4]:
key = 5
structures = rtss.GetStructures()
structures[key]
Out[4]:
In [5]:
planes = \
(np.array(rtdose.ds.GridFrameOffsetVector) \
* rtdose.ds.ImageOrientationPatient[0]) \
+ rtdose.ds.ImagePositionPatient[2]
dd = rtdose.GetDoseData()
from ipywidgets import FloatSlider, interactive
w = FloatSlider(
value=0.56,
min=planes[0],
max=planes[-1],
step=np.diff(planes)[0],
description='Slice Position (mm):',
)
def showdose(z):
plt.imshow(rtdose.GetDoseGrid(z) * dd['dosegridscaling'],
vmin=0,
vmax=dd['dosemax'] * dd['dosegridscaling'])
interactive(showdose, z=w)
In [6]:
heart = rtdose.GetDVHs()[key]
heart.name = structures[key]['name']
heart.describe()
In [7]:
heart.relative_volume.plot()
Out[7]:
In [8]:
heart.rx_dose = 14
heart.relative_dose().describe()
In [9]:
tumorbed = rtdose.GetDVHs()[9]
tumorbed.name = structures[9]['name']
tumorbed.rx_dose = 14
In [10]:
tumorbed.relative_volume.differential.plot()
Out[10]:
In [11]:
tumorbed.relative_dose().differential.absolute_dose().cumulative.plot()
Out[11]:
In [12]:
lung = rtdose.GetDVHs()[6]
lung.name = structures[6]['name']
lung.rx_dose = 14
lung.plot()
Out[12]:
In [13]:
lung.max
Out[13]:
In [14]:
lung.V5Gy
Out[14]:
In [15]:
lung.relative_volume.V5Gy
Out[15]:
In [16]:
lung.D2cc
Out[16]:
In [17]:
lung.relative_dose().D2cc
Out[17]:
In [18]:
lung.D1cc == lung.statistic('D1cc') == lung.dose_constraint(1, 'cc')
Out[18]:
In [19]:
plt.figure(figsize=(10, 6))
plt.axis([0, 20, 0, 100])
for s in structures.values():
if not s['empty']:
dvh.DVH.from_dicom_dvh(rtdose.ds, s['id'], name=s['name'], color=s['color']).relative_volume.plot()
In [20]:
b = dvhcalc.get_dvh(rtss_dcm, rtdose_dcm, key)
b.plot()
Out[20]:
In [21]:
def compare_dvh(key=1):
structure = rtss.GetStructures()[key]
orig = dvh.DVH.from_dicom_dvh(rtdose.ds, key, name=structure['name'] + ' Orig')
calc = dvhcalc.get_dvh(rtss_dcm, rtdose_dcm, key)
calc.name = structure['name'] + ' Calc'
orig.compare(calc)
In [22]:
compare_dvh(key)