17.1.1.8.1.1. cobra.util.array¶

Helper functions for array operations and sampling.

17.1.1.8.1.1.1. Module Contents¶

17.1.1.8.1.1.1.1. Functions¶

 create_stoichiometric_matrix(model: Model, array_type: str = ‘dense’, dtype: Optional[np.dtype] = None) → Union[np.ndarray, dok_matrix, lil_matrix, pd.DataFrame] Return a stoichiometric array representation of the given model. nullspace(A: np.ndarray, atol: float = 1e-13, rtol: float = 0.0) → np.ndarray Compute an approximate basis for the nullspace of A. constraint_matrices(model: Model, array_type: str = ‘dense’, zero_tol: float = 1e-06) → NamedTuple Create a matrix representation of the problem.
cobra.util.array.create_stoichiometric_matrix(model: Model, array_type: str = 'dense', dtype: Optional[np.dtype] = None) → Union[np.ndarray, dok_matrix, lil_matrix, pd.DataFrame][source]

Return a stoichiometric array representation of the given model.

The the columns represent the reactions and rows represent metabolites. S[i,j] therefore contains the quantity of metabolite i produced (negative for consumed) by reaction j.

Parameters
• model (cobra.Model) – The cobra model to construct the matrix for.

• array_type ({"dense", "dok", "lil", "DataFrame"}) – The type of array to construct. “dense” will return a standard numpy.ndarray. “dok”, or “lil” will construct a sparse array using scipy of the corresponding type. “DataFrame” will give a pandas.DataFrame with metabolite as indices and reaction as columns.

• dtype (numpy.dtype, optional) – The desired numpy data type for the array (default numpy.float64).

Returns

The stoichiometric matrix for the given model.

Return type

matrix of class dtype

Raises
• ValueError – If sparse matrix is used and scipy is not installed.

• . deprecated: – 0.18.1: “DataFrame” option for array_type will be replaced with “frame” in future versions.

cobra.util.array.nullspace(A: np.ndarray, atol: float = 1e-13, rtol: float = 0.0) → np.ndarray[source]

Compute an approximate basis for the nullspace of A.

The algorithm used by this function is based on the Singular Value Decomposition (SVD) of A.

Parameters
• A (numpy.ndarray) – A should be at most 2-D. 1-D array with length k will be treated as a 2-D with shape (1, k).

• atol (float, optional) – The absolute tolerance for a zero singular value. Singular values smaller than atol are considered to be zero (default 1e-13).

• rtol (float, optional) – The relative tolerance. Singular values less than rtol * smax are considered to be zero, where smax is the largest singular value (default 0.0).

Returns

If A is an array with shape (m, k), then ns will be an array with shape (k, n), where n is the estimated dimension of the nullspace of A. The columns of ns are a basis for the nullspace; each element in numpy.dot(A, ns) will be approximately zero.

Return type

numpy.ndarray

Notes

This is taken from the numpy cookbook.

If both atol and rtol are positive, the combined tolerance is the maximum of the two; that is:

$\mathtt{tol} = \max(\mathtt{atol}, \mathtt{rtol} * \mathtt{smax})$

Singular values smaller than tol are considered to be zero.

cobra.util.array.constraint_matrices(model: Model, array_type: str = 'dense', zero_tol: float = 1e-06) → NamedTuple[source]

Create a matrix representation of the problem.

This is used for alternative solution approaches that do not use “optlang”. The function will construct the equality matrix, inequality matrix and bounds for the complete problem.

Parameters
• model (cobra.Model) – The model from which to obtain the LP problem.

• array_type ({"dense", "dok", "lil", "DataFrame"}) – The type of array to construct. “dense” will return a standard numpy.ndarray. “dok”, or “lil” will construct a sparse array using scipy of the corresponding type. “DataFrame” will give a pandas.DataFrame with metabolite as indices and reaction as columns.

• zero_tol (float, optional) – The zero tolerance used to judge whether two bounds are the same (default 1e-6).

Returns

A named tuple consisting of 6 matrices and 2 vectors: - “equalities” is a matrix S such that S * vars = b. It

includes a row for each constraint and one column for each variable.

• ”b” is the right side of the equality equation such that S * vars = b.

• ”inequalities” is a matrix M such that lb <= M * vars <= ub. It contains a row for each inequality and as many columns as variables.

• ”bounds” is a compound matrix [lb ub] containing the lower and upper bounds for the inequality constraints in M.

• ”variable_fixed” is a boolean vector indicating whether the variable at that index is fixed (lower bound == upper_bound) and is thus bounded by an equality constraint.

• ”variable_bounds” is a compound matrix [lb ub] containing the lower and upper bounds for all variables.

Return type

NamedTuple

Notes

To accomodate non-zero equalities, the problem will add the variable “const_one” which is a variable that equals one.

Deprecated since version 0.18.1: “DataFrame” option for array_type will be replaced with “frame” in future versions.