Skip to content

Fix stride computation for dimensions with shape 0 in ndarray - #68

Open
cgarling wants to merge 1 commit into
mainfrom
roman-l2-check
Open

Fix stride computation for dimensions with shape 0 in ndarray#68
cgarling wants to merge 1 commit into
mainfrom
roman-l2-check

Conversation

@cgarling

@cgarling cgarling commented Aug 6, 2026

Copy link
Copy Markdown
Member

Some STScI Roman L2 .asdf files produced with romanisim contain !core/ndarray nodes with a zero-length shape (e.g. the chisq/dumo arrays, shape [0, 0]) and no explicit strides key. ASDF.jl's implicit C-order stride formula (stride[i] = itemsize * prod(shape[i+1:])) collapses to zero for any outer dimension whose product includes a zero-length axis, which then failed the constructor's "strides must be positive" check.

NumPy avoids this by treating zero-length dimensions as length 1 only within the running product when computing default C-contiguous strides (PyArray_NewFromDescr). The Python asdf package relies on this. Here I reproduce that convention, and have verified it against NumPy's actual output for several representative shapes/dtypes.

I also relax the post-materialization stride sanity check in getindex: Julia's reshape/reinterpret don't preserve stride values along size-0 or size-1 axes (no adjacent elements to space apart), so the check now only compares strides for dimensions with more than one element.

I verified this patch on my local L2 .asdf files: all arrays materialize with correct shapes/dtypes, and pixel values match Python's asdf exactly (accounting for Julia's column-major vs. NumPy's row-major indexing convention).

I added regression tests in test/test-ndarray.jl covering the implicit stride computation for zero-length dimensions and materialization of a zero-size block-backed array. The rest of the tests also pass for me locally.

The comments are somewhat verbose so I'm happy if you want to cut them down to size.

@codecov

codecov Bot commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (fd82f70) to head (34f9a77).

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #68   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines          583       587    +4     
=========================================
+ Hits           583       587    +4     

☔ 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.

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.

1 participant