From 0241f33109fcde34c65957dd7bbc009b84ecb1a0 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Wed, 16 Apr 2025 11:30:57 +0200 Subject: [PATCH] Inversion: Some refactoring. --- View/acoustic_inversion_tab.py | 370 +++++++++++++++------------------ 1 file changed, 164 insertions(+), 206 deletions(-) diff --git a/View/acoustic_inversion_tab.py b/View/acoustic_inversion_tab.py index 608a5c7..eb204dd 100644 --- a/View/acoustic_inversion_tab.py +++ b/View/acoustic_inversion_tab.py @@ -911,237 +911,195 @@ class AcousticInversionTab(QWidget): ) def update_lineEdit_by_moving_slider_fine(self): - if stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,): - self.lineEdit_slider_fine.setText( - str(stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1], self.slider_fine.value()])) + data_id = self.combobox_acoustic_data_choice.currentIndex() + + if stg.time_cross_section[data_id].shape != (0,): + time_data = stg.time_cross_section else: - self.lineEdit_slider_fine.setText( - str(stg.time[self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1], self.slider_fine.value()])) + time_data = stg.time + + self.lineEdit_slider_fine.setText( + str( + time_data[data_id][ + stg.frequency_for_inversion[1], + self.slider_fine.value() + ] + ) + ) # --- Plot fine SSC : measured vs inverted --- def fill_combobox_fine_sample(self): + data_id = self.combobox_acoustic_data_choice.currentIndex() - self.combobox_fine_sample_choice.addItems([f for f, _ in stg.sample_fine]) + self.combobox_fine_sample_choice.addItems( + [f for f, _ in stg.sample_fine] + ) # --- Get position (index, value) of sample in acoustic measurement space --- - if ((stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,)) and - (stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,))): + if ((stg.time_cross_section[data_id].shape != (0,)) + and (stg.depth_cross_section[data_id].shape != (0,))): + time_data = stg.time_cross_section + depth_data = stg.depth_cross_section + elif ((stg.time_cross_section[data_id].shape != (0,)) + and (stg.depth[data_id].shape != (0,))): + time_data = stg.time_cross_section + depth_data = stg.depth + elif ((stg.time[data_id].shape != (0,)) + and (stg.depth_cross_section[data_id].shape != (0,))): + time_data = stg.time + depth_data = stg.depth_cross_section + elif ((stg.time[data_id].shape != (0,)) + and (stg.depth[data_id].shape != (0,))): + time_data = stg.time + depth_data = stg.depth - for j in range(len(stg.time_fine)): - - (stg.fine_sample_position. - append( - ( - np.where( - np.abs(stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - stg.time_fine[j]) - == - np.nanmin(np.abs(stg.time_cross_section[ - self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - stg.time_fine[j])) - )[0][0], - np.where( - np.abs(stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - (-stg.depth_fine[j])) - == - np.nanmin(np.abs(stg.depth_cross_section[ - self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - (-stg.depth_fine[j]))) - )[0][0] - ) + for j in range(len(stg.time_fine)): + stg.fine_sample_position\ + .append( + ( + np.where( + np.abs( + time_data[data_id][ + stg.frequency_for_inversion[1] + ] - stg.time_fine[j] + ) == np.nanmin( + np.abs( + time_data[data_id][ + stg.frequency_for_inversion[1] + ] - stg.time_fine[j] + ) ) - ) - - elif ((stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,)) and - (stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,))): - - for j in range(len(stg.time_fine)): - - (stg.fine_sample_position. - append( - ( - np.where( - np.abs(stg.time_cross_section[self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - stg.time_fine[j]) - == - np.nanmin(np.abs(stg.time_cross_section[ - self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - stg.time_fine[j])) - )[0][0], - np.where( - np.abs(stg.depth[self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - (-stg.depth_fine[j])) - == - np.nanmin(np.abs(stg.depth[ - self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - (-stg.depth_fine[j]))) - )[0][0] - ) + )[0][0], + np.where( + np.abs( + depth_data[data_id][ + stg.frequency_for_inversion[1] + ] - (-stg.depth_fine[j])) + == np.nanmin( + np.abs( + depth_data[data_id][ + stg.frequency_for_inversion[1] + ] - (-stg.depth_fine[j]) + ) ) - ) - - elif ((stg.time[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,)) and - (stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,))): - - for j in range(len(stg.time_fine)): - - (stg.fine_sample_position. - append( - ( - np.where( - np.abs(stg.time[self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - stg.time_fine[j]) - == - np.nanmin(np.abs(stg.time[ - self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - stg.time_fine[j])) - )[0][0], - np.where( - np.abs(stg.depth_cross_section[self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - (-stg.depth_fine[j])) - == - np.nanmin(np.abs(stg.depth_cross_section[ - self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - (-stg.depth_fine[j]))) - )[0][0] - ) - ) - ) - - elif ((stg.time[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,)) and - (stg.depth[self.combobox_acoustic_data_choice.currentIndex()].shape != (0,))): - - for j in range(len(stg.time_fine)): - - (stg.fine_sample_position. - append( - ( - np.where( - np.abs(stg.time[self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - stg.time_fine[j]) - == - np.nanmin(np.abs(stg.time[ - self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - stg.time_fine[j])) - )[0][0], - np.where( - np.abs(stg.depth[self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - (-stg.depth_fine[j])) - == - np.nanmin(np.abs(stg.depth[ - self.combobox_acoustic_data_choice.currentIndex()][ - stg.frequency_for_inversion[1]] - (-stg.depth_fine[j]))) - )[0][0] - ) - ) - ) + )[0][0] + ) + ) def plot_measured_vs_inverted_SSC_fine(self): + data_id = self.combobox_acoustic_data_choice.currentIndex() - if self.combobox_acoustic_data_choice.count() > 0: + if self.combobox_acoustic_data_choice.count() <= 0: + return - if stg.SSC_fine[self.combobox_acoustic_data_choice.currentIndex()].shape == (0,): + self.verticalLayout_groupbox_plot_measured_vs_inverted_SSC_fine\ + .removeWidget(self.toolbar_inverted_vs_measured_SSC_fine) + self.verticalLayout_groupbox_plot_measured_vs_inverted_SSC_fine\ + .removeWidget(self.canvas_inverted_vs_measured_SSC_fine) - self.verticalLayout_groupbox_plot_measured_vs_inverted_SSC_fine.removeWidget(self.toolbar_inverted_vs_measured_SSC_fine) - self.verticalLayout_groupbox_plot_measured_vs_inverted_SSC_fine.removeWidget(self.canvas_inverted_vs_measured_SSC_fine) + if stg.SSC_fine[data_id].shape == (0,): + self.canvas_inverted_vs_measured_SSC_fine = FigureCanvas() + self.toolbar_inverted_vs_measured_SSC_fine = NavigationToolBar( + self.canvas_inverted_vs_measured_SSC_fine, self + ) + else: + fig, ax = plt.subplots(nrows=1, ncols=1, layout="constrained") - self.canvas_inverted_vs_measured_SSC_fine = FigureCanvas() - self.toolbar_inverted_vs_measured_SSC_fine = NavigationToolBar(self.canvas_inverted_vs_measured_SSC_fine, self) + self.figure_measured_vs_inverted_fine = fig + self.axis_measured_vs_inverted_fine = ax - self.verticalLayout_groupbox_plot_measured_vs_inverted_SSC_fine.addWidget(self.toolbar_inverted_vs_measured_SSC_fine) - self.verticalLayout_groupbox_plot_measured_vs_inverted_SSC_fine.addWidget(self.canvas_inverted_vs_measured_SSC_fine) + self.canvas_inverted_vs_measured_SSC_fine = FigureCanvas( + self.figure_measured_vs_inverted_fine + ) + self.toolbar_inverted_vs_measured_SSC_fine = NavigationToolBar( + self.canvas_inverted_vs_measured_SSC_fine, self + ) + self.verticalLayout_groupbox_plot_measured_vs_inverted_SSC_fine\ + .addWidget(self.toolbar_inverted_vs_measured_SSC_fine) + self.verticalLayout_groupbox_plot_measured_vs_inverted_SSC_fine\ + .addWidget(self.canvas_inverted_vs_measured_SSC_fine) + + if stg.SSC_fine[data_id].shape == (0,): + fine_id = self.combobox_fine_sample_choice.currentData() + + self.fine_sample_to_plot = [ + int(f[1:]) - 1 for f in fine_id + ] + + if self.fine_sample_to_plot: + fine_range = lambda : self.fine_sample_to_plot else: + fine_range = lambda : range(len(stg.sample_fine)) - self.verticalLayout_groupbox_plot_measured_vs_inverted_SSC_fine.removeWidget(self.toolbar_inverted_vs_measured_SSC_fine) - self.verticalLayout_groupbox_plot_measured_vs_inverted_SSC_fine.removeWidget(self.canvas_inverted_vs_measured_SSC_fine) + self.axis_measured_vs_inverted_fine.plot( + [stg.Ctot_fine[k] for k in fine_range()], + [ + stg.SSC_fine[data_id][ + stg.fine_sample_position[k][1], + stg.fine_sample_position[k][0] + ] for k in fine_range() + ], + ls=" ", marker='o', ms=5, mec='black', mfc="black" + ) - self.figure_measured_vs_inverted_fine, self.axis_measured_vs_inverted_fine = ( - plt.subplots(nrows=1, ncols=1, layout="constrained")) + self.axis_measured_vs_inverted_fine.plot( + [ + 0, np.nanmax( + [ + np.nanmax( + [stg.Ctot_fine[c] for c in fine_range()] + ), + np.nanmax( + [ + stg.SSC_fine[data_id][ + stg.fine_sample_position[i][1], + stg.fine_sample_position[i][0] + ] for i in fine_range() + ] + ) + ] + ) + 1 + ], + [ + 0, np.nanmax( + [ + np.nanmax( + [stg.Ctot_fine[c] for c in fine_range()] + ), + np.nanmax( + [ + stg.SSC_fine[data_id][ + stg.fine_sample_position[i][1], + stg.fine_sample_position[i][0] + ] for i in fine_range() + ] + ) + ] + ) + 1 + ], + ls="solid", linewidth=1, color="k" + ) - self.canvas_inverted_vs_measured_SSC_fine = FigureCanvas(self.figure_measured_vs_inverted_fine) - self.toolbar_inverted_vs_measured_SSC_fine = NavigationToolBar(self.canvas_inverted_vs_measured_SSC_fine, self) + # --- Display sample label on plot --- + for i in fine_range(): + self.axis_measured_vs_inverted_fine.text( + stg.Ctot_fine[i], + stg.SSC_fine[data_id][ + stg.fine_sample_position[i][1], + stg.fine_sample_position[i][0] + ], + stg.sample_fine[i][0], + fontstyle="normal", fontweight="light", fontsize=10 + ) - self.verticalLayout_groupbox_plot_measured_vs_inverted_SSC_fine.addWidget(self.toolbar_inverted_vs_measured_SSC_fine) - self.verticalLayout_groupbox_plot_measured_vs_inverted_SSC_fine.addWidget(self.canvas_inverted_vs_measured_SSC_fine) + self.axis_measured_vs_inverted_fine\ + .set_xlabel("Measured SSC fine (g/L)") + self.axis_measured_vs_inverted_fine\ + .set_ylabel("Inverted SSC fine (g/L)") - self.fine_sample_to_plot = [int(f[1:]) - 1 for f in self.combobox_fine_sample_choice.currentData()] - - if self.fine_sample_to_plot: - - self.axis_measured_vs_inverted_fine.plot( - [stg.Ctot_fine[k] for k in self.fine_sample_to_plot], - [stg.SSC_fine[self.combobox_acoustic_data_choice.currentIndex()][ - stg.fine_sample_position[k][1], - stg.fine_sample_position[k][0]] for k in self.fine_sample_to_plot], - ls=" ", marker='o', ms=5, mec='black', mfc="black" - ) - - self.axis_measured_vs_inverted_fine.plot( - [0, np.nanmax([np.nanmax([stg.Ctot_fine[c] for c in self.fine_sample_to_plot]), - np.nanmax([stg.SSC_fine[self.combobox_acoustic_data_choice.currentIndex()][ - stg.fine_sample_position[i][1], - stg.fine_sample_position[i][0]] - for i in self.fine_sample_to_plot]) ]) + 1], - [0, np.nanmax([np.nanmax([stg.Ctot_fine[c] for c in self.fine_sample_to_plot]), - np.nanmax([stg.SSC_fine[self.combobox_acoustic_data_choice.currentIndex()][ - stg.fine_sample_position[i][1], - stg.fine_sample_position[i][0]] - for i in self.fine_sample_to_plot])]) + 1], - ls="solid", linewidth=1, color="k" - ) - - # --- Display sample label on plot --- - for i in self.fine_sample_to_plot: - self.axis_measured_vs_inverted_fine.text( - stg.Ctot_fine[i], - stg.SSC_fine[self.combobox_acoustic_data_choice.currentIndex()][ - stg.fine_sample_position[i][1], - stg.fine_sample_position[i][0]], - stg.sample_fine[i][0], - fontstyle="normal", fontweight="light", fontsize=10) - - else: - - self.axis_measured_vs_inverted_fine.plot( - [stg.Ctot_fine[k] for k in range(len(stg.sample_fine))], - [stg.SSC_fine[self.combobox_acoustic_data_choice.currentIndex()][ - stg.fine_sample_position[k][1], - stg.fine_sample_position[k][0]] for k in - range(len(stg.sample_fine))], - ls=" ", marker='o', ms=5, mec='black', mfc="black" - ) - - self.axis_measured_vs_inverted_fine.plot( - [0, np.nanmax([np.nanmax([stg.Ctot_fine[c] for c in range(len(stg.sample_fine))]), - np.nanmax([stg.SSC_fine[self.combobox_acoustic_data_choice.currentIndex()][ - stg.fine_sample_position[i][1], - stg.fine_sample_position[i][0]] - for i in range(len(stg.sample_fine))])]) + 1], - [0, np.nanmax([np.nanmax([stg.Ctot_fine[c] for c in range(len(stg.sample_fine))]), - np.nanmax([stg.SSC_fine[self.combobox_acoustic_data_choice.currentIndex()][ - stg.fine_sample_position[i][1], - stg.fine_sample_position[i][0]] - for i in range(len(stg.sample_fine))])]) + 1], - ls="solid", linewidth=1, color="k" - ) - - for j in range(len(stg.sample_fine)): - self.axis_measured_vs_inverted_fine.text( - stg.Ctot_fine[j], - stg.SSC_fine[self.combobox_acoustic_data_choice.currentIndex()][ - stg.fine_sample_position[j][1], - stg.fine_sample_position[j][0]], - stg.sample_fine[j][0], - fontstyle="normal", fontweight="light", fontsize=10) - - self.axis_measured_vs_inverted_fine.set_xlabel("Measured SSC fine (g/L)") - self.axis_measured_vs_inverted_fine.set_ylabel("Inverted SSC fine (g/L)") - - self.figure_measured_vs_inverted_fine.canvas.draw_idle() + self.figure_measured_vs_inverted_fine.canvas.draw_idle() def update_plot_sample_position_on_concentration_field(self): # --- Plot sample position on concentration field ---