Spaces:
Sleeping
Sleeping
Commit ·
731c0dd
1
Parent(s): 0b30b31
add filter option for temp, and input button
Browse files- app.py +21 -7
- data_utils.py +3 -1
- plot_utils.py +15 -15
app.py
CHANGED
|
@@ -19,6 +19,8 @@ original_df, df, X, Y, T = prepare_data(og_file, training_file)
|
|
| 19 |
app_ui = ui.page_fluid(
|
| 20 |
ui.input_radio_buttons("tab_choice", "Choose page:", ["Effect of trees on temperature", "Effect of temperature on trees"]),
|
| 21 |
ui.output_ui("tab_slider"),
|
|
|
|
|
|
|
| 22 |
ui.output_ui("page_ui")
|
| 23 |
)
|
| 24 |
|
|
@@ -49,13 +51,13 @@ def server(input, output, session):
|
|
| 49 |
def page_ui():
|
| 50 |
if input.tab_choice() == "Effect of trees on temperature":
|
| 51 |
return ui.div(
|
| 52 |
-
ui.h2("
|
| 53 |
ui.div(ui.output_plot("temp_after_treatment"), style="display: flex; justify-content: center;"),
|
| 54 |
ui.div(ui.output_plot("temp_change"), style="display: flex; justify-content: center;"),
|
| 55 |
)
|
| 56 |
else: #Effect of temperature on trees
|
| 57 |
return ui.div(
|
| 58 |
-
ui.h2("
|
| 59 |
ui.div(ui.output_plot("trees_after_decrease"), style="display: flex; justify-content: center;"),
|
| 60 |
ui.div(ui.output_plot("tree_coverage_change"), style="display: flex; justify-content: center;"),
|
| 61 |
)
|
|
@@ -65,12 +67,17 @@ def server(input, output, session):
|
|
| 65 |
def temp_after_treatment():
|
| 66 |
gdf_trees = get_gdf_trees()
|
| 67 |
value_trees = input.tree_pct()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
try:
|
| 69 |
fig = show_two_plots(gdf_trees, f'simulated_temp_{value_trees}%', 'LST',
|
| 70 |
-
15, 35, 'coolwarm',
|
| 71 |
-
|
| 72 |
-
"Original temperature",
|
| 73 |
-
'Temperature (°C)')
|
| 74 |
return fig
|
| 75 |
except Exception as e:
|
| 76 |
print("Plotting error:", e)
|
|
@@ -92,13 +99,20 @@ def server(input, output, session):
|
|
| 92 |
def trees_after_decrease():
|
| 93 |
gdf_temp = get_gdf_temp()
|
| 94 |
value_temp = input.temp_goal()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
min_cobertura_veg = 5
|
| 96 |
max_cobertura = 100
|
| 97 |
try:
|
| 98 |
fig = show_two_plots(gdf_temp, f'total_trees_needed_for_{value_temp}C', '%CoberturaVeg',
|
| 99 |
min_cobertura_veg, max_cobertura, 'YlGn',
|
| 100 |
f"Tree coverage needed for {value_temp}°C decrease","Original tree canopy coverage",
|
| 101 |
-
"Tree canopy coverage (%)", split=
|
| 102 |
return fig
|
| 103 |
except Exception as e:
|
| 104 |
print("Plotting error:", e)
|
|
|
|
| 19 |
app_ui = ui.page_fluid(
|
| 20 |
ui.input_radio_buttons("tab_choice", "Choose page:", ["Effect of trees on temperature", "Effect of temperature on trees"]),
|
| 21 |
ui.output_ui("tab_slider"),
|
| 22 |
+
ui.input_radio_buttons("split_filter", "Apply filter?",
|
| 23 |
+
choices=["No filter", "Filter"], selected="No filter"),
|
| 24 |
ui.output_ui("page_ui")
|
| 25 |
)
|
| 26 |
|
|
|
|
| 51 |
def page_ui():
|
| 52 |
if input.tab_choice() == "Effect of trees on temperature":
|
| 53 |
return ui.div(
|
| 54 |
+
ui.h2("Simulation Viewer - Increasing tree coverage", class_="text-center"),
|
| 55 |
ui.div(ui.output_plot("temp_after_treatment"), style="display: flex; justify-content: center;"),
|
| 56 |
ui.div(ui.output_plot("temp_change"), style="display: flex; justify-content: center;"),
|
| 57 |
)
|
| 58 |
else: #Effect of temperature on trees
|
| 59 |
return ui.div(
|
| 60 |
+
ui.h2("Simulation Viewer - Decreasing temperature", class_="text-center"),
|
| 61 |
ui.div(ui.output_plot("trees_after_decrease"), style="display: flex; justify-content: center;"),
|
| 62 |
ui.div(ui.output_plot("tree_coverage_change"), style="display: flex; justify-content: center;"),
|
| 63 |
)
|
|
|
|
| 67 |
def temp_after_treatment():
|
| 68 |
gdf_trees = get_gdf_trees()
|
| 69 |
value_trees = input.tree_pct()
|
| 70 |
+
apply_filter = input.split_filter()
|
| 71 |
+
|
| 72 |
+
if apply_filter == "Filter":
|
| 73 |
+
split = f'total_trees_treatment_{value_trees}%'
|
| 74 |
+
else:
|
| 75 |
+
split = False
|
| 76 |
+
|
| 77 |
try:
|
| 78 |
fig = show_two_plots(gdf_trees, f'simulated_temp_{value_trees}%', 'LST',
|
| 79 |
+
15, 35, 'coolwarm',f"Temperature after treatment (+{value_trees}% Trees)",
|
| 80 |
+
"Original temperature",'Temperature (°C)',split=split)
|
|
|
|
|
|
|
| 81 |
return fig
|
| 82 |
except Exception as e:
|
| 83 |
print("Plotting error:", e)
|
|
|
|
| 99 |
def trees_after_decrease():
|
| 100 |
gdf_temp = get_gdf_temp()
|
| 101 |
value_temp = input.temp_goal()
|
| 102 |
+
apply_filter = input.split_filter()
|
| 103 |
+
|
| 104 |
+
if apply_filter == "Filter":
|
| 105 |
+
split = f'total_trees_needed_for_{value_temp}C'
|
| 106 |
+
else:
|
| 107 |
+
split = False
|
| 108 |
+
|
| 109 |
min_cobertura_veg = 5
|
| 110 |
max_cobertura = 100
|
| 111 |
try:
|
| 112 |
fig = show_two_plots(gdf_temp, f'total_trees_needed_for_{value_temp}C', '%CoberturaVeg',
|
| 113 |
min_cobertura_veg, max_cobertura, 'YlGn',
|
| 114 |
f"Tree coverage needed for {value_temp}°C decrease","Original tree canopy coverage",
|
| 115 |
+
"Tree canopy coverage (%)", split=split)
|
| 116 |
return fig
|
| 117 |
except Exception as e:
|
| 118 |
print("Plotting error:", e)
|
data_utils.py
CHANGED
|
@@ -46,7 +46,8 @@ def run_treatment_increase(X, T, original_df, df, value, name):
|
|
| 46 |
treatment_df = prepare_treatment_df(original_df, df)
|
| 47 |
T_sim = T.copy()
|
| 48 |
value_num = 1+ (value/100)
|
| 49 |
-
|
|
|
|
| 50 |
response = requests.post(url, headers=headers, json=data)
|
| 51 |
if response.ok:
|
| 52 |
result = response.json()
|
|
@@ -55,6 +56,7 @@ def run_treatment_increase(X, T, original_df, df, value, name):
|
|
| 55 |
print("Request failed:", response.status_code, response.text)
|
| 56 |
|
| 57 |
treatment_df[name] = result["effect"]
|
|
|
|
| 58 |
|
| 59 |
return treatment_df
|
| 60 |
|
|
|
|
| 46 |
treatment_df = prepare_treatment_df(original_df, df)
|
| 47 |
T_sim = T.copy()
|
| 48 |
value_num = 1+ (value/100)
|
| 49 |
+
T_sim = T_sim*value_num
|
| 50 |
+
data = {"X": X.tolist(), "T0":T.tolist(), "T1":(T_sim).tolist()}
|
| 51 |
response = requests.post(url, headers=headers, json=data)
|
| 52 |
if response.ok:
|
| 53 |
result = response.json()
|
|
|
|
| 56 |
print("Request failed:", response.status_code, response.text)
|
| 57 |
|
| 58 |
treatment_df[name] = result["effect"]
|
| 59 |
+
treatment_df[f'total_trees_treatment_{value}%'] = T_sim
|
| 60 |
|
| 61 |
return treatment_df
|
| 62 |
|
plot_utils.py
CHANGED
|
@@ -5,6 +5,7 @@ import matplotlib.gridspec as gridspec
|
|
| 5 |
|
| 6 |
|
| 7 |
def show_two_plots(gdf, name1, name2, vmin, vmax, colorscheme, title1, title2, label, split=False):
|
|
|
|
| 8 |
if gdf.empty:
|
| 9 |
raise ValueError("GeoDataFrame is empty")
|
| 10 |
|
|
@@ -23,36 +24,35 @@ def show_two_plots(gdf, name1, name2, vmin, vmax, colorscheme, title1, title2, l
|
|
| 23 |
|
| 24 |
axes = [fig.add_subplot(gs[0]), fig.add_subplot(gs[1]), fig.add_subplot(gs[2]) ]
|
| 25 |
|
| 26 |
-
if split:
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
above_capacity = gdf[gdf[name1] > (100-gdf['%Construccion'])]
|
| 30 |
-
above_100 = gdf[gdf[name1] > 100]
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Plot values ≤ 100 using colormap
|
| 34 |
-
|
| 35 |
|
| 36 |
# Plot values > capacity in orange
|
| 37 |
-
above_capacity.plot(color='
|
| 38 |
|
| 39 |
# Plot values > 100 in red
|
| 40 |
-
above_100.plot(color='
|
| 41 |
|
| 42 |
# Add legend manually for orange points
|
| 43 |
orange_patch = plt.Line2D([0], [0], marker='o', color='w', label='> capacity due to construction',
|
| 44 |
-
markerfacecolor='
|
| 45 |
|
| 46 |
# Add legend manually for red points
|
| 47 |
red_patch = plt.Line2D([0], [0], marker='o', color='w', label='> 100% trees',
|
| 48 |
-
markerfacecolor='
|
| 49 |
|
| 50 |
axes[0].legend(handles=[orange_patch, red_patch])
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
else:
|
| 54 |
-
# First plot
|
| 55 |
-
gdf.plot(column=name1, cmap=cmap, norm=norm, ax=axes[0], marker='s', markersize=10)
|
| 56 |
axes[0].set_title(title1)
|
| 57 |
axes[0].set_axis_off()
|
| 58 |
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
def show_two_plots(gdf, name1, name2, vmin, vmax, colorscheme, title1, title2, label, split=False):
|
| 8 |
+
print('split', split)
|
| 9 |
if gdf.empty:
|
| 10 |
raise ValueError("GeoDataFrame is empty")
|
| 11 |
|
|
|
|
| 24 |
|
| 25 |
axes = [fig.add_subplot(gs[0]), fig.add_subplot(gs[1]), fig.add_subplot(gs[2]) ]
|
| 26 |
|
| 27 |
+
if split == False:
|
| 28 |
+
# First plot
|
| 29 |
+
gdf.plot(column=name1, cmap=cmap, norm=norm, ax=axes[0], marker='s', markersize=10)
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
else:
|
| 32 |
+
# Split data into three GeoDataFrames
|
| 33 |
+
# below_100 = gdf[gdf[split] <= 100]
|
| 34 |
+
above_capacity = gdf[gdf[split] > (100-gdf['%Construccion'])]
|
| 35 |
+
above_100 = gdf[gdf[split] > 100]
|
| 36 |
+
above_colors = {"capacity":"chocolate", "100":"firebrick"}
|
| 37 |
|
| 38 |
# Plot values ≤ 100 using colormap
|
| 39 |
+
gdf.plot(column=name1, cmap=colorscheme, ax=axes[0], vmin=vmin, vmax=vmax, marker='s', markersize=10)
|
| 40 |
|
| 41 |
# Plot values > capacity in orange
|
| 42 |
+
above_capacity.plot(color=above_colors['capacity'], ax=axes[0], label='> capacity due to construction', marker='x', markersize=6)
|
| 43 |
|
| 44 |
# Plot values > 100 in red
|
| 45 |
+
above_100.plot(color=above_colors['100'], ax=axes[0], label='> 100% trees', marker='x', markersize=6)
|
| 46 |
|
| 47 |
# Add legend manually for orange points
|
| 48 |
orange_patch = plt.Line2D([0], [0], marker='o', color='w', label='> capacity due to construction',
|
| 49 |
+
markerfacecolor=above_colors['capacity'], markersize=8)
|
| 50 |
|
| 51 |
# Add legend manually for red points
|
| 52 |
red_patch = plt.Line2D([0], [0], marker='o', color='w', label='> 100% trees',
|
| 53 |
+
markerfacecolor=above_colors['100'], markersize=8)
|
| 54 |
|
| 55 |
axes[0].legend(handles=[orange_patch, red_patch])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
axes[0].set_title(title1)
|
| 57 |
axes[0].set_axis_off()
|
| 58 |
|