Skip to content

Commit

Permalink
Support nwb_version as a fixed-length string (#1669)
Browse files Browse the repository at this point in the history
* Fix #1668 allow for nwb_version to be a fixed-length string

* Updated Changelog

* Update tests/integration/hdf5/test_io.py

* Update src/pynwb/__init__.py

---------

Co-authored-by: Ryan Ly <[email protected]>
  • Loading branch information
oruebel and rly authored Mar 17, 2023
1 parent bdb94a7 commit 3e16eb3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# PyNWB Changelog


## PyNWB 2.3.2 (Upcoming)

### Enhancements and minor changes
- Fixed typos and added codespell GitHub action to check spelling in the future. @yarikoptic [#1648](https://github.com/NeurodataWithoutBorders/pynwb/pull/1648)

### Bug fixes
- Fixed bug in ``NWBHDF5IO.nwb_version`` property to support files written by third-party software with a fixed-length ``nwb_version`` attribute. @oruebel [#1669](https://github.com/NeurodataWithoutBorders/pynwb/pull/1669)

## PyNWB 2.3.1 (February 24, 2023)

### Bug fixes
Expand Down
5 changes: 5 additions & 0 deletions src/pynwb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ def nwb_version(self):
# or when the HDF5 file is not a valid NWB file
except KeyError:
return None, None
# Other system may have written nwb_version as a fixed-length string, resulting in a numpy.bytes_ object
# on read, rather than a variable-length string. To address this, decode the bytes if necessary.
if not isinstance(nwb_version_string, str):
nwb_version_string = nwb_version_string.decode()

# Parse the version string
nwb_version_parts = nwb_version_string.replace("-", ".").replace("_", ".").split(".")
nwb_version = tuple([int(i) if i.isnumeric() else i
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/hdf5/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ def test_nwb_version_property(self):
del io.attrs['nwb_version']
with NWBHDF5IO(self.path, 'r') as io:
self.assertTupleEqual(io.nwb_version, (None, None))
# check that it works when setting the attribute to a fixed-length numpy-bytes string
with File(self.path, mode='a') as io:
io.attrs['nwb_version'] = np.asarray("2.0.5", dtype=np.bytes_)[()]
with NWBHDF5IO(self.path, 'r') as io:
self.assertTupleEqual(io.nwb_version, ("2.0.5", (2, 0, 5)))

def test_check_nwb_version_ok(self):
"""Test that opening a current NWBFile passes the version check"""
Expand Down

0 comments on commit 3e16eb3

Please sign in to comment.