From ba0557294e162bbec7e7cc95fc039e49bd9d7dc7 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Tue, 11 Mar 2025 14:19:18 +0100 Subject: [PATCH] Inversion: Refactoring 'save_result_in_excel_file' method. --- View/acoustic_inversion_tab.py | 159 ++++++++++++++------------------- 1 file changed, 68 insertions(+), 91 deletions(-) diff --git a/View/acoustic_inversion_tab.py b/View/acoustic_inversion_tab.py index 38bc3e5..827f126 100644 --- a/View/acoustic_inversion_tab.py +++ b/View/acoustic_inversion_tab.py @@ -20,16 +20,11 @@ # -*- coding: utf-8 -*- - -import pandas as pd -from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QGroupBox, QComboBox, - QLabel, QPushButton, QSpacerItem, - QSlider, QLineEdit, QMessageBox, QFileDialog) - -from PyQt5.QtCore import QCoreApplication, Qt -from PyQt5.QtGui import QIcon, QPixmap - +import os import numpy as np +import pandas as pd + +from copy import deepcopy import matplotlib.pyplot as plt @@ -37,9 +32,14 @@ from matplotlib.colors import LogNorm from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolBar -from os import chdir +from PyQt5.QtWidgets import ( + QWidget, QVBoxLayout, QHBoxLayout, QGroupBox, QComboBox, + QLabel, QPushButton, QSpacerItem, QSlider, QLineEdit, + QMessageBox, QFileDialog +) -from copy import deepcopy +from PyQt5.QtCore import QCoreApplication, Qt +from PyQt5.QtGui import QIcon, QPixmap from View.checkable_combobox import CheckableComboBox @@ -1671,96 +1671,73 @@ class AcousticInversionTab(QWidget): self.figure_measured_vs_inverted_sand.canvas.draw_idle() def save_result_in_excel_file(self): - if self.combobox_acoustic_data_choice.count() > 0: - name = QFileDialog.getSaveFileName( - caption="Save As - Inversion results", directory="", filter="Excel Files (*.xlsx)", - options=QFileDialog.DontUseNativeDialog) + caption="Save As - Inversion results", + directory="", + filter="Excel Files (*.xlsx)", + options=QFileDialog.DontUseNativeDialog + ) if name[0]: - dirname = "/".join(name[0].split("/")[:-1]) + "/" - filename = name[0].split("/")[-1] - chdir(dirname) + dirname = os.path.dirname(name[0]) + filename = os.path.basename(name[0]) + os.chdir(dirname) + + results = [] for k in range(self.combobox_acoustic_data_choice.count()): - if stg.time_cross_section[k].shape != (0,): - - if stg.depth_cross_section[k].shape != (0,): - - t = np.repeat(stg.time_cross_section[k][stg.frequency_for_inversion[1]], - stg.depth_cross_section[k].shape[1]) - - r = np.zeros((stg.depth_cross_section[k].shape[1] *stg.time_cross_section[k].shape[1],1)) - - for i in range(stg.time_cross_section[k].shape[1]): - for j in range(stg.depth_cross_section[k].shape[1]): - r[i * stg.depth_cross_section[k].shape[1] + j] = ( - stg.depth_cross_section[k][int(stg.frequency_for_inversion[1]), j]) - - if stg.SSC_fine[k].shape == (0,): - stg.SSC_fine[k] = np.zeros(r.shape[0]) - if stg.SSC_sand[k].shape == (0,): - stg.SSC_sand[k] = np.zeros(r.shape[0]) - - else: - - t = np.repeat(stg.time_cross_section[k][stg.frequency_for_inversion[1]], stg.depth[k].shape[1]) - - r = np.zeros((stg.depth[k].shape[1] * stg.time_cross_section[k].shape[1], 1)) - for i in range(stg.time_cross_section[k].shape[1]): - for j in range(stg.depth[k].shape[1]): - r[i * stg.depth[k].shape[1] + j] = ( - stg.depth[k][int(stg.frequency_for_inversion[1]), j]) - - if stg.SSC_fine[k].shape == (0,): - stg.SSC_fine[k] = np.zeros(r.shape[0]) - if stg.SSC_sand[k].shape == (0,): - stg.SSC_sand[k] = np.zeros(r.shape[0]) - + time_data = stg.time_cross_section else: + time_data = stg.time - if stg.depth_cross_section[k].shape != (0,): - - t = np.repeat(stg.time[k][stg.frequency_for_inversion[1]], stg.depth_cross_section[k].shape[1]) - - r = np.zeros((stg.depth_cross_section[k].shape[1] * stg.time[k].shape[1], 1)) - for i in range(stg.time[k].shape[1]): - for j in range(stg.depth_cross_section[k].shape[1]): - r[i * stg.depth_cross_section[k].shape[1] + j] = ( - stg.depth_cross_section[k][int(stg.frequency_for_inversion[1]), j]) - - if stg.SSC_fine[k].shape == (0,): - stg.SSC_fine[k] = np.zeros(r.shape[0]) - if stg.SSC_sand[k].shape == (0,): - stg.SSC_sand[k] = np.zeros(r.shape[0]) - - else: - - t = np.repeat(stg.time[k][stg.frequency_for_inversion[1]], stg.depth[k].shape[1]) - - r = np.zeros(stg.depth[k].shape[1] * stg.time[k].shape[1]) - - for i in range(stg.time[k].shape[1]): - for j in range(stg.depth[k].shape[1]): - r[i * stg.depth[k].shape[1] + j] = ( - stg.depth[k][int(stg.frequency_for_inversion[1]), j]) - - if stg.SSC_fine[k].shape == (0,): - stg.SSC_fine[k] = np.zeros(r.shape[0]) - if stg.SSC_sand[k].shape == (0,): - stg.SSC_sand[k] = np.zeros(r.shape[0]) - - exec("result_" + str(k) + "= pd.DataFrame({'Time (sec)': t," + - "'Depth (m)': r," + - "'SSC_fine (g/L)': stg.SSC_fine[" + str(k) + "].reshape(t.shape[0])," + - "'SSC_sand (g/L)': stg.SSC_sand[" + str(k) + "].reshape(t.shape[0])})") + if stg.depth_cross_section[k].shape != (0,): + depth_data = stg.depth_cross_section + else: + depth_data = stg.depth + t = np.repeat( + time_data[k][stg.frequency_for_inversion[1]], + depth_data[k].shape[1] + ) - with pd.ExcelWriter(dirname + filename + '.xlsx') as writer: + r = np.zeros(( + depth_data[k].shape[1] * time_data[k].shape[1], 1 + )) + + for i in range(time_data[k].shape[1]): + for j in range(depth_data[k].shape[1]): + r_id = i * depth_data[k].shape[1] + j + + r[r_id] = ( + depth_data[k][ + int(stg.frequency_for_inversion[1]), j + ] + ) + + if stg.SSC_fine[k].shape == (0,): + stg.SSC_fine[k] = np.zeros(r.shape[0]) + if stg.SSC_sand[k].shape == (0,): + stg.SSC_sand[k] = np.zeros(r.shape[0]) + + results.append( + pd.DataFrame( + { + 'Time (sec)': t, 'Depth (m)': r, + 'SSC_fine (g/L)': stg.SSC_fine[k].reshape(t.shape[0]), + 'SSC_sand (g/L)': stg.SSC_sand[k].reshape(t.shape[0]), + } + ) + ) + + with pd.ExcelWriter( + os.path.join(dirname, filename + '.xlsx') + ) as writer: for k in range(self.combobox_acoustic_data_choice.count()): - eval("result_" + str(k) + ".to_excel(writer, index=False, " + - "engine='xlsxwriter', na_rep='NA', " + - "sheet_name=stg.data_preprocessed[" + str(k) + "])") + results[k].to_excel( + writer, index=False, + engine='xlsxwriter', na_rep='NA', + sheet_name=stg.data_preprocessed[k], + )