straindesign.parse_constr

Functions for parsing and converting constraints and linear expressions

Module Contents

straindesign.parse_constr.get_rids(expr, reaction_ids)[source]

Get reaction identifiers that are present in string

E.g.: input: D={‘R1’:-1.0, ‘R3’: 2.0}, translates to the string: ‘- 1.0 R1 + 2.0 R3’

Parameters:
  • expr (str) – A character string

  • reaction_ids (list of str) – List of reaction identifiers or variable names

Returns:

A list of strings containing the reaction/variable strings present in the input string

Return type:

(list of str)

straindesign.parse_constr.lineq2list(equations, reaction_ids) List[source]

Translates linear (in)equalities to list format: [lhs,sign,rhs]

Input inequalities in the form of strings are translated into a specific list format that facilitates the readout of left-hand-side, equality sign and right-hand-side of the inequality.

equations = [‘2*c - b +3*a <= 2’,’c - b = 0’,’2*b -5…], reaction_ids = [‘a’,’b’,’c’]

This will be translated to the [[{‘a’:3.0,’b’:-1.0,’c’:2.0},’<=’,2.0],[{‘b’:-1.0,’c’:1.0},’=’,0.0], …]

Parameters:
  • equations (list of str) – (List of) (in)equalities in string form equations=[‘r1 + 3*r2 = 0.3’, ‘-5*r3 -r4 <= -0.5’]

  • reaction_ids (list of str) – List of reaction identifiers or variable names that are used to recognize variables in the provided (in)equalities

Returns:

(In)equalities presented in the form: [[{‘a’:3.0,’b’:-1.0,’c’:2.0},’<=’,2.0], # e1

[{‘b’:-1.0,’c’:1.0},’=’,0.0], # e2 …] # …

Return type:

(list of lists)

straindesign.parse_constr.lineq2mat(equations, reaction_ids) Tuple[scipy.sparse.csr_matrix, Tuple, scipy.sparse.csr_matrix, Tuple][source]

Translates linear (in)equalities to matrices

Input inequalities in the form of strings is translated into matrices and vectors. The reaction list defines the order of variables and thus the columns of the resulting matrices, the order of (in)equalities will be preserved in the output matrices. As an example, take the input:

equations = [‘2*c - b +3*a <= 2’,’c - b = 0’,’2*b -a >=-2’], reaction_ids = [‘a’,’b’,’c’]

This will be translated to the form A_ineq * x <= b_ineq, A_eq * x = b_eq and hence to

A_ineq = sparse.csr_matrix([[3,-1,2],[1,-2,0]]), b_ineq = [2,2], A_eq = sparse.csr_matrix([[1,-2,0]]), b_eq = [0]

Parameters:
  • equations (list of str) – (List of) (in)equalities in string form equations=[‘r1 + 3*r2 = 0.3’, ‘-5*r3 -r4 <= -0.5’]

  • reaction_ids (list of str) – List of reaction identifiers or variable names that are used to recognize variables in the provided (in)equalities

Returns:

A_ineq, b_ineq, A_eq, b_eq. Coefficient matrices and right hand sides that represent the input (in)equalities as matrix-vector multiplications

Return type:

(Tuple)

straindesign.parse_constr.lineqlist2mat(D, reaction_ids) Tuple[scipy.sparse.csr_matrix, Tuple, scipy.sparse.csr_matrix, Tuple][source]

Translates linear (in)equalities presented in the list of lists format to matrices

Input inequalities in the list of lists form is translated into matrices and vectors. The reaction list defines the order of variables and thus the columns of the resulting matrices, the order of (in)equalities will be preserved in the output matrices. As an example, take the input:

D = [[{‘a’:3.0,’b’:-1.0,’c’:2.0},’<=’,2.0],[{‘b’:-1.0,’c’:1.0},’=’,0.0], [{‘a’:-1,’b’:2.0},’>=’,-2.0]]

This will be translated to the form A_ineq * x <= b_ineq, A_eq * x = b_eq and hence to

A_ineq = sparse.csr_matrix([[3,-1,2],[1,-2,0]]), b_ineq = [2,2], A_eq = sparse.csr_matrix([[1,-2,0]]), b_eq = [0]

Parameters:
  • D (list of dict) – (List of) (in)equalities in the list of list form: [[{‘a’:3.0,’b’:-1.0,’c’:2.0},’<=’,2.0],[{‘b’:-1.0,’c’:1.0},’=’,0.0], …]

  • reaction_ids (list of str) – List of reaction identifiers or variable names that are used to recognize variables in the provided (in)equalities

Returns:

A_ineq, b_ineq, A_eq, b_eq. Coefficient matrices and right hand sides that represent the input (in)equalities as matrix-vector multiplications

Return type:

(Tuple)

straindesign.parse_constr.lineqlist2str(D)[source]

Translates a linear (in)equality from the list format [lhs,sign,rhs] to a string

E.g. input: D=[{‘a’:3.0,’b’:-1.0,’c’:2.0},’<=’,2.0]] is translated to: out=’3.0 a - 1.0 b + 2.0 c <= 2’

Parameters:

D (list) – (In)equality in list form, e.g.: D=[{‘a’:3.0,’b’:-1.0,’c’:2.0},’<=’,2.0]]

Returns:

A list of (in)equalities in string form

Return type:

(str)

straindesign.parse_constr.linexpr2dict(expr, reaction_ids) dict[source]

Translates a linear expression into a dictionary

E.g.: input: expr=’2 R3 - R1’, reaction_ids=[‘R1’, ‘R2’, ‘R3’, ‘R4’] translates to a dict D={‘R1’:-1.0, ‘R3’: 2.0}

Parameters:
  • expr (str) – (In)equalities as a character string, e.g.: expr=’2 R3 - R1’

  • reaction_ids (list of str) – List of reaction identifiers or variable names that are used to recognize variables in the input

Returns:

A dictionary that contains the variable names and the variable coefficients in the linear expression

Return type:

(dict)

straindesign.parse_constr.linexpr2mat(expr, reaction_ids) scipy.sparse.csr_matrix[source]

Translates a linear expression into a vector

E.g.: input: expr=’2 R3 - R1’, reaction_ids=[‘R1’, ‘R2’, ‘R3’, ‘R4’] translates into sparse matrix: A = [-1 0 2 0]

Parameters:
  • expr (str) – (In)equality as a character string: e.g., expr=’2 R3 - R1’

  • reaction_ids (list of str) – List of reaction identifiers or variable names that are used to recognize variables in the input

Returns:

A single-row coefficient matrix that represents the input expression when multiplied with the variable vector

Return type:

(sparse.csr_matrix)

straindesign.parse_constr.linexprdict2mat(D, reaction_ids) scipy.sparse.csr_matrix[source]

Translates a linear expression from dict into a matrix

E.g.: input: D={‘R1’:-1.0, ‘R3’: 2.0}, reaction_ids=[‘R1’, ‘R2’, ‘R3’, ‘R4’] translates into sparse matrix: A = [-1 0 2 0]

Parameters:
  • D (dict) – Linear expression as a dictionary

  • reaction_ids (list of str) – List of reaction identifiers or variable names

Returns:

A single-row coefficient matrix that represents the input expression when multiplied with the variable vector

Return type:

(sparse.csr_matrix)

straindesign.parse_constr.linexprdict2str(D)[source]

Translates a linear expression from dict into a caracter string

E.g.: input: D={‘R1’:-1.0, ‘R3’: 2.0}, translates to the string: ‘- 1.0 R1 + 2.0 R3’

Parameters:

D (dict) – Linear expression as a dictionary

Returns:

The input linear expression as a character string

Return type:

(str)

straindesign.parse_constr.parse_constraints(constr, reaction_ids) list[source]

Parses linear constraints written as strings

Parses one or more linear constraints written as strings.

Parameters:
  • constr (str or list of str) – (List of) constraints in string form. E.g.: [‘r1 + 3*r2 = 0.3’, ‘-5*r3 -r4 <= -0.5’] or ‘1.0 r1 + 3.0*r2 =0.3,-r4-5*r3<=-0.5’ or …

  • reaction_ids (list of str) – List of reaction identifiers.

Returns:

List of constraints. Each constraint is a list of three elements. E.g.: [[{‘r1’:1.0,’r2’:3.0},’=’,0.3],[{‘r3’:-5.0,’r4’:-1.0},’<=’,-0.5],…]

Return type:

(List of dicts)

straindesign.parse_constr.parse_linexpr(expr, reaction_ids) List[source]

Parses linear expressions written as strings

Parses one or more linear expressions written as strings.

Parameters:
  • expr (str or list of str) – (List of) expressions in string form. E.g.: [‘r1 + 3*r2’, ‘-5*r3 -r4’] or ‘1.0 r1 + 3.0*r2,-r4-5*r3’ or …

  • reaction_ids (list of str) – List of reaction identifiers.

Returns:

List of expressions. Each expression is a dictionary. E.g.: [{‘r1’:1.0,’r2’:3.0},{‘r3’:-5.0,’r4’:-1.0},…]

Return type:

(List of dicts)