GeneSignature pairs a data-layer GeneSet (which genes) with a scoring recipe (how to reduce them to one number per spot). This lives in the inference layer because scoring reuses the normalization transforms in bioptimus.preprocess.omics.transforms and sits beside the other prediction reducers (Pseudobulk, slide_reduce). Gene sets stay data-layer and never gain a score method; a signature wraps a gene set, so the dependency only ever points inference → data.
Built-in scoring is a normalize-then-reduce recipe. The normalize axis reuses the shared omics transforms directly (an OmicsPreprocessor: IdentityTransform, Log1pTransform, SelfZScoreNormaliser, TotalCountNormaliser for CPM/CP10k total-count normalization). The reduce axis is a Reducer (built-ins Mean, Sum, WeightedSum, Min, Max) that collapses the resolved genes. A GeneSignatureScoringRecipe pairs one of each. Users extend the reduce axis by subclassing Reducer (fixed linear combinations should subclass LinearReducer so the engine can batch them into one matmul), and the normalize axis with any OmicsPreprocessor.
GeneSignatureDownCombine
GeneSignature carries a down_gene_set. DIFFERENCE (the default) is up - down; RATIO is up / down; LOG2_RATIO is log2(up / down) (the symmetric, better-behaved form). RATIO and LOG2_RATIO assume non-negative side scores (pair them with a non-negative normalizer such as CP10k or Log1p); a negative side score makes the ratio ill-defined and raises at score time.
GeneSignatureScoringRecipe
OmicsPreprocessor normalizer (applied to the whole matrix) with a Reducer (collapsing the resolved genes). The normalize axis reuses the shared omics transforms directly (Log1pTransform, TotalCountNormaliser for CPM/CP10k total-count normalization, SelfZScoreNormaliser, IdentityTransform), so any normalization pairs with any reduction without a combined name.
Attributes:
normalize- The omics transform applied before reduction. Defaults toIdentityTransform(no normalization).reduce- The reducer that collapses the resolved genes. Defaults toMean.
GeneSignatureMethod
A signature’s scoring recipe as accepted from callers: an explicitGeneSignatureScoringRecipe, or a bare Reducer (paired with an IdentityTransform). Strings are not accepted; a bare reducer coerces to a GeneSignatureScoringRecipe on construction.
GeneSignatureScores
GeneSignatureScoringRecipe. No p-values are computed and no control-gene baseline is subtracted.
Attributes:
scores- A DataFrame with one row per spot and one column per signature (column order matches the scored signatures). Drops straight intoplot_spatial_geneand.corr().coords- Optional(N, 2)array of spot coordinates, carried through unchanged for spatial visualization.Nonewhen not supplied; coordinates are never an input to scoring.
save
path.
Persists the score matrix as a gene_signature_scores array (with a gene_signature_names label array) row-aligned with the store’s outputs / coords, so each tile keeps its score beside its prediction. score() and save() are deliberately separate: compute in memory first, then persist explicitly.
Policy when the store already has signature scores — merge columns by name (default), overwrite, or raise. See
OnConflict.BioptimusValueError- If the score rows do not match the store’s tile count, or a conflict occurs underOnConflict.ERROR, taggedBIO-CFG-001.
GeneSignature
GeneSet and adds the reduce step that turns the set’s per-gene predictions into one score per spot. weights are only valid for a WeightedSum reducer and must align to the set’s de-duplicated genes.
Attributes:
gene_set- The genes to score (the “up” side of a two-sided signature).method- The scoring recipe: aGeneSignatureScoringRecipe, or a bareReducer(paired with anIdentityTransform). Coerced to aGeneSignatureScoringRecipeon construction.weights- Per-gene weights for aWeightedSumreducer, aligned togene_set.genes.Nonefor every other reducer.down_gene_set- Optional “down” side. When set, the signature is two-sided and its score combines the up and down sides percombine.Nonefor a one-sided signature.down_weights- Per-gene weights for the down side under aWeightedSumreducer, aligned todown_gene_set.genes.Noneotherwise.combine- How the up and down sides combine, applied only whendown_gene_setis set. Defaults toGeneSignatureDownCombine.DIFFERENCE.name- The signature name. Defaults to the gene set’s name.
effective_name
score
(N_spots,) array of per-spot scores.
Raises:
BioptimusValueError- Ifpredsdoes not match the panel (BIO-CFG-001), none of the genes resolve (BIO-GENE-001), a custom scorer returns the wrong shape (BIO-CFG-001), or aRATIO/LOG2_RATIOside score is negative (BIO-CFG-001).
to_spec
from_spec rebuilds an equivalent signature.
Returns:
A dict with name, gene_set, down_gene_set, normalize,
reduce, weights, down_weights, and combine.
from_spec
GeneSignature from a to_spec dict.
A mapping produced by
to_spec.BioptimusValueError- If a recipe piece is not registered, thecombinename is unknown (BIO-CFG-001), or the weight/combine combination is invalid (surfaced by construction).
GeneSignatureCollection
GeneSignature objects keyed by signature name.
Mirrors GeneSetCollection: behaves as a read-only Mapping[str, GeneSignature] and owns score, which the data-layer collection deliberately lacks (scoring is inference-layer).
A mapping of signature name to
GeneSignature.BioptimusValueError- Ifmembersis empty, taggedBIO-CFG-001.
from_signatures
BioptimusValueError- If two signatures share a name, taggedBIO-CFG-001.
to_spec
signatures list of {"key", "spec"} entries (see
GeneSignature.to_spec), in member order.
from_spec
GeneSignatureCollection from a to_spec dict.
Returns:
The reconstructed collection, preserving member keys and order.
Raises:
BioptimusValueError- Ifsignaturesis missing or an entry lackskeyorspec, taggedBIO-CFG-001.
from_collection
A single recipe/reducer applied to every set, or a per-set mapping of set name to recipe. Sets absent from a mapping (or when
method is None) use a default GeneSignatureScoringRecipe (IdentityTransformMean).
weights- Optional per-set weights (set name to weight sequence), forwarded to the matching signatures.down- Optional per-set down gene sets (set name toGeneSet), making the matching signatures two-sided.combine- A single combine mode applied to every two-sided set, or a per-set mapping of set name toGeneSignatureDownCombine.
GeneSignature per member set.
__getitem__
__iter__
__len__
signatures
names
score
GeneSignatureScores.
Returns:
A GeneSignatureScores with one column per member signature.
Raises:
BioptimusValueError- On a panel/shape mismatch, a signature resolving to no genes, a misbehaving custom scorer, a negativeRATIO/LOG2_RATIOside score, or acoordslength mismatch.

