Skip to content

Commit

Permalink
Merge pull request #266 from DimitriPapadopoulos/ruff
Browse files Browse the repository at this point in the history
Apply assorted ruff rules
  • Loading branch information
FrancescAlted authored Sep 30, 2024
2 parents 21dc538 + 1ec0c45 commit 7f67272
Show file tree
Hide file tree
Showing 23 changed files with 357 additions and 208 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ repos:
hooks:
- id: ruff
- id: ruff-format
exclude: ^bench/
2 changes: 1 addition & 1 deletion doc/getting_started/tutorials/04.reductions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
"outputs": [],
"source": [
"def plot_meas(meas_np, meas, chunks):\n",
" fig, ax = plt.subplots()\n",
" _fig, ax = plt.subplots()\n",
"\n",
" # Define the groups and bar width\n",
" groups = meas_np[\"time\"].keys()\n",
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,15 @@ extend-select = [
"UP",
]
ignore = [
"B028",
"PT006",
"PT004", # deprecated
"PT011",
"RET505",
"RET508",
"SIM108",
"UP038", # https://github.com/astral-sh/ruff/issues/7871
]

[tool.ruff.lint.extend-per-file-ignores]
"tests/**" = ["F841"]
4 changes: 2 additions & 2 deletions src/blosc2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class Tuner(Enum):
nthreads -= nthreads // 8

# This import must be before ndarray and schunk
from .storage import (
from .storage import ( # noqa: I001
CParams,
cparams_dflts,
DParams,
Expand All @@ -220,10 +220,10 @@ class Tuner(Enum):
empty,
frombuffer,
full,
get_slice_nchunks,
nans,
uninit,
zeros,
get_slice_nchunks,
)

from .c2array import c2context, C2Array, URLPath
Expand Down
78 changes: 48 additions & 30 deletions src/blosc2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
if TYPE_CHECKING:
from collections.abc import Callable

import tensorflow
import torch


Expand Down Expand Up @@ -132,8 +133,9 @@ def compress(
return blosc2_ext.compress(src, typesize, clevel, filter, codec)


def decompress(src: object, dst: object | bytearray = None,
as_bytearray: bool = False) -> str | bytes | bytearray | None:
def decompress(
src: object, dst: object | bytearray = None, as_bytearray: bool = False
) -> str | bytes | bytearray | None:
"""Decompresses a bytes-like compressed object.
Parameters
Expand Down Expand Up @@ -201,8 +203,12 @@ def decompress(src: object, dst: object | bytearray = None,
return blosc2_ext.decompress(src, dst, as_bytearray)


def pack(obj: object, clevel: int = 9, filter: blosc2.Filter = blosc2.Filter.SHUFFLE,
codec: blosc2.Codec = blosc2.Codec.BLOSCLZ) -> str | bytes:
def pack(
obj: object,
clevel: int = 9,
filter: blosc2.Filter = blosc2.Filter.SHUFFLE,
codec: blosc2.Codec = blosc2.Codec.BLOSCLZ,
) -> str | bytes:
"""Pack (compress) a Python object.
Parameters
Expand Down Expand Up @@ -320,8 +326,12 @@ def unpack(packed_object: str | bytes, **kwargs: dict) -> object:
return obj


def pack_array(arr: np.ndarray, clevel: int = 9, filter: blosc2.Filter = blosc2.Filter.SHUFFLE,
codec: blosc2.Codec = blosc2.Codec.BLOSCLZ) -> str | bytes:
def pack_array(
arr: np.ndarray,
clevel: int = 9,
filter: blosc2.Filter = blosc2.Filter.SHUFFLE,
codec: blosc2.Codec = blosc2.Codec.BLOSCLZ,
) -> str | bytes:
"""Pack (compress) a NumPy array. It is equivalent to the pack function.
Parameters
Expand Down Expand Up @@ -590,8 +600,9 @@ def load_array(urlpath: str, dparams: dict = None) -> np.ndarray:
return load_tensor(urlpath, dparams=dparams)


def pack_tensor(tensor: tensorflow.Tensor | torch.Tensor | np.ndarray, chunksize: int = None,
**kwargs: dict) -> bytes | int:
def pack_tensor(
tensor: tensorflow.Tensor | torch.Tensor | np.ndarray, chunksize: int = None, **kwargs: dict
) -> bytes | int:
"""Pack (compress) a TensorFlow or PyTorch tensor or a NumPy array.
Parameters
Expand Down Expand Up @@ -718,8 +729,12 @@ def unpack_tensor(cframe: bytes) -> tensorflow.Tensor | torch.Tensor | np.ndarra
return _unpack_tensor(schunk)


def save_tensor(tensor: tensorflow.Tensor | torch.Tensor | np.ndarray, urlpath: str, chunksize: int = None,
**kwargs: dict) -> int:
def save_tensor(
tensor: tensorflow.Tensor | torch.Tensor | np.ndarray,
urlpath: str,
chunksize: int = None,
**kwargs: dict,
) -> int:
"""Save a serialized PyTorch or TensorFlow tensor or NumPy array in `urlpath`.
Parameters
Expand Down Expand Up @@ -1084,7 +1099,7 @@ def print_versions():
for clib in sorted(clib_versions.keys()):
print(f" {clib}: {clib_versions[clib]}")
print(f"Python version: {sys.version}")
(sysname, nodename, release, version, machine, processor) = platform.uname()
(sysname, _nodename, release, version, machine, processor) = platform.uname()
print(f"Platform: {sysname}-{release}-{machine} ({version})")
if sysname == "Linux":
distro = os_release_pretty_name()
Expand Down Expand Up @@ -1270,8 +1285,11 @@ def compute_partition(nitems, maxshape, minpart=None):


def compute_chunks_blocks(
shape: tuple[int] | list, chunks: tuple | list | None = None, blocks: tuple | list | None = None,
dtype: np.dtype = np.uint8, **kwargs: dict
shape: tuple[int] | list,
chunks: tuple | list | None = None,
blocks: tuple | list | None = None,
dtype: np.dtype = np.uint8,
**kwargs: dict,
) -> tuple[(int, int)]:
"""
Compute educated guesses for chunks and blocks of a :ref:`NDArray`.
Expand Down Expand Up @@ -1420,13 +1438,13 @@ def compress2(src: object, **kwargs: dict) -> str | bytes:
If an internal error occurred, probably because some
parameter is not a valid parameter.
"""
if kwargs is not None and 'cparams' in kwargs:
if kwargs is not None and "cparams" in kwargs:
if len(kwargs) > 1:
raise AttributeError("Cannot pass both cparams and other kwargs already included in CParams")
if isinstance(kwargs.get('cparams'), blosc2.CParams):
kwargs = asdict(kwargs.get('cparams'))
if isinstance(kwargs.get("cparams"), blosc2.CParams):
kwargs = asdict(kwargs.get("cparams"))
else:
kwargs = kwargs.get('cparams')
kwargs = kwargs.get("cparams")

return blosc2_ext.compress2(src, **kwargs)

Expand Down Expand Up @@ -1480,13 +1498,13 @@ def decompress2(src: object, dst: object | bytearray = None, **kwargs: dict) ->
If the length of :paramref:`src` is smaller than the minimum.
If :paramref:`dst` is not None and its length is 0.
"""
if kwargs is not None and 'dparams' in kwargs:
if kwargs is not None and "dparams" in kwargs:
if len(kwargs) > 1:
raise AttributeError("Cannot pass both dparams and other kwargs already included in DParams")
if isinstance(kwargs.get('dparams'), blosc2.DParams):
kwargs = asdict(kwargs.get('dparams'))
if isinstance(kwargs.get("dparams"), blosc2.DParams):
kwargs = asdict(kwargs.get("dparams"))
else:
kwargs = kwargs.get('dparams')
kwargs = kwargs.get("dparams")

return blosc2_ext.decompress2(src, dst, **kwargs)

Expand Down Expand Up @@ -1581,11 +1599,11 @@ def ndarray_from_cframe(cframe: bytes | str, copy: bool = False) -> blosc2.NDArr


def register_codec(
codec_name: str,
id: int,
encoder: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], int] = None,
decoder: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], int] = None,
version: int = 1
codec_name: str,
id: int,
encoder: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], int] = None,
decoder: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], int] = None,
version: int = 1,
) -> None:
"""Register a user defined codec.
Expand Down Expand Up @@ -1663,10 +1681,10 @@ def decoder1(input, output, meta, schunk):


def register_filter(
id: int,
forward: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], None] = None,
backward: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], None] = None,
name: str = None
id: int,
forward: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], None] = None,
backward: Callable[[np.ndarray[np.uint8], np.ndarray[np.uint8], int, blosc2.SChunk], None] = None,
name: str = None,
) -> None:
"""Register an user defined filter.
Expand Down
45 changes: 33 additions & 12 deletions src/blosc2/lazyexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from enum import Enum
from pathlib import Path
from queue import Empty, Queue

from typing import TYPE_CHECKING

if TYPE_CHECKING:
Expand Down Expand Up @@ -540,7 +539,7 @@ def fill_chunk_operands(
if nchunk == 0:
# Initialize the iterator for reading the chunks
arr = operands["o0"]
chunks_idx, nchunks = get_chunks_idx(arr.shape, arr.chunks)
chunks_idx, _ = get_chunks_idx(arr.shape, arr.chunks)
info = (reduc, aligned, low_mem, chunks_idx)
iter_chunks = read_nchunk(list(operands.values()), info)
# Run the asynchronous file reading function from a synchronous context
Expand Down Expand Up @@ -611,7 +610,10 @@ def fill_chunk_operands(


def fast_eval(
expression: str | Callable[[tuple, np.ndarray, tuple[int]], None], operands: dict, getitem: bool, **kwargs
expression: str | Callable[[tuple, np.ndarray, tuple[int]], None],
operands: dict,
getitem: bool,
**kwargs,
) -> blosc2.NDArray | np.ndarray:
"""Evaluate the expression in chunks of operands using a fast path.
Expand Down Expand Up @@ -722,7 +724,11 @@ def fast_eval(


def slices_eval(
expression: str | Callable[[tuple, np.ndarray, tuple[int]], None], operands: dict, getitem: bool, _slice=None, **kwargs
expression: str | Callable[[tuple, np.ndarray, tuple[int]], None],
operands: dict,
getitem: bool,
_slice=None,
**kwargs,
) -> blosc2.NDArray | np.ndarray:
"""Evaluate the expression in chunks of operands.
Expand Down Expand Up @@ -897,7 +903,11 @@ def slices_eval(


def reduce_slices(
expression: str | Callable[[tuple, np.ndarray, tuple[int]], None], operands: dict, reduce_args, _slice=None, **kwargs
expression: str | Callable[[tuple, np.ndarray, tuple[int]], None],
operands: dict,
reduce_args,
_slice=None,
**kwargs,
) -> blosc2.NDArray | np.ndarray:
"""Evaluate the expression in chunks of operands.
Expand Down Expand Up @@ -1132,7 +1142,9 @@ def convert_none_out(dtype, reduce_op, reduced_shape):
return out


def chunked_eval(expression: str | Callable[[tuple, np.ndarray, tuple[int]], None], operands: dict, item=None, **kwargs):
def chunked_eval(
expression: str | Callable[[tuple, np.ndarray, tuple[int]], None], operands: dict, item=None, **kwargs
):
"""
Evaluate the expression in chunks of operands.
Expand Down Expand Up @@ -1163,7 +1175,7 @@ def chunked_eval(expression: str | Callable[[tuple, np.ndarray, tuple[int]], Non
if where:
# Make the where arguments part of the operands
operands = {**operands, **where}
shape, _, _, fast_path = validate_inputs(operands, out)
_, _, _, fast_path = validate_inputs(operands, out)

# Activate last read cache for NDField instances
for op in operands:
Expand Down Expand Up @@ -1365,7 +1377,7 @@ def get_chunk(self, nchunk):
shape = out.shape
chunks = out.chunks
# Calculate the shape of the (chunk) slice_ (specially at the end of the array)
chunks_idx, nchunks = get_chunks_idx(shape, chunks)
chunks_idx, _ = get_chunks_idx(shape, chunks)
coords = tuple(np.unravel_index(nchunk, chunks_idx))
slice_ = tuple(
slice(c * s, min((c + 1) * s, shape[i]))
Expand Down Expand Up @@ -1943,8 +1955,13 @@ def _open_lazyarray(array):
return expr


def lazyudf(func: Callable[[tuple, np.ndarray, tuple[int]], None], inputs: tuple | list,
dtype: np.dtype, chunked_eval: bool = True, **kwargs: dict) -> LazyUDF:
def lazyudf(
func: Callable[[tuple, np.ndarray, tuple[int]], None],
inputs: tuple | list,
dtype: np.dtype,
chunked_eval: bool = True,
**kwargs: dict,
) -> LazyUDF:
"""
Get a LazyUDF from a python user-defined function.
Expand Down Expand Up @@ -2003,8 +2020,12 @@ def lazyudf(func: Callable[[tuple, np.ndarray, tuple[int]], None], inputs: tuple
return LazyUDF(func, inputs, dtype, chunked_eval, **kwargs)


def lazyexpr(expression: str | bytes | LazyExpr, operands: dict = None,
out: blosc2.NDArray | np.ndarray = None, where: tuple | list = None) -> LazyExpr:
def lazyexpr(
expression: str | bytes | LazyExpr,
operands: dict = None,
out: blosc2.NDArray | np.ndarray = None,
where: tuple | list = None,
) -> LazyExpr:
"""
Get a LazyExpr from an expression.
Expand Down
Loading

0 comments on commit 7f67272

Please sign in to comment.