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

Add methods to find objects of a given neurodata type / pynwb class #1737

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from

Conversation

rly
Copy link
Contributor

@rly rly commented Jul 25, 2023

Motivation

A common feature request is to find objects of a given neurodata type or pynwb class.

For example:

  • get all objects with the neurodata type "EcephysSpecimen" from the namespace "ndx-aibs-ecephys"
  • get all objects with the neurodata type "Subject" from the namespace "core" including subclasses/subtypes such as "EcephysSpecimen" from the namespace "ndx-aibs-ecephys"
  • get all objects that are instances of a PyNWB class such as pynwb.file.Subject

Fix #560

TODO

  • Make tests

Checklist

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

@codecov
Copy link

codecov bot commented Jul 25, 2023

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (7c6868b) 91.99% compared to head (46e8878) 91.82%.

Files Patch % Lines
src/pynwb/file.py 28.57% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1737      +/-   ##
==========================================
- Coverage   91.99%   91.82%   -0.17%     
==========================================
  Files          27       27              
  Lines        2623     2630       +7     
  Branches      685      688       +3     
==========================================
+ Hits         2413     2415       +2     
- Misses        138      143       +5     
  Partials       72       72              
Flag Coverage Δ
integration 70.98% <28.57%> (-0.12%) ⬇️
unit 83.53% <28.57%> (-0.15%) ⬇️

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.

Comment on lines 375 to 376
read_nwbfile = self.read()
return read_nwbfile.find_all_of_class(pynwb_cls)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you clarify what use-case this function addresses that NWBFile.find_all_of_class does not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a comment in the docstring. It says: This method is useful for getting neurodata type objects from cached extensions where you do not have easy access to a python class to pass to NWBFile.find_all_of_class.

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense. But it seems a bit strange that this functionality is in two different places. Also, with this we would need to duplicate this also for Zarr. Instead, I'm wondering whether find_all_of_class can be modified to do both, something like:

@docval({'name': 'neurodata_types', 'type': (type, str), 'doc': 'The PyNWB container class to search for instances of or the string with the name of the neurodata_type'},)
def find_all(self, neurodata_type):
     if isinstance(neurodata_type, str):
         pynwb_cls = self.io.manager.type_map.get_dt_container_cls(neurodata_type, namespace)
     else:
        pynwb_cls = neurodata_type
     ret = [obj for obj in self.objects.values() if isinstance(obj, pynwb_class)]
     return ret

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that works too. I'll update it

src/pynwb/__init__.py Outdated Show resolved Hide resolved
@CodyCBakerPhD
Copy link
Collaborator

Idea LGTM, just needs a quick test or two

Copy link
Contributor

@bendichter bendichter left a comment

Choose a reason for hiding this comment

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

This is a nice idea. I have two suggestions:

  1. I'm not crazy about the name. I would prefer something a bit more explicit, like find_all_of_type
  2. I would rather this live in Container or maybe NWBContainer. That way it could be used on any container, not just NWBFile.

And yes, I of course agree with @CodyCBakerPhD that this will need tests

@oruebel
Copy link
Contributor

oruebel commented Jan 24, 2024

I'm wondering whether it would be simpler from a user perspective to have this part of the existing all_children method. I.e., we could add the query parameters for the types (with default value of None) to all_children. If the query parameters are set to None then all_children behaves as is and returns all the children and if the parameters are set then it would use the new logic to filter for types.

def all_children(self):

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.

Programmatically find all the time series in any NWB file
4 participants