Skip to content

Commit

Permalink
[evt] switch to ___ as subtable separator in config
Browse files Browse the repository at this point in the history
  • Loading branch information
gipert committed Apr 9, 2024
1 parent 1f4757b commit 3dc2258
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
40 changes: 15 additions & 25 deletions src/pygama/evt/build_evt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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:
Expand Down
20 changes: 13 additions & 7 deletions tests/evt/test_build_evt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand All @@ -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):
Expand Down

0 comments on commit 3dc2258

Please sign in to comment.