cobra.sampling.hr_sampler#
Provide the base class and associated functions for Hit-and-Run samplers.
Attributes#
Classes#
Functions#
|
Create a new numpy array that resides in shared memory. |
Module Contents#
- cobra.sampling.hr_sampler.logger#
- class cobra.sampling.hr_sampler.Problem#
Bases:
tuple- equalities#
- b#
- inequalities#
- bounds#
- variable_fixed#
- variable_bounds#
- nullspace#
- homogeneous#
Create a new numpy array that resides in shared memory.
- Parameters:
- Returns:
The newly created shared numpy array.
- Return type:
numpy.array
- Raises:
ValueError – If the input data (if provided) size is not equal to the created array.
- class cobra.sampling.hr_sampler.HRSampler(model: cobra.Model, thinning: int, nproj: int | None = None, seed: int | None = None, **kwargs)[source]#
Bases:
abc.ABCThe abstract base class for hit-and-run samplers.
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).
- retries#
The overall of sampling retries the sampler has observed. Larger values indicate numerical instabilities.
- Type:
- problem#
A NamedTuple whose attributes define the entire sampling problem in matrix form.
- Type:
- warmup#
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.
- Type:
- fwd_idx#
A numpy array having one entry for each reaction in the model, containing the index of the respective forward variable.
- Type:
numpy.array
- rev_idx#
A numpy array having one entry for each reaction in the model, containing the index of the respective reverse variable.
- Type:
numpy.array
- model#
- feasibility_tol#
- bounds_tol#
- thinning#
- n_samples = 0#
- retries = 0#
- problem#
- fwd_idx#
- rev_idx#
- warmup = None#
- _seed#
- __build_problem() Problem#
Build the matrix representation of the sampling problem.
- Returns:
The matrix representation in the form of a NamedTuple.
- Return type:
- generate_fva_warmup() None[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).
- Raises:
ValueError – If flux cone contains a single point or the problem is inhomogeneous.
- _reproject(p: numpy.ndarray) numpy.ndarray[source]#
Reproject a point into the feasibility region.
This function is guaranteed to return a new feasible point. However, no guarantee can be made in terms of proximity to the original point.
- Parameters:
p (numpy.array) – The current sample point.
- Returns:
A new feasible point. If p is feasible, it will return p.
- Return type:
numpy.array
- _random_point() numpy.ndarray[source]#
Find an approximately random point in the flux cone.
- _is_redundant(matrix: numpy.matrix, cutoff: float | None = None) bool[source]#
Identify redundant rows in a matrix that can be removed.
- _bounds_dist(p: numpy.ndarray) numpy.ndarray[source]#
Get the lower and upper bound distances. Negative is bad.
- abstractmethod sample(n: int, fluxes: bool = True) pandas.DataFrame[source]#
Abstract sampling function.
Should be overwritten by child classes.
- 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
- batch(batch_size: int, batch_num: int, fluxes: bool = True) pandas.DataFrame[source]#
Create a batch generator.
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(samples: numpy.matrix) numpy.ndarray[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 (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
- Return type:
numpy.array
- Raises:
ValueError – If wrong number of columns.