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

WIP: support .nwb.lindi.json files #947

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

magland
Copy link
Contributor

@magland magland commented Apr 25, 2024

Description

These are the changes needed to support .nwb.lindi.json files in addition to .nwb in spyglass.

  • The copied file name will be of the form filename_.nwb.lindi.json
  • In the LINDI case, all of the data is copied, and the raw is not omitted.

Optimistically, no other LINDI-changes to spyglass will be needed because LINDI provides a patch to h5py that allows it to automatically handle .lindi.json files based on their extension.

For more information about LINDI:
https://github.com/neurodataWithoutBorders/lindi

Checklist:

  • This PR should be accompanied by a release: unsure
  • If release, I have updated the CITATION.cff
  • This PR makes edits to table definitions: no
  • N/A If table edits, I have included an alter snippet for release notes.
  • I have updated the CHANGELOG.md with PR number and description.
  • I have added/edited docs/notebooks to reflect the changes: maybe not necessary at this stage?

@edeno edeno requested review from CBroz1 and edeno April 25, 2024 19:03
@magland
Copy link
Contributor Author

magland commented Apr 25, 2024

See this relevant discussion... monkey patching h5py may or may not be the right approach.

NeurodataWithoutBorders/lindi#58

@magland magland changed the title support .nwb.lindi.json files WIP: support .nwb.lindi.json files Apr 25, 2024
@magland
Copy link
Contributor Author

magland commented Apr 25, 2024

Based on NeurodataWithoutBorders/lindi#58
moving away from monkey patch solution. So this PR is WIP. Will follow up.

@edeno edeno marked this pull request as draft April 25, 2024 21:00
@magland
Copy link
Contributor Author

magland commented Apr 25, 2024

Updated the PR to use a new class H5pyFile in all the of the pynwb.NWBHDF5IO calls. This is equal to lindi.File in the case that lindi can be imported and is equal to h5py.File otherwise. lindi.File is the same as h5py.File except when the file path ends with .lindi.json.

Copy link
Member

@CBroz1 CBroz1 left a comment

Choose a reason for hiding this comment

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

Hi @magland - Thanks for working on this. Had a couple comments while you're in progress

# end with ".lindi.json", then H5pyFile is equivalent to h5py.File. Thus, the
# difference only matters when opening LINDI files.

USE_LINDI = os.environ.get("SPYGLASS_LINDI", "0") == "1"
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to move us away from env var in favor of putting everything in src/spyglass/setting.py, and grabbing from the custom field in dj_config

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. Could you give me some hints about the best way to do this?

Copy link
Member

Choose a reason for hiding this comment

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

I think I would add it as an item like test_mode at L145 and then again at 215. To fetch it from settings as a bare variable, it would also need a line around 516. For demo, we should also mirror the name in the example config.

if USE_LINDI:
from lindi import File as H5pyFile # noqa: F401
else:
from h5py import File as H5pyFile # noqa: F401
Copy link
Member

Choose a reason for hiding this comment

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

It seems like there are quite a few places where you're using multiple contexts

with H5pyFile(analysis_nwb_file_abs_path, "a") as h5f:
        with pynwb.NWBHDF5IO(
            file=h5f,
            mode="a",
            load_namespaces=True,
        ) as io:

Is it possible to define a context manager here that would clean up those instances by combining them into something like ...?

with H5pyNwb(
    analysis_nwb_file_abs_path,
    mode="a",
    load_namespaces=True,
) as h5f:

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 I that would be possible, but I thought it's better to be explicit.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, I think I was part motivated by the readability of the diff, and part motivated by a desire to see spyglass have less nesting than it does. If it's sometimes just one or the other, we can keep them separate. If it's always both, I'd be in favor of combining

@@ -102,8 +102,8 @@ def plot_all_dio_events(self, return_fig=False):
va="bottom",
)
ax.step(
np.asarray(event["dio"].timestamps),
np.asarray(event["dio"].data),
event["dio"].timestamps[:],
Copy link
Member

Choose a reason for hiding this comment

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

What motivated this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LINDI provides a h5py-like object which serves as a drop-in replacement, but does not support all of the low-level functionalities. It seems that np.asarray(h5py_dataset) uses some of these low-level functions. The change I made is to be compatible with lindi.

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.

None yet

2 participants