Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import numpy as np | |
| import geopandas as gpd | |
| import requests | |
| url = "https://Projects-by-IF-model-temp-api.hf.space/model-effect" | |
| headers = {"Content-Type": "application/json"} | |
| cell_area = 100*100 #in square meters | |
| def make_XYT(df, treatment_col = 'Pct_CoberturaVeg', target_col = 'LST'): | |
| Y = df[target_col].values | |
| T = df[treatment_col].values | |
| X = df.drop(columns=[treatment_col, target_col]).values | |
| return X, Y, T | |
| def prepare_data(og_file): | |
| original_df = pd.read_csv(og_file, index_col=0) | |
| # df = pd.read_csv(training_file, index_col=0) | |
| original_df = original_df.round(2) | |
| df = original_df[['Elevacion', 'Neighbor_NDBI', 'Proximidad_agua', | |
| 'Neighbor_%CoberturaVeg', 'Pct_CoberturaVeg', 'LST']].copy() | |
| df = df.astype(np.float32) | |
| X, Y, T = make_XYT(df) | |
| return original_df, df, X, Y, T | |
| def prepare_treatment_df(original_df, df): | |
| treatment_df = original_df[['x','y','Pct_CoberturaVeg']].copy() | |
| treatment_df.loc[:, 'Pct_Construccion'] = original_df['Pct_Construccion'].values | |
| treatment_df.loc[:, 'LST'] = df['LST'].values | |
| treatment_df = treatment_df.reset_index() | |
| return treatment_df | |
| def run_treatment_increase(X, T, original_df, df, value, name, simulation_mode): | |
| treatment_df = prepare_treatment_df(original_df, df) | |
| if simulation_mode == "Usar control deslizante": | |
| T_sim = T.copy() | |
| value_num = 1+ (value/100) | |
| T_sim = T_sim*value_num | |
| else: | |
| T_sim = original_df['simulacion_arboles'] | |
| data = {"X": X.tolist(), "T0":T.tolist(), "T1":(T_sim).tolist()} | |
| response = requests.post(url, headers=headers, json=data) | |
| if response.ok: | |
| result = response.json() | |
| # print("Model response:", result["effect"]) | |
| else: | |
| print("Request failed:", response.status_code, response.text) | |
| treatment_df[name] = result["effect"] | |
| treatment_df['pct_total_trees'] = T_sim | |
| treatment_df['pp_trees_increase'] = treatment_df['pct_total_trees'] - treatment_df['Pct_CoberturaVeg'] | |
| return treatment_df | |
| def process_data_trees_to_temp(value, simulation_mode, original_df, df, X, Y, T): | |
| name = 'temp_decrease' | |
| treatment_df = run_treatment_increase(X, T, original_df, df, value, name, simulation_mode) | |
| treatment_df['new_temp'] = treatment_df['LST'] + treatment_df[name] | |
| #add columns translated to square meters | |
| treatment_df['sqm_trees_increase'] = (treatment_df['pp_trees_increase']/100)*cell_area | |
| treatment_df['sqm_total_trees'] = (treatment_df['pct_total_trees']/100)*cell_area | |
| col_order = ['index', 'x', 'y', 'Pct_CoberturaVeg', 'Pct_Construccion', 'LST','temp_decrease', | |
| 'new_temp', 'pp_trees_increase','pct_total_trees', 'sqm_trees_increase','sqm_total_trees',] # to match gdf trees | |
| treatment_df = treatment_df[col_order] | |
| gdf = gpd.GeoDataFrame(treatment_df, geometry=gpd.points_from_xy(treatment_df.x, treatment_df.y)) | |
| return gdf | |
| def run_temp_decrease(X, original_df, df, goal, name,simulation_mode): | |
| treatment_df = prepare_treatment_df(original_df, df) | |
| data = {"X": X.tolist()} | |
| response = requests.post(url, headers=headers, json=data) | |
| if response.ok: | |
| result = response.json() | |
| # print("Model response:", result["effect"]) | |
| else: | |
| print("Request failed:", response.status_code, response.text) | |
| if simulation_mode == "Usar control deslizante": | |
| treatment_df['temp_decrease'] = -goal | |
| treatment_df['new_temp'] = treatment_df['LST'] + treatment_df['temp_decrease'] | |
| else: | |
| treatment_df['new_temp'] = original_df['simulacion_temp'] | |
| treatment_df['temp_decrease'] = treatment_df['new_temp'] - treatment_df['LST'] | |
| treatment_effects = np.array(result["effect"]) | |
| treatment_df[name] = treatment_df['temp_decrease'] / treatment_effects | |
| return treatment_df | |
| def process_data_temp_to_trees(goal, simulation_mode, original_df, df, X, Y, T): | |
| name = 'pp_trees_increase' | |
| treatment_df = run_temp_decrease(X, original_df, df, goal, name,simulation_mode) | |
| treatment_df['pct_total_trees'] = treatment_df['Pct_CoberturaVeg'] + treatment_df[name] | |
| #add columns translated to square meters | |
| treatment_df['sqm_total_trees'] = (treatment_df['pct_total_trees']/100)*cell_area | |
| treatment_df['sqm_trees_increase'] = (treatment_df['pp_trees_increase']/100)*cell_area | |
| col_order = ['index', 'x', 'y', 'Pct_CoberturaVeg', 'Pct_Construccion', 'LST','temp_decrease', | |
| 'new_temp', 'pp_trees_increase','pct_total_trees', 'sqm_trees_increase','sqm_total_trees',] # to match gdf trees | |
| treatment_df = treatment_df[col_order] | |
| gdf = gpd.GeoDataFrame(treatment_df, geometry=gpd.points_from_xy(treatment_df.x, treatment_df.y)) | |
| return gdf | |