Spaces:
Sleeping
Sleeping
Commit ·
b36c6ed
1
Parent(s): d3b1063
Add effect of temp change on trees
Browse files- app.py +45 -35
- data_utils.py +31 -1
- plot_utils.py +2 -4
app.py
CHANGED
|
@@ -2,41 +2,15 @@
|
|
| 2 |
|
| 3 |
import os
|
| 4 |
from shiny import reactive, render, ui, App
|
| 5 |
-
# from shiny.express import input, render, ui
|
| 6 |
-
from shinywidgets import render_widget
|
| 7 |
|
| 8 |
from plot_utils import show_one_plot, show_two_plots
|
| 9 |
-
from data_utils import prepare_data, process_data_trees_to_temp
|
| 10 |
|
| 11 |
import pandas as pd
|
| 12 |
import geopandas as gpd
|
| 13 |
|
| 14 |
-
# import matplotlib.pyplot as plt
|
| 15 |
-
# import matplotlib.colors as colors
|
| 16 |
-
# import matplotlib.cm as cm
|
| 17 |
-
# import matplotlib.gridspec as gridspec
|
| 18 |
import numpy as np
|
| 19 |
|
| 20 |
-
import requests
|
| 21 |
-
|
| 22 |
-
url = "https://Projects-by-IF-model-temp-api.hf.space/model-effect"
|
| 23 |
-
headers = {"Content-Type": "application/json"}
|
| 24 |
-
|
| 25 |
-
####test####
|
| 26 |
-
data = {
|
| 27 |
-
"X": [[1462, -0.18, 234, 8]], "T0":[10], "T1":[15]
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
response = requests.post(url, headers=headers, json=data)
|
| 31 |
-
|
| 32 |
-
if response.ok:
|
| 33 |
-
result = response.json()
|
| 34 |
-
print("Model response:", result["effect"])
|
| 35 |
-
else:
|
| 36 |
-
print("Request failed:", response.status_code, response.text)
|
| 37 |
-
#######
|
| 38 |
-
|
| 39 |
-
# treatment_df = pd.read_csv("example.csv", index_col=0)
|
| 40 |
og_file = "cleaned_dataframe.csv"
|
| 41 |
training_file = "training_dataframe.csv"
|
| 42 |
original_df, df, X, Y, T = prepare_data(og_file, training_file)
|
|
@@ -57,14 +31,20 @@ def server(input, output, session):
|
|
| 57 |
if input.tab_choice() == "Effect of trees on temperature":
|
| 58 |
return ui.input_slider("tree_pct", "Tree Increase (%)", min=0, max=100, value=40, step=5)
|
| 59 |
else: # "Effect of temperature on trees"
|
| 60 |
-
return ui.input_slider("temp_goal", "Temperature Decrease (°C)", min=0, max=
|
| 61 |
|
| 62 |
|
| 63 |
@reactive.Calc
|
| 64 |
def get_gdf_trees():
|
| 65 |
value_trees = input.tree_pct()
|
| 66 |
-
print(f"value inputed is {
|
| 67 |
return process_data_trees_to_temp(value_trees, original_df, df, X, Y, T)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
@output
|
| 70 |
@render.ui
|
|
@@ -73,12 +53,13 @@ def server(input, output, session):
|
|
| 73 |
return ui.div(
|
| 74 |
ui.h2("Temperature Simulation Viewer", class_="text-center"),
|
| 75 |
ui.div(ui.output_plot("temp_after_treatment"), style="display: flex; justify-content: center;"),
|
| 76 |
-
ui.div(ui.output_plot("
|
| 77 |
)
|
| 78 |
else: #Effect of temperature on trees
|
| 79 |
return ui.div(
|
| 80 |
ui.h2("Tree Simulation Viewer", class_="text-center"),
|
| 81 |
-
ui.div(ui.output_plot("
|
|
|
|
| 82 |
)
|
| 83 |
|
| 84 |
@output
|
|
@@ -100,15 +81,44 @@ def server(input, output, session):
|
|
| 100 |
|
| 101 |
@output
|
| 102 |
@render.plot()
|
| 103 |
-
def
|
| 104 |
gdf_trees = get_gdf_trees()
|
| 105 |
value_trees = input.tree_pct()
|
| 106 |
-
print(value_trees)
|
| 107 |
-
print(gdf_trees)
|
| 108 |
fig = show_one_plot(gdf_trees, f'treatment_effect_{value_trees}%', -2, 0, 'PuBu_r',
|
| 109 |
f"Decrease of temperature from treatment (+{value_trees}% Trees)",
|
| 110 |
"Temperature decrease (°C)")
|
| 111 |
return fig
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
app = App(app_ui, server)
|
|
|
|
| 2 |
|
| 3 |
import os
|
| 4 |
from shiny import reactive, render, ui, App
|
|
|
|
|
|
|
| 5 |
|
| 6 |
from plot_utils import show_one_plot, show_two_plots
|
| 7 |
+
from data_utils import prepare_data, process_data_trees_to_temp, process_data_temp_to_trees
|
| 8 |
|
| 9 |
import pandas as pd
|
| 10 |
import geopandas as gpd
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
import numpy as np
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
og_file = "cleaned_dataframe.csv"
|
| 15 |
training_file = "training_dataframe.csv"
|
| 16 |
original_df, df, X, Y, T = prepare_data(og_file, training_file)
|
|
|
|
| 31 |
if input.tab_choice() == "Effect of trees on temperature":
|
| 32 |
return ui.input_slider("tree_pct", "Tree Increase (%)", min=0, max=100, value=40, step=5)
|
| 33 |
else: # "Effect of temperature on trees"
|
| 34 |
+
return ui.input_slider("temp_goal", "Temperature Decrease (°C)", min=0, max=2, value=0.5, step=0.1)
|
| 35 |
|
| 36 |
|
| 37 |
@reactive.Calc
|
| 38 |
def get_gdf_trees():
|
| 39 |
value_trees = input.tree_pct()
|
| 40 |
+
print(f"value inputed is {value_trees}")
|
| 41 |
return process_data_trees_to_temp(value_trees, original_df, df, X, Y, T)
|
| 42 |
+
|
| 43 |
+
@reactive.Calc
|
| 44 |
+
def get_gdf_temp():
|
| 45 |
+
value_temp = input.temp_goal()
|
| 46 |
+
print(f"value inputed is {value_temp}")
|
| 47 |
+
return process_data_temp_to_trees(value_temp, original_df, df, X, Y, T)
|
| 48 |
|
| 49 |
@output
|
| 50 |
@render.ui
|
|
|
|
| 53 |
return ui.div(
|
| 54 |
ui.h2("Temperature Simulation Viewer", 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("Tree Simulation Viewer", 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 |
)
|
| 64 |
|
| 65 |
@output
|
|
|
|
| 81 |
|
| 82 |
@output
|
| 83 |
@render.plot()
|
| 84 |
+
def temp_change():
|
| 85 |
gdf_trees = get_gdf_trees()
|
| 86 |
value_trees = input.tree_pct()
|
|
|
|
|
|
|
| 87 |
fig = show_one_plot(gdf_trees, f'treatment_effect_{value_trees}%', -2, 0, 'PuBu_r',
|
| 88 |
f"Decrease of temperature from treatment (+{value_trees}% Trees)",
|
| 89 |
"Temperature decrease (°C)")
|
| 90 |
return fig
|
| 91 |
|
| 92 |
+
@output
|
| 93 |
+
@render.plot()
|
| 94 |
+
def trees_after_decrease():
|
| 95 |
+
gdf_temp = get_gdf_temp()
|
| 96 |
+
value_temp = input.temp_goal()
|
| 97 |
+
min_cobertura_veg = 5
|
| 98 |
+
max_cobertura = 100
|
| 99 |
+
try:
|
| 100 |
+
fig = show_two_plots(gdf_temp, f'total_trees_needed_for_{value_temp}C', '%CoberturaVeg',
|
| 101 |
+
min_cobertura_veg, max_cobertura, 'YlGn',
|
| 102 |
+
f"Tree coverage needed for {value_temp}°C decrease","Original tree canopy coverage",
|
| 103 |
+
"Tree canopy coverage (%)", split=True)
|
| 104 |
+
return fig
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print("Plotting error:", e)
|
| 107 |
+
raise e
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
@output
|
| 111 |
+
@render.plot()
|
| 112 |
+
def tree_coverage_change():
|
| 113 |
+
gdf_temp = get_gdf_temp()
|
| 114 |
+
value_temp = input.temp_goal()
|
| 115 |
+
min_tree_increase = 0
|
| 116 |
+
max_tree_increase = 90
|
| 117 |
+
fig = show_one_plot(gdf_temp, f'pp_trees_increase_for_{value_temp}C', min_tree_increase,
|
| 118 |
+
max_tree_increase, 'Greens',
|
| 119 |
+
f"Increase in tree coverage for {value_temp}°C decrease",
|
| 120 |
+
"Tree canopy coverage increase in %pt")
|
| 121 |
+
return fig
|
| 122 |
+
|
| 123 |
+
app = App(app_ui, server)
|
| 124 |
|
|
|
data_utils.py
CHANGED
|
@@ -67,4 +67,34 @@ def process_data_trees_to_temp(value, original_df, df, X, Y, T):
|
|
| 67 |
|
| 68 |
gdf = gpd.GeoDataFrame(treatment_df, geometry=gpd.points_from_xy(treatment_df.x, treatment_df.y))
|
| 69 |
|
| 70 |
-
return gdf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
gdf = gpd.GeoDataFrame(treatment_df, geometry=gpd.points_from_xy(treatment_df.x, treatment_df.y))
|
| 69 |
|
| 70 |
+
return gdf
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def run_temp_decrease(X, original_df, df, goal, name):
|
| 74 |
+
|
| 75 |
+
treatment_df = prepare_treatment_df(original_df, df)
|
| 76 |
+
data = {"X": X.tolist()}
|
| 77 |
+
response = requests.post(url, headers=headers, json=data)
|
| 78 |
+
if response.ok:
|
| 79 |
+
result = response.json()
|
| 80 |
+
# print("Model response:", result["effect"])
|
| 81 |
+
else:
|
| 82 |
+
print("Request failed:", response.status_code, response.text)
|
| 83 |
+
|
| 84 |
+
treatment_effects = np.array(result["effect"])
|
| 85 |
+
|
| 86 |
+
trees_needed = -goal / treatment_effects
|
| 87 |
+
|
| 88 |
+
treatment_df[name] = trees_needed
|
| 89 |
+
|
| 90 |
+
return treatment_df
|
| 91 |
+
def process_data_temp_to_trees(goal, original_df, df, X, Y, T):
|
| 92 |
+
|
| 93 |
+
name = f'pp_trees_increase_for_{goal}C'
|
| 94 |
+
treatment_df = run_temp_decrease(X,original_df, df, goal, name)
|
| 95 |
+
|
| 96 |
+
treatment_df[f'total_trees_needed_for_{goal}C'] = treatment_df['%CoberturaVeg'] + treatment_df[name]
|
| 97 |
+
|
| 98 |
+
gdf = gpd.GeoDataFrame(treatment_df, geometry=gpd.points_from_xy(treatment_df.x, treatment_df.y))
|
| 99 |
+
|
| 100 |
+
return gdf
|
plot_utils.py
CHANGED
|
@@ -77,19 +77,17 @@ def show_one_plot(gdf, name, vmin, vmax, colorscheme, title, label):
|
|
| 77 |
norm = colors.Normalize(vmin, vmax)
|
| 78 |
|
| 79 |
# Create subplots
|
| 80 |
-
# fig, axes = plt.subplots(1, 1, figsize=(6, 6))
|
| 81 |
fig = plt.figure(figsize=(12, 6))
|
| 82 |
gs = gridspec.GridSpec(1, 2, width_ratios=[1, 0.05], wspace=0.3)
|
| 83 |
|
| 84 |
axes = [fig.add_subplot(gs[0]), fig.add_subplot(gs[1])]
|
| 85 |
|
| 86 |
-
|
| 87 |
-
# First plot (simulated)
|
| 88 |
gdf.plot(column=name, cmap=cmap, norm=norm, ax=axes[0], marker='s', markersize=10)
|
| 89 |
axes[0].set_title(title)
|
| 90 |
axes[0].set_axis_off()
|
| 91 |
|
| 92 |
-
|
| 93 |
sm = cm.ScalarMappable(cmap=cmap, norm=norm)
|
| 94 |
sm._A = [] # Dummy data for the colorbar
|
| 95 |
cbar = fig.colorbar(sm, cax=axes[1], orientation='vertical', fraction=0.03, pad=0.02)
|
|
|
|
| 77 |
norm = colors.Normalize(vmin, vmax)
|
| 78 |
|
| 79 |
# Create subplots
|
|
|
|
| 80 |
fig = plt.figure(figsize=(12, 6))
|
| 81 |
gs = gridspec.GridSpec(1, 2, width_ratios=[1, 0.05], wspace=0.3)
|
| 82 |
|
| 83 |
axes = [fig.add_subplot(gs[0]), fig.add_subplot(gs[1])]
|
| 84 |
|
| 85 |
+
|
|
|
|
| 86 |
gdf.plot(column=name, cmap=cmap, norm=norm, ax=axes[0], marker='s', markersize=10)
|
| 87 |
axes[0].set_title(title)
|
| 88 |
axes[0].set_axis_off()
|
| 89 |
|
| 90 |
+
# Add a colorbar
|
| 91 |
sm = cm.ScalarMappable(cmap=cmap, norm=norm)
|
| 92 |
sm._A = [] # Dummy data for the colorbar
|
| 93 |
cbar = fig.colorbar(sm, cax=axes[1], orientation='vertical', fraction=0.03, pad=0.02)
|