コード例 #1
0
    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)
コード例 #2
0
    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
コード例 #3
0
    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)