Repository navigation
fix(util): accept column vectors and array rows in create_indexed_column - #892
Merged
Merged
Conversation
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>
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
…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>
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>
bendichter
approved these changes
Oct 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Fixes
util.create_indexed_columnfor 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_columnbuilds 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.addRaggedArrayin #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 asVectorData.dataholds 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 ofVectorData.data, and rows are joined along that last dimension, which puts the ragged axis first on disk as the schema requires.What changed
[elementDims x totalElements]in MATLAB and(totalElements, ...)on disk, the layout the schema and HDMF'sDynamicTable.add_rowuse.types.untyped.ObjectViewthat are vectors are lists of elements of their class, as onmain: 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 theTimeSeriesrecorded during it with object rows, with[]for a trial that has none.NWB:CreateIndexedColumn:InconsistentElementType; rows with different element shapes, or struct rows with different fields, raiseNWB:CreateIndexedColumn:InconsistentElementShape; rows of any other type, and string, character, struct or object rows that are not vectors, raiseNWB:CreateIndexedColumn:InvalidRow.main.tests.unit.createIndexedColumnTestcovers numeric vectors in both orientations, empty rows, matrix and N-D rows, string, character, struct and object-reference rows, the errors, theDynamicTableRegionform, and a file round trip that checks the on-disk dimension order.Implementation notes
cell2mat(data)'andlength(data{i})are replaced by a localflattenRows. 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
findor from indexing a column of a matrix. Two units with 3 and 2 spike times: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:
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:
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.datastores 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;...].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:
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:
How to test
Or run
tests.unit.createIndexedColumnTest.Checklist
fix #XXwhereXXis the issue number?🤖 Generated with Claude Code