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

nwb interface: allow full paths in __getitem__ (2) #364

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
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
43 changes: 42 additions & 1 deletion pynapple/io/interface_nwb.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,27 @@
If key is not in the dictionary
"""
if key.__hash__:
if "/" in key:
# allow user to specify the full path to the object
# store the original key so we can verify it later
original_key = key

Check warning on line 464 in pynapple/io/interface_nwb.py

View check run for this annotation

Codecov / codecov/patch

pynapple/io/interface_nwb.py#L464

Added line #L464 was not covered by tests
if original_key.startswith("/"):
# remove leading slash
original_key = original_key[1:]

Check warning on line 467 in pynapple/io/interface_nwb.py

View check run for this annotation

Codecov / codecov/patch

pynapple/io/interface_nwb.py#L467

Added line #L467 was not covered by tests
# use just the last part of the key
key = key.split("/")[-1]

Check warning on line 469 in pynapple/io/interface_nwb.py

View check run for this annotation

Codecov / codecov/patch

pynapple/io/interface_nwb.py#L469

Added line #L469 was not covered by tests

else:
original_key = None
if self.__contains__(key):
if isinstance(self.data[key], dict) and "id" in self.data[key]:
obj = self.nwb.objects[self.data[key]["id"]]
obj_id = self.data[key]["id"]
obj = self.nwb.objects[obj_id]
if original_key is not None:
if _path_for_nwb_object(obj) != original_key:
raise KeyError(

Check warning on line 479 in pynapple/io/interface_nwb.py

View check run for this annotation

Codecov / codecov/patch

pynapple/io/interface_nwb.py#L479

Added line #L479 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to write a little tests for this? I am not sure I understand the case where this would happen.

f"Mismatch between key and object path: {original_key} != {_path_for_nwb_object(obj)}"
)
try:
data = self._f_eval[self.data[key]["type"]](
obj, lazy_loading=self._lazy_loading
Expand All @@ -484,3 +502,26 @@
def close(self):
"""Close the NWB file"""
self.io.close()


def _path_for_nwb_object(obj):
"""Helper function to get the path of an NWB object"""
if obj.parent:
if obj.parent.name == "root":
# unfortunately, there's now way to get the parent name when the
# parent is a top-level NWB object because the parent actually
# points to root, so we need to do it this way
modules = {

Check warning on line 514 in pynapple/io/interface_nwb.py

View check run for this annotation

Codecov / codecov/patch

pynapple/io/interface_nwb.py#L514

Added line #L514 was not covered by tests
"acquisition": obj.parent.acquisition,
"analysis": obj.parent.analysis,
"stimulus": obj.parent.stimulus,
"processing": obj.parent.processing,
}
for k, v in modules.items():
if obj.name in v.keys() and v[obj.name] == obj:
return f"{k}/{obj.name}"
return obj.name

Check warning on line 523 in pynapple/io/interface_nwb.py

View check run for this annotation

Codecov / codecov/patch

pynapple/io/interface_nwb.py#L522-L523

Added lines #L522 - L523 were not covered by tests
else:
return f"{_path_for_nwb_object(obj.parent)}/{obj.name}"

Check warning on line 525 in pynapple/io/interface_nwb.py

View check run for this annotation

Codecov / codecov/patch

pynapple/io/interface_nwb.py#L525

Added line #L525 was not covered by tests
else:
return obj.name

Check warning on line 527 in pynapple/io/interface_nwb.py

View check run for this annotation

Codecov / codecov/patch

pynapple/io/interface_nwb.py#L527

Added line #L527 was not covered by tests
Loading