File size: 18,479 Bytes
0edf40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8470e60
0edf40d
8470e60
16ea8f5
0edf40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16ea8f5
0edf40d
 
 
 
 
 
16ea8f5
0edf40d
 
 
 
 
1c78e6d
0edf40d
 
 
 
 
16ea8f5
0edf40d
 
 
 
 
16ea8f5
0edf40d
 
 
 
 
 
 
 
 
 
 
 
 
16ea8f5
0edf40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16ea8f5
0edf40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16ea8f5
0edf40d
 
 
 
 
 
 
16ea8f5
0edf40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16ea8f5
0edf40d
 
 
 
 
16ea8f5
0edf40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16ea8f5
0edf40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f54d021
16ea8f5
f54d021
16ea8f5
 
 
 
 
 
 
 
f54d021
 
0edf40d
 
 
16ea8f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/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)