Example #1
0
    def test0FromList(self):
        bv1 = DataStructs.SparseBitVect(1000)
        bv2 = DataStructs.SparseBitVect(1000)
        obits = range(0, 1000, 3)

        for bit in obits:
            bv1.SetBit(bit)

        bv2.SetBitsFromList(obits)

        for i in range(1000):
            assert bv1.GetBit(i) == bv2.GetBit(i)

        self.failUnless(bv1 == bv2)
        bv2.SetBit(1)
        self.failUnless(bv1 != bv2)
        bv2.UnSetBit(1)
        self.failUnless(bv1 == bv2)

        bv1 = DataStructs.ExplicitBitVect(1000)
        bv2 = DataStructs.ExplicitBitVect(1000)
        obits = range(0, 1000, 3)

        for bit in obits:
            bv1.SetBit(bit)

        bv2.SetBitsFromList(obits)

        for i in range(1000):
            assert bv1.GetBit(i) == bv2.GetBit(i)
Example #2
0
    def test1FromList(self):
        examples = []

        bv = DS.SparseBitVect(5)
        bv.SetBitsFromList([0, 2, 4])
        examples.append([0, bv, 0])

        bv = DS.SparseBitVect(5)
        bv.SetBitsFromList([0, 2, 4])
        examples.append([0, bv, 0])

        bv = DS.SparseBitVect(5)
        bv.SetBitsFromList([0, 3, 4])
        examples.append([0, bv, 1])

        bv = DS.SparseBitVect(5)
        bv.SetBitsFromList([0, 2, 4])
        examples.append([0, bv, 0])

        bv = DS.SparseBitVect(5)
        bv.SetBitsFromList([0, 2])
        examples.append([0, bv, 1])

        r = FS.selectCMIM(examples, 2)
        self.failUnlessEqual(r, (2, 4))

        r = FS.selectCMIM(examples, 1)
        self.failUnlessEqual(r, (2, ))

        r = FS.selectCMIM(examples, 3)
        self.failUnlessEqual(r, (2, 4, -1))
Example #3
0
 def test2ranker(self):
     nbits = 100
     ninst = 100
     dm = 50
     nact = 10
     nc = 2
     RDRandom.seed(23)
     rn = rdit.InfoBitRanker(nbits, nc, rdit.InfoType.ENTROPY)
     rn.SetMaskBits([63, 70, 15, 25, 10])
     fps = []
     na = 0
     ni = 0
     for i in range(ninst):
         v = DataStructs.SparseBitVect(nbits)
         for j in range(dm):
             v.SetBit(RDRandom.randrange(0, nbits))
         if (RDRandom.randrange(0, ninst) < nact):
             na += 1
             rn.AccumulateVotes(v, 1)
             fps.append((v, 1))
         else:
             ni += 1
             rn.AccumulateVotes(v, 0)
             fps.append((v, 0))
     res = rn.GetTopN(5)
     ids = [int(x[0]) for x in res]
     ids.sort()
     self.assertTrue(ids == [10, 15, 25, 63, 70])
     with self.assertRaisesRegexp(Exception, ""):
         res = rn.GetTopN(10)
Example #4
0
def GetAtomPairFingerprintAsBitVect(mol):
    """ Returns the Atom-pair fingerprint for a molecule as
  a SparseBitVect. Note that this doesn't match the standard
  definition of atom pairs, which uses counts of the
  pairs, not just their presence.

  **Arguments**:

    - mol: a molecule

  **Returns**: a SparseBitVect

  >>> m = Chem.MolFromSmiles('CCC')
  >>> v = [ pyScorePair(m.GetAtomWithIdx(0),m.GetAtomWithIdx(1),1),
  ...       pyScorePair(m.GetAtomWithIdx(0),m.GetAtomWithIdx(2),2),
  ...     ]
  >>> v.sort()
  >>> fp = GetAtomPairFingerprintAsBitVect(m)
  >>> list(fp.GetOnBits())==v
  True
  
  """
    res = DataStructs.SparseBitVect(fpLen)
    fp = rdMolDescriptors.GetAtomPairFingerprint(mol)
    for val in fp.GetNonzeroElements().keys():
        res.SetBit(val)
    return res
Example #5
0
    def test1SparsePickle(self):
        nbits = 10000
        bv1 = DataStructs.SparseBitVect(nbits)
        for i in range(1000):
            x = random.randrange(0, nbits)
            bv1.SetBit(x)

        pkl = pickle.dumps(bv1, 1)
        bv2 = pickle.loads(pkl)
        for i in range(nbits):
            assert bv1[i] == bv2[i]
Example #6
0
    def test5sbv(self):

        n = 30
        m = 2048
        dm = 800
        lst = []
        for i in range(n):
            v = DataStructs.SparseBitVect(m)
            for j in range(dm):
                v.SetBit(random.randrange(0, m))
            lst.append(v)

        dMat = rdmmc.GetTanimotoDistMat(lst)

        sMat = rdmmc.GetTanimotoSimMat(lst)

        for i in range(n * (n - 1) // 2):
            assert feq(sMat[i] + dMat[i], 1.0)
Example #7
0
    def test1ranker(self):
        nbits = 100
        ninst = 100
        dm = 50
        nact = 10
        nc = 2
        rn = rdit.InfoBitRanker(nbits, nc, rdit.InfoType.ENTROPY)
        fps = []
        na = 0
        ni = 0
        for i in range(ninst):
            v = DataStructs.SparseBitVect(nbits)
            for j in range(dm):
                v.SetBit(RDRandom.randrange(0, nbits))

            if (RDRandom.randrange(0, ninst) < nact):
                na += 1
                rn.AccumulateVotes(v, 1)
                fps.append((v, 1))
            else:
                ni += 1
                rn.AccumulateVotes(v, 0)
                fps.append((v, 0))

        res = rn.GetTopN(50)

        rn2 = rdit.InfoBitRanker(nbits, nc)
        for fp in fps:
            rn2.AccumulateVotes(fp[0], fp[1])

        res2 = rn2.GetTopN(50)
        self.assertTrue((res == res2).all())

        rn3 = rdit.InfoBitRanker(nbits, nc, rdit.InfoType.BIASENTROPY)
        #rn3.SetBiasList([0])
        for fp in fps:
            rn3.AccumulateVotes(fp[0], fp[1])

        res3 = rn3.GetTopN(50)
        for i in range(50):
            fan = res3[i, 2] / na
            fin = res3[i, 3] / ni
            self.assertTrue(fan > fin)