Skip to content

Commit 52b76a7

Browse files
committed
feat: reupload the revised version with ray optimization
1 parent 4485885 commit 52b76a7

File tree

186 files changed

+5958
-104
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

186 files changed

+5958
-104
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ hydroecolstm.interface.show_gui()
2121

2222
### The GUI
2323

24-
- After lanching the GUI, you should see the following window (the latest version could look different)
24+
- After lanching the GUI, you should see the following window (the latest version could look different).
2525

2626
<p align="center">
2727
<img src="https://github.com/tamnva/hydroecolstm/blob/master/docs/images/intro_figure.gif" width=100% title="hover text">

docs/source/examples.rst

Lines changed: 87 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,38 @@ The configuration file ``config.yml`` and data for this example can be found `he
88

99
.. code-block:: python
1010
11-
# Import hydroecolstm function
12-
from hydroecolstm.model_run import run_train
11+
from hydroecolstm.model_run import run_config
1312
from hydroecolstm.utility.plot import plot
1413
from hydroecolstm.data.read_config import read_config
14+
from hydroecolstm.model.create_model import create_model
1515
from hydroecolstm.utility.evaluation_function import EvaluationFunction
1616
from hydroecolstm.data.read_data import read_forecast_data
1717
import matplotlib.pyplot as plt
1818
from pathlib import Path
1919
import torch
20-
21-
# Read configuration file
22-
# Please modify the path to the config.yml and link to data
23-
config = read_config("C:/example/1_streamflow_simulation/config.yml")
24-
25-
# Create model and train
26-
model, x_scaler, y_scaler, data = run_train(config)
27-
28-
# Plot training and validation losses
29-
data["trainer"].loss.drop(['epoch', 'best_model'], axis=1).plot()
30-
20+
21+
#-----------------------------------------------------------------------------#
22+
# Set up, train, test model #
23+
#-----------------------------------------------------------------------------#
24+
25+
# Read configuration file, please modify the path to the config.yml file
26+
config = read_config("C:/hydroecolstm/examples/1_streamflow_simulation/config.yml")
27+
28+
# Create model and train from config
29+
model, data, best_config = run_config(config)
30+
31+
# Evaluate the model and transform to normal scale
32+
data['y_train_simulated'] = data["y_scaler"].inverse(model.evaluate(data["x_train_scale"]))
33+
data['y_valid_simulated'] = data["y_scaler"].inverse(model.evaluate(data["x_valid_scale"]))
34+
data['y_test_simulated'] = data["y_scaler"].inverse(model.evaluate(data["x_test_scale"]))
35+
36+
# Plot train and validation loss with epoch
37+
data["loss_epoch"].plot()
38+
plt.show()
39+
40+
# Want to see all keys in data
41+
data.keys()
42+
3143
# Objective function values: MAE, NSE, RMSE, MSE
3244
objective = EvaluationFunction(config['eval_function'], config['warmup_length'])
3345
objective(data['y_train'], data['y_train_simulated'])
@@ -39,18 +51,18 @@ The configuration file ``config.yml`` and data for this example can be found `he
3951
for target in config["target_features"]:
4052
p = plot(data, object_id=str(object_id), target_feature=target)
4153
p.show()
42-
54+
4355
# Application of the model for (assumed) ungagued basins
4456
forecast_dataset = read_forecast_data(config)
45-
x_forecast_scale = x_scaler.transform(forecast_dataset["x_forecast"])
57+
x_forecast_scale = data["x_scaler"].transform(forecast_dataset["x_forecast"])
4658
y_forecast_scale = model.evaluate(x_forecast_scale)
4759
4860
# Application of the model for (assumed) ungagued basins
4961
forecast_dataset = read_forecast_data(config)
50-
x_forecast_scale = x_scaler.transform(forecast_dataset["x_forecast"])
62+
x_forecast_scale = data["x_scaler"].transform(forecast_dataset["x_forecast"])
5163
y_forecast_scale = model.evaluate(x_forecast_scale)
52-
y_forecast_simulated = y_scaler.inverse(y_forecast_scale)
53-
64+
y_forecast_simulated = data["y_scaler"].inverse(y_forecast_scale)
65+
5466
# Visualize result: train_test_period = "train" or "test"
5567
for object_id in y_forecast_simulated.keys():
5668
plt.plot(forecast_dataset["time_forecast"][object_id],
@@ -61,14 +73,27 @@ The configuration file ``config.yml`` and data for this example can be found `he
6173
color = 'red', label = "Simulated", alpha=0.9, linewidth=0.75)
6274
plt.legend()
6375
plt.show()
64-
76+
6577
# Objective function for forecast
6678
objective(forecast_dataset['y_forecast'], y_forecast_simulated)
67-
79+
6880
# Save all data and model state dicts to the output_directory
6981
torch.save(data, Path(config["output_directory"][0], "data.pt"))
70-
#torch.save(model.state_dict(), Path(config["output_directory"][0], "model.pt"))
71-
82+
torch.save(model.state_dict(),
83+
Path(config["output_directory"][0], "model_state_dict.pt"))
84+
85+
#-----------------------------------------------------------------------------#
86+
# Incase you close this file and open again, #
87+
# you can load your data, model as follows #
88+
#-----------------------------------------------------------------------------#
89+
config = read_config("C:/hydroecolstm/examples/1_streamflow_simulation/config.yml")
90+
91+
model = create_model(config)
92+
model.load_state_dict(torch.load(Path(config["output_directory"][0],
93+
"model_state_dict.pt")))
94+
95+
data = torch.load(Path(config["output_directory"][0], "data.pt"))
96+
7297
7398
Multiple outputs simulation
7499
---------------------------
@@ -78,25 +103,38 @@ The configuration file ``config.yml`` and data for this example can be found `he
78103
.. code-block:: python
79104
80105
# Import hydroecolstm function
81-
from hydroecolstm.model_run import run_train
106+
107+
from hydroecolstm.model_run import run_config
82108
from hydroecolstm.utility.plot import plot
83109
from hydroecolstm.data.read_config import read_config
110+
from hydroecolstm.model.create_model import create_model
84111
from hydroecolstm.utility.evaluation_function import EvaluationFunction
85-
from hydroecolstm.data.read_data import read_forecast_data
86112
import matplotlib.pyplot as plt
87113
from pathlib import Path
88114
import torch
89-
90-
# Read configuration file
91-
# Please modify the path to the config.yml and link to data
92-
config = read_config("C:/example/2_streamflow_isotope_simulation/config.yml")
93-
94-
# Create model and train
95-
model, x_scaler, y_scaler, data = run_train(config)
96-
97-
# Plot training and validation losses
98-
data["trainer"].loss.drop(['epoch', 'best_model'], axis=1).plot()
99-
115+
116+
#-----------------------------------------------------------------------------#
117+
# Set up, train, test model #
118+
#-----------------------------------------------------------------------------#
119+
120+
# Read configuration file, please modify the path to the config.yml file
121+
config = read_config("C:/hydroecolstm/examples/2_streamflow_isotope_simulation/config.yml")
122+
123+
# Create model and train from config
124+
model, data, best_config = run_config(config)
125+
126+
# Evaluate the model and transform to normal scale
127+
data['y_train_simulated'] = data["y_scaler"].inverse(model.evaluate(data["x_train_scale"]))
128+
data['y_valid_simulated'] = data["y_scaler"].inverse(model.evaluate(data["x_valid_scale"]))
129+
data['y_test_simulated'] = data["y_scaler"].inverse(model.evaluate(data["x_test_scale"]))
130+
131+
# Plot train and validation loss with epoch
132+
data["loss_epoch"].plot()
133+
plt.show()
134+
135+
# Want to see all keys in data
136+
data.keys()
137+
100138
# Objective function values: MAE, NSE, RMSE, MSE
101139
objective = EvaluationFunction(config['eval_function'], config['warmup_length'])
102140
objective(data['y_train'], data['y_train_simulated'])
@@ -108,7 +146,19 @@ The configuration file ``config.yml`` and data for this example can be found `he
108146
for target in config["target_features"]:
109147
p = plot(data, object_id=str(object_id), target_feature=target)
110148
p.show()
111-
149+
112150
# Save all data and model state dicts to the output_directory
113151
torch.save(data, Path(config["output_directory"][0], "data.pt"))
114-
#torch.save(model.state_dict(), Path(config["output_directory"][0], "model.pt"))
152+
torch.save(model.state_dict(),
153+
Path(config["output_directory"][0], "model_state_dict.pt"))
154+
155+
#-----------------------------------------------------------------------------#
156+
# Incase you close this file and open again, #
157+
# you can load your data, model as follows #
158+
#-----------------------------------------------------------------------------#
159+
config = read_config("C:/hydroecolstm/examples/2_streamflow_isotope_simulation/config.yml")
160+
model = create_model(config)
161+
model.load_state_dict(torch.load(Path(config["output_directory"][0],
162+
"model_state_dict.pt")))
163+
data = torch.load(Path(config["output_directory"][0], "data.pt"))
164+
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# path to time series data file, input and target features should be in this file
2+
dynamic_data_file:
3+
- C:/hydroecolstm/examples/1_streamflow_simulation/data/time_series.csv
4+
5+
# (optional input) path to catchment attributes file
6+
static_data_file:
7+
- C:/hydroecolstm/examples/1_streamflow_simulation/data/static_attributes.csv
8+
9+
# ouptut directory, model output will be save in this file
10+
output_directory:
11+
- C:/hydroecolstm/examples/1_streamflow_simulation/results
12+
13+
# input static features - column name(s) of the static_data_file
14+
input_static_features:
15+
- elev_mean
16+
- slope_mean
17+
- p_seasonality
18+
- aridity
19+
- pet_mean
20+
21+
# input dynamic features - column name(s) of the dynamic_data_file
22+
input_dynamic_features:
23+
- precipitation_mm_d
24+
- temperature_mean_degC
25+
26+
# target output - column name(s) of the dynamic_data_file
27+
target_features:
28+
- discharge_vol_m3_s
29+
30+
# selected catchments (object_id) used for traning/validation/test the model
31+
object_id:
32+
- 2009
33+
- 2016
34+
- 2018
35+
- 2019
36+
- 2020
37+
- 2024
38+
- 2030
39+
- 2033
40+
41+
# start and ending of the train period, must be in yyyy-mm-dd hh:mm format
42+
train_period:
43+
- 2010-01-01 00:00
44+
- 2020-12-31 00:00
45+
46+
# start and ending of the validation period, must be in yyyy-mm-dd hh:mm format
47+
valid_period:
48+
- 2006-01-01 00:00
49+
- 2009-12-31 00:00
50+
51+
# start and ending of the test period, must be in yyyy-mm-dd hh:mm format
52+
test_period:
53+
- 2001-01-01 00:00
54+
- 2005-12-31 00:00
55+
56+
# model class: LSTM
57+
# model_class: EA-LSTM
58+
model_class: LSTM
59+
60+
# Model head: currently only regression model (multi-layer neural network was implemented)
61+
Regression:
62+
# Activation function of each layer (layer 1 - output layer): Identity, ReLu, Sigmoid, Tanh, Softplus
63+
activation_function:
64+
- Identity
65+
# Number of neural each layer (number of neuraon in last layer = number of target features; put "None" for the last layer)
66+
num_neurons:
67+
- None
68+
# Number of layer
69+
num_layers: 1
70+
71+
# E.g., model head of 2 layers
72+
#Regression:
73+
# activation_function:
74+
# - Sigmoid
75+
# - Identity
76+
# num_neurons:
77+
# - 6
78+
# - None
79+
# num_layers: 2
80+
81+
# Scaler for input dynamic features: Z-score, MinMaxScaler, or None
82+
scaler_input_dynamic_features:
83+
- Z-score
84+
85+
# Scaler for input static features: Z-score, MinMaxScaler, or None
86+
scaler_input_static_features:
87+
- Z-score
88+
89+
# Scaler for target features: Z-score, MinMaxScaler, or None
90+
scaler_target_features:
91+
- MinMaxScaler
92+
93+
# Hidden size of the LSTM network
94+
hidden_size: 30
95+
96+
# Number of LSTM layers
97+
num_layers: 1
98+
99+
# Number of training epoch
100+
n_epochs: 300
101+
102+
# Learning rate
103+
learning_rate: 0.005
104+
105+
# Dropout rate (applied to output of each LSTM layers)
106+
dropout: 0.3
107+
108+
# Warmup length
109+
warmup_length: 30
110+
111+
# Loss function: RMSE, MSE, MAE
112+
loss_function: RMSE
113+
114+
# Sequence length
115+
sequence_length: 365
116+
117+
# Batch size
118+
batch_size: 8
119+
120+
# Patience length
121+
patience: 30
122+
123+
# (optional input) function to evaluate the selected model: NSE, RMSE, MAE, MSE
124+
eval_function: NSE
125+
126+
# optional input - applied the selected model for prediction/ungagued catchments
127+
128+
# path to the static data file contains static features for forecast catchments
129+
# if these data inside the "static_data_file" file, type static_data_file
130+
static_data_file_forecast:
131+
- static_data_file
132+
133+
# path to the dynamic data file contains dynamic features for forecast catchments
134+
# if these data inside the "dynamic_data_file" file, type dynamic_data_file
135+
dynamic_data_file_forecast:
136+
- dynamic_data_file
137+
138+
# start and ending of the forecast period, must be in yyyy-mm-dd hh:mm format
139+
forecast_period:
140+
- 2001-01-01 00:00
141+
- 2009-12-31 00:00
142+
143+
# selected catchments (object_id) for forecast
144+
object_id_forecast:
145+
- 2011
146+
- 2029
147+

examples/1_streamflow_simulation/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# path to time series data file, input and target features should be in this file
22
dynamic_data_file:
3-
- C:/example/1_streamflow_simulation/data/time_series.csv
3+
- C:/hydroecolstm/examples/1_streamflow_simulation/data/time_series.csv
44

55
# (optional input) path to catchment attributes file
66
static_data_file:
7-
- C:/example/1_streamflow_simulation/data/static_attributes.csv
7+
- C:/hydroecolstm/examples/1_streamflow_simulation/data/static_attributes.csv
88

99
# ouptut directory, model output will be save in this file
1010
output_directory:
11-
- C:/example/1_streamflow_simulation/results
11+
- C:/hydroecolstm/examples/1_streamflow_simulation/results
1212

1313
# input static features - column name(s) of the static_data_file
1414
input_static_features:

0 commit comments

Comments
 (0)