Skip to content

Commit f42c034

Browse files
Preserve staged cache source entry metadata
1 parent f31056c commit f42c034

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/spine/io/read/stage_hdf5.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ def get(self, idx: int) -> dict[str, object]:
401401
data: dict[str, object] = {
402402
"file_index": file_idx,
403403
"file_entry_index": entry_idx,
404+
"source_file_entry_index": entry_idx,
404405
}
405406
data.update(self._source_info.get(file_idx, {}))
406407
product_stage_map = self._resolved_products[file_idx]

test/test_io/test_read/test_hdf5.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,34 @@ def test_stage_hdf5_reader_loads_one_stage(tmp_path):
290290
reader.close()
291291

292292

293+
def test_stage_hdf5_reader_fills_missing_source_entry_index(tmp_path):
294+
"""Older/minimal stage caches should expose source entry metadata."""
295+
path = tmp_path / "stage_cache.h5"
296+
writer = StageHDF5Writer(str(path), overwrite=True)
297+
writer.write_stage(
298+
"deghosting",
299+
{
300+
"index": np.asarray([0, 1]),
301+
"source_file_name": np.asarray(["source.root", "source.root"]),
302+
"source_file_size": np.asarray([10, 10]),
303+
"source_file_mtime_ns": np.asarray([20, 20]),
304+
"dummy_data": [
305+
np.asarray([[1.0, 2.0]]),
306+
np.asarray([[3.0, 4.0]]),
307+
],
308+
},
309+
)
310+
writer.finalize_stage("deghosting")
311+
writer.close()
312+
313+
reader = StageHDF5Reader("deghosting", str(path), build_classes=False)
314+
entry = reader.get(1)
315+
assert entry["file_entry_index"] == 1
316+
assert entry["source_file_entry_index"] == 1
317+
np.testing.assert_array_equal(entry["dummy_data"], np.asarray([[3.0, 4.0]]))
318+
reader.close()
319+
320+
293321
def test_stage_hdf5_reader_rejects_incomplete_stages(tmp_path):
294322
"""Incomplete stages should be rejected by default."""
295323
path = tmp_path / "stage_cache.h5"

test/test_io/test_write/test_hdf5.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,36 @@ def test_stage_hdf5_writer_respects_explicit_keys(tmp_path):
529529
assert "extra_tensor" not in events.dtype.names
530530

531531

532+
def test_stage_hdf5_writer_preserves_source_entry_with_explicit_keys(tmp_path):
533+
"""Stage caches should preserve source entry provenance from file_entry_index."""
534+
cache_path = tmp_path / "cache.h5"
535+
writer = StageHDF5Writer(
536+
str(cache_path),
537+
overwrite=True,
538+
keys=["dummy_data"],
539+
)
540+
writer.write_stage(
541+
"deghosting",
542+
{
543+
"index": np.asarray([0]),
544+
"file_entry_index": np.asarray([7]),
545+
"source_file_name": np.asarray(["source.root"]),
546+
"source_file_size": np.asarray([10]),
547+
"source_file_mtime_ns": np.asarray([20]),
548+
"dummy_data": [np.asarray([[1.0, 2.0]])],
549+
"extra_tensor": [np.asarray([[3.0, 4.0]])],
550+
},
551+
)
552+
writer.finalize_stage("deghosting")
553+
writer.close()
554+
555+
with h5py.File(cache_path, "r") as out_file:
556+
stage_group = out_file["stages"]["deghosting"]
557+
assert "source_file_entry_index" in stage_group
558+
assert "source_file_entry_index" in stage_group["events"].dtype.names
559+
np.testing.assert_array_equal(stage_group["source_file_entry_index"][:], [7])
560+
561+
532562
def test_stage_hdf5_writer_rejects_mismatched_source(tmp_path):
533563
"""Writing a later stage with different source provenance should fail."""
534564
source_a = tmp_path / "source_a.root"

0 commit comments

Comments
 (0)