File: rough_test.py

package info (click to toggle)
rdkit 201603.5-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 72,364 kB
  • ctags: 18,217
  • sloc: cpp: 167,966; python: 58,855; java: 5,318; ansic: 5,239; sql: 1,908; yacc: 1,553; lex: 1,131; makefile: 418; xml: 229; sh: 192; fortran: 183; cs: 93
file content (123 lines) | stat: -rw-r--r-- 3,889 bytes parent folder | download
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
"""
## Automatically adapted for numpy.oldnumeric Jun 27, 2008 by -c

# $Id$
#
#  Copyright (C) 2004  Rational Discovery LLC
#         All Rights Reserved
#

Replaced numpy.oldnumeric with numpy methods - Jan 2015, PGedeck
"""
#pylint: disable=E1101,C0111,R0904
import unittest
import numpy as np
from rdkit import DistanceGeometry as DG

def feq(v1, v2, tol2=1e-4):
  return abs(v1 - v2) <= tol2

class TestCase(unittest.TestCase):
  def setUp(self):
    pass

  def test1SmoothPass(self):
    arr = np.array([[0, 1.0, 5.0],
                    [1.0, 0, 1.0],
                    [0.0, 1.0, 0]], np.float)
    self.assertTrue(DG.DoTriangleSmoothing(arr))
    self.assertTrue(feq(arr[0, 2], 2.0))
    self.assertTrue(feq(arr[2, 0], 0.0))
    self.assertTrue(feq(arr[0, 1], 1.0))
    self.assertTrue(feq(arr[1, 0], 1.0))
    self.assertTrue(feq(arr[1, 2], 1.0))
    
  def test2SmoothFail(self):
    arr = np.array([[0, 1.0, 5.0],
                    [1.0, 0, 1.0],
                    [3.0, 1.0, 0]], np.float)
    self.assertFalse(DG.DoTriangleSmoothing(arr))
    
    
  def test3SmoothPass(self):
    arr = np.array([[0, 1.1, 5.0],
                    [0.9, 0, 1.1],
                    [0.0, 0.9, 0]], np.float)
    self.assertTrue(DG.DoTriangleSmoothing(arr))
    self.assertTrue(feq(arr[0, 2], 2.2))
    self.assertTrue(feq(arr[2, 0], 0.0))
    self.assertTrue(feq(arr[0, 1], 1.1))
    self.assertTrue(feq(arr[1, 0], 0.9))
    self.assertTrue(feq(arr[1, 2], 1.1))
    
  
  def test4Embed(self):
    arr = np.array([[0, 1.0, 5.0],
                    [1.0, 0, 1.0],
                    [0.0, 1.0, 0]], np.float)
    self.assertTrue(DG.DoTriangleSmoothing(arr))
    coords = DG.EmbedBoundsMatrix(arr, randomSeed=100)
    v1 = coords[0] - coords[1]
    v2 = coords[1] - coords[2]
    d1 = np.dot(v1, v1)
    self.assertTrue(feq(d1, 1.0, 0.001))
    d2 = np.dot(v2, v2)
    self.assertTrue(feq(d2, 1.0, 0.001))
    
  def test5EmbedFail(self):
    arr = np.array([[0, 1.0, 5.0],
                    [1.0, 0, 1.0],
                    [3.0, 1.0, 0]], np.float)
    self.assertRaises(ValueError, lambda : DG.EmbedBoundsMatrix(arr))
    #DG.EmbedBoundsMatrix(arr,randomizeOnFailure=0,randomSeed=1)
    DG.EmbedBoundsMatrix(arr, randomizeOnFailure=1)

  def test6EmbedConstraints(self):
    arr = np.array([[0.0, 1.0, 1.0],
                    [1.0, 0.0, 1.0],
                    [0.99, 1.0, 0.0]], np.float)
    self.assertTrue(DG.DoTriangleSmoothing(arr))
    coords = DG.EmbedBoundsMatrix(arr, randomSeed=100)
    v1 = coords[0] - coords[1]
    v2 = coords[1] - coords[2]
    d1 = np.dot(v1, v1)
    
    self.assertTrue(feq(d1, 1.0, 2e-3))
    d2 = np.dot(v2, v2)
    self.assertTrue(feq(d2, 1.0, 2e-3))
    arr = np.array([[0.0, 1.0, 1.0, 1.01],
                    [1.0, 0.0, 1.0, 1.0],
                    [1.0, 1.0, 0.0, 1.0],
                    [0.99, 1.0, 1.0, 0.0]], np.float)
    self.assertTrue(DG.DoTriangleSmoothing(arr))
    coords = DG.EmbedBoundsMatrix(arr)
    v1 = coords[0] - coords[1]
    v2 = coords[1] - coords[2]
    d1 = np.dot(v1, v1)
    self.assertTrue(feq(d1, 1.0, 1e-3))
    d2 = np.dot(v2, v2)
    self.assertTrue(feq(d2, 1.0, 1e-3))

    return
    # this test is currently (rev:4769) passing on windows and
    # failing on linux.  It's kind of dependent on fp precision, so
    # it's probably ok to ditch it.
    arr = np.array([[0.0, 1.0, 1.0, 1.0],
                    [1.0, 0.0, 1.0, 1.0],
                    [1.0, 1.0, 0.0, 1.0],
                    [1.0, 1.0, 1.0, 0.0],
                   ], np.float)
    self.assertTrue(DG.DoTriangleSmoothing(arr))
    coords = DG.EmbedBoundsMatrix(arr, randomSeed=100)
    v1 = coords[0] - coords[1]
    v2 = coords[1] - coords[2]
    d1 = np.dot(v1, v1)
    self.assertTrue(feq(d1, 1.0, 1e-3))
    d2 = np.dot(v2, v2)
    self.assertTrue(feq(d2, 1.0, 1e-3))


if __name__ == '__main__':
  unittest.main()