Example #1
0
def check_cpp_support():
    from gillespy2.example_models import Example
    try:
        model = Example()
        results = model.run(solver=SSACSolver)
        return True
    except:
        return False
class TestBasicTauHybridSolver(unittest.TestCase):

    model = Example()
    results = model.run(solver=BasicTauHybridSolver, show_labels=False)
    labels_results = model.run(solver=BasicTauHybridSolver, show_labels=True)

    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, list))
        assert(isinstance(self.labels_results[0], dict))
        assert(isinstance(self.labels_results[0]['Sp'], np.ndarray))
        assert(isinstance(self.labels_results[0]['Sp'][0], np.float))

    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(species3, '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)
Example #3
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(species=species3, expression='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(expression='sin(t)')
        with self.assertRaises(ModelError):
            self.model.add_rate_rule(rule)

    def test_add_bad_expression_rate_rule_dict(self):
        species2 = gillespy2.Species('test_species2', initial_value=2, mode='continuous')
        rule = gillespy2.RateRule(species=species2, expression='')
        with self.assertRaises(ModelError):
            self.model.add_rate_rule(rule)
Example #4
0
class TestAllSolvers(unittest.TestCase):

    solvers = [SSACSolver, BasicODESolver, NumPySSASolver, BasicTauLeapingSolver, BasicTauHybridSolver]
    if can_use_cython:
        solvers.append(CythonSSASolver)

    model = Example()
    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]))
            diff_results = self.model.run(solver=solver, show_labels=False, seed=2)
            if solver.name != 'BasicODESolver':
                self.assertFalse(np.array_equal(diff_results, self.results[solver]))
    
    def test_extraneous_args(self):
        for solver in self.solvers:
            print(solver.name)
            with self.assertWarns(Warning):
                model = Example()
                results = model.run(solver=solver, nonsense='ABC')
 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.assertWarns(Warning):
                         results = model.run(solver=BasicODESolver,
                                             show_labels=label,
                                             number_of_trajectories=i)
                 else:
                     results = model.run(solver=BasicODESolver,
                                         show_labels=label,
                                         number_of_trajectories=i)
                 self.assertEqual(len(results), i)
                 self.assertTrue(
                     all([
                         isinstance(result, dict if label else np.ndarray)
                         for result in results
                     ]))
                 if label:
                     result = results[0]
                     for species in model.listOfSpecies.keys():
                         self.assertIn(species, result.keys())
                         self.assertIsInstance(result[species], np.ndarray)
                         self.assertListEqual(list(result[species].shape),
                                              list(model.tspan.shape))
                 else:
                     self.assertIsInstance(results, np.ndarray)
                     self.assertListEqual(list(results.shape), [
                         i,
                         len(model.tspan),
                         len(model.listOfSpecies.keys()) + 1
                     ])
                 for result in results[1:]:
                     if label:
                         self.assertDictEqual(results[0], result)
                     else:
                         self.assertTrue(np.array_equal(results[0], result))
Example #6
0
class TestAllSolvers(unittest.TestCase):

    solvers = [
        SSACSolver, BasicODESolver, NumPySSASolver, BasicTauLeapingSolver,
        BasicTauHybridSolver, CythonSSASolver
    ]
    model = Example()
    results = {}
    labeled_results = {}

    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)

    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], list))
            self.assertTrue(isinstance(self.labeled_results[solver][0], dict))
            self.assertTrue(
                isinstance(self.labeled_results[solver][0]['Sp'], np.ndarray))
            self.assertTrue(
                isinstance(self.labeled_results[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]))
            diff_results = self.model.run(solver=solver,
                                          show_labels=False,
                                          seed=2)
            if solver.name != 'BasicODESolver':
                self.assertFalse(
                    np.array_equal(diff_results, self.results[solver]))

    def test_extraneous_args(self):
        for solver in self.solvers:
            print(solver.name)
            with self.assertWarns(Warning):
                model = Example()
                results = model.run(solver=solver, nonsense='ABC')
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.assertWarns(Warning):
                            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))
class TestBasicTauLeapingSolver(unittest.TestCase):

    model = Example()
    results = model.run(solver=BasicTauLeapingSolver, show_labels=False)
    labels_results = model.run(solver=BasicTauLeapingSolver, show_labels=True)

    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, list))
        assert (isinstance(self.labels_results[0], dict))
        assert (isinstance(self.labels_results[0]['Sp'], np.ndarray))
        assert (isinstance(self.labels_results[0]['Sp'][0], np.float))
Example #9
0
 def test_run_example(self):
     model = Example()
     results = model.run(solver=BasicTauHybridSolver)
Example #10
0
 def test_run_example(self):
     model = Example()
     results = model.run(solver=BasicTauLeapingSolver)
Example #11
0
 def test_extraneous_args(self):
     for solver in self.solvers:
         print(solver.name)
         with self.assertWarns(Warning):
             model = Example()
             results = model.run(solver=solver, nonsense='ABC')
Example #12
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=BasicODESolver)
     stochml_model.run(solver=NumPySSASolver)
Example #13
0
 def test_run_example(self):
     if can_use_cython:
         model = Example()
         results = model.run(solver=CythonSSASolver)
class TestBasicTauLeapingSolver(unittest.TestCase):
    model = Example()
Example #15
0
 def test_file_with_directory_name_exists(self):
     with self.assertRaises(DirectoryError):
         temp = tempfile.NamedTemporaryFile()
         model = Example()
         solver = SSACSolver(model, temp.name)
Example #16
0
 def test_run_example(self):
     model = Example()
     results = model.run(solver=NumPySSASolver)
Example #17
0
class TestNumPySSASolver(unittest.TestCase):
    model = Example()
Example #18
0
 def test_run_example_precompiled(self):
     model = Example()
     solver = SSACSolver(model)
     results = model.run(solver=solver)
Example #19
0
 def test_create(self):
     model = Example()
     solver = SSACSolver(model)