Skip to content

fix(dynamictable): fix addRow errors for ragged columns - #895

Merged
ehennestad merged 1 commit into
mainfrom
fix-add-row-ragged-columns
Oct 6, 2026
Merged

ehennestad merged 1 commit into
mainfrom
fix-add-row-ragged-columns

Conversation

@ehennestad

@ehennestad ehennestad commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Fixes three addRow problems with ragged columns: an unclear error on a table without column names, a failure when a row vector is added to a column stored as a column vector, and a doubly ragged row with one cell too many silently adding a third index level.

Background — The ragged-array how-to in #855 describes how addRow builds ragged columns. Checking that description against the code turned up the three problems below. All three are on main.

Problem — A user who builds a ragged column row by row with addRow runs into three problems.

  1. On a table without column names, addRow fails with an error that does not say what is wrong.
  2. A row given as a row vector fails to append to a column stored as a column vector, although the orientation of a list of values carries no meaning.
  3. A doubly ragged row wrapped in one cell too many adds a third index level to the column without any error, so the column no longer has the structure the user built.

Solution — addRow now reports the missing column names, accepts vector rows in either orientation, and rejects a value that needs more index levels than an indexed column has.

What changed

  • addRow on a table with empty colnames raises the existing NWB:DynamicTable:AddRow:NoColumns error, which says that colnames must be filled in.
  • addRow appends a row vector to a column stored as a column vector, and a column vector to a column stored as a row vector. This covers numeric rows and cell rows of text.
  • addRow raises NWB:DynamicTable:AddRow:TooManyIndexLevels when a value needs more index levels than a column that already has an index. A column without an index still becomes ragged when a row with several elements arrives, as before.
Implementation notes
  • The editability check in DynamicTableBase read obj.id.data before the colnames check ran. A table built without columns has no id object, so the call failed there. The check now treats a table without an id object as editable.
  • addRawData infers how many index levels a value needs from its nesting, and adds VectorIndex objects when the column has fewer. It now does so only for a column without an index. Adding a level to an indexed column regroups the rows already stored, each becoming a single sub-group of the new level.
  • add2MemData reshapes a vector value to the orientation of the stored vector before concatenating.

Examples

Table without column names

The table is created without colnames, and addRow adds a value to a column that is not listed.

dynamicTable = types.hdmf_common.DynamicTable('description', 'units');
dynamicTable.addRow('spike_times', [0.1; 0.2]);

Before — The call fails inside the editability check, and the message does not mention the column names.

Error: Dot indexing is not supported for variables of type double.

After — The call fails with the error written for this case, which says to fill in colnames.

Error: The `colnames` property of the DynamicTable needs to be populated with a cell array of column names before being able to add row data.

Vector rows in either orientation

The first row is a column vector of three spike times, which makes the stored data a column vector. The second row is a row vector of two spike times.

dynamicTable = types.hdmf_common.DynamicTable('description', 'units', 'colnames', {'spike_times'});
dynamicTable.addRow('spike_times', [0.1; 0.2; 0.3]);
dynamicTable.addRow('spike_times', [0.5, 0.6]);
fprintf('spike_times: %s\n', mat2str(dynamicTable.vectordata.get('spike_times').data'));
fprintf('spike_times_index: %s\n', mat2str(double(dynamicTable.vectordata.get('spike_times_index').data')));

Before — The second addRow call fails, because the row vector cannot be stacked under the stored column vector.

Error: Dimensions of arrays being concatenated are not consistent.

After — Both rows are stored. spike_times holds all five values, and spike_times_index shows that row 1 ends after value 3 and row 2 after value 5.

spike_times: [0.1 0.2 0.3 0.5 0.6]
spike_times_index: [3 5]

Row wrapped in an extra cell

The first row is a unit with two spikes, each a [40 x 3] matrix of waveforms on three electrodes, which makes waveforms a doubly ragged column. The second row wraps the second unit's cell of three spikes in one more cell.

dynamicTable = types.hdmf_common.DynamicTable('description', 'units', 'colnames', {'waveforms'});
dynamicTable.addRow('waveforms', {rand(40, 3), rand(40, 3)});
dynamicTable.addRow('waveforms', {{rand(40, 3), rand(40, 3), rand(40, 3)}});
fprintf('columns: %s\n', strjoin(sort(dynamicTable.vectordata.keys()), ', '));
fprintf('waveforms_index_index: %s\n', mat2str(double(dynamicTable.vectordata.get('waveforms_index_index').data')));
fprintf('waveforms_index_index_index: %s\n', mat2str(double(dynamicTable.vectordata.get('waveforms_index_index_index').data')));

Before — Both calls succeed, but the result is wrong. addRow adds a third index level, waveforms_index_index_index, with one entry per row. Each row now holds a single group that contains the unit's spikes, so the column is triply ragged.

columns: waveforms, waveforms_index, waveforms_index_index, waveforms_index_index_index
waveforms_index_index: [2 5]
waveforms_index_index_index: [1 2]

After — The second addRow call fails and points to the extra cell array. The column keeps its two index levels.

Error: The value for column `waveforms` needs 3 index levels, but the column has 2. Check that the value is not wrapped in an extra cell array.

How to test

Run the three examples above, or the new unit tests:

runtests('tests.unit.dynamicTableAddRowTest')

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

Three problems when adding rows to ragged columns:

- addRow on a table with empty colnames failed with "Dot indexing is
  not supported" instead of the NoColumns error. The editability check
  read the id before the colnames check ran, and such a table has no id
  object yet. The check now treats a table without an id as editable.
- A vector row whose orientation differed from the stored vector failed
  to concatenate. A vector row is a list of scalar elements, so addRow
  now reshapes it to the stored orientation.
- A value wrapped in one cell too many added an index level to a column
  that already had an index, regrouping its existing rows without any
  error. addRow now rejects a value that needs more index levels than
  an indexed column has. A column without an index still becomes ragged
  when a multi-element row arrives.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@ehennestad
ehennestad marked this pull request as ready for review September 29, 2026 21:28
@codecov

codecov Bot commented Sep 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.29%. Comparing base (1565936) to head (d90ae59).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #895   +/-   ##
=======================================
  Coverage   95.29%   95.29%           
=======================================
  Files         239      239           
  Lines        8795     8805   +10     
=======================================
+ Hits         8381     8391   +10     
  Misses        414      414           

☔ 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 this pull request to the merge queue Oct 6, 2026
Merged via the queue into main with commit b5c5b08 Oct 6, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants