Repository navigation
Expand file tree
/
Copy pathmetrics.py
More file actions
282 lines (228 loc) · 9.51 KB
/
Copy pathmetrics.py
File metadata and controls
282 lines (228 loc) · 9.51 KB
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
import numpy as np
import pandas as pd
import multiprocessing as mp
import os
import pathlib
import time
from tempfile import mkdtemp
from typing import Dict, Optional, List
from code_duality.config import Config
from code_duality.metrics.callback import MetricsCallback
from code_duality.statistics import Statistics
from code_duality.utils import to_batch
from .multiprocess import Expectation
__all__ = ("Metrics", "ExpectationMetrics")
class Metrics:
"""Metrics base class.
This class is a base class for all metrics classes. It provides a common interface for
computing metrics and storing the results in pandas dataframes. This class supports
both synchronous and asynchronous computation of metrics, if sequenced configurations
are provided.
The methods `eval` and `eval_async` are used to compute the metrics for a single configuration,
they should be implemented in the derived classes.
Use the `compute` method to compute the metrics and store the results in the `data` attribute:
```python
metrics = MyMetrics()
metrics.compute(configs)
print(metrics.data) # shows the results in a pandas dataframe
```
"""
shortname = "metrics"
keys = []
def __init__(self):
self.data = None
def eval(
self,
config: Config,
pool: Optional[mp.Pool] = None,
) -> Dict[str, float]:
"""Compute the metrics for a single configuration."""
raise NotImplementedError()
def eval_async(
self,
config: Config,
pool: Optional[mp.Pool] = None,
) -> Dict[str, float]:
"""Compute the metrics for a single configuration asynchronously."""
raise NotImplementedError()
def compute(
self,
configs: Config,
resume: bool = True,
n_workers: int = 1,
n_async_jobs: int = 1,
callbacks: Optional[List[MetricsCallback]] = None,
) -> None:
"""Compute the metrics for a sequence of configurations.
Args:
configs: the sequence of configurations.
resume: whether to resume the computation from the last computed configuration.
n_workers: the number of workers to use.
n_async_jobs: the number of asynchronous jobs to use.
callbacks: the callbacks to use.
"""
self.configs = configs
config_seq = list(
filter(lambda c: not self.already_computed(c), configs.to_sequence()) if resume else configs.to_sequence()
)
if n_async_jobs > 1 and n_workers > 1:
data = self.run_async(config_seq, n_async_jobs, n_workers, callbacks)
else:
data = self.run(config_seq, n_workers, callbacks)
self.data = dict(data)
def run(
self,
config_seq: List[Config],
n_workers: int = 1,
callbacks: Optional[List[MetricsCallback]] = None,
) -> pd.DataFrame:
"""
Run the computation of the metrics for a sequence of configurations.
Args:
config_seq (list[Config]): the sequence of configurations.
n_workers (int): the number of workers to use. Default: 1.
callbacks: the callbacks to use. If None, no callbacks are used. Default: None.
Returns:
(pd.DataFrame) The computed data.
"""
callbacks = [] if callbacks is None else callbacks
for i, config in enumerate(config_seq):
if n_workers > 1:
with mp.get_context("spawn").Pool(n_workers) as p:
raw = pd.DataFrame(self.postprocess(self.eval(config, p)))
else:
raw = pd.DataFrame(self.postprocess(self.eval(config)))
for k, v in self.configs.summarize_subconfig(config).items():
raw[k] = v
raw["experiment"] = config.name
if self.data is None:
self.data = raw.copy()
else:
self.data = pd.concat([self.data, raw], ignore_index=True)
for c in callbacks:
c.update()
return self.data
def run_async(
self,
config_seq: List[Config],
n_async_jobs: int,
n_workers: int,
callbacks: Optional[List[MetricsCallback]] = None,
):
"""
Run the computation of the metrics for a sequence of configurations asynchronously.
Args:
config_seq (list[Config]): the sequence of configurations.
n_async_jobs (int): the number of asynchronous jobs to use.
n_workers (int): the number of workers to use.
callbacks: the callbacks to use. If None, no callbacks are used. Default: None.
Returns:
(pd.DataFrame) The computed data.
"""
if n_workers == 1:
raise ValueError("Cannot use async mode when n_workers == 1.")
callbacks = [] if callbacks is None else callbacks
for batch in to_batch(config_seq, n_async_jobs):
with mp.get_context("spawn").Pool(n_workers) as p:
async_jobs = []
# assign jobs
for config in batch:
async_jobs.append(self.eval_async(config, p))
# waiting for jobs to finish
for job in async_jobs:
job.wait()
# gathering results
for job, config in zip(async_jobs, batch):
raw = pd.DataFrame(self.postprocess(job.get()))
for k, v in self.configs.summarize_subconfig(config).items():
raw[k] = v
raw["experiment"] = config.name
if self.data is None:
self.data = raw.copy()
else:
self.data = pd.concat([self.data, raw], ignore_index=True)
# callbacks update
for c in callbacks:
c.update()
return self.data
def postprocess(self, raw):
"""Postprocess the raw data."""
return raw
def already_computed(self, config):
"""Check if the configuration has already been computed."""
if config.name not in self.data:
return False
cond = pd.DataFrame()
for k, v in self.configs.summarize_subconfig(config).items():
cond[k] = self.data[config.name][k] == v
return np.any(np.prod(cond.values, axis=-1))
def to_pickle(self, path: Optional[str | pathlib.Path] = None, **kwargs) -> str:
"""Save the data to a pickle file.
Args:
path (str, Path): the path to save the data. If None, a temporary directory is created.
kwargs: additional arguments to pass to `pd.to_pickle`.
Returns:
(str) The path to the saved file.
"""
if path is None:
path = os.path.join(mkdtemp(), f"{self.shortname}.pkl")
elif os.path.isdir(path):
path = os.path.join(path, f"{self.shortname}.pkl")
pd.to_pickle(self.data, path, **kwargs)
return str(path)
def read_pickle(self, path: str | pathlib.Path, **kwargs):
"""Read the data from a pickle file.
Args:
path (str, Path): the path to read the data from.
kwargs: additional arguments to pass to `pd.read_pickle`.
"""
if os.path.isdir(path):
path = os.path.join(path, f"{self.shortname}.pkl")
if not os.path.exists(path):
return
self.data = pd.read_pickle(path, **kwargs)
class ExpectationMetrics(Metrics):
"""Expectation metrics base class.
This class is a base class for metrics that involve sampling and computing statistics.
To use this class, you need to specify the `expectation_factory` attribute with a class
that inherits from `Expectation`.
"""
expectation_factory: Expectation = None
def eval(self, config: Config, pool: mp.Pool = None):
Metrics.eval.__doc__
expectation = self.expectation_factory(
config=config,
seed=config.get("seed", int(time.time())),
n_samples=config.metrics.get("n_samples", 1),
)
return expectation.compute(pool)
def eval_async(self, config: Config, pool: mp.Pool = None):
Metrics.eval_async.__doc__
expectation = self.expectation_factory(
config=config,
seed=config.get("seed", int(time.time())),
n_samples=config.metrics.get("n_samples", 1),
)
return expectation.compute_async(pool)
def reduce(self, samples: List[Dict[str, float]], reduction: str = "normal"):
"""Computes as set of statistics from samples."""
return {k: Statistics.from_samples([s[k] for s in samples], reduction=reduction, name=k) for k in samples[0]}
def format(self, stats: Dict[str, Statistics]):
"""Formats the statistics in a dictionary.
Note:
This method is used to format the statistics in a dictionary. The default implementation
formats the statistics in a dictionary of lists. If the statistics are not in a list format,
the method will format them in a list format.
"""
out = dict()
for k, s in stats.items():
if "samples" in s:
out[k] = s.samples.tolist()
continue
for sk, sv in s.__data__.items():
out[k + "_" + sk] = [sv]
return out
def postprocess(self, samples: List[Dict[str, float]]) -> Dict[str, Statistics]:
return self.format(self.reduce(samples, self.configs.metrics.get("reduction", "normal")))
if __name__ == "__main__":
pass