Skip to content

Use selections downstream

Carry lasso, rectangle, and inspect selections into pandas, Scanpy, or other Python analysis.

Goal

Turn the cells inside a map selection into an observation subset or a boolean column on adata.obs.

Steps

  1. Open milume on AnnData with spatial coordinates (ileum demo):
import milume as ml

adata = ml.ds.ileum_merfish_xu2026()
w = ml.peek(adata, color="cell_type")
w
  1. Draw a selection in the browser (Select regions). Each edit updates w.selections.

  2. Read the latest selection id and the cell names it covers:

sel_id = w.selections[-1]["id"]
hits = w.get_obs_names(sel_id)  # pd.Index of obs_names
  1. Join to observations by name (not row index):
adata.obs["in_selection"] = adata.obs_names.isin(hits)
adata_sel = adata[hits].copy()
  1. Run downstream code on the subset or the flag. Example summary table:
import pandas as pd

summary = pd.DataFrame(
    {"cell_type": adata.obs.loc[hits, "cell_type"].value_counts()}
)

Use w.get_obs_names("focus") when the focused selection in the UI is the one you want (Methods).

Result

hits and adata_sel (or adata.obs["in_selection"]) stay aligned with the map. Re-run steps 3–5 after you change selections in the widget.

Landmarks (w.landmarks) are durable geometry; selections are analysis regions. Traitlets and edge cases are in the LandmarksWidget API.