Download notebook

03 · Create mixed spots with known cell-class proportions

Simulate cells from MERFISH section Zhuang-ABCA-1.007 and aggregate them into spots. The source cells’ cell_class labels provide composition ground truth. Data and environment setup

[1]:
from pathlib import Path
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import FEAST

TUTORIAL = Path.cwd() if Path.cwd().name == "tutorial" else Path.cwd() / "tutorial"
sys.path.insert(0, str(TUTORIAL))
from _utils import data_root, load_counts, spatial_panel
DATA = data_root()

OUT = TUTORIAL / "outputs" / "03"
OUT.mkdir(parents=True, exist_ok=True)
print("FEAST", FEAST.__version__)
FEAST 1.0.6
[2]:
from FEAST.deconvolution import create_deconvolution_benchmark_data
reference = load_counts(DATA / "merfish/Zhuang-ABCA-1.007.h5ad", "cell_class")

Simulate single-cell counts

Keep the original cell locations and disable overshoot clipping to retain integer simulated counts.

[3]:
%%capture --no-stderr
cells = FEAST.simulate(
    reference, seed=2026, parameter_mode="hungarian", spatial_mode="reference_rank",
    assignment_solver="scipy", assignment_blocks=False,
    clip_overshoot_factor=0.0, verbose=False,
)

Aggregate cells into spots

Request roughly one hexagonal grid center per four cells. FEAST trims the grid to the tissue boundary and sums the counts assigned to each center.

[4]:
spots = create_deconvolution_benchmark_data(
    cells, downsampling_factor=0.25, grid_type="hexagonal",
    cell_type_key="cell_class", alpha=0.01,
)
truth = pd.DataFrame(spots.obsm["cell_type_proportions"],
                     index=spots.obs_names, columns=spots.uns["cell_type_names"])
n_cells = np.bincount(spots.uns["spot_assignments"], minlength=spots.n_obs)
spots.obs["n_source_cells"] = n_cells
np.testing.assert_allclose(truth.sum(axis=1), (n_cells > 0).astype(float))
print(f"{cells.n_obs:,} cells → {spots.n_obs:,} centers; {(n_cells == 0).sum()} empty centers")
9,693 cells → 1,959 centers; 201 empty centers

Plot cell-class proportions

Plot the three most abundant classes. A value of 0.6 means that 60% of the cells assigned to that spot belong to the class.

[5]:
shown = reference.obs["cell_class"].value_counts().head(3).index
fig, axes = plt.subplots(1, 3, figsize=(11, 3.5), layout="constrained")
for ax, label in zip(axes, shown):
    spatial_panel(ax, spots.obsm["spatial"], truth[label], str(label), vmax=1)
plt.show()
truth.head().round(3)
../_images/notebooks_03_deconvolution_8_0.png
[5]:
02 NP-CT-L6b Glut 03 OB-CR Glut 05 OB-IMN GABA 06 CTX-CGE GABA 09 CNU-LGE GABA 10 LSX GABA 11 CNU-HYa GABA 12 HY GABA 13 CNU-HYa Glut 29 CB Glut 30 Astro-Epen 31 OPC-Oligo 32 OEC 33 Vascular 34 Immune
0 0.0 0.0 0.000 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.000 0.0 0.0
1 0.0 0.0 0.000 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.000 0.0 0.0
2 0.0 0.0 0.000 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.000 0.0 0.0
3 0.0 0.0 0.000 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.000 1.0 0.0
4 0.0 0.0 0.667 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.333 0.0 0.0
[6]:
spots.write_h5ad(OUT / "spatial_mixtures.h5ad")
truth.to_csv(OUT / "cell_class_truth.csv")
reference.write_h5ad(OUT / "single_cell_reference.h5ad")

Empty centers have zero proportions for every class. The exported truth table includes all classes for scoring deconvolution predictions.