File: test_rotate.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 (313 lines) | stat: -rw-r--r-- 10,321 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
305
306
307
308
309
310
311
312
313
# -*- 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.
# doi: 10.25080/majora-629e541a-00e
#
# 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 MDAnalysis as mda
import numpy as np
import pytest
from MDAnalysis.lib.transformations import rotation_matrix
from MDAnalysis.transformations import rotateby
from numpy.testing import assert_array_almost_equal

from MDAnalysisTests import make_Universe


@pytest.fixture()
def rotate_universes():
    # create the Universe objects for the tests
    reference = make_Universe(trajectory=True)
    transformed = make_Universe(["masses"], trajectory=True)
    transformed.trajectory.ts.dimensions = np.array(
        [372.0, 373.0, 374.0, 90, 90, 90]
    )
    return reference, transformed


def test_rotation_matrix():
    # test if the rotation_matrix function is working properly
    # simple angle and unit vector on origin
    angle = 180
    vector = [0, 0, 1]
    pos = [0, 0, 0]
    ref_matrix = np.asarray(
        [
            [-1, 0, 0],
            [0, -1, 0],
            [0, 0, 1],
        ],
        np.float64,
    )
    matrix = rotation_matrix(np.deg2rad(angle), vector, pos)[:3, :3]
    assert_array_almost_equal(matrix, ref_matrix, decimal=6)
    # another angle in a custom axis
    angle = 60
    vector = [1, 2, 3]
    pos = [1, 2, 3]
    ref_matrix = np.asarray(
        [
            [0.53571429, -0.6229365, 0.57005291],
            [0.76579365, 0.64285714, -0.01716931],
            [-0.35576719, 0.44574074, 0.82142857],
        ],
        np.float64,
    )
    matrix = rotation_matrix(np.deg2rad(angle), vector, pos)[:3, :3]
    assert_array_almost_equal(matrix, ref_matrix, decimal=6)


@pytest.mark.parametrize(
    "point", (np.asarray([0, 0, 0]), np.asarray([[0, 0, 0]]))
)
def test_rotateby_custom_point(rotate_universes, point):
    # what happens when we use a custom point for the axis of rotation?
    ref_u = rotate_universes[0]
    trans_u = rotate_universes[1]
    trans = trans_u.trajectory.ts
    ref = ref_u.trajectory.ts
    vector = [1, 0, 0]
    pos = point.reshape(3)
    angle = 90
    matrix = rotation_matrix(np.deg2rad(angle), vector, pos)
    ref_u.atoms.transform(matrix)
    transformed = rotateby(angle, vector, point=point)(trans)
    assert_array_almost_equal(transformed.positions, ref.positions, decimal=6)


@pytest.mark.parametrize(
    "vector", (np.asarray([1, 0, 0]), np.asarray([[1, 0, 0]]))
)
def test_rotateby_vector(rotate_universes, vector):
    # what happens when we use a custom point for the axis of rotation?
    ref_u = rotate_universes[0]
    trans_u = rotate_universes[1]
    trans = trans_u.trajectory.ts
    ref = ref_u.trajectory.ts
    point = [0, 0, 0]
    angle = 90
    vec = vector.reshape(3)
    matrix = rotation_matrix(np.deg2rad(angle), vec, point)
    ref_u.atoms.transform(matrix)
    transformed = rotateby(angle, vector, point=point)(trans)
    assert_array_almost_equal(transformed.positions, ref.positions, decimal=6)


def test_rotateby_atomgroup_cog_nopbc(rotate_universes):
    # what happens when we rotate arround the center of geometry of a residue
    # without pbc?
    ref_u = rotate_universes[0]
    trans_u = rotate_universes[1]
    trans = trans_u.trajectory.ts
    ref = ref_u.trajectory.ts
    center_pos = [6, 7, 8]
    vector = [1, 0, 0]
    angle = 90
    matrix = rotation_matrix(np.deg2rad(angle), vector, center_pos)
    ref_u.atoms.transform(matrix)
    selection = trans_u.residues[0].atoms
    transformed = rotateby(angle, vector, ag=selection, weights=None)(trans)
    assert_array_almost_equal(transformed.positions, ref.positions, decimal=6)


def test_rotateby_atomgroup_com_nopbc(rotate_universes):
    # what happens when we rotate arround the center of mass of a residue
    # without pbc?
    ref_u = rotate_universes[0]
    trans_u = rotate_universes[1]
    trans = trans_u.trajectory.ts
    ref = ref_u.trajectory.ts
    vector = [1, 0, 0]
    angle = 90
    selection = trans_u.residues[0].atoms
    center_pos = selection.center_of_mass()
    matrix = rotation_matrix(np.deg2rad(angle), vector, center_pos)
    ref_u.atoms.transform(matrix)
    transformed = rotateby(angle, vector, ag=selection, weights="mass")(trans)
    assert_array_almost_equal(transformed.positions, ref.positions, decimal=6)


def test_rotateby_atomgroup_cog_pbc(rotate_universes):
    # what happens when we rotate arround the center of geometry of a residue
    # with pbc?
    ref_u = rotate_universes[0]
    trans_u = rotate_universes[1]
    trans = trans_u.trajectory.ts
    ref = ref_u.trajectory.ts
    vector = [1, 0, 0]
    angle = 90
    selection = trans_u.residues[0].atoms
    center_pos = selection.center_of_geometry(pbc=True)
    matrix = rotation_matrix(np.deg2rad(angle), vector, center_pos)
    ref_u.atoms.transform(matrix)
    transformed = rotateby(
        angle, vector, ag=selection, weights=None, wrap=True
    )(trans)
    assert_array_almost_equal(transformed.positions, ref.positions, decimal=6)


def test_rotateby_atomgroup_com_pbc(rotate_universes):
    # what happens when we rotate arround the center of mass of a residue
    # with pbc?
    ref_u = rotate_universes[0]
    trans_u = rotate_universes[1]
    trans = trans_u.trajectory.ts
    ref = ref_u.trajectory.ts
    vector = [1, 0, 0]
    angle = 90
    selection = trans_u.residues[0].atoms
    center_pos = selection.center_of_mass(pbc=True)
    matrix = rotation_matrix(np.deg2rad(angle), vector, center_pos)
    ref_u.atoms.transform(matrix)
    transformed = rotateby(
        angle, vector, ag=selection, weights="mass", wrap=True
    )(trans)
    assert_array_almost_equal(transformed.positions, ref.positions, decimal=6)


@pytest.mark.parametrize(
    "ag",
    (
        [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_rotateby_bad_ag(rotate_universes, ag):
    # this universe as a box size zero
    ts = rotate_universes[0].trajectory.ts
    ag = rotate_universes[0].residues[0].atoms
    # what happens if something other than an AtomGroup is given?
    angle = 90
    vector = [0, 0, 1]
    bad_ag = 1
    with pytest.raises(ValueError):
        rotateby(angle, vector, ag=bad_ag)(ts)


@pytest.mark.parametrize(
    "point",
    (
        [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]]),
        "thisisnotapoint",
        1,
    ),
)
def test_rotateby_bad_point(rotate_universes, point):
    # this universe as a box size zero
    ts = rotate_universes[0].trajectory.ts
    # what if the box is in the wrong format?
    angle = 90
    vector = [0, 0, 1]
    bad_position = point
    with pytest.raises(ValueError):
        rotateby(angle, vector, point=bad_position)(ts)


@pytest.mark.parametrize(
    "direction",
    (
        [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]]),
        "thisisnotadirection",
        1,
    ),
)
def test_rotateby_bad_direction(rotate_universes, direction):
    # this universe as a box size zero
    ts = rotate_universes[0].trajectory.ts
    # what if the box is in the wrong format?
    angle = 90
    point = [0, 0, 0]
    with pytest.raises(ValueError):
        rotateby(angle, direction, point=point)(ts)


def test_rotateby_bad_pbc(rotate_universes):
    # this universe as a box size zero
    ts = rotate_universes[0].trajectory.ts
    ag = rotate_universes[0].residues[0].atoms
    # is pbc passed to the center methods?
    # if yes it should raise an exception for boxes that are zero in size
    vector = [1, 0, 0]
    angle = 90
    with pytest.raises(ValueError):
        rotateby(angle, vector, ag=ag, wrap=True)(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_rotateby_bad_weights(rotate_universes, weights):
    # this universe as a box size zero
    ts = rotate_universes[0].trajectory.ts
    ag = rotate_universes[0].residues[0].atoms
    # what if a wrong center type name is passed?
    angle = 90
    vector = [0, 0, 1]
    bad_weights = " "
    with pytest.raises(TypeError):
        rotateby(angle, vector, ag=ag, weights=bad_weights)(ts)


def test_rotateby_no_masses(rotate_universes):
    # this universe as a box size zero
    ts = rotate_universes[0].trajectory.ts
    ag = rotate_universes[0].residues[0].atoms
    # if the universe has no masses and `mass` is passed as the center arg
    angle = 90
    vector = [0, 0, 1]
    bad_center = "mass"
    with pytest.raises(TypeError):
        rotateby(angle, vector, ag=ag, weights=bad_center)(ts)


def test_rotateby_no_args(rotate_universes):
    # this universe as a box size zero
    ts = rotate_universes[0].trajectory.ts
    angle = 90
    vector = [0, 0, 1]
    # if no point or AtomGroup are passed to the function
    # it should raise a ValueError
    with pytest.raises(ValueError):
        rotateby(angle, vector)(ts)