Skip to main content

Check shapes of input arrays easily

Project description

array-api-shape-check

CI Status Documentation Status Test coverage percentage

uv Ruff prek

PyPI Version Supported Python versions License


Documentation: https://array-api-shape-check.readthedocs.io

Source Code: https://github.com/34j/array-api-shape-check


Check shapes of input arrays easily.

Installation

Install this via pip (or your favourite package manager):

pip install array-api-shape-check

Usage

>>> from array_api_shape_check import check_shapes
>>> info = check_shapes("ij,*k*l,*li", (1, 4), (5, 6, 7), (1, 7, 3))
>>> info.all
((i:1->3, j:4), (*k:(5,), *l:(6, 7)), (*l:(1, 7)->(6, 7), i:3))
>>> info.unique
(i:3, j:4, *k:(5,), *l:(6, 7))

Internally check_shapes() calls parse_variable_ndim(), which determines the number of dimensions for variable subscripts by least squares. If this is successful, checks if each subscript is consistent, then finnaly raises error for all inconsistencies at once.

Diving into the details of the first item:

>>> item = info.all[0][0]
>>> item.name # the name of the subscript
'i'
>>> item.is_variable # whether the subscript is variable (starts with "*")
False
>>> item.shape_current # the current shape of the subscript
(1,)
>>> item.shape_broadcasted # the broadcasted shape of the subscript
(3,)

Not enough information to determine variable subscript ndims:

>>> import pytest
>>> from array_api_shape_check import InconsistentNdimErrorMultipleSolutions, InconsistentNdimErrorNoSolutions, InconsistentShapeError
>>> with pytest.raises(InconsistentNdimErrorMultipleSolutions, match="number of variables"):
...     check_shapes("*i*j", (1, 1))
>>> with pytest.raises(InconsistentNdimErrorMultipleSolutions, match="rank"):
...     check_shapes("*i*j,*i*j", (1, 1), (1, 1))

No solution to determine variable subscript ndims:

>>> with pytest.raises(InconsistentNdimErrorNoSolutions, match="residuals"):
...     check_shapes("*i,*i", (1, 1), (1, 1, 1))
>>> with pytest.raises(InconsistentNdimErrorNoSolutions, match="negative"):
...     check_shapes("*ij", ())

Does not match:

>>> with pytest.raises(InconsistentShapeError):
...     check_shapes("ij,*k*l,*li", (3, 4), (5, 6), (1, 7, 3))

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

Credits

Copier

This package was created with Copier and the browniebroke/pypackage-template project template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

array_api_shape_check-0.1.1.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

array_api_shape_check-0.1.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file array_api_shape_check-0.1.1.tar.gz.

File metadata

  • Download URL: array_api_shape_check-0.1.1.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for array_api_shape_check-0.1.1.tar.gz
Algorithm Hash digest
SHA256 55f2d363325bccbecc981a1e78f3318f3b51a2a0095e9089f296ddbbc80206f0
MD5 0b72c726738d595b976dffa9ca70b378
BLAKE2b-256 cdf03cceaa507039c5f312cc1a5f4de9150f74ad23eca0f32b83f73d932c8513

See more details on using hashes here.

Provenance

The following attestation bundles were made for array_api_shape_check-0.1.1.tar.gz:

Publisher: ci.yml on 34j/array-api-shape-check

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file array_api_shape_check-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for array_api_shape_check-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aab97277e6b5fcda1612f192efda023ebed3dae336a61b8259e5cbc2337a73c3
MD5 c8bfa31bebf399937164b320aca0d86b
BLAKE2b-256 ef918867af69be2cd279db54d607cb634ca5d64d40bd163fa864a8cc5512e762

See more details on using hashes here.

Provenance

The following attestation bundles were made for array_api_shape_check-0.1.1-py3-none-any.whl:

Publisher: ci.yml on 34j/array-api-shape-check

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page