Skip to content

feature_summary

Per-feature summaries of an AnnData response matrix.

FeatureSummary dataclass

Feature means and (optional) maxima of the response.

Parameters:

Name Type Description Default
mean ndarray of shape (n_features,)
required
maximum ndarray of shape (n_features,)
required
Source code in scdesigner/src/scdesigner/data/feature_summary.py
10
11
12
13
14
15
16
17
18
19
20
@dataclass(frozen=True)
class FeatureSummary:
    """Feature means and (optional) maxima of the response.

    Parameters
    ----------
    mean : ndarray of shape (n_features,)
    maximum : ndarray of shape (n_features,), optional
    """
    mean: np.ndarray
    maximum: Optional[np.ndarray]

summarize_features(adata, *, include_max=False, chunk_size=None)

:class:FeatureSummary of `adata.X

This computes the mean over columns, one chunk at a time, so that it scales to large backed anndata objects.

Parameters:

Name Type Description Default
adata AnnData

In memory or backed.

required
include_max bool

Whether to compute maximum. Otherwise it is None.

False
chunk_size int

Rows per read; see :func:~scdesigner.data.response.iter_response_chunks.

None
Source code in scdesigner/src/scdesigner/data/feature_summary.py
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
def summarize_features(
    adata: AnnData, *, include_max: bool = False, chunk_size: Optional[int] = None
) -> FeatureSummary:
    """:class:`FeatureSummary` of ``adata.X`

    This computes the mean over columns, one chunk at a time, so that it scales
    to large backed anndata objects.

    Parameters
    ----------
    adata : AnnData
        In memory or backed.
    include_max : bool
        Whether to compute ``maximum``. Otherwise it is None.
    chunk_size : int, optional
        Rows per read; see :func:`~scdesigner.data.response.iter_response_chunks`.
    """
    total = np.zeros(adata.n_vars)
    maximum = np.full(adata.n_vars, -np.inf) if include_max else None
    n_rows = 0
    for _, _, values in iter_response_chunks(adata, chunk_size):
        total += values.sum(axis=0, dtype=np.float64)
        n_rows += len(values)
        if include_max:
            np.maximum(maximum, values.max(axis=0), out=maximum)
    return FeatureSummary(mean=total / n_rows, maximum=maximum)