1#
2#  Copyright (C) 2001  greg Landrum
3#
4
5# unit testing code for the composite model COM server
6
7from rdkit import RDConfig
8import unittest
9try:
10  from win32com.client import Dispatch
11except ImportError:
12  Dispatch = None
13
14
15@unittest.skipIf(Dispatch is None, 'Test for Windows only')
16class TestCase(unittest.TestCase):
17
18  def testConnect(self):
19    # connecting to COM server
20    ok = 1
21    try:
22      c = Dispatch('RD.Composite')
23    except Exception:
24      ok = 0
25    assert ok and c is not None, 'connection to COM server failed'
26
27  def testLoad(self):
28    # loading a composite
29    c = Dispatch('RD.Composite')
30    ok = 1
31    try:
32      c.LoadComposite(RDConfig.RDCodeDir + '/ml/composite/test_data/composite_base.pkl')
33    except Exception:
34      ok = 0
35    assert ok, 'LoadComposite failed'
36
37  def testNames(self):
38    # testing descriptor names
39    c = Dispatch('RD.Composite')
40    c.LoadComposite(RDConfig.RDCodeDir + '/ml/composite/test_data/composite_base.pkl')
41    names = c.GetDescriptorNames()
42    expectedNames = ('composition', 'max_atomic', 'has3d', 'has4d', 'has5d', 'elconc', 'atvol',
43                     'isferro')
44    assert names == expectedNames, 'GetDescriptorNames failed'
45
46  def testInputOrder(self):
47    # testing input order
48    c = Dispatch('RD.Composite')
49    c.LoadComposite(RDConfig.RDCodeDir + '/ml/composite/test_data/composite_base.pkl')
50    names = c.GetDescriptorNames()
51    ok = 1
52    try:
53      c.SetInputOrder(names)
54    except Exception:
55      ok = 0
56    assert ok, 'SetInputOrder failed'
57
58  def testClassify(self):
59    # testing classification
60    argV = ['CrPt3', 'fcc', 'AuCu3', 58.09549962, 36, 4, 0.228898, 2.219, 1, 3.67481803894, 1, 0, 1,
61            0.619669341609, 14.523874905]
62    nameV = ['composition', 'Structure', 'Structure_Type', 'Volume', 'Electrons_Per_Unit',
63             'Atoms_Per_Unit', 'Hardness', 'DOS_Ef', 'isferro', 'max_atomic', 'has3d', 'has4d',
64             'has5d', 'elconc', 'atvol']
65    c = Dispatch('RD.Composite')
66    c.LoadComposite(RDConfig.RDCodeDir + '/ml/composite/test_data/composite_base.pkl')
67    c.SetInputOrder(nameV)
68    res = c.ClassifyExample(argV)
69    expected = [1, 1.0]
70    assert res[0] == expected[0], 'bad prediction'
71    assert res[1] == expected[1], 'bad confidence'
72
73
74def TestSuite():  # pragma: nocover
75  suite = unittest.TestSuite()
76  suite.addTest(TestCase('testConnect'))
77  suite.addTest(TestCase('testLoad'))
78  suite.addTest(TestCase('testNames'))
79  suite.addTest(TestCase('testInputOrder'))
80  suite.addTest(TestCase('testClassify'))
81  return suite
82
83
84if __name__ == '__main__':  # pragma: nocover
85  suite = TestSuite()
86  unittest.TextTestRunner().run(suite)
87