Skip to content

Numcodecs Delta filter throws AttributeError when astype is specifiedΒ #3256

@K-Meech

Description

@K-Meech

Zarr version

v3.1.0

Numcodecs version

v0.16.1

Python Version

3.13

Operating System

Windows

Installation

pip into a virtual environment

Description

Running the code snippet in the 'steps to reproduce' section below, throws the following error:

  File "C:\Users\kimme\miniforge3\envs\zarr-stock-only-2\Lib\site-packages\zarr\core\common.py", line 89, in run
    return await func(*item)
           ^^^^^^^^^^^^^^^^^
  File "C:\Users\kimme\miniforge3\envs\zarr-stock-only-2\Lib\site-packages\zarr\abc\codec.py", line 447, in wrap
    return await func(chunk, chunk_spec)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kimme\miniforge3\envs\zarr-stock-only-2\Lib\site-packages\zarr\codecs\bytes.py", line 80, in _decode_single
    dtype = chunk_spec.dtype.to_native_dtype()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'numpy.dtypes.Int32DType' object has no attribute 'to_native_dtype'

In previous zarr-python versions (e.g. 3.0.10), this passes without error. Removing the astype option to Delta also results in the code passing without error.

Steps to reproduce

Code snippet based on example from zarr-python docs with different dtype / astype options to the Delta filter. This throws an error:

# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "zarr@git+https://github.com/zarr-developers/zarr-python.git@main",
# ]
# ///
#
# This script automatically imports the development branch of zarr to check for issues

from numcodecs.zarr3 import Delta
import numpy as np
import zarr

filters = [Delta(dtype='<i4', astype='<i4')]
compressors = zarr.codecs.BloscCodec(cname='zstd', clevel=1, shuffle=zarr.codecs.BloscShuffle.shuffle)
data = np.arange(100000000, dtype='int32').reshape(10000, 10000)
z = zarr.create_array(
    store='data/delta-filter-v3.zarr', 
    shape=data.shape, 
    dtype=data.dtype, 
    chunks=(1000, 1000), 
    filters=filters, 
    compressors=compressors, 
    zarr_format=3
)
z[:] = 1

zarr_read = zarr.open("data/delta-filter-v3.zarr", zarr_format=3, mode="r+")
print(zarr_read[:])

The equivalent code for a v2 array passes without error:

# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "zarr@git+https://github.com/zarr-developers/zarr-python.git@main",
# ]
# ///
#
# This script automatically imports the development branch of zarr to check for issues

import numcodecs
import numpy as np
import zarr

filters = [numcodecs.Delta(dtype='<i4', astype='<i4')]
compressors = numcodecs.Blosc(cname="zstd", clevel=1, shuffle=1)
data = np.arange(100000000, dtype='int32').reshape(10000, 10000)
z = zarr.create_array(
    store='data/delta-filter-v2.zarr', 
    shape=data.shape, 
    dtype=data.dtype, 
    chunks=(1000, 1000), 
    filters=filters, 
    compressors=compressors, 
    zarr_format=2
)
z[:] = 1

zarr_read = zarr.open("data/delta-filter-v2.zarr", zarr_format=2, mode="r+")
print(zarr_read[:])

Additional output

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPotential issues with the zarr-python library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions