Preserve arbitrary pose HDF5 attributes in NWB conversion#402
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the NWB conversion pipeline to preserve all HDF5 attributes from JABS pose files by collecting them across the entire HDF5 object tree and embedding them into PoseData.metadata["hdf5_attributes"], with normalization to JSON-serializable types.
Changes:
- Added
_collect_hdf5_attributes(path)to traverse an HDF5 file (root/group/dataset) and collect attributes keyed by HDF5 object path. - Added
_h5_attr_to_jsonable(value)to normalize h5py/numpy attribute values into JSON-friendly Python types (with a warning-based string fallback). - Added unit tests covering scalar/array/bytes normalization and full-file attribute collection + JSON serializability.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/jabs/scripts/cli/convert_to_nwb.py |
Collects and serializes all pose-file HDF5 attributes into PoseData.metadata during NWB conversion. |
tests/scripts/test_convert_to_nwb.py |
Adds tests for attribute normalization and attribute collection behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When converting a JABS pose HDF5 file to NWB, any attributes stored in the source file that JABS does not explicitly parse were previously dropped. This change captures every attribute from the entire pose HDF5 file and carries it into the NWB output as metadata, so arbitrary provenance attributes are not lost.
What changed
src/jabs/scripts/cli/convert_to_nwb.py:_collect_hdf5_attributes(path)walks the whole file (root group, all sub-groups, all datasets) viah5py.visititemsand records each object's attributes keyed by its HDF5 path ("/","poseest","poseest/points", …). Objects with no attributes are omitted._h5_attr_to_jsonable(value)normalizes h5py return types (numpy scalars/arrays,bytesfrom fixed-length string attrs) into plain JSON-friendly types. An unrecognized type is preserved asstr(value)with a warning rather than dropped.pose_to_pose_datastores the result undermetadata["hdf5_attributes"].No NWB-adapter changes were needed:
PoseData.metadataalready flows through the writer into thejabs_metadatascratch JSON and is recovered on read, giving a lossless round-trip.Design note
All attributes are captured verbatim, including ones JABS already parses separately (
version,cm_per_pixel). This preserves the raw on-disk values exactly and avoids a fragile allowlist that would need maintenance as pose formats evolve.Testing
PoseData → NWB → PoseDataround-trip confirms a nestedhdf5_attributesdict comes back identical.ruff checkandruff formatclean.🤖 Generated with Claude Code