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 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): 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 __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 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, 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 __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 __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): 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 __init__(self, input1, input2, weight1, weight2, record_keeper, pNeg, pPos, time, k_array): self.input1 = input1 self.input2 = input2 self.weight1 = weight1 self.weight2 = weight2 self.record_keeper = record_keeper self.pNeg = pNeg self.pPos = pPos self.time = time self.k_array = k_array system_volume = 100 constant = 1 Model.__init__(self, name="Hidden_NN", volume=system_volume) x1 = Species(name='input 1', initial_value=int(self.input1*system_volume)) x2 = Species(name='input 2', initial_value=int(self.input2*system_volume)) w1 = Species(name='weight 1', initial_value=int(self.weight1)) w2 = Species(name='weight 2', initial_value=int(self.weight2)) y = Species(name='output', initial_value=0) xy = Species(name='record_keeper', initial_value=int(self.record_keeper)) wPlus = Species(name='positive weight error', initial_value=0) wMinus = Species(name='negative weight error', initial_value=0) wNeg = Species(name='weight annihilator', initial_value=0) sF = Species(name="feed forward signal", initial_value=constant*system_volume) f = Species(name="feed forward input", initial_value=0) membrane = Species(name='cell membrane', initial_value=constant*system_volume) pNeg = Species(name='hidden neg penalty', initial_value=int(self.pNeg)) pPos = Species(name='hidden pos penalty', initial_value=int(self.pPos)) self.add_species([x1, x2, w1, w2, y, xy, wPlus, wMinus, wNeg, sF, f, membrane, pNeg, pPos]) k1 = Parameter(name='k1', expression=self.k_array[0]) k2 = Parameter(name='k2', expression=self.k_array[1]) k3 = Parameter(name='k3', expression=self.k_array[2]) k4 = Parameter(name='k4', expression=self.k_array[3]) k5 = Parameter(name='k5', expression=self.k_array[4]) k6 = Parameter(name='k6', expression=self.k_array[5]) self.add_parameter([k1, k2, k3, k4, k5, k6]) rxn11 = Reaction(name='input 1 to output', reactants={x1: 1, w1: 1}, products={y: 1, xy: 1, w1: 1}, rate=k1) rxn12 = Reaction(name='input 2 to output', reactants={x2: 1, w2: 1}, products={y: 1, xy: 1, w2: 1}, rate=k1) rxn21 = Reaction(name='x1 annihilation', reactants={x1: 1, y: 1}, products={}, rate=k2) rxn22 = Reaction(name='x2 annihilation', reactants={x2: 1, y: 1}, products={}, rate=k2) rxn3 = Reaction(name='positive weight adjustment', reactants={wPlus: 1, xy: 1}, products={w1: 1, w2: 1, xy: 1}, rate=k3) rxn41 = Reaction(name='negative weight adjustment 1', reactants={wMinus: 1, xy: 1}, products={wNeg: 1, xy: 1}, rate=k4) rxn42 = Reaction(name='negative weight adjustment 2', reactants={wNeg: 1, w1: 1}, products={}, rate=k4) rxn43 = Reaction(name='negative weight adjustment 3', reactants={wNeg: 1, w2: 1}, products={}, rate=k4) rxn5 = Reaction(name='output to input', reactants={y: 1, sF: 1}, products={f: 1, sF: 1}, rate=k5) rxn61 = Reaction(name='create pos weight adjust', reactants={pPos: 1, membrane: 1}, products={wPlus: 1, membrane: 1}, rate=k6) rxn62 = Reaction(name='create neg weight adjust', reactants={pNeg: 1, membrane: 1}, products={wMinus: 1, membrane: 1}, rate=k6) self.add_reaction([rxn11, rxn12, rxn21, rxn22, rxn3, rxn41, rxn42, rxn43, rxn5, rxn61, rxn62]) self.timespan(self.time)
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 test_model_equality(self): model1 = Model() model2 = Model() param1 = Parameter('A', expression=0) model1.add_parameter(param1) model2.add_parameter(param1) assert model1 == model2
def test_model_hash_accuracy(self): """ Test the accuracy of the JSON hash. """ for model in self.models: # Create two two instances of the 'model' type. model_1 = model() model_2 = model() # Assert that the hash of the anonymized models are the same. self.assertEqual(model_1.to_anon().get_json_hash(), model_2.to_anon().get_json_hash()) # Create a test class and change the variable insertion order. model_1 = model() model_1.var1 = "Hello" model_1.var2 = "world" model_1.var3 = ["Hello world!"] model_2 = model() model_2.var3 = ["Hello world!"] model_2.var2 = "world" model_2.var1 = "Hello" # Generate the first model's translation table. translation_table = model_1.get_translation_table() # Assert that the JSON of the anonymized models are still the same. self.assertEqual(model_1.to_anon().to_json(), model_2.to_anon().to_json()) # Assert that the hash of the anonymized models are still the same. self.assertEqual(model_1.to_anon().get_json_hash(), model_2.to_anon().get_json_hash()) # Assert that the translation table is the same. self.assertEqual(model_1.get_translation_table().to_json(), model_2.get_translation_table().to_json()) # Assert that model_1's JSON is equivalent to model_2 -> anon -> json -> object -> named -> json. self.assertEqual( model_1.to_json(), Model.from_json( model_2.to_anon().to_json()).to_named().to_json()) # Assert that model_2's anon JSON hash is equivalent to model_2 -> anon -> json -> object -> json hash. self.assertEqual( model_1.to_anon().get_json_hash(), Model.from_json(model_2.to_anon().to_json()).get_json_hash())
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 test_model_inequality(self): model1 = Model() model2 = Model() param1 = Parameter('A', expression=0) param2 = Parameter('B', expression=1) model1.add_parameter(param1) model2.add_parameter(param2) assert model1 != model2
def test_to_csv_single_result_no_path(self): test_data = Trajectory({'time': [0]}, model=Model('test_model'), solver_name='test_solver_name') result = Results(data=[test_data]) test_nametag = "test_nametag" test_stamp = "test_stamp"
def test_to_csv_single_result_no_nametag(self): test_model = Model('test_model') test_data = Trajectory(data={'time': [0]}, model=test_model) with tempfile.TemporaryDirectory() as tempdir: result.to_csv(nametag=test_nametag, path=tempdir) assert len(os.listdir(tempdir)) is not 0
def test_named_to_anon_accuracy(self): for model in self.models: model_1 = model() # For each model, check to see if we can convert its table to and from json accurately. # For each model, generate its translation table and convert it to JSON. translation_table = model_1.get_translation_table() translation_table_json = translation_table.to_json() # Convert the JSON back into a TranslationTable object. translation_table_from_json = TranslationTable.from_json( translation_table_json) # Assert that the two tables are still identical. self.assertEqual(translation_table, translation_table_from_json) # Anonymize and convert model_1 to JSON. model_1 = model() model_1_json = model_1.to_anon().to_json() # Convert the JSON back into a Model object. model_2 = Model.from_json(model_1_json) # Assert that the anonymized model_1 and the new model_2 are identical. self.assertEquals(model_1.to_anon().to_json(), model_2.to_json()) # Convert the new model_2 to named. model_2 = model_2.to_named() # Assert that model_1 and model_2 are still the same. self.assertEquals(model_1.to_json(), model_2.to_json())
def test_equality_of_named_models(self): """ Test that a model can be converted to JSON and back. """ for model in self.models: target = model() self.assertEqual(target, Model.from_json(target.to_json()))
def test_duplicate_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]) reaction1 = Reaction(name="reaction1", reactants={species1: 1}, products={species2: 1}, rate=rate) reaction2 = Reaction(name="reaction1", reactants={species2: 1}, products={species1: 1}, rate=rate) model.add_reaction(reaction1) with self.assertRaises(ModelError): model.add_reaction(reaction2)
def test_add_nonspecies_nonreaction_nonparameter(self): model = Model() species = 'nonspecies' with self.assertRaises(ModelError): model.add_species(species) reaction = 'nonreaction' with self.assertRaises(ModelError): model.add_reaction(reaction) parameter = 'nonparameter' with self.assertRaises(ParameterError): model.add_parameter(parameter)
def test_to_csv_single_result_no_nametag(self): test_data = {'time': [0]} test_model = Model('test_model') result = Results(data=test_data, model=test_model) result.solver_name = 'test_solver' test_stamp = "test_stamp" with tempfile.TemporaryDirectory() as tempdir: result.to_csv(stamp=test_stamp, path=tempdir) assert len(os.listdir(tempdir)) is not 0
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_equality_of_anon_models(self): """ Test that an anonymous model can be converted to JSON and back. """ for model in self.models: target = model() anon_target = target.to_anon() self.assertEqual(anon_target, Model.from_json(anon_target.to_json()))
def test_xscale_plot(self): from unittest import mock trajectory = Trajectory(data={ 'time': [0.], 'foo': [1.] }, model=Model('test_model')) results = Results(data=[trajectory]) with mock.patch('matplotlib.pyplot.xscale') as mock_xscale: results.plot(xscale='log') mock_xscale.assert_called_with('log')
def start_job(): request_obj = StartJobRequest.parse_raw(request.json) if delegate.job_complete(request_obj.job_id): return ErrorResponse( msg=f"The job with id '{request_obj.job_id} has already completed." ).json(), 400 if delegate.job_exists(request_obj.job_id): return StartJobResponse( job_id=request_obj.job_id, msg="The job has already been started.", status=f"/v1/job/{request_obj.job_id}/status").json(), 202 model = Model.from_json(request_obj.model) kwargs = json.loads(request_obj.kwargs) from gillespy2.solvers import SSACSolver kwargs["solver"] = SSACSolver if not "number_of_trajectories" in kwargs: delegate.start_job(request_obj.job_id, Model.run, model, **kwargs) else: trajectories = kwargs["number_of_trajectories"] del kwargs["number_of_trajectories"] # Hacky, but it works for now. keys = [ f"{request_obj.job_id}/trajectory_{i}" for i in range(0, trajectories) ] delegate.client.scatter([model, kwargs]) dependencies = delegate.client.map(Model.run, [model] * trajectories, **kwargs, key=keys) def test_job(*args, **kwargs): data = [] for result in args: data = data + result.data from gillespy2.core import Results return Results(data) delegate.start_job(request_obj.job_id, test_job, *dependencies) return StartJobResponse( job_id=request_obj.job_id, msg="The job has been successfully started.", status=f"/v1/job/{request_obj.job_id}/status").json(), 202
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 test_duplicate_parameter_names(self): model = Model() param1 = Parameter('A', expression=0) param2 = Parameter('A', expression=0) model.add_parameter(param1) with self.assertRaises(ModelError): model.add_parameter(param2)
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_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)