def test_propensity_dict_hill(): d = Species('d') s1 = Species('s1') k = ParameterEntry(parameter_value='1', parameter_name='k') K = ParameterEntry(parameter_value='2', parameter_name='K') n = ParameterEntry(parameter_value='3', parameter_name='n') #Should store the ParameterEntry in this case P1 = Hill(k=k, K=K, n=n, s1=s1, d=d) assert P1.propensity_dict["parameters"]["k"] == k assert P1.propensity_dict["parameters"]["K"] == K assert P1.propensity_dict["parameters"]["n"] == n #assert the getters work (should return values instead of ParameterEntries) assert P1.k == k.value assert P1.K == K.value assert P1.n == n.value #Should store a numerical value in this case P2 = Hill(k=k.value, K=K.value, n=n.value, s1=s1, d=d) assert P2.propensity_dict["parameters"]["k"] == k.value assert P2.propensity_dict["parameters"]["K"] == K.value assert P2.propensity_dict["parameters"]["n"] == n.value #assert the getters work assert P2.k == k.value assert P2.K == K.value assert P2.n == n.value
def test_massaction_from_parameters(): mak = MassAction(k_forward=ParameterEntry("k", .1)) assert mak.k_forward == .1 mak = MassAction(k_forward=ParameterEntry("k", .1), k_reverse=ParameterEntry("k", .01)) assert mak.k_reverse == .01
def test_general_propensity(): S1, S2, S3 = Species("S1"), Species("S2"), Species("S3") # create some parameters k1 = ParameterEntry("k1", 1.11) k2 = ParameterEntry("k2", 2.22) gn1 = GeneralPropensity('k1*2 - k2/S1^2', propensity_species=[S1], propensity_parameters=[k1, k2]) assert str(S1) in gn1.propensity_dict['species'] assert k1.parameter_name in gn1.propensity_dict['parameters'] assert k2.parameter_name in gn1.propensity_dict['parameters'] assert gn1.pretty_print() == 'k1*2 - k2/S1^2\n k1=1.11\n k2=2.22\n' gn2 = GeneralPropensity('S1^2 + S2^2 + S3^2', propensity_species=[S1, S2, S3], propensity_parameters=[]) assert str(S1) in gn1.propensity_dict['species'] assert k1.parameter_name not in gn2.propensity_dict['parameters'] with pytest.raises(TypeError, match='propensity_species must be a list of Species!'): GeneralPropensity('S1^2 + S2^2 + S3^2', propensity_species=[k1], propensity_parameters=[]) with pytest.raises(TypeError, match='propensity_parameter must be a list of ParameterEntry!'): GeneralPropensity('S1^2 + S2^2 + S3^2', propensity_species=[], propensity_parameters=[S2]) test_formula = 'S3^2' with pytest.raises(ValueError, match=f'must be part of the formula'): GeneralPropensity(test_formula, propensity_species=[S1], propensity_parameters=[]) test_formula = 'k2*S3^2' with pytest.raises(ValueError, match=f'must be part of the formula'): GeneralPropensity(test_formula, propensity_species=[S3], propensity_parameters=[k1])
def test_indexing(self): parameter_dict = { "k": 1, ("M", "pid", "k"): 2.0, (None, "pid", "k"): 3.3, ("M", None, "k"): 4, } PD = ParameterDatabase(parameter_dictionary=parameter_dict) #Test correct accessing self.assertTrue(PD["k"].value == 1) self.assertTrue(PD[("M", "pid", "k")].value == 2.0) self.assertTrue(PD[(None, "pid", "k")].value == 3.3) self.assertTrue(PD[("M", None, "k")].value == 4) #test incorrect accessing with self.assertRaisesRegex(ValueError, f"parameter_key must be"): PD[("M", "k")] #test accessing something not in the PD with self.assertRaisesRegex(KeyError, f"ParameterKey"): PD["kb"] #test inserting values PD["kb"] = 100 self.assertTrue(PD["kb"].value == 100) PD[(None, None, "ku")] = 200 self.assertTrue(PD["ku"].value == 200) PD[("M", "pid", "ktx")] = 300 self.assertTrue(PD[("M", "pid", "ktx")].value == 300) #Test inserting ParameterEntry PE = ParameterEntry("test", 1.0) PD["test"] = PE self.assertTrue(PE in PD) #Test Correct Overwriting PE = ParameterEntry("test", 2.0) PD["test"] = PE self.assertTrue(PD["test"].value == PE.value) #Test incorrect Overwriting PE = ParameterEntry("t", 1.0) with self.assertRaisesRegex(ValueError, f"Parameter Key does not match"): PD["test"] = PE #Invalid parameter key with self.assertRaisesRegex(ValueError, f"parameter_key must be"): PD[("M", "k")] = 10 #Test overwriting PD["k"] = .1 self.assertTrue(PD["k"].value == .1) PD[("M", "pid", "ktx")] = .333 self.assertTrue(PD[("M", "pid", "ktx")].value == .333)
def test_propensity_dict_massaction(): k1 = ParameterEntry(parameter_value='1', parameter_name='k1') k2 = ParameterEntry(parameter_value='2', parameter_name='k2') #Should store the ParameterEntry in this case P1 = MassAction(k_forward=k1, k_reverse=k2) assert P1.propensity_dict["parameters"]["k_forward"] == k1 assert P1.propensity_dict["parameters"]["k_reverse"] == k2 #assert getters work (should return values instead of ParameterEntries) assert P1.k_forward == k1.value assert P1.k_reverse == k2.value #Should store a numerical value in this case P2 = MassAction(k_forward=k1.value, k_reverse=k2.value) assert P2.propensity_dict["parameters"]["k_forward"] == k1.value assert P2.propensity_dict["parameters"]["k_reverse"] == k2.value #assert getters work assert P2.k_forward == k1.value assert P2.k_reverse == k2.value
def test_parameter_entry(self): #Valid ParameterEntry Construction ParameterEntry(parameter_name="None", parameter_value=1.0, parameter_key={"part_id": "id"}, parameter_info={"comment": "comment"}) #Invalid keys param_keys = Parameter(parameter_name="None", parameter_value=1.0) with self.assertRaisesRegex(ValueError, "parameter_key must be"): ParameterEntry(parameter_name="None", parameter_value=1.0, parameter_key=param_keys) #Invalid info param_info = "blah blah" with self.assertRaisesRegex( ValueError, f"parameter_info must be None or a dictionary"): ParameterEntry(parameter_name="None", parameter_value=1.0, parameter_info=param_info)
def test_parameter_entry(self): #Valid ParameterEntry Construction #With unit P0 = ParameterEntry(parameter_name="None", parameter_value=1.0, parameter_key={"part_id": "id"}, parameter_info={ "comment": "comment", "unit": "M" }) P1 = ParameterEntry(parameter_name="None", parameter_value=1.0, parameter_key={"part_id": "id"}, parameter_info={"comment": "comment"}, unit="M") #Without unit P2 = ParameterEntry(parameter_name="None", parameter_value=1.0, parameter_key={"part_id": "id"}, parameter_info={"comment": "comment"}) #Assert unit is passed through to Parameter.unit from the parameter_info_dictionary self.assertTrue(P0.unit == "M") self.assertTrue(P1.unit == "M") self.assertTrue(P2.unit == "") #Test duplication of parameter information error with self.assertRaisesRegex(ValueError, "Recieved multiple parameter units"): P0 = ParameterEntry(parameter_name="None", parameter_value=1.0, parameter_key={"part_id": "id"}, parameter_info={ "comment": "comment", "unit": "M" }, unit="m") #Invalid keys param_keys = Parameter(parameter_name="None", parameter_value=1.0) with self.assertRaisesRegex(ValueError, "parameter_key must be"): ParameterEntry(parameter_name="None", parameter_value=1.0, parameter_key=param_keys) #Invalid info param_info = "blah blah" with self.assertRaisesRegex( ValueError, f"parameter_info must be None or a dictionary"): ParameterEntry(parameter_name="None", parameter_value=1.0, parameter_info=param_info)