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
def set_vo_data( adata: AnnData, name: str, data: utt.ProperMatrix, *, formatter: Optional[Callable[[Any], Any]] = None, ) -> Any: """ Set per-variable-per-observation (per-gene-per-cell) data. """ utl.log_set(adata, "vo", name, data, formatter=formatter) utt.mustbe_canonical(data) if not utt.frozen(data): utt.freeze(data) if name == "__x__": adata.X = data else: adata.layers[name] = data if hasattr(adata, "__derived__"): derived = getattr(adata, "__derived__") for layout in ["column_major", "row_major"]: layout_name = f"vo:{name}:{layout}" if layout_name in derived: del derived[layout_name]
def set_va_data(adata: AnnData, name: str, data: utt.ProperMatrix, *, formatter: Optional[Callable[[Any], Any]] = None) -> Any: """ Set per-variable-per-any (gene) data. If ``formatter`` is specified, its results is used when logging the operation. """ utl.log_set(adata, "va", name, data, formatter=formatter) utt.mustbe_canonical(data) if not utt.frozen(data): utt.freeze(data) adata.varm[name] = data
def set_o_data( adata: AnnData, name: str, data: utt.NumpyVector, *, formatter: Optional[Callable[[Any], Any]] = None, ) -> Any: """ Set per-observation (cell) data. If ``formatter`` is specified, its results is used when logging the operation. """ utl.log_set(adata, "o", name, data, formatter=formatter) if not isinstance(data, list): utt.mustbe_canonical(data) if not utt.frozen(data): utt.freeze(data) adata.obs[name] = data
def _get_layout_data( adata: AnnData, per: str, annotations: Annotations, *, shape: Tuple[int, ...], name: Union[str, utt.Matrix], layout: Optional[str], ) -> Any: data = _get_shaped_data(adata, per, annotations, shape=shape, name=name) if not isinstance(name, str): if not utt.is_layout(data, layout): assert layout is not None data = utc.to_layout(name, layout=layout) return data if utt.is_layout(data, layout): return data assert layout in utt.LAYOUT_OF_AXIS layout_name = f"{per}:{name}:{layout}" if not hasattr(adata, "__derived__"): derived: Dict[str, utt.ProperMatrix] = {} setattr(adata, "__derived__", derived) else: derived = getattr(adata, "__derived__") layout_data = derived.get(layout_name) if layout_data is not None: assert layout_data.shape == shape assert utt.is_layout(layout_data, layout) else: layout_data = utc.to_layout(data, layout=layout) derived[layout_name] = layout_data if not utt.frozen(layout_data): utt.freeze(layout_data) return layout_data