Optogenetics

This tutorial will demonstrate how to write optogenetics data.

Creating an NWBFile object

When creating a NWB file, the first step is to create the NWBFile object using NwbFile.
nwb = NwbFile( ...
'session_description', 'mouse in open exploration',...
'identifier', char(java.util.UUID.randomUUID), ...
'session_start_time', datetime(2018, 4, 25, 2, 30, 3), ...
'general_experimenter', 'Last, First M.', ... % 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
nwb

Adding optogenetic data

The ogen module contains two data types that you will need to write optogenetics data, OptogeneticStimulusSite, which contains metadata about the stimulus site, and OptogeneticSeries, which contains the values of the time series.
First, you need to create a Device object linked to the NWBFile:
device = types.core.Device();
nwb.general_devices.set('Device', device);
Now, you can create and add an OptogeneticStimulusSite.
ogen_stim_site = types.core.OptogeneticStimulusSite( ...
'device', types.untyped.SoftLink(device), ...
'description', 'This is an example optogenetic site.', ...
'excitation_lambda', 600.0, ...
'location', 'VISrl');
 
nwb.general_optogenetics.set('OptogeneticStimulusSite', ogen_stim_site);
With the OptogeneticStimulusSite added, you can now create and add a OptogeneticSeries. Here, we will generate some random data and specify the timing using rate. If you have samples at irregular intervals, you should use timestamps instead.
ogen_series = types.core.OptogeneticSeries( ...
'data', randn(20, 1), ...
'site', types.untyped.SoftLink(ogen_stim_site), ...
'starting_time', 0.0, ...
'starting_time_rate', 30.0); % Hz
nwb.stimulus_presentation.set('OptogeneticSeries', ogen_series);
 
nwb
Now you can write the NWB file.
nwbExport(nwb, 'ogen_tutorial.nwb');