Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

pytransc

Tests codecov Documentation Python Code style: ruff

Python library for implementing TransC MCMC sampling

This repository contains source code to implement three Trans-Conceptual MCMC sampling algorithms as described in the article Sambridge, Valentine and Hauser (2025).

Installation

The dvelopment version can be installed from this repository

pip install git+https://github.com/inlab-geo/pytransc

A release can be installed from pypi

pip install pytransc

Documentation

This package of with a single class TransC_Sampler implementing three separate MCMC samplers across independent model states implemented as functions of the class

run_product_space_sampler() - implements a fixed dimension MCMC sampler over the product space of the states, and extracts a TransC/TransD ensemble.

run_state_jump_sampler() - implements an RJ-MCMC style algorithm using pseudo-prior proposals and balance conditions.

run_ensemble_resampler() - implements a single parameter Metropolis sampler over the state indicator variable. Requires posterior ensembles in each state to be precomputed.

Other utility functions include:

run_mcmc_per_state() - performs MCMC sampling within each state.

build_auto_pseudo_prior() - fits a mixture model to posterior ensembles in each state to act as a pseudo-prior function.

get_transc_samples() - creates posterior TransC/TransD ensemble from results of any sample.

Here is the docstring of the function run_state_jump_sampler():

"""Run MCMC sampler over independent states using pre-computed ensembles.

This function performs trans-conceptual MCMC by resampling from pre-computed
posterior ensembles in each state. It calculates relative evidence of each state
by sampling over the ensemble members according to their posterior and pseudo-prior
densities.

Parameters
----------
n_walkers : int
    Number of random walkers used by the ensemble resampler.
n_steps : int
    Number of Markov chain steps to perform per walker.
n_states : int
    Number of independent states in the problem.
n_dims : list of int
    List of parameter dimensions for each state.
log_posterior_ens : StateOrderedEnsemble
    Log-posterior values of ensemble members in each state.
    Format: list of arrays, where each array contains log-posterior values
    for the ensemble members in that state.
log_pseudo_prior_ens : StateOrderedEnsemble
    Log-pseudo-prior values of ensemble members in each state.
    Format: list of arrays, where each array contains log-pseudo-prior values
    for the ensemble members in that state.
seed : int, optional
    Random number seed for reproducible results. Default is 61254557.
state_proposal_weights : list of list of float, optional
    Weights for proposing transitions between states. Should be a matrix
    where element [i][j] is the weight for proposing state j from state i.
    Diagonal elements are ignored. If None, uniform weights are used.
progress : bool, optional
    Whether to display progress information. Default is False.
walker_pool : Any | None, optional
    User-provided pool for parallelizing walker execution. The pool must
    implement a map() method compatible with the standard library's map()
    function. Default is None.
state_pool : Any | None, optional
    User-provided pool for parallelizing state-level operations such as
    pseudo-prior evaluation across states. Currently reserved for future
    enhancements. Default is None.
forward_pool : Any | None, optional
    User-provided pool for parallelizing forward solver calls within
    log_posterior evaluations. If provided, the pool will be made available
    to log_posterior functions via get_forward_pool() from pytransc.utils.forward_context.
    The pool must implement a map() method compatible with the standard library's 
    map() function. Supports ProcessPoolExecutor, ThreadPoolExecutor, 
    and schwimmbad pools. Default is None.

Returns
-------
MultiWalkerEnsembleResamplerChain
    Chain results containing state sequences, ensemble member indices,
    and diagnostics for all walkers.

Notes
-----
This method requires pre-computed posterior ensembles and their corresponding
log-density values. The ensembles can be generated using `run_mcmc_per_state()`
and the pseudo-prior values using automatic fitting routines.

The algorithm works by:
1. Selecting ensemble members within states based on posterior weights
2. Proposing transitions between states based on relative evidence
3. Accepting/rejecting proposals using Metropolis-Hastings criterion

Examples
--------
>>> results = run_ensemble_resampler(
...     n_walkers=32,
...     n_steps=1000,
...     n_states=3,
...     n_dims=[2, 3, 1],
...     log_posterior_ens=posterior_ensembles,
...     log_pseudo_prior_ens=pseudo_prior_ensembles
... )

Using with forward pool for parallel forward solver calls:

>>> from concurrent.futures import ProcessPoolExecutor
>>> with ProcessPoolExecutor(max_workers=4) as forward_pool:
...     results = run_ensemble_resampler(
...         n_walkers=32,
...         n_steps=1000,
...         n_states=3,
...         n_dims=[2, 3, 1],
...         log_posterior_ens=posterior_ensembles,
...         log_pseudo_prior_ens=pseudo_prior_ensembles,
...         forward_pool=forward_pool
...     )
"""

API documentation

API documentation is auto-generated using pdoc is available here

Example

import numpy as np
from pytransc.samplers import run_state_jump_sampler

Detailed examples of showing implementation of all three samplers can be found in

examples/Regression - Sampling across 4 states in polynomial regression with all three samplers.

examples/AirborneEM - Ensemble Sampler applied to Airborne EM data.

examples/Tomography - Ensemble Sampler applied to 2D borehole tomography to demonstrate three level parallelism.

Releasing to PyPI

The version is automatically derived from git tags using hatch-vcs. To publish a new release:

  1. Make sure all changes are merged into main.
  2. Create and push a version tag:
    git tag v0.3.0
    git push origin v0.3.0
    
  3. The publish-to-pypi GitHub Actions workflow will automatically build and publish the package to both PyPI and TestPyPI.

Tag names must start with v (e.g. v0.3.0, v1.0.0a1). The version in the published package is derived from the tag, so no manual version bumps are needed.

Licensing

pytransc is released as BSD-2-Clause licence.

Citations and Acknowledgments

Sambridge, M., Valentine, A. & Hauser, J., 2025. Trans-Conceptual Sampling: Bayesian Inference With Competing Assumptions, JGR Solid Earth, Volume 130, Issue 8, 17 August 2025, e2024JB030470.

Metadata

Release files for pytransc 0.3.0a2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytransc 0.3.0a2
File Size Uploaded Provenance
pytransc-0.3.0a2.tar.gz 23.8 MB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pytransc 0.3.0a2
File Interpreter ABI Platform
pytransc-0.3.0a2-py3-none-any.whl Python 3 none any Details

Total release size: 23.9 MB

Release files / pytransc-0.3.0a2.tar.gz

Download URL pytransc-0.3.0a2.tar.gz
Size 23.8 MB
Tags Source
SHA-256 checksum
How to use checksums
b11bac4f80bed5d41dc95e389ee4a79e86e3a54d34ab737f9bd580d14476b6a7
BLAKE2b-256 checksum
How to use checksums
767d38cf5ef999d4b5791a0b522fc4bfcc06cf20e8030052731ed5370c61ac75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 11, 2026.

Transparency log

Release files / pytransc-0.3.0a2-py3-none-any.whl

Download URL pytransc-0.3.0a2-py3-none-any.whl
Size 46.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
26f955f95f49c345e96f529848ef0a3a3bc56e6d8cd12484505965583014c763
BLAKE2b-256 checksum
How to use checksums
eb2bf447d6ec1783e75d6ab939de3ba195468a47da39fa9d66168fb3cb52324e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 11, 2026.

Transparency log

Security for pytransc 0.3.0a2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pytransc-0.3.0a2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 11, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page