mcmc
mcmc
¶
Markov Chain Monte Carlo (MCMC) module for single-cell RNA sequencing data analysis.
This module implements MCMC inference for SCRIBE models using Numpyro's NUTS.
MCMCInferenceEngine
¶
Handles MCMC inference execution.
run_inference
staticmethod
¶
run_inference(model_config, count_data, n_cells, n_genes, n_samples=2000, n_warmup=1000, n_chains=1, seed=42, mcmc_kwargs=None, annotation_prior_logits=None, dataset_indices=None, init_values=None)
Execute MCMC inference using NUTS.
| PARAMETER | DESCRIPTION |
|---|---|
model_config
|
Model configuration object.
TYPE:
|
count_data
|
Processed count data (cells as rows).
TYPE:
|
n_cells
|
Number of cells.
TYPE:
|
n_genes
|
Number of genes.
TYPE:
|
n_samples
|
Number of MCMC samples.
TYPE:
|
n_warmup
|
Number of warmup samples.
TYPE:
|
n_chains
|
Number of parallel chains.
TYPE:
|
seed
|
Random seed for reproducibility.
TYPE:
|
mcmc_kwargs
|
Keyword arguments for the NUTS kernel (e.g.,
TYPE:
|
annotation_prior_logits
|
Prior logits for annotation-guided mixture models.
TYPE:
|
init_values
|
Constrained-space values to initialize MCMC chains via
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MCMC
|
Results from the MCMC run containing samples and diagnostics. |
Source code in src/scribe/mcmc/inference_engine.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
ScribeMCMCResults
dataclass
¶
ScribeMCMCResults(samples, n_cells, n_genes, model_type, model_config, prior_params, obs=None, var=None, uns=None, n_obs=None, n_vars=None, predictive_samples=None, n_components=None, denoised_counts=None, _n_cells_per_dataset=None, _dataset_indices=None, _promoted_dataset_keys=None, _mcmc=None)
Bases: ParameterExtractionMixin, GeneSubsettingMixin, ComponentMixin, DatasetMixin, ModelHelpersMixin, SamplingMixin, LikelihoodMixin, NormalizationMixin, MixtureAnalysisMixin
SCRIBE MCMC results.
Stores posterior samples and provides analysis methods via mixins.
The underlying numpyro.infer.MCMC object is wrapped (composition)
rather than inherited, so gene/component subsetting always returns
another ScribeMCMCResults instance.
| ATTRIBUTE | DESCRIPTION |
|---|---|
samples |
Raw posterior samples keyed by parameter name.
TYPE:
|
n_cells |
Number of cells in the dataset.
TYPE:
|
n_genes |
Number of genes in the dataset.
TYPE:
|
model_type |
Model identifier (e.g.
TYPE:
|
model_config |
Configuration used for inference.
TYPE:
|
prior_params |
Prior parameter values used during inference.
TYPE:
|
obs |
Cell-level metadata from
TYPE:
|
var |
Gene-level metadata from
TYPE:
|
uns |
Unstructured metadata from
TYPE:
|
n_obs |
Number of observations (cells).
TYPE:
|
n_vars |
Number of variables (genes).
TYPE:
|
predictive_samples |
Predictive samples from :meth:
TYPE:
|
n_components |
Number of mixture components (
TYPE:
|
denoised_counts |
Denoised counts from :meth:
TYPE:
|
_mcmc |
Wrapped
TYPE:
|
concat
classmethod
¶
concat(results_list, *, align_genes='assume_aligned', join='cells', check_model=True, validation='var_only')
Concatenate multiple MCMC results objects along the cell axis.
The method supports combining objects that represent the same model and gene space while differing in cell count. Cell-specific posterior samples are concatenated along the cell axis, while non-cell-specific samples must be identical across inputs.
| PARAMETER | DESCRIPTION |
|---|---|
results_list
|
Results objects to concatenate. At least two elements are required.
TYPE:
|
align_genes
|
Gene-alignment strategy.
TYPE:
|
join
|
Concatenation axis. Only cell-axis concatenation is supported.
TYPE:
|
check_model
|
If
TYPE:
|
validation
|
Validation policy for non-cell-specific fields.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ScribeMCMCResults
|
Concatenated MCMC results with |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If inputs are empty, incompatible, or use unsupported options. |
TypeError
|
If inputs are not all |
Source code in src/scribe/mcmc/results.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
__post_init__
¶
Validate model configuration and set derived attributes.
Source code in src/scribe/mcmc/results.py
from_mcmc
classmethod
¶
Create results from an existing numpyro.infer.MCMC instance.
Extracts samples once and stores the MCMC object for diagnostics.
| PARAMETER | DESCRIPTION |
|---|---|
mcmc
|
Completed MCMC run.
TYPE:
|
n_cells
|
Number of cells.
TYPE:
|
n_genes
|
Number of genes.
TYPE:
|
model_type
|
Model identifier.
TYPE:
|
model_config
|
Model configuration.
TYPE:
|
prior_params
|
Prior parameter values.
TYPE:
|
**kwargs
|
Forwarded to the dataclass constructor (e.g.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
ScribeMCMCResults
|
|
Source code in src/scribe/mcmc/results.py
from_anndata
classmethod
¶
Create results from an MCMC instance and AnnData object.
| PARAMETER | DESCRIPTION |
|---|---|
mcmc
|
Completed MCMC run.
TYPE:
|
adata
|
AnnData object with cell/gene metadata.
TYPE:
|
model_type
|
Model identifier.
TYPE:
|
model_config
|
Model configuration.
TYPE:
|
prior_params
|
Prior parameter values.
TYPE:
|
**kwargs
|
Forwarded to the dataclass constructor.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
ScribeMCMCResults
|
|
Source code in src/scribe/mcmc/results.py
get_posterior_samples
¶
Return posterior samples.
MCMC samples already contain canonical parameters (p, r,
mixing_weights, etc.) because derived parameters are
registered as numpyro.deterministic sites and unconstrained
specs sample via TransformedDistribution in constrained
space.
| PARAMETER | DESCRIPTION |
|---|---|
descriptive_names
|
If True, rename dict keys from internal short names to user-friendly descriptive names.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict
|
Parameter name -> sample array. |
Source code in src/scribe/mcmc/results.py
get_samples
¶
Return samples with optional chain grouping.
| PARAMETER | DESCRIPTION |
|---|---|
group_by_chain
|
Preserve the chain dimension (requires the original MCMC object).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict
|
Parameter samples. |
Source code in src/scribe/mcmc/results.py
print_summary
¶
Print MCMC summary statistics (delegates to the wrapped MCMC).
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If the MCMC object is not available (e.g. on subsets). |
Source code in src/scribe/mcmc/results.py
get_extra_fields
¶
Return MCMC extra fields (e.g. potential_energy, diverging).
Returns an empty dict when the MCMC object is not available (subsets).
Source code in src/scribe/mcmc/results.py
__getstate__
¶
Return pickle-safe state for ScribeMCMCResults.
Notes
The wrapped _mcmc object retains local closure functions from model
building and is intentionally dropped to ensure portability.
Source code in src/scribe/mcmc/results.py
MCMCResultsFactory
¶
Factory for creating MCMC results objects.
create_results
staticmethod
¶
create_results(mcmc_results, model_config, adata, count_data, n_cells, n_genes, model_type, n_components, prior_params)
Package MCMC results into a ScribeMCMCResults object.
| PARAMETER | DESCRIPTION |
|---|---|
mcmc_results
|
Raw MCMC results from NumPyro.
TYPE:
|
model_config
|
Model configuration object.
TYPE:
|
adata
|
Original AnnData object (if provided).
TYPE:
|
count_data
|
Processed count data.
TYPE:
|
n_cells
|
Number of cells.
TYPE:
|
n_genes
|
Number of genes.
TYPE:
|
model_type
|
Type of model.
TYPE:
|
n_components
|
Number of mixture components.
TYPE:
|
prior_params
|
Dictionary of prior parameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ScribeMCMCResults
|
Packaged results object. |
Source code in src/scribe/mcmc/results_factory.py
clamp_init_values
¶
Clamp init values away from distribution support boundaries.
SVI MAP estimates (stored in float32) can land exactly on support
boundaries — e.g. phi_capture = 0.0 or p = 1.0 — where the
log-probability is -inf. This makes init_to_value reject
the initialization.
| PARAMETER | DESCRIPTION |
|---|---|
init
|
Init values keyed by parameter name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, ndarray]
|
A shallow copy with boundary values nudged into the interior. |
Source code in src/scribe/mcmc/_init_from_svi.py
compute_init_values
¶
Compute MCMC init values from SVI MAP estimates.
Ensures the returned dict contains constrained-space values for all
sampled sites of the target model's parameterization. Missing
parameters are derived from the canonical pair (p, r) which is
always present when get_map(canonical=True) is used.
| PARAMETER | DESCRIPTION |
|---|---|
svi_map
|
MAP estimates from SVI, typically obtained via
TYPE:
|
target_config
|
Model configuration for the target MCMC run.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, ndarray]
|
Init values keyed by site name, all in constrained space. Includes the original SVI MAP entries plus any derived parameters needed by the target parameterization. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If canonical parameters |
Notes
init_to_value only initializes numpyro.sample sites. Extra
keys in the returned dict (e.g. deterministic sites r when the
target is mean_prob) are harmlessly ignored by NumPyro.
Hierarchical hyperparameters (logit_p_loc, log_phi_scale,
etc.) live in different spaces across parameterizations and cannot
be reliably converted. They are omitted and will fall back to
init_to_uniform inside NumPyro.
Source code in src/scribe/mcmc/_init_from_svi.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |