-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
261 lines (216 loc) · 12.1 KB
/
main.py
File metadata and controls
261 lines (216 loc) · 12.1 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import pandas as pd
import numpy as np
from datetime import datetime
import models
import os
import utilis
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
import plotly.io as pio
def invTransform(scaler, data, colName, colNames):
dummy = pd.DataFrame(np.zeros((len(data), len(colNames))), columns=colNames)
dummy[colName] = data
dummy = pd.DataFrame(scaler.inverse_transform(dummy), columns=colNames)
return dummy[colName].values
# command for vscode to view all the data at terminal
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)
pd.set_option('display.width', None)
sample = 'box2' # Can be 'box1' or 'box2', please ensure depth is zero for box1
depth = 1.7 # depth is in meters can be 0, 0.6, 0.9, or 1.7
net_rainfall = False # Can be True or False
def main():
model_name = 'PROPHET'
norm = True
cross_valid = False
print('Model:', model_name)
print('Sample:', sample)
print('Normalization:', norm)
print('Cross Validation:', cross_valid)
print('Depth:', depth)
print('Net Rainfall:', net_rainfall)
np.random.seed(123)
scaler = MinMaxScaler()
# Set file path and sheet name based on the sample
if sample == 'box1':
if net_rainfall:
excel_file_path = "./data/box1_net_rainfall.xlsx"
target_sheet_name = 'Sheet1'
regressors = ['Temperature', 'Rainfall']
else:
excel_file_path = "./data/box1.xlsx"
target_sheet_name = 'Sheet1'
regressors = ['Temperature', 'Rainfall']
# regressors = ['Temperature']
elif sample == 'box2':
if depth == 0.6:
excel_file_path = "./data/box2_depth6.xlsx"
target_sheet_name = 'Sheet1'
regressors = ['Soil Temperature', 'Water Content', 'Bulk EC']
elif depth == 0.9:
excel_file_path = "./data/box2_depth9.xlsx"
target_sheet_name = 'Sheet1'
regressors = ['Soil Temperature', 'Water Content', 'Bulk EC']
elif depth == 1.7:
excel_file_path = "./data/box2_depth17.xlsx"
target_sheet_name = 'Sheet1'
regressors = ['Soil Temperature', 'Water Content', 'Bulk EC']
else:
raise ValueError("Invalid depth for box2. Must be one of [0.6, 0.9, 1.7]")
else:
raise ValueError("Invalid sample. Must be 'box1' or 'box2'.")
# Read data from the specified sheet
df = pd.read_excel( excel_file_path, sheet_name=target_sheet_name, engine='openpyxl', nrows=620)
df['ds'] = pd.to_datetime(df['ds'])
df = df.ffill()
plt.plot(df['ds'], df['y'])
plt.show()
caos = np.random.randint(100, size=(len(df)))
# Select the appropriate columns for training based on the model
if model_name in ['PROPHET', 'NEURALPROPHET']:
dftraining = df[['ds', 'y'] + regressors].copy()
else:
dftraining = df[['y'] + regressors].copy()
sz = round(len(df)/100*10)
dftrain = dftraining[:- sz].copy()
dftest = dftraining[-sz:].copy()
# shiftday = len(dftrain) // 10
shiftday = len(dftest)
if(norm):
if model_name in ['ARIMA','SARIMAX']:
scaler.fit(dftraining.iloc[:])
dftrain.iloc[:] = scaler.transform(dftrain.iloc[:]).copy()
dftest.iloc[:] = scaler.transform(dftest.iloc[:]).copy()
else:
scaler.fit(dftraining.iloc[:,1:])
dftrain.iloc[:,1:] = scaler.transform(dftrain.iloc[:,1:]).copy()
dftest.iloc[:,1:] = scaler.transform(dftest.iloc[:,1:]).copy()
if sample == 'box1':
# correlation_table = models.correlation(dftraining[['y','Temperature', 'Rainfall']])
correlation_table = models.correlation(dftraining[['y','Temperature']])
elif sample == 'box2':
correlation_table = models.correlation(dftraining[['y','Soil Temperature', 'Bulk EC', 'Water Content']])
utilis.instance_path.set_path(
os.path.join(os.path.dirname(__file__), 'output', model_name),
'tuning_' + model_name + 'sampel_' + sample + 'netrainfall_' + str(net_rainfall) + 'depth_' + str(depth) + '_norm_' + str(norm) + '_cross_valid_' + str(cross_valid))
if cross_valid:
if model_name == 'ARIMA':
[best_params, best_model_df, hyper_parameter_table_results_list] = models.arima_tuning_workflow(None, dftrain, shiftday=shiftday)
elif model_name == 'SARIMAX':
[best_params, best_model_df, hyper_parameter_table_results_list] = models.sarimax_tuning_workflow(None, dftrain, shiftday=shiftday)
elif model_name == 'PROPHET':
[best_params, best_model_df, hyper_parameter_table_results_list] = models.prophet_tuning_workflow(None, dftrain, shiftday=shiftday)
elif model_name == 'NEURALPROPHET':
[best_params, best_model_df, hyper_parameter_table_results_list] = models.neuralprophet_tuning_workflow([], dftrain, shiftday=shiftday)
utilis.plot_flow(correlation_table, tuning = True, best_params=best_params, best_model=best_model_df, hyper_parameter_table_results=hyper_parameter_table_results_list)
else:
if model_name == 'ARIMA':
model_obj = models.model_arima(dftrain, test_set=dftest)
model_obj.train()
forecast = model_obj.forecast(dftest)
forecast.rename(columns={'yhat': 'y'})
if model_name == 'SARIMAX':
model_obj = models.model_sarimax(dftrain, test_set=dftest)
model_obj.train()
forecast = model_obj.forecast(dftest)
forecast.rename(columns={'yhat': 'y'})
elif model_name == 'PROPHET':
model_obj = models.model_prophet(dftrain, test_set=dftest)
if sample == 'box1':
model_obj.add_regressor('Temperature', models.hyper_parameters.regressor_weight['Temperature'], 'additive')
model_obj.add_regressor('Rainfall', models.hyper_parameters.regressor_weight['Rainfall'], 'additive')
elif sample == 'box2':
model_obj.add_regressor('Soil Temperature', models.hyper_parameters.regressor_weight['Soil Temperature'], 'additive')
model_obj.add_regressor('Water Content', models.hyper_parameters.regressor_weight['Water Content'], 'additive')
model_obj.add_regressor('Bulk EC', models.hyper_parameters.regressor_weight['Bulk EC'], 'additive')
model_obj.train()
future = dftest.copy()
future['y'] = np.nan
forecast = model_obj.forecast(future)
elif model_name == 'NEURALPROPHET':
validation_split_index = int(len(dftrain) * 0.9)
train_initial = dftrain.copy()
train_end = dftest.copy()
if sample == 'box1':
model_obj = models.model_neuralprophet(
train_set=train_initial,
validation_set=train_end,
test_set=None,
kwargs=models.hyper_parameters.get_neuralprophet_hyperparameters()
).add_regressor(
'Temperature', models.hyper_parameters.regressor_weight['Temperature'], 'additive'
)
model_obj.train()
regressors = dftest.copy()
regressors['y'] = pd.Series([float('nan')] * len(dftest))
future = model_obj.get_model().make_future_dataframe(
train_initial, periods=len(dftest), regressors_df=regressors, n_historic_predictions=False
)
forecast = model_obj.forecast(future)
forecast = models.exctract_yhat(forecast, size=len(dftest))
elif sample == 'box2':
model_obj = models.model_neuralprophet(
train_set=train_initial,
validation_set=train_end,
test_set=None,
kwargs=models.hyper_parameters.get_neuralprophet_hyperparameters()
).add_regressor(
'Soil Temperature', models.hyper_parameters.regressor_weight['Soil Temperature'], 'additive'
).add_regressor(
'Water Content', models.hyper_parameters.regressor_weight['Water Content'], 'additive'
).add_regressor(
'Bulk EC', models.hyper_parameters.regressor_weight['Bulk EC'], 'additive'
)
model_obj.train()
regressors = dftest.copy()
regressors['y'] = pd.Series([float('nan')] * len(dftest))
future = model_obj.get_model().make_future_dataframe(
train_initial, periods=len(dftest), regressors_df=regressors, n_historic_predictions=False
)
forecast = model_obj.forecast(future)
forecast = models.exctract_yhat(forecast, len(dftest))
if norm:
if model_name in ['ARIMA', 'SARIMAX']:
dftrain.iloc[:, :] = scaler.inverse_transform(dftrain.iloc[:, :]).copy()
dftest.iloc[:, :] = scaler.inverse_transform(dftest.iloc[:, :]).copy()
forecast.iloc[:, 1:] = scaler.inverse_transform(forecast.iloc[:, 1:]).copy()
elif model_name in ['PROPHET']:
dftrain.iloc[:,1:] = scaler.inverse_transform(dftrain.iloc[:,1:]).copy()
dftest.iloc[:,1:] = scaler.inverse_transform(dftest.iloc[:,1:]).copy()
forecast['yhat'] = invTransform(scaler, forecast['yhat'],'y',scaler.feature_names_in_)
forecast['yhat_lower']=invTransform(scaler, forecast['yhat_lower'],'y',scaler.feature_names_in_)
forecast['yhat_upper']=invTransform(scaler, forecast['yhat_upper'],'y',scaler.feature_names_in_)
elif model_name in ['NEURALPROPHET']:
dftrain.iloc[:,1:] = scaler.inverse_transform(dftrain.iloc[:,1:]).copy()
dftest.iloc[:,1:] = scaler.inverse_transform(dftest.iloc[:,1:]).copy()
forecast['yhat1'] = invTransform(scaler, forecast['yhat1'],'y',scaler.feature_names_in_)
if model_name in ['ARIMA','SARIMAX']:
dftrain['ds'] = df['ds'].iloc[:len(dftrain)]
dftest['ds'] = df['ds'].iloc[len(dftrain):]
forecast.index = dftest.index
forecast['ds'] = dftest['ds'].values
utilis.plot_flow(correlation_table, tuning = False, forecast = forecast, dftraining = dftrain, dftest = dftest, model_name = model_name.lower(), shift_data=False, model = model_obj)
namefolder = utilis.instance_path.get_path()
if model_name in ['ARIMA','SARIMAX']:
forecast.to_excel(f'{namefolder}/selected_forecast_data.xlsx', index=False)
elif model_name in ['PROPHET']:
if sample == 'box1':
forecast[['ds', 'yhat', 'yhat_upper', 'yhat_lower', 'Temperature', 'Rainfall']].to_excel(os.path.join(namefolder, 'selected_forecast_data.xlsx'), index=False)
elif sample == 'box2':
forecast[['ds', 'yhat', 'yhat_upper', 'yhat_lower', 'Soil Temperature', 'Water Content', 'Bulk EC']].to_excel(os.path.join(namefolder, 'selected_forecast_data.xlsx'), index=False)
elif model_name in ['NEURALPROPHET']:
if sample == 'box1':
forecast[['ds', 'yhat1', 'Temperature', 'Rainfall']].to_excel(os.path.join(namefolder, 'selected_forecast_data.xlsx'), index=False)
elif sample == 'box2':
forecast[['ds', 'yhat1', 'Soil Temperature', 'Water Content', 'Bulk EC']].to_excel(os.path.join(namefolder, 'selected_forecast_data.xlsx'), index=False)
'''plot con normalizzazione scaler'''
f,ax2=plt.subplots(1)
plt.plot(dftrain['ds'], (dftrain['y']),'k.',label='Training')
plt.plot(dftest['ds'], (dftest['y']),'r-', label='Validation')
if model_name in ['ARIMA','SARIMAX', 'PROPHET']:
plt.plot(dftest['ds'], forecast['yhat'], ls='-', c='#0072B2', label='Prediction')
else:
plt.plot(dftest['ds'], forecast['yhat1'], ls='-', c='#0072B2', label='Prediction')
if __name__ == '__main__':
main()