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

Dict-like methods for ProcessingModule #2020

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## PyNWB 3.0.0 (Upcoming)

### Enhancements and minor changes
- Added dictionary-like operations directly on `ProcessingModule` objects (e.g., `len(processing_module)`). @bendichter [#2020](https://github.com/NeurodataWithoutBorders/pynwb/pull/2020)
- Added `__all__` to modules. @bendichter [#2021](https://github.com/NeurodataWithoutBorders/pynwb/pull/2021)
- Added `pynwb.read_nwb` convenience method to simplify reading an NWBFile written with any backend @h-mayorquin [#1994](https://github.com/NeurodataWithoutBorders/pynwb/pull/1994)

Expand Down
4 changes: 3 additions & 1 deletion docs/gallery/domain/plot_behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,9 @@

with NWBHDF5IO("behavioral_tutorial.nwb", "r") as io:
read_nwbfile = io.read()
print(read_nwbfile.processing["behavior"].children)
behavior_module = read_nwbfile.processing["behavior"]

print(f"Available data interfaces: {list(behavior_module.values())}")

####################
# For instance, we can access the :py:class:`~pynwb.behavior.SpatialSeries` data
Expand Down
40 changes: 40 additions & 0 deletions src/pynwb/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,46 @@ def get_data_interface(self, **kwargs):
warn(PendingDeprecationWarning('get_data_interface will be replaced by get'))
return self.get(kwargs['data_interface_name'])

def __len__(self):
"""Get the number of data interfaces in this ProcessingModule.

Returns
-------
int
Number of data interfaces
"""
return len(self.data_interfaces)

def keys(self):
"""Get the names of data interfaces in this ProcessingModule.

Returns
-------
KeysView
View of interface names
"""
return self.data_interfaces.keys()

def values(self):
"""Get the data interfaces in this ProcessingModule.

Returns
-------
ValuesView
View of interfaces
"""
return self.data_interfaces.values()

def items(self):
"""Get the (name, interface) pairs in this ProcessingModule.

Returns
-------
ItemsView
View of (name, interface) pairs
"""
return self.data_interfaces.items()


@register_class('TimeSeries', CORE_NAMESPACE)
class TimeSeries(NWBDataInterface):
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,35 @@ def test_getitem(self):
tmp = self.pm["test_ts"]
self.assertIs(tmp, ts)

def test_len(self):
"""Test that len() returns number of data interfaces."""
self.assertEqual(len(self.pm), 0)
ts = self._create_time_series()
self.pm.add(ts)
self.assertEqual(len(self.pm), 1)
ts2 = TimeSeries(name="test_ts2", data=[1, 2, 3], unit="unit", rate=1.0)
self.pm.add(ts2)
self.assertEqual(len(self.pm), 2)

def test_dict_methods(self):
"""Test dictionary-like methods (keys, values, items)."""
ts = self._create_time_series()
ts2 = TimeSeries(name="test_ts2", data=[1, 2, 3], unit="unit", rate=1.0)
self.pm.add(ts)
self.pm.add(ts2)

# Test keys()
keys = self.pm.keys()
self.assertEqual(set(keys), {"test_ts", "test_ts2"})

# Test values()
values = self.pm.values()
self.assertEqual(set(values), {ts, ts2})

# Test items()
items = self.pm.items()
self.assertEqual(set(items), {("test_ts", ts), ("test_ts2", ts2)})


class TestTimeSeries(TestCase):
def test_init_no_parent(self):
Expand Down
Loading