def test_model_add__multiple_components__in_order(self): import gillespy2 s1 = gillespy2.Species(name="s1", initial_value=29) k1 = gillespy2.Parameter(name="k1", expression=29) r1 = gillespy2.Reaction(name="r1", reactants={"s1": 1}, rate="k1") rr1 = gillespy2.RateRule(name="rr1", variable="k1", formula="29") ar1 = gillespy2.AssignmentRule(name="ar1", variable="s1", formula="29") ea = gillespy2.EventAssignment(name="ea", variable="k1", expression="29") et = gillespy2.EventTrigger(expression="t > 29") e1 = gillespy2.Event(name="e1", trigger=et, assignments=[ea]) divide = gillespy2.FunctionDefinition(name="divide", function="x / y", args=["x", "y"]) tspan = gillespy2.TimeSpan(range(100)) model = gillespy2.Model(name="Test Model") model.add([s1, k1, r1, rr1, ar1, e1, divide, tspan]) self.assertIn("ar1", model.listOfAssignmentRules) self.assertIn("e1", model.listOfEvents) self.assertIn("divide", model.listOfFunctionDefinitions) self.assertIn("k1", model.listOfParameters) self.assertIn("rr1", model.listOfRateRules) self.assertIn("r1", model.listOfReactions) self.assertIn("s1", model.listOfSpecies) self.assertEqual(tspan, model.tspan)
def test_add_assignment_rule(self): model = Example() species = gillespy2.Species('test_species4', initial_value=1) rule = gillespy2.AssignmentRule(name='ar1', variable=species.name, formula='2') model.add_species([species]) model.add_assignment_rule([rule]) results = model.run() self.assertEquals(results[species.name][0], 2) self.assertEquals(results[species.name][-1], 2) self.assertEqual(results[0].solver_name,'TauHybridSolver')
def setUpClass(cls): cls.solvers = [ SSACSolver, ODESolver, NumPySSASolver, TauLeapingSolver, TauHybridSolver, ODECSolver, TauLeapingCSolver, TauHybridCSolver, ] cls.sbml_features = { "AssignmentRule": lambda model, variable: model.add_assignment_rule(gillespy2.AssignmentRule(variable=variable, formula="1/(t+1)")), "RateRule": lambda model, variable: model.add_rate_rule(gillespy2.RateRule(variable=variable, formula="2*t")), "Event": lambda model, variable: model.add_event(gillespy2.Event( trigger=gillespy2.EventTrigger(expression="t>1"), assignments=[gillespy2.EventAssignment(variable=variable, expression="100")] )), "FunctionDefinition": lambda model, variable: model.add_function_definition( gillespy2.FunctionDefinition(name="fn", function="variable", args=["variable"])), } # List of supported SBML features for each solver. # When a feature is implemented for a particular solver, add the feature to its list. cls.solver_supported_sbml_features = { NumPySSASolver: [], TauLeapingSolver: [], ODESolver: [], TauHybridSolver: [ "AssignmentRule", "RateRule", "Event", "FunctionDefinition", ], SSACSolver: [], ODECSolver: [], TauLeapingCSolver: [], TauHybridCSolver: [ "RateRule", "Event", ], } cls.model = create_decay() cls.results = {} cls.labeled_results = {} cls.labeled_results_more_trajectories = {}
def __get_rules(sbml_model, gillespy_model, errors): for i in range(sbml_model.getNumRules()): rule = sbml_model.getRule(i) rule_name = rule.getId() rule_variable = rule.getVariable() rule_string = __get_math(rule.getMath()) if rule_variable in gillespy_model.listOfParameters: # Treat Non-Constant Parameters as Species value = gillespy_model.listOfParameters[rule_variable].expression species = gillespy2.Species(name=rule_variable, initial_value=value, allow_negative_populations=True, mode='continuous') gillespy_model.delete_parameter(rule_variable) gillespy_model.add_species([species]) t = [] if rule.isAssignment(): assign_value = eval(rule_string, {**eval_globals, **init_state}) postponed_evals[rule_variable] = rule_string gillespy_rule = gillespy2.AssignmentRule(name=rule_name, variable=rule_variable, formula=rule_string) gillespy_model.add_assignment_rule(gillespy_rule) init_state[gillespy_rule.variable] = eval(gillespy_rule.formula, { **init_state, **eval_globals }) if rule.isRate(): gillespy_rule = gillespy2.RateRule(name=rule_name, variable=rule_variable, formula=rule_string) gillespy_model.add_rate_rule(gillespy_rule) if rule.isAlgebraic(): t.append('algebraic') if len(t) > 0: t[0] = t[0].capitalize() msg = ", ".join(t) msg += " rule" else: msg = "Rule" errors.append([ "{0} '{1}' found on line '{2}' with equation '{3}'. gillespy does not support SBML Algebraic Rules" .format(msg, rule.getId(), rule.getLine(), libsbml.formulaToString(rule.getMath())), -5 ])