예제 #1
0
class DSMTS_004_01(DSMTS_Test):
    species = ['X']
    process = stocal.Process([
        stocal.MassAction([], {'X': 5}, 1.),
        stocal.MassAction(['X'], [], 0.2)
    ])
    initial_state = {}
예제 #2
0
class DSMTS_003_01(DSMTS_Test):
    species = ['P', 'P2']
    process = stocal.Process([
        stocal.MassAction(['P', 'P'], ['P2'], 0.001),
        stocal.MassAction(['P2'], ['P', 'P'], 0.01)
    ])
    initial_state = {'P': 100}
예제 #3
0
 def test_iter_steps(self, steps=50):
     """If steps is given, sampler returns this many transitions"""
     transition = stocal.MassAction({'a': 1}, {}, 1.)
     sampler = self.Sampler(stocal.Process([transition]), {'a': 100},
                            steps=steps)
     self.assertEqual(sum(1 for _ in sampler), steps)
     self.assertEqual(sampler.step, steps)
예제 #4
0
class DSMTS_001_01(DSMTS_Test):
    species = ['X']
    process = stocal.Process([
        stocal.MassAction(['X'], ['X', 'X'], 0.1),
        stocal.MassAction(['X'], [], 0.11)
    ])
    initial_state = {'X': 100}
예제 #5
0
 def test_iter_empty_tmax(self):
     """If tmax is given, sampler.time advances to it"""
     sampler = self.Sampler(stocal.Process([]), {'a': 100}, tmax=100.)
     with self.assertRaises(StopIteration):
         next(iter(sampler))
     self.assertEqual(sampler.step, 0)
     self.assertEqual(sampler.time, 100.)
예제 #6
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, {})
예제 #7
0
 def test_init_optional_args(self):
     """init can be called with optional arguments"""
     proc = stocal.Process([])
     self.assertEqual(self.Sampler(proc, {}).time, 0.)
     self.assertEqual(self.Sampler(proc, {}, t=1.).time, 1.)
     self.assertEqual(self.Sampler(proc, {}, tmax=10.).tmax, 10.)
     self.assertEqual(self.Sampler(proc, {}, steps=10).steps, 10)
예제 #8
0
 def test_perform_transition_changes_state(self):
     transition = stocal.MassAction([], ['a'], 1.)
     process = stocal.Process([transition])
     sampler = self.Sampler(process, {}, tmax=100.)
     time, trans, args = sampler.propose_potential_transition()
     sampler.perform_transition(time, trans, *args)
     self.assertEqual(sampler.state, {'a': 1})
예제 #9
0
 def test_iter_empty(self):
     """The empty process stops iteration immediately"""
     sampler = self.Sampler(stocal.Process([]), {'a': 100})
     with self.assertRaises(StopIteration):
         next(iter(sampler))
     self.assertEqual(sampler.step, 0)
     self.assertEqual(sampler.time, 0.)
예제 #10
0
 def test_propose_potential_transition_in_finite_time(self):
     """Proposed (time,transition) for empty process is (inf,None)"""
     process = stocal.Process([stocal.MassAction([], ['a'], 1.)])
     sampler = self.Sampler(process, {}, tmax=100.)
     time, transition, args = sampler.propose_potential_transition()
     self.assertTrue(time < float('inf'))
     self.assertNotEqual(transition, None)
예제 #11
0
 def test_perform_transition_advances_time(self):
     transition = stocal.MassAction([], ['a'], 1.)
     process = stocal.Process([transition])
     sampler = self.Sampler(process, {}, tmax=100.)
     time, trans, args = sampler.propose_potential_transition()
     sampler.perform_transition(time, trans, *args)
     self.assertGreater(sampler.time, 0.)
예제 #12
0
 def test_propose_potential_transition_seed(self):
     """Samplers initialized with the same random seed propose equal transitions"""
     process = stocal.Process([stocal.MassAction([], ['a'], 1.)])
     sampler_a = self.Sampler(process, {'a': 100}, seed=10)
     sampler_b = self.Sampler(process, sampler_a.state, seed=10)
     self.assertEqual(sampler_a.propose_potential_transition(),
                      sampler_b.propose_potential_transition())
예제 #13
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({}))
예제 #14
0
 def test_update_state_disables_static(self):
     """update_state can disable static transitions"""
     transition = stocal.MassAction({'a': 1}, {}, 1.)
     sampler = self.Sampler(stocal.Process([transition]), {'a': 1})
     sampler.update_state({'a': 0})
     with self.assertRaises(StopIteration):
         next(iter(sampler))
예제 #15
0
class DSMTS_002_03(DSMTS_002_01):
    # The original test tests for the overloading of global parameters
    # by local parameters. stocal does not have these concepts.
    # We merely check whether the process produces reported results
    # for an immigration rate of 5.
    process = stocal.Process(
        [stocal.MassAction([], ['X'], 5.),
         stocal.MassAction(['X'], [], 0.1)])
예제 #16
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)
예제 #17
0
 def test_transitions_counts_mutliplicities(self):
     """Sampler.transitions should give access to all transitions."""
     proc = stocal.Process()
     sampler = self.Sampler(proc, {})
     sampler.add_transition(stocal.MassAction({}, {'a': 1}, 1.))
     sampler.add_transition(stocal.MassAction({}, {'a': 1}, 1.))
     sampler.add_transition(stocal.MassAction({}, {'b': 1}, 1.))
     self.assertEqual(len(sampler.transitions), 3)
예제 #18
0
 def test_iter_steps_and_tmax(self):
     """if steps exceed, time should not be advanced to tmax"""
     transition = stocal.MassAction({'a': 1}, {}, 1.)
     proc = stocal.Process([transition])
     sampler = self.Sampler(proc, {'a': 100}, steps=0, tmax=10.)
     with self.assertRaises(StopIteration):
         next(iter(sampler))
     self.assertEqual(sampler.step, 0)
     self.assertEqual(sampler.time, 0.)
예제 #19
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})
예제 #20
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)
예제 #21
0
    def test_update_state_enables_infered(self):
        """update_state can enable infered transitions"""
        class Rule(stocal.Rule):
            """Rule that infers one reaction for any species"""
            Transition = stocal.MassAction

            def infer_transitions(self, new_species, state):
                for species in new_species:
                    yield self.Transition({species: 1}, {}, 1.)

        sampler = self.Sampler(stocal.Process([], [Rule()]), {})
        sampler.update_state({'a': 1})
        self.assertTrue(isinstance(next(iter(sampler)), Rule.Transition))
예제 #22
0
    def test_update_state_disables_infered(self):
        """update_state can disable infered transitions"""
        class Rule(stocal.Rule):
            """Rule that infers one reaction for any species"""
            Transition = stocal.MassAction

            def infer_transitions(self, new_species, state):
                for species in new_species:
                    yield self.Transition({species: 1}, {}, 1.)

        sampler = self.Sampler(stocal.Process([], [Rule()]), {'a': 1})
        sampler.update_state({'a': 0})
        with self.assertRaises(StopIteration):
            next(iter(sampler))
예제 #23
0
    def test_init_bounds(self):
        """accepted parameters are within range

        state must have only positive copy numbers
        t must be non-negative
        tmax must be non-negative
        steps must non-negative
        """
        proc = stocal.Process([])
        with self.assertRaises(ValueError):
            self.Sampler(proc, {}, t=-1.)
        with self.assertRaises(ValueError):
            self.Sampler(proc, {}, tmax=-1.)
        with self.assertRaises(ValueError):
            self.Sampler(proc, {}, steps=-1)
        with self.assertRaises(ValueError):
            self.Sampler(proc, {'a': 0})
        with self.assertRaises(ValueError):
            self.Sampler(proc, {'a': -1})
예제 #24
0
    KL = BA
    LK = BA


class BA_BB(Polymerization):
    signature = [BA, BB]
    KL = BB


# The initial condition is just a bunch of monomers of types AA and BB

state = {
    AA('a'): 100,
    AA('b'): 100,
    AA('c'): 100,
    BB('x'): 100,
    BB('y'): 100,
    BB('z'): 100,
}

# The process is defined to use all subclasses of Polymerization

process = stocal.Process(rules=[R() for R in Polymerization.__subclasses__()])

# And we go in to sample the trajectory of the process...

if __name__ == '__main__':
    traj = process.sample(state, steps=1000)
    for trans in traj:
        print(traj.time, traj.state)
예제 #25
0
 def test_update_state_enables_static(self):
     """update_state can enable static transitions"""
     transition = stocal.MassAction({'a': 1}, {}, 1.)
     sampler = self.Sampler(stocal.Process([transition]), {})
     sampler.update_state({'a': 1})
     self.assertIs(next(iter(sampler)), transition)
예제 #26
0
파일: bugs.py 프로젝트: MrLiono21/stocal
 def setUp(self):
     self.process = stocal.Process(transitions=[stocal.Event([], ['a'], 1)],
                                   rules=[self.Rule()])
예제 #27
0
 def test_add_transition_enables_transition(self):
     """transitions added with add_transition must be executable"""
     sampler = self.Sampler(stocal.Process([]), {'a': 1})
     transition = stocal.MassAction({'a': 1}, {}, 1.)
     sampler.add_transition(transition)
     self.assertIs(next(iter(sampler)), transition)
예제 #28
0
    def novel_reactions(self, k, l):
        yield self.Transition([k, l], [k+l], alpha)
        yield self.Transition([k, l], [l+k], alpha)


class AutoCatalysisRule(stocal.ReactionRule):
    """Replicate any string from two matching substrings"""
    Transition = stocal.MassAction

    def __str__(self):
        return 'k+l+kl --> 2*kl'

    def novel_reactions(self, k, l, m):
        k, l, m = sorted([k, l, m], key=len)
        if k+l == m:
            yield self.Transition([k, l, m], {m: 2}, beta)
        if l+k == m:
            yield self.Transition([k, l, m], {m: 2}, beta)


process = stocal.Process(
    rules=[DegradationRule(), LigationRule(), AutoCatalysisRule()]
)


if __name__ == '__main__':
    traj = process.trajectory(initial_state, tmax=100.)
    for _ in traj:
        print(traj.time, traj.state)
예제 #29
0
 def test_propose_potential_transition_empty(self):
     """Proposed (time,transition) for empty process is (inf,None)"""
     sampler = self.Sampler(stocal.Process([]), {'a': 100}, tmax=100.)
     time, transition, args = sampler.propose_potential_transition()
     self.assertEqual(time, float('inf'))
     self.assertEqual(transition, None)
예제 #30
0
파일: events.py 프로젝트: nimpod/stocal
"""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'])