Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add normalize_shape since it is not in Zarr's public API #479

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions cubed/array_api/creation_functions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import math
from typing import TYPE_CHECKING, Iterable, List

from zarr.util import normalize_shape

from cubed.backend_array_api import namespace as nxp
from cubed.core import Plan, gensym
from cubed.core.ops import map_blocks
Expand All @@ -12,7 +10,7 @@
virtual_in_memory,
virtual_offsets,
)
from cubed.utils import to_chunksize
from cubed.utils import normalize_shape, to_chunksize
from cubed.vendor.dask.array.core import normalize_chunks

if TYPE_CHECKING:
Expand Down
3 changes: 1 addition & 2 deletions cubed/random.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import random as pyrandom

from numpy.random import Generator, Philox
from zarr.util import normalize_shape

from cubed.backend_array_api import namespace as nxp
from cubed.backend_array_api import numpy_array_to_backend_array
from cubed.core.ops import map_blocks
from cubed.utils import block_id_to_offset
from cubed.utils import block_id_to_offset, normalize_shape
from cubed.vendor.dask.array.core import normalize_chunks


Expand Down
11 changes: 11 additions & 0 deletions cubed/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
join_path,
map_nested,
memory_repr,
normalize_shape,
offset_to_block_id,
peak_measured_mem,
split_into,
Expand Down Expand Up @@ -170,3 +171,13 @@ def test_broadcast_trick():
a = nxp.ones((), dtype=nxp.int8)
b = broadcast_trick(nxp.ones)((), dtype=nxp.int8)
assert_array_equal(a, b)


def test_normalize_shape():
assert normalize_shape(2) == (2,)
assert normalize_shape((2,)) == (2,)
assert normalize_shape((2, 0)) == (2, 0)
assert normalize_shape((2, 3)) == (2, 3)

with pytest.raises(TypeError):
normalize_shape(None)
17 changes: 16 additions & 1 deletion cubed/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import collections
import itertools
import numbers
import platform
import sys
import sysconfig
Expand All @@ -12,7 +13,7 @@
from operator import add
from pathlib import Path
from posixpath import join
from typing import Dict, Tuple, Union
from typing import Dict, Tuple, Union, cast
from urllib.parse import quote, unquote, urlsplit, urlunsplit

import numpy as np
Expand Down Expand Up @@ -385,3 +386,17 @@ def _concatenate2(arrays, axes=None):
return ret
else:
return concatenate(arrays, axis=axes[0])


def normalize_shape(shape: Union[int, Tuple[int, ...], None]) -> Tuple[int, ...]:
"""Normalize a `shape` argument to a tuple of ints."""

if shape is None:
raise TypeError("shape is None")

if isinstance(shape, numbers.Integral):
shape = (int(shape),)

shape = cast(Tuple[int, ...], shape)
shape = tuple(int(s) for s in shape)
return shape
Loading