AnnData Integration
pyroNMF accepts an AnnData object as input and returns a copy of that
object with all results stored in obsm, varm, and uns. The original
input is never modified.
Input expectations
adata.X: non-negative counts matrix of shape(n_samples, n_genes). Dense (numpy.ndarray) and sparse (scipy.sparse) matrices are both supported.adata.obsm['spatial'](optional): 2-D coordinates of shape(n_samples, 2)for spatial visualisation. Required whenspatial=True.Named layers in
adata.layerscan be used instead of.Xvia thelayerargument.
Output structure
All outputs are stored as pandas.DataFrame objects (in obsm / varm)
or scalars / DataFrames (in uns). Pattern columns are named
Pattern_1, Pattern_2, … for learned patterns. Fixed-pattern columns
retain the column names from the input fixed_patterns DataFrame and appear
first (before the learned patterns) in semi-supervised runs.
obsm — sample-level factors
All keys are indexed by adata.obs_names.
Variational parameters
Key |
Shape |
Models |
Description |
|---|---|---|---|
|
|
exponential, gamma |
Variational rate/location of P — one value per sample per pattern. Present for per-element models; not present for |
Sampled P — last iteration
Key |
Shape |
Models |
Description |
|---|---|---|---|
|
|
all |
P sampled at the final optimisation step. |
Sampled P — best chi-squared snapshot
The model always tracks the minimum chi-squared step internally regardless
of whether use_chisq=True.
Key |
Shape |
Models |
Description |
|---|---|---|---|
|
|
all |
P sampled at the step with the lowest chi-squared. |
|
|
exponential, gamma |
|
Posterior statistics — sampling phase
Accumulated only during num_sample_steps steps. Before accumulation,
each column of P is divided by its sum so that patterns are on a
comparable scale (see Scale normalisation).
Key |
Shape |
Models |
Description |
|---|---|---|---|
|
|
all |
Posterior mean of normalised P across all sampling steps. Primary posterior estimate. |
|
|
all |
Posterior sample variance of normalised P. |
|
|
all |
Raw running sum of normalised P (= |
|
|
all |
Raw running sum of normalised P² (intermediate for |
Scaled patterns
Key |
Shape |
Description |
|---|---|---|
|
|
|
|
|
|
Marker matrices
Binary vote matrices accumulated over the sampling phase. At each sampling step, the dominant pattern for each sample wins a vote.
Key |
Shape |
Description |
|---|---|---|
|
|
Votes based on raw P: for each sample, the pattern with the highest P value wins. |
|
|
Votes based on column-sum-normalised P. |
|
|
Softmax-normalised P accumulated over sampling steps (soft assignment). |
Semi-supervised only — fixed_samples mode
Key |
Shape |
Description |
|---|---|---|
|
|
The user-supplied fixed sample patterns (unchanged). |
|
|
Fixed patterns concatenated with |
|
|
Fixed patterns concatenated with |
varm — gene-level factors
All keys are indexed by adata.var_names.
Variational parameters
Key |
Shape |
Models |
Description |
|---|---|---|---|
|
|
exponential, gamma |
Variational rate/location of A — one value per gene per pattern. Not present for |
Sampled A — last iteration
Key |
Shape |
Models |
Description |
|---|---|---|---|
|
|
all |
A sampled at the final optimisation step. |
Sampled A — best chi-squared snapshot
Key |
Shape |
Models |
Description |
|---|---|---|---|
|
|
all |
A sampled at the step with the lowest chi-squared. |
|
|
exponential, gamma |
|
Posterior statistics — sampling phase
A is rescaled to absorb the normalisation applied to P before accumulation.
Key |
Shape |
Models |
Description |
|---|---|---|---|
|
|
all |
Posterior mean of scaled A across sampling steps. Primary posterior estimate. |
|
|
all |
Posterior sample variance of scaled A. |
|
|
all |
Raw running sum of scaled A. |
|
|
all |
Raw running sum of scaled A². |
Scaled patterns
Key |
Shape |
Description |
|---|---|---|
|
|
|
|
|
|
Marker matrices
Key |
Shape |
Description |
|---|---|---|
|
|
Votes based on raw A: for each gene, the pattern with the highest A value wins. |
|
|
Votes based on scale-corrected A. |
|
|
Softmax-normalised A accumulated over sampling steps. |
Semi-supervised only — fixed_genes mode
Key |
Shape |
Description |
|---|---|---|
|
|
The user-supplied fixed gene patterns (unchanged). |
|
|
Fixed patterns concatenated with |
|
|
Fixed patterns concatenated with |
uns — metadata and scalars
Run metadata (every run)
Key |
Type |
Description |
|---|---|---|
|
int |
Wall-clock time for the training loop. |
|
DataFrame |
ELBO loss values indexed by step (logged every 10 steps). |
|
float |
Scale parameter used for the Gamma prior ( |
|
DataFrame |
Single-column table of all run configuration values (see below). |
settings keys:
Key |
Description |
|---|---|
|
Number of learned patterns. |
|
Total steps (burn-in + sampling). |
|
Number of sampling steps. |
|
Device used ( |
|
Negative Binomial probability parameter. |
|
Whether chi-squared penalty was active. |
|
Scale factor (Gamma model). |
|
Internal model type string (e.g., |
|
TensorBoard log directory (if logging was enabled). |
Chi-squared tracking
Key |
Type |
Description |
|---|---|---|
|
int |
Step index at which the minimum chi-squared occurred. |
|
float |
The minimum chi-squared value achieved during training. |
Model-family-specific scalars
exponential and gamma — variational parameters are per-element
(loc_A, loc_P) and stored in varm / obsm; no additional scalar
uns entries beyond scale (Gamma only).
exponentialSingle — single shared scalar rates:
Key |
Type |
Description |
|---|---|---|
|
float |
Learned scalar rate for the P Exponential prior. |
|
float |
Learned scalar rate for the A Exponential prior. |
|
float |
|
|
float |
|
Pattern naming
Learned patterns are labelled Pattern_1, Pattern_2, … In semi-supervised
runs the fixed-pattern column names (from the input fixed_patterns
DataFrame) appear first, followed by the learned pattern labels:
["Layer1", "Layer2", "Pattern_1", "Pattern_2", ...]
This naming is applied uniformly across all obsm, varm, and uns
DataFrame outputs.
Scale normalisation
During the sampling phase, at each step the model normalises P by dividing each column by its sum, then absorbs the inverse scale into A:
Pn = P / P.sum(axis=0) # each pattern column sums to 1
An = A * P.sum(axis=0) # A absorbs the removed scale
This makes patterns directly comparable across runs and datasets.
mean_P and mean_A are the averages of Pn and An over all
num_sample_steps steps and are the recommended posterior estimates for
most downstream analyses.