-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmodels.py
142 lines (120 loc) · 3.83 KB
/
models.py
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
import sys
sys.path.append("..")
from src.models import (
Persistence,
LinearRegression,
LinearNetwork,
RecurrentNetwork,
EARecurrentNetwork,
load_model,
)
from src.analysis import all_explanations_for_file
from scripts.utils import get_data_path
def persistence(experiment="one_month_forecast",):
data_path = get_data_path()
spatial_mask = data_path / "interim/boundaries_preprocessed/kenya_asal_mask.nc"
spatial_mask = None
predictor = Persistence(data_path, experiment=experiment, spatial_mask=spatial_mask)
predictor.evaluate(save_preds=True)
def regression(
experiment="one_month_forecast",
include_pred_month=True,
surrounding_pixels=None,
ignore_vars=None,
):
data_path = get_data_path()
spatial_mask = data_path / "interim/boundaries_preprocessed/kenya_asal_mask.nc"
spatial_mask = None
predictor = LinearRegression(
data_path,
experiment=experiment,
include_pred_month=include_pred_month,
surrounding_pixels=surrounding_pixels,
ignore_vars=ignore_vars,
static="embeddings",
spatial_mask=spatial_mask,
)
predictor.train()
predictor.evaluate(save_preds=True)
# mostly to test it works
# predictor.explain(save_shap_values=True)
def linear_nn(
experiment="one_month_forecast",
include_pred_month=True,
surrounding_pixels=None,
ignore_vars=None,
pretrained=False,
static=None,
):
predictor = LinearNetwork(
layer_sizes=[100],
data_folder=get_data_path(),
experiment=experiment,
include_pred_month=include_pred_month,
surrounding_pixels=surrounding_pixels,
ignore_vars=ignore_vars,
static=static,
)
predictor.train(num_epochs=50, early_stopping=5)
predictor.evaluate(save_preds=True)
predictor.save_model()
# _ = predictor.explain(save_shap_values=True)
def rnn(
experiment="one_month_forecast",
include_pred_month=True,
surrounding_pixels=None,
ignore_vars=None,
pretrained=True,
static=None,
):
predictor = RecurrentNetwork(
hidden_size=128,
data_folder=get_data_path(),
experiment=experiment,
include_pred_month=include_pred_month,
surrounding_pixels=surrounding_pixels,
ignore_vars=ignore_vars,
static=static,
)
predictor.train(num_epochs=50, early_stopping=5)
predictor.evaluate(save_preds=True)
predictor.save_model()
# _ = predictor.explain(save_shap_values=True)
def earnn(
experiment="one_month_forecast",
include_pred_month=True,
surrounding_pixels=None,
pretrained=True,
ignore_vars=None,
static="embeddings",
):
data_path = get_data_path()
if static is None:
print("** Cannot fit EALSTM without spatial information **")
return
if not pretrained:
predictor = EARecurrentNetwork(
hidden_size=128,
data_folder=data_path,
experiment=experiment,
include_pred_month=include_pred_month,
surrounding_pixels=surrounding_pixels,
ignore_vars=ignore_vars,
static=static,
)
predictor.train(num_epochs=50, early_stopping=5)
predictor.evaluate(save_preds=True)
predictor.save_model()
else:
predictor = load_model(data_path / f"models/{experiment}/ealstm/model.pt")
test_file = data_path / f"features/{experiment}/test/2018_3"
assert test_file.exists()
all_explanations_for_file(test_file, predictor, batch_size=100)
if __name__ == "__main__":
# ignore_vars = ["VCI", "p84.162", "sp", "tp", "VCI1M"]
ignore_vars = None
persistence()
# regression(ignore_vars=ignore_vars)
# linear_nn(ignore_vars=ignore_vars, static=None)
rnn(ignore_vars=ignore_vars, static=None)
earnn(ignore_vars=ignore_vars, static=None)