def test_ifloordiv(self): """test inplace division""" mset_a = multiset({'a': 3, 'z': 7}) mset_b = multiset({'a': 1, 'z': 2}) mset_c = mset_a self.assertEqual(mset_a // 3, mset_b) self.assertIs(mset_a, mset_c)
def test_contains_multiset(self): """test subset of""" mset = multiset({'a': 1, 'z': 2}) self.assertIn(multiset(), mset) self.assertIn(mset, multiset({'a': 1, 'z': 2})) self.assertIn(mset, multiset({'a': 2, 'z': 2})) self.assertIn(mset, multiset({'a': 1, 'x': 1, 'z': 2}))
def test_union(self): """test union""" mset_a = multiset({'a': 1, 'z': 2}) mset_b = multiset({'a': 1}) mset_c = multiset({'x': 2}) mset_d = multiset({'a': 2, 'x': 2, 'z': 2}) self.assertEqual(mset_a.union(mset_b, mset_c), mset_d) self.assertIsNot(mset_a.union(mset_b, mset_c), mset_a)
def test_add(self): """test addition""" mset_a = multiset({'a': 1, 'z': 2}) mset_b = multiset({'a': 1, 'x': 2}) mset_c = multiset({'a': 2, 'x': 2, 'z': 2}) self.assertEqual(mset_a + mset_b, mset_c) self.assertNotEqual(mset_a, mset_c) self.assertNotEqual(mset_b, mset_c)
def test_sub_with_overlap(self): """test difference""" mset_a = multiset({'a': 2, 'x': 2, 'z': 2}) mset_b = multiset({'a': 1, 'b': 4, 'z': 2}) mset_c = multiset({'a': 1, 'x': 2}) self.assertEqual(mset_a - mset_b, mset_c) self.assertNotEqual(mset_a, mset_c) self.assertNotEqual(mset_b, mset_c)
def test_symmetric_difference(self): """test symmetric difference""" mset_a = multiset({'a': 2, 'x': 2, 'z': 2}) mset_b = multiset({'a': 1, 'b': 1}) mset_c = multiset({'x': 2}) mset_d = multiset({'a': 1, 'b': 1, 'z': 2}) self.assertEqual(mset_a.symmetric_difference(mset_b, mset_c), mset_d) self.assertIsNot(mset_a.symmetric_difference(mset_b, mset_c), mset_a)
def test_imul(self): """test inplace multiplication""" mset_a = multiset({'a': 1, 'z': 2}) mset_b = multiset({'a': 3, 'z': 6}) mset_c = mset_a mset_a *= 3 self.assertEqual(mset_a, mset_b) self.assertIs(mset_a, mset_c)
def test_iadd(self): """test inplace addition""" mset_a = multiset({'a': 1, 'z': 2}) mset_b = multiset({'a': 1, 'x': 2}) mset_c = multiset({'a': 2, 'x': 2, 'z': 2}) mset_d = mset_a mset_a += mset_b self.assertEqual(mset_a, mset_c) self.assertIs(mset_a, mset_d)
def test_isub(self): """test inplace difference""" mset_a = multiset({'a': 2, 'x': 2, 'z': 2}) mset_b = multiset({'a': 1, 'x': 2}) mset_c = multiset({'a': 1, 'z': 2}) mset_d = mset_a mset_a -= mset_b self.assertEqual(mset_a, mset_c) self.assertIs(mset_a, mset_d)
def __iter__(self): while True: transitions = multiset() averages = multiset() for n, data in zip(range(self.steps), self.sampler): transitions += data[2] averages += data[1] if not transitions: break yield self.time, 1. / self.steps * averages, transitions
def test_setitem(self): """test setitem""" mset = multiset({'a': 5}) mset['a'] = 1 mset['z'] = 2 self.assertEqual(mset['a'], 1) self.assertEqual(mset['z'], 2)
def __iter__(self): while True: transitions = multiset() for n, data in zip(range(self.steps), self.sampler): transitions += data[2] if not transitions: break yield self.time, self.state, transitions
def __iter__(self): algorithm = self.algorithm time = self.sampler.time transitions = multiset() while True: ptime, trans, args = algorithm.propose_potential_transition( ) # TODO: needs to iterate over self.sampler! if ptime > time + self.dt: time += self.dt yield time, self.state, transitions transitions = multiset() if ptime == float('inf'): break while self.skip and time + self.dt < ptime: time += self.dt transitions[trans] += 1 self.algorithm.perform_transition(ptime, trans, *args)
def every(trajectory, dt): tmax = trajectory.tmax trajectory.tmax = trajectory.time while trajectory.time < tmax: transitions = multiset() if trajectory.steps and trajectory.step >= trajectory.steps: break trajectory.tmax += dt for trans in trajectory: transitions += {trans: 1} yield transitions
def test_ge(self): """test right subset""" mset = multiset({'a': 1, 'z': 2}) self.assertGreaterEqual(mset, multiset({'a': 1, 'z': 2})) self.assertGreaterEqual(mset, multiset({'a': 1, 'z': 1})) self.assertGreaterEqual(mset, multiset({'a': 1})) self.assertFalse(mset >= multiset({'a': 2, 'z': 2})) self.assertGreaterEqual(mset, multiset())
def test_init_assert_number_values(self): """raise ValueError for non-numeric values""" with self.assertRaises(TypeError): multiset({'a': None}) with self.assertRaises(TypeError): multiset({'a': object()}) with self.assertRaises(TypeError): multiset({'a': "string"})
def test_lt(self): """test left true subset""" mset = multiset({'a': 1, 'z': 2}) self.assertFalse(mset < multiset({'a': 1, 'z': 2})) self.assertLess(mset, multiset({'a': 2, 'z': 2})) self.assertLess(mset, multiset({'a': 1, 'x': 1, 'z': 2})) self.assertFalse(mset < multiset({'a': 1, 'z': 1})) self.assertFalse(mset < multiset())
def test_le(self): """test left subset""" mset = multiset({'a': 1, 'z': 2}) self.assertLessEqual(mset, multiset({'a': 1, 'z': 2})) self.assertLessEqual(mset, multiset({'a': 2, 'z': 2})) self.assertLessEqual(mset, multiset({'a': 1, 'x': 1, 'z': 2})) self.assertFalse(mset <= multiset({'a': 1, 'z': 1})) self.assertFalse(mset <= multiset())
def test_gt(self): """test right true subset""" mset = multiset({'a': 1, 'z': 2}) self.assertFalse(mset > multiset({'a': 1, 'z': 2})) self.assertGreater(mset, multiset({'a': 1, 'z': 1})) self.assertGreater(mset, multiset({'a': 1})) self.assertFalse(mset >= multiset({'a': 2, 'z': 2})) self.assertGreater(mset, multiset())
def __iter__(self): algorithm = self.algorithm time = self.sampler.time transitions = multiset() averages = multiset() while True: ptime, trans, args = algorithm.propose_potential_transition( ) # TODO: needs to iterate over self.sampler! if ptime > time + self.dt: time += self.dt yield time, 1. / self.dt * averages, transitions if ptime == float('inf'): break transitions = multiset() averages = multiset() while ptime > time + self.dt: time += self.dt if not self.skip: yield time, self.state, {} transitions[trans] += 1 averages += (ptime - max(time, algorithm.time)) * algorithm.state algorithm.perform_transition(ptime, trans, *args)
def test_init_from_unordered_list_special_case(self): """assert initialization with unordered list ['az']""" mset_a = multiset(['az']) mset_b = multiset({'az': 1}) self.assertEqual(mset_a, mset_b)
def test_init_with_keywords(self): """assert initialization with keywords""" mset_a = multiset({'a': 1, 'z': 2}) mset_b = multiset(a=1, z=2) self.assertEqual(mset_a, mset_b)
def test_init_from_unordered_list(self): """assert initialization with unordered list""" mset_a = multiset(['z', 'a', 'z']) mset_b = multiset({'a': 1, 'z': 2}) self.assertEqual(mset_a, mset_b)
def test_eq(self): """test equality""" mset_a = multiset({'a': 1, 'z': 2}) mset_b = multiset({'a': 1, 'z': 2}) self.assertEqual(mset_a, mset_b)
def test_eq_zero(self): """test equality with and without explicit zero""" mset_a = multiset() mset_b = multiset({'a': 0}) self.assertEqual(mset_a, mset_b)
def test_ne(self): """test inequality""" mset_a = multiset({'a': 1, 'z': 2}) mset_b = multiset({'a': 1, 'z': 1}) self.assertNotEqual(mset_a, mset_b)
def __iter__(self): while not self.has_reached_end(): # step 1: partition reactions -- Eq. (10) Jcrit, Jncr = self.identify_critical_reactions() Irs = set(s for trans in self.transitions for s in trans.reactants) Incr = set(s for trans in Jncr for s in trans.reactants) if Incr: # step 2: determine noncritical tau -- Eqs. (32) and (33) mu = { s: sum( trans.stoichiometry.get(s, 0) * a for trans, a in Jncr.items()) for s in Incr } var = { s: sum( trans.stoichiometry.get(s, 0)**2 * a for trans, a in Jncr.items()) for s in Incr } eps = { s: max(self.epsilon * self.state[s] * self.gi(s), 1.) for s in Incr } tau_ncr1 = min((eps[s] / abs(mu[s])) if mu[s] else float('inf') for s in Incr) tau_ncr2 = min( (eps[s]**2 / abs(var[s])) if var[s] else float('inf') for s in Incr) tau_ncr = min((tau_ncr1, tau_ncr2, self.tmax - self.time)) else: tau_ncr = float('inf') a0 = sum(mult * prop for _, prop, mult in self.propensities.items()) if not a0: break while True: # step 3: abandon tau leaping if not enough expected gain if tau_ncr <= self.tauleap_threshold / a0: if self.abandon_tauleap == -1: self.abandon_tauleap = self.step it = DirectMethod.__iter__(self) for _ in range(self.micro_steps): trans = next(it) self.num_reactions += 1 yield self.time, self.state, {trans: 1} break elif self.abandon_tauleap != -1: logging.debug("Abandoned tau-leaping for %d steps" % (self.step - self.abandon_tauleap)) self.abandon_tauleap = -1 # step 4: determine critical tau ac = sum(propensity for trans, propensity in Jcrit.items()) tau_crit = -log(self.rng.random()) / ac if ac else float('inf') # step 5: determine actual tau tau = min((tau_ncr, tau_crit, self.tmax - self.time)) # step 5a firings = { trans: self.rng2.poisson(propensity * tau) for trans, propensity in Jncr.items() } firings = {trans: n for trans, n in firings.items() if n} # step 5b if tau == tau_crit: # fire exactly one ciritical reaction transition = None pick = self.rng.random() * ac for transition, propensity in Jcrit.items(): pick -= propensity if pick < 0.: break firings[transition] = 1 new_reactions = sum(firings.values()) # avoid overshooting self.steps if self.steps and self.step + new_reactions > self.steps: tau_ncr /= 2 continue all_reactants = sum( (n * trans.true_reactants for trans, n in firings.items()), multiset()) all_products = sum( (n * trans.true_products for trans, n in firings.items()), multiset()) net_reactants = all_reactants - all_products net_products = all_products - all_reactants # step 6a: avoid negative copy numbers if any(self.state[s] < n for s, n in net_reactants.items()): tau_ncr /= 2 continue else: # step 6b: perform transitions self.time += tau self.step += 1 self.num_reactions += new_reactions self.state -= net_reactants for rule in self.process.rules: for trans in rule.infer_transitions( net_products, self.state): trans.rule = rule self.add_transition(trans) self.state += net_products # update propensities affected_species = set().union(*(trans.affected_species for trans in firings)) affected = self.dependency_graph.affected_transitions( affected_species) self.update_propensities(affected) self.prune_transitions() yield self.time, self.state, firings break if self.step != self.steps and self.tmax < float('inf'): self.time = self.tmax
def test_domain(self): """test multiset domain / underlying set""" mset = multiset({'a': 1, 'z': 2}) self.assertEqual(mset.domain, set(['a', 'z']))
def propensity(self, state): state = state if isinstance(state, multiset) else multiset(state) v_max = state[self.enzyme] * self.k_cat return (v_max * state[self.substrate]) / (self.k_m + state[self.substrate])
def test_null(self): """assert empty multiset is False""" self.assertFalse(multiset()) self.assertTrue(multiset({'a': 1}))