Пример #1
0
    def test_positive_parent_multi(self):
        """fetching a parent term when terms have multiple parents."""

        # load an ontology in which Y7 is connected to both Y2 and Y1
        Yfile = join(testdir, "Ymulti.obo")
        Yobo = MinimalObo(Yfile)
        Ydefaults = dict.fromkeys(Yobo.ids(), 0.0001)
        Ydefaults["Y:003"] = 0.0002
        Ydefaults["Y:005"] = 0.0002
        # make slight variations of representations
        rs = ReferenceSet(dict(refA=0.5, refB=0.5),
                          ids=Yobo.ids(),
                          row_priors=Ydefaults)
        refA = Representation(name="refA")
        refA.set("Y:002", 0.5).set("Y:005", 1).impute(Yobo, Ydefaults)
        refB = Representation(name="refB")
        refB.set("Y:001", 0.5).impute(Yobo, Ydefaults)
        rs.add(refA).add(refB)
        rs.learn_obo(Yobo)

        self.assertEqual(
            rs._positive_ancestor(rs.columns["refA"], rs.rows["Y:007"]),
            rs.rows["Y:002"], "Y2 is a positive ancestor")
        self.assertEqual(
            rs._positive_ancestor(rs.columns["refB"], rs.rows["Y:007"]),
            rs.rows["Y:001"], "Y1 is a positive immediate parent")
Пример #2
0
    def test_learn_from_obo(self):
        """create parents_of tuples for all features"""

        r1 = Representation(name="refA").set("DOID:0014667", 0.4)
        rs = ReferenceSet(dict(refA=0.5, refB=0.5), ids=obo.ids())
        rs.add(r1)
        self.assertEqual(rs.parents, None)
        rs.learn_obo(obo)
        self.assertEqual(len(rs.parents), len(obo.ids()))
Пример #3
0
 def test_between2(self):
     """inference when model equally similar to two refs"""  
     
     # let ref universe have two annotations
     rs = ReferenceSet(dict(refA=0.5, refB=0.5), ids=self.obo.ids(),
                       row_priors=self.obodefaults)
     rs.add(self.refA).add(self.refB)
     rs.learn_obo(self.obo)
     rs.prep()
     
     inf = rs.inference(self.y3model)
     self.assertAlmostEqual(inf["refA"], inf["refB"], msg="equally likely")        
Пример #4
0
 def test_between_refs_and_null(self):
     """inference when model is similar to two refs and a there is a null"""  
     
     # let ref universe have two annotations and one null
     rs = ReferenceSet(dict(null=0.8, refA=0.15, refB=0.15), 
                       ids=self.obo.ids(), row_priors=self.obodefaults)
     rs.add(self.refA).add(self.refB)        
     rs.learn_obo(self.obo)
     rs.prep()
     
     inf = rs.inference(self.y3model)            
     self.assertAlmostEqual(inf["refA"], inf["refB"], 
                            msg="equally likely")
Пример #5
0
 def test_model_nodata(self):
     """inference when references are unequal but model has no data"""  
     
     model = Representation(name="nodata")
     # let ref universe have two annotations and one null
     rs = ReferenceSet(dict(null=0.8, refA=0.1, refB=0.1), 
                       ids=self.obo.ids(), row_priors=self.obodefaults)
     rs.add(self.refA).add(self.refB)
     rs.learn_obo(self.obo)
     rs.prep()
     
     inf = rs.inference(model)             
     self.assertAlmostEqual(inf["refA"], inf["refB"], 
                            msg="equally likely")
Пример #6
0
 def test_difference_in_priors(self):
     """inference when model matches two references, 
     but have different priors"""  
     
     # let ref universe have two annotations and one null
     rs = ReferenceSet(dict(null=0.85, refA=0.05, refB=0.1), 
                       ids=self.obo.ids(), row_priors=self.obodefaults)
     rs.add(self.refA).add(self.refB)
     rs.learn_obo(self.obo)
     rs.prep()
     
     inf = rs.inference(self.y3model)             
     self.assertLess(inf["refA"], inf["refB"], 
                     msg="equal match, but A has weaker prior")
Пример #7
0
    def test_FP_can_increase(self):
        """FP can in principle yield greater score"""

        # make a new reference set with different priors
        priors2 = self.priors.copy()
        priors2["Y:002"] = 0.1
        rs2 = ReferenceSet(dict(null=0.4, refA=0.3, refB=0.3),
                           ids=Yobo.ids(),
                           row_priors=priors2)
        rs2.add(self.refnull).add(self.refA).add(self.refB)
        rs2.learn_obo(Yobo)

        FP = Representation(name="model").set("Y:002", 0.2)
        chain = rs2.inference_chain(FP, "refB", verbose=True, fp_penalty=1)
        chain.evaluate_inference()
        self.assertGreater(chain.posterior, chain.prior)
Пример #8
0
    def test_positive_parent(self):
        """fetching a parent term that has a positive value."""

        rs = ReferenceSet(dict(refA=0.5), ids=Yobo.ids(), row_priors=Ydefaults)
        refA = Representation(name="refA")
        refA.set("Y:002", 0.5).set("Y:005", 1).impute(Yobo, Ydefaults)
        rs.add(refA)
        rs.learn_obo(Yobo)

        refAindex = rs.columns["refA"]
        self.assertEqual(rs._positive_ancestor(refAindex, rs.rows["Y:002"]),
                         rs.rows["Y:002"], "Y2 is itself is positive")
        self.assertEqual(rs._positive_ancestor(refAindex, rs.rows["Y:007"]),
                         rs.rows["Y:002"], "Y2 is immediate parent of Y7")
        self.assertEqual(rs._positive_ancestor(refAindex, rs.rows["Y:006"]),
                         rs.rows["Y:005"], "Y5 is immediate parent of Y6")
Пример #9
0
 def test_underflow(self):
     """attempt to get underflow in individual p."""  
     
     # let model have very sure values
     model = Representation(name="underflow")
     model.set("Y:007", 0.00001).set("Y:004", 1).set("Y:003", 1)
     # let ref universe have two annotations and one null
     refA = Representation(name="refA").set("Y:003", 1)        
     refB = Representation(name="refB").set("Y:003", 1)        
     rs = ReferenceSet(dict(null=0.98, refA=0.001, refB=0.001), 
                       ids=self.obo.ids())
     rs.add(refA).add(refB)
     rs.learn_obo(self.obo)
     rs.prep()        
             
     result = rs.inference(model, verbose=True)                 
     self.assertGreaterEqual(result["refA"], 0, 
                     msg="must always be a number, even if zero")        
     self.assertGreaterEqual(result["refB"], 0, 
                     msg="must always be a number, even if zero")
     self.assertGreaterEqual(result["refB"], 0, 
                     msg="must always be a number, even if zero")        
Пример #10
0
    def test_TP_scores_better_than_FP(self):
        """FPs must score lower than TPs"""

        # make a new reference set with different (lower) priors
        priors2 = self.priors.copy()
        priors2["Y:002"] = 0.55
        priors2["Y:007"] = 0.15
        rs2 = ReferenceSet(dict(null=0.4, refA=0.3, refB=0.3),
                           ids=Yobo.ids(),
                           row_priors=priors2)
        rs2.add(self.refnull).add(self.refA).add(self.refB)
        rs2.learn_obo(Yobo)

        # compare with refA, which has Y:002 equal to 1
        FP = Representation(name="FP").set("Y:007", 1)
        chain_FP = rs2.inference_chain(FP, "refA", verbose=True, fp_penalty=2)
        chain_FP.evaluate_inference()
        TP = Representation(name="TP").set("Y:002", 1)
        chain_TP = rs2.inference_chain(TP, "refA", verbose=True)
        chain_TP.evaluate_inference()

        self.assertGreaterEqual(chain_TP.posterior, chain_FP.posterior)
Пример #11
0
    def test_FP_with_fp_penalty(self):
        """FP increases more with lower fp_penalty"""

        # make a new reference set with different priors
        priors2 = self.priors.copy()
        priors2["Y:003"] = 0.4
        priors2["Y:002"] = 0.15
        priors2["Y:007"] = 0.1
        rs2 = ReferenceSet(dict(null=0.4, ref=0.3),
                           ids=Yobo.ids(),
                           row_priors=priors2)
        ref = Representation(name="ref")
        ref.set("Y:001", 1).impute(Yobo, priors2)
        ref.set("Y:007", priors2["Y:007"] / 2)
        rs2.add(self.refnull).add(ref)
        rs2.learn_obo(Yobo)

        FP = Representation(name="model").set("Y:007", 0.35)
        chain1 = rs2.inference_chain(FP, "ref", verbose=True, fp_penalty=0.1)
        chain1.evaluate_inference()
        self.assertGreater(chain1.posterior, chain1.prior)
        chain2 = rs2.inference_chain(FP, "ref", verbose=True, fp_penalty=1)
        chain2.evaluate_inference()
        self.assertLess(chain2.posterior, chain1.posterior)
Пример #12
0
    def test_inference_chain(self):
        """compute an inference chain."""

        # create a reference set
        refA = Representation(name="refA")
        refA.set("Y:002", 1).impute(Yobo, Ydefaults)
        refB = Representation(name="refB")
        refB.set("Y:001", 1).impute(Yobo, Ydefaults)
        rs = ReferenceSet(dict(refA=0.5, refB=0.5),
                          ids=Yobo.ids(),
                          row_priors=Ydefaults)
        rs.add(refA).add(refB)
        rs.learn_obo(Yobo)

        # compute a chain object explaining scoring steps
        chain = rs.inference_chain(refA, "refB", verbose=True)
        self.assertEqual(chain.__dict__["model"], "refA")
        self.assertEqual(chain.__dict__["reference"], "refB")
        self.assertGreater(len(chain.data), 2,
                           "data chain should describe multiple features")
        self.assertTrue("background" in chain.data[0].__dict__,
                        "chain data have comparison information")
        self.assertTrue("result" in chain.data[0].__dict__,
                        "chain data have TP/FP/etc codes")