-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpredict_missing_gvi.py
More file actions
196 lines (142 loc) · 6.88 KB
/
predict_missing_gvi.py
File metadata and controls
196 lines (142 loc) · 6.88 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LinearRegression
from pygam import LinearGAM, s
import geopandas as gpd
import pandas as pd
import numpy as np
import rasterio
import sys
import os
# Function to calculate mean NDVI taken from Yúri Grings' GitHub repository
# https://github.com/Spatial-Data-Science-and-GEO-AI-Lab/GreenEx_Py
from modules.availability import get_mean_NDVI
def calculate_ndvi(gvi, ndvi, N, city, crs):
ndvi_folder = os.path.join("results", city, "ndvi")
mean_ndvi = get_mean_NDVI( point_of_interest_file=gvi,
ndvi_raster_file = ndvi,
buffer_type="euclidean",
buffer_dist=N,
crs_epsg=crs,
write_to_file=False,
save_ndvi=False)
# Save the calculated NDVI values to a file
mean_ndvi.to_crs(crs=4326, inplace=True)
path_to_file = os.path.join(ndvi_folder, "calculated_ndvi_values.gpkg")
mean_ndvi.to_file(path_to_file, driver="GPKG", crs=4326)
return path_to_file
def linear_regression(city):
ndvi_folder = os.path.join("results", city, "ndvi")
# Load ndvi layer
ndvi_file = os.path.join(ndvi_folder, "calculated_ndvi_values.gpkg")
ndvi_df = gpd.read_file(ndvi_file, layer="calculated_ndvi_values", crs=4326)
# Separate data into known and missing GVI values
known_df = ndvi_df[ndvi_df['missing'] == False].copy()
missing_df = ndvi_df[ndvi_df['missing'] == True].copy()
# Split known data into features (NDVI) and target (GVI)
X_train = known_df[['mean_NDVI']]
y_train = known_df['GVI']
# Prepare missing data for prediction
X_test = missing_df[['mean_NDVI']]
# Perform linear regression
lin_reg = LinearRegression()
lin_reg.fit(X_train, y_train)
predicted_GVI = lin_reg.predict(X_test)
# Assign the predicted values to the missing GVI values in the DataFrame
missing_df['GVI'] = predicted_GVI
# Concatenate the updated missing values with the known values
updated_df = pd.concat([known_df, missing_df])
path_to_file = os.path.join(ndvi_folder, "calculated_missing_values_linreg.gpkg")
updated_df.to_file(path_to_file, driver="GPKG", crs=4326)
# Compute RMSE using cross-validation
rmse_scores = np.sqrt(-cross_val_score(lin_reg, X_train, y_train, scoring='neg_mean_squared_error', cv=5))
avg_rmse = np.mean(rmse_scores)
# Compute R2 score using cross-validation
r2_scores = cross_val_score(lin_reg, X_train, y_train, scoring='r2', cv=5)
avg_r2 = np.mean(r2_scores)
# Get the number of parameters (including intercept)
k = X_train.shape[1] + 1
n = len(y_train) # number of samples
# Calculate the AIC
aic = n * np.log(avg_rmse ** 2) + 2 * k
print("<----- Linear Regression ----->")
print("R2 value:", avg_r2)
print("RMSE:", avg_rmse)
print("AIC value:", aic)
return updated_df
def gam_regression(city):
ndvi_folder = os.path.join("results", city, "ndvi")
# Load ndvi layer
ndvi_file = os.path.join(ndvi_folder, "calculated_ndvi_values.gpkg")
ndvi_df = gpd.read_file(ndvi_file, layer="calculated_ndvi_values", crs=4326)
# Separate data into known and missing GVI values
known_df = ndvi_df[ndvi_df['missing'] == False].copy()
missing_df = ndvi_df[ndvi_df['missing'] == True].copy()
# Split known data into features (NDVI) and target (GVI)
X_train = known_df[['mean_NDVI']]
y_train = known_df['GVI']
# Prepare missing data for prediction
X_test = missing_df[['mean_NDVI']]
n_features = 1 # number of features used in the model
lams = np.logspace(-5, 5, 20) * n_features
splines = 25
# Train a Generalized Additive Model (GAM)
gam = LinearGAM(
s(0, n_splines=splines)).gridsearch(
X_train.values,
y_train.values,
lam=lams
)
predicted_GVI = gam.predict(X_test.values)
# Assign the predicted values to the missing GVI values in the DataFrame
missing_df['GVI'] = predicted_GVI
# Concatenate the updated missing values with the known values
updated_df = pd.concat([known_df, missing_df])
path_to_file= os.path.join(ndvi_folder, "calculated_missing_values_gam.gpkg")
updated_df.to_file(path_to_file, driver="GPKG", crs=4326)
# Compute RMSE using cross-validation
rmse_scores = np.sqrt(-cross_val_score(gam, X_train, y_train, scoring='neg_mean_squared_error', cv=5))
avg_rmse = np.mean(rmse_scores)
# Get the number of parameters (including intercept)
k = X_train.shape[1] + 1
n = len(y_train) # number of samples
# Calculate the AIC
aic = n * np.log(avg_rmse ** 2) + 2 * k
print("<----- Linear GAM ----->")
print("RMSE:", avg_rmse)
print("AIC value:", aic)
return updated_df
def clean_points(city, crs):
# Cleans the GVI points data by dropping points outside the extent of the NDVI file.
# File paths for the GVI points and NDVI files
# The NDVI file has to be stored in results/city/ndvi folder and has to be named ndvi.tif
gvi = os.path.join("results", city, "gvi", "gvi-points.gpkg")
ndvi = os.path.join("results", city, "ndvi", f"ndvi.tif")
gvi_df = gpd.read_file(gvi, layer="gvi-points", crs=4326)
gvi_df.to_crs(epsg=crs, inplace=True)
# Get the extent of the NDVI file
with rasterio.open(ndvi) as src:
extent = src.bounds
# Filter the GVI points to include only those within the extent of the NDVI file
filtered_gvi = gvi_df.cx[extent[0]:extent[2], extent[1]:extent[3]]
# Save the filtered GVI points to a new file to preserve the original data
filtered_gvi_path = os.path.join("results", city, "ndvi", "filtered-points.gpkg")
filtered_gvi.to_file(filtered_gvi_path, driver="GPKG", crs=crs)
return filtered_gvi_path, ndvi
if __name__ == "__main__":
# Read command-line arguments
args = sys.argv
# Extract city, CRS, and distance from the command-line arguments
city = args[1] # City to analyze
ndvi_file_exists = bool(int(args[2])) # Indicates if we already have the NDVI values
if not ndvi_file_exists:
crs = int(args[3]) # CRS in meters, suitable for the area in which we are working
# For example, we can use the same CRS as the roads.gpkg file
# IMPORTANT: The NDVI image should be in this CRS
distance = int(args[4]) # The distance used to generate the sample points
# Step 1: Clean the GVI points by filtering points outside the extent of the NDVI file
gvi, ndvi = clean_points(city, crs)
# Step 2: Calculate the mean NDVI values from the filtered GVI points
ndvi_path = calculate_ndvi(gvi, ndvi, distance//2, city, crs)
# Step 3: Train a Linear Regression model to predict missing GVI values
linreg = linear_regression(city)
lingam = gam_regression(city)