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))
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))
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")
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")
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, {})
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")
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
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")
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})
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)
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")
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")
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")
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))
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)
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)
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")
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")
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)")
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)
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")
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)")
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)")
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))
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})
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")
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")