Replies: 6 comments 4 replies
-
This refactoring could be messy, but I am imagining this would take the form of a helper function I agree that monkey-patching is the most straightforward approach and eases adoption, but I worry that it may be confusing to downstream users who see Wikipedia also lists some other pitfalls with monkey-patching regarding name clashes and future-proofing as the patched library changes behavior. I think those are less of an issue here, but worth considering. |
Beta Was this translation helpful? Give feedback.
-
I believe that NWBHDF5IO now accepts exactly one of There are also other places in spyglass where h5py is used without pynwb. |
Beta Was this translation helpful? Give feedback.
-
@rly yeah there are some definite pitfalls. I'll think about this some more. |
Beta Was this translation helpful? Give feedback.
-
Part of the concern is, that you want LINDI to be seen as a "good-citizen" in the broader ecosystem, i.e., we should avoid modifying the behavior of other libraries without making it explicit that those changes are happening. Creating adapter/wrapper classes is one way to make this explicit because a user clearly sees that they are using a different type. If that is possible, then I think it is a good choice. In general, monkey-patching is often a convenience, but I try to avoid it if possible, because it is in-transparent. If we really decide that monkey-patching is the way to go, then I think the question is whether we can make it explicit to the user that this is happening. E.g., would it possible to require the user to explicitly initiate the monkey-patch, e.g., by requiring the user to import a particular file, e.g, |
Beta Was this translation helpful? Give feedback.
-
Okay makes sense. I'll move away from the monkey patch idea, and provide a more involved but more explicit PR to spyglass. |
Beta Was this translation helpful? Give feedback.
-
See the proposed spyglass changes here |
Beta Was this translation helpful? Give feedback.
-
Hi @rly !
I made some PRs, but they are not ready for review.
I'm trying to get spyglass to work with .lindi.json files, but I am realizing that it's going to be pretty ugly to modify the code to use either h5py or lindi depending on the file type. For example, right now the path (string) is passed in to pynwb.NWBHDF5IO, so that would need to be refactored in a messy way throughout.
As a possible solution, I'm working on monkey-patching h5py so that it will automatically use LindiH5pyFile in the case where the file has extension .lindi.json. This would simplify things a lot as spyglass could essentially be left as is. More generally, this would make lindi much easier to use with existing code bases that use h5py.
But what this means is that lindi must support the write modes (r+, w, w-, x, a) in a way that actually modifies the underlying .json file. I was hesitant about this since modifying a large-ish json every time there is a write operation is not ideal (as a side note, I remember you made a comment in on of the PRs that the existing way of just modifying the in-memory rfs is not intuitive). But h5py.File has a flush() function that can be called at any time during writing and is also called on close. I think a good solution could be to write the .json file on every flush. Consolidation of chunks can also happen at this time as well.
The other consideration is that the h5py interface has no mechanism for setting a staging area for chunks. So I'm trying out a system where the staging area is by default [filename].lindi.json.d , a directory that is adjacent to the json file.
So it would work like this
And the resulting file would be test.lindi.json
Beta Was this translation helpful? Give feedback.
All reactions