|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from functools import cache |
| 4 | +from importlib.util import find_spec |
| 5 | +from types import EllipsisType |
| 6 | +from typing import TYPE_CHECKING |
| 7 | +from warnings import warn |
| 8 | + |
| 9 | +import h5py |
| 10 | +import numpy as np |
| 11 | +import pandas as pd |
| 12 | +import scipy |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + pass |
| 16 | + |
| 17 | +############################# |
| 18 | +# scipy sparse array compat # |
| 19 | +############################# |
| 20 | + |
| 21 | + |
| 22 | +CSMatrix = scipy.sparse.csr_matrix | scipy.sparse.csc_matrix |
| 23 | +CSArray = scipy.sparse.csr_array | scipy.sparse.csc_array |
| 24 | + |
| 25 | + |
| 26 | +class Empty: |
| 27 | + pass |
| 28 | + |
| 29 | + |
| 30 | +Index1D = slice | int | str | np.int64 | np.ndarray | pd.Series |
| 31 | +IndexRest = Index1D | EllipsisType |
| 32 | +Index = ( |
| 33 | + IndexRest |
| 34 | + | tuple[Index1D, IndexRest] |
| 35 | + | tuple[IndexRest, Index1D] |
| 36 | + | tuple[Index1D, Index1D, EllipsisType] |
| 37 | + | tuple[EllipsisType, Index1D, Index1D] |
| 38 | + | tuple[Index1D, EllipsisType, Index1D] |
| 39 | + | CSMatrix |
| 40 | + | CSArray |
| 41 | +) |
| 42 | +H5Group = h5py.Group |
| 43 | +H5Array = h5py.Dataset |
| 44 | +H5File = h5py.File |
| 45 | + |
| 46 | + |
| 47 | +############################# |
| 48 | +# Optional deps |
| 49 | +############################# |
| 50 | +@cache |
| 51 | +def is_zarr_v2() -> bool: |
| 52 | + import zarr |
| 53 | + from packaging.version import Version |
| 54 | + |
| 55 | + return Version(zarr.__version__) < Version("3.0.0") |
| 56 | + |
| 57 | + |
| 58 | +if is_zarr_v2(): |
| 59 | + msg = "anndata will no longer support zarr v2 in the near future. Please prepare to upgrade to zarr>=3." |
| 60 | + warn(msg, DeprecationWarning, stacklevel=2) |
| 61 | + |
| 62 | + |
| 63 | +if find_spec("awkward") or TYPE_CHECKING: |
| 64 | + import awkward # noqa: F401 |
| 65 | + from awkward import Array as AwkArray |
| 66 | +else: |
| 67 | + |
| 68 | + class AwkArray: |
| 69 | + @staticmethod |
| 70 | + def __repr__(): |
| 71 | + return "mock awkward.highlevel.Array" |
| 72 | + |
| 73 | + |
| 74 | +if find_spec("zappy") or TYPE_CHECKING: |
| 75 | + from zappy.base import ZappyArray |
| 76 | +else: |
| 77 | + |
| 78 | + class ZappyArray: |
| 79 | + @staticmethod |
| 80 | + def __repr__(): |
| 81 | + return "mock zappy.base.ZappyArray" |
| 82 | + |
| 83 | + |
| 84 | +if TYPE_CHECKING: |
| 85 | + # type checkers are confused and can only see …core.Array |
| 86 | + from dask.array.core import Array as DaskArray |
| 87 | +elif find_spec("dask"): |
| 88 | + from dask.array import Array as DaskArray |
| 89 | +else: |
| 90 | + |
| 91 | + class DaskArray: |
| 92 | + @staticmethod |
| 93 | + def __repr__(): |
| 94 | + return "mock dask.array.core.Array" |
| 95 | + |
| 96 | + |
| 97 | +# https://github.com/scverse/anndata/issues/1749 |
| 98 | +def is_cupy_importable() -> bool: |
| 99 | + try: |
| 100 | + import cupy # noqa: F401 |
| 101 | + except ImportError: |
| 102 | + return False |
| 103 | + return True |
| 104 | + |
| 105 | + |
| 106 | +if is_cupy_importable() or TYPE_CHECKING: |
| 107 | + from cupy import ndarray as CupyArray |
| 108 | + from cupyx.scipy.sparse import csc_matrix as CupyCSCMatrix |
| 109 | + from cupyx.scipy.sparse import csr_matrix as CupyCSRMatrix |
| 110 | + from cupyx.scipy.sparse import spmatrix as CupySparseMatrix |
| 111 | + |
| 112 | + try: |
| 113 | + import dask.array as da |
| 114 | + except ImportError: |
| 115 | + pass |
| 116 | + else: |
| 117 | + da.register_chunk_type(CupyCSRMatrix) |
| 118 | + da.register_chunk_type(CupyCSCMatrix) |
| 119 | +else: |
| 120 | + |
| 121 | + class CupySparseMatrix: |
| 122 | + @staticmethod |
| 123 | + def __repr__(): |
| 124 | + return "mock cupyx.scipy.sparse.spmatrix" |
| 125 | + |
| 126 | + class CupyCSRMatrix: |
| 127 | + @staticmethod |
| 128 | + def __repr__(): |
| 129 | + return "mock cupyx.scipy.sparse.csr_matrix" |
| 130 | + |
| 131 | + class CupyCSCMatrix: |
| 132 | + @staticmethod |
| 133 | + def __repr__(): |
| 134 | + return "mock cupyx.scipy.sparse.csc_matrix" |
| 135 | + |
| 136 | + class CupyArray: |
| 137 | + @staticmethod |
| 138 | + def __repr__(): |
| 139 | + return "mock cupy.ndarray" |
| 140 | + |
| 141 | + |
| 142 | +def lazy_import_torch() -> None: |
| 143 | + try: |
| 144 | + import torch |
| 145 | + |
| 146 | + return torch |
| 147 | + except ImportError: |
| 148 | + raise ImportError( |
| 149 | + "The optional module 'torch' is not installed. Please install it using 'pip install ehrdata[torch]'." |
| 150 | + ) from None |
0 commit comments