-
Notifications
You must be signed in to change notification settings - Fork 43
/
upgrade.py
211 lines (175 loc) · 5.91 KB
/
upgrade.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
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
"""
Config file upgrading module modified from grond
"""
import sys
import os
import copy
import difflib
from pyrocko import guts_agnostic as aguts
from logging import getLogger
from pyrocko import guts
logger = getLogger('upgrade')
def rename_attribute(old, new):
def func(path, obj):
if old in obj:
obj.rename_attribute(old, new)
return func
def rename_class(new):
def func(path, obj):
obj._tagname = new
return func
def drop_attribute(old, newclass=None):
def func(path, obj):
if old in obj:
if newclass is not None:
oldclass = obj.get(old)
if not isinstance(oldclass, newclass):
obj.drop_attribute(old)
else:
logger.debug(
'Not updating "%s" attribute, it already meets'
' new class.', old)
else:
obj.drop_attribute(old)
return func
def set_attribute(k, v, cond=None):
def func(path, obj):
if cond is not None:
if obj[k] == cond:
obj[k] = v
else:
obj[k] = v
return func
def color_diff(diff):
green = '\x1b[32m'
red = '\x1b[31m'
blue = '\x1b[34m'
dim = '\x1b[2m'
reset = '\x1b[0m'
for line in diff:
if line.startswith('+'):
yield green + line + reset
elif line.startswith('-'):
yield red + line + reset
elif line.startswith('^'):
yield blue + line + reset
elif line.startswith('@'):
yield dim + line + reset
else:
yield line
def upgrade_config_file(fn, diff=True, update=[]):
rules = [
('beat.SeismicConfig',
drop_attribute('blacklist')),
('beat.SeismicConfig',
drop_attribute('calc_data_cov')),
('beat.SeismicConfig',
set_attribute(
'noise_estimator',
aguts.load(string='''!beat.SeismicNoiseAnalyserConfig
structure: variance
pre_arrival_time: 5
'''), False)),
('beat.ProblemConfig',
drop_attribute('dataset_specific_residual_noise_estimation')),
('beat.ProblemConfig',
set_attribute('mode', 'ffi', 'ffo')),
('beat.WaveformFitConfig',
set_attribute('preprocess_data', True, True)),
('beat.WaveformFitConfig',
set_attribute(
'filterer',
aguts.load(string='''
- !beat.heart.Filter
lower_corner: 0.01
upper_corner: 0.1
order: 4
'''))),
('beat.MetropolisConfig',
drop_attribute('n_stages')),
('beat.MetropolisConfig',
drop_attribute('stage')),
('beat.ParallelTemperingConfig',
set_attribute('resample', False, False)),
('beat.FFOConfig',
rename_class('beat.FFIConfig')),
('beat.GeodeticLinearGFConfig',
drop_attribute('extension_widths')),
('beat.GeodeticLinearGFConfig',
drop_attribute('extension_lengths')),
('beat.GeodeticLinearGFConfig',
drop_attribute('patch_widths')),
('beat.GeodeticLinearGFConfig',
drop_attribute('patch_lengths')),
('beat.SeismicLinearGFConfig',
drop_attribute('extension_widths')),
('beat.SeismicLinearGFConfig',
drop_attribute('extension_lengths')),
('beat.SeismicLinearGFConfig',
drop_attribute('patch_widths')),
('beat.SeismicLinearGFConfig',
drop_attribute('patch_lengths')),
('beat.GeodeticConfig',
drop_attribute('fit_plane')),
('beat.GeodeticConfig',
drop_attribute('datadir')),
('beat.GeodeticConfig',
drop_attribute('names')),
('beat.GeodeticConfig',
drop_attribute('blacklist')),
('beat.GeodeticConfig',
drop_attribute('types', dict)),
('beat.EulerPoleConfig',
rename_attribute('blacklist', 'station_blacklist'))
]
def apply_rules(path, obj):
for tagname, func in rules:
if obj._tagname == tagname:
func(path, obj)
updates_avail = ['hierarchicals', 'hypers', 'structure']
t1 = aguts.load(filename=fn)
t2 = copy.deepcopy(t1)
for upd in update:
if upd not in updates_avail:
raise TypeError('Update not available for "%s"' % upd)
n_upd = len(update)
if n_upd > 0:
fn_tmp = fn + 'tmp'
if 'structure' in update:
aguts.apply_tree(t2, apply_rules)
aguts.dump(t2, filename=fn_tmp, header=True)
t2 = guts.load(filename=fn_tmp)
if 'hypers' in update:
t2.update_hypers()
if 'hierarchicals' in update:
t2.update_hierarchicals()
t2.problem_config.validate_priors()
guts.dump(t2, filename=fn_tmp)
t2 = aguts.load(filename=fn_tmp)
else:
fn_tmp = fn
s1 = aguts.dump(t1)
s2 = aguts.dump(t2)
if diff:
result = list(difflib.unified_diff(
s1.splitlines(1), s2.splitlines(1),
'normalized old', 'normalized new'))
if sys.stdout.isatty():
sys.stdout.writelines(color_diff(result))
else:
sys.stdout.writelines(result)
else:
aguts.dump(t2, filename=fn_tmp, header=True)
upd_config = guts.load(filename=fn_tmp)
if 'hypers' in update:
logger.info(
'Updated hyper parameters! Previous hyper'
' parameter bounds are invalid now!')
if 'hierarchicals' in update:
logger.info('Updated hierarchicals.')
guts.dump(upd_config, filename=fn)
if n_upd > 0:
os.remove(fn_tmp)
if __name__ == '__main__':
fn = sys.argv[1]
upgrade_config_file(fn)