Esempio n. 1
0
 def test_status_3(self, algorithm, setup_basic):
     species_names, rxn_names, V_r, V_p, X0, k = setup_basic
     V_p = np.array([[0, 0], [0, 0], [0, 1]])
     X0 = np.array([10, 0, 0], dtype=np.int64)
     sim = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     sim.simulate(algorithm=algorithm, max_t=10, max_iter=100, chem_flag=True)
     assert sim.results.status_list[0] == 3
Esempio n. 2
0
 def test_monotonic(self, algorithm, setup_basic):
     species_names, rxn_names, V_r, V_p, X0, k = setup_basic
     sim1 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     sim1.simulate(algorithm=algorithm)
     results = sim1.results
     for t_array in results.t_list:
         assert (np.diff(t_array) > 0).all()
Esempio n. 3
0
 def test_sim(self, setup_basic):
     species_names, rxn_names, V_r, V_p, X0, k = setup_basic
     sim = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     sim.simulate(algorithm="direct",
                  max_t=1e5,
                  max_iter=int(1e8),
                  chem_flag=False)
Esempio n. 4
0
 def test_long(self, algorithm, setup_long):
     species_names, rxn_names, V_r, V_p, X0, k = setup_long
     sim1 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     sim1.simulate(
         algorithm=algorithm, max_t=1e5, max_iter=int(1e8), chem_flag=False
     )
     X_output = np.array([0, 0, X0.sum()])
     assert (sim1.results.final[-1] == X_output).all()
Esempio n. 5
0
 def test_X0_Vr_shape(self, setup_basic):
     species_names, rxn_names, V_r, V_p, X0, k = setup_basic
     X0 = np.array([100, 0, 0, 0])
     with pytest.raises(ValueError):
         Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     X0 = np.array([100, 0])
     with pytest.raises(ValueError):
         Simulation(species_names, rxn_names, V_r, V_p, X0, k)
Esempio n. 6
0
 def test_init_good(self, algorithm, setup_large):
     """
         Test if initialization works.
     """
     species_names, rxn_names, V_r, V_p, X0, k = setup_large
     sim = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     with pytest.warns(Warning):
         sim.results
     sim.simulate(algorithm=algorithm)
     assert sim.results
Esempio n. 7
0
 def test_X0_2d(self, setup_basic):
     species_names, rxn_names, V_r, V_p, X0, k = setup_basic
     X0 = np.array([[100, 0, 0]])
     print(X0.shape)
     with pytest.raises(ValueError):
         Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     X0 = np.array([[100], [0], [0]])
     print(X0.shape)
     with pytest.raises(ValueError):
         Simulation(species_names, rxn_names, V_r, V_p, X0, k)
Esempio n. 8
0
def test_plotting(setup_basic):
    """ Test if plot generates without errors """
    species_name, rxn_names, V_r, V_p, X0, k = setup_basic
    n_runs = 10
    sim1 = Simulation(species_name, rxn_names, V_r, V_p, X0, k)
    sim1.simulate(
        algorithm="direct", max_t=150, max_iter=1000, chem_flag=True, n_rep=n_runs
    )
    sim1.plot(species_names=["A"])
    sim1.plot(species_names=["A", "B"], new_names=["Species A", "Secies B"])
    sim1.plot()
Esempio n. 9
0
 def test_iter_len(self, algorithm, setup_large):
     """
         Test the ``__len__`` method and if shape of x and t match.
     """
     species_names, rxn_names, V_r, V_p, X0, k = setup_large
     n_rep = 10
     sim = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     sim.simulate(algorithm=algorithm, n_rep=n_rep)
     results = sim.results
     assert len(results) == n_rep
     for x, t, s in results:
         assert x.shape[0] == t.shape[0]
         assert s
Esempio n. 10
0
 def test_contains_getitem(self, algorithm, setup_large):
     """
         Test the ``__getitem__`` method.
     """
     species_names, rxn_names, V_r, V_p, X0, k = setup_large
     n_rep = 10
     sim = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     sim.simulate(algorithm=algorithm, n_rep=n_rep)
     results = sim.results
     assert 9 in results
     x, t, s = results[9]
     assert x.shape[0] == t.shape[0]
     assert s
     with pytest.raises(IndexError):
         results[100]
Esempio n. 11
0
 def create_sim_inst(react_stoic):
     prod_stoic = np.random.randint(0, 5, react_stoic.shape)
     k = np.random.rand(react_stoic.shape[1])
     X = np.random.randint(0, 5, react_stoic.shape[0], dtype=np.int64)
     species_names = ["X"] * react_stoic.shape[0]
     rxn_names = ["R"] * react_stoic.shape[1]
     sim = Simulation(species_names, rxn_names, react_stoic, prod_stoic, X, k)
     return sim
Esempio n. 12
0
    def test_get_states(self, algorithm, setup_large):
        """
            Test the ``get_state`` method.
        """
        species_names, rxn_names, V_r, V_p, X0, k = setup_large
        n_rep = 3
        max_t = 1e5
        max_iter = 100
        sim = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
        sim.simulate(algorithm=algorithm,
                     max_t=max_t,
                     max_iter=max_iter,
                     n_rep=n_rep)
        results = sim.results
        assert np.isclose(results.get_state(0.0),
                          np.array(X0, dtype=np.float)).all()

        x_list = [
            np.array([0, 1, 2, 3]).reshape(-1, 1),
            np.array([0, 2, 3, 4]).reshape(-1, 1),
            np.array([0, 0, 1, 2]).reshape(-1, 1),
        ]
        t_list = [
            np.array([0, 1, 2, 3]),
            np.array([0, 1, 2, 3]),
            np.array([0, 1, 2, 3]),
        ]
        status_list = [1, 1, 1]
        seed = [0, 1, 2]
        res = Results(["A"], ["r1"], t_list, x_list, status_list, algorithm,
                      seed)
        zero_array = np.array([0])
        one_array = np.array([1])
        two_array = np.array([2])
        three_array = np.array([3])
        four_array = np.array([4])
        assert np.isclose(res.get_state(0),
                          [zero_array, zero_array, zero_array]).all()
        assert np.isclose(res.get_state(1),
                          [one_array, two_array, zero_array]).all()
        assert np.isclose(res.get_state(1 + np.finfo(float).eps),
                          [one_array, two_array, zero_array]).all()
        assert np.isclose(res.get_state(3),
                          [three_array, four_array, two_array]).all()
Esempio n. 13
0
 def test_final(self, algorithm, setup_large):
     """
         Test the ``final`` property.
     """
     species_names, rxn_names, V_r, V_p, X0, k = setup_large
     n_rep = 3
     max_t = 1e5
     max_iter = 100
     sim = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     sim.simulate(algorithm=algorithm,
                  max_t=max_t,
                  max_iter=max_iter,
                  n_rep=n_rep)
     results = sim.results
     final_times, final_states = results.final
     assert final_times.shape[0] == final_states.shape[0]
     if algorithm != "tau_leaping" and algorithm != "tau_leaping":
         for i in range(final_states.shape[0]):
             assert (final_states[0, :] == final_states[i, :]).all()
Esempio n. 14
0
 def test_init_bad(self, algorithm, setup_large):
     """
         Test if initialization fails when it is supposed to.
     """
     species_names, rxn_names, V_r, V_p, X0, k = setup_large
     sim = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     sim.simulate(algorithm=algorithm)
     results = sim.results
     t_list = results.t_list
     x_list = results.x_list
     status_list = results.status_list
     algorithm = "direct"
     seed = [0] * len(status_list)
     assert Results(species_names, rxn_names, t_list, x_list, status_list,
                    algorithm, seed)
     with pytest.raises(ValueError):
         Results(
             species_names,
             rxn_names,
             t_list,
             x_list,
             status_list,
             algorithm,
             seed[:-2],
         )
     with pytest.raises(ValueError):
         Results(
             species_names,
             rxn_names,
             t_list[:-2],
             x_list,
             status_list,
             algorithm,
             seed,
         )
     with pytest.raises(ValueError):
         t_list[0] = t_list[0][:-2]
         Results(species_names, rxn_names, t_list, x_list, status_list,
                 algorithm, seed)
     with pytest.raises(ValueError):
         status_list[-1] = "fail"
         Results(species_names, rxn_names, t_list, x_list, status_list,
                 algorithm, seed)
Esempio n. 15
0
 def test_get_states_high_time(self, algorithm, setup_00003):
     """
         Test the ``get_state`` method at longer model times.
     """
     (
         species_names,
         rxn_names,
         V_r,
         V_p,
         X0,
         k,
         _,
         _,
         _,
         max_t,
         max_iter,
         n_rep,
     ) = setup_00003
     sim = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     sim.simulate(algorithm=algorithm,
                  max_t=10000,
                  max_iter=max_iter,
                  n_rep=1)
     print("status", sim.results.status_list[0], sim.results.t_list,
           sim.results.x_list)
     states = sim.results.get_state(t=15000)
     _, x_final = sim.results.final
     assert states[0] == x_final[0]
     sim.simulate(algorithm=algorithm, max_t=2, max_iter=max_iter, n_rep=1)
     with pytest.warns(UserWarning):
         sim.results.get_state(t=1_000_000)
Esempio n. 16
0
 def test_reproduce_fail(self, algorithm, setup_basic):
     species_names, rxn_names, V_r, V_p, X0, k = setup_basic
     sim1 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     sim2 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     sim1.simulate(algorithm=algorithm)
     sim2.simulate(algorithm=algorithm, seed=1)
     for i, j in zip(sim1.results.t_list, sim2.results.t_list):
         if i.shape[0] == j.shape[0]:
             assert not (
                 all(
                     (i == j).all()
                     for i, j in zip(sim1.results.t_list, sim2.results.t_list)
                 )
             )
Esempio n. 17
0
 def test_incorrect_seed(self, algorithm, setup_basic):
     species_names, rxn_names, V_r, V_p, X0, k = setup_basic
     sim1 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     with pytest.raises(TypeError):
         sim1.simulate(algorithm=algorithm, n_rep=2, seed=[1])
     with pytest.raises(TypeError):
         sim1.simulate(algorithm=algorithm, n_rep=2, seed=[1, 2, 3])
Esempio n. 18
0
 def test_reproduce(self, algorithm, setup_basic):
     species_names, rxn_names, V_r, V_p, X0, k = setup_basic
     sim1 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     sim2 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     sim1.simulate(algorithm=algorithm)
     sim2.simulate(algorithm=algorithm)
     assert all(
         (i == j).all() for i, j in zip(sim1.results.t_list, sim2.results.t_list)
     )
     assert all(
         (i == j).all() for i, j in zip(sim1.results.x_list, sim2.results.x_list)
     )
     assert all(
         i == j for i, j in zip(sim1.results.status_list, sim2.results.status_list)
     )
Esempio n. 19
0
 def test_Vr_neg(self, setup_basic):
     species_names, rxn_names, V_r, V_p, X0, k = setup_basic
     V_r = np.array([[-1, 0], [0, 1], [0, 0]])
     with pytest.raises(ValueError):
         Simulation(species_names, rxn_names, V_r, V_p, X0, k)
Esempio n. 20
0
 def test_too_high_order(self, setup_basic):
     species_names, rxn_names, V_r, V_p, X0, k = setup_basic
     V_r = np.array([[2, 0], [2, 1], [0, 0]])
     with pytest.raises(ValueError):
         Simulation(species_names, rxn_names, V_r, V_p, X0, k)
Esempio n. 21
0
 def test_maxiter_type(self, algorithm, setup_basic):
     species_names, rxn_names, V_r, V_p, X0, k = setup_basic
     sim1 = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     with pytest.raises(TypeError):
         sim1.simulate(algorithm=algorithm, max_iter=100.0)
Esempio n. 22
0
 def test_correct_model_file(self):
     sim = Simulation.load_model("tests/models/00001.txt", "ModelFile")
     sim.simulate()
     assert sim.results.status_list == [2]
Esempio n. 23
0
 def test_correct_model_str(self, setup_00001_correct):
     sim = Simulation.load_model(setup_00001_correct, "ModelString")
     sim.simulate()
     assert sim.results.status_list == [2]
     sim.simulate(max_t=20.0, max_iter=5)
     assert sim.results.status_list == [1]
Esempio n. 24
0
 def test_neg_k(self, setup_large):
     species_names, rxn_names, V_r, V_p, X0, k = setup_large
     k = np.array([1, 1, -1, 1, -1])
     with pytest.raises(ValueError):
         Simulation(species_names, rxn_names, V_r, V_p, X0, k)
Esempio n. 25
0
 def test_Vp_Vr_shape(self, setup_basic):
     species_names, rxn_names, V_r, V_p, X0, k = setup_basic
     V_p = np.array([[0], [1], [0]])
     with pytest.raises(ValueError):
         Simulation(species_names, rxn_names, V_r, V_p, X0, k)
Esempio n. 26
0
 def test_null(self, algorithm, setup_basic):
     species_names, rxn_names, V_r, V_p, X0, k = setup_basic
     k = np.array([0.0, 0.0])
     sim = Simulation(species_names, rxn_names, V_r, V_p, X0, k)
     sim.simulate(algorithm=algorithm)
     assert sim.results.status_list[0] == -2
Esempio n. 27
0
 def test_kdet_Vr_shape(self, setup_basic):
     species_names, rxn_names, V_r, V_p, X0, k = setup_basic
     k = np.array([1, 1, 1])
     with pytest.raises(ValueError):
         Simulation(species_names, rxn_names, V_r, V_p, X0, k)