Exemple #1
0
 def test_iter_includes_all_events_at_tmax(self):
     proc = stocal.Process([
         stocal.Event({}, {'a': 1}, 10, 10),
         stocal.Event({}, {'b': 1}, 0, 10),
     ])
     sampler = self.Sampler(proc, {}, tmax=10)
     for _ in sampler:
         pass
     self.assertEqual(sampler.step, 3)
     self.assertEqual(sampler.time, 10)
     self.assertEqual(sampler.state, {'a': 1, 'b': 2})
Exemple #2
0
 def test_iter_simultaneous_events(self):
     """two Events can occur at the exact same time"""
     proc = stocal.Process([
         stocal.Event({}, {'a': 1}, 10),
         stocal.Event({}, {'b': 1}, 10),
     ])
     sampler = self.Sampler(proc, {})
     for _ in sampler:
         pass
     self.assertEqual(sampler.state, {'a': 1, 'b': 1})
     self.assertEqual(sampler.step, 2)
     self.assertEqual(sampler.time, 10)
Exemple #3
0
 def test_do_not_apply_inapplicable_events(self):
     """assert that Event does not fire if reactants are missing"""
     process = stocal.Process([stocal.Event(['a'], ['b'], 1)])
     traj = self.Sampler(process, {})
     for _ in traj:
         pass
     self.assertEqual(traj.state, stocal.structures.multiset({}))
Exemple #4
0
    def test_fire_inferred_event(self):
        """sampler fires inferred events"""
        class Rule(stocal.ReactionRule):
            Transition = stocal.Event

            def novel_reactions(self, x):
                if x == 'a':
                    yield self.Transition(['a'], [], 10)

        process = stocal.Process(transitions=[stocal.Event([], ['a'], 1)],
                                 rules=[Rule()])

        traj = stocal.algorithms.AndersonNRM(process, {})
        it = iter(traj)

        try:
            trans = next(it)
        except StopIteration:
            self.fail("Static event not fired.")
        self.assertEqual(traj.time, 1)
        self.assertEqual(traj.state, {'a': 1})

        try:
            trans = next(it)
        except StopIteration:
            self.fail("Infered event not fired.")
        self.assertEqual(traj.time, 10)
        self.assertEqual(traj.state, {})
Exemple #5
0
 def test_exact_number_of_events(self):
     """sampler performs specified number of events"""
     proc = stocal.Process([stocal.Event({}, {'a': 1}, 0, 1)])
     sampler = self.Sampler(proc, {}, tmax=10)
     for _ in sampler:
         pass
     self.assertEqual(sampler.step, 11)
     self.assertAlmostEqual(sampler.time, 10)
Exemple #6
0
    def test_text_occurrence_delayed(self):
        """assert that repeated events do not fire before first occurrence

        test closes issue #1
        """
        self.assertEqual(
            stocal.Event(['a'], ['z'], 1, 1).next_occurrence(0), 1)
        self.assertEqual(
            stocal.Event(['a'], ['z'], 1, 1).next_occurrence(0.5), 1)
        self.assertEqual(
            stocal.Event(['a'], ['z'], 1, 1).next_occurrence(1), 1)
        self.assertEqual(
            stocal.Event(['a'], ['z'], 1, 1).next_occurrence(1.5), 2)
        self.assertEqual(
            stocal.Event(['a'], ['z'], 2, 1).next_occurrence(0), 2)
        self.assertEqual(
            stocal.Event(['a'], ['z'], 2, 1).next_occurrence(1), 2)
        self.assertEqual(
            stocal.Event(['a'], ['z'], 2, 1).next_occurrence(1.5), 2)
        self.assertEqual(
            stocal.Event(['a'], ['z'], 2, 1).next_occurrence(2), 2)
        self.assertEqual(
            stocal.Event(['a'], ['z'], 2, 1).next_occurrence(2.5), 3)
Exemple #7
0
 def setUp(self):
     self.process = stocal.Process(transitions=[stocal.Event([], ['a'], 1)],
                                   rules=[self.Rule()])
Exemple #8
0
 def test_text_occurrence_delayed(self):
     """assert that repeated events do not fire before first occurrence"""
     event = stocal.Event(['a'], ['z'], 1, 1)
     self.assertEqual(event.next_occurrence(0), 1)
Exemple #9
0
"""Event example

stocal.Event's can be added to a processes definition just like
Reactions. Process.trajectory returns an StochasticSimulationAlgorithm
that can cope with deterministic transitions (e.g. FirstReactionMethod).
Sampler selection and usage is entirely transparent to the user.
"""
import stocal


process = stocal.Process([
    stocal.MassAction(['A', 'A'], ['A2'], 0.01),
    stocal.MassAction(['A2'], ['A', 'A'], 1.),
    stocal.Event([], ['A'], 0., 1.)
])


if __name__ == '__main__':
    traj = process.sample({}, tmax=100)
    for _ in traj:
        print(traj.time, traj.state['A'], traj.state['A2'])
Exemple #10
0
 def test_trajectory_with_events(self):
     """Partly deterministic processes return an appropriate sampler"""
     proc = self.Process([stocal.Event({}, {'a': 1}, 1.)])
     proc.trajectory({})
Exemple #11
0
 def test_sample_return_type_behavior(self):
     """Partly deterministic processes return an appropriate sampler"""
     proc = self.Process([stocal.Event({}, {'a': 1}, 1.)])
     traj = iter(proc.sample({}))
     self.assertEqual(len(next(traj)), 2)
Exemple #12
0
 def setUp(self):
     self.process = stocal.Process([stocal.Event(['a'], ['b'], 1)])