diff --git a/DataBase-Outputs/MaxShearX.pdf b/DataBase-Outputs/MaxShearX.pdf new file mode 100644 index 00000000..3dfe05e7 Binary files /dev/null and b/DataBase-Outputs/MaxShearX.pdf differ diff --git a/Python Scripts/PySeesTKO/pyseestko/plotting.py b/Python Scripts/PySeesTKO/pyseestko/plotting.py index edc8c92b..5c961b89 100644 --- a/Python Scripts/PySeesTKO/pyseestko/plotting.py +++ b/Python Scripts/PySeesTKO/pyseestko/plotting.py @@ -2,18 +2,24 @@ # IMPORT LIBRARIES # ================================================================================== # Objects -from matplotlib.ticker import FuncFormatter -from matplotlib import pyplot as plt -from pathlib import Path -from typing import Union, Tuple -from numpy.typing import NDArray -from scipy.signal import savgol_filter # Para suavizado -from pyseestko.errors import PlottingError -from pyseestko.utilities import pwl -from typing import List +from matplotlib.ticker import FuncFormatter +from matplotlib import pyplot as plt +from pathlib import Path +from typing import Union, Tuple +from numpy.typing import NDArray +from scipy.signal import savgol_filter # Para suavizado +from pyseestko.errors import PlottingError +from pyseestko.utilities import pwl +from typing import List +from sklearn.preprocessing import PowerTransformer +from scipy.stats import shapiro, fligner + # Packages -import pandas as pd -import numpy as np +import statsmodels.api as sm +import scipy.stats as stats +import seaborn as sns +import pandas as pd +import numpy as np # ================================================================================== # MAIN FUNCTIONS CLASS # ================================================================================== @@ -89,55 +95,129 @@ def plotStatisticByReplicas(df:pd.DataFrame, statistic:str, title:str, ylabel:st plt.show() return fig, ax -#NOTE:DEPRECATED -def plotValidation(drifts_df_lst, spectra_df_lst, base_shear_df_lst): - # DRIFTS - mean_drift_x_df, mean_drift_y_df, mean_drift_df = getReplicaCummStatisticDriftDFs(drifts_df_lst, statistic='mean') - std_drift_x_df, std_drift_y_df, std_drift_df = getReplicaCummStatisticDriftDFs(drifts_df_lst, statistic='std') - - # Plot the mean drifts - fig1, ax = plotStatisticByReplicas(df=mean_drift_x_df, statistic='mean', ylabel='Drift', title='Number of replicas vs Mean Drift X') - fig2, ax = plotStatisticByReplicas(df=mean_drift_y_df, statistic='mean', ylabel='Drift', title='Number of replicas vs Mean Drift Y') - fig1.savefig('C:/Users/oioya/OneDrive - miuandes.cl/Escritorio/Git-Updated/Thesis-Project-Simulation-Data-Analysis/DataBase-Outputs/Analysis Output/Number of replicas vs Mean Drift X.png') - fig2.savefig('C:/Users/oioya/OneDrive - miuandes.cl/Escritorio/Git-Updated/Thesis-Project-Simulation-Data-Analysis/DataBase-Outputs/Analysis Output/Number of replicas vs Mean Drift Y.png') - - # Plot the std drifts - fig1, ax = plotStatisticByReplicas(df=std_drift_x_df, statistic='std', ylabel='Acceleration Spectra[m/s/s]', title='Number of replicas vs Std Drift X') - fig2, ax = plotStatisticByReplicas(df=std_drift_y_df, statistic='std', ylabel='Acceleration Spectra[m/s/s]', title='Number of replicas vs Std Drift Y') - fig1.savefig('C:/Users/oioya/OneDrive - miuandes.cl/Escritorio/Git-Updated/Thesis-Project-Simulation-Data-Analysis/DataBase-Outputs/Analysis Output/Number of replicas vs Std Drift X.png') - fig2.savefig('C:/Users/oioya/OneDrive - miuandes.cl/Escritorio/Git-Updated/Thesis-Project-Simulation-Data-Analysis/DataBase-Outputs/Analysis Output/Number of replicas vs Std Drift Y.png') - - # SPECTRUMSS - mean_spectra_x_df, mean_spectra_y_df, mean_spectra_df = getCummStatisticSpectraDFs(spectra_df_lst, statistic='mean') - std_spectra_x_df, std_spectra_y_df, std_spectra_df = getCummStatisticSpectraDFs(spectra_df_lst, statistic='std') - - # Plot the mean spectra - fig1, ax = plotStatisticByReplicas(df=mean_spectra_x_df, statistic='mean', ylabel='Drift', title='Number of replicas vs Mean Spectra X') - fig2, ax = plotStatisticByReplicas(df=mean_spectra_y_df, statistic='mean', ylabel='Drift', title='Number of replicas vs Mean Spectra Y') - fig1.savefig('C:/Users/oioya/OneDrive - miuandes.cl/Escritorio/Git-Updated/Thesis-Project-Simulation-Data-Analysis/DataBase-Outputs/Analysis Output/Number of replicas vs Mean Spectra X.png') - fig2.savefig('C:/Users/oioya/OneDrive - miuandes.cl/Escritorio/Git-Updated/Thesis-Project-Simulation-Data-Analysis/DataBase-Outputs/Analysis Output/Number of replicas vs Mean Spectra Y.png') - - # Plot the std spectra - fig1, ax = plotStatisticByReplicas(df=std_spectra_x_df, statistic='std', ylabel='Acceleration Spectra[m/s/s]',title='Number of replicas vs Std Spectra X') - fig2, ax = plotStatisticByReplicas(df=std_spectra_y_df, statistic='std', ylabel='Acceleration Spectra[m/s/s]',title='Number of replicas vs Std Spectra Y') - fig1.savefig('C:/Users/oioya/OneDrive - miuandes.cl/Escritorio/Git-Updated/Thesis-Project-Simulation-Data-Analysis/DataBase-Outputs/Analysis Output/Number of replicas vs Std Spectra X.png') - fig2.savefig('C:/Users/oioya/OneDrive - miuandes.cl/Escritorio/Git-Updated/Thesis-Project-Simulation-Data-Analysis/DataBase-Outputs/Analysis Output/Number of replicas vs Std Spectra Y.png') - - # SHEAR BASE - max_shear_x_df, max_shear_y_df, max_shear_df = getReplicaCummStatisticBaseShearDFs(base_shear_df_lst, statistic='mean') - - # Plot the mean base shear - fig1, ax = plotStatisticByReplicas(df=max_shear_x_df, statistic='mean', ylabel='Base Shear [kN]', title='Number of replicas vs Mean Base Shear X') - fig2, ax = plotStatisticByReplicas(df=max_shear_y_df, statistic='mean', ylabel='Base Shear [kN]', title='Number of replicas vs Mean Base Shear Y') - fig1.savefig('C:/Users/oioya/OneDrive - miuandes.cl/Escritorio/Git-Updated/Thesis-Project-Simulation-Data-Analysis/DataBase-Outputs/Analysis Output/Number of replicas vs Mean Base Shear X') - fig2.savefig('C:/Users/oioya/OneDrive - miuandes.cl/Escritorio/Git-Updated/Thesis-Project-Simulation-Data-Analysis/DataBase-Outputs/Analysis Output/Number of replicas vs Mean Base Shear Y') - - # Plot the std base shear - fig1, ax = plotStatisticByReplicas(df=max_shear_x_df, statistic='std', ylabel='Base Shear [kN]', title='Number of replicas vs Std Base Shear X') - fig2, ax = plotStatisticByReplicas(df=max_shear_y_df, statistic='std', ylabel='Base Shear [kN]', title='Number of replicas vs Std Base Shear Y') - fig1.savefig('C:/Users/oioya/OneDrive - miuandes.cl/Escritorio/Git-Updated/Thesis-Project-Simulation-Data-Analysis/DataBase-Outputs/Analysis Output/Number of replicas vs Std Base Shear X') - fig2.savefig('C:/Users/oioya/OneDrive - miuandes.cl/Escritorio/Git-Updated/Thesis-Project-Simulation-Data-Analysis/DataBase-Outputs/Analysis Output/Number of replicas vs Std Base Shear Y') +# Función para verificar supuestos y generar gráficos de normalidad +def analyze_manova_assumptions(df, dependent_vars, group_vars, project_path, drift=True, xdir=True): + plt.rcParams.update({ + "text.usetex": True, + "font.size": 13, + "font.family": "serif", + "text.latex.preamble": r'\usepackage{amsmath}' + }) + + # Filtrar solo las columnas de interés + df = df[dependent_vars + group_vars] + + # Transformación de Box-Cox + pt = PowerTransformer(method='box-cox', standardize=True) + X_transformed = pt.fit_transform(df[dependent_vars]) + df_transformed = pd.DataFrame(X_transformed, columns=dependent_vars) + for group_var in group_vars: + df_transformed[group_var] = df[group_var].values + + # Verificación de normalidad individual con Shapiro-Wilk + normality_results = {var: shapiro(X_transformed[:, i]).pvalue for i, var in enumerate(dependent_vars)} + + # Verificación de homogeneidad de varianzas con Fligner-Killeen + df_transformed['combined_factors'] = df_transformed[group_vars].astype(str).agg('-'.join, axis=1) + homogeneity_results = {} + for var in dependent_vars: + grouped_data = [group[var].values for name, group in df_transformed.groupby('combined_factors')] + _, p_value = fligner(*grouped_data) + homogeneity_results[var] = p_value + + # Generación de gráficos de normalidad en dos filas y cinco columnas + fig, axes = plt.subplots(2, 5, figsize=(9, 5)) + + # Etiquetas de estaciones + station_labels = [f'Estación {i}' for i in [1, 5, 10, 15, 20]] + + for i, var in enumerate(dependent_vars): + # Histograma en la primera fila + sns.histplot(df_transformed[var], kde=True, ax=axes[0, i]) + axes[0, i].set_title(f'Histograma de normalidad\n{station_labels[i]}') if i == 2 else axes[0, i].set_title(station_labels[i]) + axes[0, i].set_xlabel('') + axes[0, i].set_ylabel('') if i != 0 else axes[0, i].set_ylabel('Frecuencia') + + # Q-Q plot en la segunda fila + sm.qqplot(df_transformed[var], line='s', ax=axes[1, i]) + #axes[0, i].set_title(station_labels[i]) + axes[1, i].set_title(f'Q-Q Plot de normalidad\n{station_labels[i]}') if i == 2 else axes[1, i].set_title(station_labels[i]) + axes[1, i].set_ylabel('') if i != 0 else axes[1, i].set_ylabel('Cuantiles de la muestra') + axes[1, i].set_xlabel('Cuantiles teóricos') if i==2 else axes[1, i].set_xlabel('') + + plt.tight_layout() + plt.show() + file_name = 'drift' if drift else 'spectra' + xdir = 'x' if xdir else 'y' + fig.savefig(project_path / f'manova_supps_{file_name}_{xdir}.pdf', dpi=100) + # Resultados en un DataFrame + results_df = pd.DataFrame({ + 'Variable': dependent_vars, + 'Normalidad (p-valor)': list(normality_results.values()), + 'Homogeneidad (p-valor)': list(homogeneity_results.values()) + }) + + return results_df + + +def analyze_anova_assumptions(df, analysis_columns, project_path): + """ + Realiza un análisis de los supuestos para ANOVA en las columnas especificadas y guarda los resultados en archivos de imagen. + + Parameters: + df (pd.DataFrame): DataFrame con las columnas 'Sim_Type', 'Nsubs', 'Station', 'Iteration' y columnas de análisis. + analysis_columns (list): Lista de las columnas para las cuales se desea realizar el análisis. + output_file_prefix (str): Prefijo del nombre del archivo para guardar las imágenes de los resultados. + """ + plt.rcParams.update({ + "text.usetex": True, + "font.size": 13, + "font.family": "serif", + "text.latex.preamble": r'\usepackage{amsmath}' + }) + + for col in analysis_columns: + # Agrupar por Sim_Type, Nsubs y Station, y calcular la media de la columna especificada + df_grouped = df.groupby(['Sim_Type', 'Nsubs', 'Station']).agg({col: 'mean'}).reset_index() + + # Aplicar la transformación Box-Cox + df_grouped[f'{col}_boxcox'], fitted_lambda = stats.boxcox(df_grouped[col]) + + # Verificar normalidad usando la prueba de Shapiro-Wilk + shapiro_test = stats.shapiro(df_grouped[f'{col}_boxcox']) + + # Verificar homocedasticidad usando la prueba de Levene + grouped_data = [df_grouped[df_grouped['Sim_Type'] == g][f'{col}_boxcox'].values for g in df_grouped['Sim_Type'].unique()] + levene_test = stats.levene(*grouped_data) + + # Crear la figura y los ejes + fig, ax = plt.subplots(1, 2, figsize=(8, 5)) + + # Histograma para los datos transformados con Box-Cox + sns.histplot(df_grouped[f'{col}_boxcox'], kde=True, ax=ax[0]) + ax[0].set_title(f'Histograma de {col}') + + # Gráfico Q-Q para verificar normalidad + sm.qqplot(df_grouped[f'{col}_boxcox'], line='s', ax=ax[1]) + ax[1].set_title(f'Q-Q Plot de {col}') + + # Ajustar la disposición de la figura + plt.tight_layout() + + # Guardar la figura como imagen + save_path = project_path / f'{col}.pdf' + save_path.parent.mkdir(parents=True, exist_ok=True) # Crear directorio si no existe + fig.savefig(save_path, dpi=100) + plt.close(fig) + + # Mostrar resultados de las pruebas en consola + print(f'fitted_lambda: {fitted_lambda:.3f}') + print(f"Resultados de la Prueba de Shapiro-Wilk para {col} (Normalidad)") + print(f"Estadístico: {shapiro_test.statistic:.3f}, Valor p: {shapiro_test.pvalue:.3f}\n") + + print(f"Resultados de la Prueba de Levene para {col} (Homocedasticidad)") + print(f"Estadístico: {levene_test.statistic:.3f}, Valor p: {levene_test.pvalue:.3f}\n") # ================================================================================== diff --git a/Python Scripts/PySeesTKO/pyseestko/queries.py b/Python Scripts/PySeesTKO/pyseestko/queries.py index 83d6f801..e14a5ee0 100644 --- a/Python Scripts/PySeesTKO/pyseestko/queries.py +++ b/Python Scripts/PySeesTKO/pyseestko/queries.py @@ -8,7 +8,7 @@ from pyseestko.utilities import checkMainQueryInput #type: ignore from pyseestko.utilities import save_df_to_csv_paths #type: ignore from pathlib import Path -from typing import List, Dict, Tuple +from typing import List, Dict, Tuple from tqdm import tqdm from matplotlib import pyplot as plt @@ -22,22 +22,22 @@ git_path = Path('C:/Users/oioya/OneDrive - miuandes.cl/Escritorio/Git-Updated/Thesis-Project-Simulation-Data-Analysis/DataBase-Outputs') def executeMainQuery( # Params - sim_types : List[int], - stations : List[int], - iterations : List[int], - nsubs_lst : List[int], - mag_map : Dict[float, str], - loc_map : Dict[int, str], + sim_types : List[int], + stations : List[int], + iterations : List[int], + nsubs_lst : List[int], + mag_map : Dict[float, str], + loc_map : Dict[int, str], rup_map : Dict[int, str], - # DataBase params - user : str, - password : str, - host : str, + # DataBase params + user : str, + password : str, + host : str, database : str, # Save params - save_drift : bool = True, - save_spectra: bool = True, - save_b_shear: bool = True, + save_drift : bool = True, + save_spectra: bool = True, + save_b_shear: bool = True, save_results: bool = False, # Plot params show_plots : bool = True, @@ -47,10 +47,10 @@ def executeMainQuery( dpi : int = 300, file_type : str = 'png', # Optional params - linearity : int = 1, - stories : int = 20, - magnitude : int = 6.7, - rupture_type: int = 1, + linearity : int = 1, + stories : int = 20, + magnitude : int = 6.7, + rupture_type: int = 1, # Logic params windows : bool = True, project_path: Path = git_path/ 'DataBase-Outputs', @@ -99,26 +99,26 @@ def executeMainQuery( This is mean to be used for statistical analysis, so this is the main fuction to access to the specific data and then analyze it with diverse statistical methods such as ANOVA, POWER ANALYSIS OR MANOVA. """ - + # -------------------------------------- INITIALIZATION ----------------------------------------- # Check the input checkMainQueryInput(sim_types, nsubs_lst, iterations, stations, save_drift, save_spectra, save_b_shear, grid) - + # Iterate over the subs, then over the sim_type and then over the stations so we can get all the results total_iterations = len(sim_types) * len(stations) * len(iterations) * len(nsubs_lst) pbar = tqdm(total=total_iterations, desc='Processing') - + # -------------------------------------- EXECUTE THE MAIN QUERY --------------------------------- # Iterate over the simulation types drifts_df_dict, spectra_df_dict, base_shear_df_dict = queryMetricsInGridPlots( sim_types, stations, iterations, nsubs_lst, mag_map, loc_map, rup_map, project_path, pbar, - user, password, host, database, + user, password, host, database, save_drift, save_spectra, save_b_shear, show_plots, fig_size, xlim_sup, dpi, file_type, - linearity, stories, magnitude, rupture_type, + linearity, stories, magnitude, rupture_type, windows, verbose) pbar.close() print('Done!') - + # -------------------------------------- SAVE THE RESULTS --------------------------------------- save_csv_drift = project_path / 'Analysis Output' / 'CSV' / 'Drift' save_csv_spectra = project_path / 'Analysis Output' / 'CSV' / 'Spectra' @@ -126,38 +126,38 @@ def executeMainQuery( if save_results: save_df_to_csv_paths(drifts_df_dict, spectra_df_dict,base_shear_df_dict, save_csv_drift, save_csv_spectra, save_csv_b_shear) - + return drifts_df_dict, spectra_df_dict, base_shear_df_dict def queryMetricsInSinglePlots( # Params - sim_types : List[int], - stations : List[int], - iterations : List[int], - nsubs_lst : List[int], - mag_map : Dict[float, str], - loc_map : Dict[int, str], + sim_types : List[int], + stations : List[int], + iterations : List[int], + nsubs_lst : List[int], + mag_map : Dict[float, str], + loc_map : Dict[int, str], rup_map : Dict[int, str], project_path: Path, pbar : tqdm, - # DataBase params - user : str, - password : str, - host : str, + # DataBase params + user : str, + password : str, + host : str, database : str, # Plot params - save_drift : bool = True, - save_spectra: bool = True, - save_b_shear: bool = True, + save_drift : bool = True, + save_spectra: bool = True, + save_b_shear: bool = True, show_plots : bool = True, xlim_sup : float = 0.008, dpi : int = 100, file_type : str = 'png', # Sim params - linearity : int = 1, - stories : int = 20, - magnitude : int = 6.7, - rupture_type: int = 1, + linearity : int = 1, + stories : int = 20, + magnitude : int = 6.7, + rupture_type: int = 1, # Optional params windows : bool = True, verbose : bool = True, @@ -191,69 +191,69 @@ def queryMetricsInSinglePlots( rup_map.get(rupture_type, 'None'), iteration, loc_map.get(station, 'None'), stories, nsubs, plotter, windows=windows, verbose=verbose) - + # Get the results for zone = 'Las Condes', soil_category = 'B' and importance = 2 - drift, spectra, base_shear, _ = query.getAllResults(save_drift, - save_spectra, - save_b_shear, + drift, spectra, base_shear, _ = query.getAllResults(save_drift, + save_spectra, + save_b_shear, structure_weight, xlim_sup = xlim_sup, verbose=verbose) - + # Append the results to the dicts sim_type_name = sim_type_map[sim_type] sim_name = f'{sim_type_name}_20f{nsubs}s_rup_bl_{iteration}_s{station}' drifts_df_dict[sim_name] = drift spectra_df_dict[sim_name] = spectra base_shear_df_dict[sim_name] = base_shear - + # Update tqdm pbar.update(1) return drifts_df_dict, spectra_df_dict, base_shear_df_dict def queryMetricsInGridPlots( # Params - sim_types : List[int], - stations : List[int], - iterations : List[int], - nsubs_lst : List[int], - mag_map : Dict[float, str], - loc_map : Dict[int, str], + sim_types : List[int], + stations : List[int], + iterations : List[int], + nsubs_lst : List[int], + mag_map : Dict[float, str], + loc_map : Dict[int, str], rup_map : Dict[int, str], project_path: Path, pbar : tqdm, - # DataBase params - user : str, - password : str, - host : str, + # DataBase params + user : str, + password : str, + host : str, database : str, # Plot params - save_drift : bool = True, - save_spectra: bool = True, - save_b_shear: bool = True, + save_drift : bool = True, + save_spectra: bool = True, + save_b_shear: bool = True, show_plots : bool = True, fig_size : Tuple[float, float] = (19.2, 10.8), xlim_sup : float = 0.008, dpi : int = 100, file_type : str = 'png', # Sim params - linearity : int = 1, - stories : int = 20, - magnitude : int = 6.7, - rupture_type: int = 1, + linearity : int = 1, + stories : int = 20, + magnitude : int = 6.7, + rupture_type: int = 1, # Optional params windows : bool = True, verbose : bool = True, )-> Tuple[Dict[str, pd.DataFrame], Dict[str, pd.DataFrame], Dict[str, pd.DataFrame]]: """ This function will execute the main query to get the results from the database in grid plots - The grid is going to be a 3x3 grid with the drifts, spectra and base shear. + The grid is going to be a 3x3 grid with the drifts, spectra and base shear. """ # Init params sim_type_map = {1: 'FixBase', 2: 'AbsBound', 3: 'DRM'} drifts_df_dict = {} spectra_df_dict = {} base_shear_df_dict = {} - + # Save path params save_drift = project_path / 'Drift Output' if save_drift else None save_spectra = project_path / 'Story Spectra Output' if save_spectra else None @@ -264,7 +264,7 @@ def queryMetricsInGridPlots( for nsubs in nsubs_lst: structure_weight = 22241.3 if nsubs == 4 else 18032.3 Qmax = nch.computeMaxBaseShear_c6_3_7_2(structure_weight) - + # Drift params drift_axes = [np.full((3, 3), None), np.full((3, 3), None)] spectra_axes = [np.full((3, 3), None), np.full((3, 3), None)] @@ -282,38 +282,38 @@ def queryMetricsInGridPlots( station, show_plots=show_plots, grid=True, dpi=dpi, file_type=file_type) query = ProjectQueries(user, password, host, database, sim_type, linearity, mag_map.get(magnitude, 'None'), - rup_map.get(rupture_type, 'None'), + rup_map.get(rupture_type, 'None'), iteration, loc_map.get(station, 'None'), - stories, nsubs, plotter, windows=windows, verbose=verbose) - + stories, nsubs, plotter, windows=windows, verbose=verbose) + # -------------------------------------- EXECUTE THE MAIN QUERY --------------------------------- # Get the results for zone = 'Las Condes', soil_category = 'B' and importance = 2 - drift, spectra, base_shear, axes = query.getAllResults(save_drift, save_spectra, save_b_shear, + drift, spectra, base_shear, axes = query.getAllResults(save_drift, save_spectra, save_b_shear, structure_weight, xlim_sup=xlim_sup, verbose=verbose, drift_axes=drift_axes, spectra_axes=spectra_axes, base_shear_axes=base_shear_axes, save_fig=False, fig_size=fig_size) drift_axes, spectra_axes, base_shear_axes = axes # update the axes - + # Fill dictionaries sim_type_name = sim_type_map[sim_type] sim_name = f'{sim_type_name}_20f{nsubs}s_rup_bl_{iteration}_s{station}' drifts_df_dict[sim_name] = drift spectra_df_dict[sim_name] = spectra base_shear_df_dict[sim_name] = base_shear - + # -------------------------------------- PLOT THE RESULTS --------------------------------- - # Add plot of mean drifts + # Add plot of mean drifts if iteration == iterations[-1]: # Plot mean drift _plotMeanDriftColor(drifts_df_dict, plotter, xlim_sup, drift_axes, save_fig, fig_size) if save_drift else None - + # Plot mean spectra - _plotMeanSpectraColor(spectra_df_dict, plotter, spectra_axes, save_fig, fig_size) if save_spectra else None - + _plotMeanSpectraColor(spectra_df_dict, plotter, spectra_axes, save_fig, fig_size) if save_spectra else None + # Plot mean base shear _plotMeanBaseShearColor(base_shear_df_dict, plotter, base_shear_axes, save_fig, fig_size, Qmax) if save_b_shear else None - + # Update tqdm pbar.update(1) else: @@ -332,21 +332,21 @@ def _plotMeanDriftColor(drifts_df_dict: Dict[str, pd.DataFrame], plotter: Plotti mean_drifts_y = pd.concat([df['CM y'] for df in list(drifts_df_dict.values())[-5:]], axis=1).mean(axis=1).values plotter.setup_direction(x_direction=True) plotter.plotModelDrift([], mean_drifts_x, [], [], - xlim_sup = xlim_sup, - axes = drift_axes[0], - legend = True, + xlim_sup = xlim_sup, + axes = drift_axes[0], + legend = True, save_fig = save_fig, fig_size = fig_size, line_color = 'red') plotter.setup_direction(x_direction=False) plotter.plotModelDrift([], [], [], mean_drifts_y, - xlim_sup = xlim_sup, - axes = drift_axes[1], - legend = True, + xlim_sup = xlim_sup, + axes = drift_axes[1], + legend = True, save_fig = save_fig, fig_size = fig_size, line_color = 'blue') - + def _plotMeanSpectraColor(spectra_df_dict: Dict[str, pd.DataFrame], plotter: Plotting, spectra_axes:plt.Axes, save_fig:bool, fig_size:bool): columns_x = [f'Story {story} x' for story in [1,5,10,15,20]] columns_y = [f'Story {story} y' for story in [1,5,10,15,20]] @@ -356,33 +356,33 @@ def _plotMeanSpectraColor(spectra_df_dict: Dict[str, pd.DataFrame], plotter: Plo plotter.plotMeanStoriesSpectrums(mean_spectras_x, 'x', [1,5,10,15,20], save_fig, spectra_axes[0], fig_size) plotter.setup_direction(x_direction=False) plotter.plotMeanStoriesSpectrums(mean_spectras_y, 'y', [1,5,10,15,20], save_fig, spectra_axes[1], fig_size) - + def _plotMeanBaseShearColor(base_shear_df_dict: Dict[str, pd.DataFrame], plotter: Plotting, base_shear_axes:plt.Axes, save_fig:bool, fig_size:bool, Qmax:float): mean_base_shear_x = pd.concat([df['Shear X'] for df in list(base_shear_df_dict.values())[-5:]], axis=1).mean(axis=1) mean_base_shear_y = pd.concat([df['Shear Y'] for df in list(base_shear_df_dict.values())[-5:]], axis=1).mean(axis=1) plotter.setup_direction(x_direction=True) plotter.plotShearBaseOverTime( - time = mean_base_shear_x.index, - time_shear_fma = mean_base_shear_x.values, - Qmax = Qmax, - dir_ = 'x', - axes = base_shear_axes[0], - save_fig = save_fig, - fig_size = fig_size, + time = mean_base_shear_x.index, + time_shear_fma = mean_base_shear_x.values, + Qmax = Qmax, + dir_ = 'x', + axes = base_shear_axes[0], + save_fig = save_fig, + fig_size = fig_size, leyend = True, mean = True) plotter.setup_direction(x_direction=False) plotter.plotShearBaseOverTime( - time = mean_base_shear_y.index, - time_shear_fma = mean_base_shear_y.values, - Qmax = Qmax, - dir_ = 'y', - axes = base_shear_axes[1], - save_fig = save_fig, - fig_size = fig_size, + time = mean_base_shear_y.index, + time_shear_fma = mean_base_shear_y.values, + Qmax = Qmax, + dir_ = 'y', + axes = base_shear_axes[1], + save_fig = save_fig, + fig_size = fig_size, leyend = True, mean = True) - + def getDriftDFs(drifts_df_lst:List[pd.DataFrame]): """ This function will get the drifts dataframes of the x and y directions @@ -392,12 +392,12 @@ def getDriftDFs(drifts_df_lst:List[pd.DataFrame]): on the maximum drifts of the x and y directions. of the x and y directions. It's supposed to be used after the main query is executed. - + Parameters ---------- drifts_df_lst : List[pd.DataFrame] List of drifts dataframes. - + Returns ------- df1 : pd.DataFrame @@ -423,21 +423,21 @@ def getDriftDFs(drifts_df_lst:List[pd.DataFrame]): # Concatenate X and Y drift_df = pd.concat([df1, df2], axis=0) - + return df1, df2, drift_df def getReplicaCummStatisticDriftDFs(drifts_df_lst:List[pd.DataFrame], statistic:str='mean'): """ This function will get the cummultive mean drifts dataframes of the x and y directions, for each story as index, based on the max value for each replica, given a certain type of simulation, - certain type of structure and certain location. That means, the first column gives the mean given 1 replica, + certain type of structure and certain location. That means, the first column gives the mean given 1 replica, the second column gives the mean given 2 replicas and so on. - + Parameters ---------- drifts_df_lst : List[pd.DataFrame] List of drifts dataframes. - + Returns ------- df1 : pd.DataFrame @@ -450,10 +450,10 @@ def getReplicaCummStatisticDriftDFs(drifts_df_lst:List[pd.DataFrame], statistic: # Check input if statistic not in ['mean', 'std']: raise ValueError(f'Statistic must be mean or std, current: {statistic}') - + # Init params drift_x_df, drift_y_df, drift_df = getDriftDFs(drifts_df_lst) - + #X Direction stories_statistic_lst = [] for i, column in enumerate(drift_x_df.columns): @@ -463,7 +463,7 @@ def getReplicaCummStatisticDriftDFs(drifts_df_lst:List[pd.DataFrame], statistic: stories_statistic_lst.append(drift_x_df[drift_x_df.columns[:i+1]].std(axis=1).loc[[1,5,10,15,20]]) df1 = pd.concat(stories_statistic_lst, axis=1) df1 = df1.droplevel((1,2)) - + #Y Direction stories_statistic_lst = [] for i, column in enumerate(drift_y_df.columns): @@ -473,11 +473,11 @@ def getReplicaCummStatisticDriftDFs(drifts_df_lst:List[pd.DataFrame], statistic: stories_statistic_lst.append(drift_y_df[drift_y_df.columns[:i+1]].std(axis=1).loc[[1,5,10,15,20]]) df2 = pd.concat(stories_statistic_lst, axis=1) df2 = df2.droplevel((1,2)) - + # Concatenate X and Y drift_df = pd.concat([df1, df2], axis=0) - - return df1, df2, drift_df + + return df1, df2, drift_df def DEPgetSpectraDfs(spectra_df_lst:List[pd.DataFrame]): """ @@ -487,12 +487,12 @@ def DEPgetSpectraDfs(spectra_df_lst:List[pd.DataFrame]): It's input is a list of spectra dataframes and it will return the mean spectra dataframes of the x and y directions. It's supposed to be used after the main query is executed. - + Parameters ---------- spectra_df_lst : List[pd.DataFrame] List of spectra dataframes. - + Returns ------- df1 : pd.DataFrame @@ -503,7 +503,7 @@ def DEPgetSpectraDfs(spectra_df_lst:List[pd.DataFrame]): Concatenation of df1 and df2. """ # X Direction - max_spectra_lst_X = [(dfx[0][['Story 1', 'Story 5', 'Story 10', 'Story 15', 'Story 20']].T.set_index(pd.Index([1, 5, 10, 15, 20], name='Story')).max(axis=1)) + max_spectra_lst_X = [(dfx[0][['Story 1', 'Story 5', 'Story 10', 'Story 15', 'Story 20']].T.set_index(pd.Index([1, 5, 10, 15, 20], name='Story')).max(axis=1)) for dfx in spectra_df_lst] df1 = pd.concat(max_spectra_lst_X, axis=1) df1.columns = pd.Index([f'rep_{i+1}' for i in range(len(max_spectra_lst_X))]) @@ -511,7 +511,7 @@ def DEPgetSpectraDfs(spectra_df_lst:List[pd.DataFrame]): df1 = df1.set_index(pd.Index(['x'] * len(df1), name='Dir'), append=True) #Y Direction - max_spectra_lst_Y = [(dfy[0][['Story 1', 'Story 5', 'Story 10', 'Story 15', 'Story 20']].T.set_index(pd.Index([1, 5, 10, 15, 20], name='Story')).max(axis=1)) + max_spectra_lst_Y = [(dfy[0][['Story 1', 'Story 5', 'Story 10', 'Story 15', 'Story 20']].T.set_index(pd.Index([1, 5, 10, 15, 20], name='Story')).max(axis=1)) for dfy in spectra_df_lst] df2 = pd.concat(max_spectra_lst_Y, axis=1) df2.columns = pd.Index([f'rep_{i+1}' for i in range(len(max_spectra_lst_Y))]) @@ -527,7 +527,7 @@ def getCummStatisticSpectraDFs(spectra_df_lst:List[pd.DataFrame], statistic:str= based on the max value for each replica, given a certain type of simulation, certain type of structure and certain location. That means, the first column gives the mean given 1 replica, the second column gives the mean given 2 replicas and so on. - + Parameters ---------- spectra_df_lst : List[pd.DataFrame] @@ -545,10 +545,10 @@ def getCummStatisticSpectraDFs(spectra_df_lst:List[pd.DataFrame], statistic:str= # Check input if statistic not in ['mean', 'std']: raise ValueError(f'Statistic must be mean or std, current: {statistic}') - + # Init params spectra_x_df, spectra_y_df, spectra_df = getSpectraDfs(spectra_df_lst) - + #X Direction stories_statistic_lst = [] for i, column in enumerate(spectra_x_df.columns): @@ -558,7 +558,7 @@ def getCummStatisticSpectraDFs(spectra_df_lst:List[pd.DataFrame], statistic:str= stories_statistic_lst.append(spectra_x_df[spectra_x_df.columns[:i+1]].std(axis=1)) df1 = pd.concat(stories_statistic_lst, axis=1) df1 = df1.droplevel((1,2)) - + #Y Direction stories_statistic_lst = [] for i, column in enumerate(spectra_y_df.columns): @@ -568,17 +568,17 @@ def getCummStatisticSpectraDFs(spectra_df_lst:List[pd.DataFrame], statistic:str= stories_statistic_lst.append(spectra_y_df[spectra_y_df.columns[:i+1]].std(axis=1)) df2 = pd.concat(stories_statistic_lst, axis=1) df2 = df2.droplevel((1,2)) - + # Concatenate X and Y spectra_df = pd.concat([df1, df2], axis=0) - + return df1, df2, spectra_df def getReplicaCummStatisticBaseShearDFs(base_shear_df_lst:List[pd.DataFrame], statistic:str='mean'): # Check input if statistic not in ['mean', 'std']: raise ValueError(f'Statistic must be mean or std, current: {statistic}') - + # Init params spectra_x_lst = [df['Shear X'].max() for df in base_shear_df_lst] spectra_y_lst = [df['Shear Y'].max() for df in base_shear_df_lst] @@ -586,10 +586,10 @@ def getReplicaCummStatisticBaseShearDFs(base_shear_df_lst:List[pd.DataFrame], st # Create a one row DataFrame, with index name equal to "Base Shear" max_shear_x_df = pd.DataFrame([spectra_x_lst], index=['Base Shear'], columns=[f'rep_{i}' for i in range(1,11)]) max_shear_y_df = pd.DataFrame([spectra_y_lst], index=['Base Shear'], columns=[f'rep_{i}' for i in range(1,11)]) - + # Concatenate X and Y max_shear_df = pd.concat([max_shear_x_df, max_shear_y_df], axis=0) - + return max_shear_x_df, max_shear_y_df, max_shear_df @@ -732,14 +732,14 @@ def getAllResults(self, """ This function will execute all the queries to get the results from the database """ - + # Init params start_time = time.time() drift_results_df = None spectra_results_df = None base_shear_results_df = None xlim_sup = 0.0001 - + # =================================================================================================== # ==================================== QUERY THE DRIFT PER FLOOR ==================================== # =================================================================================================== @@ -750,25 +750,25 @@ def getAllResults(self, # Generate drift direction X plot self.plotter.setup_direction(x_direction=True) - drift_axes_x = self.plotter.plotModelDrift(max_corner_x, max_center_x, max_corner_y, max_center_y, - xlim_sup = xlim_sup, - axes = drift_axes[0], - legend = False, + drift_axes_x = self.plotter.plotModelDrift(max_corner_x, max_center_x, max_corner_y, max_center_y, + xlim_sup = xlim_sup, + axes = drift_axes[0], + legend = False, save_fig = save_fig, fig_size = fig_size) - + # Generate drift direction Y plot self.plotter.setup_direction(x_direction=False) - drift_axes_y = self.plotter.plotModelDrift(max_corner_x, max_center_x, max_corner_y, max_center_y, - xlim_sup = xlim_sup, - axes = drift_axes[1], - legend = False, + drift_axes_y = self.plotter.plotModelDrift(max_corner_x, max_center_x, max_corner_y, max_center_y, + xlim_sup = xlim_sup, + axes = drift_axes[1], + legend = False, save_fig = save_fig, fig_size = fig_size) - drift_results_df = pd.DataFrame({'CM x': max_center_x, - 'CM y': max_center_y, - 'Max x': max_corner_x, - 'Max y': max_corner_y}, + drift_results_df = pd.DataFrame({'CM x': max_center_x, + 'CM y': max_center_y, + 'Max x': max_corner_x, + 'Max y': max_corner_y}, index=range(1, len(max_corner_x)+1)).rename_axis('Story') drift_axes = (drift_axes_x, drift_axes_y) @@ -785,15 +785,15 @@ def getAllResults(self, # Generate spectra direction X plot self.plotter.setup_direction(x_direction=True) spectra_axes_x, spa_x_df = self.plotter.plotLocalStoriesSpectrums( - accel_df, story_nodes_df, 'x', stories_lst, + accel_df, story_nodes_df, 'x', stories_lst, axes= spectra_axes[0], save_fig = save_fig, fig_size = fig_size) - + # Generate spectra direction Y plot self.plotter.setup_direction(x_direction=False) spectra_axes_y, spa_y_df = self.plotter.plotLocalStoriesSpectrums( - accel_df, story_nodes_df, 'y', stories_lst, + accel_df, story_nodes_df, 'y', stories_lst, axes = spectra_axes[1], save_fig = save_fig, fig_size = fig_size) @@ -818,10 +818,10 @@ def getAllResults(self, self.plotter.save_path = Path(save_b_shear) self.plotter.setup_direction(x_direction=True) shear_axes_x = self.plotter.plotShearBaseOverTime( - time = time_series, - time_shear_fma = shear_x, - Qmax = Qmax, - dir_ = 'x', + time = time_series, + time_shear_fma = shear_x, + Qmax = Qmax, + dir_ = 'x', axes = base_shear_axes[0], save_fig = save_fig, fig_size = fig_size, @@ -829,18 +829,18 @@ def getAllResults(self, ) self.plotter.setup_direction(x_direction=False) shear_axes_y = self.plotter.plotShearBaseOverTime( - time = time_series, - time_shear_fma = shear_y, - Qmax = Qmax, - dir_ = 'y', + time = time_series, + time_shear_fma = shear_y, + Qmax = Qmax, + dir_ = 'y', axes = base_shear_axes[1], save_fig = save_fig, fig_size = fig_size, mean = False ) - base_shear_results_df = pd.DataFrame({'Shear X': shear_x, - 'Shear Y': shear_y, - 'Shear Z': shear_z}, + base_shear_results_df = pd.DataFrame({'Shear X': shear_x, + 'Shear Y': shear_y, + 'Shear Z': shear_z}, index=time_series).rename_axis('Time Step') base_shear_axes = (shear_axes_x, shear_axes_y) # =================================================================================================== @@ -852,7 +852,7 @@ def getAllResults(self, if verbose: print(f'Elapsed time: {round(end_time - start_time)} seconds.') return drift_results_df, spectra_results_df, base_shear_results_df, axes - +