Skip to content

Commit

Permalink
Moved channel obtaining by meta data to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
patgo25 committed Nov 28, 2023
1 parent b64c4af commit d2ab092
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 136 deletions.
61 changes: 25 additions & 36 deletions src/pygama/evt/build_evt.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,6 @@ def build_evt(
f_hit: str,
f_evt: str,
evt_config: str | dict,
meta_path: str = None,
wo_mode: str = "write_safe",
group: str = "/evt/",
tcm_group: str = "/hardware_tcm_1/",
Expand Down Expand Up @@ -951,28 +950,31 @@ def build_evt(
chns = {}

for k, v in tbl_cfg["channels"].items():
if isinstance(v, str):
# only import legend meta data when needed.
# LEGEND collaborators can use the meta keyword
# Why for users w/o access to the LEGEND meta data this is still working
if "meta" in v:
lm = import_module("legendmeta")
lmeta = lm.LegendMetadata(path=meta_path)
chmap = lmeta.channelmap(re.search(r"\d{8}T\d{6}Z", f_dsp).group(0))
m, sys, usa = v.split("_", 2)
tmp = [
f"ch{e}"
for e in chmap.map("daq.rawid")
if chmap.map("daq.rawid")[e]["system"] == sys
]
chns[k] = [
e
for e in tmp
if chmap.map("daq.rawid")[int(e[2:])]["analysis"]["usability"]
== usa
]
else:
chns[k] = [v]
if isinstance(v, dict):
# it is a meta module. module_name must exist
if "module" not in v.keys():
raise ValueError(
"Need module_name to load channel via a meta data module"
)

attr = {}
# the time_key argument is set to the time key of the DSP file
# in case it is not provided by the config
if "time_key" not in v.keys():
attr["time_key"] = re.search(r"\d{8}T\d{6}Z", f_dsp).group(0)

# if "None" do None
elif "None" == v["time_key"]:
attr["time_key"] = None

# load module
p, m = v["module"].rsplit(".", 1)
met = getattr(import_module(p, package=__package__), m)
chns[k] = met(v | attr)

elif isinstance(v, str):
chns[k] = [v]

elif isinstance(v, list):
chns[k] = [e for e in v]

Expand Down Expand Up @@ -1062,19 +1064,6 @@ def build_evt(
wo_mode=wo_mode,
)

# if get_ch flag is true and exists and result dic contains channels entry
# write also channels information
# if "get_ch" in v.keys() and v["get_ch"] and "channels" in result.keys():
# obj = result["channels"]
# if isinstance(obj, np.ndarray):
# obj = Array(result["channels"])
# lstore.write_object(
# obj=obj,
# name=group + k + "_id",
# lh5_file=f_evt,
# wo_mode=wo_mode,
# )

log.info("Done")


Expand Down
20 changes: 20 additions & 0 deletions src/pygama/evt/modules/legend_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Module for importing channel lists from LEGEND meta data
"""
from importlib import import_module

def legend_meta(params:dict) -> list:
# only import legend meta data when needed.
# LEGEND collaborators can use the meta keyword
# While for users w/o access to the LEGEND meta data this is still working
lm = import_module("legendmeta")
lmeta = lm.LegendMetadata(path=params["meta_path"])
chmap = lmeta.channelmap(params['time_key'])
tmp = [
f"ch{e}"
for e in chmap.map("daq.rawid")
if chmap.map("daq.rawid")[e]["system"] == params['system']
]
if 'usability' not in params.keys(): return tmp
else:
return [e for e in tmp if chmap.map("daq.rawid")[int(e[2:])]["analysis"]["usability"] == params['usability']]
2 changes: 1 addition & 1 deletion tests/evt/configs/module-test-evt-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"lar_energy": {
"channels": "spms_on",
"aggregation_mode": "function",
"expression": ".modules.spm.get_energy(0.5,evt.t0,48000,1000,5000)"
"expression": "pygama.evt.modules.spm.get_energy(0.5,evt.t0,48000,1000,5000)"
},
"lar_multiplicity": {
"channels": "spms_on",
Expand Down
170 changes: 85 additions & 85 deletions tests/evt/configs/query-test-evt-config.json
Original file line number Diff line number Diff line change
@@ -1,88 +1,88 @@
{
"channels": {
"geds_on": ["ch1084803", "ch1084804", "ch1121600"]
"channels": {
"geds_on": ["ch1084803", "ch1084804", "ch1121600"]
},
"operations": {
"multiplicity": {
"channels": "geds_on",
"aggregation_mode": "sum",
"expression": "hit.cuspEmax_ctc_cal > a",
"parameters": { "a": 25 },
"initial": 0
},
"operations":{
"multiplicity": {
"channels": "geds_on",
"aggregation_mode": "sum",
"expression": "hit.cuspEmax_ctc_cal > a",
"parameters": { "a": 25 },
"initial": 0
},
"test_sum": {
"channels": "geds_on",
"aggregation_mode": "sum",
"query":"evt.multiplicity == 1",
"expression": "True",
"initial": false
},
"test_first": {
"channels": "geds_on",
"aggregation_mode": ["first", "dsp.tp_0_est"],
"query":"evt.multiplicity == 1",
"expression": "True",
"initial": false
},
"test_first2": {
"channels": "geds_on",
"aggregation_mode": ["first", "dsp.tp_0_est"],
"expression": "True",
"initial": false
},
"test_last": {
"channels": "geds_on",
"aggregation_mode": ["last", "dsp.tp_0_est"],
"query":"evt.multiplicity == 1",
"expression": "True",
"initial": false
},
"test_last2": {
"channels": "geds_on",
"aggregation_mode": ["last", "dsp.tp_0_est"],
"expression": "True",
"initial": false
},
"test_any": {
"channels": "geds_on",
"aggregation_mode": "any",
"query":"evt.multiplicity == 1",
"expression": "True",
"initial": false
},
"test_any2": {
"channels": "geds_on",
"aggregation_mode": "any",
"query":"hit.cuspEmax_ctc_cal >25",
"expression": "True",
"initial": false
},
"test_all": {
"channels": "geds_on",
"aggregation_mode": "all",
"query":"evt.multiplicity == 1",
"expression": "True",
"initial": false
},
"test_all2": {
"channels": "geds_on",
"aggregation_mode": "all",
"query":"hit.cuspEmax_ctc_cal >25",
"expression": "True",
"initial": false
},
"test_vov": {
"channels": "geds_on",
"aggregation_mode": "vov",
"query":"evt.multiplicity == 1",
"expression": "True",
"initial": false
},
"test_vov2": {
"channels": "geds_on",
"aggregation_mode": "vov",
"expression": "True",
"initial": false
}
"test_sum": {
"channels": "geds_on",
"aggregation_mode": "sum",
"query": "evt.multiplicity == 1",
"expression": "True",
"initial": false
},
"test_first": {
"channels": "geds_on",
"aggregation_mode": ["first", "dsp.tp_0_est"],
"query": "evt.multiplicity == 1",
"expression": "True",
"initial": false
},
"test_first2": {
"channels": "geds_on",
"aggregation_mode": ["first", "dsp.tp_0_est"],
"expression": "True",
"initial": false
},
"test_last": {
"channels": "geds_on",
"aggregation_mode": ["last", "dsp.tp_0_est"],
"query": "evt.multiplicity == 1",
"expression": "True",
"initial": false
},
"test_last2": {
"channels": "geds_on",
"aggregation_mode": ["last", "dsp.tp_0_est"],
"expression": "True",
"initial": false
},
"test_any": {
"channels": "geds_on",
"aggregation_mode": "any",
"query": "evt.multiplicity == 1",
"expression": "True",
"initial": false
},
"test_any2": {
"channels": "geds_on",
"aggregation_mode": "any",
"query": "hit.cuspEmax_ctc_cal >25",
"expression": "True",
"initial": false
},
"test_all": {
"channels": "geds_on",
"aggregation_mode": "all",
"query": "evt.multiplicity == 1",
"expression": "True",
"initial": false
},
"test_all2": {
"channels": "geds_on",
"aggregation_mode": "all",
"query": "hit.cuspEmax_ctc_cal >25",
"expression": "True",
"initial": false
},
"test_vov": {
"channels": "geds_on",
"aggregation_mode": "vov",
"query": "evt.multiplicity == 1",
"expression": "True",
"initial": false
},
"test_vov2": {
"channels": "geds_on",
"aggregation_mode": "vov",
"expression": "True",
"initial": false
}
}
}
}
21 changes: 7 additions & 14 deletions tests/evt/test_build_evt.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def test_basics(lgnd_test_data, tmptestdir):
f_dsp=lgnd_test_data.get_path(tcm_path.replace("tcm", "dsp")),
f_hit=lgnd_test_data.get_path(tcm_path.replace("tcm", "hit")),
f_evt=outfile,
meta_path=None,
evt_config=f"{config_dir}/basic-evt-config.json",
wo_mode="o",
group="/evt/",
Expand Down Expand Up @@ -57,7 +56,6 @@ def test_lar_module(lgnd_test_data, tmptestdir):
f_dsp=lgnd_test_data.get_path(tcm_path.replace("tcm", "dsp")),
f_hit=lgnd_test_data.get_path(tcm_path.replace("tcm", "hit")),
f_evt=outfile,
meta_path=None,
evt_config=f"{config_dir}/module-test-evt-config.json",
wo_mode="o",
group="/evt/",
Expand Down Expand Up @@ -85,7 +83,6 @@ def test_lar_t0_vov_module(lgnd_test_data, tmptestdir):
f_dsp=lgnd_test_data.get_path(tcm_path.replace("tcm", "dsp")),
f_hit=lgnd_test_data.get_path(tcm_path.replace("tcm", "hit")),
f_evt=outfile,
meta_path=None,
evt_config=f"{config_dir}/module-test-t0-vov-evt-config.json",
wo_mode="o",
group="/evt/",
Expand All @@ -112,7 +109,6 @@ def test_vov(lgnd_test_data, tmptestdir):
f_dsp=lgnd_test_data.get_path(tcm_path.replace("tcm", "dsp")),
f_hit=lgnd_test_data.get_path(tcm_path.replace("tcm", "hit")),
f_evt=outfile,
meta_path=None,
evt_config=f"{config_dir}/vov-test-evt-config.json",
wo_mode="o",
group="/evt/",
Expand Down Expand Up @@ -144,25 +140,24 @@ def test_graceful_crashing(lgnd_test_data, tmptestdir):
f_tcm = lgnd_test_data.get_path(tcm_path)
f_dsp = lgnd_test_data.get_path(tcm_path.replace("tcm", "dsp"))
f_hit = lgnd_test_data.get_path(tcm_path.replace("tcm", "hit"))
meta_path = None
f_config = f"{config_dir}/basic-evt-config.json"

with pytest.raises(RuntimeError):
build_evt(f_dsp, f_tcm, f_hit, outfile, f_config, meta_path)
build_evt(f_dsp, f_tcm, f_hit, outfile, f_config)

with pytest.raises(RuntimeError):
build_evt(f_tcm, f_hit, f_dsp, outfile, f_config, meta_path)
build_evt(f_tcm, f_hit, f_dsp, outfile, f_config)

with pytest.raises(TypeError):
build_evt(f_tcm, f_dsp, f_hit, outfile, None, meta_path)
build_evt(f_tcm, f_dsp, f_hit, outfile, None)

conf = {"operations": {}}
with pytest.raises(ValueError):
build_evt(f_tcm, f_dsp, f_hit, outfile, conf, meta_path)
build_evt(f_tcm, f_dsp, f_hit, outfile, conf)

conf = {"channels": {"geds_on": ["ch1084803", "ch1084804", "ch1121600"]}}
with pytest.raises(ValueError):
build_evt(f_tcm, f_dsp, f_hit, outfile, conf, meta_path)
build_evt(f_tcm, f_dsp, f_hit, outfile, conf)

conf = {
"channels": {"geds_on": ["ch1084803", "ch1084804", "ch1121600"]},
Expand All @@ -177,7 +172,7 @@ def test_graceful_crashing(lgnd_test_data, tmptestdir):
},
}
with pytest.raises(ValueError):
build_evt(f_tcm, f_dsp, f_hit, outfile, conf, meta_path)
build_evt(f_tcm, f_dsp, f_hit, outfile, conf)


def test_query(lgnd_test_data, tmptestdir):
Expand All @@ -190,7 +185,6 @@ def test_query(lgnd_test_data, tmptestdir):
f_dsp=lgnd_test_data.get_path(tcm_path.replace("tcm", "dsp")),
f_hit=lgnd_test_data.get_path(tcm_path.replace("tcm", "hit")),
f_evt=outfile,
meta_path=None,
evt_config=f"{config_dir}/query-test-evt-config.json",
wo_mode="o",
group="/evt/",
Expand All @@ -207,9 +201,8 @@ def test_skimming(lgnd_test_data, tmptestdir):
f_tcm = lgnd_test_data.get_path(tcm_path)
f_dsp = lgnd_test_data.get_path(tcm_path.replace("tcm", "dsp"))
f_hit = lgnd_test_data.get_path(tcm_path.replace("tcm", "hit"))
meta_path = None
f_config = f"{config_dir}/vov-test-evt-config.json"
build_evt(f_tcm, f_dsp, f_hit, outfile, f_config, meta_path)
build_evt(f_tcm, f_dsp, f_hit, outfile, f_config)

lstore = store.LH5Store()
ac = lstore.read_object("/evt/multiplicity", outfile)[0].nda
Expand Down

0 comments on commit d2ab092

Please sign in to comment.