Exemplo n.º 1
0
 def test_write_dataset_datachunkiterator_data_only(self):
     a = np.arange(30).reshape(5, 2, 3)
     aiter = iter(a)
     daiter = DataChunkIterator.from_iterable(aiter, buffer_size=2)
     tstamps = np.arange(5.)
     ts = TimeSeries(name='ts_name',
                     data=daiter,
                     unit='A',
                     timestamps=tstamps)
     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.assertListEqual(dset[:].tolist(), a.tolist())
Exemplo n.º 2
0
 def addContainer(self, file):
     ts = TimeSeries('test_timeseries',
                     'example_source',
                     list(range(100, 200, 10)),
                     'SIunit',
                     timestamps=list(range(10)),
                     resolution=0.1)
     self.file.add_acquisition(ts)
     mod = file.create_processing_module(
         'test_module', 'a test source for a ProcessingModule',
         'a test module')
     mod.add_container(
         Clustering("an example source for Clustering",
                    "A fake Clustering interface", [0, 1, 2, 0, 1, 2],
                    [100, 101, 102], list(range(10, 61, 10))))
Exemplo n.º 3
0
    def to_nwb(self, nwbfile: NWBFile) -> NWBFile:
        lick_timeseries = TimeSeries(
            name='licks',
            data=self.value['frame'].values,
            timestamps=self.value['timestamps'].values,
            description=('Timestamps and stimulus presentation '
                         'frame indices for lick events'),
            unit='N/A')

        # Add lick interface to nwb file, by way of a processing module:
        licks_mod = ProcessingModule('licking',
                                     'Licking behavior processing module')
        licks_mod.add_data_interface(lick_timeseries)
        nwbfile.add_processing_module(licks_mod)

        return nwbfile
Exemplo n.º 4
0
 def setUp(self):
     data = np.random.rand(160, 3)
     self.ts_multi = SpatialSeries(
         name="test_timeseries",
         data=data,
         reference_frame="lowerleft",
         starting_time=0.0,
         rate=1.0,
     )
     self.ts_single = TimeSeries(
         name="test_timeseries",
         data=data[:, 0],
         unit="m",
         starting_time=0.0,
         rate=1.0,
     )
Exemplo n.º 5
0
def get_motion_correction(expt, channel):
    fpath = os.path.join(expt.sima_path(), 'sequences.pkl')

    with open(fpath, 'rb') as f:
        aa = pickle.load(f)

    obj = aa[0]

    while True:
        if 'displacements' in obj:
            data = np.swapaxes(obj['displacements'][..., channel], 1, 2)
            return TimeSeries(name='motion_correction',
                              data=data,
                              unit='pixels',
                              rate=1 / expt.frame_period())
        obj = obj['base']
Exemplo n.º 6
0
 def test_write_dataset_custom_compress(self):
     a = H5DataIO(np.arange(30).reshape(5, 2, 3),
                  compression='gzip',
                  compression_opts=5,
                  shuffle=True,
                  fletcher32=True)
     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.compression, 'gzip')
         self.assertEqual(dset.compression_opts, 5)
         self.assertEqual(dset.shuffle, True)
         self.assertEqual(dset.fletcher32, True)
Exemplo n.º 7
0
    def setUpContainer(self):
        self.timeseries = TimeSeries(name='dummy timeseries', description='desc',
                                     data=np.ones((3, 3)), unit='flibs',
                                     timestamps=np.ones((3,)))
        bands = DynamicTable(name='bands', description='band info for LFPSpectralAnalysis', columns=[
            VectorData(name='band_name', description='name of bands', data=['alpha', 'beta', 'gamma']),
            VectorData(name='band_limits', description='low and high cutoffs in Hz', data=np.ones((3, 2)))
        ])
        spec_anal = DecompositionSeries(name='LFPSpectralAnalysis',
                                        description='my description',
                                        data=np.ones((3, 3, 3)),
                                        timestamps=np.ones((3,)),
                                        source_timeseries=self.timeseries,
                                        metric='amplitude',
                                        bands=bands)

        return spec_anal
Exemplo n.º 8
0
def add_ekg(nwbfile, ecog_path, ekg_elecs):
    if os.path.split(ecog_path)[1] == 'RawHTK':
        rate, data = readhtks(ecog_path, ekg_elecs)
    elif os.path.split(ecog_path)[1] == 'ecog.mat':
        with File(ecog_path, 'r') as f:
            data = f['ecogDS']['data'][:, ekg_elecs]
            rate = f['ecogDS']['sampFreq'][:].ravel()[0]
    elif os.path.split(ecog_path)[1] == 'raw.mat':
        rate, data = load_wavs(ecog_path, ekg_elecs)

    ekg_ts = TimeSeries('EKG',
                        H5DataIO(data, compression='gzip'),
                        unit='V',
                        rate=rate,
                        conversion=.001,
                        description='electrotorticography')
    nwbfile.add_acquisition(ekg_ts)
Exemplo n.º 9
0
    def test_init(self):
        ts = TimeSeries('test_ts',
                        'a hypothetical source',
                        list(),
                        'unit',
                        timestamps=list())

        iS = IndexSeries('test_iS',
                         'a hypothetical source',
                         list(),
                         'unit',
                         ts,
                         timestamps=list())
        self.assertEqual(iS.name, 'test_iS')
        self.assertEqual(iS.source, 'a hypothetical source')
        self.assertEqual(iS.unit, 'unit')
        self.assertEqual(iS.indexed_timeseries, ts)
Exemplo n.º 10
0
    def test_copy(self):
        self.nwbfile.add_unit(spike_times=[1., 2., 3.])
        device = self.nwbfile.create_device('a')
        elecgrp = self.nwbfile.create_electrode_group('a', 'b', device=device, location='a')
        self.nwbfile.add_electrode(np.nan, np.nan, np.nan, np.nan, 'a', 'a', elecgrp, id=0)
        self.nwbfile.add_electrode(np.nan, np.nan, np.nan, np.nan, 'b', 'b', elecgrp)
        elec_region = self.nwbfile.create_electrode_table_region([1], 'name')

        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 = ElectricalSeries('test_ts2', [0, 1, 2, 3, 4, 5],
                               electrodes=elec_region, timestamps=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5])
        self.nwbfile.add_acquisition(ts1)
        self.nwbfile.add_acquisition(ts2)
        self.nwbfile.add_trial(start_time=50.0, stop_time=70.0)
        self.nwbfile.add_invalid_times_column('comments', 'description of reason for omitting time')
        self.nwbfile.create_processing_module('test_mod', 'test_description')
        self.nwbfile.create_time_intervals('custom_interval', 'a custom time interval')
        self.nwbfile.intervals['custom_interval'].add_interval(start_time=10., stop_time=20.)
        newfile = self.nwbfile.copy()

        # test dictionaries
        self.assertIs(self.nwbfile.devices['a'], newfile.devices['a'])
        self.assertIs(self.nwbfile.acquisition['test_ts1'], newfile.acquisition['test_ts1'])
        self.assertIs(self.nwbfile.acquisition['test_ts2'], newfile.acquisition['test_ts2'])
        self.assertIs(self.nwbfile.processing['test_mod'], newfile.processing['test_mod'])

        # test dynamic tables
        self.assertIsNot(self.nwbfile.electrodes, newfile.electrodes)
        self.assertIs(self.nwbfile.electrodes['x'], newfile.electrodes['x'])
        self.assertIsNot(self.nwbfile.units, newfile.units)
        self.assertIs(self.nwbfile.units['spike_times'], newfile.units['spike_times'])
        self.assertIsNot(self.nwbfile.trials, newfile.trials)
        self.assertIsNot(self.nwbfile.trials.parent, newfile.trials.parent)
        self.assertIs(self.nwbfile.trials.id, newfile.trials.id)
        self.assertIs(self.nwbfile.trials['start_time'], newfile.trials['start_time'])
        self.assertIs(self.nwbfile.trials['stop_time'], newfile.trials['stop_time'])
        self.assertIsNot(self.nwbfile.invalid_times, newfile.invalid_times)
        self.assertTupleEqual(self.nwbfile.invalid_times.colnames, newfile.invalid_times.colnames)
        self.assertIsNot(self.nwbfile.intervals['custom_interval'], newfile.intervals['custom_interval'])
        self.assertTupleEqual(self.nwbfile.intervals['custom_interval'].colnames,
                              newfile.intervals['custom_interval'].colnames)
        self.assertIs(self.nwbfile.intervals['custom_interval']['start_time'],
                      newfile.intervals['custom_interval']['start_time'])
        self.assertIs(self.nwbfile.intervals['custom_interval']['stop_time'],
                      newfile.intervals['custom_interval']['stop_time'])
Exemplo n.º 11
0
    def setUp(self):
        type_map = get_type_map()
        self.manager = BuildManager(type_map)
        self.path = "test_pynwb_io_hdf5.h5"
        self.start_time = datetime(1970, 1, 1, 12, 0, 0)
        self.create_date = datetime(2017, 4, 15, 12, 0, 0)

        self.ts_builder = GroupBuilder('test_timeseries',
                                       attributes={'ancestry': 'TimeSeries',
                                                   'source': 'example_source',
                                                   'neurodata_type': 'TimeSeries',
                                                   'help': 'General purpose TimeSeries'},
                                       datasets={'data': DatasetBuilder('data', list(range(100, 200, 10)),
                                                                        attributes={'unit': 'SIunit',
                                                                                    'conversion': 1.0,
                                                                                    'resolution': 0.1}),
                                                 'timestamps': DatasetBuilder(
                                                     'timestamps', list(range(10)),
                                                     attributes={'unit': 'Seconds', 'interval': 1})})
        self.ts = TimeSeries('test_timeseries', 'example_source', list(range(100, 200, 10)),
                             unit='SIunit', resolution=0.1, timestamps=list(range(10)))
        self.manager.prebuilt(self.ts, self.ts_builder)
        self.builder = GroupBuilder(
            'root',
            groups={'acquisition':
                    GroupBuilder('acquisition',
                                 groups={'timeseries':
                                         GroupBuilder('timeseries',
                                                      groups={'test_timeseries': self.ts_builder}),
                                         'images': GroupBuilder('images')}),
                    'analysis': GroupBuilder('analysis'),
                    'epochs': GroupBuilder('epochs'),
                    'general': GroupBuilder('general'),
                    'processing': GroupBuilder('processing'),
                    'stimulus': GroupBuilder(
                        'stimulus',
                        groups={'presentation':
                                GroupBuilder('presentation'),
                                'templates': GroupBuilder('templates')})},
            datasets={'file_create_date': DatasetBuilder('file_create_date', [str(self.create_date)]),
                      'identifier': DatasetBuilder('identifier', 'TEST123'),
                      'session_description': DatasetBuilder('session_description', 'a test NWB File'),
                      'nwb_version': DatasetBuilder('nwb_version', '1.0.6'),
                      'session_start_time': DatasetBuilder('session_start_time', str(self.start_time))},
            attributes={'neurodata_type': 'NWBFile'})
Exemplo n.º 12
0
def lick_piezo():
    """
    Adds lick_piezo to acquisition.
    Needs data from lickPiezo.raw.npy and lickPiezo.timestamps.npy.
    """
    lp_raw = read_npy_file('lickPiezo.raw.npy')
    lp_timestamps = read_npy_file('lickPiezo.timestamps.npy')
    lp_rate = get_rate(lp_timestamps)
    lick_piezo_ts = TimeSeries(
        name='lickPiezo',
        starting_time=lp_timestamps[0, 1],
        rate=lp_rate,
        data=np.ravel(lp_raw),
        unit='V',
        description='Voltage values from a thin-film piezo connected to the '
        'lick spout, so that values are proportional to deflection '
        'of the spout and licks can be detected as peaks of the signal.')
    nwb_file.add_acquisition(lick_piezo_ts)
Exemplo n.º 13
0
def sparse_noise():
    """
    Adds receptive field mapping task to nwb_file.stimulus.
    Needs data from sparseNoise.positions.npy and sparseNoise.times.npy.
    """
    sparse_noise_pos = read_npy_file('sparseNoise.positions.npy')
    sparse_noise_time = read_npy_file('sparseNoise.times.npy')

    sp_noise = TimeSeries(
        name='receptive_field_mapping_sparse_noise',
        timestamps=np.ravel(sparse_noise_time),
        data=sparse_noise_pos,
        unit='degrees visual angle',
        description='White squares shown on the screen with randomized '
        'positions and timing - see manuscript Methods.',
        comments='The altitude (first column) and azimuth (second column) '
        'of the square.')
    nwb_file.add_stimulus(sp_noise)
Exemplo n.º 14
0
 def test_get_neurodata_type(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)
     p1 = ts1.get_ancestor(neurodata_type='NWBFile')
     self.assertIs(p1, self.nwbfile)
     p2 = ts2.get_ancestor(neurodata_type='NWBFile')
     self.assertIs(p2, self.nwbfile)
Exemplo n.º 15
0
    def test_constructor(self):
        """Test that the constructors."""

        eye_tracking = EllipseSeries(name='eye_tracking',
                                     data=np.ones((100, 2)),
                                     reference_frame='nose',
                                     area=np.ones((100, )),
                                     width=np.ones((100, )),
                                     height=np.ones((100, )),
                                     angle=np.ones((100, )),
                                     timestamps=np.arange(100) / 20)

        pupil_tracking = EllipseSeries(name='pupil_tracking',
                                       data=np.ones((100, 2)),
                                       reference_frame='nose',
                                       area=np.ones((100, )),
                                       width=np.ones((100, )),
                                       height=np.ones((100, )),
                                       angle=np.ones((100, )),
                                       timestamps=eye_tracking)

        corneal_reflection_tracking = EllipseSeries(
            name='corneal_reflection_tracking',
            data=np.ones((100, 2)),
            reference_frame='nose',
            area=np.ones((100, )),
            width=np.ones((100, )),
            height=np.ones((100, )),
            angle=np.ones((100, )),
            timestamps=eye_tracking)

        likely_blink = TimeSeries(timestamps=[1.0, 5.0, 6.0],
                                  data=[1, 1, 1],
                                  name='likely_blink',
                                  description='blinks',
                                  unit='N/A')

        ellipse_eye_tracking = EllipseEyeTracking(
            eye_tracking=eye_tracking,
            pupil_tracking=pupil_tracking,
            corneal_reflection_tracking=corneal_reflection_tracking,
            likely_blink=likely_blink)

        return ellipse_eye_tracking
Exemplo n.º 16
0
    def setUp(self):
        self.start_time = datetime(1971, 1, 1, 12, tzinfo=tzutc())

        self.data = np.arange(2000).reshape((2, 1000))
        self.timestamps = np.linspace(0, 1, 1000)

        self.container = TimeSeries(
            name='data_ts',
            unit='V',
            data=self.data,
            timestamps=self.timestamps
        )

        self.data_filename = os.path.join(os.getcwd(), 'test_time_series_modular_data.nwb')
        self.link_filename = os.path.join(os.getcwd(), 'test_time_series_modular_link.nwb')

        self.read_container = None
        self.link_read_io = None
        self.data_read_io = None
Exemplo n.º 17
0
    def roundtripContainer(self):
        # create and write data file
        data_file = NWBFile(
            session_description='a test file',
            identifier='data_file',
            session_start_time=self.start_time
        )
        data_file.add_acquisition(self.container)

        with HDF5IO(self.data_filename, 'w', manager=get_manager()) as data_write_io:
            data_write_io.write(data_file)

        # read data file
        with HDF5IO(self.data_filename, 'r', manager=get_manager()) as self.data_read_io:
            data_file_obt = self.data_read_io.read()

            # write "link file" with timeseries.data that is an external link to the timeseries in "data file"
            # also link timeseries.timestamps.data to the timeseries.timestamps in "data file"
            with HDF5IO(self.link_filename, 'w', manager=get_manager()) as link_write_io:
                link_file = NWBFile(
                    session_description='a test file',
                    identifier='link_file',
                    session_start_time=self.start_time
                )
                self.link_container = TimeSeries(
                    name='test_mod_ts',
                    unit='V',
                    data=data_file_obt.get_acquisition('data_ts'),  # test direct link
                    timestamps=H5DataIO(
                        data=data_file_obt.get_acquisition('data_ts').timestamps,
                        link_data=True  # test with setting link data
                    )
                )
                link_file.add_acquisition(self.link_container)
                link_write_io.write(link_file)

        # note that self.link_container contains a link to a dataset that is now closed

        # read the link file
        self.link_read_io = HDF5IO(self.link_filename, 'r', manager=get_manager())
        self.read_nwbfile = self.link_read_io.read()
        return self.getContainer(self.read_nwbfile)
Exemplo n.º 18
0
 def test_init(self):
     tstamps = np.arange(1.0, 100.0, 0.1, dtype=np.float)
     ts = TimeSeries("test_ts",
                     "a hypothetical source",
                     list(range(len(tstamps))),
                     'unit',
                     timestamps=tstamps)
     tsi = TimeSeriesIndex()
     tsi.add_row(40, 105, ts)
     ept = EpochTable()
     self.assertEqual(ept.name, 'epochs')
     ept.add_row(10.0, 20.0, "test,unittest,pynwb",
                 ListSlicer(tsi.data, slice(0, 1)), 'a test epoch')
     row = ept[0]
     self.assertEqual(row[0], 10.0)
     self.assertEqual(row[1], 20.0)
     self.assertEqual(row[2], "test,unittest,pynwb")
     self.assertEqual(row[3].data, tsi.data)
     self.assertEqual(row[3].region, slice(0, 1))
     self.assertEqual(row[4], 'a test epoch')
Exemplo n.º 19
0
    def run_conversion(self,
                       nwbfile: NWBFile,
                       metadata: dict = None,
                       stub_test: bool = False):
        conditions = intervals_from_traces(self.recording_extractor)
        mech_stim = TimeIntervals(
            name='MechanicalStimulus',
            description=
            "Activation times inferred from TTL commands for mechanical stimulus."
        )
        laser_stim = TimeIntervals(
            name='LaserStimulus',
            description=
            "Activation times inferred from TTL commands for cortical laser stimulus."
        )
        for j, table in enumerate([mech_stim, laser_stim]):
            for row in conditions[j]:
                table.add_row(
                    dict(start_time=float(row[0]), stop_time=float(row[1])))
        # TODO - these really should be IntervalSeries added to stimulus, rather than processing
        check_module(nwbfile, 'stimulus',
                     "Contains stimuli data.").add(mech_stim)
        check_module(nwbfile, 'stimulus',
                     "Contains stimuli data.").add(laser_stim)

        if stub_test or self.subset_channels is not None:
            recording = self.subset_recording(stub_test=stub_test)
        else:
            recording = self.recording_extractor

        # Pressure values
        nwbfile.add_stimulus(
            TimeSeries(
                name='MechanicalPressure',
                data=H5DataIO(recording.get_traces(0), compression="gzip"),
                unit=self.recording_extractor._channel_smrxinfo[0]['unit'],
                conversion=recording.get_channel_property(0, 'gain'),
                rate=recording.get_sampling_frequency(),
                description=
                "Pressure sensor attached to the mechanical stimulus used to repeatedly evoke spiking."
            ))
Exemplo n.º 20
0
def align_by_trials(
    timeseries: TimeSeries,
    start_label="start_time",
    before=0.0,
    after=1.0,
):
    """
    Args:
        timeseries: TimeSeries
        start_label: str
            default: 'start_time'
        before: float
            time after start_label in secs (positive goes back in time)
        after: float
            time after stop_label in secs (positive goes forward in time)
    Returns:
        np.array(shape=(n_trials, n_time, ...))
    """
    trials = timeseries.get_ancestor("NWBFile").trials
    return align_by_time_intervals(timeseries, trials, start_label, before,
                                   after)
Exemplo n.º 21
0
 def test_write_dataset_datachunkiterator_with_compression(self):
     a = np.arange(30).reshape(5, 2, 3)
     aiter = iter(a)
     daiter = DataChunkIterator.from_iterable(aiter, buffer_size=2)
     wrapped_daiter = H5DataIO(data=daiter,
                               compression='gzip',
                               compression_opts=5,
                               shuffle=True,
                               fletcher32=True)
     ts = TimeSeries(name='ts_name', data=wrapped_daiter, unit='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.assertEqual(dset.shape, a.shape)
         self.assertListEqual(dset[:].tolist(), a.tolist())
         self.assertEqual(dset.compression, 'gzip')
         self.assertEqual(dset.compression_opts, 5)
         self.assertEqual(dset.shuffle, True)
         self.assertEqual(dset.fletcher32, True)
Exemplo n.º 22
0
 def setUpContainer(self):
     container = NWBFile('a test source',
                         'a test NWB File',
                         'TEST123',
                         self.start_time,
                         file_create_date=self.create_date)
     ts = TimeSeries('test_timeseries',
                     'example_source',
                     list(range(100, 200, 10)),
                     'SIunit',
                     timestamps=list(range(10)),
                     resolution=0.1)
     container.add_acquisition(ts)
     mod = container.create_processing_module(
         'test_module', 'a test source for a ProcessingModule',
         'a test module')
     mod.add_container(
         Clustering("an example source for Clustering",
                    "A fake Clustering interface", [0, 1, 2, 0, 1, 2],
                    [100, 101, 102], list(range(10, 61, 10))))
     return container
Exemplo n.º 23
0
def face_nwb():
    """
    Adds Face Energy BehavioralTimeSeries to behavior processing module.
    Needs data from face.motionEnergy.npy and face.timestamps.npy.
    """
    face_motion_energy = read_npy_file('face.motionEnergy.npy')
    face_timestamps = read_npy_file('face.timestamps.npy')
    face_rate = get_rate(face_timestamps)
    face_energy = TimeSeries(
        name='face_motion_energy',
        data=np.ravel(face_motion_energy),
        unit='arb. unit',
        starting_time=face_timestamps[0, 1],
        rate=face_rate,
        description='Features extracted from the video of the frontal aspect of '
        'the subject, including the subject\'s face and forearms.',
        comments='The integrated motion energy across the whole frame, i.e. '
        'sum( (thisFrame-lastFrame)^2 ). Some smoothing is applied '
        'before this operation.')
    face_interface = BehavioralTimeSeries(face_energy)
    behavior_module.add_data_interface(face_interface)
Exemplo n.º 24
0
    def _test_scratch_container(self, validate=True, **kwargs):
        data = TimeSeries('test_ts', [1, 2, 3, 4, 5],
                          unit='unit',
                          timestamps=[1.1, 1.2, 1.3, 1.4, 1.5])
        nwbfile = NWBFile('test',
                          'test',
                          self.start_time,
                          file_create_date=self.create_date)

        nwbfile.add_scratch(data, **kwargs)

        self.writer = NWBHDF5IO(self.filename, mode='w')
        self.writer.write(nwbfile)
        self.writer.close()

        self.reader = NWBHDF5IO(self.filename, mode='r')
        self.read_nwbfile = self.reader.read()
        ret = self.read_nwbfile.get_scratch('test_ts')

        self.assertContainerEqual(data, ret)
        if validate:
            self.validate()
Exemplo n.º 25
0
 def setUpContainer(self):
     container = NWBFile(
         'a test source',
         'a test NWB File',
         'TEST123',
         self.start_time,
         file_create_date=self.create_date,
         experimenter='test experimenter',
         stimulus_notes='test stimulus notes',
         experiment_description='test experiment description',
         data_collection='test data collection notes',
         institution='nomad',
         lab='nolab',
         notes='nonotes',
         pharmacology='nopharmacology',
         protocol='noprotocol',
         related_publications='nopubs',
         session_id='007',
         slices='noslices',
         source_script='nosources',
         surgery='nosurgery',
         virus='novirus',
         source_script_file_name='nofilename')
     self.ts = TimeSeries('test_timeseries',
                          'example_source',
                          list(range(100, 200, 10)),
                          'SIunit',
                          timestamps=list(range(10)),
                          resolution=0.1)
     container.add_acquisition(self.ts)
     self.mod = container.create_processing_module(
         'test_module', 'a test source for a ProcessingModule',
         'a test module')
     self.clustering = Clustering("an example source for Clustering",
                                  "A fake Clustering interface",
                                  [0, 1, 2, 0, 1, 2], [100, 101, 102],
                                  list(range(10, 61, 10)))
     self.mod.add_container(self.clustering)
     return container
Exemplo n.º 26
0
def ncs_to_timeseries(ncs, data_time_len, downsample=4):
    rate = float(ncs['sampling_rate'])

    # Use AcqEntName instead of channel_number to infer channel
    ch = ncs['channel_number']
    # ch = int(ncs['header']['AcqEntName'][3:])
    n_samples = int(data_time_len * rate)
    if downsample:
        data = ncs['data'][:n_samples:downsample]
        rate = rate / downsample
    else:
        data = ncs['data']

    # ch_ts = TimeSeries(name='channel_{}'.format(ch),rate =rate, data=data.astype(np.float16),conversion=1.0/10**6,unit='V')
    ts_kwargs = {
        'name': 'channel_{}'.format(ch + 1),
        'rate': rate,
        'data': data.astype(np.float16),
        'conversion': 1.0 / 10**6,
        'comments': ncs['header']['AcqEntName']
    }

    return TimeSeries(**ts_kwargs)
Exemplo n.º 27
0
    def to_nwb(self, nwbfile: NWBFile) -> NWBFile:

        # If there is no lick data, do not write
        # anything to the NWB file (this is
        # expected for passive sessions)
        if len(self.value['frame']) == 0:
            return nwbfile

        lick_timeseries = TimeSeries(
            name='licks',
            data=self.value['frame'].values,
            timestamps=self.value['timestamps'].values,
            description=('Timestamps and stimulus presentation '
                         'frame indices for lick events'),
            unit='N/A')

        # Add lick interface to nwb file, by way of a processing module:
        licks_mod = ProcessingModule('licking',
                                     'Licking behavior processing module')
        licks_mod.add_data_interface(lick_timeseries)
        nwbfile.add_processing_module(licks_mod)

        return nwbfile
Exemplo n.º 28
0
Arquivo: nwb.py Projeto: elijahc/emu
def ncs_to_timeseries(ncs, data_time_len=None, fs=1000, dtype=np.float16):
    rate = float(ncs['sampling_rate'])
    n_samples = None

    # Use AcqEntName instead of channel_number to infer channel
    ch = ncs['channel_number']
    # ch = int(ncs['header']['AcqEntName'][3:])
    if fs:
        downsample = int(rate / fs)
        rate = fs
    else:
        downsample = 1

    if data_time_len:
        n_samples = int(data_time_len * rate)
        data = ncs['data'][:n_samples:downsample]
    else:
        data = ncs['data']

    # ch_ts = TimeSeries(name='channel_{}'.format(ch),rate =rate, data=data.astype(np.float16),conversion=1.0/10**6,unit='V')
    ts_kwargs = {
        'name':
        os.path.split(ncs['file_path'])[1],
        'rate':
        float(rate),
        'data':
        data.astype(dtype),
        'starting_time':
        ncs['timestamp'][0] / 1000 / 1000,
        'comments':
        '{} originally from file {}'.format(ncs['header']['AcqEntName'],
                                            ncs['file_path']),
        'unit':
        'uV',
    }

    return TimeSeries(**ts_kwargs)
Exemplo n.º 29
0
    def run(self):
        ts_file, dat_file = self.input()[0]
        print(ts_file, ts_file.path)
        start_time = np.load(ts_file.path)[0]
        self.default_ts = self.make_timeseries(ts_file, dat_file, 1)
        print(self.default_ts.timestamps)

        nwbfile = NWBFile(
            session_description='pt1 Neuralynx Data',
            identifier='pt1',
            session_start_time=datetime.fromtimestamp(start_time / 1000 /
                                                      1000),
        )

        nwbfile.add_acquisition(self.default_ts)
        i = 2
        for ts_out, dat_out in tqdm(self.input(),
                                    total=len(self.channels),
                                    desc='Creating Channels'):
            nts = np.load(ts_out.path) / 1000 / 1000
            if np.all(nts == self.default_ts.timestamps):
                nts = self.default_ts

            dat = np.load(dat_out.path)
            ch_ts = TimeSeries(
                name='channel_{}'.format(i),
                timestamps=nts,
                data=dat,
                unit='uV',
            )
            nwbfile.add_acquisition(ch_ts)
            i += 1

        out_fp = os.path.join(self.save_path(), 'data.nwb')
        with NWBHDF5IO(out_fp, 'w') as io:
            io.write(nwbfile)
Exemplo n.º 30
0
create_date = datetime(2017, 4, 15, 12, tzinfo=tzlocal())
data = np.arange(1000).reshape((100, 10))
timestamps = np.arange(100)
filename1 = 'external1_example.nwb'
filename2 = 'external2_example.nwb'
filename3 = 'external_linkcontainer_example.nwb'
filename4 = 'external_linkdataset_example.nwb'

# Create the first file
nwbfile1 = NWBFile(session_description='demonstrate external files',
                   identifier='NWBE1',
                   session_start_time=start_time,
                   file_create_date=create_date)
# Create the second file
test_ts1 = TimeSeries(name='test_timeseries1',
                      data=data,
                      unit='SIunit',
                      timestamps=timestamps)
nwbfile1.add_acquisition(test_ts1)
# Write the first file
io = NWBHDF5IO(filename1, 'w')
io.write(nwbfile1)
io.close()

# Create the second file
nwbfile2 = NWBFile(session_description='demonstrate external files',
                   identifier='NWBE2',
                   session_start_time=start_time,
                   file_create_date=create_date)
# Create the second file
test_ts2 = TimeSeries(name='test_timeseries2',
                      data=data,