def test_int_type_mismatch(self): model = Model() y1 = np.int64(5) y2 = np.int32(5) species1 = Species('A', initial_value=y1) species2 = Species('B', initial_value=y2) model.add_species([species1, species2])
def __init__(self, parameter_values=None): Model.__init__(self, name="Simple_Hybrid_Model") # Species A = Species(name='A', initial_value=0) B = Species(name='B', initial_value=0) self.add_species([A, B]) # Parameters k1 = Parameter(name='k1', expression=1) k2 = Parameter(name='k2', expression=10) self.add_parameter([k1, k2]) # Rate Rule rate_rule = RateRule(B, "cos(t)") self.add_rate_rule(rate_rule) # Reactions r1 = Reaction(name='r1', reactants={A: 1}, products={}, propensity_function="k1*B") r2 = Reaction(name='r2', reactants={}, products={B: 1}, rate=k2) self.add_reaction([r1, r2]) self.timespan(numpy.linspace(0, 1, 11))
def test_model_hash_whitespace_accuracy(self): """ Test that differences in whitespace do not change the hash of a model. """ model_no_whitespace = create_michaelis_menten() model_with_whitespace = create_michaelis_menten() X = Species(name="X", initial_value=int(0.65609071 * 300.0)) Y = Species(name="Y", initial_value=int(0.85088331 * 300.0)) model_no_whitespace.add_species([X, Y]) model_with_whitespace.add_species([X, Y]) # Up to this point the JSON hash of the two models should be the same. self.assertEquals(model_no_whitespace.get_json_hash(), model_with_whitespace.get_json_hash()) # Add a custom reaction to both models, differing the amount of whitespace in the # propensity functions. reaction_no_whitespace = Reaction( name="X production", reactants={}, products={X: 1}, propensity_function="300*1.0/(1.0+(Y*Y/(300*300)))") reaction_with_whitespace = Reaction( name="X production", reactants={}, products={X: 1}, propensity_function= "300 * 1.0 / (1.0 + (Y *Y / (300 * 300)))") model_no_whitespace.add_reaction(reaction_no_whitespace) model_with_whitespace.add_reaction(reaction_with_whitespace)
def __init__(self, parameter_values=None): # initialize Model Model.__init__(self, name="Schlogl") # Species s1 = Species(name='A', initial_value=300) s2 = Species(name='B', initial_value=300) s3 = Species(name='C', initial_value=300) s4 = Species(name='X', initial_value=300) self.add_species([s1, s2, s3, s4]) k1 = Parameter(name='k1', expression=1) k2 = Parameter(name='k2', expression=1) self.add_parameter([k1, k2]) j1 = Reaction(name="j1", reactants={ s1: 1, s4: 1 }, products={s4: 2.0}, rate=k1) j2 = Reaction(name="j2", reactants={ s2: 1, s4: 1 }, products={s3: 1}, rate=k2) self.add_reaction([j1, j2]) self.timespan(np.linspace(0, 100000, 100))
def __init__(self, parameter_values=None): # Initialize the model. Model.__init__(self, name="Toggle_Switch") # Species A = Species(name='A', initial_value=10.0) B = Species(name='B', initial_value=10.0) self.add_species([A, B]) # Parameters alpha1 = Parameter(name='alpha1', expression=10.0) alpha2 = Parameter(name='alpha2', expression=10.0) beta = Parameter(name='beta', expression=2.0) gamma = Parameter(name='gamma', expression=2.0) mu = Parameter(name='mu', expression=1.0) self.add_parameter([alpha1, alpha2, beta, gamma, mu]) # Reactions cu = Reaction(name="r1", reactants={}, products={A: 1}, rate=alpha1.value * (1 + B.initial_value**float(beta.expression))) cv = Reaction( name="r2", reactants={}, products={B: 1}, rate=alpha2.value(1 + A.initial_value**float(gamma.expression))) du = Reaction(name="r3", reactants={A: 1}, products={}, rate=mu) dv = Reaction(name="r4", reactants={B: 1}, products={}, rate=mu) self.add_reaction([cu, cv, du, dv]) self.timespan(np.linspace(0, 250, 251))
def __init__(self, parameter_values=None): # initialize Model Model.__init__(self, name="Michaelis_Menten") # parameters rate1 = Parameter(name='rate1', expression=0.0017) rate2 = Parameter(name='rate2', expression=0.5) rate3 = Parameter(name='rate3', expression=0.1) self.add_parameter([rate1, rate2, rate3]) # Species A = Species(name='A', initial_value=301) B = Species(name='B', initial_value=120) C = Species(name='C', initial_value=0) D = Species(name='D', initial_value=0) self.add_species([A, B, C, D]) # reactions r1 = Reaction(name="r1", reactants={A: 1, B: 1}, products={C: 1}, rate=rate1) r2 = Reaction(name="r2", reactants={C: 1}, products={A: 1, B: 1}, rate=rate2) r3 = Reaction(name="r3", reactants={C: 1}, products={B: 1, D: 1}, rate=rate3) self.add_reaction([r1, r2, r3]) self.timespan(np.linspace(0, 100, 101))
def test_species_parameter_name_substrings(self): model = Model() rate = Parameter(name='rate', expression=1) model.add_parameter(rate) species1 = Species('A', initial_value=100) species2 = Species('AA', initial_value=0) model.add_species([species1, species2]) reaction1 = Reaction(name="reaction1", reactants={species1: 1}, products={species2: 1}, rate=rate) model.add_reaction(reaction1) number_points = 11 model.timespan(np.linspace(0, 1, number_points)) from gillespy2.solvers.numpy.ssa_solver import NumPySSASolver results = model.run(number_of_trajectories=1, solver=NumPySSASolver, seed=1) self.assertTrue(len(results['time']) == number_points) self.assertTrue(len(results[species1.name]) == number_points) self.assertTrue(len(results[species2.name]) == number_points) self.assertGreater(results[species1.name][0], results[species1.name][-1]) self.assertLess(results[species2.name][0], results[species2.name][-1]) self.assertEqual( np.sum(results[species1.name]) + np.sum(results[species2.name]), number_points * species1.initial_value)
def test_ode_propensity(self): model = Model() rate = Parameter(name='rate', expression=0.5) model.add_parameter(rate) species1 = Species('A', initial_value=10) species2 = Species('B', initial_value=10) model.add_species([species1, species2]) r1 = Reaction(name='r1', reactants={'A': 1}, products={}, rate=rate) r2 = Reaction(name='r2', reactants={'A': 2}, products={'B': 1}, rate=rate) r3 = Reaction(name='r3', reactants={ 'A': 1, 'B': 1 }, products={}, rate=rate) r4 = Reaction(name='r4', reactants={'A': 1}, products={}, propensity_function='t') model.add_reaction([r1, r2, r3, r4]) self.assertEqual(model.listOfReactions['r1'].ode_propensity_function, 'rate*A') self.assertEqual(model.listOfReactions['r2'].ode_propensity_function, 'rate*A*A') self.assertEqual(model.listOfReactions['r3'].ode_propensity_function, 'rate*A*B') self.assertEqual(model.listOfReactions['r4'].ode_propensity_function, 't')
def __init__(self, parameter_values=None): # First call the gillespy2.Model initializer. Model.__init__(self, name="Dimerization") # Define parameters for the rates of creation and dissociation. k_c = Parameter(name='k_c', expression=0.005) k_d = Parameter(name='k_d', expression=0.08) self.add_parameter([k_c, k_d]) # Define variables for the molecular species representing M and D. m = Species(name='monomer', initial_value=30) d = Species(name='dimer', initial_value=0) self.add_species([m, d]) # Define the reactions representing the process. In GillesPy2, # the list of reactants and products for a Reaction object are each a # Python dictionary in which the dictionary keys are Species objects # and the values are stoichiometries of the species in the reaction. r_creation = Reaction(name="r_creation", rate=k_c, reactants={m: 2}, products={d: 1}) r_dissociation = Reaction(name="r_dissociation", rate=k_d, reactants={d: 1}, products={m: 2}) self.add_reaction([r_creation, r_dissociation]) # Set the timespan for the simulation. self.timespan(np.linspace(0, 100, 101))
def __init__(self, parameter_values=None, init_v=1): # initialize Model Model.__init__(self, name="Simple_Hybrid_Model") # Species A = Species(name='A', initial_value=0) V = Species(name='V', initial_value=init_v) self.add_species([A, V]) # parameters rate1 = Parameter(name='rate1', expression=20.0) rate2 = Parameter(name='rate2', expression=10.0) rate_rule1 = RateRule(V, "cos(t)") self.add_parameter([rate1, rate2]) self.add_rate_rule(rate_rule1) # reactions r1 = Reaction(name="r1", reactants={}, products={A: 1}, propensity_function="rate1 * V") r2 = Reaction(name="r2", reactants={A: 1}, products={}, rate=rate2) self.add_reaction([r1, r2]) self.timespan(np.linspace(0, 100, 1001))
def test_duplicate_species_names(self): model = Model() species1 = Species('A', initial_value=0) species2 = Species('A', initial_value=0) model.add_species(species1) with self.assertRaises(ModelError): model.add_species(species2)
def create_simple_hybrid_model(parameter_values=None): model = Model(name="Simple_Hybrid_Model") # Species A = Species(name='A', initial_value=0) B = Species(name='B', initial_value=0) model.add_species([A, B]) # Parameters k1 = Parameter(name='k1', expression=1) k2 = Parameter(name='k2', expression=10) model.add_parameter([k1, k2]) # Rate Rule rate_rule = RateRule(name='Brate', variable='B', formula="cos(t)") model.add_rate_rule(rate_rule) # Reactions r1 = Reaction(name='r1', reactants={A: 1}, products={}, propensity_function="k1*B") r2 = Reaction(name='r2', reactants={}, products={B: 1}, rate=k2) model.add_reaction([r1, r2]) model.timespan(numpy.linspace(0, 1, 11)) return model
def test_addingMultipleSameSpecies_ThrowsError(self): A = Species(name='A', initial_value=0) B = Species(name='B', initial_value=0) with self.assertRaises(ModelError) as ex: self.model.add_species([A, B]) self.assertIn( 'Name "{}" is unavailable. A species with that name exists.'. format(A.name), str(ex.exception))
def test_species_setter(self): sp1 = Species('A', initial_value=10) sp1.set_initial_value(5) self.assertEqual(sp1.initial_value, 5) with self.assertRaises(SpeciesError): sp1.set_initial_value(-1) sp2 = Species('B', initial_value=5, mode='discrete') with self.assertRaises(SpeciesError): sp2.set_initial_value(.5)
def __init__(self, parameter_values=None): Model.__init__(self, name="tyson-2-state", volume=300.0) # Species X = Species(name='X', initial_value=int(0.65609071 * 300.0)) Y = Species(name='Y', initial_value=int(0.85088331 * 300.0)) self.add_species([X, Y]) P = Parameter(name='p', expression=2.0) kt = Parameter(name='kt', expression=20.0) kd = Parameter(name='kd', expression=1.0) a0 = Parameter(name='a0', expression=0.005) a1 = Parameter(name='a1', expression=0.05) a2 = Parameter(name='a2', expression=0.1) kdx = Parameter(name='kdx', expression=1.0) self.add_parameter([P, kt, kd, a0, a1, a2, kdx]) # creation of X: rxn1 = Reaction( name='X production', reactants={}, products={X: 1}, propensity_function='300 * 1.0 / (1.0 + (Y * Y / (300 * 300)))') # degradadation of X: rxn2 = Reaction(name='X degradation', reactants={X: 1}, products={}, rate=kdx) # creation of Y: rxn3 = Reaction(name='Y production', reactants={X: 1}, products={ X: 1, Y: 1 }, rate=kt) # degradation of Y: rxn4 = Reaction(name='Y degradation', reactants={Y: 1}, products={}, rate=kd) # nonlinear Y term: rxn5 = Reaction(name='Y nonlin', reactants={Y: 1}, products={}, propensity_function= 'Y / a0 + a1 * (Y / 300) + a2 * Y * Y / (300 * 300)') self.add_reaction([rxn1, rxn2, rxn3, rxn4, rxn5]) self.timespan(np.linspace(0, 100, 101))
def test_add_event(self): from gillespy2.core.events import Event, EventTrigger, EventAssignment model = Model() model.add_species(Species(name="A", initial_value=1, mode="discrete")) model.add_species(Species(name="B", initial_value=2, mode="discrete")) e1t = EventTrigger(expression="t>1") e1a1 = EventAssignment(variable="A", expression="3") e1a2 = EventAssignment(variable="B", expression="4") test_event = Event(name="e1", trigger=e1t, assignments=[e1a1, e1a2]) model.add_event(test_event) passed_test = str(model)
def test_reaction_valid_reactant(self): model = Model() rate = Parameter(name='rate', expression=0.5) model.add_parameter(rate) species1 = Species('A', initial_value=0) species2 = Species('B', initial_value=0) model.add_species([species1, species2]) reaction1 = Reaction(name="reaction1", reactants={'A': 1}, products={species2: 1}, rate=rate) model.add_reaction(reaction1) assert "reaction1" in model.listOfReactions
def test_reaction_invalid_product(self): model = Model() rate = Parameter(name='rate', expression=0.5) model.add_parameter(rate) species1 = Species('A', initial_value=0) species2 = Species('B', initial_value=0) model.add_species([species1, species2]) reaction1 = Reaction(name="reaction1", reactants={species1: 1}, products={'species2': 1}, rate=rate) with self.assertRaises(ModelError): model.add_reaction(reaction1)
def test_no_reaction_name(self): model = Model() rate = Parameter(name='rate', expression=0.5) model.add_parameter(rate) species1 = Species('A', initial_value=0) species2 = Species('B', initial_value=0) model.add_species([species1, species2]) # add two reactions that has no name reaction1 = Reaction(reactants={species1: 1}, products={species2: 1}, rate=rate) reaction2 = Reaction(reactants={species2: 1}, products={species1: 1}, rate=rate) model.add_reaction([reaction1, reaction2])
def test_add_single_species(self): C = Species(name='C', initial_value=0) self.model.add_species(C) species = self.model.get_species('C') self.assertIsInstance(species, Species, msg='{0} has incorrect type'.format(species))
def test_add_reaction_dict(self): model = Model() rate = Parameter(name='rate', expression=0.5) model.add_parameter(rate) species1 = Species('A', initial_value=0) species2 = Species('B', initial_value=0) model.add_species([species1, species2]) reactions = { name: Reaction(name=name, reactants={species1: 1}, products={species2: 1}, rate=rate) for name in ["reaction1", "reaction2"] } with self.assertRaises(ModelError): model.add_reaction(reactions)
def test_run_nonsolver(self): model = Model() rate = Parameter(name='rate', expression=0.5) model.add_parameter(rate) species1 = Species(name='A', initial_value=0) species2 = Species(name='B', initial_value=0) model.add_species(species1) model.add_species(species2) reaction = Reaction(name='reaction1', reactants={species1: 1}, products={species2: 1}, rate=rate) with self.assertRaises(SimulationError): results = model.run(number_of_trajectories=1, solver='non_solver', seed=1)
def test_addingSameSpecies_ThrowsError(self): A = Species(name='A', initial_value=0) with self.assertRaises(ModelError) as ex: self.model.add_species(A) self.assertEqual( str(ex.exception), 'Name "{}" is unavailable. A species with that name exists.'. format(A.name))
def test_altered_model_failure(self): model = MichaelisMenten() for solver in self.solvers: tmpResults = model.run(solver=solver) with self.assertRaises(gillespyError.SimulationError): sp1 = Species('sp2', initial_value=5) model.add_species(sp1) tmpResults = model.run(solver=solver, resume=tmpResults, t=150) model.delete_species('sp2')
def __init__(self, model_name="Degradation"): super().__init__(name=model_name) A = Species(name="A", initial_value=2000) self.add_species(A) k1 = Parameter(name="k1", expression="0.1") self.add_parameter(k1) r1 = Reaction(name="r1", rate="k1", reactants={"A": 1}) self.add_reaction(r1) self.timespan(np.arange(0, 151))
def __init__(self, parameter_values=None): # initialize Model Model.__init__(self, name="Trichloroethylene") # Species A = Species(name='TCE', initial_value=300) B = Species(name='Epoxide', initial_value=120) C = Species(name='Dichloracatate', initial_value=0) D = Species(name='LossOfOneCL', initial_value=0) E = Species(name='Glyoxylate', initial_value=0) F = Species(name='Output', initial_value=0) self.add_species([A, B, C, D, E, F]) # Parameters K1 = Parameter(name='K1', expression=0.00045 * 0.000025) K2 = Parameter(name='K2', expression=14) K3 = Parameter(name='K3', expression=0.033) K4 = Parameter(name='K4', expression=500 * 0.0001) K5 = Parameter(name='K5', expression=500 * 0.0001) self.add_parameter([K1, K2, K3, K4, K5]) # Reactions J1 = Reaction(name="J1", reactants={A: 1}, products={B: 1}, rate=K2) J2 = Reaction(name="J2", reactants={B: 1}, products={C: 1}, rate=K3) J3 = Reaction(name="J3", reactants={C: 1}, products={D: 1}, rate=K4) J4 = Reaction(name="J4", reactants={D: 1}, products={E: 1}, rate=K4) J5 = Reaction(name="J5", reactants={E: 1}, products={F: 1}, rate=K5) self.add_reaction([J1, J2, J3, J4, J5]) self.timespan(np.linspace(0, 10000, 100))
def __init__(self, parameter_values=None): # Initialize the model. Model.__init__(self, name="Example") # Species S = Species(name='Sp', initial_value=100) self.add_species([S]) # Parameters k1 = Parameter(name='k1', expression=3.0) self.add_parameter([k1]) # Reactions rxn1 = Reaction(name='S degradation', reactants={S: 1}, products={}, rate=k1) self.add_reaction([rxn1]) self.timespan(np.linspace(0, 20, 101))
def __init__(self, parameter_values=None): # Initialize the model. Model.__init__(self, name="Toggle_Switch") # Species A = Species(name='A', initial_value=10) B = Species(name='B', initial_value=10) self.add_species([A, B]) # Parameters alpha1 = Parameter(name='alpha1', expression=10) alpha2 = Parameter(name='alpha2', expression=10) beta = Parameter(name='beta', expression=2) gamma = Parameter(name='gamma', expression=2) mu = Parameter(name='mu', expression=1) self.add_parameter([alpha1, alpha2, beta, gamma, mu]) # Reactions self.add_reaction( Reaction(name="cu", reactants={}, products={'A': 1}, propensity_function="alpha1/(1+pow(B, beta))")) self.add_reaction( Reaction(name="cv", reactants={}, products={'B': 1}, propensity_function="alpha2/(1+pow(A, gamma))")) self.add_reaction( Reaction(name="du", reactants={'A': 1}, products={}, rate=self.listOfParameters["mu"])) self.add_reaction( Reaction(name="dv", reactants={'B': 1}, products={}, rate=self.listOfParameters["mu"])) self.timespan(np.linspace(0, 250, 251))
def test_invalid_initial_value_negative(self): with self.assertRaises(ValueError): species = Species('A', initial_value=-1)
def test_valid_initial_value_negative(self): species = Species('A', initial_value=-1, allow_negative_populations=True)