Skip to content

Commit

Permalink
571 Add "from MATLAB Table" example to Dynamic Tables tutorial (#604)
Browse files Browse the repository at this point in the history
* Fix: Ensure autogenerated id column is column vector in util.table2nwb

* Add table's variable description to vectordata description property

* Add nwb type descriptions to table's VariableDescription property

* Add conversion from table to nwb table in tutorial (Issue #571 )

* Fix failing test
  • Loading branch information
ehennestad authored Nov 1, 2024
1 parent bc4b87d commit 70fcace
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 26 deletions.
5 changes: 4 additions & 1 deletion +tests/+system/DynamicTableTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ function toTableTest(testCase)
ExpectedSubTable = testCase.file.intervals_trials.getRow(1:20);
% convert DynamicTable to MATLAB table
TrialsTable = testCase.file.intervals_trials.toTable();
TrialsTable.id = []; %remove id column
% Remove id column and variable descriptions as they are not
% present in the ExpectedSubTable retrieved from getRow method
TrialsTable.id = [];
TrialsTable.Properties.VariableDescriptions = {};
% retrieve rows from MATLAB table
ActualSubTable = TrialsTable(1:20,:);
% compare
Expand Down
5 changes: 5 additions & 0 deletions +types/+util/+dynamictable/nwbToTable.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

% deal with DynamicTableRegion columns when index is false
[columns, remainingColumns] = deal(DynamicTable.colnames);
columnDescriptions = repmat({''}, 1, length(columns));

for i = 1:length(columns)
cn = columns{i};
Expand All @@ -61,6 +62,7 @@
else
cv = DynamicTable.vectordata.get(cn);
end
columnDescriptions{i} = cv.description;
if ~index && ...
(isa(cv,'types.hdmf_common.DynamicTableRegion') ||...
isa(cv,'types.core.DynamicTableRegion'))
Expand All @@ -85,3 +87,6 @@

% Update the columns order to be the same as the original
matlabTable = matlabTable(:, [{'id'}, columns]);

% Add variable descriptions
matlabTable.Properties.VariableDescriptions = [{''}, columnDescriptions];
11 changes: 9 additions & 2 deletions +util/table2nwb.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
if ismember('id', T.Properties.VariableNames)
id = T.id;
else
id = 0:height(T)-1;
id = transpose( 0:height(T)-1 ); % Must be column vector
end

nwbtable = types.hdmf_common.DynamicTable( ...
Expand All @@ -28,8 +28,15 @@

for col = T
if ~strcmp(col.Properties.VariableNames{1},'id')

if ~isempty(col.Properties.VariableDescriptions{1})
description = col.Properties.VariableDescriptions{1};
else
description = 'no description provided';
end

nwbtable.vectordata.set(col.Properties.VariableNames{1}, ...
types.hdmf_common.VectorData('data', col.Variables',...
'description', 'my description'));
'description', description));
end
end
Binary file modified tutorials/dynamic_tables.mlx
Binary file not shown.
81 changes: 58 additions & 23 deletions tutorials/html/dynamic_tables.html

Large diffs are not rendered by default.

0 comments on commit 70fcace

Please sign in to comment.