Ejemplo n.º 1
0
def _get_shaped_data(
    adata: AnnData,
    per: str,
    annotations: Annotations,
    *,
    shape: Tuple[int, ...],
    name: Union[str, utt.Shaped],
) -> Any:
    if isinstance(name, str):
        if per == "vo" and name == "__x__":
            data = _fix_data(adata.X)
        else:
            if name not in annotations:  # type: ignore
                raise _unknown_data(adata, name, per)
            data = _fix_data(annotations[name])

        if not utt.frozen(data):
            utt.freeze(data)

    else:
        if utt.is_1d(name):
            data = utt.to_numpy_vector(name)
        else:
            data = utt.to_proper_matrix(name)  # type: ignore

    assert data.shape == shape
    return data
Ejemplo n.º 2
0
def get_vo_frame(
    adata: AnnData,
    name: Union[str, utt.Matrix] = "__x__",
    *,
    layout: Optional[str] = None,
    formatter: Optional[Callable[[Any], Any]] = None,
) -> utt.PandasFrame:
    """
    Get per-variable-per-observation (per-gene-per-cell) data as a pandas data frame.

    Rows are observations (cells), indexed by the observation names (typically cell barcode).
    Columns are variables (genes), indexed by their names.

    If ``name`` is a string, it is the name of a per-variable annotation to fetch.
    Otherwise, it should be some matrix of data of the appropriate size.

    If ``layout`` (default: {layout}) is specified, it must be one of ``row_major`` or
    ``column_major``. If this requires relayout of the data, the result is cached in a hidden data
    member for future reuse.
    """
    data = _get_vo_data(adata, name, layout=layout, formatter=formatter)
    frame = utt.maybe_pandas_frame(data)
    if frame is None:
        frame = utt.to_pandas_frame(utt.to_proper_matrix(data),
                                    index=adata.obs_names,
                                    columns=adata.var_names)
    return frame
Ejemplo n.º 3
0
def get_va_frame(
    adata: AnnData,
    name: Union[str, utt.Matrix],
    *,
    columns: Optional[Collection],
    layout: Optional[str] = None,
    formatter: Optional[Callable[[Any], Any]] = None,
) -> utt.PandasFrame:
    """
    Get per-variable-per-any (per-cell-per-any) data as a pandas data frame.

    Rows are variables (genes), indexed by their names.
    Columns are "something" - specify ``columns`` to specify an index.

    If ``name`` is a string, it is the name of a per-variable annotation to fetch.
    Otherwise, it should be some matrix of data of the appropriate size.

    If ``layout`` (default: {layout}) is specified, it must be one of ``row_major`` or
    ``column_major``. If this requires relayout of the data, the result is cached in a hidden data
    member for future reuse.
    """
    data = _get_va_data(adata, name, layout=layout, formatter=formatter)
    frame = utt.maybe_pandas_frame(data)
    if frame is None:
        frame = utt.to_pandas_frame(utt.to_proper_matrix(data),
                                    index=adata.var_names,
                                    columns=columns)
    return frame
Ejemplo n.º 4
0
def get_vo_proper(
    adata: AnnData,
    name: Union[str, utt.Matrix] = "__x__",
    *,
    layout: Optional[str] = None,
    formatter: Optional[Callable[[Any], Any]] = None,
) -> utt.ProperMatrix:
    """
    Same as ``get_vo_data`` but returns a :py:const:`metacells.utilities.typing.ProperMatrix`.
    """
    data = _get_vo_data(adata, name, layout=layout, formatter=formatter)
    return utt.to_proper_matrix(data, default_layout=layout or "row_major")