Gamma from DICOM — PyMedPhys

Toggle navigation sidebar
Toggle in-page Table of Contents

# Gamma from DICOM

PyMedPhys has multiple ways to calculate Gamma. There are also a range of interfaces that can be used. Presented here is a simplified interface which receives as its input two DICOM file paths for the purpose of directly calculating Gamma from a pair of RT DICOM dose files.

    import numpy as np
    import matplotlib.pyplot as plt

    !pip install pydicom pymedphys

    import pydicom
    import pymedphys

    Requirement already satisfied: pydicom in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (2.3.0)
    Requirement already satisfied: pymedphys in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (0.38.0)
    Requirement already satisfied: protobuf<3.19.0 in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from pymedphys) (3.18.1)
    Requirement already satisfied: ipython>=7.31.1 in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from pymedphys) (8.4.0)
    Requirement already satisfied: typing-extensions in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from pymedphys) (4.2.0)
    Requirement already satisfied: stack-data in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from ipython>=7.31.1->pymedphys) (0.2.0)
    Requirement already satisfied: matplotlib-inline in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from ipython>=7.31.1->pymedphys) (0.1.3)
    Requirement already satisfied: backcall in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from ipython>=7.31.1->pymedphys) (0.2.0)
    Requirement already satisfied: jedi>=0.16 in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from ipython>=7.31.1->pymedphys) (0.18.1)
    Requirement already satisfied: pickleshare in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from ipython>=7.31.1->pymedphys) (0.7.5)
    Requirement already satisfied: traitlets>=5 in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from ipython>=7.31.1->pymedphys) (5.2.1.post0)
    Requirement already satisfied: setuptools>=18.5 in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from ipython>=7.31.1->pymedphys) (62.1.0)
    Requirement already satisfied: pygments>=2.4.0 in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from ipython>=7.31.1->pymedphys) (2.12.0)
    Requirement already satisfied: decorator in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from ipython>=7.31.1->pymedphys) (5.1.1)
    Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from ipython>=7.31.1->pymedphys) (3.0.29)
    Requirement already satisfied: colorama in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from ipython>=7.31.1->pymedphys) (0.4.4)
    Requirement already satisfied: parso<0.9.0,>=0.8.0 in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from jedi>=0.16->ipython>=7.31.1->pymedphys) (0.8.3)
    Requirement already satisfied: wcwidth in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=7.31.1->pymedphys) (0.2.5)
    Requirement already satisfied: executing in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from stack-data->ipython>=7.31.1->pymedphys) (0.8.3)
    Requirement already satisfied: asttokens in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from stack-data->ipython>=7.31.1->pymedphys) (2.0.5)
    Requirement already satisfied: pure-eval in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from stack-data->ipython>=7.31.1->pymedphys) (0.2.2)
    Requirement already satisfied: six in c:\users\matthew\appdata\local\pypoetry\cache\virtualenvs\pymedphys-cpenjhgj-py3.10\lib\site-packages (from asttokens->stack-data->ipython>=7.31.1->pymedphys) (1.16.0)

    WARNING: You are using pip version 22.0.4; however, version 22.1.2 is available.
    You should consider upgrading via the 'C:\Users\Matthew\AppData\Local\pypoetry\Cache\virtualenvs\pymedphys-CPEnJHgj-py3.10\Scripts\python.exe -m pip install --upgrade pip' command.

## Getting the demo DICOM files

Let’s download some demo files for the purpose of demonstrating `gamma_dicom` usage.

    reference_filepath = pymedphys.data_path("original_dose_beam_4.dcm")
    reference_filepath

    original_dose_beam_4.dcm?download=1: 0.00B [00:00, ?B/s]

    original_dose_beam_4.dcm?download=1:   0%|                     | 8.19k/24.4M [00:02<2:01:03, 3.36kB/s]

    original_dose_beam_4.dcm?download=1:   0%|                       | 32.8k/24.4M [00:02<26:58, 15.1kB/s]

    original_dose_beam_4.dcm?download=1:   0%|                       | 81.9k/24.4M [00:03<09:51, 41.1kB/s]

    original_dose_beam_4.dcm?download=1:   1%|▏                        | 197k/24.4M [00:03<03:42, 109kB/s]

    original_dose_beam_4.dcm?download=1:   2%|▍                        | 426k/24.4M [00:03<01:34, 253kB/s]

    original_dose_beam_4.dcm?download=1:   4%|▉                        | 868k/24.4M [00:03<00:43, 544kB/s]

    original_dose_beam_4.dcm?download=1:   6%|█▍                      | 1.41M/24.4M [00:04<00:27, 825kB/s]

    original_dose_beam_4.dcm?download=1:   6%|█▍                      | 1.52M/24.4M [00:04<00:30, 741kB/s]

    original_dose_beam_4.dcm?download=1:  11%|██▍                    | 2.57M/24.4M [00:04<00:14, 1.50MB/s]

    original_dose_beam_4.dcm?download=1:  13%|██▉                    | 3.11M/24.4M [00:05<00:14, 1.49MB/s]

    original_dose_beam_4.dcm?download=1:  16%|███▋                   | 3.88M/24.4M [00:05<00:10, 1.88MB/s]

    original_dose_beam_4.dcm?download=1:  18%|████                   | 4.37M/24.4M [00:05<00:11, 1.80MB/s]

    original_dose_beam_4.dcm?download=1:  20%|████▌                  | 4.88M/24.4M [00:06<00:11, 1.76MB/s]

    original_dose_beam_4.dcm?download=1:  22%|█████                  | 5.39M/24.4M [00:06<00:10, 1.74MB/s]

    original_dose_beam_4.dcm?download=1:  24%|█████▌                 | 5.93M/24.4M [00:06<00:10, 1.75MB/s]

    original_dose_beam_4.dcm?download=1:  27%|██████                 | 6.47M/24.4M [00:07<00:10, 1.76MB/s]

    original_dose_beam_4.dcm?download=1:  29%|██████▌                | 7.01M/24.4M [00:07<00:09, 1.77MB/s]

    original_dose_beam_4.dcm?download=1:  31%|███████▏               | 7.57M/24.4M [00:07<00:09, 1.80MB/s]

    original_dose_beam_4.dcm?download=1:  33%|███████▌               | 8.04M/24.4M [00:07<00:07, 2.17MB/s]

    original_dose_beam_4.dcm?download=1:  33%|███████▋               | 8.14M/24.4M [00:07<00:09, 1.70MB/s]

    original_dose_beam_4.dcm?download=1:  35%|████████               | 8.62M/24.4M [00:08<00:07, 2.15MB/s]

    original_dose_beam_4.dcm?download=1:  36%|████████▏              | 8.72M/24.4M [00:08<00:09, 1.65MB/s]

    original_dose_beam_4.dcm?download=1:  38%|████████▋              | 9.19M/24.4M [00:08<00:06, 2.18MB/s]

    original_dose_beam_4.dcm?download=1:  38%|████████▊              | 9.29M/24.4M [00:08<00:09, 1.64MB/s]

    original_dose_beam_4.dcm?download=1:  40%|█████████▏             | 9.73M/24.4M [00:08<00:06, 2.18MB/s]

    original_dose_beam_4.dcm?download=1:  40%|█████████▎             | 9.88M/24.4M [00:08<00:09, 1.61MB/s]

    original_dose_beam_4.dcm?download=1:  43%|█████████▊             | 10.4M/24.4M [00:08<00:06, 2.24MB/s]

    original_dose_beam_4.dcm?download=1:  43%|█████████▊             | 10.5M/24.4M [00:09<00:08, 1.61MB/s]

    original_dose_beam_4.dcm?download=1:  45%|██████████▎            | 11.0M/24.4M [00:09<00:05, 2.24MB/s]

    original_dose_beam_4.dcm?download=1:  45%|██████████▍            | 11.0M/24.4M [00:09<00:08, 1.63MB/s]

    original_dose_beam_4.dcm?download=1:  47%|██████████▊            | 11.5M/24.4M [00:09<00:05, 2.21MB/s]

    original_dose_beam_4.dcm?download=1:  48%|██████████▉            | 11.6M/24.4M [00:09<00:07, 1.65MB/s]

    original_dose_beam_4.dcm?download=1:  49%|███████████▍           | 12.1M/24.4M [00:09<00:05, 2.23MB/s]

    original_dose_beam_4.dcm?download=1:  50%|███████████▌           | 12.2M/24.4M [00:10<00:07, 1.66MB/s]

    original_dose_beam_4.dcm?download=1:  52%|███████████▉           | 12.6M/24.4M [00:10<00:05, 2.18MB/s]

    original_dose_beam_4.dcm?download=1:  53%|████████████           | 12.8M/24.4M [00:10<00:06, 1.68MB/s]

    original_dose_beam_4.dcm?download=1:  54%|████████████▍          | 13.2M/24.4M [00:10<00:05, 2.17MB/s]

    original_dose_beam_4.dcm?download=1:  55%|████████████▋          | 13.4M/24.4M [00:10<00:06, 1.69MB/s]

    original_dose_beam_4.dcm?download=1:  56%|████████████▉          | 13.7M/24.4M [00:10<00:05, 2.03MB/s]

    original_dose_beam_4.dcm?download=1:  57%|█████████████▏         | 14.0M/24.4M [00:10<00:06, 1.70MB/s]

    original_dose_beam_4.dcm?download=1:  59%|█████████████▋         | 14.5M/24.4M [00:11<00:04, 2.31MB/s]

    original_dose_beam_4.dcm?download=1:  60%|█████████████▊         | 14.6M/24.4M [00:11<00:05, 1.65MB/s]

    original_dose_beam_4.dcm?download=1:  62%|██████████████▏        | 15.1M/24.4M [00:11<00:04, 2.31MB/s]

    original_dose_beam_4.dcm?download=1:  62%|██████████████▎        | 15.2M/24.4M [00:11<00:05, 1.63MB/s]

    original_dose_beam_4.dcm?download=1:  64%|██████████████▊        | 15.7M/24.4M [00:11<00:03, 2.30MB/s]

    original_dose_beam_4.dcm?download=1:  65%|██████████████▊        | 15.8M/24.4M [00:11<00:05, 1.62MB/s]

    original_dose_beam_4.dcm?download=1:  67%|███████████████▎       | 16.3M/24.4M [00:12<00:03, 2.30MB/s]

    original_dose_beam_4.dcm?download=1:  67%|███████████████▍       | 16.4M/24.4M [00:12<00:04, 1.61MB/s]

    original_dose_beam_4.dcm?download=1:  69%|███████████████▉       | 16.9M/24.4M [00:12<00:03, 2.30MB/s]

    original_dose_beam_4.dcm?download=1:  69%|███████████████▉       | 17.0M/24.4M [00:12<00:04, 1.61MB/s]

    original_dose_beam_4.dcm?download=1:  72%|████████████████▍      | 17.5M/24.4M [00:12<00:02, 2.32MB/s]

    original_dose_beam_4.dcm?download=1:  72%|████████████████▌      | 17.5M/24.4M [00:12<00:04, 1.61MB/s]

    original_dose_beam_4.dcm?download=1:  74%|█████████████████      | 18.1M/24.4M [00:12<00:02, 2.32MB/s]

    original_dose_beam_4.dcm?download=1:  74%|█████████████████      | 18.1M/24.4M [00:13<00:03, 1.67MB/s]

    original_dose_beam_4.dcm?download=1:  76%|█████████████████▎     | 18.4M/24.4M [00:13<00:03, 1.94MB/s]

    original_dose_beam_4.dcm?download=1:  77%|█████████████████▋     | 18.7M/24.4M [00:13<00:02, 2.10MB/s]

    original_dose_beam_4.dcm?download=1:  77%|█████████████████▋     | 18.7M/24.4M [00:13<00:03, 1.55MB/s]

    original_dose_beam_4.dcm?download=1:  79%|██████████████████▏    | 19.3M/24.4M [00:13<00:02, 2.38MB/s]

    original_dose_beam_4.dcm?download=1:  79%|██████████████████▏    | 19.3M/24.4M [00:13<00:03, 1.69MB/s]

    original_dose_beam_4.dcm?download=1:  80%|██████████████████▍    | 19.6M/24.4M [00:13<00:02, 1.95MB/s]

    original_dose_beam_4.dcm?download=1:  82%|██████████████████▊    | 19.9M/24.4M [00:13<00:02, 2.14MB/s]

    original_dose_beam_4.dcm?download=1:  82%|██████████████████▊    | 19.9M/24.4M [00:14<00:02, 1.60MB/s]

    original_dose_beam_4.dcm?download=1:  83%|███████████████████▏   | 20.4M/24.4M [00:14<00:01, 2.30MB/s]

    original_dose_beam_4.dcm?download=1:  84%|███████████████████▎   | 20.5M/24.4M [00:14<00:02, 1.69MB/s]

    original_dose_beam_4.dcm?download=1:  86%|███████████████████▊   | 21.0M/24.4M [00:14<00:01, 2.28MB/s]

    original_dose_beam_4.dcm?download=1:  87%|███████████████████▉   | 21.2M/24.4M [00:14<00:01, 1.78MB/s]

    original_dose_beam_4.dcm?download=1:  88%|████████████████████▏  | 21.4M/24.4M [00:14<00:01, 2.04MB/s]

    original_dose_beam_4.dcm?download=1:  89%|████████████████████▌  | 21.8M/24.4M [00:14<00:01, 2.22MB/s]

    original_dose_beam_4.dcm?download=1:  89%|████████████████████▌  | 21.8M/24.4M [00:14<00:01, 1.66MB/s]

    original_dose_beam_4.dcm?download=1:  91%|████████████████████▉  | 22.2M/24.4M [00:15<00:00, 2.41MB/s]

    original_dose_beam_4.dcm?download=1:  92%|█████████████████████  | 22.4M/24.4M [00:15<00:01, 1.81MB/s]

    original_dose_beam_4.dcm?download=1:  93%|█████████████████████▍ | 22.7M/24.4M [00:15<00:00, 2.11MB/s]

    original_dose_beam_4.dcm?download=1:  94%|█████████████████████▋ | 23.0M/24.4M [00:15<00:00, 2.34MB/s]

    original_dose_beam_4.dcm?download=1:  94%|█████████████████████▋ | 23.0M/24.4M [00:15<00:00, 1.67MB/s]

    original_dose_beam_4.dcm?download=1:  96%|██████████████████████ | 23.5M/24.4M [00:15<00:00, 2.35MB/s]

    original_dose_beam_4.dcm?download=1:  97%|██████████████████████▎| 23.7M/24.4M [00:15<00:00, 1.88MB/s]

    original_dose_beam_4.dcm?download=1:  98%|██████████████████████▌| 24.0M/24.4M [00:15<00:00, 2.14MB/s]

    original_dose_beam_4.dcm?download=1: 100%|██████████████████████▉| 24.3M/24.4M [00:16<00:00, 1.96MB/s]

    original_dose_beam_4.dcm?download=1: 24.4MB [00:16, 1.50MB/s]

    WindowsPath('C:/Users/Matthew/.pymedphys/data/original_dose_beam_4.dcm')

    evaluation_filepath = pymedphys.data_path("logfile_dose_beam_4.dcm")
    evaluation_filepath

    logfile_dose_beam_4.dcm?download=1: 0.00B [00:00, ?B/s]

    logfile_dose_beam_4.dcm?download=1:   0%|                      | 8.19k/24.4M [00:02<2:10:30, 3.12kB/s]

    logfile_dose_beam_4.dcm?download=1:   0%|                        | 32.8k/24.4M [00:02<28:47, 14.1kB/s]

    logfile_dose_beam_4.dcm?download=1:   0%|                        | 98.3k/24.4M [00:03<08:26, 48.0kB/s]

    logfile_dose_beam_4.dcm?download=1:   1%|▏                         | 213k/24.4M [00:03<03:36, 112kB/s]

    logfile_dose_beam_4.dcm?download=1:   2%|▍                         | 442k/24.4M [00:03<01:35, 250kB/s]

    logfile_dose_beam_4.dcm?download=1:   3%|▉                         | 836k/24.4M [00:04<00:48, 484kB/s]

    logfile_dose_beam_4.dcm?download=1:   4%|▉                         | 868k/24.4M [00:04<00:58, 401kB/s]

    logfile_dose_beam_4.dcm?download=1:   6%|█▍                       | 1.39M/24.4M [00:04<00:30, 753kB/s]

    logfile_dose_beam_4.dcm?download=1:   7%|█▊                       | 1.75M/24.4M [00:05<00:25, 871kB/s]

    logfile_dose_beam_4.dcm?download=1:   9%|██▏                      | 2.11M/24.4M [00:05<00:23, 961kB/s]

    logfile_dose_beam_4.dcm?download=1:  10%|██▍                     | 2.51M/24.4M [00:05<00:20, 1.06MB/s]

    logfile_dose_beam_4.dcm?download=1:  12%|██▊                     | 2.90M/24.4M [00:05<00:19, 1.13MB/s]

    logfile_dose_beam_4.dcm?download=1:  14%|███▎                    | 3.33M/24.4M [00:06<00:17, 1.21MB/s]

    logfile_dose_beam_4.dcm?download=1:  15%|███▋                    | 3.75M/24.4M [00:06<00:16, 1.26MB/s]

    logfile_dose_beam_4.dcm?download=1:  17%|████▏                   | 4.21M/24.4M [00:06<00:15, 1.33MB/s]

    logfile_dose_beam_4.dcm?download=1:  19%|████▌                   | 4.65M/24.4M [00:07<00:14, 1.37MB/s]

    logfile_dose_beam_4.dcm?download=1:  21%|█████                   | 5.13M/24.4M [00:07<00:13, 1.43MB/s]

    logfile_dose_beam_4.dcm?download=1:  23%|█████▌                  | 5.60M/24.4M [00:07<00:12, 1.47MB/s]

    logfile_dose_beam_4.dcm?download=1:  25%|█████▉                  | 6.08M/24.4M [00:08<00:12, 1.50MB/s]

    logfile_dose_beam_4.dcm?download=1:  27%|██████▍                 | 6.55M/24.4M [00:08<00:11, 1.52MB/s]

    logfile_dose_beam_4.dcm?download=1:  29%|██████▉                 | 7.05M/24.4M [00:08<00:11, 1.55MB/s]

    logfile_dose_beam_4.dcm?download=1:  31%|███████▍                | 7.54M/24.4M [00:09<00:10, 1.57MB/s]

    logfile_dose_beam_4.dcm?download=1:  33%|███████▉                | 8.03M/24.4M [00:09<00:08, 1.88MB/s]

    logfile_dose_beam_4.dcm?download=1:  33%|███████▉                | 8.04M/24.4M [00:09<00:10, 1.50MB/s]

    logfile_dose_beam_4.dcm?download=1:  35%|████████▍               | 8.54M/24.4M [00:09<00:10, 1.54MB/s]

    logfile_dose_beam_4.dcm?download=1:  37%|████████▉               | 9.03M/24.4M [00:09<00:09, 1.56MB/s]

    logfile_dose_beam_4.dcm?download=1:  39%|█████████▎              | 9.52M/24.4M [00:10<00:09, 1.58MB/s]

    logfile_dose_beam_4.dcm?download=1:  41%|█████████▊              | 10.0M/24.4M [00:10<00:07, 1.93MB/s]

    logfile_dose_beam_4.dcm?download=1:  41%|█████████▊              | 10.0M/24.4M [00:10<00:09, 1.50MB/s]

    logfile_dose_beam_4.dcm?download=1:  43%|██████████▎             | 10.5M/24.4M [00:10<00:08, 1.55MB/s]

    logfile_dose_beam_4.dcm?download=1:  45%|██████████▊             | 11.0M/24.4M [00:11<00:07, 1.82MB/s]

    logfile_dose_beam_4.dcm?download=1:  45%|██████████▊             | 11.0M/24.4M [00:11<00:08, 1.51MB/s]

    logfile_dose_beam_4.dcm?download=1:  47%|███████████▎            | 11.5M/24.4M [00:11<00:08, 1.55MB/s]

    logfile_dose_beam_4.dcm?download=1:  49%|███████████▊            | 12.0M/24.4M [00:11<00:07, 1.59MB/s]

    logfile_dose_beam_4.dcm?download=1:  51%|████████████▎           | 12.5M/24.4M [00:11<00:06, 1.96MB/s]

    logfile_dose_beam_4.dcm?download=1:  51%|████████████▎           | 12.5M/24.4M [00:12<00:07, 1.50MB/s]

    logfile_dose_beam_4.dcm?download=1:  53%|████████████▊           | 13.0M/24.4M [00:12<00:07, 1.56MB/s]

    logfile_dose_beam_4.dcm?download=1:  55%|█████████████▎          | 13.5M/24.4M [00:12<00:05, 1.96MB/s]

    logfile_dose_beam_4.dcm?download=1:  55%|█████████████▎          | 13.5M/24.4M [00:12<00:07, 1.47MB/s]

    logfile_dose_beam_4.dcm?download=1:  57%|█████████████▊          | 14.0M/24.4M [00:12<00:06, 1.54MB/s]

    logfile_dose_beam_4.dcm?download=1:  59%|██████████████▎         | 14.5M/24.4M [00:13<00:06, 1.59MB/s]

    logfile_dose_beam_4.dcm?download=1:  62%|██████████████▊         | 15.0M/24.4M [00:13<00:04, 1.96MB/s]

    logfile_dose_beam_4.dcm?download=1:  62%|██████████████▊         | 15.0M/24.4M [00:13<00:06, 1.50MB/s]

    logfile_dose_beam_4.dcm?download=1:  64%|███████████████▎        | 15.5M/24.4M [00:13<00:04, 1.97MB/s]

    logfile_dose_beam_4.dcm?download=1:  64%|███████████████▎        | 15.5M/24.4M [00:13<00:06, 1.45MB/s]

    logfile_dose_beam_4.dcm?download=1:  66%|███████████████▋        | 16.0M/24.4M [00:13<00:04, 2.07MB/s]

    logfile_dose_beam_4.dcm?download=1:  66%|███████████████▊        | 16.0M/24.4M [00:14<00:05, 1.41MB/s]

    logfile_dose_beam_4.dcm?download=1:  67%|████████████████▏       | 16.4M/24.4M [00:14<00:04, 1.89MB/s]

    logfile_dose_beam_4.dcm?download=1:  68%|████████████████▎       | 16.6M/24.4M [00:14<00:05, 1.45MB/s]

    logfile_dose_beam_4.dcm?download=1:  69%|████████████████▋       | 16.9M/24.4M [00:14<00:03, 1.93MB/s]

    logfile_dose_beam_4.dcm?download=1:  70%|████████████████▊       | 17.1M/24.4M [00:14<00:04, 1.48MB/s]

    logfile_dose_beam_4.dcm?download=1:  72%|█████████████████▏      | 17.5M/24.4M [00:14<00:03, 1.95MB/s]

    logfile_dose_beam_4.dcm?download=1:  72%|█████████████████▎      | 17.6M/24.4M [00:15<00:04, 1.50MB/s]

    logfile_dose_beam_4.dcm?download=1:  74%|█████████████████▋      | 18.0M/24.4M [00:15<00:03, 1.88MB/s]

    logfile_dose_beam_4.dcm?download=1:  74%|█████████████████▊      | 18.2M/24.4M [00:15<00:04, 1.55MB/s]

    logfile_dose_beam_4.dcm?download=1:  76%|██████████████████▏     | 18.5M/24.4M [00:15<00:03, 1.93MB/s]

    logfile_dose_beam_4.dcm?download=1:  77%|██████████████████▍     | 18.7M/24.4M [00:15<00:03, 1.59MB/s]

    logfile_dose_beam_4.dcm?download=1:  78%|██████████████████▊     | 19.1M/24.4M [00:15<00:02, 2.01MB/s]

    logfile_dose_beam_4.dcm?download=1:  79%|██████████████████▉     | 19.3M/24.4M [00:16<00:03, 1.64MB/s]

    logfile_dose_beam_4.dcm?download=1:  80%|███████████████████▎    | 19.6M/24.4M [00:16<00:02, 2.00MB/s]

    logfile_dose_beam_4.dcm?download=1:  81%|███████████████████▌    | 19.8M/24.4M [00:16<00:02, 1.68MB/s]

    logfile_dose_beam_4.dcm?download=1:  83%|███████████████████▊    | 20.2M/24.4M [00:16<00:02, 2.06MB/s]

    logfile_dose_beam_4.dcm?download=1:  84%|████████████████████    | 20.4M/24.4M [00:16<00:02, 1.99MB/s]

    logfile_dose_beam_4.dcm?download=1:  84%|████████████████████▏   | 20.5M/24.4M [00:16<00:02, 1.62MB/s]

    logfile_dose_beam_4.dcm?download=1:  86%|████████████████████▌   | 20.9M/24.4M [00:16<00:01, 2.36MB/s]

    logfile_dose_beam_4.dcm?download=1:  86%|████████████████████▋   | 21.0M/24.4M [00:16<00:02, 1.66MB/s]

    logfile_dose_beam_4.dcm?download=1:  88%|█████████████████████   | 21.4M/24.4M [00:17<00:01, 2.12MB/s]

    logfile_dose_beam_4.dcm?download=1:  89%|█████████████████████▎  | 21.6M/24.4M [00:17<00:01, 1.76MB/s]

    logfile_dose_beam_4.dcm?download=1:  90%|█████████████████████▋  | 22.0M/24.4M [00:17<00:01, 2.24MB/s]

    logfile_dose_beam_4.dcm?download=1:  91%|█████████████████████▉  | 22.3M/24.4M [00:17<00:01, 1.84MB/s]

    logfile_dose_beam_4.dcm?download=1:  93%|██████████████████████▎ | 22.7M/24.4M [00:17<00:00, 2.30MB/s]

    logfile_dose_beam_4.dcm?download=1:  94%|██████████████████████▌ | 22.9M/24.4M [00:17<00:00, 2.25MB/s]

    logfile_dose_beam_4.dcm?download=1:  94%|██████████████████████▌ | 23.0M/24.4M [00:17<00:00, 1.77MB/s]

    logfile_dose_beam_4.dcm?download=1:  96%|███████████████████████ | 23.5M/24.4M [00:17<00:00, 2.55MB/s]

    logfile_dose_beam_4.dcm?download=1:  97%|███████████████████████▏| 23.6M/24.4M [00:18<00:00, 1.89MB/s]

    logfile_dose_beam_4.dcm?download=1:  99%|███████████████████████▋| 24.1M/24.4M [00:18<00:00, 2.49MB/s]

    logfile_dose_beam_4.dcm?download=1: 100%|███████████████████████▉| 24.3M/24.4M [00:18<00:00, 2.01MB/s]

    logfile_dose_beam_4.dcm?download=1: 24.4MB [00:18, 1.32MB/s]

    WindowsPath('C:/Users/Matthew/.pymedphys/data/logfile_dose_beam_4.dcm')

    reference = pydicom.read_file(str(reference_filepath), force=True)
    evaluation = pydicom.read_file(str(evaluation_filepath), force=True)

    axes_reference, dose_reference = pymedphys.dicom.zyx_and_dose_from_dataset(reference)
    axes_evaluation, dose_evaluation = pymedphys.dicom.zyx_and_dose_from_dataset(evaluation)

    (z_ref, y_ref, x_ref) = axes_reference
    (z_eval, y_eval, x_eval) = axes_evaluation

Importantly the shape of the coordinates are in the same order as the dose axis order

    np.shape(z_ref)

    (152,)

    np.shape(y_ref)

    (158,)

    np.shape(x_ref)

    (254,)

    np.shape(dose_reference)

    (152, 158, 254)

## Calculate and display Gamma

    gamma_options = {
        'dose_percent_threshold': 1,
        'distance_mm_threshold': 1,
        'lower_percent_dose_cutoff': 20,
        'interp_fraction': 10,  # Should be 10 or more for more accurate results
        'max_gamma': 2,
        'random_subset': None,
        'local_gamma': True,
        'ram_available': 2**29  # 1/2 GB
    }

    gamma = pymedphys.gamma(
        axes_reference, dose_reference,
        axes_evaluation, dose_evaluation,
        **gamma_options)

    valid_gamma = gamma[~np.isnan(gamma)]

    num_bins = (
        gamma_options['interp_fraction'] * gamma_options['max_gamma'])
    bins = np.linspace(0, gamma_options['max_gamma'], num_bins + 1)

    plt.hist(valid_gamma, bins, density=True)
    #if density is True, y value is probability density; otherwise, it is count in a bin
    plt.xlim([0, gamma_options['max_gamma']])
    plt.xlabel('gamma index')
    plt.ylabel('probability density')

    pass_ratio = np.sum(valid_gamma <= 1) / len(valid_gamma)

    if gamma_options['local_gamma']:
        gamma_norm_condition = 'Local gamma'
    else:
        gamma_norm_condition = 'Global gamma'

    plt.title(f"Dose cut: {gamma_options['lower_percent_dose_cutoff']}% | {gamma_norm_condition} ({gamma_options['dose_percent_threshold']}%/{gamma_options['distance_mm_threshold']}mm) | Pass Rate(\u03B3<=1): {pass_ratio*100:.2f}% \n ref pts: {len(z_ref)*len(y_ref)*len(x_ref)} | valid \u03B3 pts: {len(valid_gamma)}")

    # plt.savefig('gamma_hist.png', dpi=300)

    Text(0.5, 1.0, 'Dose cut: 20% | Local gamma (1%/1mm) | Pass Rate(γ<=1): 100.00% \n ref pts: 6100064 | valid γ pts: 106910')

## Plotting the Dose and the Gamma

    max_ref_dose = np.max(dose_reference)

    lower_dose_cutoff = gamma_options['lower_percent_dose_cutoff'] / 100 * max_ref_dose

    relevant_slice = (
        np.max(dose_reference, axis=(1, 2)) >
        lower_dose_cutoff)
    slice_start = np.max([
            np.where(relevant_slice)[0][0],
            0])
    slice_end = np.min([
            np.where(relevant_slice)[0][-1],
            len(z_ref)])

    z_vals = z_ref[slice(slice_start, slice_end, 5)]

    eval_slices = [
        dose_evaluation[np.where(z_i == z_eval)[0][0], :, :]
        for z_i in z_vals
    ]

    ref_slices = [
        dose_reference[np.where(z_i == z_ref)[0][0], :, :]
        for z_i in z_vals
    ]

    gamma_slices = [
        gamma[np.where(z_i == z_ref)[0][0], :, :]
        for z_i in z_vals
    ]

    diffs = [
        eval_slice - ref_slice
        for eval_slice, ref_slice
        in zip(eval_slices, ref_slices)
    ]

    max_diff = np.max(np.abs(diffs))

    for i, (eval_slice, ref_slice, diff, gamma_slice) in enumerate(zip(eval_slices, ref_slices, diffs, gamma_slices)):
        fig, ax = plt.subplots(figsize=(13,10), nrows=2, ncols=2)

        fig.suptitle('Slice Z_{0}'.format(slice_start+i*5), fontsize=12)
        c00 = ax[0,0].contourf(
            x_eval, y_eval, eval_slice, 100,
            vmin=0, vmax=max_ref_dose)
        ax[0,0].set_title("Evaluation")
        fig.colorbar(c00, ax=ax[0,0], label='Dose (Gy)')
        ax[0,0].invert_yaxis()
        ax[0,0].set_xlabel('x (mm)')
        ax[0,0].set_ylabel('y (mm)')

        c01 = ax[0,1].contourf(
            x_ref, y_ref, ref_slice, 100,
            vmin=0, vmax=max_ref_dose)
        ax[0,1].set_title("Reference")
        fig.colorbar(c01, ax=ax[0,1], label='Dose (Gy)')
        ax[0,1].invert_yaxis()
        ax[0,1].set_xlabel('x (mm)')
        ax[0,1].set_ylabel('y (mm)')

        c10 = ax[1,0].contourf(
            x_ref, y_ref, diff, 100,
            vmin=-max_diff, vmax=max_diff, cmap=plt.get_cmap('seismic'))
        ax[1,0].set_title("Dose difference")
        cbar = fig.colorbar(c10, ax=ax[1,0], label='[Dose Eval] - [Dose Ref] (Gy)')
        cbar.formatter.set_powerlimits((0, 0)) #use scientific notation
        ax[1,0].invert_yaxis()
        ax[1,0].set_xlabel('x (mm)')
        ax[1,0].set_ylabel('y (mm)')

        c11 = ax[1,1].contourf(
            x_ref, y_ref, gamma_slice, 100,
            vmin=0, vmax=2, cmap=plt.get_cmap('coolwarm'))
        ax[1,1].set_title(
            f"{gamma_norm_condition} ({gamma_options['dose_percent_threshold']} % / {gamma_options['distance_mm_threshold']} mm)")
        fig.colorbar(c11, ax=ax[1,1], label='gamma index')
        ax[1,1].invert_yaxis()
        ax[1,1].set_xlabel('x (mm)')
        ax[1,1].set_ylabel('y (mm)')

        plt.show()
        print("\n")