Skip to content
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
5 changes: 0 additions & 5 deletions .github/workflows/test-cpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,6 @@ jobs:
python -m build --sdist --wheel .
twine check dist/*

- name: Check runtime version
run: |
pip install dist/*.whl
python -c 'import anndata; print(anndata.__version__)'

check:
if: always()
needs:
Expand Down
1 change: 1 addition & 0 deletions docs/release-notes/1318.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate `__version__` and use standard {func}`~importlib.metadata.version` API {user}`flying-sheep`
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ Home-page = "https://github.com/scverse/anndata"

[project.optional-dependencies]
dev = [
# runtime dev version generation
"hatch-vcs",
"anndata[dev-doc]",
]
doc = [
Expand Down Expand Up @@ -146,7 +144,9 @@ addopts = [
filterwarnings = [
"ignore::anndata._warnings.OldFormatWarning",
"ignore::anndata._warnings.ExperimentalFeatureWarning",
"ignore:.*first_column_names:FutureWarning:scanpy", # scanpy 1.10.x
"ignore:.*first_column_names:FutureWarning:scanpy", # scanpy 1.10.x
"ignore:Importing read_.* from `anndata` is deprecated:FutureWarning:scanpy",
"ignore:`__version__` is deprecated:FutureWarning:scanpy",
]
# When `--strict-warnings` is used, all warnings are treated as errors, except those:
filterwarnings_when_strict = [
Expand Down
41 changes: 23 additions & 18 deletions src/anndata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from ._core.merge import concat
from ._core.raw import Raw
from ._settings import settings
from ._version import __version__
from ._warnings import (
ExperimentalFeatureWarning,
ImplicitModificationWarning,
Expand All @@ -28,30 +27,13 @@
# We use these in tests by attribute access
from . import logging # noqa: F401 # isort: skip

_DEPRECATED_IO = (
"read_loom",
"read_hdf",
"read_excel",
"read_umi_tools",
"read_csv",
"read_text",
"read_mtx",
)
_DEPRECATED = {method: f"io.{method}" for method in _DEPRECATED_IO}


def __getattr__(attr_name: str) -> Any:
return module_get_attr_redirect(attr_name, deprecated_mapping=_DEPRECATED)


__all__ = [
"AnnData",
"ExperimentalFeatureWarning",
"ImplicitModificationWarning",
"OldFormatWarning",
"Raw",
"WriteWarning",
"__version__",
"abc",
"concat",
"experimental",
Expand All @@ -63,3 +45,26 @@ def __getattr__(attr_name: str) -> Any:
"types",
"typing",
]

_DEPRECATED_IO = (
"read_loom",
"read_hdf",
"read_excel",
"read_umi_tools",
"read_csv",
"read_text",
"read_mtx",
)
_DEPRECATED = {method: f"io.{method}" for method in _DEPRECATED_IO}


def __getattr__(attr_name: str) -> Any:
if attr_name == "__version__":
import warnings
from importlib.metadata import version

msg = "`__version__` is deprecated, use `importlib.metadata.version('anndata')` instead."
warnings.warn(msg, FutureWarning, stacklevel=2)
return version("anndata")

return module_get_attr_redirect(attr_name, deprecated_mapping=_DEPRECATED)
14 changes: 1 addition & 13 deletions src/anndata/_core/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@

import numpy as np
import pandas as pd
import scipy
from natsort import natsorted
from packaging.version import Version
from scipy import sparse

from anndata._core.file_backing import to_memory
Expand All @@ -30,7 +28,6 @@
CupyCSRMatrix,
CupySparseMatrix,
DaskArray,
_map_cat_to_str,
)
from ..utils import asarray, axis_len, warn_once
from .anndata import AnnData
Expand Down Expand Up @@ -185,15 +182,6 @@ def equal_sparse(a, b) -> bool:
# Comparison broken for CSC matrices
# https://github.com/cupy/cupy/issues/7757
a, b = CupyCSRMatrix(a), CupyCSRMatrix(b)
if Version(scipy.__version__) >= Version("1.16.0rc1"):
# TODO: https://github.com/scipy/scipy/issues/23068
return bool(
a.format == b.format
and (a.shape == b.shape)
and np.all(a.indptr == b.indptr)
and np.all(a.indices == b.indices)
and np.all((a.data == b.data) | (np.isnan(a.data) & np.isnan(b.data)))
)
comp = a != b
if isinstance(comp, bool):
return not comp
Expand Down Expand Up @@ -1646,7 +1634,7 @@ def concat( # noqa: PLR0912, PLR0913, PLR0915
)
if index_unique is not None:
concat_indices = concat_indices.str.cat(
_map_cat_to_str(label_col), sep=index_unique
label_col.map(str, na_action="ignore"), sep=index_unique
)
concat_indices = pd.Index(concat_indices)

Expand Down
4 changes: 2 additions & 2 deletions src/anndata/_core/sparse_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
from abc import ABC
from collections.abc import Iterable
from functools import cached_property
from importlib.metadata import version
from itertools import accumulate, chain, pairwise
from math import floor
from pathlib import Path
from typing import TYPE_CHECKING, NamedTuple

import h5py
import numpy as np
import scipy
import scipy.sparse as ss
from packaging.version import Version
from scipy.sparse import _sparsetools
Expand Down Expand Up @@ -54,7 +54,7 @@
from scipy.sparse import spmatrix as _cs_matrix


SCIPY_1_15 = Version(scipy.__version__) >= Version("1.15rc0")
SCIPY_1_15 = Version(version("scipy")) >= Version("1.15rc0")


class BackedFormat(NamedTuple):
Expand Down
29 changes: 11 additions & 18 deletions src/anndata/_io/h5ad.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from .specs import read_elem, write_elem
from .specs.registry import IOSpec, write_spec
from .utils import (
H5PY_V3,
_read_legacy_raw,
idx_chunks_along_axis,
no_write_dataset_2d,
Expand Down Expand Up @@ -324,16 +323,12 @@ def read_dataframe_legacy(dataset: h5py.Dataset) -> pd.DataFrame:
"Consider rewriting it."
)
warn(msg, OldFormatWarning, stacklevel=2)
if H5PY_V3:
df = pd.DataFrame(
_decode_structured_array(
_from_fixed_length_strings(dataset[()]), dtype=dataset.dtype
)
df = pd.DataFrame(
_decode_structured_array(
_from_fixed_length_strings(dataset[()]), dtype=dataset.dtype
)
else:
df = pd.DataFrame(_from_fixed_length_strings(dataset[()]))
df.set_index(df.columns[0], inplace=True)
return df
)
return df.set_index(df.columns[0])


def read_dataframe(group: h5py.Group | h5py.Dataset) -> pd.DataFrame:
Expand All @@ -346,10 +341,9 @@ def read_dataframe(group: h5py.Group | h5py.Dataset) -> pd.DataFrame:

@report_read_key_on_error
def read_dataset(dataset: h5py.Dataset):
if H5PY_V3:
string_dtype = h5py.check_string_dtype(dataset.dtype)
if (string_dtype is not None) and (string_dtype.encoding == "utf-8"):
dataset = dataset.asstr()
string_dtype = h5py.check_string_dtype(dataset.dtype)
if (string_dtype is not None) and (string_dtype.encoding == "utf-8"):
dataset = dataset.asstr()
value = dataset[()]
if not hasattr(value, "dtype"):
return value
Expand All @@ -362,10 +356,9 @@ def read_dataset(dataset: h5py.Dataset):
return value[0]
elif len(value.dtype.descr) > 1: # Compound dtype
# For backwards compat, now strings are written as variable length
dtype = value.dtype
value = _from_fixed_length_strings(value)
if H5PY_V3:
value = _decode_structured_array(value, dtype=dtype)
value = _decode_structured_array(
_from_fixed_length_strings(value), dtype=value.dtype
)
if value.shape == ():
value = value[()]
return value
Expand Down
12 changes: 6 additions & 6 deletions src/anndata/_io/specs/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections.abc import Mapping
from copy import copy
from functools import partial
from importlib.metadata import version
from itertools import product
from types import MappingProxyType
from typing import TYPE_CHECKING
Expand All @@ -21,7 +22,7 @@
from anndata._core.index import _normalize_indices
from anndata._core.merge import intersect_keys
from anndata._core.sparse_dataset import _CSCDataset, _CSRDataset, sparse_dataset
from anndata._io.utils import H5PY_V3, check_key, zero_dim_array_as_scalar
from anndata._io.utils import check_key, zero_dim_array_as_scalar
from anndata._warnings import OldFormatWarning
from anndata.compat import (
NULLABLE_NUMPY_STRING_TYPE,
Expand Down Expand Up @@ -609,7 +610,7 @@ def write_vlen_string_array_zarr(
if is_zarr_v2():
import numcodecs

if Version(numcodecs.__version__) < Version("0.13"):
if Version(version("numcodecs")) < Version("0.13"):
msg = "Old numcodecs version detected. Please update for improved performance and stability."
warnings.warn(msg, UserWarning, stacklevel=2)
# Workaround for https://github.com/zarr-developers/numcodecs/issues/514
Expand Down Expand Up @@ -665,10 +666,9 @@ def _to_hdf5_vlen_strings(value: np.ndarray) -> np.ndarray:
@_REGISTRY.register_read(ZarrArray, IOSpec("rec-array", "0.2.0"))
def read_recarray(d: ArrayStorageType, *, _reader: Reader) -> np.recarray | npt.NDArray:
value = d[()]
dtype = value.dtype
value = _from_fixed_length_strings(value)
if H5PY_V3:
value = _decode_structured_array(value, dtype=dtype)
value = _decode_structured_array(
_from_fixed_length_strings(value), dtype=value.dtype
)
return value


Expand Down
9 changes: 2 additions & 7 deletions src/anndata/_io/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from __future__ import annotations

from collections.abc import Callable
from functools import WRAPPER_ASSIGNMENTS, wraps
from itertools import pairwise
from typing import TYPE_CHECKING, cast
from typing import TYPE_CHECKING, Literal, cast
from warnings import warn

import h5py
from packaging.version import Version

from .._core.sparse_dataset import BaseCompressedSparseDataset

if TYPE_CHECKING:
Expand All @@ -21,9 +19,6 @@

Storage = StorageType | BaseCompressedSparseDataset

# For allowing h5py v3
# https://github.com/scverse/anndata/issues/442
H5PY_V3 = Version(h5py.__version__).major >= 3

# -------------------------------------------------------------------------------
# Type conversion
Expand Down
62 changes: 0 additions & 62 deletions src/anndata/_version.py

This file was deleted.

14 changes: 3 additions & 11 deletions src/anndata/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from codecs import decode
from collections.abc import Mapping, Sequence
from functools import cache, partial, singledispatch
from importlib.metadata import version
from importlib.util import find_spec
from types import EllipsisType
from typing import TYPE_CHECKING, TypeVar
Expand Down Expand Up @@ -75,10 +76,9 @@ class Empty:
#############################
@cache
def is_zarr_v2() -> bool:
import zarr
from packaging.version import Version

return Version(zarr.__version__) < Version("3.0.0")
return Version(version("zarr")) < Version("3.0.0")


if is_zarr_v2():
Expand Down Expand Up @@ -213,7 +213,7 @@ def old_positionals(*old_positionals):

NULLABLE_NUMPY_STRING_TYPE = (
np.dtype("O")
if Version(np.__version__) < Version("2")
if Version(version("numpy")) < Version("2")
else np.dtypes.StringDType(na_object=pd.NA)
)

Expand Down Expand Up @@ -428,11 +428,3 @@ def _safe_transpose(x):
return _transpose_by_block(x)
else:
return x.T


def _map_cat_to_str(cat: pd.Categorical) -> pd.Categorical:
if Version(pd.__version__) >= Version("2.1"):
# Argument added in pandas 2.1
return cat.map(str, na_action="ignore")
else:
return cat.map(str)
Loading
Loading