Ejemplo n.º 1
0
def test_multiple_threshold_inputs():
    coords, reference, evaluation, _ = get_dummy_gamma_set()

    pymedphys.gamma(
        coords,
        reference,
        coords,
        evaluation,
        [3],
        [0.3, 0.5],
        lower_percent_dose_cutoff=0,
    )
Ejemplo n.º 2
0
def does_gamma_scale_as_expected(init_distance_threshold, threshold_ratio,
                                 scales_to_test):
    coords, reference, evaluation, _ = get_dummy_gamma_set()

    all_scales = np.concatenate([[1], scales_to_test])

    distance_thresholds_to_test = init_distance_threshold * all_scales
    dose_thresholds_to_test = distance_thresholds_to_test * threshold_ratio

    gamma_results = []
    for dose, distance in zip(dose_thresholds_to_test,
                              distance_thresholds_to_test):
        gamma_results.append(
            pymedphys.gamma(
                coords,
                reference,
                coords,
                evaluation,
                dose,
                distance,
                lower_percent_dose_cutoff=0,
            ))

    for i, scale in enumerate(scales_to_test):
        print(scale)

        abs_diff = np.abs(gamma_results[i + 1] - gamma_results[0] / scale)

        print(np.max(abs_diff))

        assert np.all(abs_diff <= 0.1)
Ejemplo n.º 3
0
    def GammaIndex(self, im1, im2, imageThreshold, distance_threshold,
                   interp_fraction, dose_threshold, lower_dose_cutoff):
        shape = np.shape(im1)
        xgrid = np.linspace(1, shape[0] * imageThreshold[0], shape[0])
        ygrid = np.linspace(1, shape[1] * imageThreshold[1], shape[1])
        zgrid = np.linspace(1, shape[2] * imageThreshold[2], shape[2])
        coords1 = (xgrid, ygrid, zgrid)
        shape = np.shape(im2)
        xgrid = np.linspace(1, shape[0] * imageThreshold[0], shape[0])
        ygrid = np.linspace(1, shape[1] * imageThreshold[1], shape[1])
        zgrid = np.linspace(1, shape[2] * imageThreshold[2], shape[2])
        coords2 = (xgrid, ygrid, zgrid)
        ## Gamma index parameters
        gamma_options = {
            'dose_percent_threshold': dose_threshold,
            'distance_mm_threshold': distance_threshold,
            'lower_percent_dose_cutoff': lower_dose_cutoff,
            'interp_fraction':
            interp_fraction,  # Should be 10 or more for more accurate results
            'max_gamma': 2,
            'random_subset': None,
            'local_gamma': False,
            'ram_available': 2**29  # 1/2 GB
        }

        gamma_const = gamma(coords1, im1, coords2, im2, **gamma_options)
        valid_gamma_const = np.ma.masked_invalid(gamma_const)
        valid_gamma_const = valid_gamma_const[~valid_gamma_const.mask]
        size = len(valid_gamma_const)
        if size < 1:
            size = 1
        gamma_index = np.sum(valid_gamma_const <= 1) / size
        return gamma_index, gamma_const
Ejemplo n.º 4
0
def calculate_gamma(reference_mudensity, evaluation_mudensity, gamma_options):
    gamma = pymedphys.gamma(
        COORDS,
        to_tuple(reference_mudensity),
        COORDS,
        to_tuple(evaluation_mudensity),
        **gamma_options,
    )

    return gamma
Ejemplo n.º 5
0
def test_lower_dose_threshold():
    """Verify that the lower dose threshold works as expected"""
    ref = [0, 1, 1.9, 2, 2.1, 3, 4, 5, 10, 10]
    coords_ref = (np.arange(len(ref)), )

    evl = [10] * (len(ref) + 2)
    coords_evl = (np.arange(len(evl)) - 4, )

    result = pymedphys.gamma(coords_ref, ref, coords_evl, evl, 10, 1)

    assert np.array_equal(ref < 0.2 * np.max(ref), np.isnan(result))
Ejemplo n.º 6
0
def calc_gamma(mudensity_tel, mudensity_trf):
    gamma = pymedphys.gamma(
        COORDS,
        mudensity_tel,
        COORDS,
        mudensity_trf,
        PERCENT_DEVIATION,
        MM_DIST_THRESHOLD,
        local_gamma=True,
        quiet=True,
        max_gamma=2,
    )

    return gamma
Ejemplo n.º 7
0
def test_regression_of_gamma_3d():
    """Test for changes in expected 3D gamma."""
    coords, reference, evaluation, expected_gamma = get_dummy_gamma_set()

    gamma3d = np.round(
        pymedphys.gamma(coords,
                        reference,
                        coords,
                        evaluation,
                        3,
                        0.3,
                        lower_percent_dose_cutoff=0),
        decimals=1,
    )

    assert np.all(expected_gamma == gamma3d)
Ejemplo n.º 8
0
def test_regression_of_gamma_2d():
    """Test for changes in expected 2D gamma."""
    coords, reference, evaluation, expected_gamma = get_dummy_gamma_set()

    gamma2d = np.round(
        pymedphys.gamma(
            coords[1::],
            reference[5, :, :],
            coords[1::],
            evaluation[5, :, :],
            3,
            0.3,
            lower_percent_dose_cutoff=0,
        ),
        decimals=1,
    )

    assert np.all(expected_gamma[5, :, :] == gamma2d)
Ejemplo n.º 9
0
def run_gamma(
    filepath_ref,
    filepath_eval,
    random_subset=None,
    max_gamma=1.1,
    dose_threshold=1,
    distance_threshold=1,
):

    if random_subset is not None:
        np.random.seed(42)

    ds_ref = pydicom.read_file(filepath_ref)
    ds_eval = pydicom.read_file(filepath_eval)

    axes_reference = load_yx_from_dicom(ds_ref)
    dose_reference = dose_from_dataset(ds_ref)

    axes_evaluation = load_yx_from_dicom(ds_eval)
    dose_evaluation = dose_from_dataset(ds_eval)

    gamma = pymedphys.gamma(
        axes_reference,
        dose_reference,
        axes_evaluation,
        dose_evaluation,
        dose_threshold,
        distance_threshold,
        lower_percent_dose_cutoff=20,
        interp_fraction=10,
        max_gamma=max_gamma,
        local_gamma=True,
        skip_once_passed=True,
        random_subset=random_subset,
    )

    return gamma
Ejemplo n.º 10
0
    os.system("pip install numpy scipy pydicom")

    import pydicom
    import pymedphys

    reference_filepath = pymedphys.data_path("original_dose_beam_4.dcm")
    evaluation_filepath = pymedphys.data_path("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)

    gamma_options = {
        "dose_percent_threshold": 1,  # This is a bit overkill here at 1%/1mm
        "distance_mm_threshold": 1,
        "lower_percent_dose_cutoff": 20,
        "interp_fraction":
        10,  # Should be 10 or more, see the paper referenced above
        "max_gamma": 2,
        "random_subset": None,  # Can be used to get quick pass rates
        "local_gamma": True,  # Change to false for global gamma
        "ram_available": 2**29,  # 1/2 GB
    }

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