This repository was archived by the owner on Jun 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSnakefile_basic.smk
More file actions
executable file
·158 lines (139 loc) · 5.05 KB
/
Snakefile_basic.smk
File metadata and controls
executable file
·158 lines (139 loc) · 5.05 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
import os
import tensorflow as tf
import numpy as np
code_dir = '..'
# if using river_dl installed with pip this is not needed
import sys
sys.path.insert(1, code_dir)
from river_dl.preproc_utils import asRunConfig
from river_dl.preproc_utils import prep_all_data
from river_dl.evaluate import combined_metrics
from river_dl.postproc_utils import plot_obs
from river_dl.predict import predict_from_io_data
from river_dl.train import train_model
from river_dl import loss_functions as lf
from river_dl.tf_models import LSTMModel
out_dir = config['out_dir']
loss_function = lf.multitask_rmse(config['lambdas'])
rule all:
input:
expand("{outdir}/{metric_type}_metrics.csv",
outdir=out_dir,
metric_type=['overall', 'month', 'reach', 'month_reach'],
),
expand("{outdir}/asRunConfig.yml", outdir=out_dir)
rule as_run_config:
output:
"{outdir}/asRunConfig.yml"
run:
asRunConfig(config, code_dir, output[0])
rule prep_io_data:
input:
config['sntemp_file'],
config['obs_file'],
output:
"{outdir}/prepped.npz"
run:
prep_all_data(
x_data_file=input[0],
y_data_file=input[1],
x_vars=config['x_vars'],
y_vars_finetune=config['y_vars'],
spatial_idx_name='segs_test',
time_idx_name='times_test',
catch_prop_file=None,
train_start_date=config['train_start_date'],
train_end_date=config['train_end_date'],
val_start_date=config['val_start_date'],
val_end_date=config['val_end_date'],
test_start_date=config['test_start_date'],
test_end_date=config['test_end_date'],
segs=None,
out_file=output[0],
trn_offset = config['trn_offset'],
tst_val_offset = config['tst_val_offset'])
model = LSTMModel(
config['hidden_size'],
recurrent_dropout=config['recurrent_dropout'],
dropout=config['dropout'],
num_tasks=len(config['y_vars'])
)
# train the model on observations
rule train_model:
input:
"{outdir}/prepped.npz",
output:
directory("{outdir}/trained_weights/"),
"{outdir}/finetune_log.csv",
"{outdir}/finetune_time.txt",
run:
optimizer = tf.optimizers.Adam(learning_rate=config['finetune_learning_rate'])
model.compile(optimizer=optimizer, loss=loss_function)
data = np.load(input[0])
train_model(model,
x_trn = data['x_trn'],
y_trn = data['y_obs_trn'],
epochs = config['epochs'],
batch_size = 2,
seed = config['seed'],
x_val = data['x_val'],
y_val = data['y_obs_val'],
# I need to add a trailing slash here. Otherwise the wgts
# get saved in the "outdir"
weight_dir = output[0] + "/",
log_file = output[1],
time_file = output[2],
early_stop_patience=config['early_stopping'])
rule make_predictions:
input:
"{outdir}/trained_weights/",
"{outdir}/prepped.npz"
output:
"{outdir}/{partition}_preds.feather",
group: 'train_predict_evaluate'
run:
weight_dir = input[0] + '/'
model.load_weights(weight_dir)
predict_from_io_data(model=model,
io_data=input[1],
partition=wildcards.partition,
outfile=output[0],
trn_offset = config['trn_offset'],
spatial_idx_name='segs_test',
time_idx_name='times_test',
tst_val_offset = config['tst_val_offset'])
def get_grp_arg(wildcards):
if wildcards.metric_type == 'overall':
return None
elif wildcards.metric_type == 'month':
return 'month'
elif wildcards.metric_type == 'reach':
return 'seg_id_nat'
elif wildcards.metric_type == 'month_reach':
return ['seg_id_nat', 'month']
rule combine_metrics:
input:
config['obs_file'],
"{outdir}/trn_preds.feather",
"{outdir}/val_preds.feather",
output:
"{outdir}/{metric_type}_metrics.csv"
group: 'train_predict_evaluate'
params:
grp_arg = get_grp_arg
run:
combined_metrics(obs_file=input[0],
pred_trn=input[1],
pred_val=input[2],
spatial_idx_name='segs_test',
time_idx_name='times_test',
group=params.grp_arg,
outfile=output[0])
rule plot_prepped_data:
input:
"{outdir}/prepped.npz",
output:
"{outdir}/{variable}_{partition}.png",
run:
plot_obs(input[0], wildcards.variable, output[0],
partition=wildcards.partition)