| 1 |
|
| 2 |
import sys |
| 3 |
import numpy |
| 4 |
import multiprocessing |
| 5 |
|
| 6 |
import csb.test as test |
| 7 |
import csb.bio.utils as cbu |
| 8 |
import csb.io |
| 9 |
|
| 10 |
|
| 11 |
X1 = numpy.array([ |
| 12 |
[ 0., 0., 0.], |
| 13 |
[ 1., 0., 0.], |
| 14 |
[ 0., 1., 0.]]) |
| 15 |
|
| 16 |
X2 = numpy.array([ |
| 17 |
[ 0., 0., 0.], |
| 18 |
[ 1., 2., 0.], |
| 19 |
[-2., -1., 0.]]) |
| 20 |
|
| 21 |
X3 = numpy.array([ |
| 22 |
[ 0., 0., 0.], |
| 23 |
[ 2., -1., 0.], |
| 24 |
[-1., 2., 0.]]) |
| 25 |
|
| 26 |
RZ = numpy.array([ |
| 27 |
[ 0., 1., 0.], |
| 28 |
[-1., 0., 0.], |
| 29 |
[ 0., 0., 1.]]) |
| 30 |
|
| 31 |
X4 = numpy.array([ |
| 32 |
[ 0., 0., 0.], |
| 33 |
[ 1., 0., 0.], |
| 34 |
[ 0., 1., 0.], |
| 35 |
[ 1., 1., 0.]]) |
| 36 |
|
| 37 |
X5 = numpy.array([ |
| 38 |
[ 0., 0., 0.], |
| 39 |
[ 100., 0., 0.], |
| 40 |
[ 0., 100., 0.], |
| 41 |
[ 50., 50., 0.]]) |
| 42 |
|
| 43 |
X6 = numpy.array([ |
| 44 |
[ 0., 0., 0.], |
| 45 |
[ 100., 0., 0.], |
| 46 |
[ 0., 100., 0.], |
| 47 |
[ 60., 60., 0.]]) |
| 48 |
|
| 49 |
X7 = numpy.array([ |
| 50 |
[ 0., 0., 0.], |
| 51 |
[ 100., 0., 0.], |
| 52 |
[ 0., 100., 0.], |
| 53 |
[ 0., 0., 100.]]) |
| 54 |
|
| 55 |
|
| 56 |
@test.regression |
| 57 |
class Regressions(test.Case): |
| 58 |
|
| 59 |
def _timeoutTest(self): |
| 60 |
cbu.tm_superimpose([[1, 1, 1]], [[1, 1, 1]]) |
| 61 |
|
| 62 |
def _multiprocessingTest(self): |
| 63 |
return True |
| 64 |
|
| 65 |
def _runProcess(self, target, timeout=1.0): |
| 66 |
|
| 67 |
p = multiprocessing.Process(target=target) |
| 68 |
p.start() |
| 69 |
p.join(timeout=timeout) |
| 70 |
|
| 71 |
return p |
| 72 |
|
| 73 |
@test.skip("n/a on this platform", sys.platform.startswith('win')) |
| 74 |
def testTMSuperimpose(self): |
| 75 |
""" |
| 76 |
@see: [CSB 0000058] |
| 77 |
""" |
| 78 |
try: |
| 79 |
self._runProcess(target=self._multiprocessingTest) |
| 80 |
except: |
| 81 |
self.skipTest("may produce a false positive") |
| 82 |
|
| 83 |
p = self._runProcess(target=self._timeoutTest, timeout=5.0) |
| 84 |
|
| 85 |
if p.is_alive(): |
| 86 |
p.terminate() |
| 87 |
self.fail('timeout expired') |
| 88 |
|
| 89 |
def testRmsdMirrorImage(self): |
| 90 |
X, Y = X7, X7.copy() |
| 91 |
Y[:, 0] *= -1 |
| 92 |
rmsd = cbu.rmsd(X, Y) |
| 93 |
|
| 94 |
self.assertAlmostEqual(rmsd, 50.0) |
| 95 |
|
| 96 |
|
| 97 |
@test.functional |
| 98 |
class TestUtils(test.Case): |
| 99 |
|
| 100 |
def assertArrayEqual(self, first, second, eps=1e-7): |
| 101 |
diff = numpy.asarray(first) - numpy.asarray(second) |
| 102 |
self.assertTrue((abs(diff) < eps).all()) |
| 103 |
|
| 104 |
def testFit(self): |
| 105 |
R, t = cbu.fit(X1, X2) |
| 106 |
Y = numpy.dot(X2, R.T) + t |
| 107 |
|
| 108 |
self.assertArrayEqual(R, RZ) |
| 109 |
self.assertArrayEqual(t, [0., 0., 0.]) |
| 110 |
self.assertArrayEqual(Y, X3) |
| 111 |
|
| 112 |
def testWFit(self): |
| 113 |
w = numpy.array([1., 1., 0.]) |
| 114 |
R, t = cbu.wfit(X1, X2, w) #@UnusedVariable |
| 115 |
|
| 116 |
d = 5.0**0.5 |
| 117 |
self.assertArrayEqual(t, [-d / 2.0 + 0.5, 0., 0.]) |
| 118 |
|
| 119 |
def testScaleAndFit(self): |
| 120 |
R, t, s = cbu.scale_and_fit(2.0 * X1, X1) |
| 121 |
|
| 122 |
self.assertArrayEqual(R, numpy.identity(3)) |
| 123 |
self.assertArrayEqual(t, [0., 0., 0.]) |
| 124 |
self.assertAlmostEqual(s, 2.0) |
| 125 |
|
| 126 |
def testFitWellordered(self): |
| 127 |
R, t = cbu.fit_wellordered(X5, X6, 10, 1.0) #@UnusedVariable |
| 128 |
|
| 129 |
self.assertArrayEqual(t, [0., 0., 0.]) |
| 130 |
|
| 131 |
def testRmsd(self): |
| 132 |
rmsd = cbu.rmsd(X1, X2) |
| 133 |
|
| 134 |
self.assertAlmostEqual(rmsd, (4./3.)**0.5) |
| 135 |
|
| 136 |
def testWrmsd(self): |
| 137 |
w = numpy.array([1., 1., 0.]) |
| 138 |
rmsd = cbu.wrmsd(X1, X2, w) |
| 139 |
|
| 140 |
d = 5.0**0.5 |
| 141 |
self.assertAlmostEqual(rmsd, d / 2.0 - 0.5) |
| 142 |
|
| 143 |
def testTorsionRmsd(self): |
| 144 |
rmsd = cbu.torsion_rmsd(X1[:,:2], X1[:,:2]) |
| 145 |
|
| 146 |
self.assertAlmostEqual(rmsd, 0.0) |
| 147 |
|
| 148 |
def testTmScore(self): |
| 149 |
score = cbu.tm_score(X1, X3) |
| 150 |
|
| 151 |
self.assertAlmostEqual(score, 0.4074, 4) |
| 152 |
|
| 153 |
def testTmSuperimpose(self): |
| 154 |
R, t, score = cbu.tm_superimpose(X1, X2) #@UnusedVariable |
| 155 |
|
| 156 |
self.assertAlmostEqual(score, 0.4074, 4) |
| 157 |
|
| 158 |
def testCenterOfMass(self): |
| 159 |
com = cbu.center_of_mass(X4) |
| 160 |
|
| 161 |
self.assertArrayEqual(com, [0.5, 0.5, 0.0]) |
| 162 |
|
| 163 |
def testRadiusOfGyration(self): |
| 164 |
gyradius = cbu.radius_of_gyration(X4) |
| 165 |
|
| 166 |
s2 = 2.0**0.5 |
| 167 |
self.assertArrayEqual(gyradius, s2 / 2.0) |
| 168 |
|
| 169 |
def testSecondMoments(self): |
| 170 |
sm = cbu.second_moments(X1) |
| 171 |
|
| 172 |
# TODO: correct? |
| 173 |
sm_test = numpy.array([ |
| 174 |
[ 2./3., -1./3., 0. ], |
| 175 |
[-1./3., 2./3., 0. ], |
| 176 |
[ 0., 0., 0. ]]) |
| 177 |
self.assertArrayEqual(sm, sm_test) |
| 178 |
|
| 179 |
def testInertiaTensor(self): |
| 180 |
it = cbu.inertia_tensor(X1) |
| 181 |
|
| 182 |
# TODO: correct? |
| 183 |
it_test = numpy.array([ |
| 184 |
[ 2./3., 1./3., 0. ], |
| 185 |
[ 1./3., 2./3., 0. ], |
| 186 |
[ 0., 0., 4./3. ]]) |
| 187 |
self.assertArrayEqual(it, it_test) |
| 188 |
|
| 189 |
def testFindPairs(self): |
| 190 |
pairs11 = list(cbu.find_pairs(1.2, X1)) |
| 191 |
pairs12 = list(cbu.find_pairs(1.2, X1, X2)) |
| 192 |
|
| 193 |
self.assertEqual(len(pairs11), 2) |
| 194 |
self.assertEqual(len(pairs12), 3) |
| 195 |
|
| 196 |
def testDistanceMatrix(self): |
| 197 |
d = cbu.distance_matrix(X1) |
| 198 |
|
| 199 |
s2 = 2.0**0.5 |
| 200 |
d_test = [ |
| 201 |
[ 0., 1., 1. ], |
| 202 |
[ 1., 0., s2 ], |
| 203 |
[ 1., s2, 0. ]] |
| 204 |
self.assertArrayEqual(d, d_test) |
| 205 |
|
| 206 |
def testDistance(self): |
| 207 |
d = cbu.distance(X1, X2) |
| 208 |
|
| 209 |
self.assertEqual(d.shape, (len(X1),)) |
| 210 |
self.assertArrayEqual(d[:2], [0., 2.]) |
| 211 |
|
| 212 |
def testRmsdCur(self): |
| 213 |
rmsd = cbu.rmsd_cur(X1, X2) |
| 214 |
|
| 215 |
self.assertAlmostEqual(rmsd, 2.0) |
| 216 |
|
| 217 |
|
| 218 |
def testRad(self): |
| 219 |
|
| 220 |
converted = cbu.rad([[0, 90, 180]])[0] |
| 221 |
|
| 222 |
self.assertEqual(converted[0], 0) |
| 223 |
self.assertAlmostEqual(converted[1], numpy.pi / 2.0) |
| 224 |
self.assertAlmostEqual(converted[2], numpy.pi) |
| 225 |
|
| 226 |
def testDeg(self): |
| 227 |
|
| 228 |
converted = cbu.deg([[0, numpy.pi / 2.0, numpy.pi]])[0] |
| 229 |
|
| 230 |
self.assertEqual(converted[0], 0) |
| 231 |
self.assertAlmostEqual(converted[1], 90) |
| 232 |
self.assertAlmostEqual(converted[2], 180) |
| 233 |
|
| 234 |
|
| 235 |
if __name__ == '__main__': |
| 236 |
|
| 237 |
test.Console() |
| 238 |
|