1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
from contextlib import contextmanager
from copy import deepcopy
from functools import reduce, singledispatch, wraps
from typing import Any, KeysView, Optional, Sequence, Tuple
import numpy as np
import pandas as pd
from pandas.api.types import is_bool_dtype
from scipy import sparse
from .access import ElementRef
from ..logging import anndata_logger as logger
from ..compat import ZappyArray
class _SetItemMixin:
"""\
Class which (when values are being set) lets their parent AnnData view know,
so it can make a copy of itself.
This implements copy-on-modify semantics for views of AnnData objects.
"""
def __setitem__(self, idx: Any, value: Any):
if self._view_args is None:
super().__setitem__(idx, value)
else:
logger.warning(
f"Trying to set attribute `.{self._view_args.attrname}` of view, copying."
)
with self._update() as container:
container[idx] = value
@contextmanager
def _update(self):
adata_view, attr_name, keys = self._view_args
new = adata_view.copy()
attr = getattr(new, attr_name)
container = reduce(lambda d, k: d[k], keys, attr)
yield container
adata_view._init_as_actual(new)
class _ViewMixin(_SetItemMixin):
def __init__(
self,
*args,
view_args: Tuple["anndata.AnnData", str, Tuple[str, ...]] = None,
**kwargs,
):
if view_args is not None:
view_args = ElementRef(*view_args)
self._view_args = view_args
super().__init__(*args, **kwargs)
# TODO: This makes `deepcopy(obj)` return `obj._view_args.parent._adata_ref`, fix it
def __deepcopy__(self, memo):
parent, attrname, keys = self._view_args
return deepcopy(getattr(parent._adata_ref, attrname))
class ArrayView(_SetItemMixin, np.ndarray):
def __new__(
cls,
input_array: Sequence[Any],
view_args: Tuple["anndata.AnnData", str, Tuple[str, ...]] = None,
):
arr = np.asanyarray(input_array).view(cls)
if view_args is not None:
view_args = ElementRef(*view_args)
arr._view_args = view_args
return arr
def __array_finalize__(self, obj: Optional[np.ndarray]):
if obj is not None:
self._view_args = getattr(obj, "_view_args", None)
def keys(self) -> KeysView[str]:
# it’s a structured array
return self.dtype.names
def copy(self, order: str = "C") -> np.ndarray:
# we want a conventional array
return np.array(self)
def toarray(self) -> np.ndarray:
return self.copy()
# Unlike array views, SparseCSRView and SparseCSCView
# do not propagate through subsetting
class SparseCSRView(_ViewMixin, sparse.csr_matrix):
pass
class SparseCSCView(_ViewMixin, sparse.csc_matrix):
pass
class DictView(_ViewMixin, dict):
pass
class DataFrameView(_ViewMixin, pd.DataFrame):
_metadata = ["_view_args"]
@wraps(pd.DataFrame.drop)
def drop(self, *args, inplace: bool = False, **kw):
if not inplace:
return self.copy().drop(*args, **kw)
with self._update() as df:
df.drop(*args, inplace=True, **kw)
@singledispatch
def as_view(obj, view_args):
raise NotImplementedError(f"No view type has been registered for {type(obj)}")
@as_view.register(np.ndarray)
def as_view_array(array, view_args):
return ArrayView(array, view_args=view_args)
@as_view.register(pd.DataFrame)
def as_view_df(df, view_args):
return DataFrameView(df, view_args=view_args)
@as_view.register(sparse.csr_matrix)
def as_view_csr(mtx, view_args):
return SparseCSRView(mtx, view_args=view_args)
@as_view.register(sparse.csc_matrix)
def as_view_csc(mtx, view_args):
return SparseCSCView(mtx, view_args=view_args)
@as_view.register(dict)
def as_view_dict(d, view_args):
return DictView(d, view_args=view_args)
@as_view.register(ZappyArray)
def as_view_zappy(z, view_args):
# Previous code says ZappyArray works as view,
# but as far as I can tell they’re immutable.
return z
def _resolve_idxs(old, new, adata):
t = tuple(_resolve_idx(old[i], new[i], adata.shape[i]) for i in (0, 1))
return t
@singledispatch
def _resolve_idx(old, new, l):
return old[new]
@_resolve_idx.register(np.ndarray)
def _resolve_idx_ndarray(old, new, l):
if is_bool_dtype(old):
old = np.where(old)[0]
return old[new]
@_resolve_idx.register(np.integer)
@_resolve_idx.register(int)
def _resolve_idx_scalar(old, new, l):
return np.array([old])[new]
@_resolve_idx.register(slice)
def _resolve_idx_slice(old, new, l):
if isinstance(new, slice):
return _resolve_idx_slice_slice(old, new, l)
else:
return np.arange(*old.indices(l))[new]
def _resolve_idx_slice_slice(old, new, l):
r = range(*old.indices(l))[new]
# Convert back to slice
start, stop, step = r.start, r.stop, r.step
if len(r) == 0:
stop = start
elif stop < 0:
stop = None
return slice(start, stop, step)
|