Skip to content

Add moveaxis and fix permute_dims for zero-size arrays - #804

Open
andyrochi wants to merge 5 commits into
Blosc:mainfrom
andyrochi:feat/moveaxis-466
Open

andyrochi wants to merge 5 commits into
Blosc:mainfrom
andyrochi:feat/moveaxis-466

Conversation

@andyrochi

@andyrochi andyrochi commented Nov 17, 2026 •

Copy link
Copy Markdown

Part of #466

Adds blosc2.moveaxis, which the 2024.12 Array API standard requires (blosc2 declares __array_api_version__ = "2024.12"). It's on the list of missing functions in this comment on #466, which test_has_names checks for. It computes the new axis order the same way np.moveaxis does, then delegates to permute_dims. It accepts the same **kwargs as permute_dims (e.g. urlpath).

Note: like permute_dims, moveaxis returns a copy. A view would need axis-reordering views (#442).

This PR also fixes a bug in permute_dims on arrays with a zero-length dimension. moveaxis depends on it. permute_dims reused the input's chunks, which contain 0 for these arrays, and blosc2.empty rejects those. Reproduce this on main:

import blosc2
blosc2.permute_dims(blosc2.zeros((0, 0)), (0, 1))
# ValueError: shapes with 0 dims are not supported

Array API conformance

Run with array-api-tests (ARRAY_API_TESTS_VERSION=2024.12):

Test main This PR
test_manipulation_functions.py::test_permute_dims fails (zero-size shapes) passes
test_manipulation_functions.py::test_moveaxis fails (moveaxis is not defined) passes
test_has_names.py (moveaxis) fails (name missing) passes

Questions

  1. I'm putting moveaxis in linalg.py for now since permute_dims lives there and this is just a wrapper for that. But ndarray.py also seems like a reasonable place for it to live, since the Array API groups moveaxis with the manipulation functions (expand_dims, squeeze, concat, …). Let me know if it's better to move this to ndarray.py.
  2. Should moveaxis also be added to utils.linalg_funcs so it works in lazy-expression strings? I left it out to keep this PR minimal.

AI disclosure: After understanding the requirements, I used AI agents to plan and implement the changes. I reviewed every step and ran the test suite and the array-api conformance checks myself.

permute_dims reused the input chunks, which contain 0 for zero-size arrays and are rejected by blosc2.empty. Return an empty result directly instead.
Required by the 2024.12 Array API standard. Implemented on top of permute_dims.

Refs Blosc#466
@andyrochi andyrochi changed the title feat: add moveaxis Add moveaxis and fix permute_dims for zero-size arrays Nov 18, 2026
@andyrochi
andyrochi marked this pull request as ready for review November 18, 2026 00:06
@andyrochi andyrochi mentioned this pull request Nov 18, 2026
3 of 18 tasks

@FrancescAlted FrancescAlted left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR and for catching the zero-size permute_dims bug as well! The implementation looks clean overall. Before merging, could you address a couple of minor items?

  1. Fix pre-commit.ci / Ruff check:
    • In src/blosc2/linalg.py (_normalize_moveaxis_axes), isinstance(axes, (int, np.integer)) violates Ruff UP038 — please use isinstance(axes, int | np.integer) and run ruff check / ruff format (or pre-commit run) across the modified files so pre-commit.ci passes.
  2. Validate integer elements in axis sequences:
    • _normalize_moveaxis_axes currently doesn't check element types when axes is a sequence, so e.g. moveaxis(a, (0.0,), (1,)) silently succeeds and moveaxis(a, (0.5,), (0,)) raises a ValueError inside permute_dims instead of TypeError. Coercing each element with operator.index(ax) handles this cleanly across NumPy 1.26 and 2.x.
  3. Additional test cases (nice to have):
    • A zero-size moveaxis case (e.g. shape (2, 0, 3)), a 0-D case (shape=(), source=(), destination=()), a **kwargs pass-through check (e.g. chunks or urlpath), and a non-integer sequence element ((0.5,)) raising TypeError.

Regarding your questions:

  1. linalg.py vs ndarray.py: Keeping moveaxis in linalg.py right next to permute_dims and matrix_transpose (and re-exporting at top-level blosc2.moveaxis) fits the existing layout well.
  2. utils.linalg_funcs (lazy expressions): Leaving it out of this PR to keep it minimal is totally fine. If added in a follow-up, it will also need utils.linalg_shape and expression_graph.py (_SHAPES and _KEYWORDS for source/destination in safe mode).

Non-integer axes raised ValueError deep inside permute_dims or were silently accepted for 1-D input. Coerce each axis with operator.index so they raise TypeError, as in NumPy.
Add the blank line ruff format expects after the imports. This was already failing pre-commit.ci on main.
@andyrochi

Copy link
Copy Markdown
Author

Hi @FrancescAlted, thanks! I made the following changes according to your feedback:

  1. I've switched it to int | np.integer. The pre-commit.ci failure seems to come from tests/test_native_graph_sources.py on main, and I've fixed that in a separate commit as well.
  2. I've followed your suggestions and added validation for axis sequences and more test cases. Please take a look.
  3. I'll keep moveaxis in linalg.py and I'm interested in working on a lazy-expression follow-up!

This branch has not been deployed

No deployments
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