From eba0a08479f475be6b11d777688c903bf4332418 Mon Sep 17 00:00:00 2001
From: Stefan Reck <stefan.reck@fau.de>
Date: Fri, 16 Apr 2021 13:02:24 +0200
Subject: [PATCH] use only one command

---
 examples/orcasong_neturino_mc.toml | 10 ++++
 orcasong/from_toml.py              | 81 +++++++++++++++---------------
 orcasong/parser.py                 |  3 +-
 3 files changed, 51 insertions(+), 43 deletions(-)
 create mode 100644 examples/orcasong_neturino_mc.toml

diff --git a/examples/orcasong_neturino_mc.toml b/examples/orcasong_neturino_mc.toml
new file mode 100644
index 0000000..4f54ca0
--- /dev/null
+++ b/examples/orcasong_neturino_mc.toml
@@ -0,0 +1,10 @@
+# the mode to run orcasong in; either 'graph' or 'image'
+mode="graph"
+# arguments for running orcasong (see FileGraph or FileBinner in orcasong.core).
+max_n_hits = 2000
+time_window = [-100, 5000]
+# built-in extractor function to use
+extractor = "neutrino_mc"
+
+[extractor_config]
+# optional arguments for the extractor function can go here. None in this case.
diff --git a/orcasong/from_toml.py b/orcasong/from_toml.py
index 2e24ed7..9958fd1 100644
--- a/orcasong/from_toml.py
+++ b/orcasong/from_toml.py
@@ -3,60 +3,59 @@ import toml
 import orcasong.core
 import orcasong.extractors as extractors
 
-# available extractors. First argument has to be the input filename
+# built-in extractors. First argument has to be the input filename,
+# other parameters can be set via 'extractor_config' dict in the toml
 EXTRACTORS = {
     "neutrino_mc": extractors.get_neutrino_mc_info_extr,
     "neutrino_data": extractors.get_real_data_info_extr,
 }
 
+MODES = {
+    "graph": orcasong.core.FileGraph,
+    "image": orcasong.core.FileBinner,
+}
+
 
-def _add_args(parser):
+def add_parser_run(subparsers):
+    parser = subparsers.add_parser(
+        "run",
+        description='Produce a dl file from an aanet file.')
     parser.add_argument('infile', type=str, help="Aanet file in h5 format.")
     parser.add_argument('toml_file', type=str, help="Orcasong configuration in toml format.")
     parser.add_argument('--detx_file', type=str, default=None, help=(
         "Optional detx file to calibrate on the fly. Can not be used if a "
         "detx_file has also been given in the toml file."))
     parser.add_argument('--outfile', type=str, default=None, help=(
-        "Path to output file. Default: Save with auto ogenerated name in cwd."))
+        "Path to output file. Default: Save with auto generated name in cwd."))
+    parser.set_defaults(func=run_orcasong)
 
 
-def add_parser_filegraph(subparsers):
-    parser = subparsers.add_parser(
-        "graph",
-        description='Produce a graph dl file from an aanet file.')
-    _add_args(parser)
-    parser.set_defaults(func=get_run_orcasong(orcasong.core.FileGraph))
+def run_orcasong(infile, toml_file, detx_file=None, outfile=None):
+    if outfile is None:
+        outfile = f"{os.path.splitext(os.path.basename(infile))[0]}_dl.h5"
 
+    cfg = toml.load(toml_file)
+    processor = _get_verbose(cfg["mode"], MODES)
 
-def add_parser_filebinner(subparsers):
-    parser = subparsers.add_parser(
-        "image",
-        description='Produce an image dl file from an aanet file.')
-    _add_args(parser)
-    parser.set_defaults(func=get_run_orcasong(orcasong.core.FileBinner))
-
-
-def get_run_orcasong(processor):
-    def run_orcasong(infile, toml_file, detx_file=None, outfile=None):
-        if outfile is None:
-            outfile = f"{os.path.splitext(os.path.basename(infile))[0]}_dl.h5"
-
-        cfg = toml.load(toml_file)
-        if "detx_file" in cfg:
-            if detx_file is not None:
-                raise ValueError("detx_file passed to function AND defined in toml")
-            detx_file = cfg.pop("detx_file")
-
-        if "extractor" in cfg:
-            extractor_name = cfg.pop("extractor")
-            extractor_cfg = cfg.pop("extractor_config", {})
-            extractor = EXTRACTORS[extractor_name](infile, **extractor_cfg)
-        else:
-            extractor = None
-
-        processor(
-            det_file=detx_file,
-            extractor=extractor,
-            **cfg,
-        ).run(infile=infile, outfile=outfile)
-    return run_orcasong
+    if "detx_file" in cfg:
+        if detx_file is not None:
+            raise ValueError("detx_file passed to function AND defined in toml")
+        detx_file = cfg.pop("detx_file")
+
+    if "extractor" in cfg:
+        extractor_cfg = cfg.pop("extractor_config", {})
+        extractor = _get_verbose(cfg.pop("extractor"), EXTRACTORS)(infile, **extractor_cfg)
+    else:
+        extractor = None
+
+    processor(
+        det_file=detx_file,
+        extractor=extractor,
+        **cfg,
+    ).run(infile=infile, outfile=outfile)
+
+
+def _get_verbose(key, d):
+    if key not in d:
+        raise KeyError(f"Unknown key {key} (available: {list(d.keys())}")
+    return d[key]
diff --git a/orcasong/parser.py b/orcasong/parser.py
index 6ff14d4..69eecb5 100644
--- a/orcasong/parser.py
+++ b/orcasong/parser.py
@@ -120,8 +120,7 @@ def main():
     )
     subparsers = parser.add_subparsers()
 
-    from_toml.add_parser_filegraph(subparsers)
-    from_toml.add_parser_filebinner(subparsers)
+    from_toml.add_parser_run(subparsers)
     _add_parser_concatenate(subparsers)
     _add_parser_h5shuffle(subparsers)
     _add_parser_h5shuffle2(subparsers)
-- 
GitLab