Neurodata Without Borders Extracellular Electrophysiology Tutorial
This tutorial
Create fake data for a hypothetical extracellular electrophysiology experiment. The types of data we will convert are:
- Voltage recording
- Local field potential (LFP)
- Spike times
It is recommended to first work through the Introduction to MatNWB tutorial, which demonstrates installing MatNWB and creating an NWB file with subject information, animal position, and trials, as well as writing and reading NWB files in MATLAB. Setting up the NWB File
An NWB file represents a single session of an experiment. Each file must have a session_description, identifier, and session start time. Create a new NWBFile object with those and additional metadata. For all MatNWB functions, we use the Matlab method of entering keyword argument pairs, where arguments are entered as name followed by value. 'session_description', 'mouse in open exploration',...
'identifier', 'Mouse5_Day3', ...
'session_start_time', datetime(2018, 4, 25, 2, 30, 3, 'TimeZone', 'local'), ...
'timestamps_reference_time', datetime(2018, 4, 25, 3, 0, 45, 'TimeZone', 'local'), ...
'general_experimenter', 'Last Name, First Name', ... % optional
'general_session_id', 'session_1234', ... % optional
'general_institution', 'University of My Institution', ... % optional
'general_related_publications', {'DOI:10.1016/j.neuron.2016.12.011'}); % optional
Extracellular Electrophysiology
In order to store extracellular electrophysiology data, you first must create an electrodes table describing the electrodes that generated this data. Extracellular electrodes are stored in an electrodes table, which is also a DynamicTable. electrodes has several required fields: x, y, z, impedance, location, filtering, and electrode_group. Electrodes Table
Since this is a DynamicTable, we can add additional metadata fields. We will be adding a "label" column to the table. ElectrodesDynamicTable = types.hdmf_common.DynamicTable(...
'colnames', {'location', 'group', 'group_name', 'label'}, ...
'description', 'all electrodes');
Device = types.core.Device(...
'description', 'the best array', ...
'manufacturer', 'Probe Company 9000' ...
nwb.general_devices.set('array', Device);
shankGroupName = sprintf('shank%d', iShank);
EGroup = types.core.ElectrodeGroup( ...
'description', sprintf('electrode group for %s', shankGroupName), ...
'location', 'brain area', ...
'device', types.untyped.SoftLink(Device) ...
nwb.general_extracellular_ephys.set(shankGroupName, EGroup);
for iElectrode = 1:numChannelsPerShank
ElectrodesDynamicTable.addRow( ...
'location', 'unknown', ...
'group', types.untyped.ObjectView(EGroup), ...
'group_name', shankGroupName, ...
'label', sprintf('%s-electrode%d', shankGroupName, iElectrode));
ElectrodesDynamicTable.toTable() % Display the table
ans = 12×5 table
| id | location | group | group_name | label |
---|
1 | 0 | 'unknown' | 1×1 ObjectView | 'shank1' | 'shank1-electrode1' |
---|
2 | 1 | 'unknown' | 1×1 ObjectView | 'shank1' | 'shank1-electrode2' |
---|
3 | 2 | 'unknown' | 1×1 ObjectView | 'shank1' | 'shank1-electrode3' |
---|
4 | 3 | 'unknown' | 1×1 ObjectView | 'shank2' | 'shank2-electrode1' |
---|
5 | 4 | 'unknown' | 1×1 ObjectView | 'shank2' | 'shank2-electrode2' |
---|
6 | 5 | 'unknown' | 1×1 ObjectView | 'shank2' | 'shank2-electrode3' |
---|
7 | 6 | 'unknown' | 1×1 ObjectView | 'shank3' | 'shank3-electrode1' |
---|
8 | 7 | 'unknown' | 1×1 ObjectView | 'shank3' | 'shank3-electrode2' |
---|
9 | 8 | 'unknown' | 1×1 ObjectView | 'shank3' | 'shank3-electrode3' |
---|
10 | 9 | 'unknown' | 1×1 ObjectView | 'shank4' | 'shank4-electrode1' |
---|
11 | 10 | 'unknown' | 1×1 ObjectView | 'shank4' | 'shank4-electrode2' |
---|
12 | 11 | 'unknown' | 1×1 ObjectView | 'shank4' | 'shank4-electrode3' |
---|
nwb.general_extracellular_ephys_electrodes = ElectrodesDynamicTable;
Links
In the above loop, we create ElectrodeGroup objects. The electrodes table then uses an ObjectView in each row to link to the corresponding ElectrodeGroup object. An ObjectView is an object that allow you to create a link from one neurodata type referencing another. ElectricalSeries
Voltage data are stored in ElectricalSeries objects. ElectricalSeries is a subclass of TimeSeries specialized for voltage data. In order to create our ElectricalSeries object, we will need to reference a set of rows in the electrodes table to indicate which electrodes were recorded. We will do this by creating a DynamicTableRegion, which is a type of link that allows you to reference specific rows of a DynamicTable, such as the electrodes table, by row indices. electrode_table_region = types.hdmf_common.DynamicTableRegion( ...
'table', types.untyped.ObjectView(ElectrodesDynamicTable), ...
'description', 'all electrodes', ...
'data', (0:length(ElectrodesDynamicTable.id.data)-1)');
Now create an ElectricalSeries object to hold acquisition data collected during the experiment.
electrical_series = types.core.ElectricalSeries( ...
'starting_time', 0.0, ... % seconds
'starting_time_rate', 30000., ... % Hz
'data', randn(12, 3000), ...
'electrodes', electrode_table_region, ...
This is the voltage data recorded directly from our electrodes, so it goes in the acquisition group.
nwb.acquisition.set('ElectricalSeries', electrical_series);
LFP
Local field potential (LFP) refers in this case to data that has been downsampled and/or filtered from the original acquisition data and is used to analyze signals in the lower frequency range. Filtered and downsampled LFP data would also be stored in an ElectricalSeries. To help data analysis and visualization tools know that this ElectricalSeries object represents LFP data, store it inside an LFP object, then place the LFP object in a ProcessingModule named 'ecephys'. This is analogous to how we stored the SpatialSeries object inside of a Position object and stored the Position object in a ProcessingModule named 'behavior' earlier. electrical_series = types.core.ElectricalSeries( ...
'starting_time', 0.0, ... % seconds
'starting_time_rate', 1000., ... % Hz
'data', randn(12, 100), ...
'electrodes', electrode_table_region, ...
lfp = types.core.LFP('ElectricalSeries', electrical_series);
ecephys_module = types.core.ProcessingModule(...
'description', 'extracellular electrophysiology');
ecephys_module.nwbdatainterface.set('LFP', lfp);
nwb.processing.set('ecephys', ecephys_module);
Sorted Spike Times
Ragged Arrays
Spike times are stored in another DynamicTable of subtype Units. The default Units table is at /units in the HDF5 file. You can add columns to the Units table just like you did for electrodes and trials. Here, we generate some random spike data and populate the table. spikes = cell(1, num_cells);
spikes{iShank} = rand(1, randi([16, 28]));
spikes
spikes = 1×10 cell
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|
1 | 1×21 double | 1×24 double | 1×18 double | 1×28 double | 1×25 double | 1×18 double | 1×21 double | 1×28 double | 1×16 double | 1×19 double |
---|
Spike times are an example of a ragged array- it's like a matrix, but each row has a different number of elements. We can represent this type of data as an indexed column of the units DynamicTable. These indexed columns have two components, the vector data object that holds the data and the vector index object that holds the indices in the vector that indicate the row breaks. You can use the convenience function util.create_indexed_column to create these objects. [spike_times_vector, spike_times_index] = util.create_indexed_column(spikes);
nwb.units = types.core.Units( ...
'colnames', {'spike_times'}, ...
'description', 'units table', ...
'spike_times', spike_times_vector, ...
'spike_times_index', spike_times_index ...
nwb.units.toTable
ans = 10×2 table
| id | spike_times |
---|
1 | 1 | 21×1 double |
---|
2 | 2 | 24×1 double |
---|
3 | 3 | 18×1 double |
---|
4 | 4 | 28×1 double |
---|
5 | 5 | 25×1 double |
---|
6 | 6 | 18×1 double |
---|
7 | 7 | 21×1 double |
---|
8 | 8 | 28×1 double |
---|
9 | 9 | 16×1 double |
---|
10 | 10 | 19×1 double |
---|
Unsorted Spike Times
In MATLAB, while the Units table is used to store spike times and waveform data for spike-sorted, single-unit activity, you may also want to store spike times and waveform snippets of unsorted spiking activity. This is useful for recording multi-unit activity detected via threshold crossings during data acquisition. Such information can be stored using SpikeEventSeries objects. % In the SpikeEventSeries the dimensions should be ordered as
% [num_events, num_channels, num_samples].
% Define spike snippets: 20 events, 3 channels, 40 samples per event.
spike_snippets = rand(20, 3, 40);
% Permute spike snippets (See dimensionMapNoDataPipes tutorial)
spike_snippets = permute(spike_snippets, [3,2,1])
% Create electrode table region referencing electrodes 0, 1, and 2
shank0_table_region = types.hdmf_common.DynamicTableRegion( ...
'table', types.untyped.ObjectView(ElectrodesDynamicTable), ...
'description', 'shank0', ...
% Define spike event series for unsorted spike times
spike_events = types.core.SpikeEventSeries( ...
'data', spike_snippets, ...
'timestamps', (0:19)', ... % Timestamps for each event
'description', 'events detected with 100uV threshold', ...
'electrodes', shank0_table_region ...
% Add spike event series to NWB file acquisition
nwb.acquisition.set('SpikeEvents_Shank0', spike_events);
Designating Electrophysiology Data
As mentioned above, ElectricalSeries objects are meant for storing specific types of extracellular recordings. In addition to this TimeSeries class, NWB provides some Processing Modules for designating the type of data you are storing. We will briefly discuss them here, and refer the reader to the API documentation and Intro to NWB for more details on using these objects. For storing unsorted spiking data, there are two options. Which one you choose depends on what data you have available. If you need to store complete and/or continuous raw voltage traces, you should store the traces with ElectricalSeries objects as acquisition data, and use the EventDetection class for identifying the spike events in your raw traces. If you do not want to store the raw voltage traces and only the waveform ‘snippets’ surrounding spike events, you should use SpikeEventSeries objects. The results of spike sorting (or clustering) should be stored in the top-level Units table. The Units table can hold just the spike times of sorted units or, optionally, include additional waveform information. You can use the optional predefined columns waveform_mean, waveform_sd, and waveforms in the Units table to store individual and mean waveform data. For local field potential data, there are two options. Again, which one you choose depends on what data you have available. With both options, you should store your traces with ElectricalSeries objects. If you are storing unfiltered local field potential data, you should store the ElectricalSeries objects in LFP data interface object(s). If you have filtered LFP data, you should store the ElectricalSeries objects in FilteredEphys data interface object(s). Writing the NWB File
nwbExport(nwb, 'ecephys_tutorial.nwb')
Reading NWB Data
Data arrays are read passively from the file. Calling TimeSeries.data does not read the data values, but presents an HDF5 object that can be indexed to read data. This allows you to conveniently work with datasets that are too large to fit in RAM all at once. load with no input arguments reads the entire dataset:
nwb2 = nwbRead('ecephys_tutorial.nwb', 'ignorecache');
nwb2.processing.get('ecephys'). ...
nwbdatainterface.get('LFP'). ...
electricalseries.get('ElectricalSeries'). ...
Accessing Data Regions
If all you need is a data region, you can index a DataStub object like you would any normal array in MATLAB, as shown below. When indexing the dataset this way, only the selected region is read from disk into RAM. This allows you to handle very large datasets that would not fit entirely into RAM.
nwb2.processing.get('ecephys'). ...
nwbdatainterface.get('LFP'). ...
electricalseries.get('ElectricalSeries'). ...
data(1:5, 1:10)
-1.0039 -0.5621 -1.1019 0.1768 0.2032 0.1612 -0.5518 0.8552 -1.3040 -0.5646
-0.3458 -1.2921 -0.1967 1.7260 -1.5245 1.3653 -0.6380 0.8438 -0.7094 0.7466
0.2758 -0.3401 0.3549 0.4890 -0.2288 0.1290 2.1648 0.1316 -0.2172 0.3036
-0.8548 -1.5282 -0.0919 -0.1388 1.7996 -0.2845 -1.1904 0.5773 -0.3059 -0.9745
0.1536 -0.2051 2.4873 1.0999 0.6398 -0.1086 -0.2511 -0.0993 1.3019 0.0095
% You can use the getRow method of the table to load spike times of a specific unit.
% To get the values, unpack from the returned table.
nwb.units.getRow(1).spike_times{1}
0.8383
0.6321
0.2418
0.2965
0.9865
0.2779
0.9945
0.5980
0.1216
0.1694
Learn more!
See the API documentation to learn what data types are available.
MATLAB tutorials
Python tutorials
See our tutorials for more details about your data type:
Check out other tutorials that teach advanced NWB topics: