예제 #1
0
    def __init__(self, parameter_values=None):
        Model.__init__(self, name="chapter2")
        self.volume = 1

        # Parameters
        self.add_parameter(Parameter(name="birth_A", expression="1"))
        self.add_parameter(Parameter(name="birth_B", expression="0.1"))
        self.add_parameter(Parameter(name="death_B", expression="0.05"))

        # Variables
        self.add_species(Species(name="A", initial_value=100, mode="discrete"))
        self.add_species(Species(name="B", initial_value=0, mode="discrete"))

        # Reactions
        self.add_reaction(
            Reaction(name="birthA",
                     reactants={},
                     products={'A': 1},
                     rate=self.listOfParameters["birth_A"]))
        self.add_reaction(
            Reaction(name="birthB",
                     reactants={'B': 1},
                     products={},
                     rate=self.listOfParameters["death_B"]))
        self.add_reaction(
            Reaction(name="birthC",
                     reactants={'A': 1},
                     products={'B': 1},
                     rate=self.listOfParameters["birth_B"]))

        # Timespan
        self.timespan(np.arange(0, 100, 1.0))
예제 #2
0
    def __init__(self, parameter_values=None):
        Model.__init__(self, name="lotka_volterra")
        self.volume = 1

        ## Parameters
        self.add_parameter(Parameter(name="k1", expression=1.0))
        self.add_parameter(Parameter(name="k2", expression=0.005))
        self.add_parameter(Parameter(name="k3", expression=0.6))

        ## Species
        self.add_species(Species(name='prey', initial_value = 100, mode = 'discrete'))
        self.add_species(Species(name='predator', initial_value = 100, mode = 'discrete'))

        ## Reactions
        self.add_reaction(Reaction(name="r1",
                                   reactants = {'prey' : 1},
                                   products = {'prey' : 2},
                                   rate = self.listOfParameters['k1']))
        self.add_reaction(Reaction(name="r2",
                                   reactants = {'predator': 1, 'prey' : 1},
                                   products = {'predator' : 2},
                                   rate = self.listOfParameters['k2']))
        self.add_reaction(Reaction(name="r3",
                                   reactants = {'predator' : 1},
                                   products = {},
                                   rate = self.listOfParameters['k3']))

        # Timespan
        self.timespan(np.linspace(0, 50, 51))
예제 #3
0
 def setUp(self):
     self.valid_ma_reaction = Reaction(name="test_reaction",
                                       reactants={"A": 1},
                                       products={"B": 1},
                                       rate="k1")
     self.valid_cp_reaction = Reaction(name="test_reaction",
                                       reactants={"A": 1},
                                       products={"B": 1},
                                       propensity_function="k1 * A")
예제 #4
0
 def test_constructor__products_invalid_keys(self):
     """ Test the Reaction constructor with products keyed with invalid keys. """
     with self.assertRaises(ReactionError):
         Reaction(name="test_reaction",
                  reactants={"A": 1},
                  products={1: 1},
                  rate="k2")
예제 #5
0
 def test_constructor__products_is_none(self):
     """ Test the Reaction constructor with None as products. """
     reaction = Reaction(name="test_reaction",
                         reactants={"A": 1},
                         products=None,
                         rate="k2")
     self.assertEqual(reaction.products, {})
예제 #6
0
 def test_constructor__invalid_products(self):
     """ Test the Reaction constructor with non-dict products. """
     with self.assertRaises(ReactionError):
         Reaction(name="test_reaction",
                  reactants={"A": 1},
                  products=[["B", 1]],
                  rate="k2")
예제 #7
0
    def build_reaction(self, args, parameters):
        '''
        Build a GillesPy2 reaction.

        Attributes
        ----------
        args : dict
            A json representation of a reaction.
        parameters : list
            List of GillesPy2 parameters.
        '''
        if args['reactionType'] not in 'custom-propensity':
            rate = list(
                filter(lambda p: p.name == args['rate']['name'],
                       parameters))[0]
            propensity = None
        else:
            rate = None
            propensity = args['propensity']
        R = Reaction(name=args['name'],
                     reactants=self.build_stoich_species_dict(
                         args['reactants']),
                     products=self.build_stoich_species_dict(args['products']),
                     rate=rate,
                     propensity_function=propensity)
        return R
예제 #8
0
 def test_constructor__products_invalid_values(self):
     """ Test the Reaction constructor with products containing invalid stoichiometry. """
     with self.assertRaises(ReactionError):
         Reaction(name="test_reaction",
                  reactants={"A": 1},
                  products={"B": "1"},
                  rate="k2")
예제 #9
0
 def test_constructor__products_keyed_by_species_obj(self):
     """ Test the Reaction constructor with products keyed by species objs. """
     test_species = Species(name="B", initial_value=0)
     reaction = Reaction(name="test_reaction",
                         reactants={"A": 1},
                         products={test_species: 1},
                         rate="k2")
     self.assertEqual(reaction.products, {"B": 1})
예제 #10
0
 def test_model_add__reaction(self):
     from gillespy2 import Reaction
     r4 = Reaction(name="r4",
                   reactants={"s1": 1},
                   products={"s2": 1},
                   rate="k1")
     self.model.add(r4)
     self.assertIn("r4", self.model.listOfReactions)
예제 #11
0
 def test_constructor__float_rate(self):
     """ Test the Reaction constructor with a float rate. """
     reaction = Reaction(name="test_reaction",
                         reactants={"A": 1},
                         products={"B": 1},
                         rate=0.5)
     self.assertIsInstance(reaction.marate, str)
     self.assertEqual(reaction.marate, "0.5")
예제 #12
0
 def test_constructor__rate_and_ode_propensity_function_are_not_none(self):
     """ Test the Reaction constructor with rate and propensity function set. """
     with self.assertRaises(ReactionError):
         Reaction(name="test_reaction",
                  reactants={"A": 1},
                  products={"B": 1},
                  rate="k1",
                  ode_propensity_function="A**2 + B**2")
예제 #13
0
 def test_constructor__parameter_rate(self):
     """ Test the Reaction constructor with an parameter object as rate. """
     k1 = Parameter(name="k1", expression="20")
     reaction = Reaction(name="test_reaction",
                         reactants={"A": 1},
                         products={"B": 1},
                         rate=k1)
     self.assertIsInstance(reaction.marate, str)
     self.assertEqual(reaction.marate, "k1")
예제 #14
0
 def test_constructor__name_is_none_or_empty(self):
     """ Test the Reaction constructor with None or empty string as name. """
     test_names = [None, ""]
     for test_name in test_names:
         with self.subTest(name=test_name):
             reaction = Reaction(name=test_name,
                                 reactants={"A": 1},
                                 products={"B": 1},
                                 propensity_function="k1 * A * B")
             self.assertIsNotNone(re.search("rxn.*", reaction.name))
예제 #15
0
 def test_constructor__rate_not_accepted_type(self):
     """ Test the Reaction constructor with a rate that is an invalid type. """
     test_rates = ["", ["k1"]]
     for test_rate in test_rates:
         with self.subTest(rate=test_rate):
             with self.assertRaises(ReactionError):
                 reaction = Reaction(name="test_reaction",
                                     reactants={"A": 1},
                                     products={"B": 1},
                                     rate=test_rate)
예제 #16
0
 def test_constructor__invalid_ode_propensity_function(self):
     """ Test the Reaction constructor with a ode propensity function that is not of the proper type. """
     test_opfs = ["", ["k1 * A * B"]]
     for test_opf in test_opfs:
         with self.subTest(ode_propensity_function=test_opf):
             with self.assertRaises(ReactionError):
                 Reaction(name="test_reaction",
                          reactants={"A": 1},
                          products={"B": 1},
                          ode_propensity_function=test_opf)
예제 #17
0
 def test_constructor__float_ode_propensity_function(self):
     """ Test the Reaction constructor with a float ode propensity function. """
     reaction = Reaction(name="test_reaction",
                         reactants={"A": 1},
                         products={"B": 1},
                         ode_propensity_function=0.5)
     self.assertIsInstance(reaction.propensity_function, str)
     self.assertEqual(reaction.propensity_function, "0.5")
     self.assertIsInstance(reaction.ode_propensity_function, str)
     self.assertEqual(reaction.ode_propensity_function, "0.5")
예제 #18
0
 def test_constructor__int_propensity_function(self):
     """ Test the Reaction constructor with an int propensity function. """
     reaction = Reaction(name="test_reaction",
                         reactants={"A": 1},
                         products={"B": 1},
                         propensity_function=20)
     self.assertIsInstance(reaction.propensity_function, str)
     self.assertEqual(reaction.propensity_function, "20")
     self.assertIsInstance(reaction.ode_propensity_function, str)
     self.assertEqual(reaction.ode_propensity_function, "20")
예제 #19
0
 def test_constructor__custom_ode_propensity(self):
     """ Test the Reaction constructor for a custom ode propensity reaction. """
     reaction = Reaction(name="test_reaction",
                         reactants={"A": 1},
                         products={"B": 1},
                         ode_propensity_function="k1 * A * B")
     self.assertEqual(reaction.name, "test_reaction")
     self.assertEqual(reaction.reactants, {"A": 1})
     self.assertEqual(reaction.products, {"B": 1})
     self.assertEqual(reaction.propensity_function, "((k1*A)*B)")
     self.assertEqual(reaction.ode_propensity_function, "((k1*A)*B)")
예제 #20
0
 def test_constructor__annotation_invalid_type(self):
     """ Test the Reaction construct with an annotation of invalid type. """
     test_annotations = [5, 0.1, ["g"]]
     for test_annotation in test_annotations:
         with self.subTest(annotation=test_annotation):
             with self.assertRaises(ReactionError):
                 reaction = Reaction(name="test_reaction",
                                     reactants={"A": 1},
                                     products={"B": 1},
                                     rate="k1",
                                     annotation=test_annotation)
예제 #21
0
 def test_constructor__invalid_reactants_and_products(self):
     """ Test the Reaction constructor with reactants and products both set to None or empty. """
     test_reacs = [None, {}]
     test_prods = [None, {}]
     for test_reac in test_reacs:
         for test_prod in test_prods:
             with self.subTest(reactants=test_reac, products=test_prod):
                 with self.assertRaises(ReactionError):
                     Reaction(name="test_reaction",
                              reactants=test_reac,
                              products=test_prod,
                              rate="k1")
예제 #22
0
 def test_constructor__different_custom_propensity(self):
     """ Test the Reaction constructor for a custom propensity reaction with different propensities. """
     reaction = Reaction(name="test_reaction",
                         reactants={"A": 1},
                         products={"B": 1},
                         propensity_function="k1 * A * B",
                         ode_propensity_function="k1 * A * B / vol")
     self.assertEqual(reaction.name, "test_reaction")
     self.assertEqual(reaction.reactants, {"A": 1})
     self.assertEqual(reaction.products, {"B": 1})
     self.assertEqual(reaction.propensity_function, "((k1*A)*B)")
     self.assertEqual(reaction.ode_propensity_function, "(((k1*A)*B)/vol)")
예제 #23
0
 def test_constructor__mass_action(self):
     """ Test the Reaction constructor for a mass action reaction. """
     reaction = Reaction(name="test_reaction",
                         reactants={"A": 1},
                         products={"B": 1},
                         rate="k1")
     self.assertEqual(reaction.name, "test_reaction")
     self.assertEqual(reaction.reactants, {"A": 1})
     self.assertEqual(reaction.products, {"B": 1})
     self.assertEqual(reaction.marate, "k1")
     self.assertEqual(reaction.propensity_function, "(k1*A)")
     self.assertEqual(reaction.ode_propensity_function, "(k1*A)")
예제 #24
0
 def test_constructor__no_name(self):
     """ Test the Reaction constructor with no name provided. """
     reaction = Reaction(reactants={"A": 1},
                         products={"B": 1},
                         propensity_function="k1 * A * B")
     self.assertIsNotNone(re.search("rxn.*", reaction.name))
예제 #25
0
 def test_constructor__rate_and_propensity_functions_are_none(self):
     """ Test the Reaction constructor with rate and propensity functions set to None. """
     with self.assertRaises(ReactionError):
         Reaction(name="test_reaction",
                  reactants={"A": 1},
                  products={"B": 1})
예제 #26
0
 def test_constructor__invalid_name(self):
     """ Test the Reaction constructor with non-str name. """
     with self.assertRaises(ReactionError):
         Reaction(name=0, reactants={"A": 1}, products={"B": 1}, rate="k1")
예제 #27
0
 def test_constructor__no_reactants_or_products(self):
     """ Test the Reaction constructor with reactants and products not set. """
     with self.assertRaises(ReactionError):
         Reaction(name="test_reaction", rate="k1")