Source code for trackcell.tl.spatial

"""
Tools for spatial analyses.
"""

import numpy as np
import pandas as pd
from scipy.sparse import issparse
from scipy.spatial import cKDTree
from scipy.spatial.distance import cdist
from typing import Any, List, Optional
import warnings

from .._logging import get_logger

logger = get_logger(__name__)


# Perceptually distinct colors for up to 7 genes (ColorBrewer-inspired)
DEFAULT_GENE_COLORS = [
    "#e41a1c",  # Red
    "#377eb8",  # Blue
    "#4daf4a",  # Green
    "#ff7f00",  # Orange
    "#984ea3",  # Purple
    "#a65628",  # Brown
    "#f781bf",  # Pink
]


def _hex_to_rgb(hex_color: str) -> tuple:
    """Convert hex color string to (R, G, B) tuple in [0, 1]."""
    h = hex_color.lstrip("#")
    return tuple(int(h[i : i + 2], 16) / 255.0 for i in (0, 2, 4))


def _rgb_to_hex(r: float, g: float, b: float) -> str:
    """Convert (R, G, B) in [0, 1] to hex string."""

    def clamp(v):
        return max(0, min(255, int(round(v * 255))))

    return f"#{clamp(r):02x}{clamp(g):02x}{clamp(b):02x}"


def _multigene_facet(
    adata,
    genes: List[str],
    colors: List[str],
    layer: Optional[str] = None,
    vmin_percentile: float = 1.0,
    vmax_percentile: float = 99.0,
    ncols: int = 3,
    figsize: Optional[tuple] = None,
    edges_width: float = 0.3,
    edges_color: str = "#cccccc30",
    alpha: float = 0.8,
    show: bool = True,
    ax=None,
    library_id: Optional[str] = None,
):
    """Faceted multi-gene subplots, one gene per panel with single-hue colormap."""
    import matplotlib.pyplot as plt
    from matplotlib.colors import LinearSegmentedColormap

    n_genes = len(genes)
    nrows = int(np.ceil(n_genes / ncols))

    if figsize is None:
        figsize = (5 * ncols, 5 * nrows)

    if ax is None:
        fig, axes = plt.subplots(nrows, ncols, figsize=figsize, squeeze=False)
    else:
        axes = np.atleast_2d(np.asarray(ax))
        fig = axes.flat[0].figure

    axes_flat = axes.flatten()
    temp_keys: list[str] = []

    try:
        for i, (gene, color) in enumerate(zip(genes, colors)):
            ax_i = axes_flat[i]
            # Single-hue colormap: white → gene color
            gene_cmap = LinearSegmentedColormap.from_list(
                f"_{gene}", ["#fafafa", color]
            )

            # Extract per-cell values (respecting ``layer``) for percentile
            # clipping.  spatial_cell() reads gene expression from .X, so a
            # layer requires a temporary obs column.
            values = adata[:, gene].X
            if layer is not None:
                values = adata[:, gene].layers[layer]
            if hasattr(values, "toarray"):
                values = values.toarray()
            values = np.asarray(values, dtype=float).ravel()
            finite = values[np.isfinite(values)]
            if finite.size:
                vmin = float(np.percentile(finite, vmin_percentile))
                vmax = float(np.percentile(finite, vmax_percentile))
            else:
                vmin, vmax = None, None

            plot_key = gene
            if layer is not None:
                plot_key = f"__facet_{gene}"
                adata.obs[plot_key] = values
                temp_keys.append(plot_key)

            from trackcell.pl.plot import spatial_cell

            spatial_cell(
                adata,
                color=plot_key,
                cmap=gene_cmap,
                vmin=vmin,
                vmax=vmax,
                ax=ax_i,
                show=False,
                legend=True,
                edges_width=edges_width,
                edges_color=edges_color,
                alpha=alpha,
                library_id=library_id,
            )
            ax_i.set_title(gene, fontsize=12, fontweight="bold")
    finally:
        for key in temp_keys:
            if key in adata.obs.columns:
                del adata.obs[key]

    # Hide unused axes
    for j in range(n_genes, len(axes_flat)):
        axes_flat[j].set_visible(False)

    fig.tight_layout(rect=[0, 0, 1, 1])

    if show:
        plt.show()

    return axes


[docs] def multigene_blend( adata, genes: List[str], mode: str = "blend", colors: Optional[List[str]] = None, layer: Optional[str] = None, vmin_percentile: float = 1.0, vmax_percentile: float = 99.0, gamma: float = 1.0, background: str = "#e0e0e0", key_added: str = "multigene_blend", # Facet-specific ncols: int = 3, figsize: Optional[tuple] = None, edges_width: float = 0.3, edges_color: str = "#cccccc30", alpha_facet: float = 0.8, show: bool = True, ax=None, library_id: Optional[str] = None, inplace: bool = True, ): """ Multi-gene co-expression visualization with two modes. **Mode 'blend'** (default): Computes blended hex colors by weighted RGB averaging of per-gene base colors. The result is a hex color per cell stored in ``adata.obs[key_added]``, usable with ``spatial_cell(color=key_added)``. The blend encodes **relative composition**, not absolute expression level: because the per-gene weights sum to 1, a cell with one UMI of gene A and a cell with 1,000 UMIs of gene A get the same colour when no other gene is expressed. Use ``mode='facet'`` (or a single-gene ``spatial_cell`` plot) when intensity matters. **Mode 'facet'**: Draws faceted subplots, one gene per panel, each using a single-hue colormap (white → gene color). Returns matplotlib axes. Parameters ---------- adata : AnnData Annotated data with gene expression. genes : list of str Gene names. Up to 7 recommended for blend mode. mode : str 'blend' (default) or 'facet'. colors : list of str, optional Base colors for each gene (hex). Default: perceptually distinct palette. layer : str, optional Use adata.layers[layer] instead of adata.X. vmin_percentile : float Lower percentile for clipping (blend & facet, default 1). vmax_percentile : float Upper percentile for clipping (blend & facet, default 99). gamma : float Gamma correction for blend mode (>1 darkens, <1 brightens). background : str Background hex color for blend mode (zero-expression cells). key_added : str obs column name for blend mode output. inplace : bool If True (blend), stores in adata.obs. If False, returns Series. **Facet-specific parameters:** ncols : int Number of columns in facet grid (default 3). figsize : tuple, optional Figure size. Auto-computed from ncols if None. edges_width : float Cell edge width for facet plots (default 0.3). edges_color : str Cell edge color for facet plots (default '#cccccc30'). alpha_facet : float Cell transparency for facet plots (default 0.8). show : bool Whether to call plt.show() (default True). ax : matplotlib.Axes or array, optional Pre-existing axes to plot on. library_id : str, optional Library id passed to spatial_cell. Returns ------- blend mode: pd.Series or None facet mode: np.ndarray of matplotlib.Axes Examples -------- >>> import trackcell as tcl >>> >>> # Mode 1: Blend → single composite image >>> tcl.tl.multigene_blend(adata, genes=['EPCAM', 'PECAM1', 'VWF']) >>> tcl.pl.spatial_cell(adata, color='multigene_blend') >>> >>> # Mode 2: Facet → subplots per gene >>> tcl.tl.multigene_blend( ... adata, genes=['EPCAM', 'PECAM1', 'VWF'], ... mode='facet', ncols=3, ... ) """ n_genes = len(genes) if n_genes < 2: raise ValueError(f"Need at least 2 genes, got {n_genes}") if len(set(genes)) != n_genes: raise ValueError(f"`genes` contains duplicates: {genes}") if not (0 <= vmin_percentile < vmax_percentile <= 100): raise ValueError( "Need 0 <= vmin_percentile < vmax_percentile <= 100, got " f"({vmin_percentile}, {vmax_percentile})" ) # Validate genes missing = [g for g in genes if g not in adata.var_names] if missing: raise ValueError(f"Genes not found in adata.var_names: {missing}") # Normalise colours early: matplotlib accepts names and RGB tuples, but the # blend math needs hex and ``params['colors']`` is what the legend reads. from matplotlib.colors import is_color_like, to_hex if colors is not None and len(colors) != n_genes: raise ValueError( f"colors length ({len(colors)}) must match genes length ({n_genes})" ) # Default colors if colors is None: if n_genes <= len(DEFAULT_GENE_COLORS): colors = DEFAULT_GENE_COLORS[:n_genes] else: # Extend the palette beyond the 7 hand-picked colours. A cyclic # qualitative map keeps genes distinguishable for 8-20 genes. from matplotlib import colormaps cmap = colormaps.get_cmap("tab20" if n_genes <= 20 else "hsv") colors = [to_hex(cmap(i / max(n_genes, 1))) for i in range(n_genes)] normalized_colors = [] for color in colors: if not is_color_like(color): raise ValueError( f"`colors` contains {color!r}, which is not a matplotlib colour." ) normalized_colors.append(to_hex(color)) colors = normalized_colors if mode == "facet": return _multigene_facet( adata=adata, genes=genes, colors=colors, layer=layer, vmin_percentile=vmin_percentile, vmax_percentile=vmax_percentile, ncols=ncols, figsize=figsize, edges_width=edges_width, edges_color=edges_color, alpha=alpha_facet, show=show, ax=ax, library_id=library_id, ) if mode != "blend": raise ValueError(f"Unknown mode '{mode}'. Valid modes: 'blend', 'facet'.") # ── Blend mode ── if n_genes > 7: warnings.warn( f"{n_genes} genes may produce muddy blends. " f'Consider using <= 7 genes or mode="facet".' ) if not np.isfinite(gamma) or gamma <= 0: raise ValueError(f"`gamma` must be a positive finite number, got {gamma!r}.") if not is_color_like(background): raise ValueError( f"`background` must be a matplotlib colour, got {background!r}." ) background = to_hex(background) # ── Extract expression matrix ── X = adata[:, genes].X if layer is None else adata[:, genes].layers[layer] # ``issparse`` rather than ``hasattr(X, 'toarray')``: AnnData's dense # ``ArrayView`` also exposes ``toarray``, so the old check skipped the float # cast for every integer-valued ``.X`` and the normalised values were then # truncated to zero by ``np.zeros_like``. if issparse(X): X = X.toarray() X = np.asarray(X, dtype=float) if not np.isfinite(X).all(): raise ValueError("Expression matrix contains non-finite values.") if (X < 0).any(): raise ValueError( "multigene_blend needs non-negative expression values; " "scaled/centred layers produce meaningless colour mixtures." ) n_cells = X.shape[0] # ── Convert base colors to RGB ── base_rgb = np.array([_hex_to_rgb(c) for c in colors]) # (n_genes, 3) # ── Per-gene normalization (percentile clipping) ── X_norm = np.zeros_like(X, dtype=float) for j in range(n_genes): col = X[:, j] vmin = float(np.percentile(col, vmin_percentile)) vmax = float(np.percentile(col, vmax_percentile)) # A gene expressed in fewer cells than the upper tail of the percentile # range (e.g. 3 cells in 1000 with vmax_percentile=99) has vmax == 0. # Falling through would zero the whole gene and paint its cells as # background, silently dropping exactly the rare markers a co-expression # map is often used to find. Fall back to the observed range. if vmax <= vmin: vmax = float(col.max()) vmin = min(vmin, float(col.min())) if vmax > vmin: col_clipped = np.clip(col, vmin, vmax) X_norm[:, j] = (col_clipped - vmin) / (vmax - vmin) else: X_norm[:, j] = 0.0 # ── Weighted RGB blending ── # weight_i = norm_expr_i / (sum(norm_expr) + eps) # RGB = Σ(weight_i * base_rgb_i) total = X_norm.sum(axis=1, keepdims=True) # (n_cells, 1) weights = np.divide(X_norm, total, where=total > 0) # (n_cells, n_genes) rgb = weights @ base_rgb # (n_cells, 3) # ── Gamma correction ── if gamma != 1.0: rgb = np.power(rgb, gamma) # ── Convert to hex ── hex_colors = [] for i in range(n_cells): if total[i, 0] > 0: hex_colors.append(_rgb_to_hex(rgb[i, 0], rgb[i, 1], rgb[i, 2])) else: hex_colors.append(background) result = pd.Series(hex_colors, index=adata.obs_names, name=key_added) # ── Store metadata ── params = { "genes": genes, "colors": colors, "vmin_percentile": vmin_percentile, "vmax_percentile": vmax_percentile, "gamma": gamma, "background": background, "n_unique_colors": result.nunique(), } result.attrs.update(params) if inplace: adata.obs[key_added] = result adata.uns[f"{key_added}_params"] = params logger.info( '[multigene_blend] %s genes → %s unique colors in adata.obs["%s"]', n_genes, f"{result.nunique():,}", key_added, ) return None return result
def _resolve_labeldist_scalefactors( adata, library_id: Optional[str], microns_per_pixel: Optional[float], hires_scale: Optional[float], ) -> tuple[str, float, Optional[float], bool]: """Return ``(library_id, um_per_unit, hires_scale, coordinates_are_pixels)``. The factor is resolved through :func:`trackcell.pl.resolve_microns_per_unit`, which is the single source of truth for coordinate units in the package. An explicit ``microns_per_pixel`` is treated as a pixel-size factor (the historical meaning of the argument), so callers that pass it keep the fullres/hires behaviour. """ spatial_meta = adata.uns.get("spatial") if not spatial_meta: raise ValueError("`adata.uns['spatial']` is missing scalefactor information.") if library_id is None: available = list(spatial_meta.keys()) if len(available) == 0: raise ValueError("No library_id found in `adata.uns['spatial']`.") library_id = available[0] if len(available) > 1: warnings.warn( f"Multiple library_ids found: {available}. Using '{library_id}'. " "Specify `library_id` explicitly to use a different one." ) elif library_id not in spatial_meta: raise ValueError( f"`library_id` '{library_id}' not found in `adata.uns['spatial']`. " f"Available library_ids: {list(spatial_meta.keys())}" ) if microns_per_pixel is not None: resolved_factor = float(microns_per_pixel) coordinates_are_pixels = True else: # Deferred import keeps ``tl`` free of an import-time dependency on ``pl``. from ..pl.scalebar import resolve_microns_per_unit resolved_factor, unit_family = resolve_microns_per_unit(adata, library_id) coordinates_are_pixels = unit_family == "px" if not np.isfinite(resolved_factor) or resolved_factor <= 0: raise ValueError( "A positive microns-per-unit factor is required. Provide " "`microns_per_pixel`, or include the coordinate unit in the object's " "spatial metadata." ) scalefactors = spatial_meta[library_id].get("scalefactors", {}) resolved_hires_scale = hires_scale if resolved_hires_scale is None: resolved_hires_scale = scalefactors.get( "tissue_hires_scalef" ) or scalefactors.get("regist_target_img_scalef") if resolved_hires_scale is not None and ( not np.isfinite(resolved_hires_scale) or resolved_hires_scale <= 0 ): raise ValueError("`hires_scale` must be positive when provided.") return ( library_id, float(resolved_factor), (float(resolved_hires_scale) if resolved_hires_scale is not None else None), coordinates_are_pixels, ) def _infer_labeldist_coordinate_system( coords_all: np.ndarray, microns_per_pixel: float, hires_scale: Optional[float], ) -> str: if hires_scale is None or hires_scale >= 0.99: return "fullres" coord_range = np.ptp(coords_all[:, :2], axis=0) max_range_px = float(np.nanmax(coord_range)) if not np.isfinite(max_range_px) or max_range_px <= 0: warnings.warn( "Could not infer spatial coordinate resolution from coordinate range; " "using coordinate_system='fullres'." ) return "fullres" size_um_fullres = max_range_px * microns_per_pixel size_um_hires = max_range_px * (microns_per_pixel / hires_scale) expected_max_size_um = 7000 fullres_reasonable = size_um_fullres <= expected_max_size_um hires_reasonable = size_um_hires <= expected_max_size_um if hires_reasonable and not fullres_reasonable: return "hires" if fullres_reasonable and not hires_reasonable: return "fullres" resolved = "hires" if hires_scale < 0.5 else "fullres" warnings.warn( "Could not confidently infer whether `adata.obsm['spatial']` is in fullres " f"or hires pixels (fullres span={size_um_fullres:.1f} um, " f"hires span={size_um_hires:.1f} um). Using coordinate_system='{resolved}'. " "Pass `coordinate_system='fullres'` or `coordinate_system='hires'` explicitly " "to avoid this heuristic." ) return resolved
[docs] def hd_labeldist( adata, groupby: str, label: str, inplace: bool = True, method: str = "kdtree", coordinate_system: str = "auto", library_id: Optional[str] = None, microns_per_pixel: Optional[float] = None, hires_scale: Optional[float] = None, ): """ Compute the distance from every cell to the nearest cell annotated with a specific label. Works for Visium HD (``obsm['spatial']`` in image pixels) and for platforms whose coordinates already have a physical scale: Xenium / Atera (µm) and STOmics GEF (resolution units, typically 500 nm). The conversion factor is resolved through :func:`trackcell.pl.resolve_microns_per_unit`, so it cannot disagree with scale bars or :func:`cells_in_radius` on the same object. Distances are reported both in the pixel coordinate system stored in `.obsm["spatial"]` (SpaceRanger target/hires layer) and in microns using the scalefactors embedded in `adata.uns["spatial"]`. By default, the function preserves the previous auto-detection heuristic for hires vs full-res coordinates. For reproducible distances, pass ``coordinate_system='fullres'`` or ``coordinate_system='hires'`` explicitly. Parameters ---------- adata : sc.AnnData Annotated data matrix with spatial coordinates and SpaceRanger scalefactors. groupby : str Column name in `adata.obs` that contains the annotation labels. label : str Target label within `groupby` for which distances will be computed. inplace : bool, default True If True, the function adds two columns to `adata.obs`: `{label}_px` (distance in the raw coordinate units of `obsm['spatial']`: pixels for Visium HD, µm for Xenium / Atera, resolution units for STOmics) and `{label}_dist` (physical distance in microns). If False, the function returns a dataframe with the two columns. method : str, default "kdtree" Method to use for distance computation: - "kdtree": Use KDTree spatial indexing (recommended, O(n log n) time, O(n) memory). Best for large datasets with many cells. - "cdist": Use scipy's cdist function (O(n*m) time and memory, where m is number of label cells). Faster for small datasets but memory-intensive for large ones. coordinate_system : {'auto', 'fullres', 'hires'}, default 'auto' Pixel coordinate system used by `adata.obsm['spatial']`. `fullres` uses `microns_per_pixel` directly; `hires` divides by `hires_scale`; `auto` uses a heuristic and warns when ambiguous. Only meaningful for pixel coordinates: when the object declares a physical unit (µm / resolution units), `auto` resolves to ``'data'``, `fullres` is ignored with a warning, and `hires` raises. library_id : str, optional Spatial library id in `adata.uns['spatial']`. Defaults to the first one. microns_per_pixel : float, optional Explicit pixel size (µm per pixel). Treated as a pixel-coordinate factor, so fullres/hires handling stays available. Do not pass it for Xenium / Atera objects, whose coordinates are already µm. hires_scale : float, optional Override `tissue_hires_scalef` / `regist_target_img_scalef`. Returns ------- pandas.DataFrame | None Returns a DataFrame with `{label}_px` and `{label}_dist` when `inplace=False`. Otherwise, modifies `adata.obs` in place and returns None. """ if "spatial" not in adata.obsm: raise ValueError("`adata.obsm['spatial']` is required but missing.") if groupby not in adata.obs.columns: raise ValueError(f"`{groupby}` not found in `adata.obs`.") if label not in adata.obs[groupby].unique(): raise ValueError(f"`{label}` not present in `adata.obs['{groupby}']`.") coordinate_system = coordinate_system.lower() if coordinate_system not in {"auto", "fullres", "hires"}: raise ValueError("`coordinate_system` must be 'auto', 'fullres', or 'hires'.") coords = adata.obsm["spatial"] if coords is None or len(coords) == 0: raise ValueError("Spatial coordinates are empty.") mask_label = (adata.obs[groupby] == label).to_numpy() if not mask_label.any(): raise ValueError(f"No observations with label `{label}` were found.") coords_all = np.asarray(coords, dtype=float) if coords_all.ndim != 2 or coords_all.shape[1] < 2: raise ValueError("`adata.obsm['spatial']` must be an n_obs x >=2 array.") coords_all = coords_all[:, :2] coords_label = coords_all[mask_label] # Compute distances using selected method if method == "kdtree": # Method 1: KDTree (recommended for large datasets) # Memory: O(n + m), Time: O(n log m) where n=all cells, m=label cells tree = cKDTree(coords_label) dist_px, _ = tree.query(coords_all, k=1) elif method == "cdist": # Method 2: cdist (faster for small datasets, but memory-intensive) # Memory: O(n*m), Time: O(n*m) dist_matrix = cdist(coords_all, coords_label, metric="euclidean") dist_px = dist_matrix.min(axis=1) else: raise ValueError(f"Unknown method: {method}. Choose 'kdtree' or 'cdist'.") resolved_library_id, resolved_factor, resolved_hires_scale, is_pixel = ( _resolve_labeldist_scalefactors( adata, library_id=library_id, microns_per_pixel=microns_per_pixel, hires_scale=hires_scale, ) ) resolved_coordinate_system = coordinate_system if is_pixel: if coordinate_system == "auto": resolved_coordinate_system = _infer_labeldist_coordinate_system( coords_all, microns_per_pixel=resolved_factor, hires_scale=resolved_hires_scale, ) elif coordinate_system == "hires" and resolved_hires_scale is None: raise ValueError( "coordinate_system='hires' requires `hires_scale` or a hires scale " "in scalefactors." ) if resolved_coordinate_system == "hires": dist_um = dist_px * (resolved_factor / resolved_hires_scale) else: dist_um = dist_px * resolved_factor else: # Coordinates already carry a physical scale (µm for Xenium/Atera, # resolution units for STOmics). There is no fullres/hires distinction # to infer, and applying the metadata `pixel_size` here was the bug that # shrank Xenium distances by 0.2125. if coordinate_system == "hires": raise ValueError( "coordinate_system='hires' is only meaningful when " "`adata.obsm['spatial']` is in image pixels. This object declares " "a physical coordinate unit (µm or a fixed resolution unit)." ) if coordinate_system == "fullres": warnings.warn( "coordinate_system='fullres' is ignored: this object's " "coordinates already have a physical scale, so there is no " "fullres/hires distinction.", UserWarning, stacklevel=2, ) resolved_coordinate_system = "data" dist_um = dist_px * resolved_factor col_px = f"{label}_px" col_um = f"{label}_dist" params = { "groupby": groupby, "label": label, "method": method, "coordinate_system": resolved_coordinate_system, "requested_coordinate_system": coordinate_system, "library_id": resolved_library_id, "microns_per_pixel": resolved_factor, "hires_scale": resolved_hires_scale, "unit_family": "px" if is_pixel else "um", } if inplace: adata.obs[col_px] = dist_px adata.obs[col_um] = dist_um adata.uns[f"{label}_dist_params"] = params return None result = pd.DataFrame({col_px: dist_px, col_um: dist_um}, index=adata.obs.index) result.attrs["params"] = params return result
[docs] def cells_in_radius( adata: Any, center, radius: float, *, radius_unit: str = "um", basis: str = "spatial", library_id: Optional[str] = None, microns_per_unit: Optional[float] = None, include_boundary: bool = True, return_distances: bool = False, ) -> Any: """Select cells inside a circle defined by a center point and a physical radius. The intended workflow is to draw an overview plot, pick a point in the tissue (``ax.transData.inverted()`` on a click, ``plt.ginput``, or plain coordinates), and then keep every cell within a given distance of it. Because ``radius`` is a *physical* length by default, the same call means the same thing on data whose coordinates are µm (Xenium, Atera, MERSCOPE) and on data whose coordinates are image pixels (Visium, Visium HD). Parameters ---------- adata Annotated data object with spatial coordinates in ``adata.obsm[basis]``. center : tuple of float Circle center ``(x, y)`` **in the coordinate system of ``adata.obsm[basis]``** — i.e. the numbers you read off the plot. radius : float Circle radius, in ``radius_unit``. radius_unit : {'um', 'data'}, default 'um' ``'um'`` interprets ``radius`` as micrometers and converts it to data units through :func:`trackcell.pl.resolve_microns_per_unit`. ``'data'`` uses raw ``obsm`` units, which is what you want if the coordinates are already in µm and you prefer to say so explicitly. basis : str, default 'spatial' Key in ``adata.obsm`` holding the 2D coordinates. library_id : str, optional Spatial library id used for the µm conversion (defaults to the first). microns_per_unit : float, optional Explicit µm per data unit, overriding metadata inference. include_boundary : bool, default True Whether cells at ``distance == radius`` are kept (``<=`` vs ``<``). return_distances : bool, default False When ``True`` return ``(mask, distances)`` with distances in µm. Returns ------- numpy.ndarray or tuple Boolean mask aligned with ``adata.obs_names``, usable directly as ``adata[mask].copy()``. With ``return_distances=True``, also the distance of every cell to ``center`` in µm. Examples -------- >>> import trackcell as tcl >>> center = (4500.0, 2600.0) # µm, read off the overview plot >>> mask = tcl.tl.cells_in_radius(adata, center, radius=150) >>> roi = adata[mask].copy() >>> tcl.pl.spatial_cell(roi, color="celltype", scalebar=True) Notes ----- For Visium HD pixel coordinates ``radius=150`` means 150 µm, not 150 px: ``scalefactors['microns_per_pixel']`` is applied automatically. """ if basis not in adata.obsm: raise ValueError(f"`adata.obsm['{basis}']` is required but missing.") coords = np.asarray(adata.obsm[basis]) if coords.ndim != 2 or coords.shape[1] < 2: raise ValueError( f"`adata.obsm['{basis}']` must be an n_obs × 2 array of coordinates." ) coords = coords[:, :2] center_arr = np.asarray(center, dtype=float).ravel() if center_arr.size != 2 or not np.all(np.isfinite(center_arr)): raise ValueError(f"`center` must be two finite numbers (x, y), got {center!r}.") radius = float(radius) if not np.isfinite(radius) or radius <= 0: raise ValueError(f"`radius` must be a positive finite number, got {radius!r}.") unit = str(radius_unit).strip().lower() if unit in ("um", "µm", "μm", "micron", "microns", "micrometers"): # Deferred import: keeps `tl` free of an import-time dependency on `pl`. from ..pl.scalebar import resolve_microns_per_unit microns_per_unit, _ = resolve_microns_per_unit( adata, library_id, microns_per_unit=microns_per_unit, ) elif unit in ("data", "pixel", "px", "none", "unit"): microns_per_unit = 1.0 else: raise ValueError(f"`radius_unit` must be 'um' or 'data', got {radius_unit!r}.") radius_data = radius / float(microns_per_unit) distances = np.linalg.norm(coords - center_arr, axis=1) mask = np.asarray( distances <= radius_data if include_boundary else distances < radius_data ) if return_distances: return mask, distances * float(microns_per_unit) return mask