Skip to content

fix(dynamictable): add an empty ragged row without advancing the index - #908

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

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

Conversation

@ehennestad

@ehennestad ehennestad commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Fixes a bug where addRow silently writes a wrong index when a ragged row has no elements: rows of 3, 0 and 2 elements give [3 4 6] instead of [3 3 5].

Background — A ragged column has a different number of elements in each row. All the elements are stored in one dataset, and an index holds the position where each row ends. Rows of 3, 0 and 2 elements give the index [3 3 5]: the empty row ends where the row before it ends.

Problem — addRow writes a wrong index when a row has no elements, for example a unit without spikes. Adding rows of 3, 0 and 2 spike times gives the index [3 4 6] instead of [3 3 5]. addRow counts the empty value as one element, so the index moves forward by one although nothing is added to the data. Every later row then points one element too far: the empty row reads back with 1 element, and the last row points past the end of the data. Nothing reports it when the rows are added and the export succeeds. The error only appears when a row is read.

Solution — addRow reads a value without elements as a row with zero elements. The index repeats its previous value for that row, and the rows after it are read correctly.

This is one of several independent fixes for DynamicTable helper bugs found in an audit of +types/+util/+dynamictable. #895 fixes other addRow errors for ragged columns and does not cover this case.

What changed

  • addRow with [], or with {} for a text column, adds an empty row to a ragged column.
  • An empty row can be the first row of a column. The column gets an index that starts at 0. Before, no index was created and the next addRow failed, because the table had an id without a matching row in the column.
  • An empty row added to a column with one value per row turns it into a ragged column, as a row with several values does. Before, the id was added without a row in the column, and the next addRow failed.
  • An empty character vector is still one element, an empty text value.
  • PyNWB reads the exported table with an empty list for the empty row.
Implementation notes

In addRawData.m, getNestedDataDepth gives a value without elements the nesting depth of a ragged row, so the column gets an index when it has none and the value is no longer wrapped as a single element. nestedAdd reports zero added elements for an empty value at the data level without touching the data. getTypeMap skips a column that has no element to take the type from, which is the state of a table after an empty first row.

On a doubly ragged column, [] adds a row with one empty sub-row.

Examples

Empty row between two rows

Three units are added with 3, 0 and 2 spike times, and each row is read back.

units = types.hdmf_common.DynamicTable('description', 'units', 'colnames', {'spike_times'});
units.addRow('spike_times', [1 2 3]);
units.addRow('spike_times', []);
units.addRow('spike_times', [4 5]);

fprintf('spike_times: %s\n', mat2str(units.vectordata.get('spike_times').data));
fprintf('index: %s\n', mat2str(double(units.vectordata.get('spike_times_index').data')));
for iRow = 1:3
    try
        row = units.getRow(iRow);
        fprintf('row %d: %d elements\n', iRow, numel(row.spike_times{1}));
    catch ME
        fprintf('row %d: Error: %s\n', iRow, ME.message);
    end
end

Before — The index is silently wrong. The column holds the 5 spike times, but the index ends at 6. The second row reads back with 1 element, which is the first spike of the third unit, and the third row fails.

spike_times: [1 2 3 4 5]
index: [3 4 6]
row 1: 3 elements
row 2: 1 elements
row 3: Error: Index in position 2 exceeds array bounds. Index must not exceed 5.

After — The index repeats 3 for the empty row and ends at 5, the number of spike times. Each row reads back with the number of elements it was given.

spike_times: [1 2 3 4 5]
index: [3 3 5]
row 1: 3 elements
row 2: 0 elements
row 3: 2 elements

Empty row as the first row

The first unit has no spike times, and the second has 2.

units = types.hdmf_common.DynamicTable('description', 'units', 'colnames', {'spike_times'});
units.addRow('spike_times', []);
try
    units.addRow('spike_times', [4 5]);
    fprintf('index: %s\n', mat2str(double(units.vectordata.get('spike_times_index').data')));
catch ME
    fprintf('Error: %s\n', ME.message);
end
fprintf('id: %s\n', mat2str(double(units.id.data')));

Before — The first addRow adds an id but no row to the column, so the second addRow fails. The last line shows the one id that was added.

Error: Special column `id` of DynamicTable needs to match the detected height of 0. Found 1 IDs.
id: 0

After — The first row ends at element 0 and the second at element 2. The table has two ids.

index: [0 2]
id: [0 1]

How to test

Run the new tests:

nwbtest('ProcedureName', 'testAddEmptyRow*', 'ProduceCodeCoverage', false)

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

addRow treated a value without elements as a single element. On a
column that has an index, the index advanced by one although nothing
was added, so every later row pointed at the wrong elements. On a
column without an index, nothing was added at all while the id grew.

Give an empty value the nesting depth of a ragged row, so the column
gets an index when it has none, and let the data level report that no
element was added. getTypeMap skips a column that has no element to
take the type from, which a table has after an empty first row.

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

codecov Bot commented Sep 30, 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 (3dd695c).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #908      +/-   ##
==========================================
+ Coverage   95.29%   95.30%   +0.01%     
==========================================
  Files         239      239              
  Lines        8795     8804       +9     
==========================================
+ Hits         8381     8391      +10     
+ 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 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

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"

@ehennestad
ehennestad added this pull request to the merge queue Oct 6, 2026
Merged via the queue into main with commit bed11d7 Oct 6, 2026
22 of 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