-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlibconfig.py
80 lines (67 loc) · 2.48 KB
/
libconfig.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
from dataclasses import dataclass
from typing import Dict, Any, Callable
import pandas as pd
from pprint import pprint, pformat
@dataclass
class Config:
dataset_name: str
method_name: str
dataset_mutator_name: str
model_name: str
meta: Dict["str", Any]
method: Callable = None
dataset: Callable = None
dataset_mutator: Callable = None
def serialize(self):
meta_str = "__".join(
[
f"{k}={v}" if k != "stop_function" else f"{k}={v[0]}"
for k, v in self.meta.items()
]
)
return f"{self.dataset_name}__{self.dataset_mutator_name}__{self.method_name}__{self.model_name}__{meta_str}"
def json(self):
return {
"dataset_name": self.dataset_name,
"method_name": self.method_name,
"dataset_mutator_name": self.dataset_mutator_name,
"model_name": self.model_name,
"meta": {
k: v if k != "stop_function" else v[0] for k, v in self.meta.items()
},
}
def __repr__(self):
return pformat(self.json())
class Configurations:
def __init__(self, matrix):
self.configurations = []
self.meta = matrix["meta"]
for dataset in matrix["datasets"]:
for method in matrix["methods"]:
for model in matrix["models"]:
for dataset_mutator in matrix["dataset_mutators"].items():
self.configurations.append(
Config(
dataset_name=dataset[0],
dataset=dataset[1],
method_name=method[0],
method=method[1],
dataset_mutator_name=dataset_mutator[0],
dataset_mutator=dataset_mutator[1],
model_name=model,
meta=matrix["meta"],
)
)
def __repr__(self):
return pformat(
{
"meta": self.meta.__repr__(),
"configurations": self.configurations.__repr__(),
}
)
def __iter__(self, *args, **kwargs):
return self.configurations.__iter__(*args, **kwargs)
def __getitem__(self, *args, **kwargs):
return self.configurations.__getitem__(*args, **kwargs)
def __len__(self):
return len(self.configurations)