-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
433 lines (345 loc) · 13.3 KB
/
utility.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
import streamlit as st
import pandas as pd
import datetime as dt
import altair as alt
import requests
import folium
import plotly.graph_objects as go
from folium.plugins import MousePosition
from st_aggrid import (
AgGrid,
ColumnsAutoSizeMode,
GridOptionsBuilder,
GridUpdateMode,
JsCode,
)
def load_lottieurl(url) -> dict:
r = requests.get(url)
if r.status_code != 200:
return None
return r.json()
def set_dataset_size(df: pd.DataFrame, size: str) -> pd.DataFrame:
"""
Limits dataset to 30 days, 7 days or 24 hours time periods.
Returns filtered dataframe.
"""
# df['time (UTC)'] = pd.to_datetime(df['time (UTC)'], format='%d-%b-%Y %H:%M:%S')
# Removing timezone info
df["time (UTC)"] = pd.to_datetime(
df["time (UTC)"], infer_datetime_format=True
).dt.tz_localize(None)
max_time = df["time (UTC)"].max()
min_time = max_time - dt.timedelta(days=size)
df = df.loc[(df["time (UTC)"] >= min_time) & (df["time (UTC)"] <= max_time)]
return df
def data_filter(df: pd.DataFrame) -> pd.DataFrame:
"""
Updates ag-Grid table and map according to selected filters.
Returns filtered dataframe.
"""
filter_container = st.container()
with filter_container:
filter_col1, filter_col2 = st.columns((1, 1))
with filter_col1:
periodicals = {"Past 30 Days": 30, "Past 7 Days": 7, "Last 24 Hours": 1}
option = st.selectbox(
label="Dataset size",
options=periodicals.keys(),
)
df = set_dataset_size(df, periodicals[option])
with filter_col2:
# source panel only displays data source and has no other use right now
source = st.selectbox(
label="Dataset source",
options=["United States Geological Survey"],
disabled=True,
)
# If not selected, funtion will return dataframe before applying additional filters
add_filter = st.checkbox("Add more filters")
if not add_filter:
return df
# FILTERING DATAFRAME
selected_columns = st.multiselect(
"Choose an option to filter dataframe",
df.loc[:, df.columns != "place"].columns,
)
for column in selected_columns:
left, right = st.columns((1, 20))
if column == "time (UTC)":
if len(df) > 0:
date_col1, date_col2, date_col3, date_col4 = st.columns((1, 4.4, 4.4, 10))
start_dt = date_col2.date_input(
"➤ Start date", value=df["time (UTC)"].min()
)
end_dt = date_col3.date_input(
"➤ End date", value=df["time (UTC)"].max()
)
if start_dt <= end_dt:
df = df.loc[
(
df["time (UTC)"]
>= dt.datetime(
start_dt.year, start_dt.month, start_dt.day
)
)
& (
df["time (UTC)"]
<= dt.datetime(end_dt.year, end_dt.month, end_dt.day)
+ dt.timedelta(days=1)
)
]
else:
date_col2.warning("Please check your date range")
else:
right.error(f"Not enough {column} values to filter")
elif column in ["latitude", "longitude"]:
if column == "latitude":
min_coordinate = -90
max_coordinate = 90
elif column == "longitude":
min_coordinate = -180
max_coordinate = 180
coordinate_input = right.slider(
f"➤ Select your {column} range",
min_value=min_coordinate,
max_value=max_coordinate,
value=(min_coordinate, max_coordinate),
)
df = df.loc[df[column].between(*coordinate_input)]
elif column in ["depth", "mag"]:
if len(df) > 1:
min_value = float(df[column].min())
max_value = float(df[column].max())
if min_value == max_value:
right.warning(f"{column.capitalize()} value is same for all rows")
else:
value_range = right.slider(
f"➤ Select your {column} range",
min_value=min_value,
max_value=max_value,
value=(min_value, max_value),
help=f"You can limit minimum and maximum {column} values via sliders. This will update your table.",
)
df = df.loc[df[column].between(*value_range)]
else:
right.error(f"Not enough {column} values to filter")
elif column in ["magType", "type", "locationSource", "magSource", "status"]:
if len(df) > 0:
category_input = right.multiselect(
f"➤ Values for {column}",
df[column].unique(),
default=list(df[column].unique()),
)
df = df.loc[df[column].isin(category_input)]
else:
right.error(f"Not enough {column} values to filter")
return df
def convert_to_csv(df):
"""Converts filtered pandas dataframe to CSV file"""
return df.to_csv(index=False, encoding="utf-8")
def create_data_grid(df: pd.DataFrame) -> list:
"""Creates ag-Grid data grid with filtered data and returns list of selected rows"""
grid = GridOptionsBuilder.from_dataframe(df)
# Separating grid into discrete pages instead of scrolling
grid.configure_pagination(enabled=True)
# Selection checkboxes on the grid
grid.configure_selection(selection_mode="multiple", header_checkbox=True, use_checkbox=True)
# Changing cell style for magnitude values over or equal to 6
cellsytle_jscode = JsCode(
"""
function(params) {
if (params.value >= 7) {
return {
'color': 'white',
'backgroundColor': 'firebrick'
};
} else if (params.value >= 6) {
return {
'color': 'white',
'backgroundColor': 'chocolate'
};
} else if (params.value >= 5) {
return {
'color': 'white',
'backgroundColor': 'olivedrab'
};
}
};
"""
)
grid.configure_column("mag", cellStyle=cellsytle_jscode)
grid.configure_grid_options(domLayout="normal")
gridOptions = grid.build()
grid_response = AgGrid(
df,
gridOptions=gridOptions,
height=500,
width="100%",
theme="balham",
update_mode=GridUpdateMode.SELECTION_CHANGED,
columns_auto_size_mode=ColumnsAutoSizeMode.FIT_CONTENTS,
allow_unsafe_jscode=True,
)
return grid_response["selected_rows"]
def draw_world_map(tiles: str):
"""Create empty map centered on the coordinates (0,0) with given map tileset"""
map = folium.Map(
min_zoom=1.5,
min_lon=-250,
max_lon=250,
min_lat=-85,
max_lat=85,
max_bounds=True,
tiles=tiles,
attr="Latest Earthquakes",
)
formatter = "function(num) {return L.Util.formatNum(num, 3) + ' º ';};"
MousePosition(
position="bottomleft",
separator=" | ",
empty_string="NaN",
lng_first=False,
num_digits=20,
prefix="Coordinates(lat | lon):",
lat_formatter=formatter,
lng_formatter=formatter,
).add_to(map)
return map
def magnitude_colorcode(mag: float) -> str:
if mag >= 7:
return "red"
elif 7 > mag >= 6:
return "orange"
elif 6 > mag >= 5:
return "green"
return "blue"
def add_map_marker(map, lat: float, lon: float, mag: float, depth: float, place: str):
"""Add marker and info popup to map"""
marker_color = magnitude_colorcode(mag=mag)
info = f"""<center> <b> {place} </b> </center>
<br> <center> <b> Mag: {float(round(mag, 1))} </b> <center>
<center> Depth: {depth} km </center>
<br> <center> Latitude/Longitude: {lat}/{lon} </center>
"""
popup = folium.Popup(info)
marker_location = (lat, lon)
folium.Marker(
location=marker_location,
popup=popup,
icon=folium.Icon(icon="info-sign", color=marker_color),
).add_to(map)
def map_layer_panel() -> str:
"""Create map layer panel and returns selected tiles from the panel"""
layers = ["Base Map", "World Imagery", "Street Map"]
map_layer = st.selectbox("Select map layer", layers)
if map_layer == layers[0]:
tiles = "https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}"
elif map_layer == layers[1]:
tiles = "https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
elif map_layer == layers[2]:
tiles = "https://tile.openstreetmap.org/{z}/{x}/{y}.png"
return tiles
def circle_search_panel(map, df: pd.DataFrame) -> tuple:
"""Creates search area input panel and returns latitude, longitude and radius values"""
with st.form(key="Area Parameters", clear_on_submit=False):
lat_input = st.number_input(
label="Latitude",
value=39.57,
min_value=-90.0,
max_value=90.0,
format="%.15f",
)
lon_input = st.number_input(
label="Longitude",
value=32.53,
min_value=-180.0,
max_value=180.0,
format="%.15f",
)
radius_input = st.slider(
label="Radius (km)", min_value=0, max_value=1000, step=5
)
st.form_submit_button("Apply")
return lat_input, lon_input, radius_input
def create_hourly_distribution_bar_chart(df: pd.DataFrame, x_axis: str, y_axis: str):
"""Creates hourly distribution bar chart with given x and y axis values"""
events = df.loc[:, y_axis].tolist()
scale = alt.Scale(domain=[0, max(events) + max(events) / 10])
bars = (
alt.Chart(data=df)
.mark_bar(cornerRadiusTopLeft=4, cornerRadiusTopRight=4)
.encode(
x=alt.X(x_axis),
y=alt.Y(y_axis, scale=scale),
color=alt.condition(
alt.datum[y_axis] == max(events),
alt.value("orange"),
alt.value("steelblue"),
),
)
)
text = bars.mark_text(
align="center", baseline="middle", dy=-10 # Moves text up
).encode(text=f"{y_axis}:O")
st.altair_chart(bars + text, use_container_width=True)
def create_magnitude_bar_chart(df: pd.DataFrame, x_axis: str, y_axis: str):
"""Creates magnitude bar chart with given x and y axis values"""
events = df.loc[:, y_axis].tolist()
scale = alt.Scale(domain=[0, max(events) + max(events) / 10])
bars = (
alt.Chart(data=df)
.mark_bar(cornerRadiusTopLeft=2, cornerRadiusTopRight=2)
.encode(
x=alt.X(x_axis),
y=alt.Y(y_axis, scale=scale),
color=alt.condition(
alt.datum[y_axis] == max(events),
alt.value("orange"),
alt.value("steelblue"),
),
tooltip=[x_axis, y_axis],
)
.interactive()
)
st.altair_chart(bars, use_container_width=True)
def create_worldwide_earthquakes_bar_chart(df: pd.DataFrame, x_axis: str, y_axis: str):
"""Creates annual earthquake bar chart with given x and y axis values"""
events = df.loc[:, y_axis].tolist()
scale = alt.Scale(domain=[0, max(events) + max(events) / 10])
bars = (
alt.Chart(data=df)
.mark_bar(
cornerRadiusTopLeft=4,
cornerRadiusTopRight=4,
)
.encode(
x=alt.X(x_axis),
y=alt.Y(y_axis, scale=scale),
color=alt.condition(
alt.datum[y_axis] == max(events),
alt.value("orange"),
alt.value("steelblue"),
),
)
)
text = bars.mark_text(
align="center", baseline="middle", dy=-10 # Moves text up
).encode(text=f"{y_axis}:O")
rule = alt.Chart(df).mark_rule(color="red").encode(y=f"mean({y_axis}):Q")
st.altair_chart(bars + rule + text, use_container_width=True)
def create_scattergeo_map(lat: list, lon: list, hovertext: list):
"""Creates 3d outline map with given latitude, longitude and popup text list"""
fig = go.Figure()
fig.add_trace(
go.Scattergeo(
name="",
lat=lat,
lon=lon,
hovertext=hovertext,
mode="markers",
marker=dict(size=15, opacity=0.6, color="firebrick", symbol="circle"),
)
)
fig.update_geos(projection_type="orthographic")
fig.update_layout(width=750, height=750)
st.plotly_chart(fig, use_container_width=True)