-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaders.py
233 lines (188 loc) · 7.51 KB
/
readers.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
import os
import sys
import json
import numpy as np
import pandas as pd
import pickle as pkl
from abc import ABC, abstractmethod
from pathlib import Path
from collections import defaultdict
from typing import Union, Set, List, Dict, Tuple
from utils import generate_dbscan_config_tree
class Reader(ABC):
def __init__(self, path: Union[str, Path]) -> None:
self.path = path
self.test_datasets = set()
@abstractmethod
def analyze(self) -> None:
pass
@abstractmethod
def get_configuration(self, test: str) -> Tuple:
pass
@abstractmethod
def get_all_configurations(self, test: str) -> List[Tuple]:
pass
@abstractmethod
def get_test_ari(self, test: str) -> float:
pass
@abstractmethod
def is_in(self, test: str) -> bool:
pass
@abstractmethod
def to_ari_dataframe(self, remove_extension: bool=True) -> pd.DataFrame:
pass
@classmethod
def _verify_and_insert(cls, dikt: dict, key: str, value) -> dict:
assert not key in dikt
dikt[key] = value
return dikt
class KIndexedList:
def __init__(self):
self.data = defaultdict(list)
def clear(self) -> None:
for k in self.data:
self.data[k].clear()
def insert(self, k: int, index: int) -> None:
self.data[k].append(index)
def get(self, k: int) -> List[int]:
return self.data[k]
def get_all(self) -> Dict:
return dict(self.data)
def gather(self, data_dict: Dict, fixed_level: str) -> List[Dict]:
result = []
for k in self.data:
for idx in self.data[k]:
subdict = {}
p, score_dict = data_dict[k][fixed_level][idx]
subdict['k'] = k
subdict['ari'] = score_dict['ari']
subdict['configuration'] = p
result.append(subdict)
return result
class ODGridReader(Reader):
def __init__(self, path: Union[str, Path], model: str) -> None:
self.path = path
self.test_datasets = set()
self.configs = {}
self.model = model
self.ari = {}
def analyze(self):
with open(self.path, 'rb') as fp:
results = pkl.load(fp)
for dataset in results:
self.test_datasets.add(dataset)
best_ari = -2
best_index_dict = KIndexedList()
for k in results[dataset]:
for model in results[dataset][k]:
if model != self.model:
continue
for idx, (p, res_dict) in enumerate(results[dataset][k][model]):
current_ari = res_dict['ari']
if current_ari > best_ari:
best_ari = current_ari
best_index_dict.clear()
best_index_dict.insert(k, idx)
self.ari[dataset] = current_ari
elif current_ari == best_ari:
best_index_dict.insert(k, idx)
full_list = best_index_dict.gather(results[dataset], self.model)
res = [(self.model, tmp['configuration'], {'k': tmp['k']}) for tmp in full_list]
self.configs[dataset] = res
def get_configuration(self, test: str) -> Dict:
return self.configs[test][0]
def get_all_configurations(self, test: str) -> List[Dict]:
return self.configs[test]
def get_test_ari(self, test: str) -> float:
return self.ari[test]
def is_in(self, test: str) -> bool:
return test in self.test_datasets
def to_ari_dataframe(self, remove_ext: bool=True) -> pd.DataFrame:
res = defaultdict(list)
for dataset in self.test_datasets:
dt = dataset
if remove_ext:
dt = os.path.splitext(dataset)[0]
res['dataset'].append(dt)
res['ari'].append(self.get_test_ari(dataset))
return pd.DataFrame(res)
class ODGridAllReader(ODGridReader):
def __init__(self, path: Union[str, Path], model: str, dataset_list: Union[Set, List, None]=None) -> None:
super().__init__(path, model)
self.configs = defaultdict(list)
self.dataset_list = dataset_list
def analyze(self) -> None:
with open(self.path, 'rb') as fp:
results = pkl.load(fp)
for dataset in results:
if not self.dataset_list is None:
if not dataset in self.dataset_list:
continue
self.test_datasets.add(dataset)
for k in results[dataset]:
for model in results[dataset][k]:
if model != self.model:
continue
for idx, (p, res_dict) in enumerate(results[dataset][k][model]):
current_ari = res_dict['ari']
res = (self.model, p, {'k': k}, current_ari)
self.configs[dataset].append(res)
class ClusteringGridReader(Reader):
def __init__(self, path: Union[str, Path], model: str) -> None:
super().__init__(path)
self.model = model
self.test_datasets = set()
self.configs = {}
self.ari = {}
def analyze(self) -> None:
with open(self.path, 'rb') as fp:
results = pkl.load(fp)
for dataset in results:
assert not dataset in self.test_datasets
self.test_datasets.add(dataset)
best_ari = -2
self.configs[dataset] = []
model = self.model
for idx, (p, res_dict) in enumerate(results[dataset][model]):
current_ari = res_dict['ari']
if current_ari > best_ari:
best_ari = current_ari
self.configs[dataset].clear()
self.configs[dataset].append((self.model, p, {}))
elif current_ari == best_ari:
self.configs[dataset].append((self.model, p, {}))
self.ari[dataset] = best_ari
def is_in(self, test: str) -> bool:
return test in self.test_datasets
def get_test_ari(self, test: str) -> float:
return self.ari[test]
def get_configuration(self, test: str) -> Dict:
return self.configs[test][0]
def get_all_configurations(self, test: str) -> List[Dict]:
return self.configs[test]
def to_ari_dataframe(self, remove_extension: bool) -> pd.DataFrame:
res = defaultdict(list)
for dataset in self.test_datasets:
dt = dataset
if remove_extension:
dt = os.path.splitext(dataset)[0]
res['dataset'].append(dt)
res['ari'].append(self.get_test_ari(dataset))
return pd.DataFrame(res)
class ClusteringGridAllReader(ClusteringGridReader):
def __init__(self, path: Union[str, Path], model: str, dataset_list: Union[None, List, Set]=None) -> None:
super().__init__(path, model)
self.configs = defaultdict(list)
if not dataset_list is None:
dataset_list = set(dataset_list)
self.dataset_list = dataset_list
def analyze(self) -> None:
with open(self.path, 'rb') as fp:
results = pkl.load(fp)
for dataset in results:
if not self.dataset_list is None:
if not dataset in self.dataset_list:
continue
self.test_datasets.add(dataset)
for p, res_dict in results[dataset][self.model]:
self.configs[dataset].append((self.model, p, {}, res_dict['ari']))