-
Notifications
You must be signed in to change notification settings - Fork 14
/
make.py
147 lines (118 loc) · 4.36 KB
/
make.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
#!/usr/bin/env python
"""Run gammapy benchmarks"""
import getpass
import importlib
import logging
import platform
import shutil
import subprocess
import sys
import tempfile
import warnings
from pathlib import Path
import click
import numpy as np
import yaml
from psrecord.main import monitor
log = logging.getLogger(__name__)
THIS_REPO = Path(__file__).parent
AVAILABLE_BENCHMARKS = {
"io": "io.py",
"analysis_3d": "analysis_3d.py",
"analysis_3d_joint": "analysis_3d_joint.py",
"lightcurve_1d": "lightcurve_1d.py",
"lightcurve_3d": "lightcurve_3d.py",
"spectrum_1d": "spectrum_1d.py",
"spectrum_1d_joint": "spectrum_1d_joint.py",
"tsmap_estimator": "tsmap_estimator.py",
"ring_background_estimator": "ring_background_estimator.py",
"npred": "npred.py",
}
MONITOR_OPTIONS = {"duration": None, "interval": 0.5, "include_children": True}
def get_provenance():
"""Compute provenance info about software and data used."""
data = {
"env": {
"user": getpass.getuser(),
"machine": platform.machine(),
"system": platform.system(),
},
"software": {},
}
data["software"]["python_executable"] = sys.executable
data["software"]["python_version"] = platform.python_version()
data["software"]["numpy"] = importlib.import_module("numpy").__version__
data["software"]["scipy"] = importlib.import_module("scipy").__version__
data["software"]["astropy"] = importlib.import_module("astropy").__version__
data["software"]["gammapy"] = importlib.import_module("gammapy").__version__
return data
@click.group()
@click.option(
"--log-level",
default="info",
type=click.Choice(["debug", "info", "warning", "error", "critical"]),
)
@click.option("--show-warnings", is_flag=True, help="Show warnings?")
def cli(log_level, show_warnings):
"""
Run and manage Gammapy benchmarks.
"""
levels = dict(
debug=logging.DEBUG,
info=logging.INFO,
warning=logging.WARNING,
error=logging.ERROR,
critical=logging.CRITICAL,
)
logging.basicConfig(level=levels[log_level])
log.setLevel(level=levels[log_level])
if not show_warnings:
warnings.simplefilter("ignore")
@cli.command("run-benchmark", help="Run Gammapy benchmarks")
@click.argument("benchmarks", type=click.Choice(list(AVAILABLE_BENCHMARKS) + ["all"]))
def run_benchmarks(benchmarks):
info = get_provenance()
if benchmarks == "all":
benchmarks = list(AVAILABLE_BENCHMARKS)
else:
benchmarks = [benchmarks]
result = {} # condenses the results from each benchmark
for benchmark in benchmarks:
results_folder = THIS_REPO / f"results/{benchmark}"
results_folder.mkdir(exist_ok=True, parents=True)
results_filename = results_folder / "results.txt"
plot_filename = results_folder / "results.png"
provenance_filename = results_folder / "provenance.yaml"
run_single_benchmark(
benchmark,
logfile=str(results_filename),
plot=str(plot_filename),
**MONITOR_OPTIONS,
)
log.info("Writing {}".format(results_filename))
log.info("Writing {}".format(plot_filename))
with provenance_filename.open("w") as fh:
log.info("Writing {}".format(provenance_filename))
yaml.dump(info, fh, default_flow_style=False)
dict = {}
t, cpu, _, memory = np.loadtxt(results_filename, unpack=True)
dict["total_time"] = float(max(t))
dict["CPU_max"] = float(max(cpu[2:]))
dict["CPU_mean"] = float(np.mean(cpu[2:]))
dict["memory_peak"] = float(np.max(memory))
result[benchmark] = dict
yaml_filename = THIS_REPO / "results/results.yaml"
with yaml_filename.open("w") as fh:
log.info("Writing {}".format(yaml_filename))
yaml.dump(result, fh, default_flow_style=False)
def run_single_benchmark(benchmark, **kwargs):
script_path = Path(AVAILABLE_BENCHMARKS[benchmark]).absolute()
cmd = "python {}".format(script_path)
log.info(f"Executing command: {cmd}")
with tempfile.TemporaryDirectory() as path:
process = subprocess.Popen(cmd, shell=True, cwd=str(path))
pid = process.pid
monitor(pid, **kwargs)
shutil.copyfile(Path(path) / "bench.yaml", f"results/{benchmark}/bench.yaml")
if __name__ == "__main__":
cli()