mass.thermo.conc_sampling.conc_hr_sampler
¶
Provide base class for Hit-and-Run concentration samplers.
New samplers should derive from the abstract ConcHRSampler
class
where possible to provide a uniform interface.”“
Based on sampling implementations in cobra.sampling.hr_sampler
.
Module Contents¶
Classes¶
The abstract base class for hit and run concentration samplers. |
Functions¶
|
Sample new feasible point from point |
-
mass.thermo.conc_sampling.conc_hr_sampler.
LOGGER
[source]¶ Logger for
conc_hr_sampler
submodule.- Type
-
mass.thermo.conc_sampling.conc_hr_sampler.
MAX_TRIES
= 100[source]¶ Maximum number of retries for sampling.
- Type
-
class
mass.thermo.conc_sampling.conc_hr_sampler.
ConcHRSampler
(concentration_solver, thinning, nproj=None, seed=None)[source]¶ The abstract base class for hit and run concentration samplers.
- Parameters
concentration_solver (ConcSolver) – The
ConcSolver
to use in generating samples.thinning (int) – The thinning factor for the generated sampling chain as a positive
int
> 0. A thinning factor of 10 means samples are returned every 10 steps.A positive
int
> 0 indicating how often to reporject the sampling point into the feasibility space. Avoids numerical issues at the cost of lower samplimg. IfNone
then the value is determined via the following:nproj = int(min(len(self.concentration_solver.variables)**3, 1e6))
Default is
None
A positive
int
> 0 indiciating random number seed that should be used. IfNone
provided, the current time stamp is used.Default is
None
.
-
concentration_solver
¶ The
ConcSolver
used to generate samples.- Type
-
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. See docstring ofProblem
for more information.- 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
-
property
nproj
(self)[source]¶ Get or set
nproj
value.- Parameters
A positive
int
> 0 indicating how often to reporject the sampling point into the feasibility space. Avoids numerical issues at the cost of lower sampling. IfNone
then the value is determined via the following:nproj = int(min(len(self.concentration_solver.variables)**3, 1e6))
-
generate_cva_warmup
(self)[source]¶ Generate the warmup points for the sampler.
Generates warmup points by setting each concentration as the sole objective and minimizing/maximizing it. Also caches the projection of the warmup points into the nullspace for non-homogenous problems.
-
sample
(self, n, concs=True)[source]¶ Abstract sampling function.
Should be overwritten by child classes.
-
batch
(self, batch_size, batch_num, concs=True)[source]¶ Create a batch generator.
This is useful to generate
n
batches ofm
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
).concs (boolean) – Whether to return concentrations or the internal solver variables. If
False
will return a variable for each metabolite and reaction Keq as well as all additional variables that may have been defined in the model.
- Yields
pandas.core.frame.DataFrame – A
pandas.DataFrame
with dimensions(batch_size x n_m)
containing a valid concentration sample for a total ofn_m
metabolites (or variables ifconcs=False
) in each row.
-
_reproject
(self, p)[source]¶ Reproject a point into the feasibility region.
This function is guarunteed to return a new feasible point. However, no guaruntees in terms of proximity to the original point can be made.
- Parameters
p (numpy.ndarray) – The current sample point.
- Returns
A new feasible point. If p was feasible it wil return p.
- Return type
Warning
This method is intended for internal use only.
-
_random_point
(self)[source]¶ Find an approximately random point in the concentration cone.
Warning
This method is intended for internal use only.
-
_is_redundant
(self, matrix, cutoff=None)[source]¶ Identify redundant rows in a matrix that can be removed.
Warning
This method is intended for internal use only.
-
mass.thermo.conc_sampling.conc_hr_sampler.
step
(sampler, x, delta, fraction=None, tries=0)[source]¶ Sample new feasible point from point
x
in the directiondelta
.Has to be declared outside of class to be used for multiprocessing
- Parameters
sampler (ConcHRSampler) – The sampler instance.
x (float) – The starting point from which to sample.
delta (float) – The direction to travel from the point at
x
.fraction (float or None) – The fraction of the alpha range to use in determining alpha. If
None
then thenp.random.uniform()
function to get alpha.tries (int) – Number of tries. If the number of tries is greater than the
MAX_TRIES
, aRuntimeError
will be raised.
- Returns
The new feasible point.
- Return type
- Raises
RunTimeError – Raised when
tries > MAX_TRIES