Exemple #1
0
def mu_density_per_patient_and_display(filepaths,
                                       display=True,
                                       grid_resolution=1,
                                       max_leaf_gap=400,
                                       leaf_pair_widths=[5] * 80):
    print('Starting')
    print('...calculating beam no. 0')
    mu_density = mu_density_per_beam_and_display(
        filepaths[0],
        display=True,
        grid_resolution=grid_resolution,
        max_leaf_gap=max_leaf_gap,
        leaf_pair_widths=leaf_pair_widths)

    if len(filepaths) > 1:
        for i in range(len(filepaths) - 1):
            print('...calculating beam no. ' + str(i + 1))
            mu_density += mu_density_per_beam_and_display(
                filepaths[i + 1],
                display=True,
                grid_resolution=grid_resolution,
                max_leaf_gap=max_leaf_gap,
                leaf_pair_widths=leaf_pair_widths)

    if display:
        grid = mudensity.get_grid(max_leaf_gap=max_leaf_gap,
                                  grid_resolution=grid_resolution,
                                  leaf_pair_widths=leaf_pair_widths)
        mudensity.display_mu_density(grid, mu_density)
        print('Total MU density of Current Case')
        plt.show()

    return mu_density
Exemple #2
0
def calc_mu_density_per_segment_and_display(data,
                                            check_mlc=True,
                                            display=True,
                                            grid_resolution=1,
                                            max_leaf_gap=400,
                                            leaf_pair_widths=[5] * 80):
    mu = data.loc[:, 'Step Dose/Actual Value (Mu)'].to_numpy()
    mlc_1 = np.expand_dims(data.loc[:, [
        'Y1 Leaf ' + str(i + 1) + '/Scaled Actual (mm)'
        for i in range(len(leaf_pair_widths))
    ]].to_numpy(),
                           axis=-1)
    mlc_2 = np.expand_dims(data.loc[:, [
        'Y2 Leaf ' + str(i + 1) + '/Scaled Actual (mm)'
        for i in range(len(leaf_pair_widths))
    ]].to_numpy(),
                           axis=-1)
    mlc = np.append(mlc_1, mlc_2, 2)
    jaw = data.loc[:, [
        'X1 Diaphragm/Scaled Actual (mm)', 'X2 Diaphragm/Scaled Actual (mm)'
    ]].to_numpy()
    jaw = np.clip(jaw, -200, 200)

    if check_mlc:
        print(
            '.........showing the setup MLC-jaw positions of current segment')
        # Check MLC and jaw coordinates
        plt.plot(mlc[0] * np.array([-1, 1]), np.arange(-200, 200, 5))
        plt.axhline(-jaw[0, 0], xmin=-200, xmax=200, color='red')
        plt.axhline(jaw[0, 1], xmin=-200, xmax=200, color='green')
        plt.xlim(-200, 200)
        plt.ylim(200, -200)
        plt.show()

    mu_density = mudensity.calc_mu_density(mu,
                                           mlc,
                                           jaw,
                                           grid_resolution=grid_resolution,
                                           max_leaf_gap=max_leaf_gap,
                                           leaf_pair_widths=leaf_pair_widths)

    if display:
        grid = mudensity.get_grid(max_leaf_gap=max_leaf_gap,
                                  grid_resolution=grid_resolution,
                                  leaf_pair_widths=leaf_pair_widths)
        mudensity.display_mu_density(grid, mu_density)
        print('......MU Density of Current Segment')
        plt.show()

    return mu_density
Exemple #3
0
def calc_mu_density_per_segment_and_display(cp,
                                            current_weight,
                                            total_mu,
                                            check_mlc=True,
                                            display=True,
                                            grid_resolution=1,
                                            max_leaf_gap=400,
                                            leaf_pair_widths=[5] * 80):
    # Ref: correlate the DICOM MLC leaf positions with Monaco display
    # multipling by [-1, 1]
    raw_mlc = np.array(
        [cp.BeamLimitingDevicePositionSequence[1].LeafJawPositions])
    mlc = np.swapaxes(raw_mlc.reshape(1, 2, len(leaf_pair_widths)), 1,
                      2) * np.array([-1, 1])
    mlc = np.append(mlc, mlc, 0)
    jaw = np.array([cp.BeamLimitingDevicePositionSequence[0].LeafJawPositions
                    ]) * np.array([-1, 1])
    jaw = np.append(jaw, jaw, 0)
    mu = [current_weight * total_mu, cp.CumulativeMetersetWeight * total_mu]

    if check_mlc:
        print(
            '.........showing the setup MLC-jaw positions of current segment')
        # Check MLC and jaw coordinates
        plt.plot(mlc[0] * np.array([-1, 1]), np.arange(-200, 200, 5))
        plt.axhline(-jaw[0, 0], xmin=-200, xmax=200, color='red')
        plt.axhline(jaw[0, 1], xmin=-200, xmax=200, color='green')
        plt.xlim(-200, 200)
        plt.ylim(200, -200)
        plt.show()

    mu_density = mudensity.calc_mu_density(mu,
                                           mlc,
                                           jaw,
                                           grid_resolution=grid_resolution,
                                           max_leaf_gap=max_leaf_gap,
                                           leaf_pair_widths=leaf_pair_widths)
    current_weight = cp.CumulativeMetersetWeight

    if display:
        grid = mudensity.get_grid(max_leaf_gap=max_leaf_gap,
                                  grid_resolution=grid_resolution,
                                  leaf_pair_widths=leaf_pair_widths)
        mudensity.display_mu_density(grid, mu_density)
        print('......MU Density of Current Segment')
        plt.show()

    return mu_density
Exemple #4
0
def mu_density_per_beam_and_display(beam,
                                    total_mu,
                                    display=True,
                                    grid_resolution=1,
                                    max_leaf_gap=400,
                                    leaf_pair_widths=[5] * 80):
    N_CP = beam.NumberOfControlPoints

    print('......calculating segment no. 0')
    mu_density = calc_mu_density_per_segment_and_display(
        beam.ControlPointSequence[0],
        0,
        total_mu=total_mu,
        check_mlc=True,
        display=True,
        grid_resolution=grid_resolution,
        max_leaf_gap=max_leaf_gap,
        leaf_pair_widths=leaf_pair_widths)

    if N_CP > 1:
        current_weight = beam.ControlPointSequence[0].CumulativeMetersetWeight
        for i in range(N_CP - 1):
            print('......calculating segment no. ' + str(i + 1))
            mu_density += calc_mu_density_per_segment_and_display(
                beam.ControlPointSequence[i + 1],
                current_weight,
                total_mu=total_mu,
                check_mlc=False,
                display=False,
                grid_resolution=grid_resolution,
                max_leaf_gap=max_leaf_gap,
                leaf_pair_widths=leaf_pair_widths)
            current_weight = beam.ControlPointSequence[
                i + 1].CumulativeMetersetWeight
    if display:
        grid = mudensity.get_grid(max_leaf_gap=max_leaf_gap,
                                  grid_resolution=grid_resolution,
                                  leaf_pair_widths=leaf_pair_widths)
        mudensity.display_mu_density(grid, mu_density)
        print('...MU Density of Current Beam')
        plt.show()
        print('..............................................')

    return mu_density
Exemple #5
0
def mu_density_per_patient_and_display(file,
                                       fraction_group,
                                       display=True,
                                       grid_resolution=1,
                                       max_leaf_gap=400,
                                       leaf_pair_widths=[5] * 80):
    print('Starting')
    print('...calculating beam no. 0')

    total_mu = fraction_group.ReferencedBeamSequence[0].BeamMeterset
    mu_density = mu_density_per_beam_and_display(
        file.BeamSequence[0],
        total_mu,
        display=True,
        grid_resolution=grid_resolution,
        max_leaf_gap=max_leaf_gap,
        leaf_pair_widths=leaf_pair_widths)

    if fraction_group.NumberOfBeams > 1:
        for i in range(fraction_group.NumberOfBeams - 1):
            print('...calculating beam no. ' + str(i + 1))
            total_mu = fraction_group.ReferencedBeamSequence[i +
                                                             1].BeamMeterset
            mu_density += mu_density_per_beam_and_display(
                file.BeamSequence[i + 1],
                total_mu,
                display=True,
                grid_resolution=grid_resolution,
                max_leaf_gap=max_leaf_gap,
                leaf_pair_widths=leaf_pair_widths)

    if display:
        grid = mudensity.get_grid(max_leaf_gap=max_leaf_gap,
                                  grid_resolution=grid_resolution,
                                  leaf_pair_widths=leaf_pair_widths)
        mudensity.display_mu_density(grid, mu_density)
        print('Total MU density of Current Case')
        plt.show()

    return mu_density
Exemple #6
0
def mu_density_per_beam_and_display(filepath,
                                    display=True,
                                    grid_resolution=1,
                                    max_leaf_gap=400,
                                    leaf_pair_widths=[5] * 80):
    data = _read_beam_log_data(filepath)

    print('......calculating segment no. 0')
    mu_density = calc_mu_density_per_segment_and_display(
        data[data['Segment'] == 1],
        check_mlc=True,
        display=True,
        grid_resolution=grid_resolution,
        max_leaf_gap=max_leaf_gap,
        leaf_pair_widths=leaf_pair_widths)

    if np.amax(data['Segment']) > 1:
        for i in range(np.amax(data['Segment']) - 1):
            print('......calculating segment no. ' + str(i + 1))
            mu_density += calc_mu_density_per_segment_and_display(
                data[data['Segment'] == (i + 2)],
                check_mlc=False,
                display=False,
                grid_resolution=grid_resolution,
                max_leaf_gap=max_leaf_gap,
                leaf_pair_widths=leaf_pair_widths)

    if display:
        grid = mudensity.get_grid(max_leaf_gap=max_leaf_gap,
                                  grid_resolution=grid_resolution,
                                  leaf_pair_widths=leaf_pair_widths)
        mudensity.display_mu_density(grid, mu_density)
        print('...MU Density of Current Beam')
        plt.show()
        print('..............................................')

    return mu_density
Exemple #7
0
                dicom_out_filepath = '{}{}.mudensity.png'.format(
                    prepend_string, dicom_filename)
                filepath_mu_density_map[dicom_out_filepath] = dicom

                diff_filepath = '{}[{}]-[{}].mudensity_diff.png'.format(
                    prepend_string, trf_filename, dicom_filename)
                filepath_mu_density_diff_map[diff_filepath] = diff

maximum_mudensity = np.max(
    [mudensity for _, mudensity in filepath_mu_density_map.items()])

grid = get_grid(grid_resolution=grid_resolution)

for filepath, mudensity in filepath_mu_density_map.items():
    plt.figure()
    display_mu_density(grid, mudensity, vmin=0, vmax=maximum_mudensity)
    plt.savefig(filepath, dpi=image_dpi)

maximum_diff = np.max(
    np.abs([
        mudensity_diff
        for _, mudensity_diff in filepath_mu_density_diff_map.items()
    ]))

for filepath, mudensity_diff in filepath_mu_density_diff_map.items():
    plt.figure()
    display_mu_density(grid,
                       mudensity_diff,
                       vmin=-maximum_diff,
                       vmax=maximum_diff,
                       cmap='seismic')
Exemple #8
0
from pymedphys.mudensity import calc_mu_density, get_grid, display_mu_density

leaf_pair_widths = (5, 5, 5)
max_leaf_gap = 10

mu = [0, 2, 5, 10]
mlc = [[[1, 1], [2, 2], [3, 3]], [[2, 2], [3, 3], [4, 4]],
       [[-2, 3], [-2, 4], [-2, 5]], [[0, 0], [0, 0], [0, 0]]]

jaw = [[7.5, 7.5], [7.5, 7.5], [-2, 7.5], [0, 0]]

grid = get_grid(max_leaf_gap=max_leaf_gap, leaf_pair_widths=leaf_pair_widths)

mu_density = calc_mu_density(mu,
                             mlc,
                             jaw,
                             max_leaf_gap=max_leaf_gap,
                             leaf_pair_widths=leaf_pair_widths)

display_mu_density(grid, mu_density)
Exemple #9
0
# In[12]:

mu_density_dicom = np.loadtxt(
    'C:/Users/sjiae08541/Documents/Project_MU_density/mu_density_dicom.txt')

# In[13]:

grid_resolution = 1
max_leaf_gap = 400
leaf_pair_widths = [5] * 80
grid = mudensity.get_grid(max_leaf_gap=max_leaf_gap,
                          grid_resolution=grid_resolution,
                          leaf_pair_widths=leaf_pair_widths)

mudensity.display_mu_density(grid, -mu_density + np.flip(mu_density_dicom))

# In[ ]:

# In[14]:

### Lulu's Implementation with PyMedPhys
from pymedphys import Delivery

# In[15]:

delivery_data = Delivery.from_logfile(filepaths[0])
mu = delivery_data.monitor_units
mlc = delivery_data.mlc
jaw = delivery_data.jaw