Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default load_namespaces=True #1748

Merged
merged 26 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0345a46
default load_namespaces=True
bendichter Jul 28, 2023
792451d
Update CHANGELOG.md
bendichter Jul 28, 2023
382f0fb
change logic around to ignore load_namespaces when it cannot be used
bendichter Jul 28, 2023
acb1c8d
Merge remote-tracking branch 'origin/load_namespaces_true' into load_…
bendichter Jul 28, 2023
26faa0c
flake8
bendichter Jul 29, 2023
955fb8d
Merge branch 'dev' into load_namespaces_true
bendichter Jul 29, 2023
2c6a415
Update __init__.py
rly Jul 29, 2023
7f1a1e3
Merge branch 'dev' into load_namespaces_true
bendichter Jul 29, 2023
9d5ab33
fix back-compatibility test
bendichter Jul 31, 2023
41ba0db
Merge remote-tracking branch 'origin/load_namespaces_true' into load_…
bendichter Jul 31, 2023
6c941a0
flake8
bendichter Jul 31, 2023
f8e35d3
flake8
bendichter Jul 31, 2023
df2a32e
remove warning from test
bendichter Aug 1, 2023
e7023c2
Merge branch 'dev' into load_namespaces_true
oruebel Sep 6, 2023
8087153
rmv useless test
bendichter Sep 21, 2023
8a95beb
Merge remote-tracking branch 'origin/load_namespaces_true' into load_…
bendichter Sep 21, 2023
28ba0d1
Merge branch 'dev' into load_namespaces_true
bendichter Sep 21, 2023
6c4d772
Merge branch 'dev' into load_namespaces_true
rly Oct 4, 2023
a833c9e
Merge branch 'dev' into load_namespaces_true
rly Oct 4, 2023
8b86a20
Update tests, validate script for default load ns
rly Oct 4, 2023
ac71956
Update CHANGELOG.md
rly Oct 4, 2023
2384a69
Update docs/gallery/advanced_io/linking_data.py
rly Oct 4, 2023
a0daffa
Update docs/gallery/advanced_io/linking_data.py
rly Oct 4, 2023
930eef6
Update test_file.py
rly Oct 4, 2023
a8cf307
Merge branch 'dev' into load_namespaces_true
rly Oct 22, 2023
ad0e08f
Merge branch 'dev' into load_namespaces_true
rly Nov 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Enhancements and minor changes
- Add `TimeSeries.get_timestamps()`. @bendichter [#1741](https://github.com/NeurodataWithoutBorders/pynwb/pull/1741)
- Add `TimeSeries.get_data_in_units()`. @bendichter [#1745](https://github.com/NeurodataWithoutBorders/pynwb/pull/1745)
- For `NWBHDF5IO()`, change the default of arg `load_namespaces` from `False` to `True`. @bendichter [#1748](https://github.com/NeurodataWithoutBorders/pynwb/pull/1748)
- Updated `ExternalResources` name change to `HERD`, along with HDMF 3.9.0 being the new minimum. @mavaylon1 [#1754](https://github.com/NeurodataWithoutBorders/pynwb/pull/1754)

### Documentation and tutorial enhancements
Expand Down
81 changes: 37 additions & 44 deletions docs/gallery/advanced_io/linking_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,50 @@

PyNWB supports linking between files using external links.

"""
Example Use Case: Integrating data from multiple files
---------------------------------------------------------

####################
# Example Use Case: Integrating data from multiple files
# ---------------------------------------------------------
#
# NBWContainer classes (e.g., :py:class:`~pynwb.base.TimeSeries`) support the integration of data stored in external
# HDF5 files with NWB data files via external links. To make things more concrete, let's look at the following use
# case. We want to simultaneously record multiple data streams during data acquisition. Using the concept of external
# links allows us to save each data stream to an external HDF5 files during data acquisition and to
# afterwards link the data into a single NWB:N file. In this case, each recording becomes represented by a
# separate file-system object that can be set as read-only once the experiment is done. In the following
# we are using :py:meth:`~pynwb.base.TimeSeries` as an example, but the same approach works for other
# NWBContainers as well.
#
NBWContainer classes (e.g., :py:class:`~pynwb.base.TimeSeries`) support the integration of data stored in external
HDF5 files with NWB data files via external links. To make things more concrete, let's look at the following use
case. We want to simultaneously record multiple data streams during data acquisition. Using the concept of external
links allows us to save each data stream to an external HDF5 files during data acquisition and to
afterwards link the data into a single NWB:N file. In this case, each recording becomes represented by a
rly marked this conversation as resolved.
Show resolved Hide resolved
separate file-system object that can be set as read-only once the experiment is done. In the following
we are using :py:meth:`~pynwb.base.TimeSeries` as an example, but the same approach works for other
NWBContainers as well.

####################
# .. tip::
#
# The same strategies we use here for creating External Links also apply to Soft Links.
# The main difference between soft and external links is that soft links point to other
# objects within the same file while external links point to objects in external files.
#
.. tip::

####################
# .. tip::
#
# In the case of :py:meth:`~pynwb.base.TimeSeries`, the uncorrected timestamps generated by the acquisition
# system can be stored (or linked) in the *sync* group. In the NWB:N format, hardware-recorded time data
# must then be corrected to a common time base (e.g., timestamps from all hardware sources aligned) before
# it can be included in the *timestamps* of the *TimeSeries*. This means, in the case
# of :py:meth:`~pynwb.base.TimeSeries` we need to be careful that we are not including data with incompatible
# timestamps in the same file when using external links.
#
The same strategies we use here for creating External Links also apply to Soft Links.
The main difference between soft and external links is that soft links point to other
objects within the same file while external links point to objects in external files.

####################
# .. warning::
#
# External links can become stale/break. Since external links are pointing to data in other files
# external links may become invalid any time files are modified on the file system, e.g., renamed,
# moved or access permissions are changed.
#
.. tip::

####################
# Creating test data
# ---------------------------
#
# In the following we are creating two :py:meth:`~pynwb.base.TimeSeries` each written to a separate file.
# We then show how we can integrate these files into a single NWBFile.
In the case of :py:meth:`~pynwb.base.TimeSeries`, the uncorrected timestamps generated by the acquisition
system can be stored (or linked) in the *sync* group. In the NWB:N format, hardware-recorded time data
rly marked this conversation as resolved.
Show resolved Hide resolved
must then be corrected to a common time base (e.g., timestamps from all hardware sources aligned) before
it can be included in the *timestamps* of the *TimeSeries*. This means, in the case
of :py:meth:`~pynwb.base.TimeSeries` we need to be careful that we are not including data with incompatible
timestamps in the same file when using external links.


.. warning::

External links can become stale/break. Since external links are pointing to data in other files
external links may become invalid any time files are modified on the file system, e.g., renamed,
moved or access permissions are changed.


Creating test data
---------------------------

In the following we are creating two :py:meth:`~pynwb.base.TimeSeries` each written to a separate file.
We then show how we can integrate these files into a single NWBFile.
"""

# sphinx_gallery_thumbnail_path = 'figures/gallery_thumbnails_linking_data.png'

from datetime import datetime
from uuid import uuid4

Expand Down
2 changes: 1 addition & 1 deletion docs/gallery/general/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def __init__(self, **kwargs):
# explicitly specify this. This behavior is enabled by the *load_namespaces*
# argument to the :py:class:`~pynwb.NWBHDF5IO` constructor.

with NWBHDF5IO("cache_spec_example.nwb", mode="r", load_namespaces=True) as io:
with NWBHDF5IO("cache_spec_example.nwb", mode="r") as io:
nwbfile = io.read()

####################
Expand Down
16 changes: 6 additions & 10 deletions src/pynwb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os.path
from pathlib import Path
from copy import deepcopy
from warnings import warn
import h5py

from hdmf.spec import NamespaceCatalog
Expand Down Expand Up @@ -206,8 +205,9 @@ class NWBHDF5IO(_HDF5IO):
'doc': 'the mode to open the HDF5 file with, one of ("w", "r", "r+", "a", "w-", "x")',
'default': 'r'},
{'name': 'load_namespaces', 'type': bool,
'doc': 'whether or not to load cached namespaces from given path - not applicable in write mode',
'default': False},
'doc': ('whether or not to load cached namespaces from given path - not applicable in write mode '
'or when `manager` is not None or when `extensions` is not None'),
'default': True},
{'name': 'manager', 'type': BuildManager, 'doc': 'the BuildManager to use for I/O', 'default': None},
{'name': 'extensions', 'type': (str, TypeMap, list),
'doc': 'a path to a namespace, a TypeMap, or a list consisting paths to namespaces and TypeMaps',
Expand All @@ -223,14 +223,10 @@ def __init__(self, **kwargs):
popargs('path', 'mode', 'manager', 'extensions', 'load_namespaces',
'file', 'comm', 'driver', 'herd_path', kwargs)
# Define the BuildManager to use
if mode in 'wx' or manager is not None or extensions is not None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this introduced a bug.

load_namespaces = False

if load_namespaces:
if manager is not None:
warn("loading namespaces from file - ignoring 'manager'")
if extensions is not None:
warn("loading namespaces from file - ignoring 'extensions' argument")
# namespaces are not loaded when creating an NWBHDF5IO object in write mode
if 'w' in mode or mode == 'x':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here was correct.

raise ValueError("cannot load namespaces from file when writing to it")

tm = get_type_map()
super().load_namespaces(tm, path, file=file_obj, driver=driver)
Expand Down
1 change: 0 additions & 1 deletion tests/back_compat/test_import_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def test_outer_import_structure(self):
"spec",
"testing",
"validate",
"warn",
]
for member in expected_structure:
self.assertIn(member=member, container=current_structure)
2 changes: 1 addition & 1 deletion tests/back_compat/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_read(self):
with self.subTest(file=f.name):
with warnings.catch_warnings(record=True) as warnings_on_read:
warnings.simplefilter("always")
with NWBHDF5IO(str(f), 'r', load_namespaces=True) as io:
with NWBHDF5IO(str(f), 'r') as io:
errors = validate(io)
io.read()
for w in warnings_on_read:
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/hdf5/test_ecephys.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ def setUpTwoElectricalSeries(self):
data2 = list(zip(reversed(range(10)), reversed(range(10, 20))))
timestamps = list(map(lambda x: x/10., range(10)))
es1 = ElectricalSeries(name='test_eS1', data=data1, electrodes=region1, timestamps=timestamps)
es2 = ElectricalSeries(name='test_eS2', data=data2, electrodes=region2, channel_conversion=[4., .4],
timestamps=timestamps)
es2 = ElectricalSeries(
name='test_eS2', data=data2, electrodes=region2, channel_conversion=[4., .4], timestamps=timestamps
)
return es1, es2

def addContainer(self, nwbfile):
Expand Down
2 changes: 1 addition & 1 deletion tests/read_dandi/test_read_dandi.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_read_first_nwb_asset(self):
s3_url = first_asset.get_content_url(follow_redirects=1, strip_query=True)

try:
with NWBHDF5IO(path=s3_url, load_namespaces=True, driver="ros3") as io:
with NWBHDF5IO(path=s3_url, driver="ros3") as io:
io.read()
except Exception as e:
print(traceback.format_exc())
Expand Down
7 changes: 3 additions & 4 deletions tests/unit/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ def test_simple(self):
lab='Chang Lab')
with NWBHDF5IO(self.path, 'w') as io:
io.write(nwbfile)
with NWBHDF5IO(self.path, 'r', load_namespaces=True) as reader:
with NWBHDF5IO(self.path, 'r') as reader:
nwbfile = reader.read()


Expand All @@ -563,9 +563,8 @@ def test_simple(self):
with NWBHDF5IO(self.path, 'w') as io:
io.write(nwbfile, cache_spec=False)

with self.assertWarnsWith(UserWarning, "No cached namespaces found in %s" % self.path):
with NWBHDF5IO(self.path, 'r', load_namespaces=True) as reader:
nwbfile = reader.read()
with NWBHDF5IO(self.path, 'r') as reader:
nwbfile = reader.read()
bendichter marked this conversation as resolved.
Show resolved Hide resolved


class TestTimestampsRefDefault(TestCase):
Expand Down
8 changes: 3 additions & 5 deletions tests/validation/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,13 @@ def test_validate_io_cached(self):

def test_validate_io_cached_extension(self):
"""Test that validating a file with cached spec against its cached namespaces succeeds."""
with NWBHDF5IO('tests/back_compat/2.1.0_nwbfile_with_extension.nwb', 'r', load_namespaces=True) as io:
with NWBHDF5IO('tests/back_compat/2.1.0_nwbfile_with_extension.nwb', 'r') as io:
errors = validate(io)
self.assertEqual(errors, [])

def test_validate_io_cached_extension_pass_ns(self):
"""Test that validating a file with cached extension spec against the extension namespace succeeds."""
with NWBHDF5IO('tests/back_compat/2.1.0_nwbfile_with_extension.nwb', 'r', load_namespaces=True) as io:
with NWBHDF5IO('tests/back_compat/2.1.0_nwbfile_with_extension.nwb', 'r') as io:
errors = validate(io, 'ndx-testextension')
self.assertEqual(errors, [])

Expand All @@ -237,9 +237,7 @@ def test_validate_io_cached_core_with_io(self):
For back-compatability, test that validating a file with cached extension spec against the core
namespace succeeds when using the `io` + `namespace` keywords.
"""
with NWBHDF5IO(
path='tests/back_compat/2.1.0_nwbfile_with_extension.nwb', mode='r', load_namespaces=True
) as io:
with NWBHDF5IO(path='tests/back_compat/2.1.0_nwbfile_with_extension.nwb', mode='r') as io:
results = validate(io=io, namespace="core")
self.assertEqual(results, [])

Expand Down
Loading