From 5b63855bd8fc2c5a98cf8c6eadbaaee419fc9fae Mon Sep 17 00:00:00 2001 From: brahim Date: Mon, 11 Sep 2023 13:50:59 +0200 Subject: [PATCH] Exceptions are added to manage error on algorithm bottom detection --- View/acoustic_data_tab.py | 222 ++++++++++++++++++++++---------------- 1 file changed, 128 insertions(+), 94 deletions(-) diff --git a/View/acoustic_data_tab.py b/View/acoustic_data_tab.py index e450345..2c6871b 100644 --- a/View/acoustic_data_tab.py +++ b/View/acoustic_data_tab.py @@ -431,7 +431,7 @@ class AcousticDataTab(QWidget): self.gridlayout_compute_bathymetry.addWidget(self.combobox_freq_choice, 0, 0, 2, 1) self.label_from_bathy = QLabel() - self.label_from_bathy.setText("From ") + self.label_from_bathy.setText("From - ") self.gridlayout_compute_bathymetry.addWidget(self.label_from_bathy, 0, 1, 1, 1) self.spinbox_depth_min = QSpinBox() @@ -443,7 +443,7 @@ class AcousticDataTab(QWidget): self.gridlayout_compute_bathymetry.addWidget(self.label_depth_min_unit, 0, 3, 1, 1) self.label_to_bathy = QLabel() - self.label_to_bathy.setText("to ") + self.label_to_bathy.setText("to - ") self.gridlayout_compute_bathymetry.addWidget(self.label_to_bathy, 0, 4, 1, 1) self.spinbox_depth_max = QSpinBox() @@ -822,8 +822,14 @@ class AcousticDataTab(QWidget): def plot_transect_with_BS_raw_data(self): # --- Condition if table is not filled --- - - if self.tableModel.rowCount(1) == 10: + if not self.lineEdit_acoustic_file.text(): + msgBox = QMessageBox() + msgBox.setWindowTitle("Plot transect Error") + msgBox.setIcon(QMessageBox.Warning) + msgBox.setText("Load data before plot transect 2D field") + msgBox.setStandardButtons(QMessageBox.Ok) + msgBox.exec() + elif self.tableModel.rowCount(1) == 10: msgBox = QMessageBox() msgBox.setWindowTitle("Plot transect Error") msgBox.setIcon(QMessageBox.Warning) @@ -925,7 +931,14 @@ class AcousticDataTab(QWidget): self.fig_BS.canvas.draw_idle() def plot_transect_with_SNR_data(self): - if self.tableModel.rowCount(1) == 10: + if not self.lineEdit_noise_file.text(): + msgBox = QMessageBox() + msgBox.setWindowTitle("Plot transect Error") + msgBox.setIcon(QMessageBox.Warning) + msgBox.setText("Load data before plot SNR 2D field") + msgBox.setStandardButtons(QMessageBox.Ok) + msgBox.exec() + elif self.tableModel.rowCount(1) == 10: msgBox = QMessageBox() msgBox.setWindowTitle("Plot transect Error") msgBox.setIcon(QMessageBox.Warning) @@ -1076,8 +1089,8 @@ class AcousticDataTab(QWidget): # msgBox.setText("Plot transect before compute bathymety algorithm") # msgBox.setStandardButtons(QMessageBox.Ok) # msgBox.exec() - # elif (self.canvas_BS) and (self.canvas_SNR == None): - else: + elif self.canvas_BS != None: + # else: # --- Record frequency choose for bottom detection --- stg.freq_bottom_detection = self.combobox_freq_choice.currentIndex() @@ -1095,114 +1108,135 @@ class AcousticDataTab(QWidget): ind_min = np.where(stg.r >= rmin)[0][0] ind_max = np.where(stg.r <= rmax)[0][-1] # Getting the peak - val_bottom[d] = np.nanmax(stg.BS_raw_data[ind_min:ind_max, self.combobox_freq_choice.currentIndex(), d]) - # Getting the range cell of the peak - ind_bottom = np.where(stg.BS_raw_data[ind_min:ind_max, self.combobox_freq_choice.currentIndex(), d] - == val_bottom[d])[0][0] - np.append(stg.ind_bottom, ind_bottom) + try: + val_bottom[d] = np.nanmax(stg.BS_raw_data[ind_min:ind_max, self.combobox_freq_choice.currentIndex(), d]) + except ValueError as e: + msgBox = QMessageBox() + msgBox.setWindowTitle("Detect bottom Error") + msgBox.setIcon(QMessageBox.Warning) + msgBox.setText(f"{e} : maximum value of section bottom is not found. \n " + f"Please change parameter of algorithm") + msgBox.setStandardButtons(QMessageBox.Ok) + msgBox_return = msgBox.exec() + if msgBox_return == msgBox.Ok: + break #msgBox.close() + else: + # Getting the range cell of the peak + ind_bottom = np.where(stg.BS_raw_data[ind_min:ind_max, self.combobox_freq_choice.currentIndex(), d] + == val_bottom[d])[0][0] + np.append(stg.ind_bottom, ind_bottom) - r_bottom[d] = stg.r[ind_bottom + ind_min] - r_bottom_ind.append(ind_bottom + ind_min) - # Updating the range where we will look for the peak (in the next cell) - rmin = r_bottom[d] - locale.atof(self.doublespinbox_next_cell.text()) - rmax = r_bottom[d] + locale.atof(self.doublespinbox_next_cell.text()) + r_bottom[d] = stg.r[ind_bottom + ind_min] + r_bottom_ind.append(ind_bottom + ind_min) + # Updating the range where we will look for the peak (in the next cell) + rmin = r_bottom[d] - locale.atof(self.doublespinbox_next_cell.text()) + rmax = r_bottom[d] + locale.atof(self.doublespinbox_next_cell.text()) BS_section_bottom = np.zeros((stg.r.shape[0], stg.time.shape[0])) for i in range(BS_section_bottom.shape[0]): - # print(r_bottom_temp_ind[i]) - # print(i) - BS_section_bottom[r_bottom_ind[i]][i] = 1 - # print(BS_section_bottom[r_bottom_temp_ind[i]][i]) + try: + BS_section_bottom[r_bottom_ind[i]][i] = 1 + except IndexError as e: + msgBox = QMessageBox() + msgBox.setWindowTitle("Detect bottom Error") + msgBox.setIcon(QMessageBox.Warning) + msgBox.setText(f"{e} : maximum value of section bottom is not found. \n " + f"Please change parameter of algorithm") + msgBox.setStandardButtons(QMessageBox.Ok) + msgBox_return = msgBox.exec() + if msgBox_return == msgBox.Ok: + break # msgBox.close() - # --- Record r_bottom for other tabs --- - stg.r_bottom = r_bottom[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: - np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]] - stg.val_bottom = val_bottom[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: + if BS_section_bottom.sum() > 2: + # --- Record r_bottom for other tabs --- + stg.r_bottom = r_bottom[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]] + stg.val_bottom = val_bottom[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: + np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]] - # --- Plot transect BS with bathymetry --- - for f in range(stg.freq.shape[0]): - self.axis_BS[f].cla() + # --- Plot transect BS with bathymetry --- + for f in range(stg.freq.shape[0]): + self.axis_BS[f].cla() - val_min = np.min(stg.BS_raw_data[:, f, :]) - val_max = np.max(stg.BS_raw_data[:, f, :]) - if val_min == 0: - val_min = 1e-5 + val_min = np.min(stg.BS_raw_data[:, f, :]) + val_max = np.max(stg.BS_raw_data[:, f, :]) + if val_min == 0: + val_min = 1e-5 - pcm = self.axis_BS[f].pcolormesh( - stg.time[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: - np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]], - -stg.r, - (stg.BS_raw_data[:, f, - np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: - np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]]), - cmap='viridis', norm=LogNorm(vmin=val_min, vmax=val_max)) + pcm = self.axis_BS[f].pcolormesh( + stg.time[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: + np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]], + -stg.r, + (stg.BS_raw_data[:, f, + np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: + np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]]), + cmap='viridis', norm=LogNorm(vmin=val_min, vmax=val_max)) - self.axis_BS[f].plot( - stg.time[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: - np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]], - - r_bottom[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: - np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]], - color='black', linewidth=1, linestyle="solid") + self.axis_BS[f].plot( + stg.time[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: + np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]], + - r_bottom[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: + np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]], + color='black', linewidth=1, linestyle="solid") - self.axis_BS[f].text(1, .70, stg.freq_text[f], - fontsize=14, fontweight='bold', fontname="Ubuntu", c="black", alpha=0.5, - horizontalalignment='right', verticalalignment='bottom', - transform=self.axis_BS[f].transAxes) + self.axis_BS[f].text(1, .70, stg.freq_text[f], + fontsize=14, fontweight='bold', fontname="Ubuntu", c="black", alpha=0.5, + horizontalalignment='right', verticalalignment='bottom', + transform=self.axis_BS[f].transAxes) - self.fig_BS.canvas.draw_idle() + self.fig_BS.canvas.draw_idle() - # --- Plot transect SNR with bathymetry --- + # --- Plot transect SNR with bathymetry --- + if self.canvas_SNR != None: + x, y = np.meshgrid( + stg.time[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: + np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]], + stg.r) - x, y = np.meshgrid( - stg.time[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: - np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]], - stg.r) + for f in range(stg.freq.shape[0]): + self.axis_SNR[f].cla() - for f in range(stg.freq.shape[0]): - self.axis_SNR[f].cla() + val_min = np.min(stg.snr[:, f, :]) + val_max = np.max(stg.snr[:, f, :]) + if val_min == 0: + val_min = 1e-5 + if val_max > 1000: + levels = np.array([00.1, 1, 2, 10, 100, 1000, 1e6]) + else: + levels = np.array([00.1, 1, 2, 10, 100, val_max]) + bounds = [00.1, 1, 2, 10, 100, 1000, val_max, val_max * 1.2] + norm = BoundaryNorm(boundaries=bounds, ncolors=300) - val_min = np.min(stg.snr[:, f, :]) - val_max = np.max(stg.snr[:, f, :]) - if val_min == 0: - val_min = 1e-5 - if val_max > 1000: - levels = np.array([00.1, 1, 2, 10, 100, 1000, 1e6]) - else: - levels = np.array([00.1, 1, 2, 10, 100, val_max]) - bounds = [00.1, 1, 2, 10, 100, 1000, val_max, val_max * 1.2] - norm = BoundaryNorm(boundaries=bounds, ncolors=300) + cf = self.axis_SNR[f].contourf(x, -y, + stg.snr[:, f, + np.where(np.round(stg.time, + 2) == self.spinbox_tmin.value())[0][0]: + np.where(np.round(stg.time, + 2) == self.spinbox_tmax.value())[0][0]], + levels, cmap='gist_rainbow', norm=norm) # , shading='gouraud') - cf = self.axis_SNR[f].contourf(x, -y, - stg.snr[:, f, - np.where(np.round(stg.time, - 2) == self.spinbox_tmin.value())[0][0]: - np.where(np.round(stg.time, - 2) == self.spinbox_tmax.value())[0][0]], - levels, cmap='gist_rainbow', norm=norm) # , shading='gouraud') + self.axis_SNR[f].text(1, .70, stg.freq_text[f], + fontsize=14, fontweight='bold', fontname="Ubuntu", c="black", alpha=0.5, + horizontalalignment='right', verticalalignment='bottom', + transform=self.axis_SNR[f].transAxes) - self.axis_SNR[f].text(1, .70, stg.freq_text[f], - fontsize=14, fontweight='bold', fontname="Ubuntu", c="black", alpha=0.5, - horizontalalignment='right', verticalalignment='bottom', - transform=self.axis_SNR[f].transAxes) + self.axis_SNR[f].plot( + stg.time[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: + np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]], + - r_bottom[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: + np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]], + # + np.min(r_bottom[np.where(np.round(noise_data._time, 2) == self.spinbox_tmin.value())[0][0]: + # np.where(np.round(noise_data._time, 2) == self.spinbox_tmax.value())[0][0]]), + # np.max(self._model.r_bottom_cross_section) - self._model.r_bottom_cross_section + np.min(self._model.r_bottom_cross_section), + color='black', linewidth=1, linestyle="solid") - self.axis_SNR[f].plot( - stg.time[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: - np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]], - - r_bottom[np.where(np.round(stg.time, 2) == self.spinbox_tmin.value())[0][0]: - np.where(np.round(stg.time, 2) == self.spinbox_tmax.value())[0][0]], - # + np.min(r_bottom[np.where(np.round(noise_data._time, 2) == self.spinbox_tmin.value())[0][0]: - # np.where(np.round(noise_data._time, 2) == self.spinbox_tmax.value())[0][0]]), - # np.max(self._model.r_bottom_cross_section) - self._model.r_bottom_cross_section + np.min(self._model.r_bottom_cross_section), - color='black', linewidth=1, linestyle="solid") + self.axis_SNR[f].text(1, .70, stg.freq_text[f], + fontsize=14, fontweight='bold', fontname="Ubuntu", c="black", alpha=0.5, + horizontalalignment='right', verticalalignment='bottom', + transform=self.axis_BS[f].transAxes) - self.axis_SNR[f].text(1, .70, stg.freq_text[f], - fontsize=14, fontweight='bold', fontname="Ubuntu", c="black", alpha=0.5, - horizontalalignment='right', verticalalignment='bottom', - transform=self.axis_BS[f].transAxes) - - self.fig_SNR.canvas.draw_idle() + self.fig_SNR.canvas.draw_idle() # else: #