def test_append(self): proc_mod = self.nwbfile.create_processing_module(name='test_proc_mod', description='') proc_inter = LFP(name='test_proc_dset') proc_mod.add(proc_inter) device = self.nwbfile.create_device(name='test_device') e_group = self.nwbfile.create_electrode_group( name='test_electrode_group', description='', location='', device=device) self.nwbfile.add_electrode(x=0.0, y=0.0, z=0.0, imp=np.nan, location='', filtering='', group=e_group) electrodes = self.nwbfile.create_electrode_table_region(region=[0], description='') e_series = ElectricalSeries( name='test_es', electrodes=electrodes, data=np.ones(shape=(100, )), rate=10000.0, ) proc_inter.add_electrical_series(e_series) with NWBHDF5IO(self.path, mode='w') as io: io.write(self.nwbfile, cache_spec=False) with NWBHDF5IO(self.path, mode='a') as io: nwb = io.read() link_electrodes = nwb.processing['test_proc_mod'][ 'LFP'].electrical_series['test_es'].electrodes ts2 = ElectricalSeries(name='timeseries2', data=[4., 5., 6.], rate=1.0, electrodes=link_electrodes) nwb.add_acquisition(ts2) io.write(nwb) # also attempt to write same spec again self.assertIs( nwb.processing['test_proc_mod'] ['LFP'].electrical_series['test_es'].electrodes, nwb.acquisition['timeseries2'].electrodes) with NWBHDF5IO(self.path, mode='r') as io: nwb = io.read() np.testing.assert_equal(nwb.acquisition['timeseries2'].data[:], ts2.data) self.assertIs( nwb.processing['test_proc_mod'] ['LFP'].electrical_series['test_es'].electrodes, nwb.acquisition['timeseries2'].electrodes) errors = validate(io) for e in errors: print('ERROR', e)
def setUpElectricalSeriesContainers(self): TestElectricalSeriesIO.make_electrode_table(self) region1 = DynamicTableRegion('electrodes', [0, 2], 'the first and third electrodes', self.table) region2 = DynamicTableRegion('electrodes', [1, 3], 'the second and fourth electrodes', self.table) data1 = list(zip(range(10), range(10, 20))) data2 = list(zip(reversed(range(10)), reversed(range(10, 20)))) timestamps = list(map(lambda x: x/10, range(10))) es1 = ElectricalSeries('test_eS1', 'a hypothetical source', data1, region1, timestamps=timestamps) es2 = ElectricalSeries('test_eS2', 'a hypothetical source', data2, region2, timestamps=timestamps) return (es1, es2)
def test_append(self): FILENAME = 'test_append.nwb' nwb = NWBFile(session_description='hi', identifier='hi', session_start_time=datetime(1970, 1, 1, 12, tzinfo=tzutc())) proc_mod = nwb.create_processing_module(name='test_proc_mod', description='') proc_inter = LFP(name='test_proc_dset') proc_mod.add_data_interface(proc_inter) device = nwb.create_device(name='test_device') e_group = nwb.create_electrode_group(name='test_electrode_group', description='', location='', device=device) nwb.add_electrode(x=0.0, y=0.0, z=0.0, imp=np.nan, location='', filtering='', group=e_group) electrodes = nwb.create_electrode_table_region(region=[0], description='') e_series = ElectricalSeries( name='test_device', electrodes=electrodes, data=np.ones(shape=(100, )), rate=10000.0, ) proc_inter.add_electrical_series(e_series) with NWBHDF5IO(FILENAME, mode='w') as io: io.write(nwb) with NWBHDF5IO(FILENAME, mode='a') as io: nwb = io.read() elec = nwb.modules['test_proc_mod']['LFP'].electrical_series[ 'test_device'].electrodes ts2 = ElectricalSeries(name='timeseries2', data=[4, 5, 6], rate=1.0, electrodes=elec) nwb.add_acquisition(ts2) io.write(nwb) with NWBHDF5IO(FILENAME, mode='r') as io: nwb = io.read() np.testing.assert_equal(nwb.acquisition['timeseries2'].data[:], ts2.data)
def setUpTwoElectricalSeries(self): """ Return two test ElectricalSeries to read/write """ TestElectricalSeriesIO.make_electrode_table(self) region1 = DynamicTableRegion('electrodes', [0, 2], 'the first and third electrodes', self.table) region2 = DynamicTableRegion('electrodes', [1, 3], 'the second and fourth electrodes', self.table) data1 = list(zip(range(10), range(10, 20))) data2 = list(zip(reversed(range(10)), reversed(range(10, 20)))) timestamps = list(map(lambda x: x/10, range(10))) es1 = ElectricalSeries('test_eS1', data1, region1, timestamps=timestamps) es2 = ElectricalSeries('test_eS2', data2, region2, channel_conversion=[4., .4], timestamps=timestamps) return es1, es2
def test_link(self): table = make_electrode_table() region = DynamicTableRegion('electrodes', [0, 2], 'the first and third electrodes', table) ts1 = ElectricalSeries('test_ts1', [0, 1, 2, 3, 4, 5], region, timestamps=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5]) ts2 = ElectricalSeries('test_ts2', ts1, region, timestamps=ts1) ts3 = ElectricalSeries('test_ts3', ts2, region, timestamps=ts2) self.assertEqual(ts2.data, [0, 1, 2, 3, 4, 5]) self.assertEqual(ts2.timestamps, [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]) self.assertEqual(ts3.data, [0, 1, 2, 3, 4, 5]) self.assertEqual(ts3.timestamps, [0.0, 0.1, 0.2, 0.3, 0.4, 0.5])
def store_linenoise_notch_CAR(elec_series, processing, mean_frac=.95, round_func=np.ceil): """Apply a notch filter at 60 Hz and its harmonics, calculate and remove the common average reference (CAR), and finally store the signal and the CAR. Parameters ---------- elec_series : ElectricalSeries ElectricalSeries to process. processing : Processing module NWB Processing module to save processed data. mean_frac : float Fraction of the data to be taken in the mean. 0. < mean_frac <= 1. round_func : callable Function for rounding the fraction of channels. Returns ------- X_CAR_ln : ndarray, (n_time, n_channels) Data with line noise and CAR removed. elec_series_CAR_ln : ElectricalSeries ElectricalSeries that holds X_CAR_ln. """ rate = elec_series.rate X = elec_series.data[:] X_ln = apply_linenoise_notch(X, rate) avg = CAR(X_ln, mean_frac=mean_frac, round_func=round_func) X_CAR_ln = X_ln - avg elec_series_CAR_ln = ElectricalSeries( 'CAR_ln_' + elec_series.name, X_CAR_ln, elec_series.electrodes, starting_time=elec_series.starting_time, rate=rate, description=('CAR_lned: ' + elec_series.description)) CAR_series = ElectricalSeries('CAR', avg, elec_series.electrodes, starting_time=elec_series.starting_time, rate=rate, description=('CAR: ' + elec_series.description)) processing.add(elec_series_CAR_ln) processing.add(CAR_series) return X_CAR_ln, elec_series_CAR_ln
def store_subtract_CAR(elec_series, processing, mean_frac=.95, round_func=np.ceil): """Compute and subtract the common average (mean) reference across channels. Parameters ---------- elec_series : ElectricalSeries ElectricalSeries to process. processing : Processing module NWB Processing module to save processed data. mean_frac : float Fraction of the channels to include in the mean. Between 0 and 1. `mean_frac` must include at least one channel. `mean_frac = 1` is equivalent to the mean. Lower fractions interpolate between the mean and the median. round_func : callable Function which specifies how to round to the channel number. Returns ------- X_CAR : ndarray, (n_time, n_channels) X with CAR removed. elec_series_CAR : ElectricalSeries ElectricalSeries that holds X_CAR. """ X = elec_series.data[:] rate = elec_series.rate avg = CAR(X, mean_frac=mean_frac, round_func=round_func) X_CAR = X - avg elec_series_CAR = ElectricalSeries('CAR_' + elec_series.name, X_CAR, elec_series.electrodes, starting_time=elec_series.starting_time, rate=rate, description=('CARed: ' + elec_series.description)) CAR_series = ElectricalSeries('CAR', avg, elec_series.electrodes, starting_time=elec_series.starting_time, rate=rate, description=('CAR: ' + elec_series.description)) processing.add(elec_series_CAR) processing.add(CAR_series) return X_CAR, elec_series_CAR
def store_subtract_CAR(elec_series, processing, mean_frac=.95, round_func=np.ceil): """ Compute and subtract the common average (mean) reference across channels. Parameters ---------- X : ndarray, (n_time, n_channels) Input timeseries. nwb : NWBFile NWBFile to write to. mean_frac : float Fraction of the channels to include in the mean. Between 0 and 1. mean_frac must include at least one channel. round_func : callable Function which specifies how to round to the channel number. Returns ------- Xp : ndarray, (n_time, n_channels) Common average reference. """ X = elec_series.data[:] rate = elec_series.rate avg = CAR(X, mean_frac=mean_frac, round_func=round_func) X_CAR = X - avg elec_series_CAR = ElectricalSeries('CAR_' + elec_series.name, X_CAR, elec_series.electrodes, starting_time=elec_series.starting_time, rate=rate, description=('CARed: ' + elec_series.description)) CAR_series = ElectricalSeries('CAR', avg, elec_series.electrodes, starting_time=elec_series.starting_time, rate=rate, description=('CAR: ' + elec_series.description)) processing.add(elec_series_CAR) processing.add(CAR_series) return X_CAR, elec_series_CAR
def add_ecephys_processed(self): """Add filtered membrane voltage data""" self._create_electrodes_ecephys() with h5py.File(self.source_paths['path_calibration'], 'r') as f: electrode_table_region = self.nwbfile.create_electrode_table_region( region=[0], description='electrode') ecephys_rate = 1 / np.array(f['dte']) trace_data = np.squeeze(f['ephys_baseline_subtracted']) trace_name = 'filtered_membrane_voltage' description = 'voltage trace filtered between 250 Hz and 5 kHz' electrical_series = ElectricalSeries( name=trace_name, description=description, data=trace_data, electrodes=electrode_table_region, starting_time=0., rate=ecephys_rate, ) # Stores processed data ecephys_module = self.nwbfile.create_processing_module( name='ecephys', description= 'contains extracellular electrophysiology processed data') ecephys_module.add(electrical_series)
def addContainer(self, file): table = ElectrodeTable('electrodes') dev1 = Device('dev1', 'a test source') group = ElectrodeGroup('tetrode1', 'a test source', 'tetrode description', 'tetrode location', dev1) table.add_row(1, 1.0, 2.0, 3.0, -1.0, 'CA1', 'none', 'first channel of tetrode', group) table.add_row(2, 1.0, 2.0, 3.0, -2.0, 'CA1', 'none', 'second channel of tetrode', group) table.add_row(3, 1.0, 2.0, 3.0, -3.0, 'CA1', 'none', 'third channel of tetrode', group) table.add_row(4, 1.0, 2.0, 3.0, -4.0, 'CA1', 'none', 'fourth channel of tetrode', group) file.set_device(dev1) file.set_electrode_group(group) file.set_electrode_table(table) region = ElectrodeTableRegion( table, [0, 2], 'the first and third electrodes') # noqa: F405 data = list(zip(range(10), range(10, 20))) timestamps = list(range(10)) es = ElectricalSeries('test_eS', 'a hypothetical source', data, region, timestamps=timestamps) file.add_acquisition(es) return es
def setUpContainer(self): self.make_electrode_table(self) region = DynamicTableRegion('electrodes', [0, 2], 'the first and third electrodes', self.table) data = list(zip(range(10), range(10, 20))) timestamps = list(map(lambda x: x/10, range(10))) ret = ElectricalSeries('test_eS', 'a hypothetical source', data, region, timestamps=timestamps) return ret
def __add_electrodes_helper(nwbfile, h5_grp, positions_csv): electrodes_df = pd.read_csv(positions_csv, sep=' ') electrode_ids = h5_grp['channel_id'][:] data = h5_grp['data'][:] start, stop, timestep = h5_grp['time'][:] # Check sonata file attributes for time units time_units = __get_attrs(h5_grp['time'], 'units', 'ms').lower() if time_units == 's': t_conv = 1.0 else: t_conv = 1.0/1000.0 device = nwbfile.create_device('simulated_implant') electrode_group = nwbfile.create_electrode_group( 'simulated_implant', 'description', 'unknown', device) for (id, x, y, z) in electrodes_df.values: nwbfile.add_electrode(x=x, y=y, z=z, imp=np.nan, id=int(id), location='unknown', filtering='none', group=electrode_group) match_electrodes = [np.where(electrode_ids == x)[0] for x in electrodes_df['channel']] electrodes = nwbfile.create_electrode_table_region(match_electrodes, 'all electrodes') nwbfile.add_acquisition( ElectricalSeries('ElectricalSeries', data, starting_time=start*t_conv, rate=1 / (timestep*t_conv), electrodes=electrodes)) return nwbfile
def test_link_resolve(self): print("TEST_LINK_RESOLVE") nwbfile = NWBFile("source", "a file with header data", "NB123A", '2018-06-01T00:00:00') device = nwbfile.create_device('device_name', 'source') electrode_group = nwbfile.create_electrode_group( name='electrode_group_name', source='source', description='desc', device=device, location='unknown') nwbfile.add_electrode( 0, 1.0, 2.0, 3.0, # position? imp=2.718, location='unknown', filtering='unknown', description='desc', group=electrode_group) etr = nwbfile.create_electrode_table_region([0], 'etr_name') for passband in ('theta', 'gamma'): electrical_series = ElectricalSeries(name=passband + '_phase', source='ephys_analysis', data=[1., 2., 3.], rate=0.0, electrodes=etr) nwbfile.add_acquisition(electrical_series) with NWBHDF5IO(self.path, 'w') as io: io.write(nwbfile) with NWBHDF5IO(self.path, 'r') as io: io.read()
def extract_raw_htk(self, device_name, dev_conf, electrode_table_region): ''' adapted from mars.HTKNWB.add_raw_htk now manages one device at a time ''' # Create the instrument reader device_reader = EPhysInstrumentData( htkdir=self.raw_path, prefix=dev_conf['prefix'], postfix=dev_conf['ch_ids'], device_name=dev_conf['device_type'], read_on_create=False) # Read the raw data with the device_reader device_reader.read_data(create_iterator=True, time_axis_first=True, has_bands=False) # Create the electrical series e_series = ElectricalSeries( name=device_name, #name data=device_reader.data, #data electrodes=electrode_table_region, #electrode table region starting_time=0.0, rate=dev_conf['sampling_rate']) return e_series
def test_link_resolve(self): nwbfile = NWBFile("a file with header data", "NB123A", datetime(2018, 6, 1, tzinfo=tzlocal())) device = nwbfile.create_device('device_name') electrode_group = nwbfile.create_electrode_group( name='electrode_group_name', description='desc', device=device, location='unknown') nwbfile.add_electrode(id=0, x=1.0, y=2.0, z=3.0, # position? imp=2.718, location='unknown', filtering='unknown', group=electrode_group) etr = nwbfile.create_electrode_table_region([0], 'etr_name') for passband in ('theta', 'gamma'): electrical_series = ElectricalSeries(name=passband + '_phase', data=[1., 2., 3.], rate=0.0, electrodes=etr) nwbfile.add_acquisition(electrical_series) with NWBHDF5IO(self.path, 'w') as io: io.write(nwbfile) with NWBHDF5IO(self.path, 'r') as io: io.read()
def ncs_to_grp_eseries(grp, electrode_locations, nwbfile, data_time_len=None, dtype=np.float32, fs=None): grp_ts_kwargs = [] channels = [] for ch, ts_kwargs in iter_ncs_to_timeseries(grp.ncs_path.values, data_time_len=data_time_len, dtype=dtype, fs=fs): # row = electrode_locations.where(electrode_locations.chan_num==ch+1).dropna().iloc[0] channels.append(ch) grp_ts_kwargs.append(ts_kwargs) if len(grp_ts_kwargs) > 0: dats = [t['data'] for t in grp_ts_kwargs] shapes = np.unique([len(t['data']) for t in grp_ts_kwargs]) if len(shapes) == 1: dats = np.stack(dats) ts_kwargs['data'] = dats electrode_table_region = nwbfile.create_electrode_table_region( channels, 'Channel') ts_kwargs['electrodes'] = electrode_table_region ts_kwargs['name'] = grp.anat_sh.unique()[0] ts = ElectricalSeries(**ts_kwargs) return ts
def extract_tdt(self, device_name, dev_conf, electrode_table_region): ''' extracts TDT data for a single device, and returns an ElectricalSeries. Args: - device_name: (str) either 'ECoG' or 'Poly' - dev_conf: (dict) metadata for the device. nwb_builder.metadata['device'][device_name] - electrode_table_region: NWB electrode table region for the device Returns: - e_series: (ElectricalSeries) to be added to the NWB file ''' data, tdt_params = self.__extract_from_tdt_files(device_name, dev_conf) starting_time = tdt_params['start_time'] rate = tdt_params['sample_rate'] # Create the electrical series e_series = ElectricalSeries( name=device_name, data=data, electrodes=electrode_table_region, starting_time=starting_time, rate=rate, # **add_other_fields_as_necessary ) return e_series
def write_lfp( nwbfile: NWBFile, data: ArrayLike, fs: float, electrode_inds: Optional[List[int]] = None, name: Optional[str] = "LFP", description: Optional[str] = "local field potential signal", ): """ Add LFP from neuroscope to a "ecephys" processing module of an NWBFile. Parameters ---------- nwbfile: pynwb.NWBFile data: array-like fs: float electrode_inds: list(int), optional name: str, optional description: str, optional Returns ------- LFP pynwb.ecephys.ElectricalSeries """ if electrode_inds is None: if nwbfile.electrodes is not None and data.shape[1] <= len( nwbfile.electrodes.id.data[:]): electrode_inds = list(range(data.shape[1])) else: electrode_inds = list(range(len(nwbfile.electrodes.id.data[:]))) table_region = nwbfile.create_electrode_table_region( electrode_inds, "electrode table reference") data = H5DataIO( DataChunkIterator(tqdm(data, desc="writing lfp data"), buffer_size=int(fs * 3600)), compression="gzip", ) lfp_electrical_series = ElectricalSeries( name=name, description=description, data=data, electrodes=table_region, conversion=1e-6, rate=fs, resolution=np.nan, ) ecephys_mod = check_module( nwbfile, "ecephys", "intermediate data from extracellular electrophysiology recordings, e.g., LFP", ) if "LFP" not in ecephys_mod.data_interfaces: ecephys_mod.add_data_interface(LFP(name="LFP")) ecephys_mod.data_interfaces["LFP"].add_electrical_series( lfp_electrical_series) return lfp_electrical_series
def setUpContainer(self): data = list(range(10)) ts = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] TestElectricalSeriesIO.make_electrode_table(self) region = DynamicTableRegion('electrodes', [0, 2], 'the first and third electrodes', self.table) self.eS = ElectricalSeries('test_eS', 'a hypothetical source', data, region, timestamps=ts) eD = EventDetection('test_ed', 'detection_method', self.eS, (1, 2, 3), (0.1, 0.2, 0.3)) return eD
def setUpContainer(self): """ Return the test ElectricalSeries to read/write """ self.make_electrode_table(self) region = DynamicTableRegion('electrodes', [0, 2], 'the first and third electrodes', self.table) data = list(zip(range(10), range(10, 20))) timestamps = list(map(lambda x: x/10, range(10))) es = ElectricalSeries('test_eS', data, region, channel_conversion=[4., .4], timestamps=timestamps) return es
def write_recording(recording, save_path, acquisition_name='ElectricalSeries'): assert HAVE_NWB, "To use the Nwb extractors, install pynwb: \n\n pip install pynwb\n\n" M = recording.get_num_channels() nwbfile = NWBFile( session_description='', identifier='', session_start_time=datetime.now(), ) device = nwbfile.create_device(name='device_name') eg_name = 'electrode_group_name' eg_description = "electrode_group_description" eg_location = "electrode_group_location" electrode_group = nwbfile.create_electrode_group( name=eg_name, location=eg_location, device=device, description=eg_description ) for m in range(M): location = recording.get_channel_property(m, 'location') impedence = -1.0 while len(location) < 3: location = np.append(location, [0]) nwbfile.add_electrode( id=m, x=float(location[0]), y=float(location[1]), z=float(location[2]), imp=impedence, location='electrode_location', filtering='none', group=electrode_group, ) electrode_table_region = nwbfile.create_electrode_table_region( list(range(M)), 'electrode_table_region' ) rate = recording.get_sampling_frequency() ephys_data = recording.get_traces().T ephys_ts = ElectricalSeries( name=acquisition_name, data=ephys_data, electrodes=electrode_table_region, starting_time=recording.frame_to_time(0), rate=rate, resolution=1e-6, comments='Generated from SpikeInterface::NwbRecordingExtractor', description='acquisition_description' ) nwbfile.add_acquisition(ephys_ts) if os.path.exists(save_path): os.remove(save_path) with NWBHDF5IO(save_path, 'w') as io: io.write(nwbfile)
def write(self, name, t, v, description, comments=""): self.nwbfile.add_acquisition( ElectricalSeries(name, v, self.electrode_table_region, timestamps=t, resolution=0.001, comments=comments, description=description))
def test_init(self): data = list(range(10)) ts = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] table = make_electrode_table() region = DynamicTableRegion('electrodes', [0, 2], 'the first and third electrodes', table) eS = ElectricalSeries('test_eS', data, region, timestamps=ts) # noqa: F405 self.assertEqual(eS.name, 'test_eS') self.assertEqual(eS.data, data) self.assertEqual(eS.timestamps, ts)
def addContainer(self, nwbfile): """ Add electrode table region and related objects to the given NWBFile """ self.dev1 = nwbfile.create_device(name='dev1') self.group = nwbfile.create_electrode_group( name='tetrode1', description='tetrode description', location='tetrode location', device=self.dev1) nwbfile.add_electrode(id=1, x=1.0, y=2.0, z=3.0, imp=-1.0, location='CA1', filtering='none', group=self.group, group_name='tetrode1') nwbfile.add_electrode(id=2, x=1.0, y=2.0, z=3.0, imp=-2.0, location='CA1', filtering='none', group=self.group, group_name='tetrode1') nwbfile.add_electrode(id=3, x=1.0, y=2.0, z=3.0, imp=-3.0, location='CA1', filtering='none', group=self.group, group_name='tetrode1') nwbfile.add_electrode(id=4, x=1.0, y=2.0, z=3.0, imp=-4.0, location='CA1', filtering='none', group=self.group, group_name='tetrode1') region = nwbfile.create_electrode_table_region(region=tuple([1, 2, 3]), name='electrodes', description='desc') nwbfile.add_acquisition( ElectricalSeries(name='test_data', data=np.arange(10), timestamps=np.arange(10.), electrodes=region)) self.container = region # override self.container which has the placeholder
def test_add_electrical_series(self): lfp = LFP() table = make_electrode_table() region = DynamicTableRegion('electrodes', [0, 2], 'the first and third electrodes', table) eS = ElectricalSeries('test_eS', [0, 1, 2, 3], region, timestamps=[0.1, 0.2, 0.3, 0.4]) lfp.add_electrical_series(eS) self.assertEqual(lfp.electrical_series.get('test_eS'), eS)
def test_init(self): table = make_electrode_table() region = DynamicTableRegion('electrodes', [0, 2], 'the first and third electrodes', table) eS = ElectricalSeries('test_eS', [0, 1, 2, 3], region, timestamps=[0.1, 0.2, 0.3, 0.4]) fe = FilteredEphys(eS) self.assertEqual(fe.electrical_series.get('test_eS'), eS) self.assertEqual(fe['test_eS'], fe.electrical_series.get('test_eS'))
def setUp(self): nwbfile = NWBFile( "my first synthetic recording", "EXAMPLE_ID", datetime.now(tzlocal()), experimenter="Dr. Matthew Douglass", lab="Vision Neuroscience Laboratory", institution="University of Middle Earth at the Shire", experiment_description= "We recorded from macaque monkeys during memory-guided saccade task", session_id="LONELYMTL", ) device = nwbfile.create_device(name="trodes_rig123") electrode_group = nwbfile.create_electrode_group( name="tetrode1", description="an example tetrode", location="somewhere in the hippocampus", device=device, ) for idx in [1, 2, 3, 4]: nwbfile.add_electrode( id=idx, x=1.0, y=2.0, z=3.0, imp=float(-idx), location="CA1", filtering="none", group=electrode_group, ) electrode_table_region = nwbfile.create_electrode_table_region( [0, 2], "the first and third electrodes") self.electrodes = electrode_table_region rate = 10.0 np.random.seed(1234) data_len = 1000 ephys_data = np.random.rand(data_len * 2).reshape((data_len, 2)) ephys_timestamps = np.arange(data_len) / rate self.ephys_ts = ElectricalSeries( "test_ephys_data", ephys_data, self.electrodes, timestamps=ephys_timestamps, resolution=0.001, description="Random numbers generated with numpy.random.rand", ) self.lfp = LFP(electrical_series=self.ephys_ts, name="LFP data")
def test_ext(): nwbfile = NWBFile('description', 'id', datetime.now().astimezone()) device = nwbfile.create_device('device_name') electrode_group = nwbfile.create_electrode_group('electrode_group', 'desc', 'loc', device=device) for i in np.arange(20.): nwbfile.add_electrode(i, i, i, np.nan, 'loc', 'filt', electrode_group) bipolar_scheme = DynamicTable(name='bipolar_scheme', description='desc') bipolar_scheme.add_column(name='anode', description='desc', index=True, table=nwbfile.electrodes) bipolar_scheme.add_column(name='cathode', description='desc', index=True, table=nwbfile.electrodes) bipolar_scheme.add_row(anode=[0], cathode=[1]) bipolar_scheme.add_row(anode=[0, 1], cathode=[2, 3]) bipolar_scheme.add_row(anode=[0, 1], cathode=[2]) ecephys_ext = EcephysExt(bipolar_scheme=bipolar_scheme) nwbfile.add_lab_meta_data(ecephys_ext) bipolar_scheme_region = DynamicTableRegion( name='electrodes', data=np.arange(0, 3), description='desc', table=nwbfile.lab_meta_data['extracellular_ephys_extensions']. bipolar_scheme) ec_series = ElectricalSeries(name='test_ec_series', description='desc', data=np.random.rand(100, 3), rate=1000., electrodes=bipolar_scheme_region) nwbfile.add_acquisition(ec_series) with NWBHDF5IO('test_nwb.nwb', 'w') as io: io.write(nwbfile) with NWBHDF5IO('test_nwb.nwb', 'r', load_namespaces=True) as io: nwbfile = io.read() assert_array_equal( nwbfile.acquisition['test_ec_series'].electrodes.table['anode'][2] ['x'], [0., 1.]) os.remove('test_nwb.nwb')
def test_init(self): dev1 = Device('dev1') # noqa: F405 group = ElectrodeGroup( # noqa: F405, F841 'tetrode1', 'tetrode description', 'tetrode location', dev1) table = make_electrode_table() region = DynamicTableRegion('electrodes', [0, 2], 'the first and third electrodes', table) eS = ElectricalSeries( # noqa: F405 'test_eS', [0, 1, 2, 3], region, timestamps=[0.1, 0.2, 0.3, 0.4]) fe = FilteredEphys(eS) # noqa: F405 self.assertEqual(fe.electrical_series.get('test_eS'), eS) self.assertEqual(fe['test_eS'], fe.electrical_series.get('test_eS'))
def write_lfp(nwbfile, data, fs, name='LFP', description='local field potential signal', electrode_inds=None): """ Add LFP from neuroscope to a "ecephys" processing module of an NWBFile Parameters ---------- nwbfile: pynwb.NWBFile data: array-like fs: float name: str description: str electrode_inds: list(int) Returns ------- LFP pynwb.ecephys.ElectricalSeries """ if electrode_inds is None: electrode_inds = list(range(data.shape[1])) table_region = nwbfile.create_electrode_table_region( electrode_inds, 'electrode table reference') data = H5DataIO(DataChunkIterator(tqdm(data, desc='writing lfp data'), buffer_size=int(fs * 3600)), compression='gzip') lfp_electrical_series = ElectricalSeries(name=name, description=description, data=data, electrodes=table_region, conversion=np.nan, rate=fs, resolution=np.nan) ecephys_mod = check_module( nwbfile, 'ecephys', 'intermediate data from extracellular electrophysiology recordings, e.g., LFP' ) if 'LFP' not in ecephys_mod.data_interfaces: ecephys_mod.add_data_interface(LFP(name='LFP')) ecephys_mod.data_interfaces['LFP'].add_electrical_series( lfp_electrical_series) return lfp_electrical_series