Spaces:
Sleeping
Sleeping
Commit ·
d3b1063
1
Parent(s): 206190b
Connect model to code
Browse files- app.py +31 -16
- data_utils.py +43 -0
- plot_utils.py +2 -1
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from shiny import reactive, render, ui, App
|
|
| 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
|
| 10 |
|
| 11 |
import pandas as pd
|
| 12 |
import geopandas as gpd
|
|
@@ -21,6 +21,8 @@ import requests
|
|
| 21 |
|
| 22 |
url = "https://Projects-by-IF-model-temp-api.hf.space/model-effect"
|
| 23 |
headers = {"Content-Type": "application/json"}
|
|
|
|
|
|
|
| 24 |
data = {
|
| 25 |
"X": [[1462, -0.18, 234, 8]], "T0":[10], "T1":[15]
|
| 26 |
}
|
|
@@ -32,8 +34,9 @@ if response.ok:
|
|
| 32 |
print("Model response:", result["effect"])
|
| 33 |
else:
|
| 34 |
print("Request failed:", response.status_code, response.text)
|
|
|
|
| 35 |
|
| 36 |
-
treatment_df = pd.read_csv("example.csv", index_col=0)
|
| 37 |
og_file = "cleaned_dataframe.csv"
|
| 38 |
training_file = "training_dataframe.csv"
|
| 39 |
original_df, df, X, Y, T = prepare_data(og_file, training_file)
|
|
@@ -41,19 +44,27 @@ original_df, df, X, Y, T = prepare_data(og_file, training_file)
|
|
| 41 |
print(X)
|
| 42 |
|
| 43 |
|
| 44 |
-
ui.input_slider("trees", "Percentage increase in tree coverage", 0, 100, 20)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
app_ui = ui.page_fluid(
|
| 48 |
ui.input_radio_buttons("tab_choice", "Choose page:", ["Effect of trees on temperature", "Effect of temperature on trees"]),
|
|
|
|
| 49 |
ui.output_ui("page_ui")
|
| 50 |
)
|
| 51 |
|
| 52 |
def server(input, output, session):
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
@output
|
| 59 |
@render.ui
|
|
@@ -73,11 +84,12 @@ def server(input, output, session):
|
|
| 73 |
@output
|
| 74 |
@render.plot()
|
| 75 |
def temp_after_treatment():
|
|
|
|
|
|
|
| 76 |
try:
|
| 77 |
-
fig = show_two_plots(
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
f"Temperature after treatment (+{value}% Trees)",
|
| 81 |
"Original temperature",
|
| 82 |
'Temperature (°C)')
|
| 83 |
return fig
|
|
@@ -89,9 +101,12 @@ def server(input, output, session):
|
|
| 89 |
@output
|
| 90 |
@render.plot()
|
| 91 |
def treatment_trees_effect():
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
"Temperature decrease (°C)")
|
| 96 |
return fig
|
| 97 |
|
|
|
|
| 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
|
|
|
|
| 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 |
}
|
|
|
|
| 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)
|
|
|
|
| 44 |
print(X)
|
| 45 |
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
app_ui = ui.page_fluid(
|
| 48 |
ui.input_radio_buttons("tab_choice", "Choose page:", ["Effect of trees on temperature", "Effect of temperature on trees"]),
|
| 49 |
+
ui.output_ui("tab_slider"),
|
| 50 |
ui.output_ui("page_ui")
|
| 51 |
)
|
| 52 |
|
| 53 |
def server(input, output, session):
|
| 54 |
+
@output
|
| 55 |
+
@render.ui
|
| 56 |
+
def tab_slider():
|
| 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=5, value=1, step=0.2)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@reactive.Calc
|
| 64 |
+
def get_gdf_trees():
|
| 65 |
+
value_trees = input.tree_pct()
|
| 66 |
+
print(f"value inputed is {input.tree_pct()}")
|
| 67 |
+
return process_data_trees_to_temp(value_trees, original_df, df, X, Y, T)
|
| 68 |
|
| 69 |
@output
|
| 70 |
@render.ui
|
|
|
|
| 84 |
@output
|
| 85 |
@render.plot()
|
| 86 |
def temp_after_treatment():
|
| 87 |
+
gdf_trees = get_gdf_trees()
|
| 88 |
+
value_trees = input.tree_pct()
|
| 89 |
try:
|
| 90 |
+
fig = show_two_plots(gdf_trees, f'simulated_temp_{value_trees}%', 'LST',
|
| 91 |
+
15, 35, 'coolwarm',
|
| 92 |
+
f"Temperature after treatment (+{value_trees}% Trees)",
|
|
|
|
| 93 |
"Original temperature",
|
| 94 |
'Temperature (°C)')
|
| 95 |
return fig
|
|
|
|
| 101 |
@output
|
| 102 |
@render.plot()
|
| 103 |
def treatment_trees_effect():
|
| 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 |
|
data_utils.py
CHANGED
|
@@ -1,5 +1,11 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def make_XYT(df, treatment_col = '%CoberturaVeg', target_col = 'LST'):
|
| 5 |
|
|
@@ -25,3 +31,40 @@ def prepare_data(og_file, training_file):
|
|
| 25 |
|
| 26 |
return original_df, df, X, Y, T
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
| 3 |
+
import geopandas as gpd
|
| 4 |
+
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
url = "https://Projects-by-IF-model-temp-api.hf.space/model-effect"
|
| 8 |
+
headers = {"Content-Type": "application/json"}
|
| 9 |
|
| 10 |
def make_XYT(df, treatment_col = '%CoberturaVeg', target_col = 'LST'):
|
| 11 |
|
|
|
|
| 31 |
|
| 32 |
return original_df, df, X, Y, T
|
| 33 |
|
| 34 |
+
|
| 35 |
+
def prepare_treatment_df(original_df, df):
|
| 36 |
+
treatment_df = original_df[['x','y','%CoberturaVeg']].copy()
|
| 37 |
+
treatment_df.loc[:, '%Construccion'] = original_df['%Construccion'].values
|
| 38 |
+
treatment_df.loc[:, 'LST'] = df['LST'].values
|
| 39 |
+
|
| 40 |
+
treatment_df = treatment_df.reset_index()
|
| 41 |
+
return treatment_df
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def run_treatment_increase(X, T, original_df, df, value, name):
|
| 45 |
+
|
| 46 |
+
treatment_df = prepare_treatment_df(original_df, df)
|
| 47 |
+
T_sim = T.copy()
|
| 48 |
+
value_num = 1+ (value/100)
|
| 49 |
+
data = {"X": X.tolist(), "T0":T.tolist(), "T1":(T_sim*value_num).tolist()}
|
| 50 |
+
response = requests.post(url, headers=headers, json=data)
|
| 51 |
+
if response.ok:
|
| 52 |
+
result = response.json()
|
| 53 |
+
# print("Model response:", result["effect"])
|
| 54 |
+
else:
|
| 55 |
+
print("Request failed:", response.status_code, response.text)
|
| 56 |
+
|
| 57 |
+
treatment_df[name] = result["effect"]
|
| 58 |
+
|
| 59 |
+
return treatment_df
|
| 60 |
+
|
| 61 |
+
def process_data_trees_to_temp(value, original_df, df, X, Y, T):
|
| 62 |
+
|
| 63 |
+
name = f'treatment_effect_{value}%'
|
| 64 |
+
treatment_df = run_treatment_increase(X, T, original_df, df, value, name)
|
| 65 |
+
|
| 66 |
+
treatment_df[f'simulated_temp_{value}%'] = treatment_df['LST'] + treatment_df[name]
|
| 67 |
+
|
| 68 |
+
gdf = gpd.GeoDataFrame(treatment_df, geometry=gpd.points_from_xy(treatment_df.x, treatment_df.y))
|
| 69 |
+
|
| 70 |
+
return gdf
|
plot_utils.py
CHANGED
|
@@ -83,8 +83,9 @@ def show_one_plot(gdf, name, vmin, vmax, colorscheme, title, label):
|
|
| 83 |
|
| 84 |
axes = [fig.add_subplot(gs[0]), fig.add_subplot(gs[1])]
|
| 85 |
|
|
|
|
| 86 |
# First plot (simulated)
|
| 87 |
-
gdf.plot(column=name, cmap=cmap, ax=axes[0], marker='s', markersize=10)
|
| 88 |
axes[0].set_title(title)
|
| 89 |
axes[0].set_axis_off()
|
| 90 |
|
|
|
|
| 83 |
|
| 84 |
axes = [fig.add_subplot(gs[0]), fig.add_subplot(gs[1])]
|
| 85 |
|
| 86 |
+
print(gdf[name].min())
|
| 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 |
|