File: test_fit.py

package info (click to toggle)
mdanalysis 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,696 kB
  • sloc: python: 92,135; ansic: 8,156; makefile: 215; sh: 138
file content (304 lines) | stat: -rw-r--r-- 10,035 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8
#
# MDAnalysis --- https://www.mdanalysis.org
# Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors
# (see the file AUTHORS for the full list of names)
#
# Released under the Lesser GNU Public Licence, v2.1 or any higher version
#
# Please cite your use of MDAnalysis in published work:
#
# R. J. Gowers, M. Linke, J. Barnoud, T. J. E. Reddy, M. N. Melo, S. L. Seyler,
# D. L. Dotson, J. Domanski, S. Buchoux, I. M. Kenney, and O. Beckstein.
# MDAnalysis: A Python package for the rapid analysis of molecular dynamics
# simulations. In S. Benthall and S. Rostrup editors, Proceedings of the 15th
# Python in Science Conference, pages 102-109, Austin, TX, 2016. SciPy.
#
# N. Michaud-Agrawal, E. J. Denning, T. B. Woolf, and O. Beckstein.
# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations.
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#
import numpy as np
import pytest
from numpy.testing import assert_array_almost_equal

from MDAnalysisTests import make_Universe
from MDAnalysis.transformations.fit import fit_translation, fit_rot_trans
from MDAnalysis.lib.transformations import rotation_matrix


@pytest.fixture()
def fit_universe():
    # make a test universe
    test = make_Universe(("masses",), trajectory=True)
    ref = make_Universe(("masses",), trajectory=True)
    ref.atoms.positions += np.asarray([10, 10, 10], np.float32)
    return test, ref


@pytest.mark.parametrize(
    "universe",
    (
        [0, 1],
        [0, 1, 2, 3, 4],
        np.array([0, 1]),
        np.array([0, 1, 2, 3, 4]),
        np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]),
        np.array([[0], [1], [2]]),
        "thisisnotanag",
        1,
    ),
)
def test_fit_translation_bad_ag(fit_universe, universe):
    ts = fit_universe[0].trajectory.ts
    test_u = fit_universe[0]
    ref_u = fit_universe[1]
    # what happens if something other than an AtomGroup or Universe is given?
    with pytest.raises(AttributeError):
        fit_translation(universe, ref_u)(ts)


@pytest.mark.parametrize(
    "weights",
    (
        " ",
        "totallynotmasses",
        123456789,
        [0, 1, 2, 3, 4],
        np.array([0, 1]),
        np.array([0, 1, 2, 3, 4]),
        np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]),
    ),
)
def test_fit_translation_bad_weights(fit_universe, weights):
    ts = fit_universe[0].trajectory.ts
    test_u = fit_universe[0]
    ref_u = fit_universe[1]
    # what happens if a bad string for center is given?
    with pytest.raises(ValueError):
        fit_translation(test_u, ref_u, weights=weights)(ts)


@pytest.mark.parametrize(
    "plane", (1, [0, 1], [0, 1, 2, 3, 4], np.array([0, 1]), "xyz", "notaplane")
)
def test_fit_translation_bad_plane(fit_universe, plane):
    ts = fit_universe[0].trajectory.ts
    test_u = fit_universe[0]
    ref_u = fit_universe[1]
    # what happens if a bad string for center is given?
    with pytest.raises(ValueError):
        fit_translation(test_u, ref_u, plane=plane)(ts)


def test_fit_translation_no_masses(fit_universe):
    ts = fit_universe[0].trajectory.ts
    test_u = fit_universe[0]
    # create a universe without masses
    ref_u = make_Universe()
    # what happens Universe without masses is given?
    with pytest.raises(TypeError) as exc:
        fit_translation(test_u, ref_u, weights="mass")(ts)
    assert "atoms.masses is missing" in str(exc.value)


def test_fit_translation_no_options(fit_universe):
    test_u = fit_universe[0]
    ref_u = fit_universe[1]
    fit_translation(test_u, ref_u)(test_u.trajectory.ts)
    # what happens when no options are passed?
    assert_array_almost_equal(
        test_u.trajectory.ts.positions,
        ref_u.trajectory.ts.positions,
        decimal=6,
    )


def test_fit_translation_residue_mismatch(fit_universe):
    test_u = fit_universe[0]
    ref_u = fit_universe[1].residues[:-1].atoms
    with pytest.raises(ValueError, match="number of residues"):
        fit_translation(test_u, ref_u)(test_u.trajectory.ts)


def test_fit_translation_com(fit_universe):
    test_u = fit_universe[0]
    ref_u = fit_universe[1]
    fit_translation(test_u, ref_u, weights="mass")(test_u.trajectory.ts)
    # what happens when the center o mass is used?
    assert_array_almost_equal(
        test_u.trajectory.ts.positions,
        ref_u.trajectory.ts.positions,
        decimal=6,
    )


@pytest.mark.parametrize("plane", ("yz", "xz", "xy"))
def test_fit_translation_plane(fit_universe, plane):
    test_u = fit_universe[0]
    ref_u = fit_universe[1]
    axes = {"yz": 0, "xz": 1, "xy": 2}
    idx = axes[plane]
    # translate the test universe on the plane coordinates only
    fit_translation(test_u, ref_u, plane=plane)(test_u.trajectory.ts)
    # the reference is 10 angstrom in all coordinates above the test universe
    shiftz = np.asanyarray([0, 0, 0], np.float32)
    shiftz[idx] = -10
    ref_coordinates = ref_u.trajectory.ts.positions + shiftz
    assert_array_almost_equal(
        test_u.trajectory.ts.positions, ref_coordinates, decimal=6
    )


def test_fit_translation_all_options(fit_universe):
    test_u = fit_universe[0]
    ref_u = fit_universe[1]
    # translate the test universe on the x and y coordinates only
    fit_translation(test_u, ref_u, plane="xy", weights="mass")(
        test_u.trajectory.ts
    )
    # the reference is 10 angstrom in the z coordinate above the test universe
    shiftz = np.asanyarray([0, 0, -10], np.float32)
    ref_coordinates = ref_u.trajectory.ts.positions + shiftz
    assert_array_almost_equal(
        test_u.trajectory.ts.positions, ref_coordinates, decimal=6
    )


def test_fit_translation_transformations_api(fit_universe):
    test_u = fit_universe[0]
    ref_u = fit_universe[1]
    transform = fit_translation(test_u, ref_u)
    test_u.trajectory.add_transformations(transform)
    assert_array_almost_equal(
        test_u.trajectory.ts.positions,
        ref_u.trajectory.ts.positions,
        decimal=6,
    )


@pytest.mark.parametrize(
    "universe",
    (
        [0, 1],
        [0, 1, 2, 3, 4],
        np.array([0, 1]),
        np.array([0, 1, 2, 3, 4]),
        np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]),
        np.array([[0], [1], [2]]),
        "thisisnotanag",
        1,
    ),
)
def test_fit_rot_trans_bad_universe(fit_universe, universe):
    test_u = fit_universe[0]
    ref_u = universe
    with pytest.raises(AttributeError):
        fit_rot_trans(test_u, ref_u)(test_u.trajectory.ts)


def test_fit_rot_trans_shorter_universe(fit_universe):
    ref_u = fit_universe[1]
    bad_u = fit_universe[0].atoms[0:5]
    test_u = bad_u
    with pytest.raises(ValueError):
        fit_rot_trans(test_u, ref_u)(test_u.trajectory.ts)


@pytest.mark.parametrize(
    "weights",
    (
        " ",
        "totallynotmasses",
        123456789,
        [0, 1, 2, 3, 4],
        np.array([0, 1]),
        np.array([0, 1, 2, 3, 4]),
        np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]),
    ),
)
def test_fit_rot_trans_bad_weights(fit_universe, weights):
    test_u = fit_universe[0]
    ref_u = fit_universe[1]
    bad_weights = weights
    with pytest.raises(ValueError):
        fit_rot_trans(test_u, ref_u, weights=bad_weights)(test_u.trajectory.ts)


@pytest.mark.parametrize(
    "plane",
    (
        " ",
        "totallynotaplane",
        "xyz",
        123456789,
        [0, 1, 2, 3, 4],
        np.array([0, 1]),
        np.array([0, 1, 2, 3, 4]),
        np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]),
    ),
)
def test_fit_rot_trans_bad_plane(fit_universe, plane):
    test_u = fit_universe[0]
    ref_u = fit_universe[1]
    with pytest.raises(ValueError):
        fit_rot_trans(test_u, ref_u, plane=plane)(test_u.trajectory.ts)


def test_fit_rot_trans_no_options(fit_universe):
    test_u = fit_universe[0]
    ref_u = fit_universe[1]
    ref_com = ref_u.atoms.center(None)
    ref_u.trajectory.ts.positions -= ref_com
    R = rotation_matrix(np.pi / 3, [1, 0, 0])[:3, :3]
    ref_u.trajectory.ts.positions = np.dot(ref_u.trajectory.ts.positions, R)
    ref_u.trajectory.ts.positions += ref_com
    fit_rot_trans(test_u, ref_u)(test_u.trajectory.ts)
    assert_array_almost_equal(
        test_u.trajectory.ts.positions,
        ref_u.trajectory.ts.positions,
        decimal=3,
    )


@pytest.mark.parametrize("plane", ("yz", "xz", "xy"))
def test_fit_rot_trans_plane(fit_universe, plane):
    # the reference is rotated in the x axis so removing the translations and rotations
    # in the yz plane should return the same as the fitting without specifying a plane
    test_u = fit_universe[0]
    ref_u = fit_universe[1]
    ref_com = ref_u.atoms.center(None)
    mobile_com = test_u.atoms.center(None)
    axes = {"yz": 0, "xz": 1, "xy": 2}
    idx = axes[plane]
    rotaxis = np.asarray([0, 0, 0])
    rotaxis[idx] = 1
    ref_u.trajectory.ts.positions -= ref_com
    R = rotation_matrix(np.pi / 3, rotaxis)[:3, :3]
    ref_u.trajectory.ts.positions = np.dot(ref_u.trajectory.ts.positions, R)
    ref_com[idx] = mobile_com[idx]
    ref_u.trajectory.ts.positions += ref_com
    fit_rot_trans(test_u, ref_u, plane=plane)(test_u.trajectory.ts)
    assert_array_almost_equal(
        test_u.trajectory.ts.positions[:, idx],
        ref_u.trajectory.ts.positions[:, idx],
        decimal=3,
    )


def test_fit_rot_trans_transformations_api(fit_universe):
    test_u = fit_universe[0]
    ref_u = fit_universe[1]
    ref_com = ref_u.atoms.center(None)
    ref_u.trajectory.ts.positions -= ref_com
    R = rotation_matrix(np.pi / 3, [1, 0, 0])[:3, :3]
    ref_u.trajectory.ts.positions = np.dot(ref_u.trajectory.ts.positions, R)
    ref_u.trajectory.ts.positions += ref_com
    transform = fit_rot_trans(test_u, ref_u)
    test_u.trajectory.add_transformations(transform)
    assert_array_almost_equal(
        test_u.trajectory.ts.positions,
        ref_u.trajectory.ts.positions,
        decimal=3,
    )