<jupyter_start><jupyter_text>Hacking your own tools with Pylinac Pylinac's main purpose is to make doing routine quality assurance easier and as automatic as possible. The main modules can be utilized in only a few lines of code and does a complete analysis of your QA images. However, when researching new tools, comparing specific algorithms, or doing custom testing, the default tools aren't appropriate. Pylinac's modules are built on simpler tools that can be used directly or easily extended. In this tutorial, we'll introduce some of pylinac's potential as a toolkit for customizing or creating new tests. This is also viewable as a `Jupyter notebook `_.  The ``image`` Module  In many applications of QA, the physicist has images acquired via the EPID, kV imager, or scanned films. The ``image`` module provides easy tools for loading, manipulating, and converting these images. For this tutorial we'll use a few demo images and showcase the various methods available.  There are a few simple tips for using the ``image`` module:  * Load images directly using ``load``, ``load_url``, or ``load_multiples``.  * There are 3 related image classes to handle the given image types: ``Dicom``, ``File``, and    ``Array``  The docs for the image API are [here](http://pylinac.readthedocs.org/en/latest/core_modules.htmlmodule-pylinac.core.image). First, our imports for the whole tutorial:<jupyter_code>%matplotlib inline

from urllib.request import urlretrieve

import matplotlib.pyplot as plt
import numpy as np

from pylinac.core import image

# pylinac demo images' URL
PF_URL = 'https://s3.amazonaws.com/pylinac/EPID-PF-LR.dcm'
STAR_URL = 'https://s3.amazonaws.com/pylinac/starshot.tif'

# local downloaded images
PF_FILE, _ = urlretrieve(PF_URL)
STAR_FILE, _ = urlretrieve(STAR_URL)

# sample numpy array
ARR = np.arange(36).reshape((6,6))<jupyter_output><empty_output><jupyter_text>Loading images Let's load the picket fence demo image:<jupyter_code>pfimg = image.load_url(PF_URL)
type(pfimg)<jupyter_output><jupyter_text>We can load the local file just as easily:<jupyter_code>pfimg = image.load(PF_FILE)
type(pfimg)<jupyter_output><empty_output><jupyter_text>The ``load()`` function can take an string pointing to a file, a data stream, or a numpy array:<jupyter_code>arr = np.arange(36).reshape((6,6))
img2 = image.load(arr)  # .pixel_array is a numpy array of DICOM file
type(img2)<jupyter_output><empty_output><jupyter_text>Additionally, multiple images can be loaded and superimposed. For example multiple collimator star shots. Just pass a list of the images. In this case I'm passing the same image twice, but the point is this can be done with any set of images:<jupyter_code>superimposed_img = image.load_multiples([STAR_FILE, STAR_FILE])<jupyter_output>C:\Users\James\AppData\Local\Temp\tmpbx8t4po1<jupyter_text>While the ``load()`` function will always do smart image inference for you, if you already know the file type you can instantiate directly. Furthermore, keyword arguments can be passed to FileImage and ArrayImage if they are known.<jupyter_code>dcm_img = image.DicomImage(PF_FILE)
arr_img = image.ArrayImage(ARR, dpi=30, sid=500)
file_img = image.FileImage(STAR_FILE, dpi=50, sid=1000)

file_img2 = image.load(STAR_FILE, dpi=30, sid=500)

type(file_img) == type(file_img2)<jupyter_output><empty_output><jupyter_text>Plotting Images can be easily plotted using the ``plot()`` method:<jupyter_code>dcm_img.plot()<jupyter_output><empty_output><jupyter_text>The plot can also be passed to an existing matplotlib axes:<jupyter_code>fig, ax = plt.subplots()
dcm_img.plot(ax=ax)<jupyter_output><empty_output><jupyter_text>Attributes The image classes contain a number of useful attributes for analyzing and describing the data:<jupyter_code>dcm_img.dpi
dcm_img.dpmm
dcm_img.sid
dcm_img.shape<jupyter_output><empty_output><jupyter_text>There are also attributes that are useful in radiation therapy:<jupyter_code>dcm_img.cax  # the beam CAX
dcm_img.center  # the center location of the image<jupyter_output><empty_output><jupyter_text>Above, the values are the same because the EPID was not translated, which would move the CAX but not the image center.The image values can also be sampled by slicing and indexing:<jupyter_code>dcm_img[12, 60]
dcm_img[:100, 82]<jupyter_output><empty_output><jupyter_text>Data manipulation Now the really fun stuff! There are many methods available to manipulate the data.First, let's smooth the data:<jupyter_code>file_img.filter(kind='median', size=3)
file_img.plot()<jupyter_output><empty_output><jupyter_text>Sometimes starshots from scanned film have edges that are very high or low value (corners of the film can be bent or rounded). We can easily trim the edges:<jupyter_code>file_img.remove_edges(pixels=100)
file_img.plot()<jupyter_output><empty_output><jupyter_text>The data can also be explicitly inverted (EPID images oftentimes need this), or rolled on an axis:<jupyter_code>file_img.invert()
file_img.plot()
file_img.roll(direction='y', amount=300)
file_img.plot()<jupyter_output><empty_output><jupyter_text>We can also rotate and resize the image:<jupyter_code>file_img.rot90(n=1)
file_img.plot()
file_img.resize(size=(1000, 1100))
file_img.plot()<jupyter_output><empty_output><jupyter_text>Scanned film values can be very high, even in low dose areas. We can thus "ground" the image so that the lowest value is zero; this will help us later on when detecting profiles.<jupyter_code>np.min(file_img)  # before grounding
file_img.ground()
np.min(file_img)  # after grounding
file_img.plot()<jupyter_output><empty_output><jupyter_text>We can also apply a high-pass filter to the image:<jupyter_code>thresh_val = np.percentile(file_img, 95)
file_img.threshold(thresh_val)
file_img.plot()<jupyter_output><empty_output><jupyter_text>The image can also be converted to binary, which can be used later for ROI detection. Note that unlike any other method, this returns a new ArrayImage (hinted by the ``as_``)...<jupyter_code>new_img = file_img.as_binary(thresh_val)
new_img.plot()<jupyter_output><empty_output><jupyter_text>...and leaves the original unchanged.<jupyter_code>file_img.plot()<jupyter_output><empty_output><jupyter_text>The ``profile`` Module Physicists often need to evalute a profile, perhaps from a linac beam EPID image, or some fluence profile. The ``profile`` module allows the physicist to find peaks in a 1D array and determine beam profile information (FWHM, penumbra, etc). There are two main profile classes:* ``SingleProfile`` - This class is for profiles with a single peak; e.g. an open beam delivered to a film or EPID. The main goal of this class is to describe the profile (FWHM, penumbra, etc). * ``MultiProfile`` - This class is for profiles with multiple peaks. The main goal of this class is to find the peak or valley locations. A ``MultiProfile`` can be broken down into ``SingleProfiles``.* ``CircleProfile`` - A ``MultiProfile``, but in the shape of a circle. * ``CollapsedCircleProfile`` - A ``CircleProfile`` that "collapses" a thick ring of pixel data to create a 1D profile.The profile API docs are [here](http://pylinac.readthedocs.org/en/latest/core_modules.htmlmodule-pylinac.core.profile).For this demonstration we'll find some peaks and then determine profile information about one of those peaks. Let's use the starshot demo image since it contains all the types of profiles:<jupyter_code>from pylinac.core.profile import SingleProfile, MultiProfile, CircleProfile, CollapsedCircleProfile

star_img = image.load_url(STAR_URL)<jupyter_output><jupyter_text>Using a ``MultiProfile`` Let's start by sampling one row from the starshot image:<jupyter_code>row = star_img[800, :]
plt.plot(row)<jupyter_output><empty_output><jupyter_text>So, judging by the profile, it needs to be filtered for the spurious signals, it has multiple peaks, and it's upside down.Let's make a MultiProfile and clean up the data.<jupyter_code>mprof = MultiProfile(row)
mprof.plot()<jupyter_output><empty_output><jupyter_text>First, let's invert it so that pixel value increases with dose.<jupyter_code>mprof.invert()
mprof.plot()<jupyter_output><empty_output><jupyter_text>We've loaded the profile and inverted it; let's run a filter over it.<jupyter_code>mprof.filter(size=6)
mprof.plot()<jupyter_output><empty_output><jupyter_text>The profile could probably be filtered more since there's still a few spurious signals, but this will work nicely for our demonstration.First, we want to find the peak locations:<jupyter_code>mprof.find_peaks()<jupyter_output><empty_output><jupyter_text>The method has found the 3 major peaks of the profile. Note that there are actually 5 peaks if we count the spurious signals near indices 800 and 1200.For fun, let's see if we can detect these peaks. We can change the parameters to ``find_peaks()`` to optimize our search.<jupyter_code>mprof.find_peaks(threshold=0.1)<jupyter_output><empty_output><jupyter_text>By lowering the peak height threshold we've found another peak; but the peak near 1200 wasn't found. What gives?The ``find_peaks()`` method also eliminates peaks that are too close to one another. We can change that:<jupyter_code>mprof.find_peaks(threshold=0.1, min_distance=0.02)<jupyter_output><empty_output><jupyter_text>By changing the minimum distance peaks must be from each other, we've found the other peak. But, let's say we need to use these settings for whatever reason. We can additionally limit the number of peaks using ``max_number``.<jupyter_code>mprof.find_peaks(threshold=0.1, min_distance=0.02, max_number=3)<jupyter_output><empty_output><jupyter_text>Now, we can visualize where these peaks are by using the ``plot()`` method, which shows the peaks if we've searched for them; note the green dots at the detected peak locations.<jupyter_code>mprof.plot()<jupyter_output><empty_output><jupyter_text>We can also search a given portion of the region; for example if we only wanted to detect peaks in the first half of the profile we can easily add a ``search_region``. Note that the last peak was not detected.<jupyter_code>mprof.find_peaks(search_region=(0, 0.6))  # search the left 60% of the profile<jupyter_output><empty_output><jupyter_text>We can search not simply for the max value peaks, but for the FWHM peaks. Note that these values are slightly different than the max value peaks we found earlier.<jupyter_code>mprof.find_fwxm_peaks(x=50)  # 50 is 50% height<jupyter_output><empty_output><jupyter_text>Finally, we can subdivide the profile into SingleProfile's to further describe single peaks:<jupyter_code>single_profiles = mprof.subdivide()  # returns a list of SingleProfile's<jupyter_output><empty_output><jupyter_text>Using a ``SingleProfile`` ``SingleProfile``s are useful to describe profiles with a single peak. It can describe the FWXM (X=any height), penumbra on each side, field width, and calculations of the field. Continuing from above:<jupyter_code>sprof = single_profiles[0]
sprof.plot()<jupyter_output><empty_output><jupyter_text>The multiprofile has been cut into multiple single profiles, of which this is the first.Let's first find the FWHM, and the center of the FWHM:<jupyter_code>sprof.fwxm(x=50)
sprof.fwxm_center(x=50)<jupyter_output><empty_output><jupyter_text>Note that this is the same value as the first FWHM peak value we found in the MultiProfile.We can now find the penumbra values:<jupyter_code>sprof.penumbra_width(side='left', upper=80, lower=20)
sprof.penumbra_width(upper=90, lower=10)  # default is average of both sides<jupyter_output><empty_output><jupyter_text>The careful reader will notice that the profiles, since we created them, has not had a minimum value of 0. Normally, this would cause problems and sometimes it does, but pylinac normalizes the FWXM and penumbra search. However, just to make sure all is well we can easily shift the values so that the lower bound is 0:<jupyter_code>sprof.ground()
sprof.plot()
sprof.penumbra_width(upper=90, lower=10)<jupyter_output><empty_output><jupyter_text>The average penumbra is the same as we found earlier.We can also normalize and stretch the profile values. Let's first get the original maximum value so we know what we need to restore the profile:<jupyter_code>np.max(sprof)
sprof.normalize()
sprof.plot()<jupyter_output><empty_output><jupyter_text>We can also stretch the values to be bounded by any values we please:<jupyter_code>sprof.stretch(min=30, max=284)
sprof.plot()<jupyter_output><empty_output><jupyter_text>Let's restore our profile based on the earlier max and min values:<jupyter_code>sprof.stretch(max=47, min=0)
sprof.plot()<jupyter_output><empty_output><jupyter_text>Using ``CircleProfile`` and ``CollapsedCircleProfile`` Circular profiles are useful for concentric profiles; starshots are great examples. Let's explore the two circular profiles as they relate to a starshot image. Let's once again load the starshot demo image:<jupyter_code>star_img.plot()<jupyter_output><empty_output><jupyter_text>We saw above from the profile that the image is actually inverted (pixel value decreases with dose), so we need to invert the image:<jupyter_code>star_img.invert()<jupyter_output><empty_output><jupyter_text>To use a ``CircleProfile`` we need to specify the center and radius of the circle, as well as the array to operate over. The approximate center of the starshot is (1450, 1250). Let's also use a radius of 300 pixels.<jupyter_code>cprof = CircleProfile(center=(1250, 1450), radius=300, image_array=star_img)  #  center is given as (x, y)
cprof.plot()<jupyter_output><empty_output><jupyter_text>We have a nice profile showing the starshot peaks. It appears we've cut one of the peaks in half though; this is because the profile starts at 0 radians on the unit circle (directly to the right) and there is a starshot line right at 0. We can also change the direction of the profile from the default of counter-clockwise to clockwise:<jupyter_code>cprof = CircleProfile(center=(1250, 1450), radius=300, image_array=star_img, start_angle=0.2, ccw=False)
cprof.plot()<jupyter_output><empty_output><jupyter_text>Alternatively, we can roll the profile directly:<jupyter_code>cprof.roll(amount=50)
cprof.plot()<jupyter_output><empty_output><jupyter_text>Now, because ``CircleProfile`` is a subclass of ``MultiProfile`` we can search for the peaks:<jupyter_code>cprof.find_peaks()
cprof.plot()<jupyter_output><empty_output><jupyter_text>The profile is 1D, but was derived from a circular sampling. How do we know what the locations of the sampling is? We have x and y attributes:<jupyter_code>cprof.x_locations
cprof.y_locations<jupyter_output><empty_output><jupyter_text>We can also add this profile to a plot to show where it is:<jupyter_code>ax = star_img.plot(show=False)
cprof.plot2axes(ax)<jupyter_output><empty_output><jupyter_text>Looks good! Now let's take a ``CollapsedCircleProfile``. The advantage of this class is that a thick ring is sampled, which averages the pixel values. Thus, noise and spurious signals are reduced. Beyond ``CircleProfile`` there are 2 more keyword arguments:<jupyter_code>ccprof = CollapsedCircleProfile(center=(1250, 1450), radius=300, image_array=star_img, width_ratio=0.2, num_profiles=10)
ccprof.plot()<jupyter_output><empty_output><jupyter_text>Note that this profile looks smoothed; this comes from averaging over the ``num_profiles`` within the ring. The ``width_ratio`` is a function of the radius, so in this case the actual ring width is 300*0.2 = 60 pixels, and 10 equally distributed profiles are taken within that ring.Let's find the peaks and then plot the ring to the starshot image:<jupyter_code>ccprof.find_peaks()
ccprof.plot()
ax = star_img.plot(show=False)
ccprof.plot2axes(ax)<jupyter_output><empty_output><jupyter_text>We now have a good start on a starshot algorithm!<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08685223758126617, 'fraction_numerical': 0.012623871741463106, 'mean_word_length': 6.672639225181598, 'pattern_counts': {'":': 0, '<': 198, '<?xml version=': 0, '>': 198, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '50', 'dir': 'jupyter-structured-clean-dedup', 'id': '635421', 'license': 'MIT', 'n_tokens_mistral': 5167, 'n_tokens_neox': 4911, 'n_words': 1745, 'path': 'docs/source/pylinac_core_hacking.ipynb', 'repo_name': 'samuelharmer/pylinac', 'size': '15843'}