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 2 commits into
base: dev
Choose a base branch
from

Conversation

bendichter
Copy link
Contributor

@bendichter bendichter commented Jan 27, 2025

Motivation

Added support for dictionary-like operations directly on ProcessingModule objects. Currently, users need to access the data_interfaces attribute to get information about the module's contents (e.g., len(module.data_interfaces)). This PR implements dictionary-like methods on ProcessingModule itself, making the API more intuitive and consistent with Python's container conventions.

This implements the feature requested in #1993 to support operations like len(processing_module) directly.

How to test the behavior?

from pynwb import NWBFile
from pynwb.base import TimeSeries
import datetime

# Create an NWB file with a processing module
nwbfile = NWBFile(
    session_description='demonstrate module dict operations',
    identifier='ABC123',
    session_start_time=datetime.datetime.now(datetime.timezone.utc)
)

behavior_module = nwbfile.create_processing_module(
    name='behavior',
    description='behavioral data'
)

# Add some time series
ts1 = TimeSeries(name='series1', data=[1, 2, 3], unit='m', rate=1.0)
ts2 = TimeSeries(name='series2', data=[4, 5, 6], unit='m', rate=1.0)

behavior_module.add(ts1)
behavior_module.add(ts2)

# Test new dictionary-like operations
print(len(behavior_module))  # 2
print(list(behavior_module.keys()))  # ['series1', 'series2']
print(list(behavior_module.values()))  # [<TimeSeries 'series1'>, <TimeSeries 'series2'>]

for name, interface in behavior_module.items():
    print(f"{name}: {len(interface.data)} samples")

Checklist
[x] Did you update CHANGELOG.md with your changes?
[x] Have you checked our Contributing document?
[x] Have you ensured the PR clearly describes the problem and the solution?
[x] Is your contribution compliant with our coding style? This can be checked running ruff check . && codespell from the source directory.
[x] Have you checked to ensure that there aren't other open Pull Requests for the same change?
[x] Have you included the relevant issue number using "Fix #1993" notation where XXX is the issue number? By including "Fix #1993" you allow GitHub to close issue #1993 when the PR is merged.
Fix #1993

@bendichter bendichter requested a review from rly January 27, 2025 16:22
@bendichter
Copy link
Contributor Author

I'm adding venv/ as this is part of my Cline workflow

Copy link

codecov bot commented Jan 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.75%. Comparing base (739ee54) to head (84223b4).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #2020      +/-   ##
==========================================
+ Coverage   91.73%   91.75%   +0.02%     
==========================================
  Files          27       27              
  Lines        2722     2730       +8     
  Branches      710      710              
==========================================
+ Hits         2497     2505       +8     
  Misses        149      149              
  Partials       76       76              
Flag Coverage Δ
integration 72.89% <50.00%> (-0.07%) ⬇️
unit 82.34% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rly
Copy link
Contributor

rly commented Jan 27, 2025

Great, thanks! Please add a changelog entry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: Support len(processing_module) to get number of items
2 participants