straindesign.parse_constr ========================= .. py:module:: straindesign.parse_constr .. autoapi-nested-parse:: Functions for parsing and converting constraints and linear expressions Module Contents --------------- .. py:function:: get_rids(expr, reaction_ids) 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" :param expr: A character string :type expr: str :param reaction_ids: List of reaction identifiers or variable names :type reaction_ids: list of str :returns: A list of strings containing the reaction/variable strings present in the input string :rtype: (list of str) .. py:function:: lineq2list(equations, reaction_ids) -> List 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], ...] :param equations: (List of) (in)equalities in string form equations=["r1 + 3*r2 = 0.3", "-5*r3 -r4 <= -0.5"] :type equations: list of str :param reaction_ids: List of reaction identifiers or variable names that are used to recognize variables in the provided (in)equalities :type reaction_ids: list of str :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 ...] # ... :rtype: (list of lists) .. py:function:: lineq2mat(equations, reaction_ids) -> Tuple[scipy.sparse.csr_matrix, Tuple, scipy.sparse.csr_matrix, Tuple] 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] :param equations: (List of) (in)equalities in string form equations=["r1 + 3*r2 = 0.3", "-5*r3 -r4 <= -0.5"] :type equations: list of str :param reaction_ids: List of reaction identifiers or variable names that are used to recognize variables in the provided (in)equalities :type reaction_ids: list of str :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 :rtype: (Tuple) .. py:function:: lineqlist2mat(D, reaction_ids) -> Tuple[scipy.sparse.csr_matrix, Tuple, scipy.sparse.csr_matrix, Tuple] 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] :param D: (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], ...] :type D: list of dict :param reaction_ids: List of reaction identifiers or variable names that are used to recognize variables in the provided (in)equalities :type reaction_ids: list of str :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 :rtype: (Tuple) .. py:function:: lineqlist2str(D) 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" :param D: (In)equality in list form, e.g.: D=[{"a":3.0,"b":-1.0,"c":2.0},"<=",2.0]] :type D: list :returns: A list of (in)equalities in string form :rtype: (str) .. py:function:: linexpr2dict(expr, reaction_ids) -> dict 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} :param expr: (In)equalities as a character string, e.g.: expr="2 R3 - R1" :type expr: str :param reaction_ids: List of reaction identifiers or variable names that are used to recognize variables in the input :type reaction_ids: list of str :returns: A dictionary that contains the variable names and the variable coefficients in the linear expression :rtype: (dict) .. py:function:: linexpr2mat(expr, reaction_ids) -> scipy.sparse.csr_matrix 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] :param expr: (In)equality as a character string: e.g., expr="2 R3 - R1" :type expr: str :param reaction_ids: List of reaction identifiers or variable names that are used to recognize variables in the input :type reaction_ids: list of str :returns: A single-row coefficient matrix that represents the input expression when multiplied with the variable vector :rtype: (sparse.csr_matrix) .. py:function:: linexprdict2mat(D, reaction_ids) -> scipy.sparse.csr_matrix 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] :param D: Linear expression as a dictionary :type D: dict :param reaction_ids: List of reaction identifiers or variable names :type reaction_ids: list of str :returns: A single-row coefficient matrix that represents the input expression when multiplied with the variable vector :rtype: (sparse.csr_matrix) .. py:function:: linexprdict2str(D) 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" :param D: Linear expression as a dictionary :type D: dict :returns: The input linear expression as a character string :rtype: (str) .. py:function:: parse_constraints(constr, reaction_ids) -> list Parses linear constraints written as strings Parses one or more *linear* constraints written as strings. :param constr: (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 ... :type constr: str or list of str :param reaction_ids: List of reaction identifiers. :type reaction_ids: list of str :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],...] :rtype: (List of dicts) .. py:function:: parse_linexpr(expr, reaction_ids) -> List Parses linear expressions written as strings Parses one or more *linear* expressions written as strings. :param expr: (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 ... :type expr: str or list of str :param reaction_ids: List of reaction identifiers. :type reaction_ids: list of str :returns: List of expressions. Each expression is a dictionary. E.g.: [{"r1":1.0,"r2":3.0},{"r3":-5.0,"r4":-1.0},...] :rtype: (List of dicts)