#!/usr/bin/env python # coding: utf-8 # Notes: # # Les scores de responsabilité varient de -4 à 4 et définissent la propreté du point de vu des agents sanitaires ou des foyers. -4 implique que la slubrité est due aux foyers tandis que 4 implique la salubrité est due aux agents. # # Le score propreté quant à lui décris le niveau de propreté global en faisant une moyenne des scores des deux parties. # In[1]: # ## Generating dummy data # In[2]: import numpy as np import pandas as pd import random import json import plotly.express as px import streamlit as st st.set_page_config(layout="wide") col1, col2 = st.columns(2) # Données de test: Il y a 4 foyers par quartier et 10 quartiers répartis dans 2 communes pour faire les test. # # NB: En nomenclature, communauté est confondue avec région et quartier avec préfecture. # In[3]: DATA = [ {'foyer': 1, 'quartier_id':1, "community_id": 0, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 2, 'quartier_id':1, "community_id": 0, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 3, 'quartier_id':1, "community_id": 0, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 4, 'quartier_id':1, "community_id": 0, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 5, 'quartier_id':2, "community_id": 0, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 4}, {'foyer': 1, 'quartier_id':2, "community_id": 0, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 2, 'quartier_id':2, "community_id": 0, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 3, 'quartier_id':2, "community_id": 0, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 4, 'quartier_id':2, "community_id": 0, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 3, 'score_foyer': 3, 'score': 3}, {'foyer': 1, 'quartier_id':3, "community_id": 0, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 2, 'quartier_id':3, "community_id": 0, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 4, 'score': 4}, {'foyer': 3, 'quartier_id':3, "community_id": 0, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 2, 'score': 3}, {'foyer': 4, 'quartier_id':3, "community_id": 0, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 3, 'score_foyer': 3, 'score': 3}, {'foyer': 1, 'quartier_id':4, "community_id": 0, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 2, 'quartier_id':4, "community_id": 0, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 3, 'quartier_id':4, "community_id": 0, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 4, 'quartier_id':4, "community_id": 0, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 3, 'score_foyer': 3, 'score': 3}, {'foyer': 1, 'quartier_id':5, "community_id": 4, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 2, 'quartier_id':5, "community_id": 4, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 3, 'quartier_id':5, "community_id": 4, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 4, 'quartier_id':5, "community_id": 4, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 5, 'quartier_id':5, "community_id": 4, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 4}, {'foyer': 1, 'quartier_id':6, "community_id": 4, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 2, 'quartier_id':6, "community_id": 4, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 3, 'quartier_id':6, "community_id": 4, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 4, 'quartier_id':6, "community_id": 4, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 3, 'score_foyer': 3, 'score': 3}, {'foyer': 1, 'quartier_id':6, "community_id": 4, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 2, 'quartier_id':6, "community_id": 4, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 4, 'score': 4}, {'foyer': 3, 'quartier_id':6, "community_id": 4, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 2, 'score': 3}, {'foyer': 4, 'quartier_id':6, "community_id": 4, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 3, 'score_foyer': 3, 'score': 3}, {'foyer': 1, 'quartier_id':7, "community_id": 4, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 2, 'quartier_id':7, "community_id": 4, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 3, 'quartier_id':7, "community_id": 4, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 4, 'quartier_id':7, "community_id": 4, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 3, 'score_foyer': 3, 'score': 3}, {'foyer': 1, 'quartier_id':8, "community_id": 9, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 2, 'score_foyer': 3, 'score': 5/2}, {'foyer': 2, 'quartier_id':8, "community_id": 9, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 0, 'score': 2}, {'foyer': 3, 'quartier_id':8, "community_id": 9, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 4, 'quartier_id':8, "community_id": 9, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 5, 'quartier_id':9, "community_id": 9, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 4}, {'foyer': 1, 'quartier_id':9, "community_id": 9, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 2, 'quartier_id':9, "community_id": 9, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 3, 'quartier_id':9, "community_id": 9, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 0, 'score_foyer': 0, 'score': 0}, {'foyer': 4, 'quartier_id':9, "community_id": 9, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 3, 'score_foyer': 3, 'score': 3}, {'foyer': 1, 'quartier_id':10, "community_id": 9, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 2, 'quartier_id':10, "community_id": 9, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 4, 'score': 4}, {'foyer': 3, 'quartier_id':10, "community_id": 9, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 2, 'score': 3}, {'foyer': 4, 'quartier_id':10, "community_id": 9, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 3, 'score_foyer': 3, 'score': 3}, {'foyer': 1, 'quartier_id':10, "community_id": 9, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 0, 'score_foyer': 0, 'score': 0}, {'foyer': 2, 'quartier_id':11, "community_id": 9, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 3, 'quartier_id':11, "community_id": 9, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 1, 'score_foyer': 1, 'score': 1}, {'foyer': 4, 'quartier_id':11, "community_id": 9, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 3, 'score_foyer': 3, 'score': 3}, {'foyer': 1, 'quartier_id':16, "community_id": 5, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 2, 'quartier_id':16, "community_id": 5, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 3, 'quartier_id':16, "community_id": 5, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 4, 'quartier_id':16, "community_id": 5, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 5, 'quartier_id':21, "community_id": 5, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 4}, {'foyer': 1, 'quartier_id':21, "community_id": 5, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 2, 'quartier_id':21, "community_id": 5, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 3, 'quartier_id':21, "community_id": 5, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 4, 'quartier_id':21, "community_id": 5, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 3, 'score_foyer': 3, 'score': 3}, {'foyer': 1, 'quartier_id':31, "community_id": 5, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 2, 'quartier_id':31, "community_id": 5, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 3, 'score_foyer': 4, 'score': 4}, {'foyer': 3, 'quartier_id':31, "community_id": 5, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 2, 'score': 3}, {'foyer': 4, 'quartier_id':31, "community_id": 5, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 3, 'score_foyer': 3, 'score': 3}, {'foyer': 1, 'quartier_id':24, "community_id": 5, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 2, 'quartier_id':24, "community_id": 5, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 3, 'quartier_id':24, "community_id": 5, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 4, 'quartier_id':24, "community_id": 5, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 3, 'score_foyer': 3, 'score': 3}, {'foyer': 1, 'quartier_id':17, "community_id": 6, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 1, 'score_foyer': 3, 'score': 1}, {'foyer': 2, 'quartier_id':17, "community_id": 6, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 3, 'quartier_id':17, "community_id": 6, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 4, 'quartier_id':17, "community_id": 6, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 5, 'quartier_id':25, "community_id": 6, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 4}, {'foyer': 1, 'quartier_id':25, "community_id": 6, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 1, 'score_foyer': 5, 'score': 5}, {'foyer': 2, 'quartier_id':25, "community_id": 6, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 3, 'quartier_id':25, "community_id": 6, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 4, 'quartier_id':25, "community_id": 6, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 3, 'score': 3}, {'foyer': 1, 'quartier_id':32, "community_id": 6, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 2, 'quartier_id':32, "community_id": 6, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 4, 'score': 4}, {'foyer': 3, 'quartier_id':32, "community_id": 6, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 2, 'score': 3}, {'foyer': 4, 'quartier_id':32, "community_id": 6, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 3, 'score_foyer': 3, 'score': 3}, {'foyer': 1, 'quartier_id':23, "community_id": 6, 'nom': 'Foyer de Di', "mois": 1, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 2, 'quartier_id':23, "community_id": 6, 'nom': 'Foyer de Di', "mois": 2, 'annee':2000, 'score_sanitaire': 5, 'score_foyer': 5, 'score': 5}, {'foyer': 3, 'quartier_id':24, "community_id": 6, 'nom': 'Foyer de Di', "mois": 3, 'annee':2000, 'score_sanitaire': 4, 'score_foyer': 3, 'score': 1}, {'foyer': 4, 'quartier_id':25, "community_id": 6, 'nom': 'Foyer de Di', "mois": 4, 'annee':2000, 'score_sanitaire': 2, 'score_foyer': 3, 'score': 3}, ] # In[4]: data = pd.DataFrame(DATA) #data.head() # In[5]: data['score'] = (data['score_sanitaire'] + data['score_foyer']) / 2 #data.head(2) # In[6]: data['score responsabilité'] = data['score_sanitaire'] - data['score_foyer'] # In[7]: #data.head() # In[8]: #np.average(data['score'], axis=0, weights=data.index) # In[9]: def moyenne_par_quartier(quartiers, id, scoring="score"): quartier = quartiers[quartiers.quartier_id == id] return quartier[scoring].mean() # In[10]: #moyenne_par_quartier(data, 2) # Moyenne pondérée pour les quartiers qui ont peu de foyers enrégistrés dans une communauté. # In[11]: def moyenne_par_communaute(data, community_id, scoring="score"): community = data[data.community_id == community_id] avg = np.average(community[scoring], axis=0, weights=community.index) return avg # In[12]: #moyenne_par_communaute(data, 4) # In[13]: def moyenne_par_mois_par_communaute(data, community_id, month, scoring="score"): filtered = data[(data.community_id == community_id) & (data.mois == month)] avg = np.average(filtered[scoring], axis=0, weights=filtered.index) return avg # In[14]: def moyenne_par_mois_par_quartier(data, quartier_id, month, scoring="score"): filtered = data[(data.quartier_id == quartier_id) & (data.mois == month)] avg = np.average(filtered[scoring], axis=0, weights=filtered.index) return avg # In[15]: def moyenne_par_annee_par_communaute(data, community_id, year, scoring="score"): filtered = data[(data.community_id == community_id) & (data.annee == year)] avg = np.average(filtered[scoring], axis=0, weights=filtered.index) return avg # In[16]: def moyenne_par_annee_par_quartier(data, quartier_id, year, scoring="score"): filtered = data[(data.quartier_id == quartier_id) & (data.mois == year)] avg = np.average(filtered[scoring], axis=0, weights=filtered.index) return avg # ##Plot Map # In[17]: # In[18]: import geopandas as gpd import folium # Specify the path to your GeoJSON file geojson_file_path = 'BNDA_TGO_2017-06-29_lastupdate.geojson' geojson_data = json.load(open(geojson_file_path, "r")) # Read the GeoJSON file using geopandas gdf = gpd.read_file(geojson_file_path) # On définit ici quelques outils pour faire la correspondance id vers quartier et communauté (vice-versa). # In[19]: id_quartier = {} for row in gdf.iterrows(): id_quartier[row[0]] = row[1][4] #id_quartier # In[20]: quartier_id = {} for row in gdf.iterrows(): quartier_id[row[1][4]] = row[0] # In[21]: id_regions = {} for row in gdf.iterrows(): if row[1][3] not in id_regions.values(): id_regions[row[0]] = row[1][3] # In[22]: #id_regions # In[23]: data['quartier_name'] = data['quartier_id'].apply(lambda x: id_quartier[x]) data['community_name'] = data['community_id'].apply(lambda x: id_regions[x]) #data.head() # In[ ]: # In[25]: quartiers = data['quartier_name'].unique().tolist() # In[26]: qm = {} for q in quartiers: qm[q] = moyenne_par_quartier(data, quartier_id[q]) #qm # In[27]: ids = [quartier_id[q] for q in quartiers] # In[28]: quartiers = list(qm.keys()) # In[29]: scores = list(qm.values()) # #Scores de propreté - Par quartiers (préfectures) # In[30]: new_df = pd.DataFrame(data={ 'quartier': quartiers, 'scores': scores, "quartier_id": ids }) #new_df.head() # In[31]: #new_df.to_csv('new_df.csv', index=False) # In[39]: qs = new_df['quartier'].tolist() new_gdf_q = gdf[gdf.adm2nm.isin(qs)] # In[59]: gdf_merged_q = pd.merge(new_gdf_q, new_df, how='left', left_on="adm2nm", right_on="quartier") # In[60]: #gdf_merged_q.head() # In[61]: geojson = gdf_merged_q.__geo_interface__ # In[75]: # # Map Scores de propreté pour les Préfectures du Togo # Note: la carte est centrée. Il faut zoomer en arrière pour avoir le rendu. # In[86]: st.header('Score Propre') fig = px.choropleth_mapbox(gdf_merged_q, geojson=geojson, locations=gdf_merged_q.index, color='scores', mapbox_style="carto-positron", title="Scores de Propreté Pour Les Préfectures Du Togo", hover_name="adm2nm", color_continuous_scale="Viridis" ) fig.update_layout(margin={'r':0, 't':0, "l": 0, 'r': 0}) st.plotly_chart(fig) # In[101]: #gdf_merged_q.to_csv('merged_q.csv', index=False)