def setUp(self): data = np.arange(100, 200, 10) timestamps = list(range(1, 4)) + list(range(7, 10)) + list( range(17, 21)) self.ts_rate = TimeSeries( name="test_timeseries_rate", data=data, unit="m", starting_time=0.0, rate=1.0, ) self.ts_timestamps = TimeSeries( name="test_timeseries_timestamps", data=data, unit="m", timestamps=timestamps, )
def make_timeseries(self, ts_file, dat_file, ch): timestamps = np.load(ts_file.path) data = np.load(dat_file.path) ch_ts = TimeSeries(name='channel_{}'.format(ch), timestamps=timestamps / 1000 / 1000, data=data, unit='uV') return ch_ts
def test_show_text_fields(): data = np.random.rand(160, 3) ts = TimeSeries(name="test_timeseries", data=data, unit="m", starting_time=0.0, rate=1.0) assert isinstance(show_text_fields(ts), widgets.Widget)
def test_add_scratch_container_description(self): data = TimeSeries(name='test_ts', data=[1, 2, 3, 4, 5], unit='unit', timestamps=[1.1, 1.2, 1.3, 1.4, 1.5]) msg = ('The description argument is ignored when adding an NWBContainer, ScratchData, or ' 'DynamicTable to scratch.') with self.assertWarnsWith(UserWarning, msg): self.nwbfile.add_scratch(data, description='test scratch') self.assertIs(self.nwbfile.get_scratch('test_ts'), data) self.assertIs(self.nwbfile.scratch['test_ts'], data)
def test_init_no_parent(self): ts = TimeSeries('test_ts', 'a hypothetical source', list(), 'unit', timestamps=list()) self.assertEqual(ts.name, 'test_ts') self.assertIsNone(ts.parent)
def test_plot_traces_fix(self): ts = TimeSeries(name='test_timeseries', data=self.data.T, unit='m', starting_time=0.0, rate=20.0) plot_traces(ts)
def addContainer(self, file): ts = TimeSeries('test_timeseries', np.arange(100, 200, 10).astype(np.double), 'SIunit', timestamps=np.arange(10, dtype=float), resolution=0.1) file.add_acquisition(ts) return ts
def test_init_timestampslink_set(self): ts = TimeSeries('test_ts', 'a hypothetical source', list(), 'unit', timestamps=list()) self.assertIsInstance(ts.timestamp_link, set) self.assertEqual(len(ts.timestamp_link), 0)
def test_init(self): ts = TimeSeries('test_ts', np.ones((2, 2)), 'unit', timestamps=[1., 2., 3.]) pt = PupilTracking(ts) self.assertEqual(pt.time_series['test_ts'], ts)
def test_init(self): ts = TimeSeries('test_ts', np.ones((2, 2)), 'unit', timestamps=[1., 2., 3.]) bts = BehavioralTimeSeries(ts) self.assertEqual(bts.time_series['test_ts'], ts)
def test_add_acquisition_invalid_name(self): self.nwbfile.add_acquisition( TimeSeries('test_ts', [0, 1, 2, 3, 4, 5], 'grams', timestamps=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5])) msg = "\"'TEST_TS' not found in acquisition of NWBFile 'root'.\"" with self.assertRaisesWith(KeyError, msg): self.nwbfile.get_acquisition("TEST_TS")
def test_init_no_parent2(self): # noqa: F811 parent = NWBContainer('test_parent_container') ts = TimeSeries('test_ts', list(), 'unit', timestamps=list(), parent=parent) self.assertEqual(ts.name, 'test_ts') self.assertIs(ts.parent, parent)
def setUp(self): data = np.random.rand(160, 3) self.ts = TimeSeries( name="name", description="no description", data=data, starting_time=0.0, rate=100.0, unit="m", ) self.ts_single = TimeSeries( name="test_timeseries", data=data[:, 0], unit="m", starting_time=0.0, rate=100.0, ) self.tt = get_timeseries_tt(self.ts)
def test_plot_traces_fix(self): ts = TimeSeries( name="test_timeseries", data=self.data.T, unit="m", starting_time=0.0, rate=20.0, ) plot_traces(ts)
def addContainer(self, file): ts = TimeSeries('test_timeseries', 'example_source', list(range(100, 200, 10)), 'SIunit', timestamps=list(range(10)), resolution=0.1) file.add_acquisition(ts) return ts
def test_plot_traces(self): ts = TimeSeries(name='test_timeseries', data=self.data, unit='m', starting_time=0.0, rate=20.0) plot_traces(ts, self.time_start, self.time_duration, self.trace_window, self.title, self.ylabel)
def test_print_file(self): nwbfile = NWBFile(session_description='session_description', identifier='identifier', session_start_time=datetime.now(tzlocal())) ts = TimeSeries('name', [1., 2., 3.] * 1000, timestamps=[1, 2, 3]) ts2 = TimeSeries('name2', [1, 2, 3] * 1000, timestamps=[1, 2, 3]) self.assertEqual( str(ts), """ name <class 'pynwb.base.TimeSeries'> Fields: comments: no comments conversion: 1.0 data: [1. 2. 3. ... 1. 2. 3.] description: no description interval: 1 num_samples: 3000 resolution: 0.0 timestamps: [1 2 3] timestamps_unit: Seconds """) nwbfile.add_acquisition(ts) nwbfile.add_acquisition(ts2) nwbfile.add_epoch(start_time=1.0, stop_time=10.0, tags=['tag1', 'tag2']) self.assertEqual( str(nwbfile), """ root <class 'pynwb.file.NWBFile'> Fields: acquisition: { name <class 'pynwb.base.TimeSeries'>, name2 <class 'pynwb.base.TimeSeries'> } analysis: { } devices: { } electrode_groups: { } epoch_tags: {'tag1', 'tag2'} epochs: epochs <class 'pynwb.epoch.TimeIntervals'> ic_electrodes: { } imaging_planes: { } lab_meta_data: { } modules: { } ogen_sites: { } stimulus: { } stimulus_template: { } time_intervals: { } """)
def to_nwb(self, nwbfile: NWBFile) -> NWBFile: ophys_module = nwbfile.processing['ophys'] ophys_timestamps = ophys_module.get_data_interface( 'dff').roi_response_series['traces'].timestamps t1 = TimeSeries(name='ophys_motion_correction_x', data=self.value['x'].values, timestamps=ophys_timestamps, unit='pixels') t2 = TimeSeries(name='ophys_motion_correction_y', data=self.value['y'].values, timestamps=ophys_timestamps, unit='pixels') ophys_module.add_data_interface(t1) ophys_module.add_data_interface(t2) return nwbfile
def test_init_data(self): dat = [0, 1, 2, 3, 4] ts = TimeSeries('test_ts', dat, 'Volts', timestamps=[0.1, 0.2, 0.3, 0.4]) self.assertIs(ts.data, dat) self.assertEqual(ts.conversion, 1.0) self.assertEqual(ts.resolution, 0.0) self.assertEqual(ts.unit, 'Volts')
def test_get_timeseries_tt_infstarting_time(): data = list(range(100, 200, 10)) ts = TimeSeries(name='test_timeseries', data=data, unit='m', starting_time=np.inf, rate=1.0) tt = get_timeseries_tt(ts) np.testing.assert_array_equal(tt, [0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
def test_get_timeseries_tt_negativeistop(): data = list(range(100, 200, 10)) ts = TimeSeries(name='test_timeseries', data=data, unit='m', starting_time=0., rate=1.0) tt = get_timeseries_tt(ts, istop=-1) np.testing.assert_array_equal(tt, [0., 1., 2., 3., 4., 5., 6., 7.])
def test_print_file(self): nwbfile = NWBFile(session_description='session_description', identifier='identifier', session_start_time=datetime.now(tzlocal())) ts = TimeSeries('name', [1., 2., 3.] * 1000, timestamps=[1, 2, 3]) ts2 = TimeSeries('name2', [1, 2, 3] * 1000, timestamps=[1, 2, 3]) self.assertEqual( str(ts), """ name <class 'pynwb.base.TimeSeries'> Fields: comments: no comments conversion: 1.0 data: [1. 2. 3. ... 1. 2. 3.] description: no description interval: 1 resolution: -1.0 timestamps: [1 2 3] timestamps_unit: seconds """) nwbfile.add_acquisition(ts) nwbfile.add_acquisition(ts2) nwbfile.add_epoch(start_time=1.0, stop_time=10.0, tags=['tag1', 'tag2']) self.assertRegex( str(nwbfile), r""" root <class 'pynwb\.file\.NWBFile'> Fields: acquisition: { name <class 'pynwb\.base\.TimeSeries'>, name2 <class 'pynwb\.base\.TimeSeries'> } epoch_tags: { tag1, tag2 } epochs: epochs <class 'pynwb.epoch.TimeIntervals'> file_create_date: \[datetime.datetime\(.*\)\] identifier: identifier session_description: session_description session_start_time: .* timestamps_reference_time: .* """)
def test_epoch_tags(self): tags1 = ['t1', 't2'] tags2 = ['t3', 't4'] tstamps = np.arange(1.0, 100.0, 0.1, dtype=np.float) ts = TimeSeries("test_ts", list(range(len(tstamps))), 'unit', timestamps=tstamps) expected_tags = tags1 + tags2 self.nwbfile.add_epoch(0.0, 1.0, tags1, ts) self.nwbfile.add_epoch(0.0, 1.0, tags2, ts) tags = self.nwbfile.epoch_tags self.assertEqual(set(expected_tags), set(tags))
def setUpContainer(self): """ Return the test TimeSeries to read/write """ return TimeSeries( name='test_timeseries', data=list(range(1000)), unit='SIunit', timestamps=np.arange(1000.), resolution=0.1, continuity='continuous', )
def test_show_spectrogram(): data = np.random.rand(160, 12) ts = TimeSeries(name='test_timeseries', data=data, unit='m', starting_time=0.0, rate=1.0) channel = 3 show_spectrogram(ts, channel=channel)
def test_write_dataset_custom_fillvalue(self): a = H5DataIO(np.arange(20).reshape(5, 4), fillvalue=-1) ts = TimeSeries('ts_name', a, 'A', timestamps=np.arange(5)) self.nwbfile.add_acquisition(ts) with NWBHDF5IO(self.path, 'w') as io: io.write(self.nwbfile, cache_spec=False) with File(self.path, 'r') as f: dset = f['/acquisition/ts_name/data'] self.assertTrue(np.all(dset[:] == a.data)) self.assertEqual(dset.fillvalue, -1)
def addContainer(self, nwbfile): """ Add the test epochs with TimeSeries objects to the given NWBFile """ tsa, tsb = [ TimeSeries(name='a', data=np.arange(11), unit='flubs', timestamps=np.linspace(0, 1, 11)), TimeSeries(name='b', data=np.arange(13), unit='flubs', timestamps=np.linspace(0.1, 5, 13)), ] nwbfile.add_acquisition(tsa) nwbfile.add_acquisition(tsb) nwbfile.epochs = TimeIntervals.from_dataframe(pd.DataFrame({ 'foo': [1, 2, 3, 4], 'bar': ['fish', 'fowl', 'dog', 'cat'], 'start_time': [0.2, 0.25, 0.30, 0.35], 'stop_time': [0.25, 0.30, 0.40, 0.45], 'timeseries': [[(2, 1, tsa)], [(3, 1, tsa)], [(3, 1, tsa)], [(4, 1, tsa)]], 'tags': [[''], [''], ['fizz', 'buzz'], ['qaz']] }), 'epochs', columns=[ { 'name': 'foo', 'description': 'a column of integers' }, { 'name': 'bar', 'description': 'a column of strings' }, ]) # reset the thing self.container = nwbfile.epochs
def test_write_dataset_custom_chunks(self): a = H5DataIO(np.arange(30).reshape(5, 2, 3), chunks=(1, 1, 3)) ts = TimeSeries('ts_name', a, 'A', timestamps=np.arange(5)) self.nwbfile.add_acquisition(ts) with NWBHDF5IO(self.path, 'w') as io: io.write(self.nwbfile) infile = File(self.path, 'r') dset = infile['/acquisition/ts_name/data'] self.assertTrue(np.all(dset[:] == a.data)) self.assertEqual(dset.chunks, (1, 1, 3))
def test_all_children(self): ts1 = TimeSeries('test_ts1', [0, 1, 2, 3, 4, 5], 'grams', timestamps=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5]) ts2 = TimeSeries('test_ts2', [0, 1, 2, 3, 4, 5], 'grams', timestamps=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5]) self.nwbfile.add_acquisition(ts1) self.nwbfile.add_acquisition(ts2) name = 'example_electrode_group' desc = 'An example electrode' loc = 'an example location' device = self.nwbfile.create_device('a fake device') elecgrp = self.nwbfile.create_electrode_group(name, desc, loc, device) children = self.nwbfile.all_children() self.assertIn(ts1, children) self.assertIn(ts2, children) self.assertIn(device, children) self.assertIn(elecgrp, children)
def test_init(self): ts = TimeSeries('test_ts', 'a hypothetical source', list(), 'unit', timestamps=list()) pt = PupilTracking('test_pt', ts) self.assertEqual(pt.source, 'test_pt') self.assertEqual(pt.time_series, ts)