-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.py
67 lines (55 loc) · 1.72 KB
/
common.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
import json
import os
import pickle
from dataclasses import dataclass, field
from os.path import join
from typing import Dict, Any, ClassVar
@dataclass
class ScoresAttributes:
global_based: str
sample_based: str
explanations: str
method_names: str
data_weights: str
model_weights: str
model_accuracies: str
logistic_regression: str
neural_net: str
instance: ClassVar = field(default=None)
@staticmethod
def default_conf() -> dict:
return {
'global_based': 'global',
'sample_based': 'sample',
'explanations': 'expl',
'method_names': 'names',
'data_weights': 'd_weights',
'model_weights': 'm_weights',
'model_accuracies': 'm_accuracy',
'logistic_regression': 'Logistic Regression',
'neural_net': 'Single-Layer Neural Net'
}
@classmethod
def get(cls):
if not cls.instance:
conf_dict = cls.default_conf()
cls.instance = cls(**conf_dict)
return cls.instance
def load_json_file(file_path: str) -> Dict:
with open(file_path, 'r') as f:
file = json.load(f)
return file
def load_pickle(file_path: str) -> Any:
with open(file_path, 'rb') as f:
data = pickle.load(f)
return data
def to_pickle(output_dir: str, data: Any, suffix: str) -> str:
if not os.path.isdir(output_dir):
os.makedirs(output_dir)
output_path = join(output_dir, f'data_{suffix}.pkl')
print(f'Output path: {output_path}')
with open(output_path, 'wb') as f:
pickle.dump(data, f)
return output_path
def extract_pattern_type(data_path: str) -> str:
return data_path.split('.')[0].split('pattern_type_')[-1]