Skip to content

Commit

Permalink
Fix getRow method for dynamic table (#648)
Browse files Browse the repository at this point in the history
* Permute nd arrays to get correct shape for table

* Update getRow.m

Ensure fix also works if a dynamic table is created raw by raw using the addRow method

* Add test for previous failure point

* Update getRow.m

Update function to also transpose 2D arrays (matrices) which are not vectors

* Add test for toTable method for n-dimensional vectordata
  • Loading branch information
ehennestad authored Jan 14, 2025
1 parent bb9acbf commit ead90dd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
26 changes: 26 additions & 0 deletions +tests/+unit/dynamicTableTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,32 @@ function testClearDynamicTableV2_1(testCase)
rehash();
end

function testToTableForNdVectorData(testCase)
import matlab.unittest.fixtures.SuppressedWarningsFixture
testCase.applyFixture(...
SuppressedWarningsFixture('NWB:DynamicTable:VectorDataAmbiguousSize'))

arrayLength = 5;
numTableRows = 3;
nDimsToTest = [2,3,4];

for nDims = nDimsToTest
vectorDataShape = repmat(arrayLength, 1, nDims-1);

dynamicTable = types.hdmf_common.DynamicTable( ...
'description', 'test table with n-dimensional VectorData', ...
'colnames', {'columnA', 'columnB'}, ...
'columnA', types.hdmf_common.VectorData('data', randi(10, [vectorDataShape, numTableRows])), ...
'columnB', types.hdmf_common.VectorData('data', randi(10, [vectorDataShape, numTableRows])), ...
'id', types.hdmf_common.ElementIdentifiers('data', (0:numTableRows-1)' ) );

T = dynamicTable.toTable();
testCase.verifyClass(T, 'table');
testCase.verifySize(T.columnA, [numTableRows, vectorDataShape])
end
end


% Non-test functions
function dtr_table = createDynamicTableWithTableRegionReferences()
% Create a dynamic table with two columns, where the data of each column is
Expand Down
11 changes: 6 additions & 5 deletions +types/+util/+dynamictable/getRow.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@
row{i} = select(DynamicTable, indexNames, ind);

if ~istable(row{i})
% transpose row vectors
if isrow(row{i})
row{i} = row{i} .';
% or permute arrays to place last dimension first
elseif ~ismatrix(row{i}) % i.e nd array where n >= 3
if iscolumn(row{i})
% keep column vectors as is
elseif isrow(row{i})
row{i} = row{i} .'; % transpose row vectors
elseif ndims(row{i}) >= 2 % i.e nd array where ndims >= 2
% permute arrays to place last dimension first
array_size = size(row{i});
num_rows = numel(ind);

Expand Down

0 comments on commit ead90dd

Please sign in to comment.