New samplers should derive from this class where possible to provide
a uniform interface.
Parameters:
model (cobra.Model) – The cobra model from which to generate samples.
thinning (int) – The thinning factor of the generated sampling chain. A thinning of
10 means samples are returned every 10 steps.
nproj (int > 0, optional) – How often to reproject the sampling point into the feasibility
space. Avoids numerical issues at the cost of lower sampling. If
you observe many equality constraint violations with
sampler.validate you should lower this number (default None).
seed (int > 0, optional) – Sets the random number seed. Initialized to the current time stamp
if None (default None).
A numpy matrix with as many columns as reactions in the model and
more than 3 rows containing a warmup sample in each row. None if no
warmup points have been generated yet.
Generates warmup points by setting each flux as the sole objective
and minimizing/maximizing it. Also caches the projection of the
warmup points into the nullspace for non-homogeneous problems (only
if necessary).
Raises:
ValueError – If flux cone contains a single point or the problem is
inhomogeneous.
n (int) – The number of samples that are generated at once.
fluxes (bool, optional) – Whether to return fluxes or the internal solver variables. If
set to False, will return a variable for each forward and
backward flux as well as all additional variables you might
have defined in the model (default True).
Returns:
Returns a pandas DataFrame with n rows, each containing a
flux sample.
This is useful to generate batch_num batches of batch_size
samples each.
Parameters:
batch_size (int) – The number of samples contained in each batch.
batch_num (int) – The number of batches in the generator.
fluxes (bool, optional) – Whether to return fluxes or the internal solver variables. If
set to False, will return a variable for each forward and
backward flux as well as all additional variables you might
have defined in the model (default True).
Yields:
pandas.DataFrame – A DataFrame with dimensions (batch_size x n_r) containing
a valid flux sample for a total of n_r reactions (or variables
if fluxes=False) in each row.
Validate a set of samples for equality and inequality feasibility.
Can be used to check whether the generated samples and warmup points
are feasible.
Parameters:
samples (numpy.matrix) – Must be of dimension (samples x n_reactions). Contains the
samples to be validated. Samples must be from fluxes.
Returns:
A one-dimensional numpy array containing
a code of 1 to 3 letters denoting the validation result:
- ‘v’ means feasible in bounds and equality constraints
- ‘l’ means a lower bound violation
- ‘u’ means an upper bound validation
- ‘e’ means and equality constraint violation
A sampler with low memory footprint and good convergence.
Parameters:
model (cobra.Model) – The cobra model from which to generate samples.
thinning (int, optional) – The thinning factor of the generated sampling chain. A thinning of
10 means samples are returned every 10 steps (default 100).
nproj (int > 0, optional) – How often to reproject the sampling point into the feasibility
space. Avoids numerical issues at the cost of lower sampling. If
you observe many equality constraint violations with
sampler.validate you should lower this number (default None).
seed (int > 0, optional) – Sets the random number seed. Initialized to the current time stamp
if None (default None).
A numpy matrix with as many columns as reactions in the model and
more than 3 rows containing a warmup sample in each row. None if no
warmup points have been generated yet.
The center of the sampling space as estimated by the mean of all
previously generated samples.
Type:
numpy.array
Notes
ACHR generates samples by choosing new directions from the sampling
space’s center and the warmup points. The implementation used here is
the same as in the MATLAB COBRA Toolbox [2]_ and uses only the initial
warmup points to generate new directions and not any other previous
iterations. This usually gives better mixing, since the startup points
are chosen to span the space in a wide manner. This also makes the
generated sampling chain quasi-Markovian since the center converges
rapidly.
Memory usage is roughly in the order of (2 * number of reactions) ^ 2
due to the required nullspace matrices and warmup points. So, large
models easily take up a few GBs of RAM.
This is the basic sampling function for all hit-and-run samplers.
Parameters:
n (int) – The number of samples that are generated at once.
fluxes (bool, optional) – Whether to return fluxes or the internal solver variables. If
set to False, will return a variable for each forward and
backward flux as well as all additional variables you might
have defined in the model (default True).
Returns:
Returns a pandas DataFrame with n rows, each containing a
flux sample.
Return type:
pandas.DataFrame
Notes
Performance of this function linearly depends on the number
of reactions in your model and the thinning factor.
delta (np.array) – The direction to take the step in.
fraction (float, optional) – A float controlling the part of alpha difference to contribute to
the fraction of delta (default None). If None, alpha is obtained
from a normal distribution.
tries (int, optional) – Total number of tries (default 0).
Returns:
The new numpy array obtained after a step of sampling.
A parallel sampler with fast convergence and parallel execution.
See [1]_ for details.
Parameters:
model (cobra.Model) – The cobra model from which to generate samples.
processes (int, optional) – The number of processes used during sampling
(default cobra.Configuration.processes).
thinning (int, optional) – The thinning factor of the generated sampling chain. A thinning of
10 means samples are returned every 10 steps (default 100).
nproj (int > 0, optional) – How often to reproject the sampling point into the feasibility
space. Avoids numerical issues at the cost of lower sampling. If
you observe many equality constraint violations with
sampler.validate you should lower this number (default None).
seed (int > 0, optional) – Sets the random number seed. Initialized to the current time stamp
if None (default None).
A numpy matrix with as many columns as reactions in the model and
more than 3 rows containing a warmup sample in each row. None if no
warmup points have been generated yet.
The center of the sampling space as estimated by the mean of all
previously generated samples.
Type:
numpy.array
Notes
The sampler is very similar to artificial centering where each process
samples its own chain. Initial points are chosen randomly from the
warmup points followed by a linear transformation that pulls the points
a little bit towards the center of the sampling space.
If the number of processes used is larger than the one requested,
number of samples is adjusted to the smallest multiple of the number of
processes larger than the requested sample number. For instance, if you
have 3 processes and request 8 samples, you will receive 9.
Memory usage is roughly in the order of (2 * number of reactions)^2
due to the required nullspace matrices and warmup points. So, large
models easily take up a few GBs of RAM. However, most of the large
matrices are kept in shared memory. So the RAM usage is independent of
the number of processes.
This is the basic sampling function for all hit-and-run samplers.
Parameters:
n (int) – The minimum number of samples that are generated at once.
fluxes (bool, optional) – Whether to return fluxes or the internal solver variables. If
set to False, will return a variable for each forward and
backward flux as well as all additional variables you might
have defined in the model (default True).
Returns:
Returns a pandas DataFrame with n rows, each containing a
flux sample.
Return type:
pandas.DataFrame
Notes
Performance of this function linearly depends on the number
of reactions in your model and the thinning factor.
If the number of processes is larger than one, computation is split
across the CPU cores of your machine. This may shorten computation
time. However, there is also overhead in setting up parallel
computation primitives so, we recommend to calculate large numbers
of samples at once (n > 1000).
Sample valid flux distributions from a cobra model.
Currently, two methods are supported:
‘optgp’ (default) which uses the OptGPSampler that supports parallel
sampling. Requires large numbers of samples to be performant
(n > 1000). For smaller samples, ‘achr’ might be better suited.
For details, refer [1]_ .
‘achr’ which uses artificial centering hit-and-run. This is a single
process method with good convergence. For details, refer [2]_ .
Parameters:
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 ({"optgp", "achr"}, optional) – The sampling algorithm to use (default “optgp”).
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 1
will return all iterates (default 100).
processes (int, optional) – Only used for ‘optgp’. The number of processes used to generate
samples (default 1).
seed (int > 0, optional) – Sets the random number seed. Initialized to the current time stamp
if None (default None).
Returns:
The generated flux samples. Each row corresponds to a sample of the
fluxes and the columns are the reactions.
Return type:
pandas.DataFrame
Notes
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).