From ea7d84fe40df71d454eaf4117b4d726ebab7ca86 Mon Sep 17 00:00:00 2001 From: zm711 <92116279+zm711@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:35:41 -0400 Subject: [PATCH 1/2] test that all rawios accept slice(None) --- neo/test/rawiotest/rawio_compliance.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/neo/test/rawiotest/rawio_compliance.py b/neo/test/rawiotest/rawio_compliance.py index 784ffa46b..e913c5cf5 100644 --- a/neo/test/rawiotest/rawio_compliance.py +++ b/neo/test/rawiotest/rawio_compliance.py @@ -172,7 +172,7 @@ def read_analogsignals(reader): channel_names = signal_channels["name"][mask] channel_ids = signal_channels["id"][mask] - # acces by channel inde/ids/names should give the same chunk + # acces by channel index/ids/names should give the same chunk channel_indexes2 = channel_indexes[::2] channel_names2 = channel_names[::2] channel_ids2 = channel_ids[::2] @@ -214,6 +214,29 @@ def read_analogsignals(reader): ) np.testing.assert_array_equal(raw_chunk0, raw_chunk1) + # test slice(None). This should return the same array as giving + # all channel indexes or using None as an argument in `get_analogsignal_chunk` + # see https://github.com/NeuralEnsemble/python-neo/issues/1533 + + raw_chunk_slice_none = reader.get_analogsignal_chunk( + block_index=block_index, + seg_index=seg_index, + i_start=i_start, + i_stop=i_stop, + stream_index=stream_index, + channel_indexes=slice(None) + ) + raw_chunk_channel_indexes = reader.get_analogsignal_chunk( + block_index=block_index, + seg_index=seg_index, + i_start=i_start, + i_stop=i_stop, + stream_index=stream_index, + channel_indexes=channel_indexes + ) + + np.testing.assert_array_equal(raw_chunk_slice_none, raw_chunk_channel_indexes) + # test prefer_slice=True/False if nb_chan >= 3: for prefer_slice in (True, False): From e2c1cf8f09093197c21847ae5ee249d58329406b Mon Sep 17 00:00:00 2001 From: zm711 <92116279+zm711@users.noreply.github.com> Date: Wed, 28 Aug 2024 14:41:09 -0400 Subject: [PATCH 2/2] fix medrawio for slice --- neo/rawio/medrawio.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/neo/rawio/medrawio.py b/neo/rawio/medrawio.py index 373477b17..454981dd8 100644 --- a/neo/rawio/medrawio.py +++ b/neo/rawio/medrawio.py @@ -240,9 +240,18 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop, strea self.sess.set_channel_active(self._stream_info[stream_index]["raw_chans"]) num_channels = len(self._stream_info[stream_index]["raw_chans"]) self.sess.set_reference_channel(self._stream_info[stream_index]["raw_chans"][0]) + + # in the case we have a slice or we give an ArrayLike we need to iterate through the channels + # in order to activate them. else: - if any(channel_indexes < 0): - raise IndexError(f"Can not index negative channels: {channel_indexes}") + if isinstance(channel_indexes, slice): + start = channel_indexes.start or 0 + stop = channel_indexes.stop or len(self._stream_info[stream_index]["raw_chans"]) + step = channel_indexes.step or 1 + channel_indexes = [ch for ch in range(start, stop, step)] + else: + if any(channel_indexes < 0): + raise IndexError(f"Can not index negative channels: {channel_indexes}") # Set all channels to be inactive, then selectively set some of them to be active self.sess.set_channel_inactive("all") for i, channel_idx in enumerate(channel_indexes):