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

ConcHRSampler

The abstract base class for hit and run concentration samplers.

Functions

step(sampler, x, delta[, fraction, tries])

Sample new feasible point from point x in the direction delta.

Attributes

LOGGER

Logger for conc_hr_sampler submodule.

MAX_TRIES

Maximum number of retries for sampling.

mass.thermo.conc_sampling.conc_hr_sampler.LOGGER[source]

Logger for conc_hr_sampler submodule.

Type

logging.Logger

mass.thermo.conc_sampling.conc_hr_sampler.MAX_TRIES = 100[source]

Maximum number of retries for sampling.

Type

int

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.

  • nproj (int or None) –

    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. If None then the value is determined via the following:

    nproj = int(min(len(self.concentration_solver.variables)**3, 1e6))
    

    Default is None

  • seed (int or None) –

    A positive int > 0 indiciating random number seed that should be used. If None provided, the current time stamp is used.

    Default is None.

concentration_solver

The ConcSolver used to generate samples.

Type

ConcSolver

feasibility_tol

The tolerance used for checking equalities feasibility.

Type

float

bounds_tol

The tolerance used for checking bounds feasibility.

Type

float

thinning

The currently used thinning factor.

Type

int

n_samples

The total number of samples that have been generated by this sampler instance.

Type

int

retries

The overall of sampling retries the sampler has observed. Larger values indicate numerical instabilities.

Type

int

problem

A namedtuple whose attributes define the entire sampling problem in matrix form. See docstring of Problem 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

numpy.matrix

property nproj[source]

Get or set nproj value.

Parameters

value (int or None) –

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. If None then the value is determined via the following:

nproj = int(min(len(self.concentration_solver.variables)**3, 1e6))

property seed[source]

Get or set nproj value.

Parameters

value (int or None) – A positive int > 0 indiciating random number seed that should be used. If None provided, the current time stamp is used.

generate_cva_warmup()[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(n, concs=True)[source]

Abstract sampling function.

Should be overwritten by child classes.

batch(batch_size, batch_num, concs=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).

  • 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 of n_m metabolites (or variables if concs=False) in each row.

_reproject(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

numpy.ndarray

Warning

This method is intended for internal use only.

_random_point()[source]

Find an approximately random point in the concentration cone.

Warning

This method is intended for internal use only.

_is_redundant(matrix, cutoff=None)[source]

Identify redundant rows in a matrix that can be removed.

Warning

This method is intended for internal use only.

_bounds_dist(p)[source]

Get the lower and upper bound distances. Negative is bad.

Warning

This method is intended for internal use only.

__build_problem()[source]

Build the matrix representation of the sampling problem.

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 direction delta.

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 the np.random.uniform() function to get alpha.

  • tries (int) – Number of tries. If the number of tries is greater than the MAX_TRIES, a RuntimeError will be raised.

Returns

The new feasible point.

Return type

float

Raises

RunTimeError – Raised when tries > MAX_TRIES