def test_math_name_overlap(self):
     model = Example()
     gamma = gillespy2.Species('gamma',initial_value=2, mode='continuous')
     model.add_species([gamma])
     k2 = gillespy2.Parameter(name='k2', expression=1)
     model.add_parameter([k2])
     gamma_react = gillespy2.Reaction(name='gamma_react', reactants={'gamma': 1}, products={}, rate=k2)
     model.add_reaction([gamma_react])
     model.run(solver=TauHybridSolver)
 def test_change_species(self):
     model = Example()
     initial_value = model.listOfSpecies['Sp'].initial_value
     solver = VariableSSACSolver(model)
     results = model.run(solver=solver, variables={'Sp': 3})
     with self.subTest(msg='Test changed species simulation'):
         self.assertEqual(results['Sp'][0], 3)
     with self.subTest(msg='Test changed species model integrity'):
         self.assertEqual(model.listOfSpecies['Sp'].initial_value,
                          initial_value)
 def test_change_parameter(self):
     model = Example()
     initial_expression = model.listOfParameters['k1'].expression
     solver = VariableSSACSolver(model)
     results = model.run(solver=solver, variables={'k1': 0})
     with self.subTest(msg='Test changed parameter simulation'):
         self.assertEqual(results['Sp'][-1], results['Sp'][0])
     with self.subTest(msg='Test changed parameter model integrity'):
         self.assertEqual(model.listOfParameters['k1'].expression,
                          initial_expression)
 def test_add_stochastic_time_dependent_event(self):
     model = Example()
     model.listOfSpecies['Sp'].mode = 'discrete'
     eventTrig = gillespy2.EventTrigger(expression='t >= 10', initial_value=True, )
     event1 = gillespy2.Event(name='event1', trigger=eventTrig)
     ea1 = gillespy2.EventAssignment(variable='Sp', expression='1000')
     ea2 = gillespy2.EventAssignment(variable='k1', expression='0')
     event1.add_assignment([ea1, ea2])
     model.add_event(event1)
     results = model.run()
     self.assertEqual(results['Sp'][-1], 1000)
 def test_add_continuous_species_dependent_event(self):
     model = Example()
     model.listOfSpecies['Sp'].mode = 'continuous'
     eventTrig = gillespy2.EventTrigger(expression='Sp <= 90', initial_value=True, )
     event1 = gillespy2.Event(name='event1', trigger=eventTrig)
     ea1 = gillespy2.EventAssignment(variable='Sp', expression='1000')
     ea2 = gillespy2.EventAssignment(variable='k1', expression='0')
     event1.add_assignment([ea1, ea2])
     model.add_event(event1)
     results = model.run()
     self.assertEqual(results[0].solver_name,'TauHybridSolver')
     self.assertEqual(results['Sp'][-1], 1000)
 def test_add_rate_rule(self):
     model = Example()
     species = gillespy2.Species('test_species', initial_value=1)
     rule = gillespy2.RateRule(name='rr1',formula='test_species+1',variable='test_species')
     model.add_species([species])
     model.add_rate_rule([rule])
     results = model.run()
     self.assertEqual(results[0].solver_name, 'TauHybridSolver')
Example #7
0
    def test_run_example(self):
        model = Example()
        for i in [1, self.number_of_trajectories]:
            for label in [True, False]:
                with self.subTest(number_of_trajectories=i, show_labels=label):
                    if i > 1:
                        with self.assertLogs(level='WARN'):
                            results = model.run(solver=BasicODESolver, show_labels=label, number_of_trajectories=i)
                        self.assertEqual(len(results), i)
                    else:
                        results = model.run(solver=BasicODESolver, show_labels=label, number_of_trajectories=i)

                    if i > 1 or not label:
                        for result in results[1:]:
                            if label:
                                self.assertEqual(results[0], result)

                            else:
                                self.assertTrue(np.array_equal(results[0], result))
 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 test_add_rate_rule_dict(self):
     model = Example()
     species2 = gillespy2.Species('test_species2', initial_value=2, mode='continuous')
     species3 = gillespy2.Species('test_species3', initial_value=3, mode='continuous')
     rule2 = gillespy2.RateRule(species2, 'cos(t)')
     rule3 = gillespy2.RateRule(variable=species3, formula='sin(t)')
     rate_rule_dict = {'rule2': rule2, 'rule3': rule3}
     model.add_species([species2, species3])
     with self.assertRaises(ParameterError):
         model.add_rate_rule(rate_rule_dict)
Example #10
0
class TestBasicTauLeapingSolver(unittest.TestCase):

    model = Example()
    results = model.run(solver=TauLeapingSolver, show_labels=False, number_of_trajectories=1)
    labels_results = model.run(solver=TauLeapingSolver, show_labels=True, number_of_trajectories=1)

    def test_return_type(self):
        assert(isinstance(self.results, np.ndarray))
        assert(isinstance(self.results[0], np.ndarray))
        assert(isinstance(self.results[0][0], np.ndarray))
        assert(isinstance(self.results[0][0][0], np.float))

    def test_return_type_show_labels(self):
        assert(isinstance(self.labels_results, results))
        assert(isinstance(self.labels_results['Sp'], np.ndarray))
        assert(isinstance(self.labels_results['Sp'][0], np.float))
Example #11
0
class TestBasicTauHybridSolver(unittest.TestCase):
    model = Example()
    
    def test_add_rate_rule(self):
        species = gillespy2.Species('test_species', initial_value=1, mode='continuous')
        rule = gillespy2.RateRule(species, 'cos(t)')
        self.model.add_species([species])
        self.model.add_rate_rule([rule])
        self.model.run(solver=BasicTauHybridSolver)

    def test_add_rate_rule_dict(self):
        species2 = gillespy2.Species('test_species2', initial_value=2, mode='continuous')
        species3 = gillespy2.Species('test_species3', initial_value=3, mode='continuous')
        rule2 = gillespy2.RateRule(species2, 'cos(t)')
        rule3 = gillespy2.RateRule(variable=species3, formula='sin(t)')
        rate_rule_dict = {'rule2': rule2, 'rule3': rule3}
        self.model.add_species([species2, species3])
        with self.assertRaises(ParameterError):
            self.model.add_rate_rule(rate_rule_dict)
        
    def test_add_bad_species_rate_rule_dict(self):
        species2 = gillespy2.Species('test_species2', initial_value=2, mode='continuous')
        rule = gillespy2.RateRule(formula='sin(t)')
        with self.assertRaises(ModelError):
            self.model.add_rate_rule(rule)

    def test_math_name_overlap(self):
        gamma = gillespy2.Species('gamma',initial_value=2, mode='continuous')
        self.model.add_species([gamma])
        k2 = gillespy2.Parameter(name='k2', expression=1)
        self.model.add_parameter([k2])
        gamma_react = gillespy2.Reaction(name='gamma_react', reactants={'gamma':1}, products={}, rate=k2)
        self.model.add_reaction([gamma_react])
        self.model.run(solver=BasicTauHybridSolver)

    def test_add_bad_expression_rate_rule_dict(self):
        species2 = gillespy2.Species('test_species2', initial_value=2, mode='continuous')
        rule = gillespy2.RateRule(variable=species2, formula='')
        with self.assertRaises(ModelError):
            self.model.add_rate_rule(rule)
 def test_create(self):
     model = Example()
     solver = VariableSSACSolver(model)
 def test_run_example(self):
     model = Example()
     results = model.run(solver=VariableSSACSolver)
 def test_invalid_variable(self):
     model = Example()
     solver = VariableSSACSolver(model)
     with self.assertRaises(SimulationError):
         results = model.run(solver=solver, variables={'foobar': 0})
 def test_add_bad_expression_rate_rule_dict(self):
     model = Example()
     species2 = gillespy2.Species('test_species2', initial_value=2, mode='continuous')
     rule = gillespy2.RateRule(variable=species2, formula='')
     with self.assertRaises(ModelError):
         model.add_rate_rule(rule)
 def test_ensure_hybrid_continuous_species(self):
     model = Example()
     species1 = gillespy2.Species('test_species1', initial_value=1,mode='continuous')
     model.add_species(species1)
     results = model.run()
     self.assertEqual(results[0].solver_name, 'TauHybridSolver')
 def test_run_example_precompiled(self):
     model = Example()
     solver = VariableSSACSolver(model)
     results = model.run(solver=solver)
 def test_add_bad_species_rate_rule_dict(self):
     model = Example()
     rule = gillespy2.RateRule(formula='sin(t)')
     with self.assertRaises(ModelError):
         model.add_rate_rule(rule)
class TestBasicTauLeapingSolver(unittest.TestCase):
    model = Example()
Example #20
0
class TestAllSolvers(unittest.TestCase):

    solvers = [SSACSolver, VariableSSACSolver, ODESolver, NumPySSASolver, TauLeapingSolver, TauHybridSolver]

    model = Example()
    for sp in model.listOfSpecies.values():
        sp.mode = 'discrete'
    results = {}
    labeled_results = {}
    labeled_results_more_trajectories = {}

    for solver in solvers:
        labeled_results[solver] = model.run(solver=solver, number_of_trajectories=1,seed=1)
        labeled_results_more_trajectories[solver] = model.run(solver=solver, number_of_trajectories=2)

    def test_instantiated(self):
        for solver in self.solvers:
            self.model.run(solver=solver())

    def test_to_array(self):
        for solver in self.solvers:
            self.assertTrue(isinstance(self.labeled_results[solver].to_array()[0], np.ndarray))

    def test_return_type_show_labels(self):
        for solver in self.solvers:
            self.assertTrue(isinstance(self.labeled_results[solver], Results))
            self.assertTrue(isinstance(self.labeled_results[solver]['Sp'], np.ndarray))
            self.assertTrue(isinstance(self.labeled_results[solver]['Sp'][0], np.float))

            self.assertTrue(isinstance(self.labeled_results[solver][0], Trajectory))

            self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver], Results))
            self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver][0], Trajectory))
            self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver][0]['Sp'], np.ndarray))
            self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver][0]['Sp'][0], np.float))


    def test_random_seed(self):
        for solver in self.solvers:
            same_results = self.model.run(solver=solver, seed=1)
            compare_results = self.model.run(solver=solver,seed=1)
            self.assertTrue(np.array_equal(same_results.to_array(), compare_results.to_array()))
            if solver.name == 'ODESolver': continue
            diff_results = self.model.run(solver=solver, seed=2)
            self.assertFalse(np.array_equal(diff_results.to_array(),same_results.to_array()))
    
    def test_extraneous_args(self):
        for solver in self.solvers:
            with self.assertLogs(level='WARN'):
                model = Example()
                results = model.run(solver=solver, nonsense='ABC')

    def test_timeout(self):
        for solver in self.solvers:
            with self.assertLogs(level='WARN'):
                model = Oregonator()
                model.timespan(np.linspace(0, 1000000, 101))
                results = model.run(solver=solver, timeout=1)

    def test_basic_solver_import(self):
        from gillespy2.solvers.numpy.basic_tau_leaping_solver import BasicTauLeapingSolver
        from gillespy2.solvers.numpy.basic_ode_solver import BasicODESolver
        from gillespy2.solvers.numpy.basic_tau_hybrid_solver import BasicTauHybridSolver
        model = MichaelisMenten()
        results1 = model.run(solver=BasicTauLeapingSolver)
        self.assertTrue(results1[0].solver_name == 'TauLeapingSolver')

        results2 = model.run(solver=BasicODESolver)
        self.assertTrue(results2[0].solver_name == 'ODESolver')

        results3 = model.run(solver=BasicTauHybridSolver)
        self.assertTrue(results3[0].solver_name == 'TauHybridSolver')
Example #21
0
 def test_StochML_from_and_to_model(self):
     model = Example()
     stochml = StochMLDocument.from_model(model)
     stochml_model = stochml.to_model('model')
     stochml_model.run(solver=ODESolver)
     stochml_model.run(solver=NumPySSASolver)
class TestNumPySSASolver(unittest.TestCase):
    model = Example()
 def test_ensure_continuous_dynamic_timeout_warning(self):
     model = Example()
     species1 = gillespy2.Species('test_species1', initial_value=1, mode='dynamic')
     model.add_species(species1)
     with self.assertLogs(level='WARN'):
         results = model.run(timeout=1)
Example #24
0
class TestAllSolvers(unittest.TestCase):

    solvers = [SSACSolver, BasicODESolver, NumPySSASolver, BasicTauLeapingSolver, BasicTauHybridSolver]

    model = Example()
    for sp in model.listOfSpecies.values():
        sp.mode = 'discrete'
    results = {}
    labeled_results = {}
    labeled_results_more_trajectories = {}

    for solver in solvers:
        results[solver] = model.run(solver=solver, show_labels=False, seed=1)
        labeled_results[solver] = model.run(solver=solver, show_labels=True,number_of_trajectories=1)
        labeled_results_more_trajectories[solver] = model.run(solver=solver, show_labels=True,number_of_trajectories=2)

    def test_instantiated(self):
        for solver in self.solvers:
            self.model.run(solver=solver())

    def test_return_type(self):
        for solver in self.solvers:
            self.assertTrue(isinstance(self.results[solver], np.ndarray))
            self.assertTrue(isinstance(self.results[solver][0], np.ndarray))
            self.assertTrue(isinstance(self.results[solver][0][0], np.ndarray))
            self.assertTrue(isinstance(self.results[solver][0][0][0], np.float))

    def test_return_type_show_labels(self):
        for solver in self.solvers:
            self.assertTrue(isinstance(self.labeled_results[solver], Results))
            self.assertTrue(isinstance(self.labeled_results[solver]['Sp'], np.ndarray))
            self.assertTrue(isinstance(self.labeled_results[solver]['Sp'][0], np.float))

            with self.assertWarns(Warning):
                self.assertTrue(isinstance(self.labeled_results[solver][0], Results))

            self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver], EnsembleResults))
            self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver][0], Results))
            self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver][0]['Sp'], np.ndarray))
            self.assertTrue(isinstance(self.labeled_results_more_trajectories[solver][0]['Sp'][0], np.float))


    def test_random_seed(self):
        for solver in self.solvers:
            same_results = self.model.run(solver=solver, show_labels=False, seed=1)
            self.assertTrue(np.array_equal(same_results, self.results[solver]))
            if solver.name == 'BasicODESolver': continue
            diff_results = self.model.run(solver=solver, show_labels=False, seed=2)
            self.assertFalse(np.array_equal(diff_results, self.results[solver]))
    
    def test_extraneous_args(self):
        for solver in self.solvers:
            with self.assertLogs(level='WARN'):
                model = Example()
                results = model.run(solver=solver, nonsense='ABC')

    def test_timeout(self):
        for solver in self.solvers:
            with self.assertLogs(level='WARN'):
                model = Oregonator()
                model.timespan(np.linspace(0, 1000000, 101))
                results = model.run(solver=solver, timeout=1)
 def test_file_with_directory_name_exists(self):
     with self.assertRaises(DirectoryError):
         temp = tempfile.NamedTemporaryFile()
         model = Example()
         solver = VariableSSACSolver(model, temp.name)
Example #26
0
 def test_extraneous_args(self):
     for solver in self.solvers:
         with self.assertLogs(level='WARN'):
             model = Example()
             results = model.run(solver=solver, nonsense='ABC')
 def test_add_function_definition(self):
     model = Example()
     funcdef = gillespy2.FunctionDefinition(name='fun', function='Sp+1')
     model.add_function_definition(funcdef)
     results = model.run()
     self.assertEqual(results[0].solver_name,'TauHybridSolver')