File size: 4,809 Bytes
164d1c0
 
d3b1063
 
 
 
990b611
d3b1063
164d1c0
0498f42
 
d238bb5
164d1c0
 
 
 
 
 
 
 
 
 
e301a24
164d1c0
e301a24
164d1c0
 
 
e301a24
d238bb5
164d1c0
 
 
 
 
 
d3b1063
 
d238bb5
 
d3b1063
 
 
 
 
 
e301a24
d3b1063
 
e301a24
 
 
 
 
 
 
731c0dd
d3b1063
 
 
 
 
 
 
 
aa615b0
 
d3b1063
 
 
e301a24
 
 
d3b1063
e301a24
d3b1063
0498f42
e301a24
aa615b0
e301a24
d238bb5
aa615b0
e301a24
 
0498f42
d3b1063
 
b36c6ed
 
 
e301a24
b36c6ed
 
 
 
 
 
 
 
 
 
e301a24
 
 
 
 
 
b36c6ed
e301a24
 
b36c6ed
 
e301a24
b36c6ed
e301a24
 
b36c6ed
aa615b0
0498f42
 
aa615b0
e301a24
 
d238bb5
aa615b0
e301a24
 
b36c6ed
 
 
 
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
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