python-neo icon indicating copy to clipboard operation
python-neo copied to clipboard

Make BasefromrawIO context manager

Open JuliaSprenger opened this issue 5 years ago • 4 comments

Such that all rawio implementation can be used like this:

with BlackrockIO('filename') as io:
    io.read_block()

JuliaSprenger avatar Jul 24 '19 12:07 JuliaSprenger

I don't think we can do this for 0.8. This would imply a BaseRaw._close mandatory method.

And so in BaseFromraw it would be trivial:

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.close()

but but but

this ._close() must be added and tested for rawio to cleanly close all underlying memmap. It is too much work for now.

I propose to postpone to 0.9.0.

samuelgarcia avatar Jul 24 '19 20:07 samuelgarcia

What would you except from:

with SomeIO(filename') as reader:
    seg = reader.read_segment(lazy=True)

anasig = seg.analogsignals[0].load()

I think an error should be raise.

Most of RawIOs are memmap based so we would must test if the reader is closed or not at each get_analogsignal_chunk. I don't known if it would penalize the read speed.

samuelgarcia avatar Jul 24 '19 20:07 samuelgarcia

I +1'd the idea earlier, but these are good points @samuelgarcia.

jpgill86 avatar Jul 24 '19 20:07 jpgill86

Maybe this would help with the spikeinterface issue https://github.com/SpikeInterface/spikeinterface/issues/1234. Following on this.

h-mayorquin avatar Mar 06 '23 13:03 h-mayorquin