Skip to content

Commit

Permalink
Sync with zarr 3 beta (#597)
Browse files Browse the repository at this point in the history
* Sync with zarr 3 beta

* Update zarr version in ci

* dont install zarr python 3 in workflows running 3.10
  • Loading branch information
mpiannucci authored Oct 16, 2024
1 parent b75e41e commit a4cf7ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ jobs:
python -m pip install -v -e .[test,test_extras,msgpack,pcodec]
- name: Install zarr-python
if : matrix.python-version != '3.10'
shell: "bash -l {0}"
run: |
conda activate env
python -m pip install zarr==3.0.0a0
python -m pip install zarr==3.0.0b0
# This is used to test with zfpy, which does not yet support numpy 2.0
- name: Install older numpy and zfpy
Expand Down
23 changes: 12 additions & 11 deletions numcodecs/zarr3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import asyncio
from dataclasses import dataclass, replace
from functools import cached_property
import math
Expand All @@ -17,15 +18,15 @@
raise ImportError("zarr 3.0.0 or later is required to use the numcodecs zarr integration.")

from zarr.abc.codec import ArrayArrayCodec, BytesBytesCodec, ArrayBytesCodec
from zarr.buffer import NDBuffer, Buffer, BufferPrototype, as_numpy_array_wrapper
from zarr.array_spec import ArraySpec
from zarr.common import (
from zarr.core.buffer import NDBuffer, Buffer, BufferPrototype
from zarr.core.buffer.cpu import as_numpy_array_wrapper
from zarr.core.array_spec import ArraySpec
from zarr.core.common import (
JSON,
parse_named_configuration,
product,
to_thread,
)
from zarr.metadata import ArrayMetadata
from zarr.core.metadata import ArrayMetadata


CODEC_PREFIX = "numcodecs."
Expand Down Expand Up @@ -90,7 +91,7 @@ def __init__(self, *, codec_id: str, codec_config: dict[str, JSON]) -> None:
super().__init__(codec_id=codec_id, codec_config=codec_config)

async def _decode_single(self, chunk_bytes: Buffer, chunk_spec: ArraySpec) -> Buffer:
return await to_thread(
return await asyncio.to_thread(
as_numpy_array_wrapper,
self._codec.decode,
chunk_bytes,
Expand All @@ -104,7 +105,7 @@ def _encode(self, chunk_bytes: Buffer, prototype: BufferPrototype) -> Buffer:
return prototype.buffer.from_bytes(encoded)

async def _encode_single(self, chunk_bytes: Buffer, chunk_spec: ArraySpec) -> Buffer:
return await to_thread(self._encode, chunk_bytes, chunk_spec.prototype)
return await asyncio.to_thread(self._encode, chunk_bytes, chunk_spec.prototype)


class NumcodecsArrayArrayCodec(NumcodecsCodec, ArrayArrayCodec):
Expand All @@ -113,12 +114,12 @@ def __init__(self, *, codec_id: str, codec_config: dict[str, JSON]) -> None:

async def _decode_single(self, chunk_array: NDBuffer, chunk_spec: ArraySpec) -> NDBuffer:
chunk_ndarray = chunk_array.as_ndarray_like()
out = await to_thread(self._codec.decode, chunk_ndarray)
out = await asyncio.to_thread(self._codec.decode, chunk_ndarray)
return chunk_spec.prototype.nd_buffer.from_ndarray_like(out.reshape(chunk_spec.shape))

async def _encode_single(self, chunk_array: NDBuffer, chunk_spec: ArraySpec) -> NDBuffer:
chunk_ndarray = chunk_array.as_ndarray_like()
out = await to_thread(self._codec.encode, chunk_ndarray)
out = await asyncio.to_thread(self._codec.encode, chunk_ndarray)
return chunk_spec.prototype.nd_buffer.from_ndarray_like(out)


Expand All @@ -128,12 +129,12 @@ def __init__(self, *, codec_id: str, codec_config: dict[str, JSON]) -> None:

async def _decode_single(self, chunk_buffer: Buffer, chunk_spec: ArraySpec) -> NDBuffer:
chunk_bytes = chunk_buffer.to_bytes()
out = await to_thread(self._codec.decode, chunk_bytes)
out = await asyncio.to_thread(self._codec.decode, chunk_bytes)
return chunk_spec.prototype.nd_buffer.from_ndarray_like(out.reshape(chunk_spec.shape))

async def _encode_single(self, chunk_ndbuffer: NDBuffer, chunk_spec: ArraySpec) -> Buffer:
chunk_ndarray = chunk_ndbuffer.as_ndarray_like()
out = await to_thread(self._codec.encode, chunk_ndarray)
out = await asyncio.to_thread(self._codec.encode, chunk_ndarray)
return chunk_spec.prototype.buffer.from_bytes(out)


Expand Down

0 comments on commit a4cf7ad

Please sign in to comment.