diff --git a/interpolation_volumes.py b/interpolation_volumes.py index 68a316e..1ab51e4 100644 --- a/interpolation_volumes.py +++ b/interpolation_volumes.py @@ -14,39 +14,18 @@ # plt.style.use('ggplot') -def interpolation_fct(df_ablation, df_radiomics, title, fontsize=22, single_interpolation=True, flag_size=False, flag=None): - if single_interpolation is True: - # perform interpolation as a function of energy - df_radiomics.dropna(subset=['Energy [kj]'], inplace=True) - x = np.asarray(df_ablation['Energy_brochure']) - y = np.asarray(df_ablation['Ablation Volume [ml]_brochure']) - f = interp1d(x, y, fill_value="extrapolate") - df_radiomics.dropna(subset=['Ablation Volume [ml]'], inplace=True) - energy = np.asarray(df_radiomics['Energy [kj]']) - print('No of samples for ' + title + ': ', len(energy)) - ablation_vol_interpolated_brochure = f(energy) - fig, ax = plt.subplots() - plt.plot(x, y, 'o', energy, f(energy), '*') - plt.legend(['data ' + title, 'linear interpolation'], loc='best') - plt.xlabel('Energy [kJ]') - plt.ylim([0, 120]) - plt.xlim([0, 80]) - plt.ylabel('Effective Ablation Volume Brochure [ml]') - plt.grid('on') - plt.show() - figpathHist = os.path.join("figures", title + '_interpolation') - gh.save(figpathHist, width=8, height=8, ext=["png"], close=True) - else: - # perform interpolation as a function of power and time (multivariate interpolation) - points_power = np.asarray(df_ablation['Power']).reshape((len(df_ablation), 1)) - points_time = np.asarray(df_ablation['Time_Duration_Applied']).reshape((len(df_ablation), 1)) - points = np.hstack((points_power, points_time)) - values = np.asarray(df_ablation['Ablation Volume [ml]_brochure']).reshape((len(df_ablation), 1)) - df_radiomics.dropna(subset=['Power', 'Time_Duration_Applied'], inplace=True) - grid_x = np.asarray(df_radiomics['Power']).reshape((len(df_radiomics), 1)) - grid_y = np.asarray(df_radiomics['Time_Duration_Applied']).reshape((len(df_radiomics), 1)) - xi = np.hstack((grid_x, grid_y)) - ablation_vol_interpolated_brochure = griddata(points, values, xi, method='linear') +def interpolation_fct(df_ablation, df_radiomics, title, fontsize=24, flag_size=False, flag=None, flag_energy_axis=False): + + # perform interpolation as a function of power and time (multivariate interpolation) + points_power = np.asarray(df_ablation['Power']).reshape((len(df_ablation), 1)) + points_time = np.asarray(df_ablation['Time_Duration_Applied']).reshape((len(df_ablation), 1)) + points = np.hstack((points_power, points_time)) + values = np.asarray(df_ablation['Ablation Volume [ml]_brochure']).reshape((len(df_ablation), 1)) + df_radiomics.dropna(subset=['Power', 'Time_Duration_Applied'], inplace=True) + grid_x = np.asarray(df_radiomics['Power']).reshape((len(df_radiomics), 1)) + grid_y = np.asarray(df_radiomics['Time_Duration_Applied']).reshape((len(df_radiomics), 1)) + xi = np.hstack((grid_x, grid_y)) + ablation_vol_interpolated_brochure = griddata(points, values, xi, method='linear') # PREDICTED VS MEASURED ablation_vol_measured = np.asarray(df_radiomics['Ablation Volume [ml]']).reshape(len(df_radiomics), 1) @@ -57,6 +36,8 @@ def interpolation_fct(df_ablation, df_radiomics, title, fontsize=22, single_inte size_values = np.asarray(df_radiomics['no_chemo_cycle']).reshape(len(df_radiomics), 1) fig, ax = plt.subplots() + ax2 = ax.twiny() + if flag_size is True: size = np.asarray([100 * (n + 1) for n in size_values]).reshape(len(size_values), 1) size_mask = ~np.isnan(size) @@ -64,19 +45,33 @@ def interpolation_fct(df_ablation, df_radiomics, title, fontsize=22, single_inte sc = ax.scatter(ablation_vol_interpolated_brochure, ablation_vol_measured, color='steelblue', marker='o', alpha=0.7, s=size) plt.show() - legend_1 = ax.legend(*sc.legend_elements("sizes", num=6, func=lambda x: x / 100 - 1, color='steelblue'), + legend_1 = ax.legend(*sc.legend_elements("sizes", num='auto', func=lambda x: x / 100 - 1, color='steelblue'), title=flag, labelspacing=1.5, borderpad=1.5, handletextpad=3.5, fontsize=fontsize, loc='upper right') legend_1.get_title().set_fontsize(str(fontsize)) ax.add_artist(legend_1) + elif flag_energy_axis: + ax.scatter(ablation_vol_interpolated_brochure, ablation_vol_measured, color='steelblue', marker='o', s=100) + ax.set_ylabel('Effective Ablation Volume [ml]', fontsize=fontsize) + ax.set_xlabel('Predicted Ablation Volume Brochure [ml]', fontsize=fontsize) + ax.tick_params(axis='x', labelcolor='steelblue') + ax.set_ylim([0, 100]) + ax.set_xlim([0, 100]) + + energy = df_radiomics['Energy [kj]'] + ax2.scatter(energy, ablation_vol_measured, color='purple', marker='*', s=100, alpha=0.5) + ax2.set_xlabel('Energy [kj]', color='purple', fontsize=fontsize) + ax2.tick_params(axis='x', colors='purple') + ax2.set_ylim([0, 100]) + ax2.set_xlim([0, 100]) else: sc = plt.scatter(ablation_vol_interpolated_brochure, ablation_vol_measured, color='steelblue', marker='o', s=100) - plt.ylabel('Effective Ablation Volume [ml] for ' + title, fontsize=fontsize) - plt.xlabel('Predicted Ablation Volume Brochure [ml] for ' + title, fontsize=fontsize) - plt.ylim([0, 120]) - plt.xlim([0, 120]) - # plt.title(title + ' Nr. Samples: ' + str(len(ablation_vol_measured))) + plt.ylabel('Effective Ablation Volume [ml]', fontsize=fontsize) + plt.xlabel('Predicted Ablation Volume Brochure [ml]', fontsize=fontsize) + plt.ylim([0, 120]) + plt.xlim([0, 120]) + # get the data ready for linear regression X = ablation_vol_interpolated_brochure.reshape(len(ablation_vol_interpolated_brochure), 1) Y = ablation_vol_measured.reshape(len(ablation_vol_measured), 1) mask = ~np.isnan(X) & ~np.isnan(Y) @@ -84,6 +79,7 @@ def interpolation_fct(df_ablation, df_radiomics, title, fontsize=22, single_inte Y = Y[mask] X = X.reshape(len(X), 1) Y = Y.reshape(len(Y), 1) + nr_samples = len(X) regr = linear_model.LinearRegression() regr.fit(X, Y) SS_tot = np.sum((Y - np.mean(Y)) ** 2) @@ -97,13 +93,10 @@ def interpolation_fct(df_ablation, df_radiomics, title, fontsize=22, single_inte ax.tick_params(axis='x', labelsize=fontsize, color='k') plt.tick_params(labelsize=fontsize, color='black') reg = plt.plot(X, regr.predict(X), color='orange', linewidth=1.5, label='Linear Regression') - plt.plot([], [], ' ', label=label_r) - plt.plot([], [], ' ', label=label_r2) - plt.legend(fontsize=fontsize, loc='upper left') - # ax.xaxis.label.set_color('black') - # ax.yaxis.label.set_color('black') - # ax.xaxis.ticks.set_color('black') - # matplotlib.rc('axes', labelcolor='black') + plt.plot([], [], ' ', label='n = '+ str(nr_samples)) + # plt.plot([], [], ' ', label=label_r) + # plt.plot([], [], ' ', label=label_r2) + plt.legend(fontsize=fontsize, loc='best', title=title, title_fontsize=fontsize) if flag is not None: figpathHist = os.path.join("figures", title + '_measured_vs_predicted_volume_power_time_interpolation' + flag) @@ -129,9 +122,6 @@ def interpolation_fct(df_ablation, df_radiomics, title, fontsize=22, single_inte df_radiomics_covidien = df_radiomics[df_radiomics['Device_name'] == 'Covidien (Covidien MWA)'] # flag_options : 1. flag == 'No. chemo cycles' 2. flag == 'Tumour Volume [ml]' - interpolation_fct(df_amica, df_radiomics_amica, 'Amica', single_interpolation=False, flag_size=True, - flag='No. chemo cycles') - interpolation_fct(df_angyodinamics, df_radiomics_angyodinamics, 'Angyodinamics (Solero)', - single_interpolation=False, flag_size=True, flag='No. chemo cycles') - interpolation_fct(df_covidien, df_radiomics_covidien, 'Covidien', single_interpolation=False, flag_size=True, - flag='No. chemo cycles') + interpolation_fct(df_amica, df_radiomics_amica, 'Amica') + interpolation_fct(df_angyodinamics, df_radiomics_angyodinamics, 'Solero') + interpolation_fct(df_covidien, df_radiomics_covidien, 'Covidien')