17.1.1.6.1.2. cobra.sampling.hr_sampler
¶
Provide base class for Hit-and-Run samplers.
New samplers should derive from the abstract HRSampler class where possible to provide a uniform interface.
17.1.1.6.1.2.1. Module Contents¶
17.1.1.6.1.2.1.1. Classes¶
The abstract base class for hit-and-run samplers. |
17.1.1.6.1.2.1.2. Functions¶
|
Create a new numpy array that resides in shared memory. |
|
Sample a new feasible point from the point x in direction delta. |
-
cobra.sampling.hr_sampler.
Problem
[source]¶ Defines the matrix representation of a sampling problem.
-
cobra.sampling.hr_sampler.
equalities
¶ All equality constraints in the model.
- Type
numpy.array
-
cobra.sampling.hr_sampler.
b
¶ The right side of the equality constraints.
- Type
numpy.array
-
cobra.sampling.hr_sampler.
inequalities
¶ All inequality constraints in the model.
- Type
numpy.array
-
cobra.sampling.hr_sampler.
bounds
¶ The lower and upper bounds for the inequality constraints.
- Type
numpy.array
-
cobra.sampling.hr_sampler.
variable_bounds
¶ The lower and upper bounds for the variables.
- Type
numpy.array
-
cobra.sampling.hr_sampler.
homogeneous
¶ Indicates whether the sampling problem is homogenous, e.g. whether there exist no non-zero fixed variables or constraints.
- Type
boolean
-
cobra.sampling.hr_sampler.
nullspace
¶ A matrix containing the nullspace of the equality constraints. Each column is one basis vector.
- Type
-
Create a new numpy array that resides in shared memory.
- Parameters
shape (tuple of ints) – The shape of the new array.
data (numpy.array) – Data to copy to the new array. Has to have the same shape.
integer (boolean) – Whether to use an integer array. Defaults to False which means float array.
-
class
cobra.sampling.hr_sampler.
HRSampler
(model, thinning, nproj=None, seed=None)[source]¶ Bases:
object
The abstract base class for hit-and-run samplers.
- 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.
seed (int > 0, optional) – The random number seed that should be used.
-
model
¶ The cobra model from which the sampes get generated.
- Type
-
retries
¶ The overall of sampling retries the sampler has observed. Larger values indicate numerical instabilities.
- Type
-
problem
¶ A python object whose attributes define the entire sampling problem in matrix form. See docstring of Problem.
- Type
collections.namedtuple
-
warmup
¶ A matrix of 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.
- Type
-
seed
¶ Sets the random number seed. Initialized to the current time stamp if None.
- Type
int > 0, optional
-
fwd_idx
¶ Has one entry for each reaction in the model containing the index of the respective forward variable.
- Type
numpy.array
-
rev_idx
¶ Has one entry for each reaction in the model containing the index of the respective reverse variable.
- Type
numpy.array
-
generate_fva_warmup
(self)[source]¶ Generate the warmup points for the sampler.
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).
-
_reproject
(self, p)[source]¶ Reproject a point into the feasibility region.
This function is guaranteed to return a new feasible point. However, no guarantees in terms of proximity to the original point can be made.
- Parameters
p (numpy.array) – The current sample point.
- Returns
A new feasible point. If p was feasible it wil return p.
- Return type
numpy.array
-
_is_redundant
(self, matrix, cutoff=None)[source]¶ Identify rdeundant rows in a matrix that can be removed.
-
sample
(self, n, fluxes=True)[source]¶ Abstract sampling function.
Should be overwritten by child classes.
-
batch
(self, batch_size, batch_num, fluxes=True)[source]¶ Create a batch generator.
This is useful to generate n batches of m samples each.
- Parameters
batch_size (int) – The number of samples contained in each batch (m).
batch_num (int) – The number of batches in the generator (n).
fluxes (boolean) – 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.
- 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
(self, samples)[source]¶ 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 (n_samples x n_reactions). Contains the samples to be validated. Samples must be from fluxes.
- Returns
A one-dimensional numpy array of length 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 a lower bound validation
’e’ means and equality constraint violation
- Return type
numpy.array