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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.12
rev: v0.13.0
hooks:
- id: ruff
- id: ruff-check
args: ["--fix"]
- id: ruff-format
# The following can be removed once PLR0917 is out of preview
Expand Down
22 changes: 10 additions & 12 deletions benchmarks/benchmarks/anndata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ class GarbargeCollectionSuite:
# custom because `memory_profiler` is a line-by-line profiler (also: https://github.com/pythonprofilers/memory_profiler/issues/402)
def track_peakmem_garbage_collection(self, *_):
def display_top(snapshot, key_type="lineno"):
snapshot = snapshot.filter_traces(
(
tracemalloc.Filter(
inclusive=False,
filename_pattern="<frozen importlib._bootstrap>",
),
tracemalloc.Filter(
inclusive=False,
filename_pattern="<unknown>",
),
)
)
snapshot = snapshot.filter_traces((
tracemalloc.Filter(
inclusive=False,
filename_pattern="<frozen importlib._bootstrap>",
),
tracemalloc.Filter(
inclusive=False,
filename_pattern="<unknown>",
),
))
top_stats = snapshot.statistics(key_type)
total = sum(stat.size for stat in top_stats)
return total
Expand Down
8 changes: 5 additions & 3 deletions benchmarks/benchmarks/readwrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ def peakmem_write_compressed(self, *_):
self.adata.write_h5ad(self.writepth, compression="gzip")

def track_peakmem_write_compressed(self, *_):
return get_peak_mem(
(sedate(self.adata.write_h5ad), (self.writepth,), {"compression": "gzip"})
)
return get_peak_mem((
sedate(self.adata.write_h5ad),
(self.writepth,),
{"compression": "gzip"},
))


class H5ADBackedWriteSuite(H5ADWriteSuite):
Expand Down
22 changes: 10 additions & 12 deletions benchmarks/benchmarks/sparse_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ def make_alternating_mask(n):


class SparseCSRContiguousSlice:
_slices = MappingProxyType(
{
"0:1000": slice(0, 1000),
"0:9000": slice(0, 9000),
":9000:-1": slice(None, 9000, -1),
"::-2": slice(None, None, 2),
"array": np.array([0, 5000, 9999]),
"arange": np.arange(0, 1000),
"first": 0,
"alternating": make_alternating_mask(10),
}
)
_slices = MappingProxyType({
"0:1000": slice(0, 1000),
"0:9000": slice(0, 9000),
":9000:-1": slice(None, 9000, -1),
"::-2": slice(None, None, 2),
"array": np.array([0, 5000, 9999]),
"arange": np.arange(0, 1000),
"first": 0,
"alternating": make_alternating_mask(10),
})
params = (
[
(10_000, 10_000),
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ markers = [ "gpu: mark test to run on GPU", "zarr_io: mark tests that involve za
src = [ "src" ]

[tool.ruff.format]
preview = true
docstring-code-format = true

[tool.ruff.lint]
Expand Down
39 changes: 16 additions & 23 deletions src/anndata/_core/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1748,15 +1748,10 @@ def concat( # noqa: PLR0912, PLR0913, PLR0915
for r, a in zip(reindexers, adatas, strict=True)
],
)
alt_pairwise = merge(
[
{
k: r(r(v, axis=0), axis=1)
for k, v in getattr(a, f"{alt_axis_name}p").items()
}
for r, a in zip(reindexers, adatas, strict=True)
]
)
alt_pairwise = merge([
{k: r(r(v, axis=0), axis=1) for k, v in getattr(a, f"{alt_axis_name}p").items()}
for r, a in zip(reindexers, adatas, strict=True)
])
uns = uns_merge([a.uns for a in adatas])

raw = None
Expand Down Expand Up @@ -1785,17 +1780,15 @@ def concat( # noqa: PLR0912, PLR0913, PLR0915
"not concatenating `.raw` attributes."
)
warn(msg, UserWarning, stacklevel=2)
return AnnData(
**{
"X": X,
"layers": layers,
axis_name: concat_annot,
alt_axis_name: alt_annot,
f"{axis_name}m": concat_mapping,
f"{alt_axis_name}m": alt_mapping,
f"{axis_name}p": concat_pairwise,
f"{alt_axis_name}p": alt_pairwise,
"uns": uns,
"raw": raw,
}
)
return AnnData(**{
"X": X,
"layers": layers,
axis_name: concat_annot,
alt_axis_name: alt_annot,
f"{axis_name}m": concat_mapping,
f"{alt_axis_name}m": alt_mapping,
f"{axis_name}p": concat_pairwise,
f"{alt_axis_name}p": alt_pairwise,
"uns": uns,
"raw": raw,
})
12 changes: 6 additions & 6 deletions src/anndata/_core/sparse_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ def get_compressed_vectors(
indptr_slices = [slice(*(x.indptr[i : i + 2])) for i in row_idxs]
# HDF5 cannot handle out-of-order integer indexing
if isinstance(x.data, ZarrArray):
as_np_indptr = np.concatenate(
[np.arange(s.start, s.stop) for s in indptr_slices]
)
as_np_indptr = np.concatenate([
np.arange(s.start, s.stop) for s in indptr_slices
])
data = x.data[as_np_indptr]
indices = x.indices[as_np_indptr]
else:
Expand Down Expand Up @@ -309,9 +309,9 @@ def get_compressed_vectors_for_slices(
start_indptr = indptr_indices[0] - next(offsets)
if len(slices) < 2: # there is only one slice so no need to concatenate
return data, indices, start_indptr
end_indptr = np.concatenate(
[s[1:] - o for s, o in zip(indptr_indices[1:], offsets, strict=True)]
)
end_indptr = np.concatenate([
s[1:] - o for s, o in zip(indptr_indices[1:], offsets, strict=True)
])
indptr = np.concatenate([start_indptr, end_indptr])
return data, indices, indptr

Expand Down
2 changes: 1 addition & 1 deletion src/anndata/_core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __init__(

# TODO: This makes `deepcopy(obj)` return `obj._view_args.parent._adata_ref`, fix it
def __deepcopy__(self, memo):
parent, attrname, keys = self._view_args
parent, attrname, _keys = self._view_args
return deepcopy(getattr(parent._adata_ref, attrname))


Expand Down
16 changes: 7 additions & 9 deletions src/anndata/_io/h5ad.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,13 @@ def read_h5ad(

def callback(func, elem_name: str, elem, iospec):
if iospec.encoding_type == "anndata" or elem_name.endswith("/"):
return AnnData(
**{
# This is covering up backwards compat in the anndata initializer
# In most cases we should be able to call `func(elen[k])` instead
k: read_dispatched(elem[k], callback)
for k in elem
if not k.startswith("raw.")
}
)
return AnnData(**{
# This is covering up backwards compat in the anndata initializer
# In most cases we should be able to call `func(elen[k])` instead
k: read_dispatched(elem[k], callback)
for k in elem
if not k.startswith("raw.")
})
elif elem_name.startswith("/raw."):
return None
elif elem_name == "/X" and "X" in as_sparse:
Expand Down
2 changes: 1 addition & 1 deletion src/anndata/_io/specs/lazy_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from .registry import LazyDataStructures, LazyReader

BlockInfo = Mapping[
Literal[None],
None,
dict[str, Sequence[tuple[int, int]]],
]

Expand Down
9 changes: 3 additions & 6 deletions src/anndata/_io/specs/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,9 @@ def proc_spec_mapping(spec: Mapping[str, str]) -> IOSpec:
def get_spec(
elem: StorageType,
) -> IOSpec:
return proc_spec(
{
k: _read_attr(elem.attrs, k, "")
for k in ["encoding-type", "encoding-version"]
}
)
return proc_spec({
k: _read_attr(elem.attrs, k, "") for k in ["encoding-type", "encoding-version"]
})


def _iter_patterns(
Expand Down
12 changes: 5 additions & 7 deletions src/anndata/_io/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,11 @@ def read_zarr(store: PathLike[str] | str | MutableMapping | zarr.Group) -> AnnDa
# Read with handling for backwards compat
def callback(func, elem_name: str, elem, iospec):
if iospec.encoding_type == "anndata" or elem_name.endswith("/"):
return AnnData(
**{
k: read_dispatched(v, callback)
for k, v in dict(elem).items()
if not k.startswith("raw.")
}
)
return AnnData(**{
k: read_dispatched(v, callback)
for k, v in dict(elem).items()
if not k.startswith("raw.")
})
elif elem_name.startswith("/raw."):
return None
elif elem_name in {"/obs", "/var"}:
Expand Down
17 changes: 8 additions & 9 deletions src/anndata/experimental/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,10 @@ def write_concat_dense( # noqa: PLR0917
axis=axis,
)
write_elem(output_group, output_path, res)
output_group[output_path].attrs.update(
{"encoding-type": "array", "encoding-version": "0.2.0"}
)
output_group[output_path].attrs.update({
"encoding-type": "array",
"encoding-version": "0.2.0",
})


def write_concat_sparse( # noqa: PLR0917
Expand Down Expand Up @@ -259,12 +260,10 @@ def _write_concat_mappings( # noqa: PLR0913, PLR0917
Write a list of mappings to a zarr/h5 group.
"""
mapping_group = output_group.create_group(path)
mapping_group.attrs.update(
{
"encoding-type": "dict",
"encoding-version": "0.1.0",
}
)
mapping_group.attrs.update({
"encoding-type": "dict",
"encoding-version": "0.1.0",
})
for k in keys:
elems = [m[k] for m in mappings]
_write_concat_sequence(
Expand Down
7 changes: 4 additions & 3 deletions tests/lazy/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ def test_access_count_subset(
adata_remote_tall_skinny: AnnData,
):
non_obs_elem_names = filter(lambda e: e != "obs", ANNDATA_ELEMS)
remote_store_tall_skinny.initialize_key_trackers(
["obs/cat/codes", *non_obs_elem_names]
)
remote_store_tall_skinny.initialize_key_trackers([
"obs/cat/codes",
*non_obs_elem_names,
])
adata_remote_tall_skinny[adata_remote_tall_skinny.obs["cat"] == "a", :]
# all codes read in for subset (from 4 chunks as set in the fixture)
remote_store_tall_skinny.assert_access_count("obs/cat/codes", 4)
Expand Down
Loading