From 3dc2258ccca8248eeae21f2ce94cef53df062254 Mon Sep 17 00:00:00 2001 From: Luigi Pertoldi Date: Tue, 9 Apr 2024 15:26:14 +0200 Subject: [PATCH] [evt] switch to `___` as subtable separator in config --- src/pygama/evt/build_evt.py | 40 ++++++++++++++----------------------- tests/evt/test_build_evt.py | 20 ++++++++++++------- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/src/pygama/evt/build_evt.py b/src/pygama/evt/build_evt.py index b66208c32..f78a3ff87 100644 --- a/src/pygama/evt/build_evt.py +++ b/src/pygama/evt/build_evt.py @@ -203,12 +203,10 @@ def build_evt( # if mode not defined in operation, it can only be an operation on the # evt level if "aggregation_mode" not in v.keys(): - var = {} - if "parameters" in v.keys(): - var = var | v["parameters"] - # compute and eventually get rid of evt. suffix - obj = table.eval(v["expression"].replace("evt.", ""), var) + obj = table.eval( + v["expression"].replace("evt.", ""), v.get("parameters", {}) + ) # add attributes if present if "lgdo_attrs" in v.keys(): @@ -235,20 +233,11 @@ def build_evt( ) ) - pars, query, defaultv, srter = None, None, np.nan, None - if "parameters" in v.keys(): - pars = v["parameters"] - if "query" in v.keys(): - query = v["query"] - if "initial" in v.keys(): - defaultv = v["initial"] - if isinstance(defaultv, str) and ( - defaultv in ["np.nan", "np.inf", "-np.inf"] - ): - defaultv = eval(defaultv) - - if "sort" in v.keys(): - srter = v["sort"] + defaultv = v.get("initial", np.nan) + if isinstance(defaultv, str) and ( + defaultv in ["np.nan", "np.inf", "-np.inf"] + ): + defaultv = eval(defaultv) obj = evaluate_expression( datainfo, @@ -259,10 +248,10 @@ def build_evt( expr=v["expression"], n_rows=n_rows, table=table, - parameters=pars, - query=query, + parameters=v.get("parameters", None), + query=v.get("query", None), default_value=defaultv, - sorter=srter, + sorter=v.get("sort", None), ) # add attribute if present @@ -297,7 +286,7 @@ def build_evt( # if names contain slahes, put in sub-tables lvl_ptr = nested_tbl - subfields = field.strip("/").split("/") + subfields = field.strip("/").split("___") for level in subfields: # if we are at the end, just add the field if level == subfields[-1]: @@ -412,8 +401,9 @@ def evaluate_expression( pars_dict = {} if table is not None: - ok_types = (Array, ArrayOfEqualSizedArrays, VectorOfVectors) - pars_dict = {k: v for k, v in table.items() if isinstance(v, ok_types)} + pars_dict = { + k: v for k, v in table.items() if isinstance(v, (Array, VectorOfVectors)) + } # ...or defined through the configuration if parameters: diff --git a/tests/evt/test_build_evt.py b/tests/evt/test_build_evt.py index 7112d2f7e..1d38c2bdd 100644 --- a/tests/evt/test_build_evt.py +++ b/tests/evt/test_build_evt.py @@ -73,20 +73,26 @@ def test_field_nesting(lgnd_test_data, files_config): config = { "channels": {"geds_on": ["ch1084803", "ch1084804", "ch1121600"]}, "outputs": [ - "sub1/timestamp", - "sub2/multiplicity", + "sub1___timestamp", + "sub2___multiplicity", + "sub2___dummy", ], "operations": { - "sub1/timestamp": { + "sub1___timestamp": { "channels": "geds_on", "aggregation_mode": "first_at:dsp.tp_0_est", "expression": "dsp.timestamp", }, - "sub2/multiplicity": { + "sub2___multiplicity": { "channels": "geds_on", "aggregation_mode": "sum", - "expression": "hit.cuspEmax_ctc_cal > a", - "parameters": {"a": 25}, + "expression": "hit.cuspEmax_ctc_cal > 25", + "initial": 0, + }, + "sub2___dummy": { + "channels": "geds_on", + "aggregation_mode": "sum", + "expression": "hit.cuspEmax_ctc_cal > evt.sub1___timestamp", "initial": 0, }, }, @@ -108,7 +114,7 @@ def test_field_nesting(lgnd_test_data, files_config): assert sorted(evt.keys()) == ["sub1", "sub2"] assert sorted(evt.sub1.keys()) == ["timestamp"] - assert sorted(evt.sub2.keys()) == ["multiplicity"] + assert sorted(evt.sub2.keys()) == ["dummy", "multiplicity"] def test_spms_module(lgnd_test_data, files_config):