当前位置: 首页>>代码示例>>Python>>正文


Python Matrix.todense方法代码示例

本文整理汇总了Python中sympy.Matrix.todense方法的典型用法代码示例。如果您正苦于以下问题:Python Matrix.todense方法的具体用法?Python Matrix.todense怎么用?Python Matrix.todense使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sympy.Matrix的用法示例。


在下文中一共展示了Matrix.todense方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: create_element_matrix

# 需要导入模块: from sympy import Matrix [as 别名]
# 或者: from sympy.Matrix import todense [as 别名]
def create_element_matrix(cobra_model, db_cursor=None, me_matrix=False,
                          cas='sympy'):
    """Constructs a matrix of elements x metabolites for the metabolites
    in cobra_model.  If elemental compositions are not available for all
    metabolites then a symbolic matrix is returned with symbols representing
    the unknown compositions.

    cobra_model:  A cobra.Model object.

    db_cursor:  Internal use only.

    me_matrix: False. Internal use only.

    cas: 'sympy' or 'ginac'.  Specifies the computer algebra system to use.
    'ginac' is the more powerful solver however it is accessed through
    swiginac which isn't the easiest thing to install.

    """
    if cas.lower() == 'sympy':
        from sympy import solve, Matrix, Symbol, Add
        from sympy.core.numbers import Zero, Real
    elif cas.lower() == 'ginac':
        #Symbolic.Matrix might be slow to index so we might use
        #the swiginac matrix or numpy.matrix instead
        from Symbolic import Symbol, Matrix, Expr
                
        
    elements = ('c', 'h', 'o', 'n', 'p', 's', 'z') #z is for generics
    element_dict = dict(zip(elements, range(len(elements))))
    element_matrix = dok_matrix((len(elements), len(cobra_model.metabolites)))
    if db_cursor and me_matrix:
        #Used for current incarnation of ME matrix.  
        known_compositions = set()
        #1. Start off by getting known molecular compositions
        db_cursor.execute('Select Id, (c, h, o, n, p, s) from metabolite')
        metabolite_compositions = dict(db_cursor.fetchall())
        for the_metabolite, the_composition in metabolite_compositions.items():
            if the_metabolite in cobra_model.metabolites:
                known_compositions.add(the_metabolite)
                the_column = cobra_model.metabolites.index(the_metabolite)
                the_composition = eval(the_composition)
                element_matrix.update(dict([((i, the_column), the_composition[i])
                                            for i in range(len(the_composition))]))

        #2. Identify the reactions that produce generics and set the stoichiometry
        #to zero to deal with mass imbalances.
        #
        #This isn't a problem any more as the dummy reactions are not added
        #until after balancing.
        generic_production_reactions = dict([(x, cobra_model.reactions.index(x))
                                             for x in cobra_model.reactions\
                                             if x.startswith('generic_rename')])
        #collect all of the generic_X metabolite Ids and then set masses to 1 Z
        db_cursor.execute('SELECT FU_ID_Generic from Generic_FU')
        generic_metabolites = [x[0] for x in db_cursor.fetchall()]

        known_compositions.update(generic_metabolites)
        element_matrix.update(dict([((elements.index('z'),
                                      cobra_model.metabolites.index(x)), 1.)
                                    for x in generic_metabolites]))
        #Remove the generic production reactions
        for column_index in generic_production_reactions.values():
            the_column = cobra_model._S[:, column_index]
            for row_index in the_column.nonzero()[0]:
                the_column[row_index, 0] = 0

        #3. Remove demand reactions.
        #This isn't a problem any more as the demand reactions are not added
        #until after balancing.
        demand_indices = [cobra_model.reactions.index(x)
                          for x in cobra_model.reactions if 'demand' in x]
        for column_index in demand_indices:
            the_column = cobra_model._S[:, column_index]
            for row_index in the_column.nonzero()[0]:
                the_column[row_index, 0] = 0
        #4. Calculate molecular formula for transcripts and peptides.  This isn't
        #necessary, but will probably make solving the problem easier.

        #known_compositions.update(transcripts and peptides)

        #5. For all metabolites not in known compositions.  It will be
        #necessary to add a symbolic value to the matrix for each element
        #excluding z, i.e.  the_metabolite_(c, h, o, n, p, s, z). 
        metabolite_dict = dict(zip(cobra_model.metabolites,
                                   range(len(cobra_model.metabolites))))
        [metabolite_dict.pop(k)
         for k in known_compositions]
        #If there are any metabolites without compositions build a symbolic matrix
        if len(metabolite_dict) > 0:
            #Build the symbolic elemental composition matrix
            if cas.lower() == 'sympy':
                element_matrix = Matrix(element_matrix.todense())
            elif cas.lower() == 'ginac':
                element_matrix = Matrix(element_matrix.todense().tolist())
            #Now populate with symbols for the_metabolites not in known_compositions
            for the_metabolite, metabolite_index in metabolite_dict.items():
                for the_element, element_index in element_dict.items():
                    element_matrix[element_index,
                                   metabolite_index] = Symbol('%s_%s'%(the_metabolite,
                                                                       the_element))
#.........这里部分代码省略.........
开发者ID:Ajami712,项目名称:cobrapy,代码行数:103,代码来源:balance.py


注:本文中的sympy.Matrix.todense方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。