Exemplo n.º 1
0
    def test_segment_with_proxy(self):
        seg = Segment()

        proxy_anasig = AnalogSignalProxy(
            rawio=self.reader,
            global_channel_indexes=None,
            block_index=0,
            seg_index=0,
        )
        seg.analogsignals.append(proxy_anasig)

        proxy_sptr = SpikeTrainProxy(rawio=self.reader,
                                     unit_index=0,
                                     block_index=0,
                                     seg_index=0)
        seg.spiketrains.append(proxy_sptr)

        proxy_event = EventProxy(rawio=self.reader,
                                 event_channel_index=0,
                                 block_index=0,
                                 seg_index=0)
        seg.events.append(proxy_event)

        proxy_epoch = EpochProxy(rawio=self.reader,
                                 event_channel_index=1,
                                 block_index=0,
                                 seg_index=0)
        seg.epochs.append(proxy_epoch)
Exemplo n.º 2
0
    def setUp(self):
        spike_time_array = np.array([0.5, 0.6, 0.7, 1.1, 11.2, 23.6, 88.5, 99.2])
        channel_id_array = np.array([0, 0, 1, 2, 1, 0, 2, 0])
        all_channel_ids = (0, 1, 2, 3)
        self.stl_from_array = SpikeTrainList.from_spike_time_array(
            spike_time_array,
            channel_id_array,
            all_channel_ids=all_channel_ids,
            units='ms',
            t_start=0 * pq.ms,
            t_stop=100.0 * pq.ms,
            identifier=["A", "B", "C", "D"]  # annotation
        )

        self.stl_from_obj_list = SpikeTrainList(items=(
            SpikeTrain([0.5, 0.6, 23.6, 99.2], units="ms",
                       t_start=0 * pq.ms, t_stop=100.0 * pq.ms, channel_id=101),
            SpikeTrain([0.0007, 0.0112], units="s", t_start=0 * pq.ms, t_stop=100.0 * pq.ms,
                       channel_id=102),
            SpikeTrain([1100, 88500], units="us", t_start=0 * pq.ms, t_stop=100.0 * pq.ms,
                       channel_id=103),
            SpikeTrain([], units="ms", t_start=0 * pq.ms, t_stop=100.0 * pq.ms,
                       channel_id=104),
        ))

        self.stl_from_obj_list_incl_proxy = SpikeTrainList(items=(
            SpikeTrain([0.5, 0.6, 23.6, 99.2], units="ms",
                       t_start=0 * pq.ms, t_stop=100.0 * pq.ms),
            SpikeTrain([0.0007, 0.0112], units="s", t_start=0 * pq.ms, t_stop=100.0 * pq.ms),
            SpikeTrainProxy(rawio=MockRawIO(), spike_channel_index=0),
            SpikeTrain([], units="ms", t_start=0 * pq.ms, t_stop=100.0 * pq.ms),
        ))
Exemplo n.º 3
0
    def test_SpikeTrainProxy(self):
        proxy_sptr = SpikeTrainProxy(rawio=self.reader, spike_channel_index=0,
                        block_index=0, seg_index=0)

        assert proxy_sptr.name == 'unit0'
        assert proxy_sptr.t_start == 0 * pq.s
        assert proxy_sptr.t_stop == 10 * pq.s
        assert proxy_sptr.shape == (20,)
        assert proxy_sptr.left_sweep == 0.002 * pq.s
        assert proxy_sptr.sampling_rate == 10 * pq.kHz

        # full load
        full_sptr = proxy_sptr.load(time_slice=None)
        assert isinstance(full_sptr, SpikeTrain)
        assert_same_attributes(proxy_sptr, full_sptr)
        assert full_sptr.shape == proxy_sptr.shape

        # slice time
        sptr = proxy_sptr.load(time_slice=(250 * pq.ms, 500 * pq.ms))
        assert sptr.t_start == .25 * pq.s
        assert sptr.t_stop == .5 * pq.s
        assert sptr.shape == (6,)
        assert_same_attributes(proxy_sptr.time_slice(250 * pq.ms, 500 * pq.ms), sptr)

        # buggy time slice
        with self.assertRaises(AssertionError):
            sptr = proxy_sptr.load(time_slice=(2. * pq.s, 15 * pq.s))
        sptr = proxy_sptr.load(time_slice=(2. * pq.s, 15 * pq.s), strict_slicing=False)
        assert sptr.t_stop == 10 * pq.s

        # magnitude mode rescaled
        sptr_float = proxy_sptr.load(magnitude_mode='rescaled')
        assert sptr_float.dtype == 'float64'
        assert sptr_float.units == pq.s

        # magnitude mode raw
        # TODO when raw mode implemented
        # sptr_int = proxy_sptr.load(magnitude_mode='raw')
        # assert sptr_int.dtype=='int64'
        # assert sptr_int.units==pq.CompoundUnit('1/10000*s')

        # assert_arrays_almost_equal(sptr_float, sptr_int.rescale('s'), 1e-9)

        # Without waveforms
        sptr = proxy_sptr.load(load_waveforms=False)
        assert sptr.waveforms is None

        # With waveforms
        sptr = proxy_sptr.load(load_waveforms=True, magnitude_mode='rescaled')
        assert sptr.waveforms is not None
        assert sptr.waveforms.shape == (20, 1, 50)
        assert sptr.waveforms.units == 1 * pq.uV

        # slice waveforms
        sptr = proxy_sptr.load(load_waveforms=True, time_slice=(250 * pq.ms, 500 * pq.ms))
        assert sptr.waveforms.shape == (6, 1, 50)

        # test array_annotations
        assert '__array_annotations__' not in proxy_sptr.annotations
        assert 'amplitudes' in proxy_sptr.array_annotations
Exemplo n.º 4
0
    def read_segment(self,
                     block_index=0,
                     seg_index=0,
                     lazy=False,
                     signal_group_mode=None,
                     load_waveforms=False,
                     time_slice=None,
                     strict_slicing=True):
        """
        :param block_index: int default 0. In case of several block block_index can be specified.

        :param seg_index: int default 0. Index of segment.

        :param lazy: False by default.

        :param signal_group_mode: 'split-all' or 'group-by-same-units' (default depend IO):
        This control behavior for grouping channels in AnalogSignal.
            * 'split-all': each channel will give an AnalogSignal
            * 'group-by-same-units' all channel sharing the same quantity units ar grouped in
            a 2D AnalogSignal

        :param load_waveforms: False by default. Control SpikeTrains.waveforms is None or not.

        :param time_slice: None by default means no limit.
            A time slice is (t_start, t_stop) both are quantities.
            All object AnalogSignal, SpikeTrain, Event, Epoch will load only in the slice.

        :param strict_slicing: True by default.
             Control if an error is raise or not when one of  time_slice member (t_start or t_stop)
             is outside the real time range of the segment.
        """

        if lazy:
            assert time_slice is None, 'For lazy=true you must specify time_slice when loading'

        if signal_group_mode is None:
            signal_group_mode = self._prefered_signal_group_mode

        # annotations
        seg_annotations = dict(
            self.raw_annotations['blocks'][block_index]['segments'][seg_index])
        for k in ('signals', 'units', 'events'):
            seg_annotations.pop(k)
        seg_annotations = check_annotations(seg_annotations)

        seg = Segment(index=seg_index, **seg_annotations)

        # AnalogSignal
        signal_channels = self.header['signal_channels']
        if signal_channels.size > 0:
            channel_indexes_list = self.get_group_channel_indexes()
            for channel_indexes in channel_indexes_list:
                for i, (ind_within,
                        ind_abs) in self._make_signal_channel_subgroups(
                            channel_indexes,
                            signal_group_mode=signal_group_mode).items():
                    # make a proxy...
                    anasig = AnalogSignalProxy(rawio=self,
                                               global_channel_indexes=ind_abs,
                                               block_index=block_index,
                                               seg_index=seg_index)

                    if not lazy:
                        # ... and get the real AnalogSIgnal if not lazy
                        anasig = anasig.load(time_slice=time_slice,
                                             strict_slicing=strict_slicing)
                        # TODO magnitude_mode='rescaled'/'raw'

                    anasig.segment = seg
                    seg.analogsignals.append(anasig)

        # SpikeTrain and waveforms (optional)
        unit_channels = self.header['unit_channels']
        for unit_index in range(len(unit_channels)):
            # make a proxy...
            sptr = SpikeTrainProxy(rawio=self,
                                   unit_index=unit_index,
                                   block_index=block_index,
                                   seg_index=seg_index)

            if not lazy:
                # ... and get the real SpikeTrain if not lazy
                sptr = sptr.load(time_slice=time_slice,
                                 strict_slicing=strict_slicing,
                                 load_waveforms=load_waveforms)
                # TODO magnitude_mode='rescaled'/'raw'

            sptr.segment = seg
            seg.spiketrains.append(sptr)

        # Events/Epoch
        event_channels = self.header['event_channels']
        for chan_ind in range(len(event_channels)):
            if event_channels['type'][chan_ind] == b'event':
                e = EventProxy(rawio=self,
                               event_channel_index=chan_ind,
                               block_index=block_index,
                               seg_index=seg_index)
                if not lazy:
                    e = e.load(time_slice=time_slice,
                               strict_slicing=strict_slicing)
                e.segment = seg
                seg.events.append(e)
            elif event_channels['type'][chan_ind] == b'epoch':
                e = EpochProxy(rawio=self,
                               event_channel_index=chan_ind,
                               block_index=block_index,
                               seg_index=seg_index)
                if not lazy:
                    e = e.load(time_slice=time_slice,
                               strict_slicing=strict_slicing)
                e.segment = seg
                seg.epochs.append(e)

        seg.create_many_to_one_relationship()
        return seg
Exemplo n.º 5
0
    def test__cut_block_by_epochs(self):
        seg = Segment()

        proxy_anasig = AnalogSignalProxy(rawio=self.reader,
                                         global_channel_indexes=None,
                                         block_index=0,
                                         seg_index=0)
        seg.analogsignals.append(proxy_anasig)

        proxy_st = SpikeTrainProxy(rawio=self.reader,
                                   unit_index=0,
                                   block_index=0,
                                   seg_index=0)
        seg.spiketrains.append(proxy_st)

        proxy_event = EventProxy(rawio=self.reader,
                                 event_channel_index=0,
                                 block_index=0,
                                 seg_index=0)
        seg.events.append(proxy_event)

        proxy_epoch = EpochProxy(rawio=self.reader,
                                 event_channel_index=1,
                                 block_index=0,
                                 seg_index=0)
        proxy_epoch.annotate(pick='me')
        seg.epochs.append(proxy_epoch)

        loaded_epoch = proxy_epoch.load()
        loaded_event = proxy_event.load()
        loaded_st = proxy_st.load()
        loaded_anasig = proxy_anasig.load()

        block = Block()
        block.segments = [seg]
        block.create_many_to_one_relationship()

        cut_block_by_epochs(block, properties={'pick': 'me'})

        assert_neo_object_is_compliant(block)
        self.assertEqual(len(block.segments), proxy_epoch.shape[0])

        for epoch_idx in range(len(loaded_epoch)):
            sliced_event = loaded_event.time_slice(
                t_start=loaded_epoch.times[epoch_idx],
                t_stop=loaded_epoch.times[epoch_idx] +
                loaded_epoch.durations[epoch_idx])
            has_event = len(sliced_event) > 0

            sliced_anasig = loaded_anasig.time_slice(
                t_start=loaded_epoch.times[epoch_idx],
                t_stop=loaded_epoch.times[epoch_idx] +
                loaded_epoch.durations[epoch_idx])

            sliced_st = loaded_st.time_slice(
                t_start=loaded_epoch.times[epoch_idx],
                t_stop=loaded_epoch.times[epoch_idx] +
                loaded_epoch.durations[epoch_idx])

            self.assertEqual(len(block.segments[epoch_idx].events),
                             int(has_event))
            self.assertEqual(len(block.segments[epoch_idx].spiketrains), 1)
            self.assertEqual(len(block.segments[epoch_idx].analogsignals), 1)

            self.assertTrue(
                isinstance(block.segments[epoch_idx].spiketrains[0],
                           SpikeTrain))
            assert_same_attributes(block.segments[epoch_idx].spiketrains[0],
                                   sliced_st)

            self.assertTrue(
                isinstance(block.segments[epoch_idx].analogsignals[0],
                           AnalogSignal))
            assert_same_attributes(block.segments[epoch_idx].analogsignals[0],
                                   sliced_anasig)

            if has_event:
                self.assertTrue(
                    isinstance(block.segments[epoch_idx].events[0], Event))
                assert_same_attributes(block.segments[epoch_idx].events[0],
                                       sliced_event)

        block2 = Block()
        seg2 = Segment()
        epoch = Epoch(np.arange(10) * pq.s, durations=np.ones((10)) * pq.s)
        epoch.annotate(pick='me instead')
        seg2.epochs = [proxy_epoch, epoch]
        block2.segments = [seg2]
        block2.create_many_to_one_relationship()

        # test correct loading and slicing of EpochProxy objects
        # (not tested above since we used the EpochProxy to cut the block)

        cut_block_by_epochs(block2, properties={'pick': 'me instead'})

        for epoch_idx in range(len(epoch)):
            sliced_epoch = loaded_epoch.time_slice(
                t_start=epoch.times[epoch_idx],
                t_stop=epoch.times[epoch_idx] + epoch.durations[epoch_idx])
            has_epoch = len(sliced_epoch) > 0

            if has_epoch:
                self.assertTrue(
                    isinstance(block2.segments[epoch_idx].epochs[0], Epoch))
                assert_same_attributes(block2.segments[epoch_idx].epochs[0],
                                       sliced_epoch)
Exemplo n.º 6
0
    def test__time_slice(self):
        time_slice = [.5, 5.6] * pq.s

        epoch2 = Epoch([0.6, 9.5, 16.8, 34.1] * pq.s,
                       durations=[4.5, 4.8, 5.0, 5.0] * pq.s,
                       t_start=.1 * pq.s)
        epoch2.annotate(epoch_type='b')
        epoch2.array_annotate(trial_id=[1, 2, 3, 4])

        event = Event(times=[0.5, 10.0, 25.2] * pq.s, t_start=.1 * pq.s)
        event.annotate(event_type='trial start')
        event.array_annotate(trial_id=[1, 2, 3])

        anasig = AnalogSignal(np.arange(50.0) * pq.mV,
                              t_start=.1 * pq.s,
                              sampling_rate=1.0 * pq.Hz)
        irrsig = IrregularlySampledSignal(signal=np.arange(50.0) * pq.mV,
                                          times=anasig.times,
                                          t_start=.1 * pq.s)
        st = SpikeTrain(
            np.arange(0.5, 50, 7) * pq.s,
            t_start=.1 * pq.s,
            t_stop=50.0 * pq.s,
            waveforms=np.array(
                [[[0., 1.], [0.1, 1.1]], [[2., 3.], [2.1, 3.1]],
                 [[4., 5.], [4.1, 5.1]], [[6., 7.], [6.1, 7.1]],
                 [[8., 9.], [8.1, 9.1]], [[12., 13.], [12.1, 13.1]],
                 [[14., 15.], [14.1, 15.1]], [[16., 17.], [16.1, 17.1]]]) *
            pq.mV,
            array_annotations={'spikenum': np.arange(1, 9)})

        seg = Segment()
        seg.epochs = [epoch2]
        seg.events = [event]
        seg.analogsignals = [anasig]
        seg.irregularlysampledsignals = [irrsig]
        seg.spiketrains = [st]

        block = Block()
        block.segments = [seg]
        block.create_many_to_one_relationship()

        # test without resetting the time
        sliced = seg.time_slice(time_slice[0], time_slice[1])

        assert_neo_object_is_compliant(sliced)

        self.assertEqual(len(sliced.events), 1)
        self.assertEqual(len(sliced.spiketrains), 1)
        self.assertEqual(len(sliced.analogsignals), 1)
        self.assertEqual(len(sliced.irregularlysampledsignals), 1)
        self.assertEqual(len(sliced.epochs), 1)

        assert_same_attributes(
            sliced.spiketrains[0],
            st.time_slice(t_start=time_slice[0], t_stop=time_slice[1]))
        assert_same_attributes(
            sliced.analogsignals[0],
            anasig.time_slice(t_start=time_slice[0], t_stop=time_slice[1]))
        assert_same_attributes(
            sliced.irregularlysampledsignals[0],
            irrsig.time_slice(t_start=time_slice[0], t_stop=time_slice[1]))
        assert_same_attributes(
            sliced.events[0],
            event.time_slice(t_start=time_slice[0], t_stop=time_slice[1]))
        assert_same_attributes(
            sliced.epochs[0],
            epoch2.time_slice(t_start=time_slice[0], t_stop=time_slice[1]))

        seg = Segment()
        seg.epochs = [epoch2]
        seg.events = [event]
        seg.analogsignals = [anasig]
        seg.irregularlysampledsignals = [irrsig]
        seg.spiketrains = [st]

        block = Block()
        block.segments = [seg]
        block.create_many_to_one_relationship()

        # test with resetting the time
        sliced = seg.time_slice(time_slice[0], time_slice[1], reset_time=True)

        assert_neo_object_is_compliant(sliced)

        self.assertEqual(len(sliced.events), 1)
        self.assertEqual(len(sliced.spiketrains), 1)
        self.assertEqual(len(sliced.analogsignals), 1)
        self.assertEqual(len(sliced.irregularlysampledsignals), 1)
        self.assertEqual(len(sliced.epochs), 1)

        assert_same_attributes(
            sliced.spiketrains[0],
            st.time_shift(-time_slice[0]).time_slice(t_start=0 * pq.s,
                                                     t_stop=time_slice[1] -
                                                     time_slice[0]))

        anasig_target = anasig.copy()
        anasig_target = anasig_target.time_shift(-time_slice[0]).time_slice(
            t_start=0 * pq.s, t_stop=time_slice[1] - time_slice[0])
        assert_same_attributes(sliced.analogsignals[0], anasig_target)
        irrsig_target = irrsig.copy()
        irrsig_target = irrsig_target.time_shift(-time_slice[0]).time_slice(
            t_start=0 * pq.s, t_stop=time_slice[1] - time_slice[0])
        assert_same_attributes(sliced.irregularlysampledsignals[0],
                               irrsig_target)
        assert_same_attributes(
            sliced.events[0],
            event.time_shift(-time_slice[0]).time_slice(t_start=0 * pq.s,
                                                        t_stop=time_slice[1] -
                                                        time_slice[0]))
        assert_same_attributes(
            sliced.epochs[0],
            epoch2.time_shift(-time_slice[0]).time_slice(t_start=0 * pq.s,
                                                         t_stop=time_slice[1] -
                                                         time_slice[0]))

        seg = Segment()

        reader = ExampleRawIO(filename='my_filename.fake')
        reader.parse_header()

        proxy_anasig = AnalogSignalProxy(rawio=reader,
                                         stream_index=0,
                                         inner_stream_channels=None,
                                         block_index=0,
                                         seg_index=0)
        seg.analogsignals.append(proxy_anasig)

        proxy_st = SpikeTrainProxy(rawio=reader,
                                   spike_channel_index=0,
                                   block_index=0,
                                   seg_index=0)
        seg.spiketrains.append(proxy_st)

        proxy_event = EventProxy(rawio=reader,
                                 event_channel_index=0,
                                 block_index=0,
                                 seg_index=0)
        seg.events.append(proxy_event)

        proxy_epoch = EpochProxy(rawio=reader,
                                 event_channel_index=1,
                                 block_index=0,
                                 seg_index=0)
        proxy_epoch.annotate(pick='me')
        seg.epochs.append(proxy_epoch)

        loaded_epoch = proxy_epoch.load()
        loaded_event = proxy_event.load()
        loaded_st = proxy_st.load()
        loaded_anasig = proxy_anasig.load()

        block = Block()
        block.segments = [seg]
        block.create_many_to_one_relationship()

        # test with proxy objects
        sliced = seg.time_slice(time_slice[0], time_slice[1])

        assert_neo_object_is_compliant(sliced)

        sliced_event = loaded_event.time_slice(t_start=time_slice[0],
                                               t_stop=time_slice[1])
        has_event = len(sliced_event) > 0

        sliced_anasig = loaded_anasig.time_slice(t_start=time_slice[0],
                                                 t_stop=time_slice[1])

        sliced_st = loaded_st.time_slice(t_start=time_slice[0],
                                         t_stop=time_slice[1])

        self.assertEqual(len(sliced.events), int(has_event))
        self.assertEqual(len(sliced.spiketrains), 1)
        self.assertEqual(len(sliced.analogsignals), 1)

        self.assertTrue(isinstance(sliced.spiketrains[0], SpikeTrain))
        assert_same_attributes(sliced.spiketrains[0], sliced_st)

        self.assertTrue(isinstance(sliced.analogsignals[0], AnalogSignal))
        assert_same_attributes(sliced.analogsignals[0], sliced_anasig)

        if has_event:
            self.assertTrue(isinstance(sliced.events[0], Event))
            assert_same_attributes(sliced.events[0], sliced_event)
Exemplo n.º 7
0
    def test_times(self):
        for seg in [self.seg1, self.seg2]:
            # calculate target values for t_start and t_stop
            t_starts, t_stops = [], []
            for children in [
                    seg.analogsignals, seg.epochs, seg.events,
                    seg.irregularlysampledsignals, seg.spiketrains
            ]:
                for child in children:
                    if hasattr(child, 't_start'):
                        t_starts.append(child.t_start)
                    if hasattr(child, 't_stop'):
                        t_stops.append(child.t_stop)
                    if hasattr(child, 'time'):
                        t_starts.append(child.time)
                        t_stops.append(child.time)
                    if hasattr(child, 'times'):
                        t_starts.append(child.times[0])
                        t_stops.append(child.times[-1])
            targ_t_start = min(t_starts)
            targ_t_stop = max(t_stops)

            self.assertEqual(seg.t_start, targ_t_start)
            self.assertEqual(seg.t_stop, targ_t_stop)

        # Testing times with ProxyObjects
        seg = Segment()
        reader = ExampleRawIO(filename='my_filename.fake')
        reader.parse_header()

        proxy_anasig = AnalogSignalProxy(rawio=reader,
                                         stream_index=0,
                                         inner_stream_channels=None,
                                         block_index=0,
                                         seg_index=0)
        seg.analogsignals.append(proxy_anasig)

        proxy_st = SpikeTrainProxy(rawio=reader,
                                   spike_channel_index=0,
                                   block_index=0,
                                   seg_index=0)
        seg.spiketrains.append(proxy_st)

        proxy_event = EventProxy(rawio=reader,
                                 event_channel_index=0,
                                 block_index=0,
                                 seg_index=0)
        seg.events.append(proxy_event)

        proxy_epoch = EpochProxy(rawio=reader,
                                 event_channel_index=1,
                                 block_index=0,
                                 seg_index=0)
        seg.epochs.append(proxy_epoch)

        t_starts, t_stops = [], []
        for children in [
                seg.analogsignals, seg.epochs, seg.events,
                seg.irregularlysampledsignals, seg.spiketrains
        ]:
            for child in children:
                if hasattr(child, 't_start'):
                    t_starts.append(child.t_start)
                if hasattr(child, 't_stop'):
                    t_stops.append(child.t_stop)
                if hasattr(child, 'time'):
                    t_starts.append(child.time)
                    t_stops.append(child.time)
                if hasattr(child, 'times'):
                    t_starts.append(child.times[0])
                    t_stops.append(child.times[-1])
        targ_t_start = min(t_starts)
        targ_t_stop = max(t_stops)

        self.assertEqual(seg.t_start, targ_t_start)
        self.assertEqual(seg.t_stop, targ_t_stop)