-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snakefile
242 lines (211 loc) · 5.9 KB
/
Snakefile
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
def configuration(wildcards):
import yaml
from pathlib import Path
cfile = Path('config.yaml')
assert cfile.is_file(), f'Configuration file {cfile} not found'
with open(cfile, 'r') as f:
config = yaml.safe_load(f)
return config
def input_columns(wildcards):
"""Get all columns from all input files."""
import pandas as pd
import glob
files = glob.glob('input/*.csv')
columns = []
for file in files:
df = pd.read_csv(file)
columns.extend(df.columns)
# Exclude time column
columns = set(columns)
config = configuration(wildcards)
columns.discard(config['time_col'])
return list(columns)
def config_signal(wildcards):
config = configuration(wildcards)
return config['signal']
def config_noises(wildcards):
config = configuration(wildcards)
return config['noises']
def expand_config(path):
def _expand(wildcards):
config = configuration(wildcards)
return expand(path, **config)
return _expand
wildcard_constraints:
# noise=r'\d+\.\d+',
label=r'[a-zA-Z_\-0-9]+'
#label=r'^((?!full).)*$'
models = ['mreg2', 'mreg2_intercept', 'linreg', 'linreg_intercept', 'ridge', 'ridge_intercept']
rule all_figures:
input:
# Fig 1
'figures/paper_fig1.png',
# Fig 2 (noises + mean)
expand('figures/paper_fig2_{noise}.png', noise=config_noises),
'figures/paper_fig2_mean.png',
# Fig 3 (noises + mean)
expand('figures/paper_fig3_{noise}.png', noise=config_noises),
'figures/paper_fig3_mean.png',
# Fig 4
expand('figures/paper_{model}_fig4.png', model=models),
# SI Fig 3
expand('figures/fit_matrix_{model}_{noise}.png', noise=config_noises, model=models),
# All imfs
expand('figures/imfs/{label}_imf_{noise}.png', label=input_columns, noise=config_noises),
expand('imfs/full/{signal}_imf_{noise}.csv', signal=config_signal, noise=config_noises),
rule dates:
# Get timespan for analysis
input:
folder='input'
output:
'dates.json'
params:
c=configuration
script:
'snakemake/dates.py'
rule calc_trend:
# Calculate signal trend, so it can be removed if needed
input:
folder='input',
dates='dates.json'
output:
'trend.json'
params:
c=configuration
script:
'snakemake/calc_trend.py'
# TODO: this seems too fast - test for all cols/noises?
rule decompose:
# Decompose each channel into IMFs
input:
folder='input',
dates='dates.json',
trend='trend.json'
output:
'imfs/{label}_imf_{noise}.csv'
params:
c=configuration,
full=False
script:
'snakemake/decompose.py'
rule decompose_full_signal:
# Decompose full signal into IMFs
input:
folder='input',
dates='dates.json',
trend='trend.json'
output:
'imfs/full/{label}_imf_{noise}.csv'
params:
c=configuration,
full=True
script:
'snakemake/decompose.py'
rule nearest_freq:
# Find nearest frequencies in drivers to each signal IMF
input:
expand('imfs/{label}_imf_{{noise}}.csv', label=input_columns)
output:
'nearest_frequencies_{noise}.csv'
params:
c=configuration
script:
'snakemake/nearest_freq.py'
rule fit:
# Fit a model to each signal IMF
input:
imfs=expand('imfs/{label}_imf_{{noise}}.csv', label=input_columns),
freqs='nearest_frequencies_{noise}.csv'
output:
coeffs='{model}/coefficients_{noise}.json',
figure='figures/fit_matrix_{model}_{noise}.png'
params:
c=configuration
script:
'snakemake/fit.py'
rule predict:
# Predict each signal IMF
input:
imfs=expand('imfs/{label}_imf_{{noise}}.csv', label=input_columns),
freqs='nearest_frequencies_{noise}.csv',
coeffs='{model}/coefficients_{noise}.json',
dates='dates.json'
output:
'{model}/predictions_{noise}.csv'
params:
c=configuration
script:
'snakemake/predict.py'
rule combine_preds:
# Combine all predictions
input:
predictions=expand('{{model}}/predictions_{noise}.csv', noise=config_noises),
trend='trend.json'
output:
'{model}/predictions.csv'
params:
c=configuration
script:
'snakemake/combine_preds.py'
# Visualisation
rule plot_imf:
input:
imf='imfs/{imf_label}_imf_{noise}.csv'
output:
'figures/imfs/{imf_label}_imf_{noise}.png'
script:
'snakemake/plot_imf.py'
# Paper figures
rule paper_fig1:
input:
folder='input'
output:
'figures/paper_fig1.png'
params:
c=configuration
script:
'snakemake/paper_fig1.py'
rule paper_fig2:
input:
folder='input',
signal_imfs=expand_config('imfs/{signal}_imf_{noises}.csv')
output:
'figures/paper_fig2_{noise}.png',
params:
c=configuration
script:
'snakemake/paper_fig2.py'
rule paper_fig3:
input:
imfs = expand('imfs/{label}_imf_{{noise}}.csv', label=input_columns),
output:
'figures/paper_fig3_{noise}.png',
params:
c=configuration
wildcard_constraints:
noise=r'\d+\.\d+'
script:
'snakemake/paper_fig3.py'
rule paper_fig3_mean:
input:
imfs=expand('imfs/{label}_imf_{noise}.csv', label=input_columns, noise=config_noises),
output:
'figures/paper_fig3_{noise}.png',
params:
c=configuration
wildcard_constraints:
noise='mean'
script:
'snakemake/paper_fig3.py'
rule paper_fig4:
input:
folder='input',
predictions='{model}/predictions.csv',
dates='dates.json',
trend='trend.json'
output:
'figures/paper_{model}_fig4.png'
params:
c=configuration
script:
'snakemake/paper_fig4.py'