コード例 #1
0
    def test__time_slice__different_units(self):
        self.signal2.t_start = 10.0 * pq.ms
        assert_neo_object_is_compliant(self.signal2)

        t_start = 2 * pq.s + 10.0 * pq.ms
        t_stop = 4 * pq.s + 10.0 * pq.ms

        result = self.signal2.time_slice(t_start, t_stop)
        self.assertIsInstance(result, AnalogSignal)
        assert_neo_object_is_compliant(result)
        self.assertEqual(result.name, 'spam')
        self.assertEqual(result.description, 'eggs')
        self.assertEqual(result.file_origin, 'testfile.txt')
        self.assertEqual(result.annotations, {'arg1': 'test'})

        targ = AnalogSignal(np.array([[2., 3.], [2., 3.]]).T,
                            t_start=t_start.rescale(pq.ms),
                            sampling_rate=1.0 * pq.Hz,
                            units='mV',
                            name='spam',
                            description='eggs',
                            file_origin='testfile.txt',
                            arg1='test')
        assert_neo_object_is_compliant(result)

        assert_neo_object_is_compliant(self.signal2)
        self.assertEqual(self.signal2.t_start, 10.0 * pq.ms)
        self.assertAlmostEqual(result.t_stop, t_stop, delta=1e-12 * pq.ms)
        self.assertAlmostEqual(result.t_start, t_start, delta=1e-12 * pq.ms)
        assert_arrays_almost_equal(result.times, targ.times, 1e-12 * pq.ms)
        self.assertEqual(result.sampling_rate, targ.sampling_rate)
        assert_arrays_equal(result, targ)
        assert_same_sub_schema(result, targ)
コード例 #2
0
    def test__time_slice__different_units(self):
        self.signal2.t_start = 10.0 * pq.ms
        assert_neo_object_is_compliant(self.signal2)

        t_start = 2 * pq.s + 10.0 * pq.ms
        t_stop = 4 * pq.s + 10.0 * pq.ms

        result = self.signal2.time_slice(t_start, t_stop)
        self.assertIsInstance(result, AnalogSignalArray)
        assert_neo_object_is_compliant(result)
        self.assertEqual(result.name, 'spam')
        self.assertEqual(result.description, 'eggs')
        self.assertEqual(result.file_origin, 'testfile.txt')
        self.assertEqual(result.annotations, {'arg1': 'test'})

        targ = AnalogSignalArray(np.array([[2, 3], [2, 3]]).T,
                                 t_start=t_start.rescale(pq.ms),
                                 sampling_rate=1.0*pq.Hz, units='mV',
                                 name='spam', description='eggs',
                                 file_origin='testfile.txt', arg1='test')
        assert_neo_object_is_compliant(result)

        assert_neo_object_is_compliant(self.signal2)
        self.assertEqual(self.signal2.t_start, 10.0 * pq.ms)
        self.assertAlmostEqual(result.t_stop, t_stop, delta=1e-12*pq.ms)
        self.assertAlmostEqual(result.t_start, t_start, delta=1e-12*pq.ms)
        assert_arrays_almost_equal(result.times, targ.times, 1e-12*pq.ms)
        self.assertEqual(result.sampling_rate, targ.sampling_rate)
        assert_arrays_equal(result, targ)
        assert_same_sub_schema(result, targ)
コード例 #3
0
ファイル: test_event.py プロジェクト: csmarc/python-neo
 def test_duplicate_with_new_data(self):
     signal1 = self.event
     new_data = np.sort(np.random.uniform(0, 100,
                                          (self.event.size))) * pq.ms
     signal1b = signal1.duplicate_with_new_data(new_data)
     assert_arrays_almost_equal(np.asarray(signal1b), np.asarray(new_data),
                                1e-12)
コード例 #4
0
ファイル: test_misc.py プロジェクト: sanjayankur31/python-neo
    def test__add_epoch(self):
        proxy_event = EventProxy(rawio=self.reader,
                                 event_channel_index=0,
                                 block_index=0,
                                 seg_index=0)

        loaded_event = proxy_event.load()

        regular_event = Event(times=loaded_event.times -
                              1 * loaded_event.units)

        loaded_event.annotate(nix_name='neo.event.0')
        regular_event.annotate(nix_name='neo.event.1')

        seg = Segment()
        seg.events = [regular_event, proxy_event]

        # test cutting with two events one of which is a proxy
        epoch = add_epoch(seg, regular_event, proxy_event)

        assert_neo_object_is_compliant(epoch)
        exp_annos = {
            k: v
            for k, v in regular_event.annotations.items() if k != 'nix_name'
        }
        self.assertDictEqual(epoch.annotations, exp_annos)
        assert_arrays_almost_equal(epoch.times, regular_event.times, 1e-12)
        assert_arrays_almost_equal(
            epoch.durations,
            np.ones(regular_event.shape) * loaded_event.units, 1e-12)
コード例 #5
0
    def test_AnalogSignalProxy(self):
        proxy_anasig = AnalogSignalProxy(rawio=self.reader, global_channel_indexes=None,
                        block_index=0, seg_index=0,)

        assert proxy_anasig.sampling_rate == 10 * pq.kHz
        assert proxy_anasig.t_start == 0 * pq.s
        assert proxy_anasig.t_stop == 10 * pq.s
        assert proxy_anasig.duration == 10 * pq.s
        assert proxy_anasig.file_origin == 'my_filename.fake'

        # full load
        full_anasig = proxy_anasig.load(time_slice=None)
        assert isinstance(full_anasig, AnalogSignal)
        assert_same_attributes(proxy_anasig, full_anasig)

        # slice time
        anasig = proxy_anasig.load(time_slice=(2. * pq.s, 5 * pq.s))
        assert anasig.t_start == 2. * pq.s
        assert anasig.duration == 3. * pq.s
        assert anasig.shape == (30000, 16)
        assert_same_attributes(proxy_anasig.time_slice(2. * pq.s, 5 * pq.s), anasig)

        # ceil next sample when slicing
        anasig = proxy_anasig.load(time_slice=(1.99999 * pq.s, 5.000001 * pq.s))
        assert anasig.t_start == 2. * pq.s
        assert anasig.duration == 3. * pq.s
        assert anasig.shape == (30000, 16)

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

        # select channels
        anasig = proxy_anasig.load(channel_indexes=[3, 4, 9])
        assert anasig.shape[1] == 3

        # select channels and slice times
        anasig = proxy_anasig.load(time_slice=(2. * pq.s, 5 * pq.s), channel_indexes=[3, 4, 9])
        assert anasig.shape == (30000, 3)

        # magnitude mode rescaled
        anasig_float = proxy_anasig.load(magnitude_mode='rescaled')
        assert anasig_float.dtype == 'float32'
        assert anasig_float.units == pq.uV
        assert anasig_float.units == proxy_anasig.units

        # magnitude mode raw
        anasig_int = proxy_anasig.load(magnitude_mode='raw')
        assert anasig_int.dtype == 'int16'
        assert anasig_int.units == pq.CompoundUnit('0.0152587890625*uV')

        assert_arrays_almost_equal(anasig_float, anasig_int.rescale('uV'), 1e-9)

        # test array_annotations
        assert 'info' in proxy_anasig.array_annotations
        assert proxy_anasig.array_annotations['info'].size == 16
        assert 'info' in anasig_float.array_annotations
        assert anasig_float.array_annotations['info'].size == 16
コード例 #6
0
 def test__duplicate_with_new_array(self):
     signal1 = self.signals[1]
     signal2 = self.signals[2]
     data2 = self.data[2]
     signal1b = signal1.duplicate_with_new_array(data2)
     assert_arrays_almost_equal(np.asarray(signal1b), np.asarray(signal2 / 1000.0), 1e-12)
     self.assertEqual(signal1b.t_start, signal1.t_start)
     self.assertEqual(signal1b.sampling_rate, signal1.sampling_rate)
コード例 #7
0
ファイル: test_epoch.py プロジェクト: CINPLA/python-neo
 def test_duplicate_with_new_data(self):
     signal1 = self.epoch
     new_data = np.sort(np.random.uniform(0, 100, (self.epoch))) * pq.ms
     signal1b = signal1.duplicate_with_new_data(new_data)
     assert_arrays_almost_equal(np.asarray(signal1b),
                                np.asarray(new_data), 1e-12)
     assert_arrays_almost_equal(np.asarray(signal1b.durations),
                                np.asarray(signal1.durations), 1e-12)
コード例 #8
0
 def test__duplicate_with_new_array(self):
     signal1 = self.signals[1]
     signal2 = self.signals[2]
     data2 = self.data[2]
     signal1b = signal1.duplicate_with_new_array(data2)
     assert_arrays_almost_equal(np.asarray(signal1b),
                                np.asarray(signal2 / 1000.), 1e-12)
     self.assertEqual(signal1b.t_start, signal1.t_start)
     self.assertEqual(signal1b.sampling_rate, signal1.sampling_rate)
コード例 #9
0
ファイル: test_event.py プロジェクト: rishidhingra/python-neo
 def test_duplicate_with_new_data(self):
     signal1 = self.event
     new_times = np.sort(np.random.uniform(0, 100, (self.event.size))) * pq.ms
     new_labels = np.array(list("zyxwvutsrqponmlkjihgfedcba"[:self.event.size]))
     signal1b = signal1.duplicate_with_new_data(new_times, new_labels)
     assert_arrays_almost_equal(np.asarray(signal1b), np.asarray(new_times), 1e-12)
     assert_arrays_equal(signal1b.labels, new_labels)
     # After duplicating, array annotations should always be empty,
     # because different length of data would cause inconsistencies
     self.assertTrue('index' not in signal1b.array_annotations)
     self.assertTrue('test' not in signal1b.array_annotations)
     self.assertIsInstance(signal1b.array_annotations, ArrayDict)
コード例 #10
0
ファイル: test_proxyobjects.py プロジェクト: INM-6/python-neo
    def test_AnalogSignalProxy(self):
        proxy_anasig = AnalogSignalProxy(rawio=self.reader, global_channel_indexes=None,
                        block_index=0, seg_index=0,)

        assert proxy_anasig.sampling_rate == 10 * pq.kHz
        assert proxy_anasig.t_start == 0 * pq.s
        assert proxy_anasig.t_stop == 10 * pq.s
        assert proxy_anasig.duration == 10 * pq.s
        assert proxy_anasig.file_origin == 'my_filename.fake'

        # full load
        full_anasig = proxy_anasig.load(time_slice=None)
        assert isinstance(full_anasig, AnalogSignal)
        assert_same_attributes(proxy_anasig, full_anasig)

        # slice time
        anasig = proxy_anasig.load(time_slice=(2. * pq.s, 5 * pq.s))
        assert anasig.t_start == 2. * pq.s
        assert anasig.duration == 3. * pq.s
        assert anasig.shape == (30000, 16)

        # ceil next sample when slicing
        anasig = proxy_anasig.load(time_slice=(1.99999 * pq.s, 5.000001 * pq.s))
        assert anasig.t_start == 2. * pq.s
        assert anasig.duration == 3. * pq.s
        assert anasig.shape == (30000, 16)

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

        # select channels
        anasig = proxy_anasig.load(channel_indexes=[3, 4, 9])
        assert anasig.shape[1] == 3

        # select channels and slice times
        anasig = proxy_anasig.load(time_slice=(2. * pq.s, 5 * pq.s), channel_indexes=[3, 4, 9])
        assert anasig.shape == (30000, 3)

        # magnitude mode rescaled
        anasig_float = proxy_anasig.load(magnitude_mode='rescaled')
        assert anasig_float.dtype == 'float32'
        assert anasig_float.units == pq.uV
        assert anasig_float.units == proxy_anasig.units

        # magnitude mode raw
        anasig_int = proxy_anasig.load(magnitude_mode='raw')
        assert anasig_int.dtype == 'int16'
        assert anasig_int.units == pq.CompoundUnit('0.0152587890625*uV')

        assert_arrays_almost_equal(anasig_float, anasig_int.rescale('uV'), 1e-9)
コード例 #11
0
    def test__rescale_new(self):
        result = self.signal1.copy()
        result = result.rescale(pq.pA)

        self.assertIsInstance(result, AnalogSignal)
        assert_neo_object_is_compliant(result)
        self.assertEqual(result.name, 'spam')
        self.assertEqual(result.description, 'eggs')
        self.assertEqual(result.file_origin, 'testfile.txt')
        self.assertEqual(result.annotations, {'arg1': 'test'})

        self.assertEqual(result.units, 1*pq.pA)
        assert_arrays_almost_equal(np.array(result), self.data1*1000., 1e-10)
コード例 #12
0
    def test__rescale_new(self):
        result = self.signal1.copy()
        result = result.rescale(pq.pA)

        self.assertIsInstance(result, AnalogSignal)
        assert_neo_object_is_compliant(result)
        self.assertEqual(result.name, 'spam')
        self.assertEqual(result.description, 'eggs')
        self.assertEqual(result.file_origin, 'testfile.txt')
        self.assertEqual(result.annotations, {'arg1': 'test'})

        self.assertEqual(result.units, 1 * pq.pA)
        assert_arrays_almost_equal(np.array(result), self.data1 * 1000., 1e-10)
コード例 #13
0
ファイル: test_analogsignal.py プロジェクト: INM-6/python-neo
 def test__duplicate_with_new_data(self):
     signal1 = self.signals[1]
     signal2 = self.signals[2]
     data2 = self.data[2]
     signal1.array_annotate(ann=np.arange(signal1.shape[-1]))
     signal1b = signal1.duplicate_with_new_data(data2)
     assert_arrays_almost_equal(np.asarray(signal1b), np.asarray(signal2 / 1000.), 1e-12)
     self.assertEqual(signal1b.t_start, signal1.t_start)
     self.assertEqual(signal1b.sampling_rate, signal1.sampling_rate)
     # After duplicating, array annotations should always be empty,
     # because different length of data would cause inconsistencies
     self.assertEqual(signal1b.array_annotations, {})
     self.assertIsInstance(signal1b.array_annotations, ArrayDict)
コード例 #14
0
ファイル: test_event.py プロジェクト: INM-6/python-neo
 def test_duplicate_with_new_data(self):
     signal1 = self.event
     new_data = np.sort(np.random.uniform(0, 100, (self.event.size))) * pq.ms
     signal1b = signal1.duplicate_with_new_data(new_data)
     assert_arrays_almost_equal(np.asarray(signal1b), np.asarray(new_data), 1e-12)
     # Note: Labels and Durations are NOT copied any more!!!
     # After duplicating, array annotations should always be empty,
     # because different length of data would cause inconsistencies
     # Only labels and durations should be available
     assert_arrays_equal(signal1b.labels, np.ndarray((0,), dtype='S'))
     self.assertTrue('index' not in signal1b.array_annotations)
     self.assertTrue('test' not in signal1b.array_annotations)
     self.assertIsInstance(signal1b.array_annotations, ArrayDict)
コード例 #15
0
 def test__duplicate_with_new_data(self):
     signal1 = self.signals[1]
     signal2 = self.signals[2]
     data2 = self.data[2]
     signal1.array_annotate(ann=np.arange(signal1.shape[-1]))
     signal1b = signal1.duplicate_with_new_data(data2)
     assert_arrays_almost_equal(np.asarray(signal1b), np.asarray(signal2 / 1000.), 1e-12)
     self.assertEqual(signal1b.t_start, signal1.t_start)
     self.assertEqual(signal1b.sampling_rate, signal1.sampling_rate)
     # After duplicating, array annotations should always be empty,
     # because different length of data would cause inconsistencies
     self.assertEqual(signal1b.array_annotations, {})
     self.assertIsInstance(signal1b.array_annotations, ArrayDict)
コード例 #16
0
    def test2(self):
        """Read data into two segments instead of just one"""
        full_range = 8192 * pq.mV
        bio = neo.io.BlackrockIO(filename=self.fn, full_range=full_range)
        block = bio.read_block(n_starts=[0, 3], n_stops=[2, 6])
        self.assertEqual(bio.header.Channel_Count, 2)
        self.assertEqual(bio.header.n_samples, 6)

        # Everything in two segments
        self.assertEqual(len(block.segments), 2)

        # Test first seg
        seg = block.segments[0]
        self.assertEqual(len(seg.analogsignals), 2)
        assert_arrays_almost_equal(seg.analogsignals[0],
                                   [3., 4.] * pq.mV, .0001)
        assert_arrays_almost_equal(seg.analogsignals[1],
                                   [6., -4.] * pq.mV, .0001)

        # Test second seg
        seg = block.segments[1]
        self.assertEqual(len(seg.analogsignals), 2)
        assert_arrays_almost_equal(seg.analogsignals[0],
                                   [-3., -4., -5.] * pq.mV, .0001)
        assert_arrays_almost_equal(seg.analogsignals[1],
                                   [-6., 4., 5.] * pq.mV, .0001)
コード例 #17
0
    def test_time_slice_different_units(self):
        #import numpy
        av = AnalogSignalArray(numpy.array([[0,1,2,3,4,5],[0,1,2,3,4,5]]).T, t_start = 10.0 * pq.ms,sampling_rate=1.0*pq.Hz, units='mV')

        t_start = 2 * pq.s + 10.0 * pq.ms
        t_stop = 4 * pq.s + 10.0 * pq.ms
        av2 = av.time_slice(t_start,t_stop)
        correct = AnalogSignalArray(numpy.array([[2,3],[2,3]]).T, t_start = t_start,sampling_rate=1.0*pq.Hz, units='mV')

        self.assertIsInstance(av2, AnalogSignalArray)
        self.assertAlmostEqual(av2.t_stop,t_stop,delta=1e-12*ms)
        self.assertAlmostEqual(av2.t_start,t_start,delta=1e-12*ms)
        assert_arrays_almost_equal(av2.times,correct.times,1e-12*ms)
        self.assertEqual(av2.sampling_rate, correct.sampling_rate)
コード例 #18
0
    def test__rescale_new(self):
        result = self.signal1.copy()
        result = result.rescale(pq.uV)

        self.assertIsInstance(result, IrregularlySampledSignal)
        assert_neo_object_is_compliant(result)
        self.assertEqual(result.name, 'spam')
        self.assertEqual(result.description, 'eggs')
        self.assertEqual(result.file_origin, 'testfile.txt')
        self.assertEqual(result.annotations, {'arg1': 'test'})

        self.assertEqual(result.units, 1*pq.uV)
        assert_arrays_almost_equal(np.array(result), self.data1.reshape(-1, 1)*1000., 1e-10)
        assert_array_equal(result.times, self.time1quant)
コード例 #19
0
ファイル: test_utils.py プロジェクト: wvangeit/python-neo
    def test__add_epoch(self):
        starts = Event(times=[0.5, 10.0, 25.2] * pq.s)
        starts.annotate(event_type='trial start')
        starts.array_annotate(trial_id=[1, 2, 3])

        stops = Event(times=[5.5, 14.9, 30.1] * pq.s)
        stops.annotate(event_type='trial stop')
        stops.array_annotate(trial_id=[1, 2, 3])

        seg = Segment()
        seg.events = [starts, stops]

        # test cutting with one event only
        ep_starts = add_epoch(seg, starts, pre=-300 * pq.ms, post=250 * pq.ms)

        assert_neo_object_is_compliant(ep_starts)
        assert_same_annotations(ep_starts, starts)
        assert_arrays_almost_equal(ep_starts.times, starts.times - 300 * pq.ms,
                                   1e-12)
        assert_arrays_almost_equal(
            ep_starts.durations,
            (550 * pq.ms).rescale(ep_starts.durations.units) * np.ones(
                (len(starts))), 1e-12)

        # test cutting with two events
        ep_trials = add_epoch(seg, starts, stops)

        assert_neo_object_is_compliant(ep_trials)
        assert_same_annotations(ep_trials, starts)
        assert_arrays_almost_equal(ep_trials.times, starts.times, 1e-12)
        assert_arrays_almost_equal(ep_trials.durations, stops - starts, 1e-12)
コード例 #20
0
    def test2(self):
        """Read data into two segments instead of just one"""
        full_range = 8192 * pq.mV
        bio = neo.io.BlackrockIO(filename=self.fn, full_range=full_range)
        block = bio.read_block(n_starts=[0, 3], n_stops=[2, 6])
        self.assertEqual(bio.header.Channel_Count, 2)
        self.assertEqual(bio.header.n_samples, 6)

        # Everything in two segments
        self.assertEqual(len(block.segments), 2)

        # Test first seg
        seg = block.segments[0]
        self.assertEqual(len(seg.analogsignals), 2)
        assert_arrays_almost_equal(seg.analogsignals[0], [3., 4.] * pq.mV,
                                   .0001)
        assert_arrays_almost_equal(seg.analogsignals[1], [6., -4.] * pq.mV,
                                   .0001)

        # Test second seg
        seg = block.segments[1]
        self.assertEqual(len(seg.analogsignals), 2)
        assert_arrays_almost_equal(seg.analogsignals[0],
                                   [-3., -4., -5.] * pq.mV, .0001)
        assert_arrays_almost_equal(seg.analogsignals[1], [-6., 4., 5.] * pq.mV,
                                   .0001)
コード例 #21
0
    def test__time_slice__no_explicit_time(self):
        self.signal2.t_start = 10.0 * pq.ms
        assert_neo_object_is_compliant(self.signal2)

        t1 = 2 * pq.s + 10.0 * pq.ms
        t2 = 4 * pq.s + 10.0 * pq.ms

        for t_start, t_stop in [(t1, None), (None, None), (None, t2)]:
            t_start_targ = t1 if t_start is not None else self.signal2.t_start
            t_stop_targ = t2 if t_stop is not None else self.signal2.t_stop

            result = self.signal2.time_slice(t_start, t_stop)
            self.assertIsInstance(result, AnalogSignal)
            assert_neo_object_is_compliant(result)
            self.assertEqual(result.name, 'spam')
            self.assertEqual(result.description, 'eggs')
            self.assertEqual(result.file_origin, 'testfile.txt')
            self.assertEqual(result.annotations, {'arg1': 'test'})
            assert_arrays_equal(result.array_annotations['anno1'],
                                np.array([10, 11]))
            assert_arrays_equal(result.array_annotations['anno2'],
                                np.array(['k', 'l']))
            self.assertIsInstance(result.array_annotations, ArrayDict)

            targ_ind = np.where((self.signal2.times >= t_start_targ)
                                & (self.signal2.times < t_stop_targ))
            targ_array = self.signal2.magnitude[targ_ind]

            targ = AnalogSignal(targ_array,
                                t_start=t_start_targ.rescale(pq.ms),
                                sampling_rate=1.0 * pq.Hz,
                                units='mV',
                                name='spam',
                                description='eggs',
                                file_origin='testfile.txt',
                                arg1='test')
            assert_neo_object_is_compliant(result)

            assert_neo_object_is_compliant(self.signal2)
            self.assertEqual(self.signal2.t_start, 10.0 * pq.ms)
            self.assertAlmostEqual(result.t_stop,
                                   t_stop_targ,
                                   delta=1e-12 * pq.ms)
            self.assertAlmostEqual(result.t_start,
                                   t_start_targ,
                                   delta=1e-12 * pq.ms)
            assert_arrays_almost_equal(result.times, targ.times, 1e-12 * pq.ms)
            self.assertEqual(result.sampling_rate, targ.sampling_rate)
            assert_array_equal(result.magnitude, targ.magnitude)
            assert_same_sub_schema(result, targ)
コード例 #22
0
    def test__rescale_new(self):
        result = self.signal1.copy()
        result = result.rescale(pq.uV)

        self.assertIsInstance(result, IrregularlySampledSignal)
        assert_neo_object_is_compliant(result)
        self.assertEqual(result.name, 'spam')
        self.assertEqual(result.description, 'eggs')
        self.assertEqual(result.file_origin, 'testfile.txt')
        self.assertEqual(result.annotations, {'arg1': 'test'})

        self.assertEqual(result.units, 1 * pq.uV)
        assert_arrays_almost_equal(np.array(result),
                                   self.data1.reshape(-1, 1) * 1000., 1e-10)
        assert_array_equal(result.times, self.time1quant)
コード例 #23
0
 def test_duplicate_with_new_data(self):
     signal1 = self.event
     new_data = np.sort(np.random.uniform(0, 100,
                                          (self.event.size))) * pq.ms
     signal1b = signal1.duplicate_with_new_data(new_data)
     assert_arrays_almost_equal(np.asarray(signal1b), np.asarray(new_data),
                                1e-12)
     # Note: Labels and Durations are NOT copied any more!!!
     # After duplicating, array annotations should always be empty,
     # because different length of data would cause inconsistencies
     # Only labels and durations should be available
     assert_arrays_equal(signal1b.labels, np.ndarray((0, ), dtype='S'))
     self.assertTrue('index' not in signal1b.array_annotations)
     self.assertTrue('test' not in signal1b.array_annotations)
     self.assertIsInstance(signal1b.array_annotations, ArrayDict)
コード例 #24
0
    def test1(self):
        """Read data into one big segment (default)"""
        full_range = 8192 * pq.mV
        bio = neo.io.BlackrockIO(filename=self.fn, full_range=full_range)
        block = bio.read_block(n_starts=[0], n_stops=[6])
        self.assertEqual(bio.header.Channel_Count, 2)
        self.assertEqual(bio.header.n_samples, 6)

        # Everything put in one segment
        self.assertEqual(len(block.segments), 1)
        seg = block.segments[0]
        self.assertEqual(len(seg.analogsignals), 2)

        assert_arrays_almost_equal(seg.analogsignals[0],
                                   [3., 4., 5., -3., -4., -5.] * pq.mV, .0001)
        assert_arrays_almost_equal(seg.analogsignals[1],
                                   [6., -4., -5., -6., 4., 5.] * pq.mV, .0001)
コード例 #25
0
    def test1(self):
        """Read data into one big segment (default)"""
        full_range = 8192 * pq.mV
        bio = neo.io.BlackrockIO(filename=self.fn, full_range=full_range)
        block = bio.read_block(n_starts=[0], n_stops=[6])
        self.assertEqual(bio.header.Channel_Count, 2)
        self.assertEqual(bio.header.n_samples, 6)

        # Everything put in one segment
        self.assertEqual(len(block.segments), 1)
        seg = block.segments[0]
        self.assertEqual(len(seg.analogsignals), 2)

        assert_arrays_almost_equal(seg.analogsignals[0],
                                   [3., 4., 5., -3., -4., -5.] * pq.mV, .0001)
        assert_arrays_almost_equal(seg.analogsignals[1],
                                   [6., -4., -5., -6., 4., 5.] * pq.mV, .0001)
コード例 #26
0
    def test__add_epoch(self):
        proxy_event = EventProxy(rawio=self.reader, event_channel_index=0,
                                 block_index=0, seg_index=0)

        loaded_event = proxy_event.load()

        regular_event = Event(times=loaded_event.times - 1 * loaded_event.units)

        seg = Segment()
        seg.events = [regular_event, proxy_event]

        # test cutting with two events one of which is a proxy
        epoch = add_epoch(seg, regular_event, proxy_event)

        assert_neo_object_is_compliant(epoch)
        assert_same_annotations(epoch, regular_event)
        assert_arrays_almost_equal(epoch.times, regular_event.times, 1e-12)
        assert_arrays_almost_equal(epoch.durations,
                                   np.ones(regular_event.shape) * loaded_event.units, 1e-12)
コード例 #27
0
ファイル: test_analogsignal.py プロジェクト: INM-6/python-neo
    def test__rescale_new(self):
        result = self.signal1.copy()
        result = result.rescale(pq.pA)

        self.assertIsInstance(result, AnalogSignal)
        assert_neo_object_is_compliant(result)
        self.assertEqual(result.name, 'spam')
        self.assertEqual(result.description, 'eggs')
        self.assertEqual(result.file_origin, 'testfile.txt')
        self.assertEqual(result.annotations, {'arg1': 'test'})
        self.assertEqual(result.array_annotations, {'anno1': [23], 'anno2': ['A']})
        self.assertIsInstance(result.array_annotations, ArrayDict)

        self.assertEqual(result.units, 1 * pq.pA)
        assert_arrays_almost_equal(np.array(result), self.data1.reshape(-1, 1) * 1000., 1e-10)

        self.assertIsInstance(result.channel_index, ChannelIndex)
        self.assertIsInstance(result.segment, Segment)
        self.assertIs(result.channel_index, self.signal1.channel_index)
        self.assertIs(result.segment, self.signal1.segment)
コード例 #28
0
    def test_rescale(self):
        times = [2, 3, 4, 5]
        labels = ["A", "B", "C", "D"]
        arr_ann = {'index': np.arange(4), 'test': ['a', 'b', 'c', 'd']}
        evt = Event(times * pq.ms, labels=labels, array_annotations=arr_ann)
        result = evt.rescale(pq.us)

        self.assertIsInstance(result, Event)
        assert_neo_object_is_compliant(result)
        assert_arrays_equal(result.array_annotations['index'], np.arange(4))
        assert_arrays_equal(result.array_annotations['test'],
                            np.array(['a', 'b', 'c', 'd']))
        self.assertIsInstance(result.array_annotations, ArrayDict)

        self.assertEqual(result.units, 1 * pq.us)
        assert_array_equal(evt.labels, result.labels)
        assert_arrays_almost_equal(result.times,
                                   [2000, 3000, 4000, 5000] * pq.us, 1e-9)
        assert_arrays_almost_equal(result.times.magnitude,
                                   np.array([2000, 3000, 4000, 5000]), 1e-9)
コード例 #29
0
    def test__rescale_new(self):
        result = self.signal1.copy()
        result = result.rescale(pq.pA)

        self.assertIsInstance(result, AnalogSignal)
        assert_neo_object_is_compliant(result)
        self.assertEqual(result.name, 'spam')
        self.assertEqual(result.description, 'eggs')
        self.assertEqual(result.file_origin, 'testfile.txt')
        self.assertEqual(result.annotations, {'arg1': 'test'})
        self.assertEqual(result.array_annotations, {'anno1': [23], 'anno2': ['A']})
        self.assertIsInstance(result.array_annotations, ArrayDict)

        self.assertEqual(result.units, 1 * pq.pA)
        assert_arrays_almost_equal(np.array(result), self.data1.reshape(-1, 1) * 1000., 1e-10)

        self.assertIsInstance(result.channel_index, ChannelIndex)
        self.assertIsInstance(result.segment, Segment)
        self.assertIs(result.channel_index, self.signal1.channel_index)
        self.assertIs(result.segment, self.signal1.segment)
コード例 #30
0
    def test_time_slice_different_units(self):
        #import numpy
        av = AnalogSignalArray(numpy.array([[0, 1, 2, 3, 4, 5],
                                            [0, 1, 2, 3, 4, 5]]).T,
                               t_start=10.0 * pq.ms,
                               sampling_rate=1.0 * pq.Hz,
                               units='mV')

        t_start = 2 * pq.s + 10.0 * pq.ms
        t_stop = 4 * pq.s + 10.0 * pq.ms
        av2 = av.time_slice(t_start, t_stop)
        correct = AnalogSignalArray(numpy.array([[2, 3], [2, 3]]).T,
                                    t_start=t_start,
                                    sampling_rate=1.0 * pq.Hz,
                                    units='mV')

        self.assertIsInstance(av2, AnalogSignalArray)
        self.assertAlmostEqual(av2.t_stop, t_stop, delta=1e-12 * ms)
        self.assertAlmostEqual(av2.t_start, t_start, delta=1e-12 * ms)
        assert_arrays_almost_equal(av2.times, correct.times, 1e-12 * ms)
        self.assertEqual(av2.sampling_rate, correct.sampling_rate)
コード例 #31
0
    def test__time_slice__no_explicit_time(self):
        self.signal2.t_start = 10.0 * pq.ms
        assert_neo_object_is_compliant(self.signal2)

        t1 = 2 * pq.s + 10.0 * pq.ms
        t2 = 4 * pq.s + 10.0 * pq.ms

        for t_start,t_stop in [(t1,None),(None,None),(None,t2)]:

            t_start_targ = t1 if t_start!=None else self.signal2.t_start
            t_stop_targ = t2 if t_stop!=None else self.signal2.t_stop

            result = self.signal2.time_slice(t_start, t_stop)
            self.assertIsInstance(result, AnalogSignalArray)
            assert_neo_object_is_compliant(result)
            self.assertEqual(result.name, 'spam')
            self.assertEqual(result.description, 'eggs')
            self.assertEqual(result.file_origin, 'testfile.txt')
            self.assertEqual(result.annotations, {'arg1': 'test'})

            targ_ind = np.where((self.signal2.times >= t_start_targ) &
                                (self.signal2.times < t_stop_targ))
            targ_array = self.signal2.magnitude[targ_ind]

            targ = AnalogSignalArray(targ_array,
                                     t_start=t_start_targ.rescale(pq.ms),
                                     sampling_rate=1.0*pq.Hz, units='mV',
                                     name='spam', description='eggs',
                                     file_origin='testfile.txt', arg1='test')
            assert_neo_object_is_compliant(result)

            assert_neo_object_is_compliant(self.signal2)
            self.assertEqual(self.signal2.t_start, 10.0 * pq.ms)
            self.assertAlmostEqual(result.t_stop, t_stop_targ, delta=1e-12*pq.ms)
            self.assertAlmostEqual(result.t_start, t_start_targ, delta=1e-12*pq.ms)
            assert_arrays_almost_equal(result.times, targ.times, 1e-12*pq.ms)
            self.assertEqual(result.sampling_rate, targ.sampling_rate)
            assert_arrays_equal(result, targ)
            assert_same_sub_schema(result, targ)
コード例 #32
0
    def test1(self):
        """Create clu and fet files based on spiketrains in a block.

        Checks that
            Files are created
            Converted to samples correctly
            Missing sampling rate are taken from IO reader default
            Spiketrains without cluster info are assigned to cluster 0
            Spiketrains across segments are concatenated
        """
        block = neo.Block()
        segment = neo.Segment()
        segment2 = neo.Segment()
        block.segments.append(segment)
        block.segments.append(segment2)

        # Fake spiketrain 1
        st1 = neo.SpikeTrain(times=[.002, .004, .006], units='s', t_stop=1.)
        st1.annotations['cluster'] = 0
        st1.annotations['group'] = 0
        wff = np.array([[11.3, 0.2], [-0.3, 12.3], [3.0, -2.5]])
        st1.annotations['waveform_features'] = wff
        segment.spiketrains.append(st1)

        # Create empty directory for writing
        if not os.path.exists(self.dirname):
            os.mkdir(self.dirname)
        delete_test_session(self.dirname)

        # Create writer
        kio = KlustaKwikIO(filename=os.path.join(self.dirname, 'base2'),
                           sampling_rate=1000.)
        kio.write_block(block)

        # Check files were created
        for fn in ['.fet.0', '.clu.0']:
            self.assertTrue(
                os.path.exists(os.path.join(self.dirname, 'base2' + fn)))

        # Check files contain correct content
        fi = file(os.path.join(self.dirname, 'base2.fet.0'))

        # first line is nbFeatures
        self.assertEqual(fi.readline(), '2\n')

        # Now check waveforms and times are same
        data = fi.readlines()
        new_wff = []
        new_times = []
        for line in data:
            line_split = line.split()
            new_wff.append([float(val) for val in line_split[:-1]])
            new_times.append(int(line_split[-1]))
        self.assertEqual(new_times, [2, 4, 6])
        assert_arrays_almost_equal(wff, np.array(new_wff), .00001)

        # Clusters on group 0
        data = file(os.path.join(self.dirname, 'base2.clu.0')).readlines()
        data = [int(d) for d in data]
        self.assertEqual(data, [1, 0, 0, 0])

        # Now read the features and test same
        block = kio.read_block()
        train = block.segments[0].spiketrains[0]
        assert_arrays_almost_equal(wff, train.annotations['waveform_features'],
                                   .00001)

        # Empty out test session again
        delete_test_session(self.dirname)
コード例 #33
0
 def test__times(self):
     for i in range(3):
         assert_arrays_almost_equal(
             self.signals[i].times,
             numpy.arange(self.data[i].size) / self.rates[i] +
             self.t_start[i], 1e-12 * ms)
コード例 #34
0
 def test__times_getter(self):
     for i, signal in enumerate(self.signals):
         targ = np.arange(self.data[i].size)
         targ = targ / self.rates[i] + self.t_start[i]
         assert_neo_object_is_compliant(signal)
         assert_arrays_almost_equal(signal.times, targ, 1e-12 * pq.ms)
コード例 #35
0
    def test1(self):
        """Create clu and fet files based on spiketrains in a block.

        Checks that
            Files are created
            Converted to samples correctly
            Missing sampling rate are taken from IO reader default
            Spiketrains without cluster info are assigned to cluster 0
            Spiketrains across segments are concatenated
        """
        block = neo.Block()
        segment = neo.Segment()
        segment2 = neo.Segment()
        block.segments.append(segment)
        block.segments.append(segment2)

        # Fake spiketrain 1
        st1 = neo.SpikeTrain(times=[.002, .004, .006], units='s', t_stop=1.)
        st1.annotations['cluster'] = 0
        st1.annotations['group'] = 0
        wff = np.array([
            [11.3, 0.2],
            [-0.3, 12.3],
            [3.0, -2.5]])
        st1.annotations['waveform_features'] = wff
        segment.spiketrains.append(st1)

        # Create empty directory for writing
        if not os.path.exists(self.dirname):
            os.mkdir(self.dirname)
        delete_test_session(self.dirname)

        # Create writer
        kio = KlustaKwikIO(filename=os.path.join(self.dirname, 'base2'),
                           sampling_rate=1000.)
        kio.write_block(block)

        # Check files were created
        for fn in ['.fet.0', '.clu.0']:
            self.assertTrue(os.path.exists(os.path.join(self.dirname,
                                                        'base2' + fn)))

        # Check files contain correct content
        fi = file(os.path.join(self.dirname, 'base2.fet.0'))

        # first line is nbFeatures
        self.assertEqual(fi.readline(), '2\n')

        # Now check waveforms and times are same
        data = fi.readlines()
        new_wff = []
        new_times = []
        for line in data:
            line_split = line.split()
            new_wff.append([float(val) for val in line_split[:-1]])
            new_times.append(int(line_split[-1]))
        self.assertEqual(new_times, [2, 4, 6])
        assert_arrays_almost_equal(wff, np.array(new_wff), .00001)

        # Clusters on group 0
        data = file(os.path.join(self.dirname, 'base2.clu.0')).readlines()
        data = [int(d) for d in data]
        self.assertEqual(data, [1, 0, 0, 0])

        # Now read the features and test same
        block = kio.read_block()
        train = block.segments[0].spiketrains[0]
        assert_arrays_almost_equal(wff, train.annotations['waveform_features'],
                                   .00001)

        # Empty out test session again
        delete_test_session(self.dirname)
コード例 #36
0
 def test__sampling_intervals_getter(self):
     for signal, times in zip(self.signals, self.times):
         assert_arrays_almost_equal(signal.sampling_intervals,
                                    np.diff(times),
                                    threshold=1e-15)
コード例 #37
0
 def test__times_getter(self):
     for i, signal in enumerate(self.signals):
         targ = np.arange(self.data[i].size)
         targ = targ/self.rates[i] + self.t_start[i]
         assert_neo_object_is_compliant(signal)
         assert_arrays_almost_equal(signal.times, targ, 1e-12*pq.ms)
コード例 #38
0
 def test__times(self):
     for i, signal in enumerate(self.signals):
         targ = np.arange(self.data[i].shape[0])
         targ = targ/self.rates[i] + self.t_start[i]
         assert_arrays_almost_equal(signal.times, targ, 1e-12*pq.ms)
コード例 #39
0
 def test__times(self):
     for i in range(3):
         assert_arrays_almost_equal(self.signals[i].times,
                                    numpy.arange(self.data[i].shape[0])/self.rates[i] + self.t_start[i],
                                    1e-12*ms)
コード例 #40
0
 def test__times(self):
     for i, signal in enumerate(self.signals):
         targ = np.arange(self.data[i].shape[0])
         targ = targ / self.rates[i] + self.t_start[i]
         assert_arrays_almost_equal(signal.times, targ, 1e-12 * pq.ms)