-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
291 lines (249 loc) · 11.5 KB
/
config.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from collections import defaultdict
import re
from functools import partial
from typing import Any, Callable
import torch.nn as nn
from models import (
BCELoss,
get_model_list,
)
class Config:
# ////////////////////////////////////////////////////////////////////////////// Models
models = {
"seist": {
"loss": partial(BCELoss, weight=[[0.2], [1]]),
"inputs": [["z"]],
"labels": [[ "det","ppk"]],
"eval": [ "ppk"],
"targets_transform_for_loss": None,
"outputs_transform_for_loss": None,
"outputs_transform_for_results": None,
},
"lnrl": {
"loss": nn.Identity,
"inputs": [["z"]],
"labels": [[ "det","ppk"]],
"eval": ["ppk"],
"targets_transform_for_loss": None,
"outputs_transform_for_loss": None,
"outputs_transform_for_results": None,
}
}
# ////////////////////////////////////////////////////////////////////////////// Conf keys
_model_conf_keys = (
"loss",
"labels",
"eval",
"outputs_transform_for_loss",
"outputs_transform_for_results",
)
# ////////////////////////////////////////////////////////////////////////////// Metrics
_avl_metrics = (
"precision",
"recall",
"f1",
"mean",
"std",
"mae",
"mape",
"r2",
)
# ////////////////////////////////////////////////////////////////////////////// Available input and output items
_avl_io_item_types = ("soft", "value", "onehot")
_avl_io_items = {
# -------------------------------------------------------------------------- Channel-Z
"z": {"type": "soft", "metrics": ["mean", "std", "mae"]},
# -------------------------------------------------------------------------- Channel-N
"n": {"type": "soft", "metrics": ["mean", "std", "mae"]},
# -------------------------------------------------------------------------- Channel-E
"e": {"type": "soft", "metrics": ["mean", "std", "mae"]},
# -------------------------------------------------------------------------- Diff(Z)
"dz": {"type": "soft", "metrics": ["mean", "std", "mae"]},
# -------------------------------------------------------------------------- Diff(N)
"dn": {"type": "soft", "metrics": ["mean", "std", "mae"]},
# -------------------------------------------------------------------------- Diff(E)
"de": {"type": "soft", "metrics": ["mean", "std", "mae"]},
# -------------------------------------------------------------------------- 1-P(p)-P(s)
"non": {"type": "soft", "metrics": []},
# -------------------------------------------------------------------------- P(d)
"det": {"type": "soft", "metrics": ["precision", "recall", "f1"]},
# -------------------------------------------------------------------------- P(p)
"ppk": {
"type": "soft",
"metrics": ["precision", "recall", "f1", "mean", "std", "mae", "mape"],
},
# -------------------------------------------------------------------------- P(s)
"spk": {
"type": "soft",
"metrics": ["precision", "recall", "f1", "mean", "std", "mae", "mape"],
},
# -------------------------------------------------------------------------- P(p+)
"ppk+": {"type": "soft", "metrics": []},
# -------------------------------------------------------------------------- P(s+)
"spk+": {"type": "soft", "metrics": []},
# -------------------------------------------------------------------------- P(d+)
"det+": {"type": "soft", "metrics": []},
# -------------------------------------------------------------------------- Phase-P indices
"ppks": {"type": "value", "metrics": ["mean", "std", "mae", "mape", "r2"]},
# -------------------------------------------------------------------------- Phase-S indices
"spks": {"type": "value", "metrics": ["mean", "std", "mae", "mape", "r2"]},
# -------------------------------------------------------------------------- Event magnitude
"emg": {"type": "value", "metrics": ["mean", "std", "mae", "r2"]},
# -------------------------------------------------------------------------- Station magnitude
"smg": {"type": "value", "metrics": ["mean", "std", "mae", "r2"]},
# -------------------------------------------------------------------------- Back azimuth
"baz": {"type": "value", "metrics": ["mean", "std", "mae", "r2"]},
# -------------------------------------------------------------------------- Distance
"dis": {"type": "value", "metrics": ["mean", "std", "mae", "r2"]},
# -------------------------------------------------------------------------- P motion polarity
"pmp": {
"type": "onehot",
"metrics": ["precision", "recall", "f1"],
"num_classes": 2,
},
# -------------------------------------------------------------------------- Clarity
"clr": {
"type": "onehot",
"metrics": ["precision", "recall", "f1"],
"num_classes": 2,
},
}
# ////////////////////////////////////////////////////////////////////////////// (DO NOT modify the following methods)
@classmethod
def check_and_init(cls):
cls._type_to_ioitems = defaultdict(list)
for k, v in cls._avl_io_items.items():
cls._type_to_ioitems[v["type"]].append(k)
# Check models
useless_model_conf = list(cls.models)
registered_models = get_model_list()
for reg_model_name in registered_models:
for re_name in cls.models:
if re.findall(re_name, reg_model_name):
if re_name in useless_model_conf:
useless_model_conf.remove(re_name)
if len(useless_model_conf) > 0:
print(f"Useless configurations: {useless_model_conf}")
# Check models' configuration
for name, conf in cls.models.items():
missing_keys = set(cls._model_conf_keys) - set(conf)
if len(missing_keys) > 0:
raise Exception(f"Model:'{name}' Missing keys:{missing_keys}")
expanded_labels = sum(
[g if isinstance(g, (tuple, list)) else [g] for g in conf["labels"]], []
)
unknown_labels = set(expanded_labels) - set(cls._avl_io_items)
if len(unknown_labels) > 0:
raise NotImplementedError(
f"Model:'{name}' Unknown labels:{unknown_labels}"
)
expanded_inputs = sum(
[g if isinstance(g, (tuple, list)) else [g] for g in conf["inputs"]], []
)
unknown_inputs = set(expanded_inputs) - set(cls._avl_io_items)
if len(unknown_inputs) > 0:
raise NotImplementedError(
f"Model:'{name}' Unknown inputs:{unknown_labels}"
)
unknown_tasks = set(conf["eval"]) - set(cls._avl_io_items)
if len(unknown_tasks) > 0:
raise NotImplementedError(
f"Model:'{name}' Unknown tasks:{unknown_tasks}"
)
# Check io-items
for k, v in cls._avl_io_items.items():
if v["type"] not in cls._avl_io_item_types:
raise NotImplementedError(f"Unknown item type: {v['type']}, item: {k}")
unknown_metrics = set(v["metrics"]) - set(cls._avl_metrics)
if len(unknown_metrics) > 0:
raise NotImplementedError(
f"Unknown metrics:{unknown_metrics} , item: {k}"
)
@classmethod
def get_io_items(cls, type: str = None) -> list:
if type is None:
return list(cls._avl_io_items)
else:
return cls._type_to_ioitems[type]
@classmethod
def get_type(cls, name: str) -> list:
return cls._avl_io_items[name]["type"]
@classmethod
def get_num_classes(cls, name: str) -> int:
if name not in cls._avl_io_items:
raise ValueError(f"Name {name} not exists.")
item_type = cls._avl_io_items[name]["type"]
if item_type != "onehot":
raise Exception(f"Type of item '{name}' is '{item_type}'.")
num_classes = cls._avl_io_items[name]["num_classes"]
return num_classes
@classmethod
def get_model_config(cls, model_name: str) -> dict:
"""Get model configuration"""
registered_models = get_model_list()
if model_name not in registered_models:
raise NotImplementedError(
f"Unknown model:'{model_name}', registered: {registered_models}"
)
tgt_model_conf_keys = []
for re_name in cls.models:
if re.findall(re_name, model_name):
tgt_model_conf_keys.append(re_name)
if len(tgt_model_conf_keys) < 1:
raise Exception(f"Missing configuration of model {model_name}")
elif len(tgt_model_conf_keys) > 1:
raise Exception(
f"Model {model_name} matches multiple configuration items: {tgt_model_conf_keys}"
)
tgt_conf_key = tgt_model_conf_keys.pop()
conf = cls.models[tgt_conf_key]
return conf
@classmethod
def get_model_config_(cls, model_name: str, *attrs) -> Any:
"""Get model configurations"""
model_conf = cls.get_model_config(model_name=model_name)
attrs_conf = []
for attr_name in attrs:
if attr_name not in model_conf:
raise Exception(
f"Unknown attribute:'{attr_name}', supported: {list(model_conf)}"
)
attrs_conf.append(model_conf[attr_name])
if len(attrs_conf) == 1:
conf = attrs_conf[0]
else:
conf = tuple(attrs_conf)
return conf
@classmethod
def get_num_inchannels(cls,model_name:str) ->int:
"""Get number of input channels"""
in_channels = 0
inps = cls.get_model_config_(model_name,"inputs")
for inp in inps:
if isinstance(inp,(list,tuple)):
if cls._avl_io_items[inp[0]]["type"] == "soft":
in_channels = len(inp)
break
if in_channels<1:
raise Exception(f"Incorrect input channels. Model:{model_name} Inputs:{inps}")
return in_channels
@classmethod
def get_metrics(cls, item_name: str) -> list:
"""Get metrics list"""
if item_name not in cls._avl_io_items:
raise Exception(
f"Unknown item:'{item_name}', supported: {list(cls._avl_io_items)}"
)
metrics = cls._avl_io_items[item_name]["metrics"]
return metrics
@classmethod
def get_loss(cls, model_name: str):
"""Create a loss instance.
Args:
model_name (str): Model name.
Returns:
nn.Module: Loss instance.
"""
Loss = cls.get_model_config(model_name)["loss"]
return Loss()
Config.check_and_init()