Skip to content

Commit cf80cd8

Browse files
tibaldojlenain
andauthored
Structure for acceptance test package with GUI (#306)
* Structure for acceptance test package with GUI * Add logging mechanism * Use common matplotlib style * Enable support for different NectarCAM cameras * Default destination for plots in `$NECTARCHAIN_FIGURES`, if the environment variable is set * Enable logging with specified, configurable level * Create `trr_camera_<CAMERA_NAME>` output sub-directory, output everything there, and remove `camera` from parsed arguments (needed for the tools to work properly with the GUI) * Align output figure file name with pickle output * Example log message * Refactor --------- Co-authored-by: Jean-Philippe Lenain <jlenain@in2p3.fr>
1 parent 0f154b5 commit cf80cd8

2 files changed

Lines changed: 133 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sys
2+
3+
from PyQt5.QtWidgets import QApplication
4+
5+
from nectarchain.acceptance_verification_package import hillas_validation
6+
from nectarchain.trr_test_suite.gui import TestRunner
7+
8+
9+
class AcceptanceTestRunner(TestRunner):
10+
# redefine list of test modules
11+
test_modules = {
12+
"Hillas parameter validation": hillas_validation,
13+
}
14+
15+
16+
if __name__ == "__main__":
17+
app = QApplication(sys.argv)
18+
ex = AcceptanceTestRunner()
19+
sys.exit(app.exec())
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import argparse
2+
import copy
3+
import logging
4+
import os
5+
import pickle
6+
import sys
7+
from pathlib import Path
8+
9+
import matplotlib.pyplot as plt
10+
11+
from nectarchain.utils.constants import ALLOWED_CAMERAS
12+
13+
# TODO
14+
# this is just a placeholder that makes a plot for test purposes
15+
# the name is such that in the future it can be replaced
16+
# by the script to validate the distribution of Hillas parameters
17+
18+
logging.basicConfig(
19+
format="%(asctime)s %(name)s %(levelname)s %(message)s",
20+
filename=f"{os.environ.get('NECTARCHAIN_LOG', '/tmp')}/{os.getpid()}/"
21+
f"{Path(__file__).stem}_{os.getpid()}.log",
22+
handlers=[logging.getLogger("__main__").handlers],
23+
)
24+
log = logging.getLogger(__name__)
25+
26+
plt.style.use(
27+
os.path.join(
28+
os.path.abspath(os.path.dirname(__file__)), "../utils/plot_style.mpltstyle"
29+
)
30+
)
31+
32+
33+
def get_args():
34+
"""Parses command-line arguments for the Hillas parameters validation script.
35+
36+
Returns:
37+
argparse.ArgumentParser: The parsed command-line arguments.
38+
"""
39+
40+
parser = argparse.ArgumentParser(description="""To be filled""")
41+
parser.add_argument(
42+
"-r",
43+
"--run_file",
44+
type=str,
45+
help="Run file path and name",
46+
required=False,
47+
default="",
48+
)
49+
parser.add_argument(
50+
"-c",
51+
"--camera",
52+
choices=ALLOWED_CAMERAS,
53+
default=[camera for camera in ALLOWED_CAMERAS if "QM" in camera][0],
54+
help="Process data for a specific NectarCAM camera.",
55+
type=str,
56+
)
57+
parser.add_argument(
58+
"-o",
59+
"--output",
60+
type=str,
61+
help="Output directory",
62+
default=f"{os.environ.get('NECTARCHAIN_FIGURES', f'/tmp/{os.getpid()}')}",
63+
)
64+
parser.add_argument(
65+
"--temp_output", help="Temporary output directory for GUI", default=None
66+
)
67+
parser.add_argument(
68+
"-l",
69+
"--log",
70+
help="log level",
71+
default="info",
72+
type=str,
73+
)
74+
75+
return parser
76+
77+
78+
def main():
79+
parser = get_args()
80+
args = parser.parse_args()
81+
log.setLevel(args.log.upper())
82+
83+
kwargs = copy.deepcopy(vars(args))
84+
kwargs.pop("camera")
85+
camera = args.camera
86+
87+
output_dir = os.path.join(
88+
os.path.abspath(args.output),
89+
f"trr_camera_{camera}/{Path(__file__).stem}",
90+
)
91+
os.makedirs(output_dir, exist_ok=True)
92+
temp_output = os.path.abspath(args.temp_output) if args.temp_output else None
93+
94+
log.info(f"Running the script with arguments: {kwargs}")
95+
96+
# Drop arguments from the script after they are parsed, for the GUI to work properly
97+
sys.argv = sys.argv[:1]
98+
99+
fig, ax = plt.subplots()
100+
ax.plot([0, 1], [0, 1])
101+
102+
fig_name = "hillas"
103+
plot_path = os.path.join(output_dir, f"{fig_name}.png")
104+
plt.savefig(plot_path)
105+
106+
if temp_output:
107+
with open(os.path.join(temp_output, f"plot_{fig_name}.pkl"), "wb") as f:
108+
pickle.dump(fig, f)
109+
110+
plt.close("all")
111+
112+
113+
if __name__ == "__main__":
114+
main()

0 commit comments

Comments
 (0)