Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Sep 8, 2023
1 parent 9dbb96d commit 292dd4c
Show file tree
Hide file tree
Showing 74 changed files with 708 additions and 549 deletions.
5 changes: 3 additions & 2 deletions anndata/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Annotated multivariate observation data."""
from __future__ import annotations

try: # See https://github.com/maresb/hatch-vcs-footgun-example
from setuptools_scm import get_version
Expand Down Expand Up @@ -34,10 +35,10 @@
read_zarr,
)
from ._warnings import (
ExperimentalFeatureWarning,
ImplicitModificationWarning,
OldFormatWarning,
WriteWarning,
ImplicitModificationWarning,
ExperimentalFeatureWarning,
)

if True: # Bypass isort, this needs to come last
Expand Down
13 changes: 8 additions & 5 deletions anndata/_core/access.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from __future__ import annotations

from functools import reduce
from typing import NamedTuple, Tuple
from typing import TYPE_CHECKING, NamedTuple

from . import anndata
if TYPE_CHECKING:
from . import anndata


class ElementRef(NamedTuple):
parent: "anndata.AnnData"
parent: anndata.AnnData
attrname: str
keys: Tuple[str, ...] = ()
keys: tuple[str, ...] = ()

def __str__(self) -> str:
return f".{self.attrname}" + "".join(map(lambda x: f"['{x}']", self.keys))
return f".{self.attrname}" + "".join(f"['{x}']" for x in self.keys)

@property
def _parent_el(self):
Expand Down
68 changes: 38 additions & 30 deletions anndata/_core/aligned_mapping.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
from __future__ import annotations

import warnings
from abc import ABC, abstractmethod
from collections import abc as cabc
from collections.abc import Iterator, Mapping, Sequence
from copy import copy
from typing import Union, Optional, Type, ClassVar, TypeVar
from typing import Iterator, Mapping, Sequence
from typing import Tuple, List, Dict
import warnings
from typing import (
TYPE_CHECKING,
ClassVar,
TypeVar,
Union,
)

import numpy as np
import pandas as pd
from scipy.sparse import spmatrix

from ..utils import deprecated, ensure_df_homogeneous, dim_len
from . import raw, anndata
from .views import as_view, view_update
from anndata._warnings import ExperimentalFeatureWarning, ImplicitModificationWarning
from anndata.compat import AwkArray

from ..utils import deprecated, dim_len, ensure_df_homogeneous
from .access import ElementRef
from .index import _subset
from anndata.compat import AwkArray
from anndata._warnings import ExperimentalFeatureWarning, ImplicitModificationWarning
from .views import as_view, view_update

if TYPE_CHECKING:
from . import anndata, raw

OneDIdx = Union[Sequence[int], Sequence[bool], slice]
TwoDIdx = Tuple[OneDIdx, OneDIdx]
TwoDIdx = tuple[OneDIdx, OneDIdx]

I = TypeVar("I", OneDIdx, TwoDIdx, covariant=True)
# TODO: pd.DataFrame only allowed in AxisArrays?
Expand All @@ -36,16 +44,16 @@ class AlignedMapping(cabc.MutableMapping, ABC):
_allow_df: ClassVar[bool]
"""If this mapping supports heterogeneous DataFrames"""

_view_class: ClassVar[Type["AlignedViewMixin"]]
_view_class: ClassVar[type[AlignedViewMixin]]
"""The view class for this aligned mapping."""

_actual_class: ClassVar[Type["AlignedActualMixin"]]
_actual_class: ClassVar[type[AlignedActualMixin]]
"""The actual class (which has it’s own data) for this aligned mapping."""

def __repr__(self):
return f"{type(self).__name__} with keys: {', '.join(self.keys())}"

def _ipython_key_completions_(self) -> List[str]:
def _ipython_key_completions_(self) -> list[str]:
return list(self.keys())

def _validate_value(self, val: V, key: str) -> V:
Expand Down Expand Up @@ -94,7 +102,7 @@ def attrname(self) -> str:

@property
@abstractmethod
def axes(self) -> Tuple[int, ...]:
def axes(self) -> tuple[int, ...]:
"""Which axes of the parent is this aligned to?"""
pass

Expand All @@ -104,7 +112,7 @@ def is_view(self) -> bool:
pass

@property
def parent(self) -> Union["anndata.AnnData", "raw.Raw"]:
def parent(self) -> anndata.AnnData | raw.Raw:
return self._parent

def copy(self):
Expand All @@ -117,7 +125,7 @@ def copy(self):
d[k] = v.copy()
return d

def _view(self, parent: "anndata.AnnData", subset_idx: I):
def _view(self, parent: anndata.AnnData, subset_idx: I):
"""Returns a subset copy-on-write view of the object."""
return self._view_class(self, parent, subset_idx)

Expand All @@ -127,7 +135,7 @@ def as_dict(self) -> dict:


class AlignedViewMixin:
parent: "anndata.AnnData"
parent: anndata.AnnData
"""Reference to parent AnnData view"""

attrname: str
Expand Down Expand Up @@ -177,7 +185,7 @@ def __len__(self) -> int:


class AlignedActualMixin:
_data: Dict[str, V]
_data: dict[str, V]
"""Underlying mapping to the data"""

is_view = False
Expand Down Expand Up @@ -216,7 +224,7 @@ def attrname(self) -> str:
return f"{self.dim}m"

@property
def axes(self) -> Tuple[int]:
def axes(self) -> tuple[int]:
"""Axes of the parent this is aligned to"""
return (self._axis,)

Expand All @@ -225,7 +233,7 @@ def dim(self) -> str:
"""Name of the dimension this aligned to."""
return self._dimnames[self._axis]

def flipped(self) -> "AxisArraysBase":
def flipped(self) -> AxisArraysBase:
"""Transpose."""
new = self.copy()
new.dimension = abs(self._axis - 1)
Expand Down Expand Up @@ -265,9 +273,9 @@ def dim_names(self) -> pd.Index:
class AxisArrays(AlignedActualMixin, AxisArraysBase):
def __init__(
self,
parent: Union["anndata.AnnData", "raw.Raw"],
parent: anndata.AnnData | raw.Raw,
axis: int,
vals: Union[Mapping, AxisArraysBase, None] = None,
vals: Mapping | AxisArraysBase | None = None,
):
self._parent = parent
if axis not in (0, 1):
Expand All @@ -282,7 +290,7 @@ class AxisArraysView(AlignedViewMixin, AxisArraysBase):
def __init__(
self,
parent_mapping: AxisArraysBase,
parent_view: "anndata.AnnData",
parent_view: anndata.AnnData,
subset_idx: OneDIdx,
):
self.parent_mapping = parent_mapping
Expand All @@ -306,15 +314,15 @@ class LayersBase(AlignedMapping):
axes = (0, 1)

# TODO: I thought I had a more elegant solution to overriding this...
def copy(self) -> "Layers":
def copy(self) -> Layers:
d = self._actual_class(self.parent)
for k, v in self.items():
d[k] = v.copy()
return d


class Layers(AlignedActualMixin, LayersBase):
def __init__(self, parent: "anndata.AnnData", vals: Optional[Mapping] = None):
def __init__(self, parent: anndata.AnnData, vals: Mapping | None = None):
self._parent = parent
self._data = dict()
if vals is not None:
Expand All @@ -325,7 +333,7 @@ class LayersView(AlignedViewMixin, LayersBase):
def __init__(
self,
parent_mapping: LayersBase,
parent_view: "anndata.AnnData",
parent_view: anndata.AnnData,
subset_idx: TwoDIdx,
):
self.parent_mapping = parent_mapping
Expand All @@ -351,7 +359,7 @@ def attrname(self) -> str:
return f"{self.dim}p"

@property
def axes(self) -> Tuple[int, int]:
def axes(self) -> tuple[int, int]:
"""Axes of the parent this is aligned to"""
return self._axis, self._axis

Expand All @@ -364,9 +372,9 @@ def dim(self) -> str:
class PairwiseArrays(AlignedActualMixin, PairwiseArraysBase):
def __init__(
self,
parent: "anndata.AnnData",
parent: anndata.AnnData,
axis: int,
vals: Optional[Mapping] = None,
vals: Mapping | None = None,
):
self._parent = parent
if axis not in (0, 1):
Expand All @@ -381,7 +389,7 @@ class PairwiseArraysView(AlignedViewMixin, PairwiseArraysBase):
def __init__(
self,
parent_mapping: PairwiseArraysBase,
parent_view: "anndata.AnnData",
parent_view: anndata.AnnData,
subset_idx: OneDIdx,
):
self.parent_mapping = parent_mapping
Expand Down
Loading

0 comments on commit 292dd4c

Please sign in to comment.