Skip to content

Commit f32171e

Browse files
authored
fix for tcm with multiple coin cols (#611)
* fix for multiple coin cols
1 parent 644e9b4 commit f32171e

File tree

2 files changed

+75
-60
lines changed

2 files changed

+75
-60
lines changed

src/pygama/evt/build_tcm.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ def build_tcm(
9999
if out_fields is not None and not isinstance(out_fields, list):
100100
out_fields = [out_fields]
101101

102+
if len(coin_cols) != len(coin_windows):
103+
if len(coin_windows) == 1:
104+
coin_windows = coin_windows * len(coin_cols)
105+
else:
106+
msg = (
107+
"coin_cols and coin_windows must have the same length, "
108+
f"got {len(coin_cols)} and {len(coin_windows)}"
109+
)
110+
raise ValueError(msg)
111+
if len(coin_cols) != len(window_refs):
112+
if len(window_refs) == 1:
113+
window_refs = window_refs * len(coin_cols)
114+
else:
115+
msg = (
116+
"coin_cols and coin_windows must have the same length, "
117+
f"got {len(coin_cols)} and {len(window_refs)}"
118+
)
119+
raise ValueError(msg)
120+
102121
iterators = []
103122
table_keys = []
104123
all_tables = []

tests/evt/test_build_tcm.py

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -105,41 +105,16 @@ def test_generate_tcm_cols(lgnd_test_data):
105105
hash_func=None,
106106
buffer_len=1,
107107
)
108+
# fmt: off
108109
assert np.array_equal(
109110
tcm_cols.table_key.flattened_data.nda,
110111
[
111-
1,
112-
0,
113-
2,
114-
1,
115-
2,
116-
1,
117-
2,
118-
1,
119-
1,
120-
1,
121-
0,
122-
1,
123-
1,
124-
2,
125-
2,
126-
1,
127-
2,
128-
1,
129-
2,
130-
0,
131-
0,
132-
2,
133-
2,
134-
2,
135-
0,
136-
0,
137-
0,
138-
0,
139-
0,
140-
0,
112+
1, 0, 2, 1, 2, 1, 2, 1, 1, 1, 0,
113+
1, 1, 2, 2, 1, 2, 1, 2, 0, 0, 2,
114+
2, 2, 0, 0, 0, 0, 0, 0,
141115
],
142116
)
117+
# fmt: on
143118
# test invalid hash func
144119
with pytest.raises(NotImplementedError):
145120
evt.build_tcm(
@@ -175,41 +150,61 @@ def test_generate_tcm_cols(lgnd_test_data):
175150
tcm_cols.table_key.cumulative_length.nda,
176151
[30],
177152
)
153+
# fmt: off
178154
assert np.array_equal(
179155
tcm_cols.table_key.flattened_data.nda,
180156
[
181-
1084804,
182-
1084803,
183-
1121600,
184-
1084804,
185-
1121600,
186-
1084804,
187-
1121600,
188-
1084804,
189-
1084804,
190-
1084804,
191-
1084803,
192-
1084804,
193-
1084804,
194-
1121600,
195-
1121600,
196-
1084804,
197-
1121600,
198-
1084804,
199-
1121600,
200-
1084803,
201-
1084803,
202-
1121600,
203-
1121600,
204-
1121600,
205-
1084803,
206-
1084803,
207-
1084803,
208-
1084803,
209-
1084803,
210-
1084803,
157+
1084804, 1084803, 1121600, 1084804, 1121600, 1084804,
158+
1121600, 1084804, 1084804, 1084804, 1084803, 1084804,
159+
1084804, 1121600, 1121600, 1084804, 1121600, 1084804,
160+
1121600, 1084803, 1084803, 1121600, 1121600, 1121600,
161+
1084803, 1084803, 1084803, 1084803, 1084803, 1084803,
211162
],
212163
)
164+
# fmt: on
165+
166+
167+
def test_build_tcm_multiple_cols(lgnd_test_data):
168+
f_raw = lgnd_test_data.get_path(
169+
"lh5/prod-ref-l200/generated/tier/raw/cal/p03/r001/l200-p03-r001-cal-20230318T012144Z-tier_raw.lh5"
170+
)
171+
172+
with pytest.raises(ValueError):
173+
evt.build_tcm(
174+
[(f_raw, ["ch1084803/raw", "ch1084804/raw", "ch1121600/raw"])],
175+
coin_cols="timestamp",
176+
window_refs=["last", "last"],
177+
)
178+
with pytest.raises(ValueError):
179+
evt.build_tcm(
180+
[(f_raw, ["ch1084803/raw", "ch1084804/raw", "ch1121600/raw"])],
181+
coin_cols=["timestamp"],
182+
coin_windows=[1, 2],
183+
)
184+
tcm = evt.build_tcm(
185+
[(f_raw, ["ch1084803/raw", "ch1084804/raw", "ch1121600/raw"])],
186+
coin_cols=["timestamp", "table_key"],
187+
)
188+
assert isinstance(tcm, Table)
189+
assert isinstance(tcm.table_key, VectorOfVectors)
190+
assert isinstance(tcm.row_in_table, VectorOfVectors)
191+
# fmt: off
192+
assert np.array_equal(
193+
tcm.table_key.flattened_data.nda,
194+
[
195+
1084804, 1084803, 1121600, 1084804, 1121600, 1084804, 1121600,
196+
1084804, 1084804, 1084804, 1084803, 1084804, 1084804, 1121600,
197+
1121600, 1084804, 1121600, 1084804, 1121600, 1084803, 1084803,
198+
1121600, 1121600, 1121600, 1084803, 1084803, 1084803, 1084803,
199+
1084803, 1084803,
200+
],
201+
)
202+
# fmt: on
203+
204+
assert np.array_equal(
205+
tcm.table_key.cumulative_length.nda,
206+
np.arange(1, 31),
207+
)
213208

214209

215210
def test_build_tcm_write(lgnd_test_data, tmp_dir):
@@ -224,6 +219,7 @@ def test_build_tcm_write(lgnd_test_data, tmp_dir):
224219
out_name="hardware_tcm",
225220
wo_mode="of",
226221
)
222+
227223
assert os.path.exists(out_file)
228224
tcm_cols = lh5.read("hardware_tcm", out_file)
229225
assert isinstance(tcm_cols, lgdo.Struct)

0 commit comments

Comments
 (0)