-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
448 lines (381 loc) · 13.1 KB
/
utils.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
"""
General utilities for nessai importance nested sampling paper.
"""
import argparse
from ast import literal_eval
import configparser
import glob
import logging
import os
import re
import shutil
from typing import Any, Callable, Optional, Tuple
import matplotlib.pyplot as plt
import nessai
from nessai.flowsampler import FlowSampler
from nessai.flowmodel import update_config
from nessai.utils import setup_logger
from nessai_models import (
Gaussian,
Rosenbrock,
GaussianMixture,
GaussianMixtureWithData,
Pyramid,
EggBox,
)
import numpy as np
import pandas as pd
import seaborn as sns
import torch
import tqdm
try:
import ujson as json
except ImportError:
print("Could not import ujson, falling back to json")
import json
logger = logging.getLogger("ins-experiment")
def parse_args() -> argparse.ArgumentParser:
"""Parse command line args"""
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, help="Config file")
parser.add_argument(
"--tag",
type=str,
default=None,
help="Tag for outdir",
)
parser.add_argument("--log-level", type=str, default="INFO")
parser.add_argument("--seed", type=int, default=150914)
parser.add_argument("--summary", type=bool, default=True)
return parser.parse_args()
def configure_logger(level="INFO"):
"""Configure the logger."""
logger.setLevel(level)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(
logging.Formatter(
"%(asctime)s %(name)s %(levelname)-8s: %(message)s",
datefmt="%m-%d %H:%M",
)
)
stream_handler.setLevel("INFO")
logger.addHandler(stream_handler)
def configure_plotting(base_dir) -> None:
"""Configure the plotting defaults"""
sns.set_style("ticks")
sns.set_palette("colorblind")
plt.style.use(os.path.join(base_dir, "paper.mplstyle"))
def natural_sort(values):
"""Natural sort a list.
Based on: https://stackoverflow.com/a/4836734
"""
# fmt: off
convert = (lambda text: int(text) if text.isdigit() else text.lower()) # noqa: 731
alphanum_key = lambda key: [convert(c) for c in re.split("([0-9]+)", key)] # noqa: 731
# fmt: on
return sorted(values, key=alphanum_key)
def find_results_files(path, file="summary.json"):
"""Find all of the results files"""
p = path + "/**/" + file
logger.info(f"Searching for: {p}")
files = natural_sort(glob.glob(p, recursive=True))
logger.info(f"Found {len(files)} results files")
return files
def load_results(results_files):
"""Load the log-evidence"""
data = []
for i, rf in tqdm.tqdm(enumerate(results_files)):
try:
d = load_json(rf)
data.append(d)
except json.JSONDecodeError:
print(f"Skipping: {rf}")
df = pd.DataFrame(data)
return df
def load_all_results(path: str, **kwargs) -> dict:
r"""Load all the results files in a path.
Searches for directories that match the pattern `"\d+d"`.
"""
runs = natural_sort(glob.glob(path))
regex = re.compile(r"\d+d")
dims = [int(regex.findall(p)[-1][:-1]) for p in runs]
res = {}
for d, p in zip(dims, runs):
r = load_results(find_results_files(p, **kwargs))
if r.empty:
print(f"Skipping for {d}, {p}")
else:
res[d] = r
return res
def load_json(filename: str) -> dict:
"""Load a JSON file"""
with open(filename, "r") as fp:
d = json.load(fp)
return d
class HyperCubeMixin:
"""Mixin that adds hyper-cube methods to a nessai Model"""
def to_unit_hypercube(self, x: np.ndarray) -> np.ndarray:
x_out = x.copy()
for n in self.names:
x_out[n] = (x[n] - self.bounds[n][0]) / (
self.bounds[n][1] - self.bounds[n][0]
)
return x_out
def from_unit_hypercube(self, x: np.ndarray) -> np.ndarray:
x_out = x.copy()
for n in self.names:
x_out[n] = (self.bounds[n][1] - self.bounds[n][0]) * x[
n
] + self.bounds[n][0]
return x_out
def from_string(value: str) -> Any:
"""Convert the value from a string.
Uses `ast.literal` eval to convert a value from a string. If a ValueError
is raised, the string is returned.
"""
try:
return literal_eval(value)
except ValueError:
return value
def get_config(config_file: str) -> configparser.ConfigParser:
"""Get the config from a config file."""
config = configparser.ConfigParser()
config.optionxform = str
config.read(config_file)
if config.getboolean("General", "use_float64", fallback=False):
torch.set_default_dtype(torch.float64)
if eps := config.getfloat("General", "eps", fallback=None):
nessai.config.EPS = eps
return config
def get_model_class(name: str, dims: int) -> Tuple[Callable, dict]:
"""Get the class for the from the name and any kwargs."""
kwargs = {}
if name == "rosenbrock":
Model = Rosenbrock
elif name == "gaussian":
Model = Gaussian
elif name == "gmm":
Model = GaussianMixture
elif name == "gmm_paper":
Model = GaussianMixture
kwargs["config"] = [
{
"mean": np.concatenate([[0, 4], np.zeros(dims - 2)]),
"cov": np.eye(dims),
},
{
"mean": np.concatenate([[0, -4], np.zeros(dims - 2)]),
"cov": np.eye(dims),
},
{
"mean": np.concatenate([[4, 0], np.zeros(dims - 2)]),
"cov": np.eye(dims),
},
{
"mean": np.concatenate([[-4, 0], np.zeros(dims - 2)]),
"cov": np.eye(dims),
},
]
kwargs["weights"] = [0.4, 0.3, 0.2, 0.1]
kwargs["n_gaussians"] = 4
elif name == "gmm_hard":
Model = GaussianMixture
kwargs["config"] = [
{
"mean": np.concatenate([[0, 4], np.zeros(dims - 2)]),
"cov": 0.2 * np.eye(dims),
},
{
"mean": np.concatenate([[0, -4], np.zeros(dims - 2)]),
"cov": 0.1 * np.eye(dims),
},
{
"mean": np.concatenate([[4, 0], np.zeros(dims - 2)]),
"cov": 0.05 * np.eye(dims),
},
{
"mean": np.concatenate([[-4, 0], np.zeros(dims - 2)]),
"cov": 0.1 * np.eye(dims),
},
]
kwargs["weights"] = [0.4, 0.3, 0.2, 0.1]
kwargs["n_gaussians"] = 4
elif name == "gmm_data":
Model = GaussianMixtureWithData
elif name == "eggbox":
Model = EggBox
elif name == "pyramid":
Model = Pyramid
else:
raise ValueError(f"Unknown model: {name}")
return Model, kwargs
def get_model(
config: configparser.ConfigParser, **kwargs
) -> nessai.model.Model:
"""Get the model from the config"""
dims = config.getint("Model", "dims")
model_kwargs = {}
for k, v in config["Model"].items():
if k == "dims":
continue
else:
model_kwargs[k] = from_string(v)
kwargs.update(model_kwargs)
ModelClass, additional_kwargs = get_model_class(
config["General"]["model"].lower(), dims
)
kwargs.update(additional_kwargs)
print(kwargs)
logger.info(f"Model config: dims={dims}, kwargs={kwargs}")
model = ModelClass(dims, **kwargs)
return model
def get_flow_config(config: configparser.ConfigParser) -> dict:
"""Get the flow config dictionary from the config.
Uses the default config from nessai and then updates values given the
config.
"""
base_config = update_config(
dict(model_config=dict(n_inputs=config.getint("Model", "dims")))
)
for setting, value in config["Flow"].items():
if setting == "kwargs":
raise ValueError
if setting in base_config:
base_config[setting] = from_string(value)
elif setting in base_config["model_config"]:
base_config["model_config"][setting] = from_string(value)
else:
base_config["model_config"]["kwargs"][setting] = from_string(value)
logger.info(f"Flow config: {base_config}")
if base_config["model_config"]["ftype"] == "maf":
base_config["model_config"]["kwargs"].pop("linear_transform", None)
base_config["model_config"]["kwargs"].pop("pre_transform", None)
return base_config
def get_sampler(
model: nessai.model.Model, output: str, config: configparser.ConfigParser
) -> FlowSampler:
"""Get the configure sampler."""
kwargs = config["Sampler"]
print(list(kwargs.keys()))
fixed_kwargs = {}
for k, v in kwargs.items():
fixed_kwargs[k] = from_string(v)
importance_nested_sampler = config.getboolean(
"General", "importance_nested_sampler"
)
logger.info(f"Importance sampler: {importance_nested_sampler}")
logger.info(f"Kwargs for sampler: \n {fixed_kwargs}")
flow_config = get_flow_config(config)
logger.info(f"Flow config: \n {flow_config}")
logger.info("Getting sampler")
ins = FlowSampler(
model,
resume=config.getboolean("General", "resume", fallback=False),
output=output,
importance_nested_sampler=importance_nested_sampler,
flow_config=flow_config,
seed=config.getint(
"General", "seed", fallback=np.random.randint(0, 1e4)
),
plot=config.getboolean("General", "plot", fallback=False),
**fixed_kwargs,
)
return ins
def copy_config_file(
config_file: str, output: str, filename: str = "config.ini"
) -> None:
"""Copy the config file to output directory"""
os.makedirs(output, exist_ok=True)
dest = os.path.join(output, filename)
shutil.copyfile(config_file, dest)
def get_output(config, tag=None):
base = config.get("General", "output")
if tag is not None:
output = f"{base}{tag}"
else:
name = (
f"{config.get('General', 'model').lower()}"
f"_{config.get('Model', 'dims')}d"
)
count = 0
while True:
output = os.path.join(base, f"{name}_dev{count}", "")
result = os.path.join(output, "result.json")
if not os.path.exists(output) or not os.path.exists(result):
break
count += 1
if not config.getboolean("General", "increment_dir") and count > 0:
count -= 1
output = os.path.join(base, f"{name}_dev{count}", "")
return output
def get_post_processing_kwargs(config: configparser.ConfigParser) -> dict:
"""Get the post processing keyword arguments"""
kwargs = {}
for k, v in config["Postprocessing"].items():
kwargs[k] = from_string(v)
return kwargs
def save_summary(
sampler: FlowSampler, filename: str, extra: Optional[dict] = None
) -> None:
"""Save a summary of run with only the main information"""
summary = {}
if extra:
logger.info(f"Including: {extra} in summary")
summary.update(extra)
summary["log_evidence"] = sampler.ns.log_evidence
summary["log_evidence_error"] = sampler.ns.log_evidence_error
summary["sampling_time"] = sampler.ns.sampling_time.total_seconds()
summary["likelihood_evaluations"] = sampler.ns.model.likelihood_evaluations
summary["ess"] = sampler.ns.posterior_effective_sample_size
if not sampler.importance_nested_sampler:
summary["p_value"] = sampler.ns.final_p_value
with open(filename, "w") as fp:
json.dump(summary, fp, indent=4)
def run_sampler(
config_file, log_level="INFO", seed=None, summary=True, tag=None
):
"""Run the sampler"""
config = get_config(config_file)
output = get_output(config, tag=tag)
copy_config_file(config_file, output)
plot = config.getboolean("General", "plot", fallback=False)
logger.warning(f"Output file: {output}")
logger.warning(f"Plot={plot}")
if seed:
config["General"]["seed"] = str(seed)
save = config.getboolean("General", "save", fallback=True)
setup_logger(output=output, log_level=log_level)
model = get_model(config)
sampler = get_sampler(model, output, config)
sampler.run(plot=plot, save=save)
if hasattr(model, "truth"):
logger.info(f"True log-evidence: {model.truth}")
if summary:
d = dict()
if config.getboolean("General", "importance_nested_sampler") is True:
d["final_log_evidence"] = sampler.ns.final_state.log_evidence
d[
"final_log_evidence_error"
] = sampler.ns.final_state.log_evidence_error
save_summary(sampler, os.path.join(output, "summary.json"), extra=d)
def run_basic_experiment():
"""Run an experiment from start to finish"""
configure_logger()
args = parse_args()
logger.info(f"Setting random seed to: {args.seed}")
if args.seed:
seed = args.seed
np.random.seed(seed)
else:
seed = None
if hasattr(args, "tag"):
tag = args.tag
else:
tag = None
run_sampler(
args.config,
log_level=args.log_level,
seed=seed,
summary=args.summary,
tag=tag,
)