Py.Cafe

marie-anne/

2025-figurefriday-w20

Drilldown US dams and Emergency Action Plans

DocsPricing
  • app.py
  • nation-dams.csv
  • requirements.txt
  • uitleg.odt
app.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
# -*- coding: utf-8 -*-
"""
Created on Fri May 16 21:01:26 2025

@author: Marie-Anne Melis
"""

import pandas as pd
import dash
from dash import dcc, html
import plotly.graph_objects as go
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
from dash_bootstrap_templates import load_figure_template
from plotly.subplots import make_subplots


colors_class = ['rgba(184,0,127,.2)', 
                'rgba(184,0,127,.4)',
                'rgba(184,0,127,.7)',
                'rgba(184,0,127,1)']


#df_raw = pd.read_csv('nation.csv')
#df = df_raw[['Federal ID','Hazard Potential Classification','Primary Owner Type','Primary Purpose', 'EAP Prepared']].reset_index()
#df.to_csv('nation-dams.csv')
df=pd.read_csv('nation-dams.csv')


# stylesheet with the .dbc class to style  dcc, DataTable and AG Grid components with a Bootstrap theme
dbc_css = "https://cdn.jsdelivr.net/gh/AnnMarieW/dash-bootstrap-templates/dbc.min.css"
# if using the vizro theme
vizro_bootstrap = "https://cdn.jsdelivr.net/gh/mckinsey/vizro@main/vizro-core/src/vizro/static/css/vizro-bootstrap.min.css?v=2"

# default dark mode
#pio.templates.default = "vizro_dark"
load_figure_template(["vizro", "vizro_dark"])


#subplot reference https://fronkan.hashnode.dev/different-barmodes-simultaneously-in-plotly-subplots-python


#df6 == PURPOSE

dff6 = df.groupby(['Hazard Potential Classification','Primary Purpose', 'EAP Prepared'])['Federal ID'].count().reset_index()
dff6.rename(columns={"Federal ID": "Number"}, inplace=True)
# Calculate percentage within each hazard group
dff6["Percentage Hazard"] = round(dff6["Number"] / dff6.groupby("Hazard Potential Classification")["Number"].transform("sum") * 100,1)
dff6["Percentage Hazard Purpose"] = round(dff6["Number"] / dff6.groupby(["Hazard Potential Classification","Primary Purpose"])["Number"].transform("sum") * 100,1)

#df2 = OWNER

dff2 = df.groupby(['Hazard Potential Classification','Primary Owner Type', 'EAP Prepared'])['Federal ID'].count().reset_index()
dff2.rename(columns={"Federal ID": "Number"}, inplace=True)
# Calculate percentage within each hazard group
dff2["Percentage Hazard"] = round(dff2["Number"] / dff2.groupby("Hazard Potential Classification")["Number"].transform("sum") * 100,1)
dff2["Percentage Hazard Owner"] = round(dff2["Number"] / dff2.groupby(["Hazard Potential Classification","Primary Owner Type"])["Number"].transform("sum") * 100,1)


#   choose between owners and purpose view
radioitems = html.Div(
    [
        dbc.Label("Zoom in on:"),
        dbc.RadioItems(
            options=[
                {"label": "Owner dam", "value": 'Owner'},
                {"label": "Purpose dam", "value": "Purpose"},
            ],
            value="Owner",
            id="radioitems-input",
            inline=True 
        ),
    ]
)





def create_summary(df):
    
    #FUNCTION CREATES THE GENERAL OVERVIEW OF HAZARD CLASSIFICATION AND EAP 
    
    
    # Aggregate
    dff4 = df.groupby(['Hazard Potential Classification', 'EAP Prepared'])['Federal ID'].count().reset_index()
    dff4.rename(columns={"Federal ID": "Number"}, inplace=True)

    # Percentages within group
    dff4["Percentage"] = round(
        dff4["Number"] / dff4.groupby("Hazard Potential Classification")["Number"].transform("sum") * 100, 1
    )

    # Desired order and colors
    eap_statuses = ['No', 'Not Required', 'Yes']
    colors = ['rgba(0, 61, 84,1)', 
              'rgba(56, 111, 152, 1)', 
              'rgba(188, 212, 230, 1)']

    # Set correct order for y-axis
    hazard_order = ["Undetermined", "Low", "Significant", "High"]
    dff4["Hazard Potential Classification"] = pd.Categorical(
        dff4["Hazard Potential Classification"],
        categories=hazard_order,
        ordered=True
    )
    dff4 = dff4.sort_values("Hazard Potential Classification")
    
    #dfs contains number of dambs per classification
    dfs = df['Hazard Potential Classification'].value_counts().reset_index()
    dfs["Hazard Potential Classification"] = pd.Categorical(
        dfs["Hazard Potential Classification"],
        categories=hazard_order,
        ordered=True
    )
    dfs = dfs.sort_values("Hazard Potential Classification")
    
    
    # Creating two subplots
    fig = make_subplots(rows=1, cols=2,specs=[[{}, {}]], shared_xaxes=True,
                    shared_yaxes=False, vertical_spacing=0.01,
                    subplot_titles=("USA: number of dams per Hazard Potential Classification", "Emergency Action Plan prepared?"))
    
    
    

    # Build traces for subplot 2, the percentages
    #traces = []
    for status, color in zip(eap_statuses, colors):
        subset = dff4[dff4["EAP Prepared"] == status]

        fig.add_trace(
            go.Bar(
                y=subset["Hazard Potential Classification"],
                x=subset["Percentage"],
                name=status,
                orientation="h",
                offsetgroup=1,
                marker_color=color,
                text=subset["Percentage"].astype(str) + '%',
                textposition="inside",
                insidetextanchor="middle",

            ),
            row=1,
            col=2,
        ),
       
      # Subplot1 - Overview
    fig.add_trace(
          go.Bar(
              
              x=dfs['count'],
              y=dfs['Hazard Potential Classification'],
              orientation="h",
              marker_color=colors_class,
              offsetgroup=0,
              showlegend=False,
              text=dfs['count'],
                textposition="outside",
                insidetextanchor="end",
              ),
          row=1,
          col=1,
          
      )      

    #add a warning rectangle for class High , no and not required
    fig.add_shape(type="rect",
        
        x0=0, y0=2.6, x1=23.7, y1=3.4,
        line=dict(
            color=colors_class[3],
            width=1,
        ),
        fillcolor=colors_class[1],
          row=1,
          col=2,
    )
    
    #add a warning rectangle for class Significant , no and not required
    fig.add_shape(type="rect",
        
        x0=0, y0=1.6, x1=34.3, y1=2.4,
        line=dict(
            color=colors_class[3],
            width=1,
        ),
        fillcolor=colors_class[1],
          row=1,
          col=2,
    )

    fig.update_layout(
        barmode="stack",
 
        yaxis_title=None,
        xaxis_title=None,
        xaxis2_title=None,
        template="plotly_dark",
        height=350,
        yaxis=dict(
            categoryorder="array",
            categoryarray=hazard_order
        ),
        legend=dict(
            orientation="h",
            yanchor="bottom",
            y=1.05,
            xanchor="right",
            x=1,
            title_text="EAP?"
        ),
    
        yaxis2=dict(
            showgrid=False,
            showline=True,
            showticklabels=False,

        ),
        
        
        
        margin=dict(l=20, r=20, t=50, b=20),
        
        showlegend=True # Set to False if you want to hide the legend
    )
    

    return fig


def create_tab(df,view, hazard_class):
    
    #FUNCTION CREATES THE DRILLDOWN BY HAZARD POTENTIAL CLASSIFICATION INTO
    #OWNER TYPE OR PRIMARY PURPOSE 
    
    #INPUT DFF2 = OWNER OR DFF6=PURPOSE, ALWAYS FILTERED BY HAZARD POTENTIAL CLASSIFICATION
    
    if view == 'Owner':
        colname = 'Primary Owner Type'
        colnameperc = 'Percentage Hazard Owner'
    else:
        colname = 'Primary Purpose'
        colnameperc = 'Percentage Hazard Purpose'
    
    # Aggregate for the total numbers per view
    dfg = df.groupby(colname)['Number'].sum().reset_index().sort_values(colname, ascending=False)
    
    
    #color subplot if dict will not work and you're in a  hurry and
    #not even create a switch
    
   
    
    if hazard_class == 'High':
        single_bar_color = colors_class[3]
    elif hazard_class == 'Significant':
        single_bar_color = colors_class[2]
    elif hazard_class == 'Low':
        single_bar_color = colors_class[1]
    else:
        single_bar_color = colors_class[0]

    # Desired order and colors
    eap_statuses = ['No', 'Not Required', 'Yes']
    colors = ['rgba(0, 61, 84,1)', 
              'rgba(56, 111, 152, 1)', 
              'rgba(188, 212, 230, 1)']

    # # Set correct order for y-axis
    y_order = df[colname].unique().tolist()[::-1]
    
    df[colname] = pd.Categorical(
        df[colname],
        categories=y_order,
        ordered=True
    )
    df = df.sort_values(colname)
    
        
    # Creating two subplots
    fig = make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_xaxes=True,
                    shared_yaxes=False, vertical_spacing=0.001,
                    subplot_titles=("Number of dams", "Emergency Action Plan prepared?"))
    
    
    

    # Build traces => subplot 2 % yes, not required, no

    for status, color in zip(eap_statuses, colors):
        subset = df[df["EAP Prepared"] == status]

        fig.add_trace(
            go.Bar(
                y=subset[colname],
                x=subset[colnameperc],
                name=status,
                orientation="h",
                offsetgroup=1,
                marker_color=color,
                text=subset[colnameperc].astype(str) + '%',
                textposition="inside",
                insidetextanchor="middle",

            ),
            row=1,
            col=2,
        ),
     
        
        
        
        
        
      # Subplot 1 - Grouped numbers
    fig.add_trace(
          go.Bar(
             # name=df[colname],
              x=dfg['Number'],
              y=dfg[colname],
              orientation="h",
              showlegend=False,
              marker_color=single_bar_color,
              offsetgroup=0,
              text=dfg['Number'],
                textposition="outside",
                insidetextanchor="end",
              ),
          row=1,
          col=1,
      )      

    

    fig.update_layout(
        barmode="stack",


        
        yaxis_title=None,
        xaxis_title=None,
        xaxis2_title=None,
        template="plotly_dark",
 
        yaxis2=dict(
            showgrid=False,
            showline=True,
            showticklabels=False,

        ),
        legend=dict(
            orientation="h",
            yanchor="bottom",
            y=1.05,
            xanchor="right",
            x=1,
            title_text="EAP?"
        ),
        
        
        
        showlegend=True  # Set to False if you want to hide the legend
    )
    

    return dcc.Graph(figure=fig)











# Initialize Dash app
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP,  dbc_css, vizro_bootstrap])

# Layout
app.layout = dbc.Container([
    dcc.Graph(figure=create_summary(df), id='general-overview'),
    dbc.Row([
        dbc.Col( html.H2(id='click-data')),
        dbc.Col(radioitems)
        ],style={"marginTop":"2rem","marginBottom":"1rem"}),
    html.Div(id='owner-overview')
    
    ])


@app.callback(
    Output('click-data', 'children'),
    Output('owner-overview','children'),
    Input('general-overview', 'clickData'),
    Input("radioitems-input", "value"))
def display_click_data(clickData, value):
    
    if clickData is None:
        hazard_class='High'
    else:
        hazard_class=clickData["points"][0]["label"]
        
    if value is None:
        df=dff2[dff2['Hazard Potential Classification']==hazard_class]
    elif value == "Owner":
        df=dff2[dff2['Hazard Potential Classification']==hazard_class]
    else:
        df=dff6[dff6['Hazard Potential Classification']==hazard_class]
        
                      
                                                           
    h2_title = f"{hazard_class} Hazard Potential, {value}"
    
    return h2_title, create_tab(df, value, hazard_class)

if __name__ == '__main__':
    app.run(debug=True)