def display_mu_density( grid, mu_density, grid_resolution=None, cmap=None, vmin=None, vmax=None ): """Prints a colour plot of the MU Density. Examples -------- See `pymedphys.mudensity.calculate`_. """ if grid_resolution is None: grid_resolution = grid["mlc"][1] - grid["mlc"][0] x, y = pcolormesh_grid(grid["mlc"], grid["jaw"], grid_resolution) plt.pcolormesh(x, y, mu_density, cmap=cmap, vmin=vmin, vmax=vmax) plt.colorbar() plt.title("MU density") plt.xlabel("MLC direction (mm)") plt.ylabel("Jaw direction (mm)") plt.axis("equal") plt.gca().invert_yaxis()
def plot_model(width_data, length_data, factor_data): i, j, k = create_transformed_mesh(width_data, length_data, factor_data) model_width, model_length, model_factor = i, j, k # model_width_mesh, model_length_mesh = np.meshgrid( # model_width, model_length) vmin = np.nanmin( np.concatenate([model_factor.ravel(), factor_data.ravel()])) vmax = np.nanmax( np.concatenate([model_factor.ravel(), factor_data.ravel()])) # vrange = vmax - vmin plt.scatter( width_data, length_data, s=100, c=factor_data, cmap="viridis", vmin=vmin, vmax=vmax, zorder=2, ) plt.colorbar() cs = plt.contour(model_width, model_length, model_factor, 20, vmin=vmin, vmax=vmax) plt.clabel(cs, cs.levels[::2], inline=True) plt.title("Insert model") plt.xlabel("width (cm)") plt.ylabel("length (cm)")
def plot_results( grid_xx, grid_yy, logfile_mu_density, mosaiq_mu_density, diff_colour_scale=0.1 ): min_val = np.min([logfile_mu_density, mosaiq_mu_density]) max_val = np.max([logfile_mu_density, mosaiq_mu_density]) plt.figure() plt.pcolormesh(grid_xx, grid_yy, logfile_mu_density, vmin=min_val, vmax=max_val) plt.colorbar() plt.title("Logfile MU density") plt.xlabel("MLC direction (mm)") plt.ylabel("Jaw direction (mm)") plt.gca().invert_yaxis() plt.figure() plt.pcolormesh(grid_xx, grid_yy, mosaiq_mu_density, vmin=min_val, vmax=max_val) plt.colorbar() plt.title("Mosaiq MU density") plt.xlabel("MLC direction (mm)") plt.ylabel("Jaw direction (mm)") plt.gca().invert_yaxis() scaled_diff = (logfile_mu_density - mosaiq_mu_density) / max_val plt.figure() plt.pcolormesh( grid_xx, grid_yy, scaled_diff, vmin=-diff_colour_scale / 2, vmax=diff_colour_scale / 2, ) plt.colorbar(label="Limited colour range = {}".format(diff_colour_scale / 2)) plt.title("(Logfile - Mosaiq MU density) / Maximum MU Density") plt.xlabel("MLC direction (mm)") plt.ylabel("Jaw direction (mm)") plt.gca().invert_yaxis() plt.show() plt.figure() plt.pcolormesh( grid_xx, grid_yy, scaled_diff, vmin=-diff_colour_scale, vmax=diff_colour_scale ) plt.colorbar(label="Limited colour range = {}".format(diff_colour_scale)) plt.title("(Logfile - Mosaiq MU density) / Maximum MU Density") plt.xlabel("MLC direction (mm)") plt.ylabel("Jaw direction (mm)") plt.gca().invert_yaxis() plt.show() absolute_range = np.max([-np.min(scaled_diff), np.max(scaled_diff)]) plt.figure() plt.pcolormesh( grid_xx, grid_yy, scaled_diff, vmin=-absolute_range, vmax=absolute_range ) plt.colorbar(label="No limited colour range") plt.title("(Logfile - Mosaiq MU density) / Maximum MU Density") plt.xlabel("MLC direction (mm)") plt.ylabel("Jaw direction (mm)") plt.gca().invert_yaxis() plt.show()
def main(): st.write(""" # Electron Insert Factors """) patient_id = st.text_input("Patient ID") if patient_id == "": st.stop() rccc_string_search_pattern = r"\\monacoda\FocalData\RCCC\1~Clinical\*~{}\plan\*\*tel.1".format( patient_id) rccc_filepath_list = glob(rccc_string_search_pattern) nbccc_string_search_pattern = r"\\tunnel-nbcc-monaco\FOCALDATA\NBCCC\1~Clinical\*~{}\plan\*\*tel.1".format( patient_id) nbccc_filepath_list = glob(nbccc_string_search_pattern) sash_string_search_pattern = r"\\tunnel-sash-monaco\Users\Public\Documents\CMS\FocalData\SASH\1~Clinical\*~{}\plan\*\*tel.1".format( patient_id) sash_filepath_list = glob(sash_string_search_pattern) filepath_list = np.concatenate( [rccc_filepath_list, nbccc_filepath_list, sash_filepath_list]) electronmodel_regex = r"RiverinaAgility - (\d+)MeV" applicator_regex = r"(\d+)X\d+" insert_data = dict() # type: ignore for telfilepath in filepath_list: insert_data[telfilepath] = dict() with open(telfilepath, "r") as file: telfilecontents = np.array(file.read().splitlines()) insert_data[telfilepath]["reference_index"] = [] for i, item in enumerate(telfilecontents): if re.search(electronmodel_regex, item): insert_data[telfilepath]["reference_index"] += [i] insert_data[telfilepath]["applicators"] = [ re.search(applicator_regex, telfilecontents[i + 12]).group(1) # type: ignore for i in insert_data[telfilepath]["reference_index"] ] insert_data[telfilepath]["energies"] = [ re.search(electronmodel_regex, telfilecontents[i]).group(1) # type: ignore for i in insert_data[telfilepath]["reference_index"] ] for telfilepath in filepath_list: with open(telfilepath, "r") as file: telfilecontents = np.array(file.read().splitlines()) insert_data[telfilepath]["x"] = [] insert_data[telfilepath]["y"] = [] for i, index in enumerate(insert_data[telfilepath]["reference_index"]): insert_initial_range = telfilecontents[ index + 51::] # coords start 51 lines after electron model name insert_stop = np.where(insert_initial_range == "0")[0][ 0] # coords stop right before a line containing 0 insert_coords_string = insert_initial_range[:insert_stop] insert_coords = np.fromstring(",".join(insert_coords_string), sep=",") insert_data[telfilepath]["x"].append(insert_coords[0::2] / 10) insert_data[telfilepath]["y"].append(insert_coords[1::2] / 10) for telfilepath in filepath_list: insert_data[telfilepath]["width"] = [] insert_data[telfilepath]["length"] = [] insert_data[telfilepath]["circle_centre"] = [] insert_data[telfilepath]["P/A"] = [] for i in range(len(insert_data[telfilepath]["reference_index"])): width, length, circle_centre = electronfactors.parameterise_insert( insert_data[telfilepath]["x"][i], insert_data[telfilepath]["y"][i]) insert_data[telfilepath]["width"].append(width) insert_data[telfilepath]["length"].append(length) insert_data[telfilepath]["circle_centre"].append(circle_centre) insert_data[telfilepath]["P/A"].append( electronfactors.convert2_ratio_perim_area(width, length)) data_filename = r"S:\Physics\RCCC Specific Files\Dosimetry\Elekta_EFacs\electron_factor_measured_data.csv" data = pd.read_csv(data_filename) width_data = data["Width (cm @ 100SSD)"] length_data = data["Length (cm @ 100SSD)"] factor_data = data["RCCC Inverse factor (dose open / dose cutout)"] p_on_a_data = electronfactors.convert2_ratio_perim_area( width_data, length_data) for telfilepath in filepath_list: insert_data[telfilepath]["model_factor"] = [] for i in range(len(insert_data[telfilepath]["reference_index"])): applicator = float(insert_data[telfilepath]["applicators"][i]) energy = float(insert_data[telfilepath]["energies"][i]) ssd = 100 reference = ((data["Energy (MeV)"] == energy) & (data["Applicator (cm)"] == applicator) & (data["SSD (cm)"] == ssd)) number_of_measurements = np.sum(reference) if number_of_measurements < 8: insert_data[telfilepath]["model_factor"].append(np.nan) else: insert_data[telfilepath]["model_factor"].append( electronfactors.spline_model_with_deformability( insert_data[telfilepath]["width"], insert_data[telfilepath]["P/A"], width_data[reference], p_on_a_data[reference], factor_data[reference], )[0]) for telfilepath in filepath_list: st.write("---") st.write("Filepath: `{}`".format(telfilepath)) for i in range(len(insert_data[telfilepath]["reference_index"])): applicator = float(insert_data[telfilepath]["applicators"][i]) energy = float(insert_data[telfilepath]["energies"][i]) ssd = 100 st.write("Applicator: `{} cm` | Energy: `{} MeV`".format( applicator, energy)) width = insert_data[telfilepath]["width"][i] length = insert_data[telfilepath]["length"][i] plt.figure() plot_insert( insert_data[telfilepath]["x"][i], insert_data[telfilepath]["y"][i], insert_data[telfilepath]["width"][i], insert_data[telfilepath]["length"][i], insert_data[telfilepath]["circle_centre"][i], ) reference = ((data["Energy (MeV)"] == energy) & (data["Applicator (cm)"] == applicator) & (data["SSD (cm)"] == ssd)) number_of_measurements = np.sum(reference) plt.figure() if number_of_measurements < 8: plt.scatter( width_data[reference], length_data[reference], s=100, c=factor_data[reference], cmap="viridis", zorder=2, ) plt.colorbar() else: plot_model( width_data[reference], length_data[reference], factor_data[reference], ) reference_data_table = pd.concat( [ width_data[reference], length_data[reference], factor_data[reference] ], axis=1, ) reference_data_table.sort_values( ["RCCC Inverse factor (dose open / dose cutout)"], ascending=False, inplace=True, ) st.write(reference_data_table) st.pyplot() factor = insert_data[telfilepath]["model_factor"][i] st.write( "Width: `{0:0.2f} cm` | Length: `{1:0.2f} cm` | Factor: `{2:0.3f}`" .format(width, length, factor))