Skip to content

Commit cf7ecd6

Browse files
ggmarshallCopilotCopilot
authored
updates for dplms, move cuts out of pygama to script (#31)
* updates for dplms, move cuts out of pygama to script * Update src/legenddataflowscripts/par/geds/dsp/dplms.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * remove value error for testing Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * specify pickle protocol Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix(dplms): only load inplots when plot output is requested Agent-Logs-Url: https://github.com/legend-exp/legend-dataflow-scripts/sessions/cebc3462-f6e5-4ec3-ad9d-ab40fad5ab54 Co-authored-by: ggmarshall <72088559+ggmarshall@users.noreply.github.com> * Update pygama dependency version to 2.3.8a3 --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 66e026a commit cf7ecd6

3 files changed

Lines changed: 39 additions & 25 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ dynamic = ["version"]
5252
dependencies = [
5353
"colorlog",
5454
"dbetto>=1.2.3",
55-
"pygama>=2.3.0a1",
55+
"pygama>=2.3.8a3",
5656
"dspeed>=2.0",
5757
"pylegendmeta>=1.2.5",
5858
"legend-pydataobj>=1.16",

src/legenddataflowscripts/par/geds/dsp/dplms.py

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import pygama.math.distributions as pmd # noqa: F401
1010
from dbetto.catalog import Props
1111
from lgdo import Array, Table, lh5
12+
from pygama.pargen.data_cleaning import generate_cuts
1213
from pygama.pargen.dplms_ge_dict import dplms_ge_dict
14+
from pygama.pargen.dsp_optimize import run_one_dsp
1315

1416
from ....utils import build_log, convert_dict_np_to_float
1517

@@ -114,12 +116,12 @@ def par_geds_dsp_dplms() -> None:
114116
energies = lh5.read_as(
115117
f"{args.raw_table_name}/daqenergy", fft_files, library="np"
116118
)
117-
idxs = np.where(energies <= 10)[0]
119+
eidxs = np.where(energies <= 10)[0]
118120
raw_fft = lh5.read(
119121
args.raw_table_name,
120122
fft_files,
121123
n_rows=dplms_dict["n_baselines"],
122-
idx=idxs,
124+
idx=eidxs,
123125
)
124126
t1 = time.time()
125127
msg = f"Time to load fft data {(t1 - t0):.2f} s, total events {len(raw_fft)}"
@@ -142,30 +144,43 @@ def par_geds_dsp_dplms() -> None:
142144
if isinstance(dsp_config, str | list):
143145
dsp_config = Props.read_from(dsp_config)
144146

147+
dsp_fft = run_one_dsp(raw_fft, dsp_config, db_dict=db_dict)
148+
149+
cut_dict = generate_cuts(dsp_fft, cut_dict=dplms_dict["bls_cut_pars"])
150+
log.debug("Cuts are %s", cut_dict)
151+
idxs = np.full(len(dsp_fft), True, dtype=bool)
152+
for outname, info in cut_dict.items():
153+
outcol = dsp_fft.eval(info["expression"], info.get("parameters", None))
154+
dsp_fft.add_column(outname, outcol)
155+
for cut in cut_dict:
156+
idxs = dsp_fft[cut].nda & idxs
157+
158+
selected_eidxs = eidxs[: len(dsp_fft)]
159+
raw_fft = lh5.read(
160+
args.raw_table_name,
161+
fft_files,
162+
n_rows=dplms_dict["n_baselines"],
163+
idx=selected_eidxs[idxs],
164+
)
165+
166+
log.debug("Applied Cuts")
167+
168+
out_dict, plot_dict = dplms_ge_dict(
169+
raw_fft,
170+
raw_cal,
171+
dsp_config,
172+
db_dict,
173+
dplms_dict,
174+
fom_func=eval(dplms_dict.get("fom_func", "pmd.gauss_on_step")),
175+
display=1 if args.plot_path else 0,
176+
)
145177
if args.plot_path:
146-
out_dict, plot_dict = dplms_ge_dict(
147-
raw_fft,
148-
raw_cal,
149-
dsp_config,
150-
db_dict,
151-
dplms_dict,
152-
fom_func=eval(dplms_dict.get("fom_func", "pmd.gauss_on_step")),
153-
display=1,
154-
)
155178
if args.inplots:
156179
with Path(args.inplots).open("rb") as r:
157180
inplot_dict = pkl.load(r)
158181
inplot_dict.update({"dplms": plot_dict})
159-
160-
else:
161-
out_dict = dplms_ge_dict(
162-
raw_fft,
163-
raw_cal,
164-
dsp_config,
165-
db_dict,
166-
dplms_dict,
167-
fom_func=eval(dplms_dict.get("fom_func", "pmd.gauss_on_step")),
168-
)
182+
else:
183+
inplot_dict = {"dplms": plot_dict}
169184

170185
coeffs = out_dict["dplms"].pop("coefficients")
171186
dplms_pars = Table(col_dict={"coefficients": Array(coeffs)})
@@ -177,7 +192,7 @@ def par_geds_dsp_dplms() -> None:
177192
else:
178193
out_dict = {}
179194
dplms_pars = Table(col_dict={"coefficients": Array([])})
180-
if args.inplots:
195+
if args.plot_path and args.inplots:
181196
with Path(args.inplots).open("rb") as r:
182197
inplot_dict = pkl.load(r)
183198
else:
@@ -192,7 +207,6 @@ def par_geds_dsp_dplms() -> None:
192207
lh5_file=args.lh5_path,
193208
wo_mode="overwrite",
194209
)
195-
196210
Path(args.dsp_pars).parent.mkdir(parents=True, exist_ok=True)
197211
Props.write_to(args.dsp_pars, convert_dict_np_to_float(db_dict))
198212

src/legenddataflowscripts/par/geds/dsp/eopt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def par_geds_dsp_eopt() -> None:
416416

417417
Path(args.qbb_grid_path).parent.mkdir(parents=True, exist_ok=True)
418418
with Path(args.qbb_grid_path).open("wb") as f:
419-
pkl.dump(optimisers, f)
419+
pkl.dump({"eopt": optimisers}, f, protocol=pkl.HIGHEST_PROTOCOL)
420420

421421
else:
422422
Path(args.qbb_grid_path).touch()

0 commit comments

Comments
 (0)