def test_replace_species_in_Reaction(self): c1 = Complex([self.s1, self.s_old]) c2 = Complex([self.s1, self.s_new]) r1 = Reaction.from_massaction([self.s1, self.s_old], [c1], k_forward=1) self.assertTrue( r1.replace_species(self.s_old, self.s_new) == Reaction.from_massaction([self.s1, self.s_new], [c2], k_forward=1))
def test_old_reaction_interface_non_massaction(): kb = 100 ku = 10 kex = 1. G = Species(name="G", material_type="dna") #DNA A = Species(name="A", material_type="protein") #Activator X = Species(name="X", material_type="protein") # hill positive with pytest.deprecated_call(): Reaction([G], [G, X], propensity_type="hillpositive", propensity_params={"k": kex, "n": 2.0, "K": float(kb/ku), "s1": A}) # proportional hill positive with pytest.deprecated_call(): Reaction([G], [G, X], propensity_type="proportionalhillpositive", propensity_params={"k": kex, "n": 2.0, "K": float(kb/ku), "s1": A, "d": G}) # hill Negative with pytest.deprecated_call(): Reaction([G], [G, X], propensity_type="hillnegative", propensity_params={"k": kex, "n": 2.0, "K": float(kb/ku), "s1": A}) # proportional hill negative with pytest.deprecated_call(): Reaction([G], [G, X], propensity_type="proportionalhillnegative", propensity_params={"k": kex, "n": 2.0, "K": float(kb/ku), "s1": A, "d": G})
def test_reaction_list_flattening(): sp1 = Species(name='test_species_a') sp2 = Species(name='test_species_b') k_f = 1 mak = MassAction(k_forward=k_f) rxn1 = Reaction(inputs=[sp1, [sp1, sp2]], outputs=[[sp2, sp2], sp1], propensity_type=mak) rxn2 = Reaction(inputs=[sp1, sp1, sp2], outputs=[sp1, sp2, sp2], propensity_type=mak) assert rxn1 == rxn2
def test_old_reaction_interface_massaction(): A = Species(name="A") with pytest.deprecated_call(): Reaction([], [A, A], k=100) with pytest.deprecated_call(): Reaction([A, A], [], k=1, k_rev=0.1)
def test_reaction_equality(): """test for the_equality operator""" sp1 = Species(name='test_species_a') sp2 = Species(name='test_species_b') rxn1 = Reaction(inputs=[sp1, sp2], outputs=[sp2, sp2], propensity_type=MassAction(k_forward=1)) rxn2 = Reaction(inputs=[sp2, sp1], outputs=[sp2, sp2], propensity_type=MassAction(k_forward=1)) rxn3 = Reaction(inputs=[sp2, sp1], outputs=[sp2, sp2], propensity_type=MassAction(k_forward=10)) rxn4 = Reaction(inputs=[sp2, sp1], outputs=[sp2], propensity_type=MassAction(k_forward=1)) assert rxn1 == rxn2 assert rxn1 != rxn3 assert rxn1 != rxn4
def test_check_crn_validity(self): checked_species, checked_reactions = ChemicalReactionNetwork.check_crn_validity( reactions=self.rxn_list, species=self.species_list) # test that the returned species list is the same as the species list supplied self.assertEqual(self.species_list, checked_species) # test that the returned reaction list is the same as the reaction list supplied self.assertEqual(self.rxn_list, checked_reactions) species_list_with_none = self.species_list.copy() # injecting a None to the species list species_list_with_none.append(None) # test whether a non-species object is detected and Value error has been raised # A non-species object was used as a species: [test_species1, test_species2, None]!"' # A non-species object was used as a species: [test_species1, test_species2, None]!" with self.assertRaisesRegexp( ValueError, "A non-species object was used as a species!"): ChemicalReactionNetwork.check_crn_validity( reactions=self.rxn_list, species=species_list_with_none) rxn_list_with_none = self.rxn_list.copy() # injecting a None to the reaction list rxn_list_with_none.append(None) # test whether a non-reaction object is detected and Value Error has been raised with self.assertRaisesRegexp( ValueError, 'A non-reaction object was used as a reaction!'): ChemicalReactionNetwork.check_crn_validity( reactions=rxn_list_with_none, species=self.species_list) rxn2 = Reaction(inputs=[self.s1], outputs=[self.s3], k=0.1) # test warning raised if a species (in the reaction outputs) is detected which is not part of the species list with self.assertWarnsRegex( Warning, f'contains a species {self.s3.name} which is not in the CRN'): ChemicalReactionNetwork.check_crn_validity( reactions=[rxn2], species=self.species_list, warnings=True) rxn3 = Reaction(inputs=[self.s4], outputs=[self.s2], k=0.1) # test warning raised if a species (in the reaction inputs) is detected which is not part of the species list with self.assertWarnsRegex( Warning, f'contains a species {self.s4.name} which is not in the CRN'): ChemicalReactionNetwork.check_crn_validity( reactions=[rxn3], species=self.species_list, warnings=True) # test duplicate reactions rxn_list = [self.rx1, self.rx1] with self.assertWarnsRegex(Warning, 'may be duplicated in CRN definitions'): ChemicalReactionNetwork.check_crn_validity( reactions=rxn_list, species=self.species_list, warnings=True)
def test_species_merging(): sp1 = Species(name='test_species_a') chem_complexes = [WeightedSpecies(species=sp1, stoichiometry=2), WeightedSpecies(species=sp1, stoichiometry=1)] rxn = Reaction(inputs=chem_complexes, outputs=[], propensity_type=MassAction(k_forward=1)) # same species with different stoichiometry gets merged into one species assert len(rxn.inputs) == 1 sp1 = Species(name='test_species_a') chem_complexes = [WeightedSpecies(species=sp1, stoichiometry=2), WeightedSpecies(species=sp1, stoichiometry=1)] rxn = Reaction(inputs=[], outputs=chem_complexes, propensity_type=MassAction(k_forward=1)) # same species with different stoichiometry gets merged into one species assert len(rxn.outputs) == 1
def test_compile_crn(self): a = Species(name='a') b = Species(name='b') species_list = [a, b] rxn = Reaction.from_massaction(inputs=[a], outputs=[b], k_forward=0.1) CRN = ChemicalReactionNetwork(species_list, [rxn]) # create a component component = Component("comp") # creating a mock update function to decouple the update process from the rest of the code def mock_update_reactions(): rxn = Reaction.from_massaction(inputs=[a], outputs=[b], k_forward=0.1) return [rxn] def mock_update_species(): return [a, b] component.update_species = mock_update_species component.update_reactions = mock_update_reactions mixture = Mixture(components=[component]) crn_from_mixture = mixture.compile_crn() # test that the mixture has the same species as the manually build CRN object self.assertEqual(set(CRN.species), set(crn_from_mixture.species)) # test that the mixture has the same reactions as the manually build CRN object self.assertEqual(CRN.reactions, crn_from_mixture.reactions)
def test_get_all_species_containing(self): from biocrnpyler import ChemicalReactionNetwork from biocrnpyler import Species from biocrnpyler import Reaction s1 = Species(name='test_species1') s2 = Species(name='test_species2') species_list = [s1, s2] rx1 = Reaction(inputs=[s1], outputs=[s2], k=0.1) rxn_list = [rx1] crn = ChemicalReactionNetwork(species=species_list, reactions=rxn_list) s3 = Species(name='test_species3') with self.assertRaises(ValueError): crn.get_all_species_containing(species=species_list) rtn_species_list = crn.get_all_species_containing(species=s3) self.assertEqual(rtn_species_list, []) rtn_species_list = crn.get_all_species_containing(species=s1) self.assertEqual(rtn_species_list, [s1]) rtn_species_list = crn.get_all_species_containing( species=s1, return_as_strings=True) self.assertEqual(rtn_species_list, [repr(s1)])
def test_initial_condition_vector(self): from biocrnpyler import ChemicalReactionNetwork from biocrnpyler import Species from biocrnpyler import Reaction s1 = Species(name='test_species1') s2 = Species(name='test_species2') species_list = [s1, s2] rx1 = Reaction(inputs=[s1], outputs=[s2], k=0.1) rxn_list = [rx1] crn = ChemicalReactionNetwork(species=species_list, reactions=rxn_list) s3 = Species(name='test_species3') init_cond = {s1: 5, s3: 10} x0 = crn.initial_condition_vector(init_cond_dict=init_cond) not_in_the_list = False for key, value in init_cond.items(): if value not in x0 and key == s3: not_in_the_list = True self.assertTrue(not_in_the_list)
def test_write_sbml_file(self): import libsbml from biocrnpyler import ChemicalReactionNetwork from biocrnpyler import Species from biocrnpyler import Reaction s1 = Species(name='test_species1') s2 = Species(name='test_species2') species_list = [s1, s2] rx1 = Reaction(inputs=[s1], outputs=[s2], k=0.1) rxn_list = [rx1] crn = ChemicalReactionNetwork(species=species_list, reactions=rxn_list) document, _ = crn.generate_sbml_model() sbml_string = libsbml.writeSBMLToString(document) file_name = 'test_sbml.xml' with patch("builtins.open", new=mock_open()) as _file: crn.write_sbml_file(file_name) _file.assert_called_once_with(file_name, 'w') _file().write.assert_called_once_with(sbml_string)
def test_compile_crn(self): from biocrnpyler import ChemicalReactionNetwork from biocrnpyler import Species from biocrnpyler import Reaction from biocrnpyler import Mixture a = Species(name='a') b = Species(name='b') species_list = [a, b] def mock_update_reactions(): rxn = Reaction(inputs=[a], outputs=[b], k=0.1) return [rxn] rxn = Reaction(inputs=[a], outputs=[b], k=0.1) CRN = ChemicalReactionNetwork(species_list, [rxn]) mixture = Mixture(species=species_list) mixture.update_reactions = mock_update_reactions crn_from_mixture = mixture.compile_crn() self.assertEqual(CRN.species, crn_from_mixture.species) self.assertEqual(CRN.reactions, crn_from_mixture.reactions)
def test_complex_set_equality(self): sp1 = Species(name='test_species_a') sp2 = Species(name='test_species_b') # test whether two reactions with the same species are equal rxn1 = Reaction(inputs=[sp1], outputs=[], k=0.1, input_coefs=[1]) rtn = Reaction.complex_set_equality(c1=rxn1.inputs, c1_coefs=rxn1.input_coefs, c2=rxn1.inputs, c2_coefs=rxn1.input_coefs) self.assertTrue(rtn) # test that two reactions have the two species with equal coefficients are equal rxn2 = Reaction(inputs=[sp1], outputs=[sp2], k=0.1, input_coefs=[1], output_coefs=[1]) rtn = Reaction.complex_set_equality(c1=rxn1.inputs, c1_coefs=rxn1.input_coefs, c2=rxn2.inputs, c2_coefs=rxn2.input_coefs) self.assertTrue(rtn) # test that two reactions have the two species with different coefficients are not equal rxn1 = Reaction(inputs=[sp1], outputs=[], k=0.1, input_coefs=[1]) rxn2 = Reaction(inputs=[sp2], outputs=[], k=0.1, input_coefs=[2]) rtn2 = Reaction.complex_set_equality(c1=rxn1.inputs, c1_coefs=rxn1.input_coefs, c2=rxn2.inputs, c2_coefs=rxn2.input_coefs) self.assertFalse(rtn2) # test that two reactions with different species are not equal rxn1 = Reaction(inputs=[sp1, sp2], outputs=[], k=0.1, input_coefs=[1, 2]) rxn2 = Reaction(inputs=[sp2], outputs=[], k=0.1, input_coefs=[2]) rtn3 = Reaction.complex_set_equality(c1=rxn1.inputs, c1_coefs=rxn1.input_coefs, c2=rxn2.inputs, c2_coefs=rxn2.input_coefs) self.assertFalse(rtn3)
def test_check_crn_validity(self): from biocrnpyler import ChemicalReactionNetwork from biocrnpyler import Species from biocrnpyler import Reaction s1 = Species(name='test_species1') s2 = Species(name='test_species2') species_list = [s1, s2] rx1 = Reaction(inputs=[s1], outputs=[s2], k=0.1) rxn_list = [rx1] checked_species, checked_reactions = ChemicalReactionNetwork.check_crn_validity( reactions=rxn_list, species=species_list) self.assertEqual(species_list, checked_species) self.assertEqual(rxn_list, checked_reactions) species_list_with_none = species_list.copy() species_list_with_none.append(None) with self.assertRaises(ValueError): ChemicalReactionNetwork.check_crn_validity( reactions=rxn_list, species=species_list_with_none) rxn_list_with_none = rxn_list.copy() rxn_list_with_none.append(None) with self.assertRaises(ValueError): ChemicalReactionNetwork.check_crn_validity( reactions=rxn_list_with_none, species=species_list) s3 = Species(name='test_species3') s4 = Species(name='test_species4') rxn2 = Reaction(inputs=[s1], outputs=[s3], k=0.1) with self.assertWarns(Warning): ChemicalReactionNetwork.check_crn_validity(reactions=[rxn2], species=species_list, warnings=True) rxn3 = Reaction(inputs=[s4], outputs=[s2], k=0.1) with self.assertWarns(Warning): ChemicalReactionNetwork.check_crn_validity(reactions=[rxn3], species=species_list, warnings=True)
def test_replace_species_with_a_non_massaction_reaction(self): c1 = Complex([self.s1, self.s_old]) prop_hill_old = ProportionalHillPositive(k=1., s1=self.s1, K=10, d=self.s_old, n=2) r1 = Reaction([self.s1, self.s_old], [c1], propensity_type=prop_hill_old) prop_hill_new = ProportionalHillPositive(k=1., s1=self.s1, K=10, d=self.s_new, n=2) r1_new = Reaction([self.s1, self.s_new], [c1.replace_species(self.s_old, self.s_new)], propensity_type=prop_hill_new) self.assertTrue(r1.replace_species(self.s_old, self.s_new) == r1_new)
def setUp(self) -> None: """this method gets executed before every test""" self.s1 = Species(name='test_species1') self.s2 = Species(name='test_species2') self.s3 = Species(name='test_species3') self.s4 = Species(name='test_species4') self.species_list = [self.s1, self.s2] # creating a valid reaction two species self.rx1 = Reaction(inputs=[self.s1], outputs=[self.s2], k=0.1) self.rxn_list = [self.rx1] self.crn = ChemicalReactionNetwork(species=self.species_list, reactions=self.rxn_list)
def test_replace_in_a_chemical_reaction_network(self): c1 = Complex([self.s1, self.s_old]) c2 = Complex([self.s1, c1]) species = [self.s1, self.s_old, c1, c2] r1 = Reaction.from_massaction([self.s1, self.s_old], [c1], k_forward=1) crn = ChemicalReactionNetwork(species=species, reactions=[r1]) new_crn = crn.replace_species(self.s_old, self.s_new) self.assertTrue(self.s1 in new_crn.species) self.assertFalse(self.s_old in new_crn.species) self.assertTrue(self.s_new in new_crn.species) self.assertFalse(c1 in new_crn.species) self.assertFalse(c2 in new_crn.species) c1_new = Complex([self.s1, self.s_new]) c2_new = Complex([self.s1, c1_new]) self.assertTrue(c1_new in new_crn.species) self.assertTrue(c2_new in new_crn.species) r1_new = Reaction.from_massaction([self.s1, self.s_new], [c1_new], k_forward=1) self.assertFalse(r1 in new_crn.reactions) self.assertTrue(r1_new in new_crn.reactions)
def test_reaction_protection(self): #tests that Reactions cannot be changed once they are in a CRN S = Species("S") S2 = Species("S2") R = Reaction.from_massaction([S], [S2], k_forward=1.0) R2 = Reaction.from_massaction([S2], [S], k_forward=1.0) CRN = ChemicalReactionNetwork([S, S2], [R]) #Internal reactions copied correctly to return assert R in CRN.reactions assert not R is CRN._reactions[0] #Returned list does not effect internal reactions CRN.reactions[0] = R2 assert R2 not in CRN.reactions #add reactions effects internal reaction list CRN.add_reactions(R2) assert R2 in CRN.reactions assert not R2 is CRN._reactions[1] with self.assertRaisesRegex( AttributeError, "The reactions in a CRN cannot be removed or modified*"): CRN.reactions = [] #test bypassing reaction protection CRN = ChemicalReactionNetwork([], []) CRN.add_reactions([R], copy_reactions=False) assert R is CRN._reactions[0] assert S in CRN.species #test bypassing reaction protection CRN = ChemicalReactionNetwork([], []) CRN.add_reactions([R], add_species=False) assert not S in CRN.species
def test_compile_crn_directives(self): a = Species(name='a') b = Species(name='b') species_list = [a, b] rxn = Reaction.from_massaction(inputs=[a], outputs=[b], k_forward=0.1) CRN = ChemicalReactionNetwork(species_list, [rxn]) # create a component component = Component("comp") # creating a mock update function to decouple the update process from the rest of the code def mock_update_reactions(): rxn = Reaction.from_massaction(inputs=[a], outputs=[b], k_forward=0.1) return [rxn] def mock_update_species(): return [a, b] component.update_species = mock_update_species component.update_reactions = mock_update_reactions mixture = Mixture(components=[component]) #All the directives used below should not change the CRN. #They just remove some checks and safegaurds, but compilation should work the same in this simple case. #directives are best used in specific cases to compile very large models where speed is essential crn_from_mixture1 = mixture.compile_crn(copy_objects = False) # test that the mixture has the same species as the manually build CRN object self.assertEqual(set(CRN.species), set(crn_from_mixture1.species)) # test that the mixture has the same reactions as the manually build CRN object self.assertEqual(CRN.reactions, crn_from_mixture1.reactions) crn_from_mixture2 = mixture.compile_crn(add_reaction_species = False) # test that the mixture has the same species as the manually build CRN object self.assertEqual(set(CRN.species), set(crn_from_mixture2.species)) # test that the mixture has the same reactions as the manually build CRN object self.assertEqual(CRN.reactions, crn_from_mixture2.reactions) crn_from_mixture3 = mixture.compile_crn(initial_concentrations_at_end = True) # test that the mixture has the same species as the manually build CRN object self.assertEqual(set(CRN.species), set(crn_from_mixture3.species)) # test that the mixture has the same reactions as the manually build CRN object self.assertEqual(CRN.reactions, crn_from_mixture3.reactions)
def test_write_sbml_file(self): s1, s2 = Species("S1"), Species("S2") rx1 = Reaction.from_massaction(inputs=[s1], outputs=[s2], k_forward=0.1) crn = ChemicalReactionNetwork(species=[s1, s2], reactions=[rx1]) model_id = 'test_model' document, _ = crn.generate_sbml_model(model_id=model_id) sbml_string = libsbml.writeSBMLToString(document) file_name = 'test_sbml.xml' with patch("builtins.open", new=mock_open()) as _file: crn.write_sbml_file(file_name, model_id=model_id) _file.assert_called_once_with(file_name, 'w') _file().write.assert_called_once_with(sbml_string)
def test_generate_sbml_model(self): from biocrnpyler import ChemicalReactionNetwork from biocrnpyler import Species from biocrnpyler import Reaction s1 = Species(name='test_species1') s2 = Species(name='test_species2') species_list = [s1, s2] rx1 = Reaction(inputs=[s1], outputs=[s2], k=0.1) rxn_list = [rx1] crn = ChemicalReactionNetwork(species=species_list, reactions=rxn_list) document, model = crn.generate_sbml_model() self.assertEqual(len(model.getListOfSpecies()), len(crn.species)) self.assertEqual(len(model.getListOfReactions()), len(crn.reactions))
def test_generate_sbml_model(self): # generate an sbml model document, model = self.crn.generate_sbml_model() # all species from the CRN are accounted for self.assertEqual(len(model.getListOfSpecies()), len(self.crn.species)) # all reactions from the CRN are accounted for self.assertEqual(len(model.getListOfReactions()), len(self.crn.reactions)) # test a reversible reaction rx1 = Reaction(inputs=[self.s1], outputs=[self.s2], k=0.1, k_rev=0.1) rxn_list = [rx1] crn = ChemicalReactionNetwork(species=self.species_list, reactions=rxn_list) # generate an sbml model document, model = crn.generate_sbml_model() # all species from the CRN are accounted for self.assertEqual(len(model.getListOfSpecies()), len(crn.species)) # all reactions from the CRN are accounted for # the sbml represents a reverisble reaction with to separate reactions self.assertEqual(len(model.getListOfReactions()), 2*len(crn.reactions))
def test_reaction_initialization(self): # warns if both input and output species are empty mak = MassAction(k_forward=0.1) with self.assertWarns(Warning): Reaction(inputs=[], outputs=[], propensity_type=mak) # test for invalid propensity type with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], propensity_type=Species) # input must be a valid species object with self.assertRaises(TypeError): Reaction(inputs=['a'], outputs=[], propensity_type=mak) # output must be a valid species object with self.assertRaises(TypeError): Reaction(inputs=[], outputs=['b'], propensity_type=mak) rxn = Reaction.from_massaction(inputs=[], outputs=[], k_forward=0.1, k_reverse=1) # test whether the reaction is registered as reversible self.assertTrue(rxn.is_reversible) # test whether the reaction is registered as massaction self.assertTrue(isinstance(rxn.propensity_type, MassAction)) # test WeightedSpecies inputs sp1 = Species(name='test_species_a') sp2 = Species(name='test_species_b') chem_com_sp1 = WeightedSpecies(species=sp1, stoichiometry=2) chem_com_sp2 = WeightedSpecies(species=sp2, stoichiometry=1) Reaction(inputs=[chem_com_sp1], outputs=[chem_com_sp2], propensity_type=MassAction(k_forward=1)) # test different input and output lists Reaction(inputs=[chem_com_sp1], outputs=[sp2], propensity_type=MassAction(k_forward=1)) # mixing WeightedSpecies and Species is not allowed with self.assertRaises(TypeError): Reaction(inputs=[chem_com_sp1, sp2], outputs=[sp1], propensity_type=MassAction(k_forward=1))
def test_compile_crn(self): a = Species(name='a') b = Species(name='b') species_list = [a, b] # creating a mock update function to decouple the update process from the rest of the code def mock_update_reactions(): rxn = Reaction(inputs=[a], outputs=[b], k=0.1) return [rxn] rxn = Reaction(inputs=[a], outputs=[b], k=0.1) CRN = ChemicalReactionNetwork(species_list, [rxn]) mixture = Mixture(species=species_list) mixture.update_reactions = mock_update_reactions crn_from_mixture = mixture.compile_crn() # test that the mixture has the same species as the manually build CRN object self.assertEqual(CRN.species, crn_from_mixture.species) # test that the mixture has the same reactions as the manually build CRN object self.assertEqual(CRN.reactions, crn_from_mixture.reactions)
def test_complex_set_equality(self): from biocrnpyler import Reaction from biocrnpyler import Species rxn1 = Reaction(inputs=[], outputs=[], k=0.1) rxn2 = Reaction(inputs=[], outputs=[], k=0.1) rtn = Reaction.complex_set_equality(c1=rxn1.inputs, c1_coefs=rxn1.input_coefs, c2=rxn2.inputs, c2_coefs=rxn2.output_coefs) self.assertTrue(rtn) sp1 = Species(name='test_species_a') sp2 = Species(name='test_species_b') rxn1 = Reaction(inputs=[sp1], outputs=[], k=0.1) rxn2 = Reaction(inputs=[sp2], outputs=[], k=0.1) rtn2 = Reaction.complex_set_equality(c1=rxn1.inputs, c1_coefs=rxn1.input_coefs, c2=rxn2.inputs, c2_coefs=rxn2.output_coefs) self.assertFalse(rtn2)
def test_reaction_initialization(self): from biocrnpyler import Reaction with self.assertWarns(Warning): Reaction(inputs=[], outputs=[], k=0.1, propensity_type="massaction", propensity_params=None) with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], k=0.1, propensity_type="not_massaction", k_rev=1) with self.assertRaises(ValueError): prop_types = [ "hillpositive", "hillnegative", "proportionalhillpositive", "proportionalhillnegative" ] for prop_type in prop_types: Reaction(inputs=[], outputs=[], k=0.1, propensity_type=prop_type, propensity_params={'s1': 0}) with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], k=0.1, propensity_type="dummy") with self.assertRaises(ValueError): Reaction(inputs=['a'], outputs=[], k=0.1) Reaction(inputs=[], outputs=['b'], k=0.1) with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], k=0) Reaction(inputs=[], outputs=[], k=-1) rxn = Reaction(inputs=[], outputs=[], k=0.1, k_rev=1) self.assertTrue(rxn.reversible) with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], k=0.1, input_coefs=[1, 2]) with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], k=0.1, output_coefs=[1, 2])
def mock_update_reactions(): rxn = Reaction.from_massaction(inputs=[a], outputs=[b], k_forward=0.1) return [rxn]
def test_check_crn_validity(self): checked_reactions, checked_species = ChemicalReactionNetwork.check_crn_validity( reactions=self.rxn_list, species=self.species_list) # test that the returned species list is the same as the species list supplied self.assertEqual(self.species_list, checked_species) # test that the returned reaction list is the same as the reaction list supplied self.assertEqual(self.rxn_list, checked_reactions) species_list_with_none = self.species_list.copy() # injecting a None to the species list species_list_with_none.append(None) # test whether a non-species object is detected and Value error has been raised # A non-species object was used as a species: [test_species1, test_species2, None]!"' # A non-species object was used as a species: [test_species1, test_species2, None]!" with self.assertRaisesRegex( ValueError, "A non-species object was used as a species!"): ChemicalReactionNetwork.check_crn_validity( reactions=self.rxn_list, species=species_list_with_none) rxn_list_with_none = self.rxn_list.copy() # injecting a None to the reaction list rxn_list_with_none.append(None) # test whether a non-reaction object is detected and Value Error has been raised with self.assertRaisesRegex( ValueError, 'A non-reaction object was used as a reaction!'): ChemicalReactionNetwork.check_crn_validity( reactions=rxn_list_with_none, species=self.species_list) rxn2 = Reaction.from_massaction(inputs=[self.s1], outputs=[self.s3], k_forward=0.1) # test warning raised if a species (in the reaction outputs) is detected which is not part of the species list with self.assertWarnsRegex( Warning, f'are not part of any reactions in the CRN'): ChemicalReactionNetwork.check_crn_validity( reactions=[rxn2], species=self.species_list, show_warnings=True) rxn3 = Reaction.from_massaction(inputs=[self.s4], outputs=[self.s2], k_forward=0.1) # test warning raised if a species (in the reaction inputs) is detected which is not part of the species list with self.assertWarnsRegex( Warning, f'are not part of any reactions in the CRN'): ChemicalReactionNetwork.check_crn_validity( reactions=[rxn3], species=self.species_list, show_warnings=True) # test warning if reaction has unlisted species rxn4 = Reaction.from_massaction(inputs=[self.s4, self.s3], outputs=[self.s2], k_forward=0.1) with self.assertWarnsRegex( Warning, f'are not listed in the Species list, but part of the reactions' ): ChemicalReactionNetwork.check_crn_validity( reactions=[rxn4], species=[self.s4, self.s2], show_warnings=True) # test duplicate reactions are both added rxn_list = [self.rx1, self.rx1] CRN = ChemicalReactionNetwork(species=[self.s1, self.s2], reactions=rxn_list) self.assertTrue(CRN.reactions.count(self.rx1) == 2) with self.assertWarnsRegex(Warning, 'may be duplicated in CRN definitions'): ChemicalReactionNetwork.check_crn_validity( reactions=rxn_list, species=self.species_list, show_warnings=True) # test warning suppression with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. ChemicalReactionNetwork.check_crn_validity( reactions=rxn_list, species=self.species_list, show_warnings=False) assert not w
def test_reaction_initialization(self): # warns if both input and output species are empty with self.assertWarns(Warning): Reaction(inputs=[], outputs=[], k=0.1, propensity_type="massaction", propensity_params=None) # non-massaction propensities require propensity_params dict with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], k=0.1, propensity_type="not_massaction") # non-massaction propensities cannot be reversible with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], k=0.1, propensity_type="not_massaction", propensity_params={}, k_rev=1) # test under specified propensity parameters propensity_types = [ "hillpositive", "hillnegative", "proportionalhillpositive", "proportionalhillnegative" ] for propensity_type in propensity_types: with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], k=0.1, propensity_type=propensity_type, propensity_params={'s1': 0}) # test when rate is missing from the propensity parameter dictionary for a general propensity with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], k=0.1, propensity_type='general', propensity_params={}) # test unknown propensity type with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], k=0.1, propensity_type="dummy", propensity_params={}) # input must be a valid species object with self.assertRaises(ValueError): Reaction(inputs=['a'], outputs=[], k=0.1) # output must be a valid species object with self.assertRaises(ValueError): Reaction(inputs=[], outputs=['b'], k=0.1) # reaction rate coefficient must be larger than zero with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], k=0) # reaction rate coefficient must be larger than zero with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], k=-1) rxn = Reaction(inputs=[], outputs=[], k=0.1, k_rev=1) # test whether the reaction is registered as reversible self.assertTrue(rxn.reversible) # test whether the reaction is registered as massaction self.assertTrue(rxn.propensity_type == 'massaction') # test overspecified mass action sp1 = Species(name='test_species_a') sp2 = Species(name='test_species_b') with self.assertWarns(Warning): Reaction(inputs=[sp1], outputs=[sp2], propensity_type="massaction", propensity_params={}, k=0.1) # test whether the number of input and output coefficients match with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], k=0.1, input_coefs=[1, 2]) # test whether the number of input and output coefficients match with self.assertRaises(ValueError): Reaction(inputs=[], outputs=[], k=0.1, output_coefs=[1, 2])
def mock_update_reactions(): rxn = Reaction(inputs=[a], outputs=[b], k=0.1) return [rxn]