Skip to content

response

Partial (chunked) reads of backed anndata matrices X

This reads groups of cells at a time in a backend object. It only supports CSR format, since cell-level reading is much more efficient in row-oriented tables.

iter_response_chunks(adata, chunk_size=None)

(start, stop, read_response_rows(adata, start, stop)) across chunks.

This generator provides indexing information and the actual materialized chunks associated with a backed anndata object. If chunk size is not provided, defaults to 20000.

Source code in scdesigner/src/scdesigner/data/response.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def iter_response_chunks(
    adata: AnnData, chunk_size: Optional[int] = None
) -> Iterator[Tuple[int, int, np.ndarray]]:
    """``(start, stop, read_response_rows(adata, start, stop))`` across chunks.

    This generator provides indexing information and the actual materialized
    chunks associated with a backed anndata object. If chunk size is not
    provided, defaults to 20000.
    """
    if chunk_size is None:
        chunk_size = DEFAULT_CHUNK_SIZE

    for start in range(0, adata.n_obs, chunk_size):
        stop = min(start + chunk_size, adata.n_obs)
        yield start, stop, read_response_rows(adata, start, stop)

read_response_rows(adata, start, stop)

Read rows [start:stop] of adata.X

This only applies to CSR-oriented data. We read only views and never materialize the entire anndata object.

Source code in scdesigner/src/scdesigner/data/response.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def read_response_rows(adata: AnnData, start: int, stop: int) -> np.ndarray:
    """Read rows ``[start:stop]`` of ``adata.X``

    This only applies to CSR-oriented data. We read only views and never
    materialize the entire anndata object.
    """
    # check if the data is CSR format
    source, rows, cols = _source_selection(adata, start, stop)
    X = source.X
    if isinstance(X, CSCDataset):
        raise ValueError(BACKED_CSC_MESSAGE)

    # read data in memory
    if not isinstance(X, (h5py.Dataset, CSRDataset)):
        values = X[rows]
        values = values.toarray() if sp.issparse(values) else np.asarray(values)
        return values[:, cols]

    # Sort and deduplicate for backed reads; retain the requested row order.
    unique_rows, row_order = np.unique(rows, return_inverse=True)
    reader = _read_dense if isinstance(X, h5py.Dataset) else _read_csr
    values = reader(X, unique_rows)
    return values[np.ix_(row_order, cols)]