def test__add_quantity_should_preserve_data_complement(self): data2 = np.arange(10.0, 20.0) data2quant = data2 * pq.mV result = self.signal1 + data2quant 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.arange(10.0, 30.0, 2.0), units="mV", sampling_rate=1 * pq.kHz, name="spam", description="eggs", file_origin="testfile.txt", arg1="test", ) assert_neo_object_is_compliant(targ) assert_arrays_equal(result, targ) assert_same_sub_schema(result, targ)
def test__create_with_copy_false_should_return_view(self): data = np.arange(10.0) * pq.mV rate = 5000*pq.Hz signal = AnalogSignal(data, copy=False, sampling_rate=rate) data[3] = 99*pq.mV assert_neo_object_is_compliant(signal) self.assertEqual(signal[3, 0], 99*pq.mV)
def test__fake_neo__cascade(self): self.annotations['seed'] = None obj_type = 'ChannelIndex' cascade = True res = fake_neo(obj_type=obj_type, cascade=cascade) self.assertTrue(isinstance(res, ChannelIndex)) assert_neo_object_is_compliant(res) self.assertEqual(res.annotations, self.annotations) for child in res.children_recur: del child.annotations['i'] del child.annotations['j'] self.assertEqual(len(res.units), 1) unit = res.units[0] self.assertEqual(unit.annotations, self.annotations) self.assertEqual(len(res.analogsignals), 1) self.assertEqual(res.analogsignals[0].annotations, self.annotations) self.assertEqual(len(unit.spiketrains), 1) self.assertEqual(unit.spiketrains[0].annotations, self.annotations)
def test__children(self): params = {'test2': 'y1', 'test3': True} epc = Epoch([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns, labels=np.array(['test epoch 1', 'test epoch 2', 'test epoch 3'], dtype='S'), name='test', description='tester', file_origin='test.file', test1=1, **params) epc.annotate(test1=1.1, test0=[1, 2]) assert_neo_object_is_compliant(epc) segment = Segment(name='seg1') segment.epochs = [epc] segment.create_many_to_one_relationship() self.assertEqual(epc._single_parent_objects, ('Segment',)) self.assertEqual(epc._multi_parent_objects, ()) self.assertEqual(epc._single_parent_containers, ('segment',)) self.assertEqual(epc._multi_parent_containers, ()) self.assertEqual(epc._parent_objects, ('Segment',)) self.assertEqual(epc._parent_containers, ('segment',)) self.assertEqual(len(epc.parents), 1) self.assertEqual(epc.parents[0].name, 'seg1') assert_neo_object_is_compliant(epc)
def test__children(self): segment = Segment(name='seg1') segment.spikes = [self.spike1] segment.create_many_to_one_relationship() unit = Unit(name='unit1') unit.spikes = [self.spike1] unit.create_many_to_one_relationship() self.assertEqual(self.spike1._single_parent_objects, ('Segment', 'Unit')) self.assertEqual(self.spike1._multi_parent_objects, ()) self.assertEqual(self.spike1._single_parent_containers, ('segment', 'unit')) self.assertEqual(self.spike1._multi_parent_containers, ()) self.assertEqual(self.spike1._parent_objects, ('Segment', 'Unit')) self.assertEqual(self.spike1._parent_containers, ('segment', 'unit')) self.assertEqual(len(self.spike1.parents), 2) self.assertEqual(self.spike1.parents[0].name, 'seg1') self.assertEqual(self.spike1.parents[1].name, 'unit1') assert_neo_object_is_compliant(self.spike1)
def test__recordingchannelgroup__cascade(self): obj_type = 'RecordingChannelGroup' cascade = True res = fake_neo(obj_type=obj_type, cascade=cascade) self.assertTrue(isinstance(res, RecordingChannelGroup)) assert_neo_object_is_compliant(res) self.assertEqual(res.annotations, self.annotations) self.assertEqual(len(res.recordingchannels), 1) rchan = res.recordingchannels[0] self.assertEqual(rchan.annotations, self.annotations) self.assertEqual(len(res.units), 1) unit = res.units[0] self.assertEqual(unit.annotations, self.annotations) self.assertEqual(len(res.analogsignalarrays), 1) self.assertEqual(res.analogsignalarrays[0].annotations, self.annotations) self.assertEqual(len(rchan.analogsignals), 1) self.assertEqual(len(rchan.irregularlysampledsignals), 1) self.assertEqual(rchan.analogsignals[0].annotations, self.annotations) self.assertEqual(rchan.irregularlysampledsignals[0].annotations, self.annotations) self.assertEqual(len(unit.spiketrains), 1) self.assertEqual(len(unit.spikes), 1) self.assertEqual(unit.spiketrains[0].annotations, self.annotations) self.assertEqual(unit.spikes[0].annotations, self.annotations)
def test_time_slice_differnt_units(self): params = {'test2': 'y1', 'test3': True} arr_ann = {'index': np.arange(9), 'test': np.arange(100, 109)} evt = Event([0.1, 0.5, 1.1, 1.5, 1.7, 2.2, 2.9, 3.1, 3.3] * pq.ms, name='test', description='tester', file_origin='test.file', test1=1, array_annotations=arr_ann, **params) assert_neo_object_is_compliant(evt) evt.annotate(test1=1.1, test0=[1, 2]) targ = Event([2.2, 2.9] * pq.ms, name='test', description='tester', file_origin='test.file', test1=1, **params) assert_neo_object_is_compliant(targ) targ.annotate(test1=1.1, test0=[1, 2]) t_start = 0.002 * pq.s t_stop = 0.003 * pq.s result = evt.time_slice(t_start, t_stop) assert_arrays_equal(targ, result) self.assertEqual(targ.name, result.name) self.assertEqual(targ.description, result.description) self.assertEqual(targ.file_origin, result.file_origin) self.assertEqual(targ.annotations['test0'], result.annotations['test0']) self.assertEqual(targ.annotations['test1'], result.annotations['test1']) self.assertEqual(targ.annotations['test2'], result.annotations['test2']) assert_arrays_equal(result.array_annotations['index'], np.arange(5, 7)) assert_arrays_equal(result.array_annotations['test'], np.arange(105, 107)) self.assertIsInstance(result.array_annotations, ArrayDict)
def test__sampling_period(self): result1 = self.spike1.sampling_period self.spike1.sampling_rate = None assert_neo_object_is_compliant(self.spike1) result2 = self.spike1.sampling_period self.spike1.sampling_rate = self.sampling_rate1 self.spike1.sampling_period = 10.0 * pq.ms assert_neo_object_is_compliant(self.spike1) result3a = self.spike1.sampling_period result3b = self.spike1.sampling_rate self.spike1.sampling_period = None result4a = self.spike1.sampling_period result4b = self.spike1.sampling_rate self.assertEqual(result1, 10.0 / pq.Hz) self.assertEqual(result1.units, 1.0 / pq.Hz) self.assertEqual(result2, None) self.assertEqual(result3a, 10.0 * pq.ms) self.assertEqual(result3a.units, 1.0 * pq.ms) self.assertEqual(result3b, 0.1 / pq.ms) self.assertEqual(result3b.units, 1.0 / pq.ms) self.assertEqual(result4a, None) self.assertEqual(result4b, None)
def test_defaults(self): res = generate_one_simple_block() self.assertTrue(isinstance(res, Block)) assert_neo_object_is_compliant(res) self.assertEqual(len(res.segments), 0)
def test__children(self): params = {"test2": "y1", "test3": True} evt = Event( 1.5 * pq.ms, label="test epoch", name="test", description="tester", file_origin="test.file", test1=1, **params ) evt.annotate(test1=1.1, test0=[1, 2]) assert_neo_object_is_compliant(evt) segment = Segment(name="seg1") segment.events = [evt] segment.create_many_to_one_relationship() self.assertEqual(evt._single_parent_objects, ("Segment",)) self.assertEqual(evt._multi_parent_objects, ()) self.assertEqual(evt._single_parent_containers, ("segment",)) self.assertEqual(evt._multi_parent_containers, ()) self.assertEqual(evt._parent_objects, ("Segment",)) self.assertEqual(evt._parent_containers, ("segment",)) self.assertEqual(len(evt.parents), 1) self.assertEqual(evt.parents[0].name, "seg1") assert_neo_object_is_compliant(evt)
def test__slice_should_return_AnalogSignalArray(self): # slice for index in (0, np.int64(0)): result = self.signal1[3:8, index] self.assertIsInstance(result, AnalogSignal) assert_neo_object_is_compliant(result) self.assertEqual(result.name, 'spam') # should slicing really preserve name and description? self.assertEqual(result.description, 'eggs') # perhaps these should be modified to indicate the slice? self.assertEqual(result.file_origin, 'testfile.txt') self.assertEqual(result.annotations, {'arg1': 'test'}) self.assertEqual(result.size, 5) self.assertEqual(result.sampling_period, self.signal1.sampling_period) self.assertEqual(result.sampling_rate, self.signal1.sampling_rate) self.assertEqual(result.t_start, self.signal1.t_start+3*result.sampling_period) self.assertEqual(result.t_stop, result.t_start + 5*result.sampling_period) assert_array_equal(result.magnitude, self.data1[3:8].reshape(-1, 1)) # Test other attributes were copied over (in this case, defaults) self.assertEqual(result.file_origin, self.signal1.file_origin) self.assertEqual(result.name, self.signal1.name) self.assertEqual(result.description, self.signal1.description) self.assertEqual(result.annotations, self.signal1.annotations)
def test__children(self): signal = self.signals[0] segment = Segment(name='seg1') segment.analogsignals = [signal] segment.create_many_to_one_relationship() chx = ChannelIndex(name='chx1', index=np.arange(signal.shape[1])) chx.analogsignals = [signal] chx.create_many_to_one_relationship() self.assertEqual(signal._single_parent_objects, ('Segment', 'ChannelIndex')) self.assertEqual(signal._multi_parent_objects, ()) self.assertEqual(signal._single_parent_containers, ('segment', 'channel_index')) self.assertEqual(signal._multi_parent_containers, ()) self.assertEqual(signal._parent_objects, ('Segment', 'ChannelIndex')) self.assertEqual(signal._parent_containers, ('segment', 'channel_index')) self.assertEqual(len(signal.parents), 2) self.assertEqual(signal.parents[0].name, 'seg1') self.assertEqual(signal.parents[1].name, 'chx1') assert_neo_object_is_compliant(signal)
def test_time_slice_differnt_units(self): targdataquant = [[1.0], [2.0], [3.0]] * pq.mV targtime = np.logspace(1, 5, 10) targtimequant = targtime [1:4] *pq.ms targ_signal = IrregularlySampledSignal(targtimequant, signal=targdataquant, name='spam', description='eggs', file_origin='testfile.txt', arg1='test') t_start = 15 t_stop = 250 t_start = 0.015 * pq.s t_stop = .250 * pq.s result = self.signal1.time_slice(t_start, t_stop) assert_array_equal(result, targ_signal) assert_array_equal(result.times, targtimequant) self.assertEqual(result.units, 1*pq.mV) 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'})
def test__create_with_copy_true_should_return_copy(self): data = np.arange(20.0).reshape((10, 2)) * pq.mV rate = 5000 * pq.Hz signal = AnalogSignal(data, copy=True, sampling_rate=rate) assert_neo_object_is_compliant(signal) data[3, 0] = 0.099 * pq.V self.assertNotEqual(signal[3, 0], 99 * pq.mV)
def check_creation(self, rchan): assert_neo_object_is_compliant(rchan) seed = rchan.annotations['seed'] targ0 = get_fake_value('index', int, seed=seed+0, obj=RecordingChannel) self.assertEqual(rchan.index, targ0) targ1 = get_fake_value('coordinate', pq.Quantity, dim=1, seed=seed+1) assert_arrays_equal(rchan.coordinate, targ1) targ2 = get_fake_value('name', str, seed=seed+2, obj=RecordingChannel) self.assertEqual(rchan.name, targ2) targ3 = get_fake_value('description', str, seed=seed+3, obj=RecordingChannel) self.assertEqual(rchan.description, targ3) targ4 = get_fake_value('file_origin', str) self.assertEqual(rchan.file_origin, targ4) targ5 = get_annotations() targ5['seed'] = seed self.assertEqual(rchan.annotations, targ5) self.assertTrue(hasattr(rchan, 'analogsignals')) self.assertTrue(hasattr(rchan, 'irregularlysampledsignals')) self.assertEqual(len(rchan.analogsignals), self.nchildren) self.assertEqual(len(rchan.irregularlysampledsignals), self.nchildren)
def test__fake_neo__cascade(self): self.annotations['seed'] = None obj_type = Segment cascade = True res = fake_neo(obj_type=obj_type, cascade=cascade) self.assertTrue(isinstance(res, Segment)) assert_neo_object_is_compliant(res) self.assertEqual(res.annotations, self.annotations) self.assertEqual(len(res.analogsignals), 1) self.assertEqual(len(res.irregularlysampledsignals), 1) self.assertEqual(len(res.spiketrains), 1) self.assertEqual(len(res.events), 1) self.assertEqual(len(res.epochs), 1) for child in res.children: del child.annotations['i'] del child.annotations['j'] self.assertEqual(res.analogsignals[0].annotations, self.annotations) self.assertEqual(res.irregularlysampledsignals[0].annotations, self.annotations) self.assertEqual(res.spiketrains[0].annotations, self.annotations) self.assertEqual(res.events[0].annotations, self.annotations) self.assertEqual(res.epochs[0].annotations, self.annotations)
def test_block_segment(self): objects = [Segment, Block] res = generate_from_supported_objects(objects) self.assertTrue(isinstance(res, Block)) assert_neo_object_is_compliant(res) self.assertEqual(len(res.segments), 3) seg1, seg2, seg3 = res.segments self.assertEqual(len(seg1.analogsignals), 0) self.assertEqual(len(seg1.irregularlysampledsignals), 0) self.assertEqual(len(seg1.spiketrains), 0) self.assertEqual(len(seg1.events), 0) self.assertEqual(len(seg1.epochs), 0) self.assertEqual(len(seg2.analogsignals), 0) self.assertEqual(len(seg2.irregularlysampledsignals), 0) self.assertEqual(len(seg2.spiketrains), 0) self.assertEqual(len(seg2.events), 0) self.assertEqual(len(seg2.epochs), 0) self.assertEqual(len(seg3.analogsignals), 0) self.assertEqual(len(seg3.irregularlysampledsignals), 0) self.assertEqual(len(seg3.spiketrains), 0) self.assertEqual(len(seg3.events), 0) self.assertEqual(len(seg3.epochs), 0)
def check_creation(self, chx): assert_neo_object_is_compliant(chx) seed = chx.annotations['seed'] # for i, unit in enumerate(chx.units): # for sigarr in chx.analogsignals: # self.assertEqual(unit.channel_indexes[0], # sigarr.channel_index[i]) targ2 = get_fake_value('name', str, seed=seed+4, obj=ChannelIndex) self.assertEqual(chx.name, targ2) targ3 = get_fake_value('description', str, seed=seed+5, obj=ChannelIndex) self.assertEqual(chx.description, targ3) targ4 = get_fake_value('file_origin', str) self.assertEqual(chx.file_origin, targ4) targ5 = get_annotations() targ5['seed'] = seed self.assertEqual(chx.annotations, targ5) self.assertTrue(hasattr(chx, 'units')) self.assertTrue(hasattr(chx, 'analogsignals')) self.assertEqual(len(chx.units), self.nchildren) self.assertEqual(len(chx.analogsignals), self.nchildren)
def test_time_slice_empty(self): params = {'test2': 'y1', 'test3': True} epc = Epoch([]*pq.ms, durations=[]*pq.ns, labels=np.array([], dtype='S'), name='test', description='tester', file_origin='test.file', test1=1, **params) epc.annotate(test1=1.1, test0=[1, 2]) assert_neo_object_is_compliant(epc) targ = Epoch([]*pq.ms, durations=[]*pq.ns, labels=np.array([], dtype='S'), name='test', description='tester', file_origin='test.file', test1=1, **params) targ.annotate(test1=1.1, test0=[1, 2]) assert_neo_object_is_compliant(targ) t_start = 1.2 t_stop = 1.6 result = epc.time_slice(t_start, t_stop) assert_arrays_equal(result.times, targ.times) assert_arrays_equal(result.durations, targ.durations) assert_arrays_equal(result.labels, targ.labels) self.assertEqual(result.name, targ.name) self.assertEqual(result.description, targ.description) self.assertEqual(result.file_origin, targ.file_origin) self.assertEqual(result.annotations['test0'], targ.annotations['test0']) self.assertEqual(result.annotations['test1'], targ.annotations['test1']) self.assertEqual(result.annotations['test2'], targ.annotations['test2'])
def test_time_slice_differnt_units(self): params = {'test2': 'y1', 'test3': True} epc = Epoch([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns, labels=np.array(['test epoch 1', 'test epoch 2', 'test epoch 3'], dtype='S'), name='test', description='tester', file_origin='test.file', test1=1, **params) epc.annotate(test1=1.1, test0=[1, 2]) assert_neo_object_is_compliant(epc) targ = Epoch([1.5]*pq.ms, durations=[40]*pq.ns, labels=np.array(['test epoch 2'], dtype='S'), name='test', description='tester', file_origin='test.file', test1=1, **params) targ.annotate(test1=1.1, test0=[1, 2]) assert_neo_object_is_compliant(targ) t_start = 0.0012 * pq.s t_stop = 0.0016 * pq.s result = epc.time_slice(t_start, t_stop) assert_arrays_equal(result.times, targ.times) assert_arrays_equal(result.durations, targ.durations) assert_arrays_equal(result.labels, targ.labels) self.assertEqual(result.name, targ.name) self.assertEqual(result.description, targ.description) self.assertEqual(result.file_origin, targ.file_origin) self.assertEqual(result.annotations['test0'], targ.annotations['test0']) self.assertEqual(result.annotations['test1'], targ.annotations['test1']) self.assertEqual(result.annotations['test2'], targ.annotations['test2'])
def test_time_slice_differnt_units(self): params = {'test2': 'y1', 'test3': True} evt = Event([0.1, 0.5, 1.1, 1.5, 1.7, 2.2, 2.9, 3.1, 3.3]*pq.ms, name='test', description='tester', file_origin='test.file', test1=1, **params) assert_neo_object_is_compliant(evt) evt.annotate(test1=1.1, test0=[1, 2]) targ = Event([ 2.2, 2.9 ]*pq.ms, name='test', description='tester', file_origin='test.file', test1=1, **params) assert_neo_object_is_compliant(targ) targ.annotate(test1=1.1, test0=[1, 2]) t_start = 0.002 * pq.s t_stop = 0.003 * pq.s result = evt.time_slice(t_start, t_stop) assert_arrays_equal(targ, result) self.assertEqual(targ.name, result.name) self.assertEqual(targ.description, result.description) self.assertEqual(targ.file_origin, result.file_origin) self.assertEqual(targ.annotations['test0'], result.annotations['test0']) self.assertEqual(targ.annotations['test1'], result.annotations['test1']) self.assertEqual(targ.annotations['test2'], result.annotations['test2'])
def check_creation(self, rcg): assert_neo_object_is_compliant(rcg) seed = rcg.annotations['seed'] for i, rchan in enumerate(rcg.recordingchannels): self.assertEqual(rchan.name, rcg.channel_names[i].astype(str)) self.assertEqual(rchan.index, rcg.channel_indexes[i]) for i, unit in enumerate(rcg.units): for sigarr in rcg.analogsignalarrays: self.assertEqual(unit.channel_indexes[0], sigarr.channel_index[i]) targ2 = get_fake_value('name', str, seed=seed+2, obj=RecordingChannelGroup) self.assertEqual(rcg.name, targ2) targ3 = get_fake_value('description', str, seed=seed+3, obj=RecordingChannelGroup) self.assertEqual(rcg.description, targ3) targ4 = get_fake_value('file_origin', str) self.assertEqual(rcg.file_origin, targ4) targ5 = get_annotations() targ5['seed'] = seed self.assertEqual(rcg.annotations, targ5) self.assertTrue(hasattr(rcg, 'recordingchannels')) self.assertTrue(hasattr(rcg, 'units')) self.assertTrue(hasattr(rcg, 'analogsignalarrays')) self.assertEqual(len(rcg.recordingchannels), self.nchildren) self.assertEqual(len(rcg.units), self.nchildren) self.assertEqual(len(rcg.analogsignalarrays), self.nchildren)
def check_creation(self, blk): assert_neo_object_is_compliant(blk) seed = blk.annotations['seed'] targ0 = get_fake_value('file_datetime', datetime, seed=seed+0) self.assertEqual(blk.file_datetime, targ0) targ1 = get_fake_value('rec_datetime', datetime, seed=seed+1) self.assertEqual(blk.rec_datetime, targ1) targ2 = get_fake_value('index', int, seed=seed+2, obj=Block) self.assertEqual(blk.index, targ2) targ3 = get_fake_value('name', str, seed=seed+3, obj=Block) self.assertEqual(blk.name, targ3) targ4 = get_fake_value('description', str, seed=seed+4, obj=Block) self.assertEqual(blk.description, targ4) targ5 = get_fake_value('file_origin', str) self.assertEqual(blk.file_origin, targ5) targ6 = get_annotations() targ6['seed'] = seed self.assertEqual(blk.annotations, targ6) self.assertTrue(hasattr(blk, 'channel_indexes')) self.assertTrue(hasattr(blk, 'segments')) self.assertEqual(len(blk.channel_indexes), self.nchildren) self.assertEqual(len(blk.segments), self.nchildren)
def test_assert_readed_neo_object_is_compliant(self): """Reading %s files in `files_to_test` produces compliant objects. Compliance test: neo.test.tools.assert_neo_object_is_compliant """ % self.ioclass.__name__ # This is for files presents at G-Node or generated for filename in self.files_to_test: # Load each file in `files_to_test` filename = os.path.join(self.local_test_dir, filename) if self.ioclass.mode == 'file': r = self.ioclass(filename = filename) elif self.ioclass.mode == 'dir': r = self.ioclass(dirname = filename) else: continue # Read the highest supported object from the file obname = self.ioclass.supported_objects[0].__name__.lower() ob_reader = getattr(r, 'read_%s' % obname) ob = ob_reader(cascade = True, lazy = False) # Check compliance of the block assert_neo_object_is_compliant(ob) # same but lazy ob = ob_reader(cascade = True, lazy = True) assert_neo_object_is_compliant(ob) try: # for HDF5IO file should be closed before being opened again in test r.close() except: pass
def test__create_with_copy_false_should_return_view(self): data = np.arange(20.0).reshape((10, 2)) * pq.mV rate = 5000*pq.Hz signal = AnalogSignalArray(data, copy=False, sampling_rate=rate) assert_neo_object_is_compliant(signal) data[3, 0] = 99*pq.mV self.assertEqual(signal[3, 0], 99000*pq.uV)
def test_time_slice_none_both(self): targdataquant = [[0.0], [1.0], [2.0], [3.0], [4.0], [5.0], [6.0], [7.0], [8.0], [9.0]] * pq.mV targtime = np.logspace(1, 5, 10) targtimequant = targtime[0:10] * pq.ms targ_signal = IrregularlySampledSignal(targtimequant, signal=targdataquant, name='spam', description='eggs', file_origin='testfile.txt', arg1='test') t_start = None t_stop = None result = self.signal1.time_slice(t_start, t_stop) assert_array_equal(result, targ_signal) assert_array_equal(result.times, targtimequant) self.assertEqual(result.units, 1 * pq.mV) 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'}) assert_arrays_equal(result.array_annotations['anno1'], np.array([23])) assert_arrays_equal(result.array_annotations['anno2'], np.array(['A'])) self.assertIsInstance(result.array_annotations, ArrayDict)
def test_time_slice_none_both(self): params = {'test2': 'y1', 'test3': True} arr_ann = {'index': np.arange(3), 'test': ['a', 'b', 'c']} epc = Epoch([1.1, 1.5, 1.7] * pq.ms, durations=[20, 40, 60] * pq.ns, labels=np.array(['test epoch 1', 'test epoch 2', 'test epoch 3'], dtype='S'), name='test', description='tester', file_origin='test.file', test1=1, array_annotations=arr_ann, **params) epc.annotate(test1=1.1, test0=[1, 2]) assert_neo_object_is_compliant(epc) targ = Epoch([1.1, 1.5, 1.7] * pq.ms, durations=[20, 40, 60] * pq.ns, labels=np.array(['test epoch 1', 'test epoch 2', 'test epoch 3'], dtype='S'), name='test', description='tester', file_origin='test.file', test1=1, array_annotations=arr_ann, **params) targ.annotate(test1=1.1, test0=[1, 2]) assert_neo_object_is_compliant(targ) t_start = None t_stop = None result = epc.time_slice(t_start, t_stop) assert_arrays_equal(result.times, targ.times) assert_arrays_equal(result.durations, targ.durations) assert_arrays_equal(result.labels, targ.labels) self.assertEqual(result.name, targ.name) self.assertEqual(result.description, targ.description) self.assertEqual(result.file_origin, targ.file_origin) self.assertEqual(result.annotations['test0'], targ.annotations['test0']) self.assertEqual(result.annotations['test1'], targ.annotations['test1']) self.assertEqual(result.annotations['test2'], targ.annotations['test2']) assert_arrays_equal(result.array_annotations['index'], np.array([0, 1, 2])) assert_arrays_equal(result.array_annotations['test'], np.array(['a', 'b', 'c'])) self.assertIsInstance(result.array_annotations, ArrayDict)
def test__children(self): signal = self.signals[0] segment = Segment(name='seg1') segment.analogsignals = [signal] segment.create_many_to_one_relationship() rchan = RecordingChannel(name='rchan1') rchan.analogsignals = [signal] rchan.create_many_to_one_relationship() self.assertEqual(signal._single_parent_objects, ('Segment', 'RecordingChannel')) self.assertEqual(signal._multi_parent_objects, ()) self.assertEqual(signal._single_parent_containers, ('segment', 'recordingchannel')) self.assertEqual(signal._multi_parent_containers, ()) self.assertEqual(signal._parent_objects, ('Segment', 'RecordingChannel')) self.assertEqual(signal._parent_containers, ('segment', 'recordingchannel')) self.assertEqual(len(signal.parents), 2) self.assertEqual(signal.parents[0].name, 'seg1') self.assertEqual(signal.parents[1].name, 'rchan1') assert_neo_object_is_compliant(signal)
def test_recordingchannelgroup__compliance(self): assert_neo_object_is_compliant(self.rcg1) assert_neo_object_is_compliant(self.rcg2) self.assertEqual(self.rcg1.name, 'test') self.assertEqual(self.rcg2.name, 'test') self.assertEqual(self.rcg1.description, 'tester 1') self.assertEqual(self.rcg2.description, 'tester 2') self.assertEqual(self.rcg1.file_origin, 'test.file') self.assertEqual(self.rcg2.file_origin, 'test.file') self.assertEqual(self.rcg1.annotations['testarg0'], [1, 2, 3]) self.assertEqual(self.rcg2.annotations['testarg10'], [1, 2, 3]) self.assertEqual(self.rcg1.annotations['testarg1'], 1.1) self.assertEqual(self.rcg2.annotations['testarg1'], 1) self.assertEqual(self.rcg2.annotations['testarg11'], 1.1) self.assertEqual(self.rcg1.annotations['testarg2'], 'yes') self.assertEqual(self.rcg2.annotations['testarg2'], 'yes') self.assertTrue(self.rcg1.annotations['testarg3']) self.assertTrue(self.rcg2.annotations['testarg3']) self.assertTrue(hasattr(self.rcg1, 'units')) self.assertTrue(hasattr(self.rcg2, 'units')) self.assertEqual(len(self.rcg1.units), 2) self.assertEqual(len(self.rcg2.units), 3) self.assertEqual(self.rcg1.units, self.units1) self.assertEqual(self.rcg2.units, self.units2) self.assertTrue(hasattr(self.rcg1, 'recordingchannels')) self.assertTrue(hasattr(self.rcg2, 'recordingchannels')) self.assertEqual(len(self.rcg1.recordingchannels), 2) self.assertEqual(len(self.rcg2.recordingchannels), 3) for res, targ in zip(self.rcg1.recordingchannels, self.rchan1): self.assertEqual(res.name, targ.name) for res, targ in zip(self.rcg2.recordingchannels, self.rchan2): self.assertEqual(res.name, targ.name) self.assertTrue(hasattr(self.rcg1, 'analogsignalarrays')) self.assertTrue(hasattr(self.rcg2, 'analogsignalarrays')) self.assertEqual(len(self.rcg1.analogsignalarrays), 2) self.assertEqual(len(self.rcg2.analogsignalarrays), 3) for res, targ in zip(self.rcg1.analogsignalarrays, self.sigarr1): assert_arrays_equal(res, targ) self.assertEqual(res.name, targ.name) for res, targ in zip(self.rcg2.analogsignalarrays, self.sigarr2): assert_arrays_equal(res, targ) self.assertEqual(res.name, targ.name)
def test_all_supported(self): objects = [Block, Segment, ChannelIndex, Unit, AnalogSignal, IrregularlySampledSignal, SpikeTrain, Event, Epoch] res = generate_one_simple_block(supported_objects=objects) self.assertTrue(isinstance(res, Block)) assert_neo_object_is_compliant(res) self.assertEqual(len(res.segments), 3) seg1, seg2, seg3 = res.segments self.assertEqual(len(seg1.analogsignals), 4) self.assertEqual(len(seg1.irregularlysampledsignals), 0) self.assertEqual(len(seg1.spiketrains), 6) self.assertEqual(len(seg1.events), 3) self.assertEqual(len(seg1.epochs), 2) self.assertEqual(len(seg2.analogsignals), 4) self.assertEqual(len(seg2.irregularlysampledsignals), 0) self.assertEqual(len(seg2.spiketrains), 6) self.assertEqual(len(seg2.events), 3) self.assertEqual(len(seg2.epochs), 2) self.assertEqual(len(seg3.analogsignals), 4) self.assertEqual(len(seg3.irregularlysampledsignals), 0) self.assertEqual(len(seg3.spiketrains), 6) self.assertEqual(len(seg3.events), 3) self.assertEqual(len(seg3.epochs), 2)
def test_time_slice_none_start(self): params = {'test2': 'y1', 'test3': True} evt = Event([0.1, 0.5, 1.1, 1.5, 1.7, 2.2, 2.9, 3.0, 3.1, 3.3] * pq.ms, name='test', description='tester', file_origin='test.file', test1=1, **params) evt.annotate(test1=1.1, test0=[1, 2]) assert_neo_object_is_compliant(evt) targ = Event([0.1, 0.5, 1.1, 1.5, 1.7, 2.2, 2.9, 3.0] * pq.ms) t_start = None t_stop = 3.0 result = evt.time_slice(t_start, t_stop) assert_arrays_equal(targ, result) self.assertEqual(evt.name, result.name) self.assertEqual(evt.description, result.description) self.assertEqual(evt.file_origin, result.file_origin) self.assertEqual(evt.annotations['test0'], result.annotations['test0']) self.assertEqual(evt.annotations['test1'], result.annotations['test1']) self.assertEqual(evt.annotations['test2'], result.annotations['test2'])
def test_time_slice_none_both(self): params = {'test2': 'y1', 'test3': True} arr_ann = {'index': np.arange(10), 'test': np.arange(100, 110)} evt = Event([0.1, 0.5, 1.1, 1.5, 1.7, 2.2, 2.9, 3.0, 3.1, 3.3] * pq.ms, name='test', description='tester', file_origin='test.file', test1=1, array_annotations=arr_ann, **params) assert_neo_object_is_compliant(evt) evt.annotate(test1=1.1, test0=[1, 2]) t_start = None t_stop = None result = evt.time_slice(t_start, t_stop) assert_arrays_equal(evt, result) self.assertEqual(evt.name, result.name) self.assertEqual(evt.description, result.description) self.assertEqual(evt.file_origin, result.file_origin) self.assertEqual(evt.annotations['test0'], result.annotations['test0']) self.assertEqual(evt.annotations['test1'], result.annotations['test1']) self.assertEqual(evt.annotations['test2'], result.annotations['test2']) assert_arrays_equal(result.array_annotations['index'], np.arange(10)) assert_arrays_equal(result.array_annotations['test'], np.arange(100, 110)) self.assertIsInstance(result.array_annotations, ArrayDict)
def test__children(self): params = {'test2': 'y1', 'test3': True} epc = Epoch([1.1, 1.5, 1.7] * pq.ms, durations=[20, 40, 60] * pq.ns, labels=np.array(['test epoch 1', 'test epoch 2', 'test epoch 3'], dtype='U'), name='test', description='tester', file_origin='test.file', test1=1, **params) epc.annotate(test1=1.1, test0=[1, 2]) assert_neo_object_is_compliant(epc) segment = Segment(name='seg1') segment.epochs = [epc] segment.create_many_to_one_relationship() self.assertEqual(epc._parent_objects, ('Segment',)) self.assertEqual(epc._parent_containers, ('segment',)) self.assertEqual(epc._parent_objects, ('Segment',)) self.assertEqual(epc._parent_containers, ('segment',)) self.assertEqual(len(epc.parents), 1) self.assertEqual(epc.parents[0].name, 'seg1') assert_neo_object_is_compliant(epc)
def test__add_quantity_should_preserve_data_complement(self): data2 = np.arange(10.0, 20.0).reshape(-1, 1) data2quant = data2 * pq.mV result = self.signal1 + data2quant 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.arange(10.0, 30.0, 2.0), units="mV", sampling_rate=1 * pq.kHz, name='spam', description='eggs', file_origin='testfile.txt', arg1='test') assert_neo_object_is_compliant(targ) assert_array_equal(result, targ) assert_same_sub_schema(result, targ)
def test_Event_creation(self): params = {'test2': 'y1', 'test3': True} arr_ann = {'names': ['a', 'b', 'c'], 'index': np.arange(10, 13)} evt = Event([1.1, 1.5, 1.7] * pq.ms, labels=np.array(['test event 1', 'test event 2', 'test event 3'], dtype='S'), name='test', description='tester', file_origin='test.file', test1=1, array_annotations=arr_ann, **params) evt.annotate(test1=1.1, test0=[1, 2]) assert_neo_object_is_compliant(evt) assert_arrays_equal(evt.times, [1.1, 1.5, 1.7] * pq.ms) assert_arrays_equal(evt.labels, np.array(['test event 1', 'test event 2', 'test event 3'], dtype='S')) self.assertEqual(evt.name, 'test') self.assertEqual(evt.description, 'tester') self.assertEqual(evt.file_origin, 'test.file') self.assertEqual(evt.annotations['test0'], [1, 2]) self.assertEqual(evt.annotations['test1'], 1.1) self.assertEqual(evt.annotations['test2'], 'y1') self.assertTrue(evt.annotations['test3']) assert_arrays_equal(evt.array_annotations['names'], np.array(['a', 'b', 'c'])) assert_arrays_equal(evt.array_annotations['index'], np.arange(10, 13)) self.assertIsInstance(evt.array_annotations, ArrayDict)
def test_Event_creation(self): params = {'test2': 'y1', 'test3': True} evt = Event([1.1, 1.5, 1.7]*pq.ms, labels=np.array(['test event 1', 'test event 2', 'test event 3'], dtype='S'), name='test', description='tester', file_origin='test.file', test1=1, **params) evt.annotate(test1=1.1, test0=[1, 2]) assert_neo_object_is_compliant(evt) assert_arrays_equal(evt.times, [1.1, 1.5, 1.7]*pq.ms) assert_arrays_equal(evt.labels, np.array(['test event 1', 'test event 2', 'test event 3'], dtype='S')) self.assertEqual(evt.name, 'test') self.assertEqual(evt.description, 'tester') self.assertEqual(evt.file_origin, 'test.file') self.assertEqual(evt.annotations['test0'], [1, 2]) self.assertEqual(evt.annotations['test1'], 1.1) self.assertEqual(evt.annotations['test2'], 'y1') self.assertTrue(evt.annotations['test3'])
def test_EventArray_merge(self): params1 = {'testarg2': 'yes', 'testarg3': True} params2 = {'testarg2': 'no', 'testarg4': False} paramstarg = { 'testarg2': 'yes;no', 'testarg3': True, 'testarg4': False } epca1 = EventArray( [1.1, 1.5, 1.7] * pq.ms, labels=np.array( ['test event 1 1', 'test event 1 2', 'test event 1 3'], dtype='S'), name='test', description='tester 1', file_origin='test.file', testarg1=1, **params1) epca2 = EventArray( [2.1, 2.5, 2.7] * pq.us, labels=np.array( ['test event 2 1', 'test event 2 2', 'test event 2 3'], dtype='S'), name='test', description='tester 2', file_origin='test.file', testarg1=1, **params2) epcatarg = EventArray( [1.1, 1.5, 1.7, .0021, .0025, .0027] * pq.ms, labels=np.array([ 'test event 1 1', 'test event 1 2', 'test event 1 3', 'test event 2 1', 'test event 2 2', 'test event 2 3' ], dtype='S'), name='test', description='merge(tester 1, tester 2)', file_origin='test.file', testarg1=1, **paramstarg) assert_neo_object_is_compliant(epca1) assert_neo_object_is_compliant(epca2) assert_neo_object_is_compliant(epcatarg) epcares = epca1.merge(epca2) assert_neo_object_is_compliant(epcares) assert_same_sub_schema(epcatarg, epcares)
def test__fake_neo__cascade(self): self.annotations['seed'] = None obj_type = 'RecordingChannelGroup' cascade = True res = fake_neo(obj_type=obj_type, cascade=cascade) self.assertTrue(isinstance(res, RecordingChannelGroup)) assert_neo_object_is_compliant(res) self.assertEqual(res.annotations, self.annotations) for child in res.children_recur: del child.annotations['i'] del child.annotations['j'] self.assertEqual(len(res.recordingchannels), 1) rchan = res.recordingchannels[0] self.assertEqual(rchan.annotations, self.annotations) self.assertEqual(len(res.units), 1) unit = res.units[0] self.assertEqual(unit.annotations, self.annotations) self.assertEqual(len(res.analogsignalarrays), 1) self.assertEqual(res.analogsignalarrays[0].annotations, self.annotations) self.assertEqual(len(rchan.analogsignals), 1) self.assertEqual(len(rchan.irregularlysampledsignals), 1) self.assertEqual(rchan.analogsignals[0].annotations, self.annotations) self.assertEqual(rchan.irregularlysampledsignals[0].annotations, self.annotations) self.assertEqual(len(unit.spiketrains), 1) self.assertEqual(len(unit.spikes), 1) self.assertEqual(unit.spiketrains[0].annotations, self.annotations) self.assertEqual(unit.spikes[0].annotations, self.annotations)
def test__slice_should_return_AnalogSignalArray(self): # slice result = self.signal1[3:8, 0] self.assertIsInstance(result, AnalogSignal) assert_neo_object_is_compliant(result) self.assertEqual(result.name, 'spam') # should slicing really preserve name and description? self.assertEqual(result.description, 'eggs') # perhaps these should be modified to indicate the slice? self.assertEqual(result.file_origin, 'testfile.txt') self.assertEqual(result.annotations, {'arg1': 'test'}) self.assertEqual(result.size, 5) self.assertEqual(result.sampling_period, self.signal1.sampling_period) self.assertEqual(result.sampling_rate, self.signal1.sampling_rate) self.assertEqual(result.t_start, self.signal1.t_start+3*result.sampling_period) self.assertEqual(result.t_stop, result.t_start + 5*result.sampling_period) assert_array_equal(result.magnitude, self.data1[3:8].reshape(-1, 1)) # Test other attributes were copied over (in this case, defaults) self.assertEqual(result.file_origin, self.signal1.file_origin) self.assertEqual(result.name, self.signal1.name) self.assertEqual(result.description, self.signal1.description) self.assertEqual(result.annotations, self.signal1.annotations)
def test__pickle(self): signal1 = AnalogSignalArray(np.arange(55.0).reshape((11, 5)), units="mV", sampling_rate=1 * pq.kHz, channel_index=np.arange(5)) fobj = open('./pickle', 'wb') pickle.dump(signal1, fobj) fobj.close() fobj = open('./pickle', 'rb') try: signal2 = pickle.load(fobj) except ValueError: signal2 = None assert_arrays_equal(signal1, signal2) assert_neo_object_is_compliant(signal1) assert_neo_object_is_compliant(signal2) self.assertEqual(list(signal1.channel_indexes), [0, 1, 2, 3, 4]) self.assertEqual(list(signal1.channel_indexes), list(signal2.channel_indexes)) fobj.close() os.remove('./pickle')
def test_IrregularlySampledSignal_creation_units_rescale(self): params = {'test2': 'y1', 'test3': True} sig = IrregularlySampledSignal([1.1, 1.5, 1.7] * pq.s, signal=[2., 4., 6.] * pq.V, units=pq.mV, time_units=pq.ms, name='test', description='tester', file_origin='test.file', test1=1, **params) sig.annotate(test1=1.1, test0=[1, 2]) assert_neo_object_is_compliant(sig) assert_arrays_equal(sig.times, [1100, 1500, 1700] * pq.ms) assert_arrays_equal(np.asarray(sig), np.array([2000., 4000., 6000.])) self.assertEqual(sig.units, pq.mV) self.assertEqual(sig.name, 'test') self.assertEqual(sig.description, 'tester') self.assertEqual(sig.file_origin, 'test.file') self.assertEqual(sig.annotations['test0'], [1, 2]) self.assertEqual(sig.annotations['test1'], 1.1) self.assertEqual(sig.annotations['test2'], 'y1') self.assertTrue(sig.annotations['test3'])
def test_time_slice_none_both(self): targdataquant = [[0.0], [1.0], [2.0], [3.0], [4.0], [5.0], [6.0], [7.0], [8.0], [9.0]] * pq.mV targtime = np.logspace(1, 5, 10) targtimequant = targtime [0:10] *pq.ms targ_signal = IrregularlySampledSignal(targtimequant, signal=targdataquant, name='spam', description='eggs', file_origin='testfile.txt', arg1='test') t_start = None t_stop = None result = self.signal1.time_slice(t_start, t_stop) assert_array_equal(result, targ_signal) assert_array_equal(result.times, targtimequant) self.assertEqual(result.units, 1*pq.mV) 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'})
def test__slice_should_return_AnalogSignal(self): # slice result = self.signal1[3:8] 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.size, 5) self.assertEqual(result.sampling_period, self.signal1.sampling_period) self.assertEqual(result.sampling_rate, self.signal1.sampling_rate) self.assertEqual(result.t_start, self.signal1.t_start + 3 * result.sampling_period) self.assertEqual(result.t_stop, result.t_start + 5 * result.sampling_period) assert_arrays_equal(result, self.data1[3:8]) # Test other attributes were copied over (in this case, defaults) self.assertEqual(result.file_origin, self.signal1.file_origin) self.assertEqual(result.name, self.signal1.name) self.assertEqual(result.description, self.signal1.description) self.assertEqual(result.annotations, self.signal1.annotations)
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)
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, 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_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)
def test_Epoch_merge(self): params1 = {'test2': 'y1', 'test3': True} params2 = {'test2': 'no', 'test4': False} paramstarg = {'test2': 'yes;no', 'test3': True, 'test4': False} epc1 = Epoch( [1.1, 1.5, 1.7] * pq.ms, durations=[20, 40, 60] * pq.us, labels=np.array( ['test epoch 1 1', 'test epoch 1 2', 'test epoch 1 3'], dtype='S'), name='test', description='tester 1', file_origin='test.file', test1=1, **params1) epc2 = Epoch( [2.1, 2.5, 2.7] * pq.us, durations=[3, 5, 7] * pq.ms, labels=np.array( ['test epoch 2 1', 'test epoch 2 2', 'test epoch 2 3'], dtype='S'), name='test', description='tester 2', file_origin='test.file', test1=1, **params2) epctarg = Epoch( [1.1, 1.5, 1.7, .0021, .0025, .0027] * pq.ms, durations=[20, 40, 60, 3000, 5000, 7000] * pq.ns, labels=np.array([ 'test epoch 1 1', 'test epoch 1 2', 'test epoch 1 3', 'test epoch 2 1', 'test epoch 2 2', 'test epoch 2 3' ], dtype='S'), name='test', description='merge(tester 1, tester 2)', file_origin='test.file', test1=1, **paramstarg) assert_neo_object_is_compliant(epc1) assert_neo_object_is_compliant(epc2) assert_neo_object_is_compliant(epctarg) epcres = epc1.merge(epc2) assert_neo_object_is_compliant(epcres) assert_same_sub_schema(epctarg, epcres)
def test_EpochArray_creation(self): params = {'testarg2': 'yes', 'testarg3': True} epca = EpochArray([1.1, 1.5, 1.7]*pq.ms, durations=[20, 40, 60]*pq.ns, labels=np.array(['test epoch 1', 'test epoch 2', 'test epoch 3'], dtype='S'), name='test', description='tester', file_origin='test.file', testarg1=1, **params) epca.annotate(testarg1=1.1, testarg0=[1, 2, 3]) assert_neo_object_is_compliant(epca) assert_arrays_equal(epca.times, [1.1, 1.5, 1.7]*pq.ms) assert_arrays_equal(epca.durations, [20, 40, 60]*pq.ns) assert_arrays_equal(epca.labels, np.array(['test epoch 1', 'test epoch 2', 'test epoch 3'], dtype='S')) self.assertEqual(epca.name, 'test') self.assertEqual(epca.description, 'tester') self.assertEqual(epca.file_origin, 'test.file') self.assertEqual(epca.annotations['testarg0'], [1, 2, 3]) self.assertEqual(epca.annotations['testarg1'], 1.1) self.assertEqual(epca.annotations['testarg2'], 'yes') self.assertTrue(epca.annotations['testarg3'])
def test__time_slice(self): t_start = 2 * pq.s t_stop = 4 * pq.s 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, sampling_rate=1.0*pq.Hz, units='mV', t_start=t_start, name='spam', description='eggs', file_origin='testfile.txt', arg1='test') assert_neo_object_is_compliant(result) self.assertEqual(result.t_stop, t_stop) self.assertEqual(result.t_start, t_start) self.assertEqual(result.sampling_rate, targ.sampling_rate) assert_array_equal(result, targ) assert_same_sub_schema(result, targ)
def test_time_slice_empty(self): params = {'test2': 'y1', 'test3': True} arr_ann = {'index': np.array([]), 'test': np.array([])} evt = Event([] * pq.ms, name='test', description='tester', file_origin='test.file', test1=1, array_annotations=arr_ann, **params) evt.annotate(test1=1.1, test0=[1, 2]) result = evt.time_slice(t_start=0.0001, t_stop=30.0) assert_neo_object_is_compliant(evt) assert_arrays_equal(evt, result) self.assertEqual(evt.name, result.name) self.assertEqual(evt.description, result.description) self.assertEqual(evt.file_origin, result.file_origin) self.assertEqual(evt.annotations['test0'], result.annotations['test0']) self.assertEqual(evt.annotations['test1'], result.annotations['test1']) self.assertEqual(evt.annotations['test2'], result.annotations['test2']) assert_arrays_equal(result.array_annotations['index'], np.asarray([])) assert_arrays_equal(result.array_annotations['test'], np.asarray([])) self.assertIsInstance(result.array_annotations, ArrayDict)
def test_Epoch_merge(self): params1 = {'test2': 'y1', 'test3': True} params2 = {'test2': 'no', 'test4': False} paramstarg = {'test2': 'yes;no', 'test3': True, 'test4': False} arr_ann1 = {'index': np.arange(10, 13)} arr_ann2 = {'index': np.arange(3), 'test': ['a', 'b', 'c']} epc1 = Epoch([1.1, 1.5, 1.7] * pq.ms, durations=[20, 40, 60] * pq.us, labels=np.array(['test epoch 1 1', 'test epoch 1 2', 'test epoch 1 3'], dtype='S'), name='test', description='tester 1', file_origin='test.file', test1=1, array_annotations=arr_ann1, **params1) epc2 = Epoch([2.1, 2.5, 2.7] * pq.us, durations=[3, 5, 7] * pq.ms, labels=np.array(['test epoch 2 1', 'test epoch 2 2', 'test epoch 2 3'], dtype='S'), name='test', description='tester 2', file_origin='test.file', test1=1, array_annotations=arr_ann2, **params2) epctarg = Epoch([1.1, 1.5, 1.7, .0021, .0025, .0027] * pq.ms, durations=[20, 40, 60, 3000, 5000, 7000] * pq.us, labels=np.array(['test epoch 1 1', 'test epoch 1 2', 'test epoch 1 3', 'test epoch 2 1', 'test epoch 2 2', 'test epoch 2 3'], dtype='S'), name='test', description='merge(tester 1, tester 2)', file_origin='test.file', array_annotations={'index': [10, 11, 12, 0, 1, 2]}, test1=1, **paramstarg) assert_neo_object_is_compliant(epc1) assert_neo_object_is_compliant(epc2) assert_neo_object_is_compliant(epctarg) with warnings.catch_warnings(record=True) as w: epcres = epc1.merge(epc2) self.assertTrue(len(w), 1) self.assertEqual(w[0].category, UserWarning) self.assertSequenceEqual(str(w[0].message), "The following array annotations were " "omitted, because they were only present" " in one of the merged objects: " "[] from the one that was merged " "into and ['test'] from the one that " "was merged into the other") assert_neo_object_is_compliant(epcres) assert_same_sub_schema(epctarg, epcres) # Remove this, when array_annotations are added to assert_same_sub_schema assert_arrays_equal(epcres.array_annotations['index'], np.array([10, 11, 12, 0, 1, 2])) self.assertTrue('test' not in epcres.array_annotations) self.assertIsInstance(epcres.array_annotations, ArrayDict)
def test__slice_should_change_sampling_period(self): result1 = self.signal1[:2, 0] result2 = self.signal1[::2, 0] result3 = self.signal1[1:7:2, 0] self.assertIsInstance(result1, AnalogSignal) assert_neo_object_is_compliant(result1) self.assertEqual(result1.name, 'spam') self.assertEqual(result1.description, 'eggs') self.assertEqual(result1.file_origin, 'testfile.txt') self.assertEqual(result1.annotations, {'arg1': 'test'}) self.assertEqual(result1.array_annotations, { 'anno1': [23], 'anno2': ['A'] }) self.assertIsInstance(result1.array_annotations, ArrayDict) self.assertIsInstance(result2, AnalogSignal) assert_neo_object_is_compliant(result2) self.assertEqual(result2.name, 'spam') self.assertEqual(result2.description, 'eggs') self.assertEqual(result2.file_origin, 'testfile.txt') self.assertEqual(result2.annotations, {'arg1': 'test'}) self.assertEqual(result2.array_annotations, { 'anno1': [23], 'anno2': ['A'] }) self.assertIsInstance(result2.array_annotations, ArrayDict) self.assertIsInstance(result3, AnalogSignal) assert_neo_object_is_compliant(result3) self.assertEqual(result3.name, 'spam') self.assertEqual(result3.description, 'eggs') self.assertEqual(result3.file_origin, 'testfile.txt') self.assertEqual(result3.annotations, {'arg1': 'test'}) self.assertEqual(result3.array_annotations, { 'anno1': [23], 'anno2': ['A'] }) self.assertIsInstance(result3.array_annotations, ArrayDict) self.assertEqual(result1.sampling_period, self.signal1.sampling_period) self.assertEqual(result2.sampling_period, self.signal1.sampling_period * 2) self.assertEqual(result3.sampling_period, self.signal1.sampling_period * 2) assert_array_equal(result1.magnitude, self.data1[:2].reshape(-1, 1)) assert_array_equal(result2.magnitude, self.data1[::2].reshape(-1, 1)) assert_array_equal(result3.magnitude, self.data1[1:7:2].reshape(-1, 1))
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'}) 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 = 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)
def test__right_sweep(self): result1 = self.spike1.right_sweep self.spike1.left_sweep = None assert_neo_object_is_compliant(self.spike1) result2 = self.spike1.right_sweep self.spike1.left_sweep = self.left_sweep1 self.spike1.sampling_rate = None assert_neo_object_is_compliant(self.spike1) result3 = self.spike1.right_sweep self.spike1.sampling_rate = self.sampling_rate1 self.spike1.waveform = None assert_neo_object_is_compliant(self.spike1) result4 = self.spike1.right_sweep self.assertEqual(result1, 32.*pq.s) self.assertEqual(result1.units, 1.*pq.s) self.assertEqual(result2, None) self.assertEqual(result3, None) self.assertEqual(result4, None)
def test__merge(self): data1 = np.arange(1000.0, 1066.0).reshape((11, 6)) * pq.uV data2 = np.arange(2.0, 2.033, 0.001).reshape((11, 3)) * pq.mV times1 = np.arange(11.0) * pq.ms times2 = np.arange(1.0, 12.0) * pq.ms signal1 = IrregularlySampledSignal(times1, data1, name='signal1', description='test signal', file_origin='testfile.txt') signal2 = IrregularlySampledSignal(times1, data2, name='signal2', description='test signal', file_origin='testfile.txt') signal3 = IrregularlySampledSignal(times2, data2, name='signal3', description='test signal', file_origin='testfile.txt') merged12 = signal1.merge(signal2) target_data12 = np.hstack([data1, data2.rescale(pq.uV)]) assert_neo_object_is_compliant(signal1) assert_neo_object_is_compliant(signal2) assert_neo_object_is_compliant(merged12) self.assertAlmostEqual(merged12[5, 0], 1030.0 * pq.uV, 9) self.assertAlmostEqual(merged12[5, 6], 2015.0 * pq.uV, 9) self.assertEqual(merged12.name, 'merge(signal1, signal2)') self.assertEqual(merged12.file_origin, 'testfile.txt') assert_arrays_equal(merged12.magnitude, target_data12) self.assertRaises(MergeError, signal1.merge, signal3)
def test__add_two_consistent_signals_should_preserve_data_complement(self): data2 = np.arange(10.0, 20.0) data2quant = data2 * pq.mV signal2 = AnalogSignal(data2quant, sampling_rate=1 * pq.kHz, array_annotations={'abc': [1]}) assert_neo_object_is_compliant(signal2) result = self.signal1 + signal2 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) targ = AnalogSignal(np.arange(10.0, 30.0, 2.0), units="mV", sampling_rate=1 * pq.kHz, name='spam', description='eggs', file_origin='testfile.txt', arg1='test') assert_neo_object_is_compliant(targ) assert_array_equal(result, targ) assert_same_sub_schema(result, targ)
def test__compliant(self): assert_neo_object_is_compliant(self.signal1) self.assertEqual(self.signal1.name, 'spam') self.assertEqual(self.signal1.description, 'eggs') self.assertEqual(self.signal1.file_origin, 'testfile.txt') self.assertEqual(self.signal1.annotations, {'arg1': 'test'})
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)
def test__compliant(self): assert_neo_object_is_compliant(self.signal1)
def test__t_start_setter_None_ValueError(self): signal = self.signals[0] assert_neo_object_is_compliant(signal) self.assertRaises(ValueError, setattr, signal, 't_start', None)