-
Notifications
You must be signed in to change notification settings - Fork 2
/
app_callbacks.py
228 lines (198 loc) · 6.8 KB
/
app_callbacks.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
import numpy as np
import plotly.graph_objects as go
from app_helpers import calculate_water, ensemble_predict, get_bounding_box, get_bounding_box_area, get_image_path, get_water_land_per_year, load_image
from app_layout import get_mapbox
def callback_dropdown_year(df, dropdown_water_body):
if not dropdown_water_body:
return [{"label": str(n), "value": str(n)} for n in range(2016, 2020)]
layers = df.loc[dropdown_water_body, "layers"]
years = [{"label": str(n), "value": str(n)} for n in layers]
return years
def callback_histogram(df, models, dropdown_water_body):
if not dropdown_water_body:
histogram = _get_histogram_default()
return histogram
years = df.loc[dropdown_water_body, "layers"]
prediction = []
prediction_dic = dict()
for i in years:
lake = get_image_path(df, dropdown_water_body, i)
image = load_image(lake)
mask = ensemble_predict(models, image)
water_percentage = calculate_water(mask) * 100
prediction_dic[str(i)] = water_percentage
prediction.append(water_percentage)
[X, Y, _] = [years, prediction, ['#000']]
histogram = _get_histogram(X, Y)
return histogram
def callback_mapbox(df, mapbox_access_token, dropdown_water_body):
mapbox = get_mapbox(df, mapbox_access_token)
if not dropdown_water_body:
return mapbox
dff = df.loc[dropdown_water_body, ["min_longitude", "max_longitude", "min_latitude", "max_latitude"]]
min_longitude = dff["min_longitude"]
max_longitude = dff["max_longitude"]
min_latitude = dff["min_latitude"]
max_latitude = dff["max_latitude"]
longit_center = (min_longitude + max_longitude) / 2.0
latit_center = (min_latitude + max_latitude) / 2.0
mapbox.update_layout(
mapbox = {
'center': {'lon': longit_center, 'lat': latit_center},
'style': 'outdoors',
'zoom': 6},
showlegend = True)
return mapbox
def callback_pie_chart(df, models, dropdown_water_body, dropdown_year):
if not dropdown_water_body:
pie_chart = _get_pie_chart_default()
return pie_chart
dff = df.loc[dropdown_water_body, :]
# get predictions from the model
lake = get_image_path(df, dropdown_water_body, dropdown_year)
image = load_image(lake)
mask = ensemble_predict(models, image)
# receiving the area for the whole image
bounding_box = get_bounding_box(dff)
image_sqkm = get_bounding_box_area(bounding_box)
# calculating the area for water and land
water_percentage = calculate_water(mask)
water_sqkm, land_sqkm = get_water_land_per_year(fraction=water_percentage, area=image_sqkm)
water_sqkm, land_sqkm = round(water_sqkm, 2), round(land_sqkm, 2)
#plot
values=[water_sqkm, land_sqkm]
pie_chart = _get_pie_chart(values)
return pie_chart
def callback_satellite_image(df, models, dropdown_water_body, dropdown_year, slider_opacity):
figure = go.Figure()
figure.update_layout(
margin={"r": 0, "t": 0, "l": 0, "b": 0},
xaxis={'showgrid': False, 'zeroline': False, 'visible': False},
yaxis={'showgrid': False, 'zeroline': False, 'visible': False},
plot_bgcolor="#282b38",
paper_bgcolor="#282b38",
height=300,
showlegend=False,
)
if not dropdown_water_body:
return figure
lake = get_image_path(df, dropdown_water_body, dropdown_year)
image = load_image(lake)
mask = ensemble_predict(models, image)
_apply_mask_contour(figure, image, mask, slider_opacity)
return figure
def _apply_mask_contour(figure, image, mask, slider_opacity):
colorscale = [[0, 'gold'], [0.5, 'gold'], [1, 'gold']]
image = 255 * image
figure.add_trace(
go.Image(z=image)
)
figure.update_layout(
margin={"r": 60, "t": 0, "l": 0, "b": 0}
)
figure.add_trace(
go.Contour(
z=mask,
contours_coloring='lines',
line_width=3,
opacity=slider_opacity,
showlegend=False,
showscale=False,
colorscale=colorscale,
colorbar=dict(showticklabels=False))
)
return figure
def _get_histogram_default():
figure = go.Figure()
figure.update_layout(
margin={"r": 0, "t": 0, "l": 0, "b": 0},
xaxis={'showgrid': False, 'zeroline': False, 'visible': False},
yaxis={'showgrid': False, 'zeroline': False, 'visible': False},
plot_bgcolor="#282b38",
paper_bgcolor="#282b38")
return figure
def _get_histogram(X, Y):
layout = go.Layout(
bargap=0.01,
bargroupgap=0,
barmode="group",
margin=go.layout.Margin(l=10, r=0, t=0, b=30),
showlegend=False,
plot_bgcolor="#282b38",
paper_bgcolor="#282b38",
dragmode="select",
font=dict(color="white"),
height=150,
xaxis=dict(
range=[2015.5, 2019.5],
showgrid=False,
fixedrange=False
),
yaxis=dict(
range=[0, max(Y) + max(Y) / 4],
showticklabels=False,
showgrid=False,
fixedrange=True,
rangemode="nonnegative",
zeroline=False,
),
annotations=[
dict(
x=xi,
y=yi,
text=str(np.round(yi, 2)),
xanchor="center",
yanchor="middle",
showarrow=False,
font=dict(color="white"),
)
for xi, yi in zip(X, Y)
],
)
histogram = go.Figure(
data=[
go.Scatter(
opacity=1,
x=X,
y=Y,
hoverinfo="none",
mode="lines+markers",
marker=dict(color="rgb(66, 134, 244, 0)", size=40),
visible=True,
),
],
layout=layout,
)
return histogram
def _get_pie_chart_default():
figure = go.Figure()
figure.update_layout(
margin={"r": 0, "t": 0, "l": 0, "b": 0},
xaxis={'showgrid': False, 'zeroline': False, 'visible': False},
yaxis={'showgrid': False, 'zeroline': False, 'visible': False},
plot_bgcolor="#282b38",
paper_bgcolor="#282b38")
return figure
def _get_pie_chart(values):
labels=["water", "land"]
figure = go.Figure(
data=[
go.Pie(
labels=labels,
values=values,
hole=0.3,
showlegend=False)
]
)
figure.update_traces(
hoverinfo='label',
textinfo='value',
marker=dict(colors=["rgb(66, 134, 244, 0)", "rgb(150, 75, 0)"])
)
figure.update_layout(
margin={"r":100, "t":0, "l":0, "b":0},
height=150,
plot_bgcolor="#282b38",
paper_bgcolor="#282b38"
)
return figure