Skip to content

test(schema): use explicit shapes in testMultipleNullShapesDataset - #881

Merged
ehennestad merged 1 commit into
mainfrom
claude/windows-r2022a-shape-mismatch-4c9d73
Aug 27, 2026
Merged

test(schema): use explicit shapes in testMultipleNullShapesDataset#881
ehennestad merged 1 commit into
mainfrom
claude/windows-r2022a-shape-mismatch-4c9d73

Conversation

@ehennestad

Copy link
Copy Markdown
Collaborator

Motivation

Problem — The prepare release workflow runs the test suite across every MATLAB release crossed with every platform, 36 jobs in total. tests.unit.schema.MultipleShapesTest/testMultipleNullShapesDataset failed on exactly one of them, R2022a on windows-latest, and passed on the other 35. The failure reported a shape mismatch between the exported and the re-read dataset:

Actual size:
    50     1
Expected size:
     1    50

The test assigned 100 randomly shaped arrays to a MultiNullShapeDataset and exported whichever one the last iteration happened to produce. Occasionally that is a row vector, which cannot survive a round trip: MatNWB writes any vector as a rank-1 HDF5 dataset, and MATLAB reads a rank-1 dataset back as a column. MATLAB resets its random number generator only at session start, so the shape reached on iteration 100 depends on how many random numbers the tests before it drew. A release-and-platform combination that draws a different number of them lands on a different shape, which is why the failure was confined to a single matrix job and could not be reproduced from the others.

Solution — Assign an explicit list of shapes instead of random ones, and export a fixed matrix. Every matrix job now exercises the same shapes, so the test either passes everywhere or fails everywhere.

What changed

  • testMultipleNullShapesDataset assigns four named shapes — a scalar, a column vector, a row vector and a matrix — in place of 100 random ones.
  • The value handed to the round trip is a fixed 7x5 matrix rather than whatever the loop left behind.
  • The row vector case, which produced the original failure, is now covered on every run instead of appearing at random.
Implementation notes

The orientation loss is intended and is documented under dimension ordering: one-dimensional data has to be rank-1 in the file to stay readable by other NWB implementations, and MATLAB has no rank-1 array type to read it back into.

fid = H5F.create(tmp, 'H5F_ACC_TRUNC', 'H5P_DEFAULT', 'H5P_DEFAULT');
io.writeDataset(fid, '/rowvec', rand(1, 50));
H5F.close(fid);
written in MATLAB as : [1 50]
dataspace in the file: 50  (rank 1)
read back in MATLAB  : [50 1]

Assigning a row vector is still valid for the type — the schema shape is [[null], [null, null]] — so the loop keeps covering it. Only the exported value had to become orientation-stable.

Examples

The shape handed to the round trip

Standing in for the random numbers drawn by earlier tests with a variable offset into the default stream shows what the exported shape depended on. Offsets 82 and 284 both end on a row vector.

Before

randiMax = intmax('int8');
for i = 1:100
    if rand() > 0.5
        data = rand(randi(randiMax), 1);
    else
        data = rand(randi(randiMax), randi(randiMax));
    end
end
  stream offset    0 -> [3 88]
  stream offset   82 -> [1 113]
  stream offset  284 -> [1 44]

After

validShapes = {[1, 1], [23, 1], [1, 23], [7, 5]};
for iShape = 1:numel(validShapes)
    data = rand(validShapes{iShape});
end
data = rand(7, 5);
  stream offset    0 -> [7 5]
  stream offset   82 -> [7 5]
  stream offset  284 -> [7 5]

Sweeping 2001 offsets, 8 of them end on a row vector. That is the roughly 1-in-250 chance the test carried on every job.

How to test

nwbtest('Name', 'tests.unit.schema.MultipleShapesTest')

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

The test assigned 100 randomly shaped arrays to a MultiNullShapeDataset
and exported whichever one the last iteration produced. Roughly one
stream offset in 250 leaves a row vector behind, and a row vector does
not survive the round trip: matnwb writes any vector as a rank-1 HDF5
dataset, which MATLAB reads back as a column.

Because MATLAB resets its generator only at session start, the shape
reached on iteration 100 depends on how many numbers the tests before
it drew. That made the failure appear on a single release and platform
combination of the prepare release workflow matrix and nowhere else.

Assign four explicit shapes instead, covering a scalar, a column
vector, a row vector and a matrix, and export a fixed matrix. The test
exercises the same shapes on every matrix job.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@ehennestad
ehennestad requested a review from bendichter August 26, 2026 21:24
@ehennestad
ehennestad enabled auto-merge August 26, 2026 21:33
@codecov

codecov Bot commented Aug 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.27%. Comparing base (51e3e32) to head (8af3196).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #881   +/-   ##
=======================================
  Coverage   95.27%   95.27%           
=======================================
  Files         234      234           
  Lines        8311     8311           
=======================================
  Hits         7918     7918           
  Misses        393      393           

☔ 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 Aug 27, 2026
Merged via the queue into main with commit a750276 Aug 27, 2026
20 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