forked from vostpt/ICNF_DATA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsankey.py
280 lines (228 loc) · 10.8 KB
/
sankey.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
# -*- coding: utf-8 -*-
# Import Libraries
import time
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
import pandas as pd
import dash
from dash import Input, Output, dcc, html
import dash_bootstrap_components as dbc
# First Data Treatment
# Data Treatment
df_in = pd.read_csv('https://raw.githubusercontent.com/vostpt/ICNF_DATA/main/ICNF_2013_2022_SANKEY.csv') #generated by updater.py
dummy_year=[2017,2020]
dummy_district = ['Faro','Bragança']
# Cleanup where DISTRITO and CONCELHO have the same value
df_in["CONCELHO"] = np.where(df_in["DISTRITO"]==df_in["CONCELHO"], df_in["CONCELHO"]+"_concelho", df_in["CONCELHO"])
# Deal with some duplicates names across source and target
df_in["CONCELHO"] = df_in["CONCELHO"].str.capitalize()
# Sort values in dataframe
df_in = df_in.sort_values(["ANO", "DISTRITO","CONCELHO"])
# Use isin function to filter dataframe
# by district from dropdown
df_filter_district = df_in[df_in['DISTRITO'].isin(dummy_district)].reset_index()
# by year
df_filter = df_filter_district[df_filter_district['ANO'].isin(dummy_year)].reset_index()
# More Data Treatment
# Filter by ANO and DISTRITO while summing NCCO.
# Also renaming columns for readibility
df = df_in.groupby(["ANO","DISTRITO"], as_index=False)["NCCO"].sum().rename(columns={"ANO":"source","DISTRITO":"target","NCCO":"value"})
# Change ANO type to string
df["source"] = df["source"].astype(int).astype(str)
# Concatenate previous dataframe with a new dataframe that
# groups DISTRITO and CONCELHO.
# This can be done enumerous times to create more steps for the Sankey
df = pd.concat([df, df_in.groupby(["DISTRITO","CONCELHO"], as_index=False)["NCCO"].sum().rename(columns={"DISTRITO":"source","CONCELHO":"target", "NCCO":"value"})])
# Create Nodes
nodes = np.unique(df[["source","target"]], axis=None)
nodes = pd.Series(index=nodes, data=range(len(nodes)))
# Create Node Colors
# node_colors = [np.random.choice(colors) for node in nodes]
# define color scale
colors = px.colors.qualitative.Plotly
# define one random color for every node
node_colors_mappings = dict([(node,np.random.choice(colors)) for node in nodes])
node_colors = [node_colors_mappings[node] for node in nodes]
edge_colors = [node_colors_mappings[node] for node in nodes]
# Plot Graphs
fig = go.Figure(
go.Sankey(
node=dict(
label = nodes.index,
line = dict(color = "white", width = 1.0),
color = node_colors,
),
link={
"source": nodes.loc[df["source"]],
"target": nodes.loc[df["target"]],
"value": df["value"],
},
)
)
# Update Layout
#fig.update_layout(title_text="FOREST FIRES IN PORTUGAL",
# height = 900,
# width=1600,
# font_size=12)
fig.update_layout(plot_bgcolor='black', paper_bgcolor='black',font=dict(size = 10, color = 'white'),)
# START APP -----------------------------------------------------
app = dash.Dash(
external_stylesheets=[dbc.themes.CYBORG],
#suppress_callback_exceptions=True,
meta_tags=[{"name": "viewport", "content": "width=device-width, initial-scale=1"}],
)
app.title = 'VOSTPT - ICNF'
app.layout = dbc.Container(
[
# First Row
dbc.Row(
[
dbc.Col(
html.Hr(
style={
"borderWidth": "2vh",
"width": "100%",
"borderColor": "#A30000",
"opacity": "unset",
}
),
width={"size": 12},
),
],
className="g-0",
), # end of first row
# Second Row
dbc.Row(
[ # you have to create a children's array to have more than one column in a row
dbc.Col(
html.H3("FOREST FIRES IN PORTUGAL"),
width={"size": 6, "offset": 0},
), # First Column
dbc.Col(
html.H4("Data ICNF", style={"color": "#A30000"}),
width={"size": 5, "offset": 0},
), # Second Column
], # Close Children of Second Row
), # End of second row
# Third Row
dbc.Row(
[
# Year Dropdown
dbc.Col(
dcc.Dropdown(
id="dropdown_year",
options=[
{"label": i, "value": i}
for i in df_in.ANO.unique()
],
optionHeight=35, # height/space between dropdown options
value=[2013,2022], # dropdown value selected automatically when page loads
disabled=False, # disable dropdown value selection
multi=True, # allow multiple dropdown values to be selected
searchable=True, # allow user-searching of dropdown values
search_value="", # remembers the value searched in dropdown
placeholder="Please select year", # gray, default text shown when no option is selected
clearable=True, # allow user to removes the selected value
style={
"width": "100%"
}, # use dictionary to define CSS styles of your dropdown
# className='select_box', #activate separate CSS document in assets folder
# persistence=True, #remembers dropdown value. Used with persistence_type
# persistence_type='memory' #remembers dropdown value selected until...
),
),
# District Dropdown
dbc.Col(
dcc.Dropdown(
id="dropdown_district",
options=[
{"label": i, "value": i}
for i in df_in.DISTRITO.unique()
],
optionHeight=35, # height/space between dropdown options
value=['Aveiro','Viseu'], # dropdown value selected automatically when page loads
disabled=False, # disable dropdown value selection
multi=True, # allow multiple dropdown values to be selected
searchable=True, # allow user-searching of dropdown values
search_value="", # remembers the value searched in dropdown
placeholder="Please select District", # gray, default text shown when no option is selected
clearable=True, # allow user to removes the selected value
style={
"width": "100%"
}, # use dictionary to define CSS styles of your dropdown
# className='select_box', #activate separate CSS document in assets folder
# persistence=True, #remembers dropdown value. Used with persistence_type
# persistence_type='memory' #remembers dropdown value selected until...
),
),
],
),
# Fourth Row
dbc.Row(
dbc.Col(dcc.Graph(id="sankey", figure=fig))
),
],
)
@app.callback(
Output(component_id="sankey",component_property="figure"),
Input(component_id="dropdown_year", component_property="value"),
Input(component_id="dropdown_district", component_property="value"),
)
def build_graph(dropdown_year, dropdown_district):
# Data Treatment
df_in = pd.read_csv('https://raw.githubusercontent.com/vostpt/ICNF_DATA/main/ICNF_2013_2022_SANKEY.csv') #generated by updater.py
# Cleanup where DISTRITO and CONCELHO have the same value
df_in["CONCELHO"] = np.where(df_in["DISTRITO"]==df_in["CONCELHO"], df_in["CONCELHO"]+"_concelho", df_in["CONCELHO"])
# Deal with some duplicates names across source and target
df_in["CONCELHO"] = df_in["CONCELHO"].str.capitalize()
# Sort values in dataframe
df_in = df_in.sort_values(["ANO", "DISTRITO","CONCELHO"])
# Use isin function to filter dataframe
# by year from dropdown
df_filter_year = df_in[df_in['ANO'].isin(dropdown_year)].reset_index()
# by district
df_filter = df_filter_year[df_filter_year['DISTRITO'].isin(dropdown_district)].reset_index()
# More Data Treatment
# Filter by ANO and DISTRITO while summing NCCO.
# Also renaming columns for readibility
df = df_filter.groupby(["ANO","DISTRITO"], as_index=False)["NCCO"].sum().rename(columns={"ANO":"source","DISTRITO":"target","NCCO":"value"})
# Change ANO type to string
df["source"] = df["source"].astype(int).astype(str)
# Concatenate previous dataframe with a new dataframe that
# groups DISTRITO and CONCELHO.
# This can be done enumerous times to create more steps for the Sankey
df = pd.concat([df, df_filter.groupby(["DISTRITO","CONCELHO"], as_index=False)["NCCO"].sum().rename(columns={"DISTRITO":"source","CONCELHO":"target", "NCCO":"value"})])
# Create Nodes
nodes = np.unique(df[["source","target"]], axis=None)
nodes = pd.Series(index=nodes, data=range(len(nodes)))
# define color scale
colors = px.colors.sequential.Plasma_r
# define one random color for every node
node_colors_mappings = dict([(node,np.random.choice(colors)) for node in nodes])
node_colors = [node_colors_mappings[node] for node in nodes]
edge_colors = [node_colors_mappings[node] for node in nodes]
# Plot Graphs
fig = go.Figure(
go.Sankey(
node=dict(
label = nodes.index,
color = node_colors,
),
link={
"source": nodes.loc[df["source"]],
"target": nodes.loc[df["target"]],
"value": df["value"],
},
)
)
# Update Layout
fig.update_layout(plot_bgcolor='black', paper_bgcolor='black',font=dict(size = 10, color = 'white'), height=900)
# Update Orientation
# fig.update_traces(orientation="v", selector=dict(type='sankey'))
return fig
if __name__ == "__main__":
app.run_server(debug=True, port=8888)
# END APP
# END App