Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions +tests/+unit/dynamicTableTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,70 @@ function testGetRowThrowsSpecificErrorForEmptyTable(testCase)
'NWB:DynamicTable:GetRow:RowOutOfBounds');
end

function testAddEmptyRowToRaggedColumn(testCase)
dynamicTable = types.hdmf_common.DynamicTable( ...
'description', 'test table with a ragged column', ...
'colnames', {'spike_times'});

dynamicTable.addRow('spike_times', [1, 2, 3]);
dynamicTable.addRow('spike_times', []);
dynamicTable.addRow('spike_times', [4, 5]);

% The empty row ends where the row before it ends.
columnIndex = dynamicTable.vectordata.get('spike_times_index');
testCase.verifyEqual(columnIndex.data, uint64([3; 3; 5]));
testCase.verifyEqual(dynamicTable.id.data, int64([0; 1; 2]));
testCase.verifyEmpty(dynamicTable.getRow(2).spike_times{1});
testCase.verifyEqual(dynamicTable.getRow(3).spike_times{1}, [4, 5]);
end

function testAddEmptyRowAsFirstRowOfColumn(testCase)
dynamicTable = types.hdmf_common.DynamicTable( ...
'description', 'test table with a ragged column', ...
'colnames', {'spike_times'});

dynamicTable.addRow('spike_times', []);
dynamicTable.addRow('spike_times', [4, 5]);

% The column is ragged from the first row, which ends at
% element 0.
columnIndex = dynamicTable.vectordata.get('spike_times_index');
testCase.verifyEqual(columnIndex.data, uint64([0; 2]));
testCase.verifyEqual(dynamicTable.id.data, int64([0; 1]));
testCase.verifyEmpty(dynamicTable.getRow(1).spike_times{1});
testCase.verifyEqual(dynamicTable.getRow(2).spike_times{1}, [4, 5]);
end

function testAddEmptyRowToRaggedTextColumn(testCase)
dynamicTable = types.hdmf_common.DynamicTable( ...
'description', 'test table with a ragged text column', ...
'colnames', {'tags'});

dynamicTable.addRow('tags', {'a', 'b'});
dynamicTable.addRow('tags', {});
dynamicTable.addRow('tags', {'c'});

columnIndex = dynamicTable.vectordata.get('tags_index');
testCase.verifyEqual(columnIndex.data, uint64([2; 2; 3]));
testCase.verifyEqual(dynamicTable.id.data, int64([0; 1; 2]));
end

function testAddEmptyRowMakesColumnRagged(testCase)
dynamicTable = types.hdmf_common.DynamicTable( ...
'description', 'test table with one value per row', ...
'colnames', {'columnA'});

dynamicTable.addRow('columnA', 1);
dynamicTable.addRow('columnA', 2);
dynamicTable.addRow('columnA', []);

% Each existing row becomes a ragged row of one element, and the
% empty row adds none.
columnIndex = dynamicTable.vectordata.get('columnA_index');
testCase.verifyEqual(columnIndex.data, uint64([1; 2; 2]));
testCase.verifyEqual(dynamicTable.id.data, int64([0; 1; 2]));
end

function testNwbToTableWithReferencedTablesAsRowIndices(testCase)
% The default mode for the toTable() method is to return the row indices
% for dynamic table regions. This test verifies that the data type of
Expand Down
13 changes: 12 additions & 1 deletion +types/+util/+dynamictable/addRawData.m

@ehennestad ehennestad Oct 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The depth variable here refers to how many vectors are needed to describe a column.
depth = 1 - Only VectorData - standard column
depth = 2 - VectorData + VectorIndex - ragged column
depth = n - VectorData + (n-1) VectorIndex - nested ragged column

Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ function initVecData(DynamicTable, column, dataType)
else
isMultiRow = 1 < size(subData, p.Results.dataPipeDimension);
end
if isMultiRow

% A value without elements is a row of a ragged column that holds no
% elements. Like a row of several elements, it needs an index. An empty
% character vector is one element, an empty text value.
isEmptyRow = isempty(subData) && ~ischar(subData);
if isMultiRow || isEmptyRow
depth = depth + 1;
end
end
Expand Down Expand Up @@ -151,6 +156,12 @@ function initVecData(DynamicTable, column, dataType)

add2Index(Vector, numRows);
else
if isempty(data)
% A row without elements adds nothing to the column.
numRows = 0;
return
end

if ischar(data)
data = mat2cell(data, ones(size(data, 1), 1));
end % char matrices converted to cell arrays containing character vectors.
Expand Down
11 changes: 11 additions & 0 deletions +types/+util/+dynamictable/getTypeMap.m

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is used to find the data type of a column based on the columns first row. It is used when adding rows, to validate that the newly added row values match the type that already exists in each column.

If a column has no data elements yet, it is considered "untyped"

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
colVecData = DynamicTable.vectordata.get(colnm);
end

if isa(colVecData.data, 'types.untyped.DataPipe')
hasElements = ~any(size(colVecData.data) == 0);
else
hasElements = ~isempty(colVecData.data);
end
if ~hasElements
% A column whose rows are all empty has no element to take the type
% from.
continue;
end

if isa(colVecData.data, 'types.untyped.DataPipe')
colval = colVecData.data.load(1);
elseif istable(colVecData.data)
Expand Down
Loading