NeurodataWithoutBorders / matnwb

A Matlab interface for reading and writing NWB files
BSD 2-Clause "Simplified" License
50 stars 32 forks source link

how to construct trials table? #59

Closed bendichter closed 6 years ago

bendichter commented 6 years ago

We have it working that you can create a dynamictable in pynwb and read it in matnwb, but I do not know how to do the reverse. Can you give me a minimal example for constructing a trials or units table in matnwb?

lawrence-mbf commented 6 years ago

For trials:

columnNames = {'start'};
nwb.trials = types.core.DynamicTable(...
    'colnames', columnNames,...
    'description', 'trial data and properties', ...
    'id', types.core.ElementIdentifiers('data', 1:10));

for i=1:length(columnNames)
    nwb.trials.tablecolumn.set(columnNames{i}, ...
        types.core.TableColumn('data', <some vector>));
end

Things to note that may impact compatibility:

  1. There are no checks for guaranteeing that the names in colnames match the entries in tablecolumn.
  2. There are no checks guaranteeing rectangular shape of the DynamicTable (that all columns have the same number of elements as the id)
  3. 43 so the the presence of a 'start' column is not checked for, either in name or as a tablecolumn entry.

I believe all three can actually be worked around by subclassing DynamicTable and writing the checks manually.

bendichter commented 6 years ago

perfect! Thanks for the speedy response!

bendichter commented 6 years ago

OK, what did I do wrong?

nwb = nwbfile;

nwb.trials = types.core.DynamicTable(...
    'colnames', {'start','stop','condition'},...
    'description', 'trial data and properties', ...
    'id', types.core.ElementIdentifiers('data', 1:3));

nwb.trials.tablecolumn.set('start', ...
    types.core.TableColumn('data', [.5, 1.5, 2.5]));

nwb.trials.tablecolumn.set('stop', ...
    types.core.TableColumn('data', [1., 2., 3.]));

nwb.trials.tablecolumn.set('condition', ...
    types.core.TableColumn('data', [0,1,0]));

nwbExport(nwb, 'test_trials.nwb')
lawrence-mbf commented 6 years ago

Is there an error message? It runs fine on my machine.

bendichter commented 6 years ago

hmm let me update... yup, that was it. Thanks!