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

Apply repo-review suggestions #559

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exclude: ^fixture/ # files with trailing whitespaces on purpose
ci:
autoupdate_commit_msg: "chore: update pre-commit hooks"
autofix_commit_msg: "style: pre-commit fixes"
Expand All @@ -6,9 +7,21 @@ default_stages: [commit, push]
default_language_version:
python: python3
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: check-yaml
- id: debug-statements

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.6.5'
rev: v0.6.5
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
- id: ruff-format

- repo: https://github.com/scientific-python/cookie
rev: 2024.04.23
hooks:
- id: sp-repo-review
2 changes: 1 addition & 1 deletion .pyup.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# autogenerated pyup.io config file
# autogenerated pyup.io config file
# see https://pyup.io/docs/configuration/ for all available options

schedule: every month
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Numcodecs
=========

Numcodecs is a Python package providing buffer compression and transformation
Numcodecs is a Python package providing buffer compression and transformation
codecs for use in data storage and communication applications.

.. image:: https://readthedocs.org/projects/numcodecs/badge/?version=latest
Expand Down
4 changes: 2 additions & 2 deletions adhoc/blosc_memleak_check.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import sys

import numcodecs
import numpy as np
from numpy.testing import assert_array_equal

import numcodecs

codec = numcodecs.Blosc()
data = np.arange(int(sys.argv[1]))
for i in range(int(sys.argv[2])):
for _ in range(int(sys.argv[2])):
enc = codec.encode(data)
dec = codec.decode(enc)
arr = np.frombuffer(dec, dtype=data.dtype)
Expand Down
3 changes: 2 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
# serve to show the default.


import sys
import os
import sys
from unittest.mock import Mock as MagicMock

import numcodecs


Expand Down
1 change: 1 addition & 0 deletions notebooks/benchmark_vlen.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
],
"source": [
"import numpy as np\n",
"\n",
"import numcodecs\n",
"\n",
"numcodecs.__version__"
Expand Down
2 changes: 1 addition & 1 deletion numcodecs/astype.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np

from .abc import Codec
from .compat import ndarray_copy, ensure_ndarray
from .compat import ensure_ndarray, ndarray_copy


class AsType(Codec):
Expand Down
1 change: 0 additions & 1 deletion numcodecs/bitround.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np


from .abc import Codec
from .compat import ensure_ndarray_like, ndarray_copy

Expand Down
2 changes: 1 addition & 1 deletion numcodecs/blosc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def decompress_partial(source, start, nitems, dest=None):
raise RuntimeError('error during blosc partial decompression: %d', ret)

return dest


# set the value of this variable to True or False to override the
# default adaptive behaviour
Expand Down
3 changes: 1 addition & 2 deletions numcodecs/bz2.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import bz2 as _bz2


from numcodecs.abc import Codec
from numcodecs.compat import ndarray_copy, ensure_contiguous_ndarray
from numcodecs.compat import ensure_contiguous_ndarray, ndarray_copy


class BZ2(Codec):
Expand Down
9 changes: 4 additions & 5 deletions numcodecs/categorize.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from .abc import Codec
from .compat import ensure_ndarray, ndarray_copy, ensure_text


import numpy as np

from .abc import Codec
from .compat import ensure_ndarray, ensure_text, ndarray_copy


class Categorize(Codec):
"""Filter encoding categorical string data as integers.
Expand Down Expand Up @@ -41,7 +40,7 @@ class Categorize(Codec):
def __init__(self, labels, dtype, astype='u1'):
self.dtype = np.dtype(dtype)
if self.dtype.kind not in 'UO':
raise TypeError("only unicode ('U') and object ('O') dtypes are " "supported")
raise TypeError("only unicode ('U') and object ('O') dtypes are supported")
self.labels = [ensure_text(label) for label in labels]
self.astype = np.dtype(astype)
if self.astype == np.dtype(object):
Expand Down
4 changes: 1 addition & 3 deletions numcodecs/checksum32.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import struct
import zlib


import numpy as np
import struct


from .abc import Codec
from .compat import ensure_contiguous_ndarray, ndarray_copy
Expand Down
1 change: 0 additions & 1 deletion numcodecs/delta.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np


from .abc import Codec
from .compat import ensure_ndarray, ndarray_copy

Expand Down
1 change: 0 additions & 1 deletion numcodecs/fixedscaleoffset.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np


from .abc import Codec
from .compat import ensure_ndarray, ndarray_copy

Expand Down
8 changes: 4 additions & 4 deletions numcodecs/jenkins.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ cpdef uint32_t jenkins_lookup3(const uint8_t[::1] _data, uint32_t initval=0):
if length == 12:
c += (<uint32_t>k[11]) << 24
length -= 1

if length == 11:
c += (<uint32_t>k[10]) << 16
length -= 1
Expand Down Expand Up @@ -231,7 +231,7 @@ cdef inline uint32_t _jenkins_lookup3_final(uint32_t a, uint32_t b, uint32_t c):
the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
is commonly produced by subtraction) look like a single 1-bit
difference.
* the base values were pseudorandom, all zero but one bit set, or
* the base values were pseudorandom, all zero but one bit set, or
all zero plus a counter that starts at zero.

These constants passed:
Expand Down Expand Up @@ -279,7 +279,7 @@ cdef inline (uint32_t, uint32_t, uint32_t) _jenkins_lookup3_mix(uint32_t a, uint
the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
is commonly produced by subtraction) look like a single 1-bit
difference.
* the base values were pseudorandom, all zero but one bit set, or
* the base values were pseudorandom, all zero but one bit set, or
all zero plus a counter that starts at zero.

Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
Expand All @@ -289,7 +289,7 @@ cdef inline (uint32_t, uint32_t, uint32_t) _jenkins_lookup3_mix(uint32_t a, uint
14 9 3 7 17 3
Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
for "differ" defined as + with a one-bit base and a two-bit delta. I
used http://burtleburtle.net/bob/hash/avalanche.html to choose
used http://burtleburtle.net/bob/hash/avalanche.html to choose
the operations, constants, and arrangements of the variables.

This does not achieve avalanche. There are input bits of (a,b,c)
Expand Down
2 changes: 0 additions & 2 deletions numcodecs/json.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import json as _json
import textwrap


import numpy as np


from .abc import Codec
from .compat import ensure_text

Expand Down
2 changes: 1 addition & 1 deletion numcodecs/lzma.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

if _lzma:
from .abc import Codec
from .compat import ndarray_copy, ensure_contiguous_ndarray
from .compat import ensure_contiguous_ndarray, ndarray_copy

# noinspection PyShadowingBuiltins
class LZMA(Codec):
Expand Down
3 changes: 1 addition & 2 deletions numcodecs/msgpacks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np
import msgpack

import numpy as np

from .abc import Codec
from .compat import ensure_contiguous_ndarray
Expand Down
4 changes: 2 additions & 2 deletions numcodecs/ndarray_like.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Optional, Protocol, runtime_checkable
from typing import Any, ClassVar, Optional, Protocol, runtime_checkable


class _CachedProtocolMeta(Protocol.__class__):
Expand All @@ -11,7 +11,7 @@ class _CachedProtocolMeta(Protocol.__class__):
isinstance checks using the object's class as the cache key.
"""

_instancecheck_cache: dict[tuple[type, type], bool] = {}
_instancecheck_cache: ClassVar[dict[tuple[type, type], bool]] = {}

def __instancecheck__(self, instance):
key = (self, instance.__class__)
Expand Down
1 change: 0 additions & 1 deletion numcodecs/packbits.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np


from .abc import Codec
from .compat import ensure_ndarray, ndarray_copy

Expand Down
4 changes: 2 additions & 2 deletions numcodecs/pcodec.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Optional, Literal
from typing import Literal, Optional

import numcodecs
import numcodecs.abc
from numcodecs.compat import ensure_contiguous_ndarray

try:
from pcodec import standalone, ChunkConfig, PagingSpec, ModeSpec
from pcodec import ChunkConfig, ModeSpec, PagingSpec, standalone
except ImportError: # pragma: no cover
standalone = None

Expand Down
6 changes: 2 additions & 4 deletions numcodecs/quantize.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import math


import numpy as np


from .abc import Codec
from .compat import ensure_ndarray, ndarray_copy

Expand Down Expand Up @@ -65,12 +63,12 @@ def encode(self, buf):

# apply scaling
precision = 10.0**-self.digits
exp = math.log(precision, 10)
exp = math.log10(precision)
if exp < 0:
exp = int(math.floor(exp))
else:
exp = int(math.ceil(exp))
bits = math.ceil(math.log(10.0**-exp, 2))
bits = math.ceil(math.log2(10.0**-exp))
scale = 2.0**bits
enc = np.around(scale * arr) / scale

Expand Down
2 changes: 1 addition & 1 deletion numcodecs/registry.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""The registry module provides some simple convenience functions to enable
applications to dynamically register and look-up codec classes."""

from importlib.metadata import entry_points
import logging
from importlib.metadata import entry_points

logger = logging.getLogger("numcodecs")
codec_registry = {}
Expand Down
5 changes: 3 additions & 2 deletions numcodecs/shuffle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
from .compat import ensure_contiguous_ndarray
from .abc import Codec

from ._shuffle import _doShuffle, _doUnshuffle
from .abc import Codec
from .compat import ensure_contiguous_ndarray


class Shuffle(Codec):
Expand Down
12 changes: 4 additions & 8 deletions numcodecs/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
import os
from glob import glob


import numpy as np
from numpy.testing import assert_array_almost_equal, assert_array_equal
import pytest
from numpy.testing import assert_array_almost_equal, assert_array_equal


# star import needed for repr tests so eval finds names
from numcodecs import * # noqa: F403
from numcodecs.compat import ensure_bytes, ensure_ndarray
from numcodecs.registry import get_codec

# star import needed for repr tests so eval finds names
from numcodecs import * # noqa


greetings = [
'¡Hola mundo!',
'Hej Världen!',
Expand Down Expand Up @@ -200,7 +196,7 @@ def assert_array_items_equal(res, arr):
# and values
arr = arr.ravel().tolist()
res = res.ravel().tolist()
for a, r in zip(arr, res):
for a, r in zip(arr, res, strict=True):
if isinstance(a, np.ndarray):
assert_array_equal(a, r)
elif a != a:
Expand Down
6 changes: 2 additions & 4 deletions numcodecs/tests/test_astype.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import numpy as np
from numpy.testing import assert_array_equal


from numcodecs.astype import AsType
from numcodecs.tests.common import (
check_encode_decode,
check_backwards_compatibility,
check_config,
check_encode_decode,
check_repr,
check_backwards_compatibility,
)


# mix of dtypes: integer, float
# mix of shapes: 1D, 2D, 3D
# mix of orders: C, F
Expand Down
7 changes: 2 additions & 5 deletions numcodecs/tests/test_base64.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import itertools


import numpy as np
import pytest


from numcodecs.base64 import Base64
from numcodecs.tests.common import (
check_encode_decode,
check_repr,
check_backwards_compatibility,
check_encode_decode,
check_err_decode_object_buffer,
check_err_encode_object_buffer,
check_repr,
)


codecs = [
Base64(),
]
Expand Down
1 change: 0 additions & 1 deletion numcodecs/tests/test_bitround.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np

import pytest

from numcodecs.bitround import BitRound, max_bits
Expand Down
Loading
Loading