Skip to content

fix(util): accept column vectors and array rows in create_indexed_column - #892

Merged
ehennestad merged 4 commits into
mainfrom
fix-create-indexed-column-shapes
Oct 6, 2026
Merged

ehennestad merged 4 commits into
mainfrom
fix-create-indexed-column-shapes

Conversation

@ehennestad

@ehennestad ehennestad commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Fixes util.create_indexed_column for rows that are not row vectors: column-vector rows such as {[1;2;3], [4;5]} fail with a concatenation error, and matrix rows are stored transposed with a wrong index, with no error.

Background — A ragged column has a different number of elements in each row. util.create_indexed_column builds one from a cell array with one cell per row: {[1 2 3], [4 5]} gives the data [1;2;3;4;5] and the index [3 5], the position where each row ends. addRaggedArray in #855 builds on this function.

Problem — The function only handles rows that are given as row vectors. With column vectors, {[1;2;3], [4;5]} gives the error "Dimensions of arrays being concatenated are not consistent" instead of the data [1;2;3;4;5] and the index [3 5]. The function joins the rows side by side and transposes the result, and column vectors of different lengths cannot be joined side by side. A row given as a matrix, with one element per column as VectorData.data holds it, goes through the same steps without an error and comes out wrong: two rows of two 4-sample elements each are stored transposed, with the index [4 8] instead of [2 4]. The index then points past the end of the data, nothing reports it, and the file is wrong.

Solution — A vector row is read as a list of elements in either orientation, so {[1;2;3], [4;5]} gives the same data and index as {[1 2 3], [4 5]}. A matrix or N-D row is read as [elementDims x nElements], the orientation of VectorData.data, and rows are joined along that last dimension, which puts the ragged axis first on disk as the schema requires.

What changed

  • Column-vector rows work and give the same result as row-vector rows.
  • Matrix and N-D rows produce [elementDims x totalElements] in MATLAB and (totalElements, ...) on disk, the layout the schema and HDMF's DynamicTable.add_row use.
  • String arrays, character vectors, struct arrays and arrays of objects such as types.untyped.ObjectView that are vectors are lists of elements of their class, as on main: a string column, a character column with one element per character, a struct column or an object column. Struct rows are concatenated with their fields in one order. A trials table links each trial to the TimeSeries recorded during it with object rows, with [] for a trial that has none.
  • Rows whose elements differ in type raise NWB:CreateIndexedColumn:InconsistentElementType; rows with different element shapes, or struct rows with different fields, raise NWB:CreateIndexedColumn:InconsistentElementShape; rows of any other type, and string, character, struct or object rows that are not vectors, raise NWB:CreateIndexedColumn:InvalidRow.
  • Numeric, logical, string, character, struct and object vector rows give the same column as on main.
  • New tests.unit.createIndexedColumnTest covers numeric vectors in both orientations, empty rows, matrix and N-D rows, string, character, struct and object-reference rows, the errors, the DynamicTableRegion form, and a file round trip that checks the on-disk dimension order.
Implementation notes

cell2mat(data)' and length(data{i}) are replaced by a local flattenRows. The element shape is taken from the first numeric row that is not a vector; when every row is a vector the elements are scalars and the output is a column, as before. A row holding a single element may omit its trailing dimension of 1 ([k x 1] for k-sample elements), because MATLAB drops it. The signature (data, description, table) is unchanged.

Examples

Column vectors

Spike times often come as column vectors, for example from find or from indexing a column of a matrix. Two units with 3 and 2 spike times:

[vector, index] = util.create_indexed_column({[1;2;3], [4;5]});
fprintf('data size: %s\n', strjoin(string(size(vector.data)), 'x'));
fprintf('index: %s\n', mat2str(index.data.'));

Before — The rows are joined side by side, which requires them to have the same height. Column vectors of different lengths cannot be joined that way, so the call fails before anything is built:

Error: Dimensions of arrays being concatenated are not consistent.

After — a vector row is a list of scalars whatever its orientation. The five values are stacked into one column, and the index marks where each row ends: the first unit owns values 1 to 3, the second owns 4 and 5:

data size: 5x1
index: [3 5]

Matrix rows, two 4-sample elements per row

Each row holds two elements of four samples each, one element per column, which is how VectorData.data stores them. The values are chosen so that each element can be recognised in the output: [1;2;3;4], [5;6;7;8], [101;...], [105;...].

row1 = reshape(1:8, 4, 2);
row2 = 100 + reshape(1:8, 4, 2);
[vector, index] = util.create_indexed_column({row1, row2});
disp(vector.data)
fprintf('index: %s\n', mat2str(index.data.'));

Before — no error is raised, but the result is wrong in two ways. The two rows are joined side by side into a 4-by-4 matrix, and the transpose then turns every element into a row, so the stored data is the transpose of the input. Each row is counted by its larger dimension, 4, so the index claims 4 and then 8 elements although the data has only four columns. A reader that follows the index reads past the end of the data:

     1     2     3     4
     5     6     7     8
   101   102   103   104
   105   106   107   108
index: [4 8]

After — the two rows are concatenated along the last dimension, so every column is one element exactly as given. The index counts 2 elements in the first row and 2 more in the second:

     1     5   101   105
     2     6   102   106
     3     7   103   107
     4     8   104   108
index: [2 4]

How to test

row1 = reshape(1:8, 4, 2);
row2 = 100 + reshape(1:12, 4, 3);
[vector, index] = util.create_indexed_column({row1, row2});
assert(isequal(size(vector.data), [4 5]) && isequal(index.data, uint64([2; 5])))
[vector, index] = util.create_indexed_column({[1;2;3], [4;5]});
assert(isequal(vector.data, [1;2;3;4;5]) && isequal(index.data, uint64([3; 5])))
[vector, index] = util.create_indexed_column({["a" "b"], "c"});
assert(isequal(vector.data, ["a"; "b"; "c"]) && isequal(index.data, uint64([2; 3])))

Or run tests.unit.createIndexedColumnTest.

Checklist

  • Have you ensured the PR description clearly describes the problem and solutions?
  • Have you checked to ensure that there aren't other open or previously closed Pull Requests for the same change?
  • If this PR fixes an issue, is the first line of the PR description fix #XX where XX is the issue number?

🤖 Generated with Claude Code

create_indexed_column built the column with cell2mat(data)' and counted
elements with length(). That only works for row vectors: column vectors
raised a concatenation error, and matrix rows were joined side by side,
transposed, and counted along the wrong axis, producing a file whose
index does not match its data.

Rows now follow the orientation of VectorData.data. A vector row is a
list of scalars, whatever its orientation, and the result is a column
vector as before. An array row is [elementDims x nElements] with the
ragged axis last; rows are concatenated along that axis, so the data
is [elementDims x totalElements] in MATLAB and (totalElements, ...) on
disk, the order the schema and HDMF use. Element shapes are checked
across rows, and a single element may omit its trailing dimension of 1.

Add createIndexedColumnTest covering vectors, empty rows, matrix and
N-D rows, shape and type errors, the DynamicTableRegion form, and a
file round trip that checks the on-disk dimension order.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@ehennestad
ehennestad added this pull request to stack #893 September 28, 2026 18:38
@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.30%. Comparing base (1565936) to head (b411a38).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #892      +/-   ##
==========================================
+ Coverage   95.29%   95.30%   +0.01%     
==========================================
  Files         239      239              
  Lines        8795     8798       +3     
==========================================
+ Hits         8381     8385       +4     
+ Misses        414      413       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ehennestad ehennestad added the category: bug errors in the code or code behavior label Sep 29, 2026
@ehennestad
ehennestad removed the request for review from bendichter September 29, 2026 19:05
@ehennestad
ehennestad marked this pull request as draft September 29, 2026 19:05
…olumn

create_indexed_column on main joins rows with cell2mat, which also
accepts string arrays, character vectors and struct arrays: each is a
list of elements, and the column keeps the class. The previous commit
accepted numeric and logical rows only, which rejected these rows.

A string array, character vector or struct array that is a vector is
now a list of elements of that class, as on main: a string column, a
character column with one element per character, or a struct column.
Rows holding elements of different types raise
NWB:CreateIndexedColumn:InconsistentElementType, and struct rows with
different fields raise NWB:CreateIndexedColumn:InconsistentElementShape.
Struct rows are concatenated with their fields in one order.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
ehennestad and others added 2 commits October 1, 2026 15:12
A row may be a vector of objects such as types.untyped.ObjectView, with
[] for a row that references nothing. This is how a trials table links
each trial to the TimeSeries recorded during it, and main accepts it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…_column

Every numeric or logical row counted as the same element type, so rows of
different classes were concatenated and the values of one class were
converted to the other: {int32([1 2]), 3.5} gave int32([1; 2; 4]). Main
raised an error for such rows through cell2mat. The type check now uses
the class of each row, so mixed classes raise InconsistentElementType
again, and rows of one integer class keep that class.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@ehennestad
ehennestad added this pull request to the merge queue Oct 6, 2026
Merged via the queue into main with commit ff0d00e Oct 6, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: bug errors in the code or code behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants