Module implementing flux sampling for cobra models. Module Contents Functions

sample(model, n, method=’optgp’, thinning=100, processes=1, seed=None)

Sample valid flux distributions from a cobra model.

cobra.sampling.sampling.sample(model, n, method='optgp', thinning=100, processes=1, seed=None)[source]

Sample valid flux distributions from a cobra model.

The function samples valid flux distributions from a cobra model. Currently we support two methods:

  1. ‘optgp’ (default) which uses the OptGPSampler that supports parallel

    sampling 1. Requires large numbers of samples to be performant (n < 1000). For smaller samples ‘achr’ might be better suited.


  1. ‘achr’ which uses artificial centering hit-and-run. This is a single process method with good convergence 2.

  • model (cobra.Model) – The model from which to sample flux distributions.

  • n (int) – The number of samples to obtain. When using ‘optgp’ this must be a multiple of processes, otherwise a larger number of samples will be returned.

  • method (str, optional) – The sampling algorithm to use.

  • thinning (int, optional) – The thinning factor of the generated sampling chain. A thinning of 10 means samples are returned every 10 steps. Defaults to 100 which in benchmarks gives approximately uncorrelated samples. If set to one will return all iterates.

  • processes (int, optional) – Only used for ‘optgp’. The number of processes used to generate samples.

  • seed (int > 0, optional) – The random number seed to be used. Initialized to current time stamp if None.


The generated flux samples. Each row corresponds to a sample of the fluxes and the columns are the reactions.

Return type



The samplers have a correction method to ensure equality feasibility for long-running chains, however this will only work for homogeneous models, meaning models with no non-zero fixed variables or constraints ( right-hand side of the equalities are zero).



Megchelenbrink W, Huynen M, Marchiori E (2014) optGpSampler: An Improved Tool for Uniformly Sampling the Solution-Space of Genome-Scale Metabolic Networks. PLoS ONE 9(2): e86587.


Direction Choice for Accelerated Convergence in Hit-and-Run Sampling David E. Kaufman Robert L. Smith Operations Research 199846:1 , 84-95