Python Exercise #1B – Field Size Calculator
The field size of a radiation beam is generally defined using the 50% isodose line. When examining cross-plane or in-plane dose profiles, this can be referred to as the full width at half maximum (FWHM). The second python exercise, detailed here, was to develop software for the determination of field width from a dose profile stored in a comma separated value (CSV) file.
This problem can be broken up into the following components:
- Opening CSV files;
- Calculation of the FWHM value.
This solution was developed by Alexander Livingstone, a physicist in the radiation oncology group at the Royal Brisbane & Women’s Hospital. He prepared a iPython notebook here, containing the following solution and more (including flatness and symmetry calculations and plotting of results).
Opening CSV files
loadtxt() function included in NumPy allows the importation of data from a CSV file.
import numpy as np x, y = np.loadtxt("profile.csv", delimiter=',', unpack=True)
Here the data is unpacked into separate variables (
y). The data may need normalisation and smoothing (in this example by a moving average box method), where appropriate:
ynorm = y / y.max() * 100 def smooth(y, box_pts): box = np.ones(box_pts)/box_pts y_smooth = np.convolve(y, box, mode='same') retrun y_smooth ynorm = smooth(ynorm, 20)
There are a number of ways to determine the FWHM of the recorded data, and one is to generate a fitted spline using the
UnivariateSpline() function provided by SciPy, and then determine the distance between the two roots of the spline:
from scipy.interpolate import UnivariateSpline spline = UnivariateSpline(x,ynorm-50,s=0) fwhm = abs(spline.roots()-spline.roots())