Did I find the right examples for you? yes no      Crawl my project      Python Jobs

All Samples(42)  |  Call(39)  |  Derive(0)  |  Import(3)
Generates a random :py:class:`~AChemKit.reactionnet.ReactionNetwork` by assigning reaction randomly between all molecular species.

Arguments:

nmols
    Number of molecules in the reaction network.

.. note::

    :py:class:`AChemKit.reactionnet.ReactionNetwork` tracks molecules by their reactions, so if a molecule is not part of any reaction(more...)

        def Uniform(nmols, nreactions, nreactants, nproducts, rates = 1.0, cls = ReactionNetwork, rng = None):
    """
    Generates a random :py:class:`~AChemKit.reactionnet.ReactionNetwork` by assigning reaction randomly between all molecular species.

    Arguments:

    nmols
        Number of molecules in the reaction network.

    .. note::

        :py:class:`AChemKit.reactionnet.ReactionNetwork` tracks molecules by their reactions, so if a molecule is not part of any reaction
        it will not appear at all e.g. in :py:attr:`~AChemKit.reactionnet.ReactionNetwork.seen`. This could lead to differences from `nmols`.

    nreactions
        Number of reaction in the reaction network. 

    .. note::

        The value of `nreactions` is the number of times a reaction will be added to the :py:class:`~AChemKit.reactionnet.ReactionNetwork`. If it
        is already in the :py:class:`~AChemKit.reactionnet.ReactionNetwork`, it will be replaced. This can lead to :class:`AChemKit.reactionnet.ReactionNetwork` with less
        than `nreactions` reactions.

    nreactants
        Number of reactants for each reaction in the reaction network. Can be a single value or a tuple/list which will be uniformly sampled from (duplicates can be used to give a non-uniform distribution).

        .. note::
            If this is a tuple/list it will be sampled for each reaction.

    nproducts
        Number of products for each reaction in the reaction network. Can be a single value or a tuple/list which will be uniformly sampled from (duplicates can be used to give a non-uniform distribution).

        If this is None, then `nreactants` must be a list/tuple of tuples of (`nreactants`, `nproducts`) pairs that will be uniformly sampled from. Or `nreactants` must be a dictionary with keys of (`nreactants`, `nproducts`) and values of weightings, which will be sampled from.

        .. note::
            If this is a tuple/list it will be sampled for each reaction.

    rates
        Rate of each reaction in the reaction network. Can be a single value or a tuple/list which will be uniformly sampled from (duplicates can be used to give a non-uniform distribution).

        .. note::
            If this is a tuple/list it will be sampled for each reaction.

    cls
        Alternative class to use for constructing the return rather than :py:class:`AChemKit.reactionnet.ReactionNetwork`.

    rng
        Random number generator to use. If not specifed, one will be generated at random.


    These arguments can be a single value, a tuple/list which will be uniformly sampled from, or a dictionary of value/weighting which will be sampled from

    For example:

    ``Uniform(5,3,2,1)`` will generate 5 molecules with 3 reactions between them where each reaction has two reactants and one product.

    ``Uniform(5,3,(1,2), (1,2))`` will generate 5 molecules with 3 reactions between them where each reaction has one or two reactants and one or two products.

    ``Uniform(5,3,((2,1),(1,2)), None)`` will generate 5 molecules with 3 reactions between them where each reaction has either two reactants and one product or one reactant and two products.

    """

    if rng is None:
        rng = random.Random(random.random())

    if isinstance(nmols, int):
        nmols = ["M%d"%i for i in xrange(nmols)]

    nreactions = get_sample(nreactions, rng)
    

    if nproducts is None:
        #its a tuple not a generator because its the same variable name
        newnreactants = []
        newnproducts = []
        for nreactant, nproduct in [get_sample(nreactants, rng) for i in xrange(nreactions)]:
            newnreactants.append(nreactant)
            newnproducts.append(nproduct)
        nproducts = newnreactants
        nreactants = newnproducts
    else:
        #its a tuple not a generator because its the same variable name
        nreactants = [get_sample(nreactants, rng) for i in xrange(nreactions)]
        nproducts = [get_sample(nproducts, rng) for i in xrange(nreactions)]

    rates = [get_sample(rates, rng) for i in xrange(nreactions)]

    outrates = {}
    for thisnreactants, thisnproducts, thisrate in zip(nreactants, nproducts, rates):

        reactants = [get_sample(nmols, rng)  for j in xrange(thisnreactants)]
        products = [get_sample(nmols, rng)  for j in xrange(thisnproducts)]
        reactants = OrderedFrozenBag(reactants)
        products = OrderedFrozenBag(products)

        outrates[(reactants, products)] = thisrate

    return cls(outrates)
        


src/a/c/AChemKit-0.3.0/AChemKit/tools/chem_uniform.py   AChemKit(Download)
 
import AChemKit
from AChemKit.randomnet import Uniform 
 
if __name__=="__main__":
 
    rng = random.Random(args.seed)
    net = Uniform(nmols, nreactions, nreactants, nproducts, rates, rng=rng)
 
    chemstr = """#Uniform reaction network

src/a/c/AChemKit-0.3.0/AChemKit/randomnet_test.py   AChemKit(Download)
import unittest
 
from AChemKit.randomnet import Uniform, Linear
import random
 
    def test_nmols_int(self):
        net = Uniform(10, 50, 2, 2)
        net = Uniform(10, 50, 2, 2, rng = self.rng)
        self.assertEqual(net.seen, self.mols)
 
    def test_nmols_tuple(self):
        net = Uniform(self.mols, 50, 2, 2)
        net = Uniform(self.mols, 50, 2, 2, rng = self.rng)