Spaces:
Runtime error
Runtime error
| from shiny import ui, reactive, render | |
| from data_loader import load_data, apply_mappings, MAIN_DATA_URL # Import de MAIN_DATA_URL | |
| import pandas as pd | |
| import seaborn as sns | |
| import matplotlib.pyplot as plt | |
| from scipy.stats import chi2_contingency | |
| # Charger les données | |
| df = load_data(MAIN_DATA_URL) | |
| df = apply_mappings(df) | |
| # Interface utilisateur pour l'analyse statistique | |
| def statistical_analysis_ui(): | |
| return ui.page_fluid( | |
| ui.h1("Statistical Analysis"), | |
| ui.layout_sidebar( | |
| sidebar=ui.panel_sidebar( | |
| ui.input_select("product_categories", "Product Categories", choices=sorted(df['prodcat'].dropna().unique()), multiple=True), | |
| ui.input_select("hazard_categories", "Hazard Categories", choices=sorted(df['hazcat'].dropna().unique()), multiple=True), | |
| ui.input_select("notifying_countries", "Notifying Countries", choices=sorted(df['notification_from'].dropna().unique()), multiple=True), | |
| ), | |
| main=ui.panel_main( | |
| ui.output_text("chi2_result"), | |
| ui.output_plot("heatmap"), | |
| ui.output_plot("top_associations") | |
| ) | |
| ) | |
| ) | |
| # Serveur pour l'analyse statistique | |
| def statistical_analysis_server(input, output, session): | |
| def filtered_data(): | |
| # Appliquer les filtres | |
| filtered_df = df.copy() | |
| if input.product_categories(): | |
| filtered_df = filtered_df[filtered_df['prodcat'].isin(input.product_categories())] | |
| if input.hazard_categories(): | |
| filtered_df = filtered_df[filtered_df['hazcat'].isin(input.hazard_categories())] | |
| if input.notifying_countries(): | |
| filtered_df = filtered_df[filtered_df['notification_from'].isin(input.notifying_countries())] | |
| return filtered_df | |
| def chi2_result(): | |
| # Test du Chi2 | |
| contingency_table = pd.crosstab(filtered_data()['prodcat'], filtered_data()['hazcat']) | |
| chi2_stat, p_value, dof, expected = chi2_contingency(contingency_table) | |
| return f"Chi2 Statistic: {chi2_stat:.2f}, P-value: {p_value:.4f}" | |
| def heatmap(): | |
| # Heatmap | |
| contingency_table = pd.crosstab(filtered_data()['prodcat'], filtered_data()['hazcat']) | |
| fig, ax = plt.subplots(figsize=(10, 8)) | |
| sns.heatmap(contingency_table, annot=True, fmt="d", cmap="coolwarm", ax=ax) | |
| return fig | |
| def top_associations(): | |
| # Top associations | |
| contingency_table = pd.crosstab(filtered_data()['prodcat'], filtered_data()['hazcat']) | |
| top_associations = contingency_table.stack().reset_index(name='count').sort_values(by='count', ascending=False).head(10) | |
| fig, ax = plt.subplots(figsize=(10, 6)) | |
| sns.barplot(data=top_associations, x='prodcat', y='count', hue='hazcat', ax=ax) | |
| return fig | |