File: UnitTestBitEnsemble.py

package info (click to toggle)
rdkit 202009.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 129,624 kB
  • sloc: cpp: 288,030; python: 75,571; java: 6,999; ansic: 5,481; sql: 1,968; yacc: 1,842; lex: 1,254; makefile: 572; javascript: 461; xml: 229; fortran: 183; sh: 134; cs: 93
file content (141 lines) | stat: -rwxr-xr-x 4,181 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# $Id$
#
# Copyright (C) 2003-2006 greg Landrum and Rational Discovery LLC
#
#   @@ All Rights Reserved @@
#  This file is part of the RDKit.
#  The contents are covered by the terms of the BSD license
#  which is included in the file license.txt, found at the root
#  of the RDKit source tree.
#
""" unit testing code for BitEnsembles
"""
import os
import shutil
import tempfile
import unittest

from rdkit import RDConfig
from rdkit.DataStructs import SparseBitVect
# This import is important to initialize the BitEnsemble module
from rdkit.DataStructs import BitEnsembleDb
from rdkit.DataStructs.BitEnsemble import BitEnsemble


class TestCase(unittest.TestCase):

  def test1(self):
    ensemble = BitEnsemble()
    ensemble.SetBits([1, 11, 21, 31])
    self.assertEqual(ensemble.GetNumBits(), 4)
    bv = SparseBitVect(100)
    bv.SetBit(1)
    bv.SetBit(11)
    bv.SetBit(13)

    score = ensemble.ScoreWithOnBits(bv)
    assert score == 2, 'bad score: %d' % (score)
    score = ensemble.ScoreWithIndex(bv)
    assert score == 2, 'bad score: %d' % (score)

  def test2(self):
    ensemble = BitEnsemble([1, 11, 21, 31])
    bv = SparseBitVect(100)
    bv.SetBit(1)
    bv.SetBit(11)
    bv.SetBit(13)

    score = ensemble.ScoreWithOnBits(bv)
    assert score == 2, 'bad score: %d' % (score)
    score = ensemble.ScoreWithIndex(bv)
    assert score == 2, 'bad score: %d' % (score)

  def test3(self):
    ensemble = BitEnsemble()
    for bit in [1, 11, 21, 31]:
      ensemble.AddBit(bit)
    bv = SparseBitVect(100)
    bv.SetBit(1)
    bv.SetBit(11)
    bv.SetBit(13)

    score = ensemble.ScoreWithOnBits(bv)
    assert score == 2, 'bad score: %d' % (score)
    score = ensemble.ScoreWithIndex(bv)
    assert score == 2, 'bad score: %d' % (score)

  def _setupDb(self):
    from rdkit.Dbase.DbConnection import DbConnect
    fName = RDConfig.RDTestDatabase
    if RDConfig.useSqlLite:
      _, tempName = tempfile.mkstemp(suffix='sqlt')
      self.tempDbName = tempName
      shutil.copyfile(fName, tempName)
    else:  # pragma: nocover
      tempName = '::RDTests'
    self.conn = DbConnect(tempName)
    self.dbTblName = 'bit_ensemble_test'
    return self.conn

  def tearDown(self):
    if hasattr(self, 'tempDbName') and RDConfig.useSqlLite and os.path.exists(self.tempDbName):
      try:
        os.unlink(self.tempDbName)
      except:  # pragma: nocover
        import traceback
        traceback.print_exc()

  def testdb1(self):
    """ test the sig - db functionality """
    conn = self._setupDb()
    ensemble = BitEnsemble()
    for bit in [1, 3, 4]:
      ensemble.AddBit(bit)

    sigBs = [([0, 0, 0, 0, 0, 0], (0, 0, 0)),
             ([0, 1, 0, 1, 0, 0], (1, 1, 0)),
             ([0, 1, 0, 0, 1, 0], (1, 0, 1)),
             ([0, 1, 0, 0, 1, 1], (1, 0, 1)), ]
    ensemble.InitScoreTable(conn, self.dbTblName)
    for bs, tgt in sigBs:
      ensemble.ScoreToDb(bs, conn)
    conn.Commit()

    d = conn.GetData(table=self.dbTblName)
    assert len(d) == len(sigBs), 'bad number of results returned'
    for i in range(len(sigBs)):
      bs, tgt = tuple(sigBs[i])
      dbRes = tuple(d[i])
      assert dbRes == tgt, 'bad bits returned: %s != %s' % (str(dbRes), str(tgt))
    d = None
    self.conn = None

  def testdb2(self):
    """ test the sig - db functionality """
    conn = self._setupDb()
    ensemble = BitEnsemble()
    for bit in [1, 3, 4]:
      ensemble.AddBit(bit)

    sigBs = [([0, 0, 0, 0, 0, 0], (0, 0, 0)),
             ([0, 1, 0, 1, 0, 0], (1, 1, 0)),
             ([0, 1, 0, 0, 1, 0], (1, 0, 1)),
             ([0, 1, 0, 0, 1, 1], (1, 0, 1)), ]
    ensemble.InitScoreTable(conn, self.dbTblName, idInfo='id varchar(10)', actInfo='act int')
    for bs, tgt in sigBs:
      ensemble.ScoreToDb(bs, conn, id='foo', act=1)
    conn.Commit()

    d = conn.GetData(table=self.dbTblName)
    assert len(d) == len(sigBs), 'bad number of results returned'
    for i in range(len(sigBs)):
      bs, tgt = tuple(sigBs[i])
      dbRes = tuple(d[i])
      assert dbRes[1:-1] == tgt, 'bad bits returned: %s != %s' % (str(dbRes[1:-1]), str(tgt))

    d = None
    self.conn = None


if __name__ == '__main__':  # pragma: nocover
  unittest.main()