Skip to content

Commit

Permalink
Test auto update
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Sep 8, 2023
1 parent 8c00a29 commit 2651539
Show file tree
Hide file tree
Showing 22 changed files with 154 additions and 150 deletions.
4 changes: 2 additions & 2 deletions .azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ jobs:
Python3.11:
python.version: "3.11"
RUN_COVERAGE: yes
Python3.8:
python.version: "3.8"
Python3.9:
python.version: "3.9"
PreRelease:
python.version: "3.11"
PRERELEASE_DEPENDENCIES: yes
Expand Down
6 changes: 4 additions & 2 deletions anndata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
except ModuleNotFoundError:
raise RuntimeError(
"anndata is not correctly installed. Please install it, e.g. with pip."
)
) from None

# Allowing notes to be added to exceptions. See: https://github.com/scverse/anndata/issues/868
import sys
Expand Down Expand Up @@ -39,7 +39,9 @@
ImplicitModificationWarning,
ExperimentalFeatureWarning,
)
from . import experimental

if True: # Bypass isort, this needs to come last
from . import experimental


def read(*args, **kwargs):
Expand Down
6 changes: 3 additions & 3 deletions anndata/_core/aligned_mapping.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from abc import ABC, abstractmethod
from collections import abc as cabc
from copy import copy
from typing import Union, Optional, Type, ClassVar, TypeVar # Special types
from typing import Iterator, Mapping, Sequence # ABCs
from typing import Tuple, List, Dict # Generic base types
from typing import Union, Optional, Type, ClassVar, TypeVar
from typing import Iterator, Mapping, Sequence
from typing import Tuple, List, Dict
import warnings

import numpy as np
Expand Down
36 changes: 8 additions & 28 deletions anndata/_core/anndata.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,24 +308,6 @@ class AnnData(metaclass=utils.DeprecationMixinMeta):
.. _scikit-learn: http://scikit-learn.org/
"""

_BACKED_ATTRS = ["X", "raw.X"]

# backwards compat
_H5_ALIASES = dict(
X={"X", "_X", "data", "_data"},
obs={"obs", "_obs", "smp", "_smp"},
var={"var", "_var"},
uns={"uns"},
obsm={"obsm", "_obsm", "smpm", "_smpm"},
varm={"varm", "_varm"},
layers={"layers", "_layers"},
)

_H5_ALIASES_NAMES = dict(
obs={"obs_names", "smp_names", "row_names", "index"},
var={"var_names", "col_names", "index"},
)

def __init__(
self,
X: Optional[Union[np.ndarray, sparse.spmatrix, pd.DataFrame]] = None,
Expand Down Expand Up @@ -1495,12 +1477,13 @@ def _get_var_array(self, k, use_raw=False, layer=None):

def _mutated_copy(self, **kwargs):
"""Creating AnnData with attributes optionally specified via kwargs."""
if self.isbacked:
if "X" not in kwargs or (self.raw is not None and "raw" not in kwargs):
raise NotImplementedError(
"This function does not currently handle backed objects "
"internally, this should be dealt with before."
)
if self.isbacked and (
"X" not in kwargs or (self.raw is not None and "raw" not in kwargs)
):
raise NotImplementedError(
"This function does not currently handle backed objects "
"internally, this should be dealt with before."
)
new = {}

for key in ["obs", "var", "obsm", "varm", "obsp", "varp", "layers"]:
Expand Down Expand Up @@ -2171,10 +2154,7 @@ def _clean_up_old_format(self, uns):
m_attr[key] = self._get_and_delete_multicol_field(axis, key)

def _get_and_delete_multicol_field(self, a, key_multicol):
keys = []
for k in getattr(self, a).columns:
if k.startswith(key_multicol):
keys.append(k)
keys = [k for k in getattr(self, a).columns if k.startswith(key_multicol)]
values = getattr(self, a)[keys].values
getattr(self, a).drop(keys, axis=1, inplace=True)
return values
4 changes: 2 additions & 2 deletions anndata/_core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from copy import deepcopy
from collections.abc import Sequence, KeysView, Callable, Iterable
from functools import reduce, singledispatch, wraps
from typing import Any, Literal
from typing import Any, ClassVar, Literal
import warnings

import numpy as np
Expand Down Expand Up @@ -248,7 +248,7 @@ class DictView(_ViewMixin, dict):


class DataFrameView(_ViewMixin, pd.DataFrame):
_metadata = ["_view_args"]
_metadata: ClassVar = ["_view_args"]

@wraps(pd.DataFrame.drop)
def drop(self, *args, inplace: bool = False, **kw):
Expand Down
4 changes: 2 additions & 2 deletions anndata/_io/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def func_wrapper(*args, **kwargs):
break
try:
return func(*args, **kwargs)
except Exception as e:
except Exception as e: # noqa: BLE001
re_raise_error(e, elem, elem.name, "read")

return func_wrapper
Expand Down Expand Up @@ -237,7 +237,7 @@ def func_wrapper(*args, **kwargs):
break
try:
return func(*args, **kwargs)
except Exception as e:
except Exception as e: # noqa: BLE001
re_raise_error(e, elem, key, "writ")

return func_wrapper
Expand Down
2 changes: 1 addition & 1 deletion anndata/_io/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def write_csvs(
value = value[None]
try:
df = pd.DataFrame(value)
except Exception as e:
except Exception as e: # noqa: BLE001
warnings.warn(
f"Omitting to write {key!r} of type {type(e)}.",
WriteWarning,
Expand Down
4 changes: 2 additions & 2 deletions anndata/experimental/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def as_group(store, *args, **kwargs) -> Union[ZarrGroup, H5Group]:
raise NotImplementedError("This is not yet implemented.")


@as_group.register
@as_group.register(os.PathLike)
def _(store: os.PathLike, *args, **kwargs) -> Union[ZarrGroup, H5Group]:
if store.suffix == ".h5ad":
import h5py
Expand All @@ -119,7 +119,7 @@ def _(store: os.PathLike, *args, **kwargs) -> Union[ZarrGroup, H5Group]:
return zarr.open_group(store, *args, **kwargs)


@as_group.register
@as_group.register(str)
def _(store: str, *args, **kwargs) -> Union[ZarrGroup, H5Group]:
return as_group(Path(store), *args, **kwargs)

Expand Down
8 changes: 2 additions & 6 deletions anndata/experimental/multi_files/_anncollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,8 @@ def _gather_X(self):
if oidx.size > 1 and np.any(oidx[:-1] >= oidx[1:]):
oidx, reverse = np.unique(oidx, return_inverse=True)

if isinstance(vidx, slice):
arr = X[oidx, vidx]
else:
# this is a very memory inefficient approach
# todo: fix
arr = X[oidx][:, vidx]
# TODO: fix else case (very memory inefficient approach)
arr = X[oidx, vidx] if isinstance(vidx, slice) else X[oidx][:, vidx]
Xs.append(arr if reverse is None else arr[reverse])
elif isinstance(X, BaseCompressedSparseDataset):
# very slow indexing with two arrays
Expand Down
6 changes: 1 addition & 5 deletions anndata/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,11 @@ def _gen_awkward_inner(shape, rng, dtype):
return dtype(rng.randrange(1000))
else:
curr_dim_len = shape[0]
lil = []
if curr_dim_len is None:
# ragged dimension, set random length
curr_dim_len = rng.randrange(MAX_RAGGED_DIM_LEN)

for _ in range(curr_dim_len):
lil.append(_gen_awkward_inner(shape[1:], rng, dtype))

return lil
return [_gen_awkward_inner(shape[1:], rng, dtype) for _ in range(curr_dim_len)]


def gen_awkward(shape, dtype=np.int32):
Expand Down
24 changes: 15 additions & 9 deletions anndata/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from functools import partial
from itertools import product
import re
import warnings
Expand All @@ -12,6 +13,7 @@
from scipy.sparse import csr_matrix, issparse

from anndata import AnnData
from anndata._core.raw import Raw
from anndata.tests.helpers import assert_equal, gen_adata


Expand Down Expand Up @@ -461,15 +463,15 @@ def test_equality_comparisons():
adata1 = AnnData(np.array([[1, 2], [3, 4], [5, 6]]))
adata2 = AnnData(np.array([[1, 2], [3, 4], [5, 6]]))
with pytest.raises(NotImplementedError):
adata1 == adata1
adata1 == adata1 # noqa: B015
with pytest.raises(NotImplementedError):
adata1 == adata2
adata1 == adata2 # noqa: B015
with pytest.raises(NotImplementedError):
adata1 != adata2
adata1 != adata2 # noqa: B015
with pytest.raises(NotImplementedError):
adata1 == 1
adata1 == 1 # noqa: B015
with pytest.raises(NotImplementedError):
adata1 != 1
adata1 != 1 # noqa: B015


def test_rename_categories():
Expand Down Expand Up @@ -533,19 +535,23 @@ def assert_same_op_result(a1, a2, op):

for obs_k, layer in product(["a", "b", "c", "anno1"], [None, "x2"]):
assert_same_op_result(
adata, adata_dense, lambda x: x.obs_vector(obs_k, layer=layer)
adata, adata_dense, partial(AnnData.obs_vector, k=obs_k, layer=layer)
)

for obs_k in ["a", "b", "c"]:
assert_same_op_result(adata, adata_dense, lambda x: x.raw.obs_vector(obs_k))
assert_same_op_result(
adata.raw, adata_dense.raw, partial(Raw.obs_vector, k=obs_k)
)

for var_k, layer in product(["s1", "s2", "anno2"], [None, "x2"]):
assert_same_op_result(
adata, adata_dense, lambda x: x.var_vector(var_k, layer=layer)
adata, adata_dense, partial(AnnData.var_vector, k=var_k, layer=layer)
)

for var_k in ["s1", "s2", "anno2"]:
assert_same_op_result(adata, adata_dense, lambda x: x.raw.var_vector(var_k))
assert_same_op_result(
adata.raw, adata_dense.raw, partial(Raw.var_vector, k=var_k)
)


def test_1d_slice_dtypes():
Expand Down
12 changes: 5 additions & 7 deletions anndata/tests/test_concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,11 +795,9 @@ def gen_dim_array(m):

adatas = {
k: AnnData(
**{
"X": sparse.csr_matrix((m, n)),
"obsp": {"arr": gen_dim_array(m)},
"varp": {"arr": gen_dim_array(n)},
}
X=sparse.csr_matrix((m, n)),
obsp={"arr": gen_dim_array(m)},
varp={"arr": gen_dim_array(n)},
)
for k, m, n in zip("abc", Ms, Ns)
}
Expand Down Expand Up @@ -1347,8 +1345,8 @@ def test_concatenate_size_0_dim():
b = gen_adata((5, 0))

# Mostly testing that this doesn't error
a.concatenate([b]).shape == (10, 0)
b.concatenate([a]).shape == (10, 0)
assert a.concatenate([b]).shape == (10, 0)
assert b.concatenate([a]).shape == (10, 0)


def test_concat_null_X():
Expand Down
41 changes: 20 additions & 21 deletions anndata/tests/test_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_dask_write(adata, tmp_path, diskfmt):
write(orig, pth)
curr = read(pth)

with pytest.raises(Exception):
with pytest.raises(AssertionError):
assert_equal(curr.obsm["a"], curr.obsm["b"])

assert_equal(curr.varm["a"], orig.varm["a"])
Expand All @@ -104,23 +104,22 @@ def test_dask_distributed_write(adata, tmp_path, diskfmt):
pth = tmp_path / f"test_write.{diskfmt}"
g = as_group(pth, mode="w")

with dd.LocalCluster(n_workers=1, threads_per_worker=1, processes=False) as cluster:
with dd.Client(cluster):
M, N = adata.X.shape
adata.obsm["a"] = da.random.random((M, 10))
adata.obsm["b"] = da.random.random((M, 10))
adata.varm["a"] = da.random.random((N, 10))
orig = adata
if diskfmt == "h5ad":
with pytest.raises(
ValueError, match="Cannot write dask arrays to hdf5"
):
write_elem(g, "", orig)
return
write_elem(g, "", orig)
curr = read_elem(g)

with pytest.raises(Exception):
with dd.LocalCluster(
n_workers=1, threads_per_worker=1, processes=False
) as cluster, dd.Client(cluster):
M, N = adata.X.shape
adata.obsm["a"] = da.random.random((M, 10))
adata.obsm["b"] = da.random.random((M, 10))
adata.varm["a"] = da.random.random((N, 10))
orig = adata
if diskfmt == "h5ad":
with pytest.raises(ValueError, match="Cannot write dask arrays to hdf5"):
write_elem(g, "", orig)
return
write_elem(g, "", orig)
curr = read_elem(g)

with pytest.raises(AssertionError):
assert_equal(curr.obsm["a"], curr.obsm["b"])

assert_equal(curr.varm["a"], orig.varm["a"])
Expand Down Expand Up @@ -157,7 +156,7 @@ def test_dask_to_memory_check_array_types(adata, tmp_path, diskfmt):

mem = orig.to_memory()

with pytest.raises(Exception):
with pytest.raises(AssertionError):
assert_equal(curr.obsm["a"], curr.obsm["b"])

assert_equal(curr.varm["a"], orig.varm["a"])
Expand Down Expand Up @@ -195,7 +194,7 @@ def test_dask_to_memory_copy_check_array_types(adata, tmp_path, diskfmt):

mem = orig.to_memory(copy=True)

with pytest.raises(Exception):
with pytest.raises(AssertionError):
assert_equal(curr.obsm["a"], curr.obsm["b"])

assert_equal(curr.varm["a"], orig.varm["a"])
Expand Down Expand Up @@ -225,7 +224,7 @@ def test_dask_copy_check_array_types(adata):
orig = adata
curr = adata.copy()

with pytest.raises(Exception):
with pytest.raises(AssertionError):
assert_equal(curr.obsm["a"], curr.obsm["b"])

assert_equal(curr.varm["a"], orig.varm["a"])
Expand Down
12 changes: 5 additions & 7 deletions anndata/tests/test_dask_view_mem.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ def alloc_cache():

adata = ad.AnnData(
da.random.random(*size),
**{
"layers": dict(m=da.random.random(*size)),
"obsm": dict(m=da.random.random(*size)),
"obs": dict(m=da.random.random((N))),
"var": dict(m=da.random.random((N))),
"varm": dict(m=da.random.random(*size)),
},
layers=dict(m=da.random.random(*size)),
obsm=dict(m=da.random.random(*size)),
obs=dict(m=da.random.random((N))),
var=dict(m=da.random.random((N))),
varm=dict(m=da.random.random(*size)),
)
subset = adata[:10, :][:, :10]
for mn in ["varm", "obsm", "layers"]:
Expand Down
6 changes: 3 additions & 3 deletions anndata/tests/test_io_dispatched.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

def test_read_dispatched_w_regex():
def read_only_axis_dfs(func, elem_name: str, elem, iospec):
if iospec.encoding_type == "anndata":
return func(elem)
elif re.match(r"^/((obs)|(var))?(/.*)?$", elem_name):
if iospec.encoding_type == "anndata" or re.match(
r"^/((obs)|(var))?(/.*)?$", elem_name
):
return func(elem)
else:
return None
Expand Down
Loading

0 comments on commit 2651539

Please sign in to comment.