-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdash_test_month.py
436 lines (384 loc) · 15.7 KB
/
dash_test_month.py
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import pandas as pd
import plotly.express as px # (version 4.7.0 or higher)
import plotly.graph_objects as go
from dash import Dash, dcc, html, Input, Output # pip install dash (version 2.0.0 or higher)
import calendar
app = Dash(__name__)
# -- Import and clean data (importing csv into pandas)
# df = pd.read_csv("intro_bees.csv")
#df = pd.read_csv("https://raw.githubusercontent.com/Coding-with-Adam/Dash-by-Plotly/master/Other/Dash_Introduction/intro_bees.csv")
# #prepare dataframe
# MyColumns = ['State','Year','Week','Cases_per_week','Cum_Cases','SevereCases_per_week','Cum_SevereCases']
# ## load 2014 data
# Path = r'C:\Users\chateaux.m\Documents\DESU data science\Projet\Data\USA\2014.csv'
# dataframe_USA_2014 = pd.read_csv(Path)
# dataframe_USA_2014 = dataframe_USA_2014[['Reporting Area', 'MMWRYear','MMWRWeek','Dengue Fever†, Current week', 'Dengue Fever†, Cum 2014','Dengue Hemorrhagic Fever§, Current week', 'Dengue Hemorrhagic Fever§, Cum 2014']]
# dataframe_USA_2014.columns = MyColumns
# #dataframe_USA_2014
# ## 2015
# Path = r'C:\Users\chateaux.m\Documents\DESU data science\Projet\Data\USA\2015.csv'
# dataframe_USA_2015 = pd.read_csv(Path)
# #dataframe_USA_2015.columns
# dataframe_USA_2015 = dataframe_USA_2015[['Reporting Area', 'MMWRYear','MMWRWeek','Dengue§, Current week', 'Dengue§, Cum 2015','Dengue Severe, Current week', 'Dengue Severe, Cum 2015']]
# dataframe_USA_2015.columns = MyColumns
# #dataframe_USA_2015
# ## 2016
# Path = r'C:\Users\chateaux.m\Documents\DESU data science\Projet\Data\USA\2016.csv'
# dataframe_USA_2016 = pd.read_csv(Path)
# #dataframe_USA_2016.columns
# dataframe_USA_2016 = dataframe_USA_2016[['Reporting Area', 'MMWR Year','MMWR Week','Dengue§, Current week', 'Dengue§, Cum 2016','Dengue Severe, Current week', 'Dengue Severe, Cum 2016']]
# dataframe_USA_2016.columns = MyColumns
# #dataframe_USA_2016
# ## 2017
# Path = r'C:\Users\chateaux.m\Documents\DESU data science\Projet\Data\USA\2017.csv'
# dataframe_USA_2017 = pd.read_csv(Path)
# #dataframe_USA_2017.columns
# dataframe_USA_2017 = dataframe_USA_2017[['Reporting Area', 'MMWRYear','MMWRWeek','Dengue Virus Infection, Dengue§, Current week', 'Dengue Virus Infection, Dengue§, Cum 2017','Dengue Virus Infection, Severe Dengue, Current week', 'Dengue Virus Infection, Severe Dengue, Cum 2017']]
# dataframe_USA_2017.columns = MyColumns
# #dataframe_USA_2017
# ## 2018
# Path = r'C:\Users\chateaux.m\Documents\DESU data science\Projet\Data\USA\2018.csv'
# dataframe_USA_2018 = pd.read_csv(Path)
# #dataframe_USA_2018.columns
# dataframe_USA_2018 = dataframe_USA_2018[['Reporting Area', 'MMWRYear','MMWRWeek','Dengue Virus Infections, Dengue†, Current week', 'Dengue Virus Infections, Dengue†, Cum 2018','Dengue Virus Infections, Severe Dengue, Current week', 'Dengue Virus Infections, Severe Dengue, Cum 2018']]
# dataframe_USA_2018.columns = MyColumns
# #dataframe_USA_2018
# ## 2019
# Path = r'C:\Users\chateaux.m\Documents\DESU data science\Projet\Data\USA\2019.csv'
# dataframe_USA_2019 = pd.read_csv(Path)
# #dataframe_USA_2019.columns
# dataframe_USA_2019 = dataframe_USA_2019[['Reporting Area', 'MMWR Year','MMWR Week','Dengue virus infections§ , Dengue, Current week', 'Dengue virus infections§ , Dengue, Cum 2019†','Dengue virus infections§ , Severe dengue, Current week', 'Dengue virus infections§ , Severe dengue, Cum 2019†']]
# dataframe_USA_2019.columns = MyColumns
# #dataframe_USA_2019
# ## 2020
# Path = r'C:\Users\chateaux.m\Documents\DESU data science\Projet\Data\USA\2020.csv'
# dataframe_USA_2020 = pd.read_csv(Path)
# #dataframe_USA_2020.columns
# dataframe_USA_2020 = dataframe_USA_2020[['Reporting Area', 'MMWR Year','MMWR Week','Dengue virus infections, Dengue, Current week', 'Dengue virus infections, Dengue, Cum 2020†','Dengue virus infections, Severe dengue, Current week', 'Dengue virus infections, Severe dengue, Cum 2020†']]
# dataframe_USA_2020.columns = MyColumns
# #dataframe_USA_2020
# ## 2021
# Path = r'C:\Users\chateaux.m\Documents\DESU data science\Projet\Data\USA\2021.csv'
# dataframe_USA_2021 = pd.read_csv(Path)
# #dataframe_USA_2021.columns
# dataframe_USA_2021 = dataframe_USA_2021[['Reporting Area', 'MMWR Year','MMWR Week','Dengue virus infections, Dengue, Current week', 'Dengue virus infections, Dengue, Cum 2021†','Dengue virus infections, Severe dengue, Current week', 'Dengue virus infections, Severe dengue, Cum 2021†']]
# dataframe_USA_2021.columns = MyColumns
# #dataframe_USA_2021
# ## 2022
# #Path = r'C:\Users\chateaux.m\Documents\DESU data science\Projet\Data\USA\2022.csv'
# #dataframe_USA_2022 = pd.read_csv(Path)
# #dataframe_USA_2022.columns
# #dataframe_USA_2022 = dataframe_USA_2022[['Reporting Area', 'MMWR Year','MMWR Week','Dengue virus infections, Dengue, Current week', 'Dengue virus infections, Dengue, Cum 2022†','Dengue virus infections, Severe dengue, Current week', 'Dengue virus infections, Severe dengue, Cum 2022†']]
# #dataframe_USA_2022.columns = MyColumns
# #dataframe_USA_2022
# dataframe_USA = pd.concat([dataframe_USA_2014,dataframe_USA_2015,dataframe_USA_2016,dataframe_USA_2017,dataframe_USA_2018,dataframe_USA_2019,dataframe_USA_2020,dataframe_USA_2021], axis = 0)
# #remplacer les NaNs par des 0
# dataframe_USA_filled = dataframe_USA.fillna(0)
# dataframe_USA_filled
# #keep states informations only
# us_states = [
# "ALABAMA", "ARIZONA", "ARKANSAS", "CALIFORNIA",
# "COLORADO", "CONNECTICUT", "DELAWARE", "FLORIDA", "GEORGIA",
# "IDAHO", "ILLINOIS", "INDIANA", "IOWA",
# "KANSAS", "KENTUCKY", "LOUISIANA", "MAINE", "MARYLAND",
# "MASSACHUSETTS", "MICHIGAN", "MINNESOTA", "MISSISSIPPI", "MISSOURI",
# "MONTANA", "NEBRASKA", "NEVADA", "NEW HAMPSHIRE", "NEW JERSEY",
# "NEW MEXICO", "NEW YORK", "NORTH CAROLINA", "NORTH DAKOTA", "OHIO",
# "OKLAHOMA", "OREGON", "PENNSYLVANIA", "RHODE ISLAND", "SOUTH CAROLINA",
# "SOUTH DAKOTA", "TENNESSEE", "TEXAS", "UTAH", "VERMONT", "VIRGINIA",
# "WASHINGTON", "WEST VIRGINIA", "WISCONSIN", "WYOMING",
# "DISTRICT OF COLUMBIA"
# ]
# filtered_df = dataframe_USA_filled[dataframe_USA_filled['State'].isin(us_states)]
# #filtered_df.dtypes
# Year_dengue = filtered_df.groupby(['State', 'Year']).apply(lambda group: group[group['Week'] == group['Week'].max()])
# Year_dengue.reset_index(drop = True, inplace=True)
# #filtered2_df.columns[4] = 'Cases_per_year'
# #Year_dengue.drop(['Week','Cases_per_week','SevereCases_per_week'],axis = 1,inplace= True)
# Year_dengue.drop(['Week','Cases_per_week','SevereCases_per_week','Cum_SevereCases'],axis = 1,inplace= True)
# #Year_dengue.columns = ['State','Year','Cases','Severe_Cases']
# Year_dengue.columns = ['State','Year','Cases']
# #Year_dengue #penser à enlever 2022 car seulement 5 semaines prises en compte donc on ne peut pas dire qu'à la fin de la 5eme semaine on a les cas sur l'année
# #ajouter le code de l'état pour la représentation graphique
# # Dictionnaire de correspondance des noms d'États avec les codes
# state_codes = {
# "ALABAMA":"AL",
# "ARIZONA":"AZ",
# "ARKANSAS":"AR",
# "CALIFORNIA":"CA",
# "COLORADO":"CO",
# "CONNECTICUT":"CT",
# "DELAWARE":"DE",
# "FLORIDA":"FL",
# "GEORGIA":"GA",
# "IDAHO":"ID",
# "ILLINOIS":"IL",
# "INDIANA":"IN",
# "IOWA":"IA",
# "KANSAS":"KS",
# "KENTUCKY":"KY",
# "LOUISIANA":"LA",
# "MAINE":"ME",
# "MARYLAND":"MD",
# "MASSACHUSETTS":"MA",
# "MICHIGAN":"MI",
# "MINNESOTA":"MN",
# "MISSISSIPPI":"MS",
# "MISSOURI":"MO",
# "MONTANA":"MT",
# "NEBRASKA":"NE",
# "NEVADA":"NV",
# "NEW HAMPSHIRE":"NH",
# "NEW JERSEY":"NJ",
# "NEW MEXICO":"NM",
# "NEW YORK":"NY",
# "NORTH CAROLINA":"NC",
# "NORTH DAKOTA":"ND",
# "OHIO":"OH",
# "OKLAHOMA":"OK",
# "OREGON":"OR",
# "PENNSYLVANIA":"PA",
# "RHODE ISLAND":"RI",
# "SOUTH CAROLINA":"SC",
# "SOUTH DAKOTA":"SD",
# "TENNESSEE":"TN",
# "TEXAS":"TX",
# "UTAH":"UT",
# "VERMONT":"VT",
# "VIRGINIA":"VA",
# "WASHINGTON":"WA",
# "WEST VIRGINIA":"WV",
# "WISCONSIN":"WI",
# "WYOMING":"WY",
# "DISTRICT OF COLUMBIA":"DC"
# }
# # Ajouter une nouvelle colonne contenant les codes d'États correspondants
# Year_dengue['state_code'] = Year_dengue['State'].map(state_codes)
#Load previously save file instead of preparing it :
USA = pd.read_csv(r'C:\Users\chateaux.m\Documents\DESU_DS\Projet\Data\USA\USA_with_pred.csv')
#ajouter le code de l'état pour la représentation graphique
# Dictionnaire de correspondance des noms d'États avec les codes
state_codes = {
"ALABAMA":"AL",
"ALASKA":"AK",
"ARIZONA":"AZ",
"ARKANSAS":"AR",
"CALIFORNIA":"CA",
"COLORADO":"CO",
"CONNECTICUT":"CT",
"DELAWARE":"DE",
"FLORIDA":"FL",
"GEORGIA":"GA",
"HAWAII":"HI",
"IDAHO":"ID",
"ILLINOIS":"IL",
"INDIANA":"IN",
"IOWA":"IA",
"KANSAS":"KS",
"KENTUCKY":"KY",
"LOUISIANA":"LA",
"MAINE":"ME",
"MARYLAND":"MD",
"MASSACHUSETTS":"MA",
"MICHIGAN":"MI",
"MINNESOTA":"MN",
"MISSISSIPPI":"MS",
"MISSOURI":"MO",
"MONTANA":"MT",
"NEBRASKA":"NE",
"NEVADA":"NV",
"NEW HAMPSHIRE":"NH",
"NEW JERSEY":"NJ",
"NEW MEXICO":"NM",
"NEW YORK":"NY",
"NORTH CAROLINA":"NC",
"NORTH DAKOTA":"ND",
"OHIO":"OH",
"OKLAHOMA":"OK",
"OREGON":"OR",
"PENNSYLVANIA":"PA",
"RHODE ISLAND":"RI",
"SOUTH CAROLINA":"SC",
"SOUTH DAKOTA":"SD",
"TENNESSEE":"TN",
"TEXAS":"TX",
"UTAH":"UT",
"VERMONT":"VT",
"VIRGINIA":"VA",
"WASHINGTON":"WA",
"WEST VIRGINIA":"WV",
"WISCONSIN":"WI",
"WYOMING":"WY",
"DISTRICT OF COLUMBIA":"DC"
}
# Ajouter une nouvelle colonne contenant les codes d'États correspondants
USA['state_code'] = USA['State'].map(state_codes)
# print(np.sum(pd.isna(USA['state_code'])))
# Ajouter une nouvelle colonne contenant les codes ANSI correspondants
# state_ANSI = {
# "ALABAMA":"01",
# "ARIZONA":"04",
# "ARKANSAS":"05",
# "CALIFORNIA":"06",
# "COLORADO":"08",
# "CONNECTICUT":"09",
# "DELAWARE":"10",
# "FLORIDA":"12",
# "GEORGIA":"13",
# "IDAHO":"16",
# "ILLINOIS":"17",
# "INDIANA":"18",
# "IOWA":"19",
# "KANSAS":"20",
# "KENTUCKY":"21",
# "LOUISIANA":"22",
# "MAINE":"23",
# "MARYLAND":"24",
# "MASSACHUSETTS":"25",
# "MICHIGAN":"26",
# "MINNESOTA":"27",
# "MISSISSIPPI":"28",
# "MISSOURI":"29",
# "MONTANA":"30",
# "NEBRASKA":"31",
# "NEVADA":"32",
# "NEW HAMPSHIRE":"33",
# "NEW JERSEY":"34",
# "NEW MEXICO":"35",
# "NEW YORK":"36",
# "NORTH CAROLINA":"37",
# "NORTH DAKOTA":"38",
# "OHIO":"39",
# "OKLAHOMA":"40",
# "OREGON":"41",
# "PENNSYLVANIA":"42",
# "RHODE ISLAND":"44",
# "SOUTH CAROLINA":"45",
# "SOUTH DAKOTA":"46",
# "TENNESSEE":"47",
# "TEXAS":"48",
# "UTAH":"49",
# "VERMONT":"50",
# "VIRGINIA":"51",
# "WASHINGTON":"53",
# "WEST VIRGINIA":"54",
# "WISCONSIN":"55",
# "WYOMING":"56",
# "DISTRICT OF COLUMBIA":"11"
# }
# Year_dengue['ANSI'] = Year_dengue['State'].map(state_ANSI)
# USA['Normalized_cases'] = USA['Cases_per_month']/USA['Demographic']
# global_min = USA['Normalized_cases'].min()
# global_max = USA['Normalized_cases'].max()
global_min = USA['Cases_per_month'].min()
global_max = USA['Cases_per_month'].max()
month_names = list(calendar.month_name)[1:]
# Year_dengue_grouped = Year_dengue.groupby(['State', 'ANSI', 'Year','state_code']).sum()
# Year_dengue_grouped.reset_index(drop = False, inplace=True)
#print(Year_dengue_grouped[:1])
# ------------------------------------------------------------------------------
# App layout
app.layout = html.Div([
html.H1("Web Application Dashboards with Dash", style={'text-align': 'center'}),
# dcc.Dropdown(id="slct_year",
# options=[
# {"label": "2014", "value": 2014},
# {"label": "2015", "value": 2015},
# {"label": "2016", "value": 2016},
# {"label": "2017", "value": 2017},
# {"label": "2018", "value": 2018},
# {"label": "2019", "value": 2019},
# {"label": "2020", "value": 2020},
# {"label": "2021", "value": 2021}],
# multi=False,
# value=2014,
# style={'width': "40%"}
# ),
dcc.Slider(
id='slct_year',
min=USA['Year'].min(),
max=USA['Year'].max(),
step=1,
marks={str(year): str(year) for year in range(USA['Year'].min(), USA['Year'].max() + 1)},
value=2014, # Initial value (you can change this to a default year)
),
dcc.Slider(
id='slct_month',
min=USA['Month'].min(),
max=USA['Month'].max(),
step=1,
# marks={str(month): str(month) for month in range(USA['Month'].min(), USA['Month'].max() + 1)},
marks = {i: month_names[i - 1] for i in range(1, 13)},
value=1, # Initial value (you can change this to a default month)
),
# html.Div([
# dcc.Slider(
# id='slct_year',
# min=USA['Year'].min(),
# max=USA['Year'].max(),
# step=1,
# marks={str(year): str(year) for year in range(USA['Year'].min(), USA['Year'].max() + 1)},
# value=2014, # Initial value (you can change this to a default year)
# ),
# dcc.Slider(id='slct_month',
# min=1,
# max=12,
# step=1,
# marks={i: str(i) for i in range(1, 13)},
# value=1, # Initial value (you can change this to a default month)
# ),
# ], style={'display': 'flex', 'justify-content': 'space-between'}),
html.Div(id='output_container', children=[]),
html.Br(),
dcc.Graph(id='my_dengue_map', figure={})
])
# ------------------------------------------------------------------------------
# Connect the Plotly graphs with Dash Components
@app.callback(
[Output(component_id='output_container', component_property='children'),
Output(component_id='my_dengue_map', component_property='figure')],
[Input(component_id='slct_year', component_property='value'),
Input(component_id='slct_month', component_property='value')]
)
def update_graph(option_slctd,month_slctd):
print(option_slctd)
print(type(option_slctd))
container = "The year chosen by user was: {}".format(option_slctd)
# dff = Year_dengue_grouped.copy()
dff = USA.copy()
dff = dff[(dff["Year"] == option_slctd) & (dff["Month"] == month_slctd)]
#dff = dff[dff["Affected by"] == "Varroa_mites"]
# Plotly Express
fig = px.choropleth(
data_frame=dff,
locationmode='USA-states',
locations='state_code',
scope="usa",
color='Cases_per_month',
range_color=[0, 20],
hover_data=['State', 'Cases_per_month'],
color_continuous_scale=px.colors.sequential.YlOrRd,
labels={'Cases_per_month': 'Number of cases'},
template='plotly_dark'
)
# Plotly Graph Objects (GO)
# fig = go.Figure(
# data=[go.Choropleth(
# locationmode='USA-states',
# locations=dff['state_code'],
# z=dff["Cases"].astype(float),
# colorscale='Reds',
# )]
# )
#
# fig.update_layout(
# title_text="Bees Affected by Mites in the USA",
# title_xanchor="center",
# title_font=dict(size=24),
# title_x=0.5,
# geo=dict(scope='usa'),
# )
return container, fig
# ------------------------------------------------------------------------------
if __name__ == '__main__':
app.run_server(debug=True)