scdecon.simulation¶
simulation ¶
Pseudobulk simulation: synthetic bulk with known ground-truth proportions.
Operates purely on in-memory AnnData objects (no file I/O). See
:func:~scdecon.simulation.pseudobulk.split_reference to create disjoint
signature / held-out partitions and avoid leakage.
ProportionPrior ¶
Bases: StrEnum
Prior distribution used to draw per-sample cell-type proportions.
SimulationConfig
dataclass
¶
SimulationConfig(n_samples: int = 100, n_cells_per_sample: int = 500, cell_type_key: str = 'cell_type', proportion_prior: ProportionPrior = ProportionPrior.DIRICHLET, dirichlet_alpha: float = 1.0, random_state: int = 0, counts_layer: str | None = None)
Immutable configuration for pseudobulk simulation.
Attributes:
| Name | Type | Description |
|---|---|---|
n_samples |
int
|
Number of pseudobulk samples to generate. |
n_cells_per_sample |
int
|
Number of single cells summed into each pseudobulk sample. |
cell_type_key |
str
|
Column in |
proportion_prior |
ProportionPrior
|
Prior for drawing target proportions (:class: |
dirichlet_alpha |
float
|
Symmetric Dirichlet concentration (only used for the Dirichlet prior).
|
random_state |
int
|
Seed for the simulation's random generator (full reproducibility). |
counts_layer |
str | None
|
Name of the |
BaseSimulator ¶
Bases: ABC
Abstract strategy for generating pseudobulk datasets.
simulate
abstractmethod
¶
Generate a :class:PseudobulkDataset from single-cell data.
CellSumSimulator ¶
PseudobulkDataset
dataclass
¶
A simulated pseudobulk dataset with its ground-truth proportions.
Attributes:
| Name | Type | Description |
|---|---|---|
bulk |
DataFrame
|
Summed raw counts, genes (index) x samples (columns). |
proportions |
DataFrame
|
Realised ground-truth proportions, cell types (index) x samples
(columns). Each column is non-negative and sums to 1. The orientation
matches :func: |
simulate_pseudobulk ¶
simulate_pseudobulk(adata: AnnData, config: SimulationConfig, simulator: BaseSimulator | None = None) -> PseudobulkDataset
Generate a pseudobulk dataset using simulator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
Single-cell data (raw counts in |
required |
config
|
SimulationConfig
|
Simulation parameters. |
required |
simulator
|
BaseSimulator | None
|
Strategy to use. Defaults to :class: |
None
|
Returns:
| Type | Description |
|---|---|
PseudobulkDataset
|
Summed-count bulk (genes × samples) and realised proportions (cell types × samples). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
split_reference ¶
split_reference(adata: AnnData, cell_type_key: str = 'cell_type', *, signature_fraction: float = 0.5, random_state: int = 0) -> tuple[anndata.AnnData, anndata.AnnData]
Split cells into disjoint signature and held-out partitions.
The split is deterministic (seeded) and stratified by cell type: each cell type is shuffled and split independently, so both partitions retain the cell-type structure. Use the signature partition to build the signature and the held-out partition to simulate pseudobulk, preventing leakage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adata
|
AnnData
|
Single-cell data to split. Not modified (copies are returned). |
required |
cell_type_key
|
str
|
Column in |
'cell_type'
|
signature_fraction
|
float
|
Fraction of each cell type assigned to the signature partition, in
|
0.5
|
random_state
|
int
|
Seed for the deterministic shuffle. |
0
|
Returns:
| Type | Description |
|---|---|
tuple[AnnData, AnnData]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |