File: test_positionaveraging.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 (129 lines) | stat: -rw-r--r-- 4,249 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
124
125
126
127
128
129
###

import numpy as np
import pytest
from numpy.testing import assert_array_almost_equal

import MDAnalysis as md
from MDAnalysis.transformations import PositionAverager
from MDAnalysisTests import datafiles


@pytest.fixture()
def posaveraging_universes():
    """
    Create the universe objects for the tests.
    """
    u = md.Universe(datafiles.XTC_multi_frame, to_guess=())
    transformation = PositionAverager(3)
    u.trajectory.add_transformations(transformation)
    return u


@pytest.fixture()
def posaveraging_universes_noreset():
    """
    Create the universe objects for the tests.
    Position averaging reset is set to False.
    """
    u = md.Universe(datafiles.XTC_multi_frame, to_guess=())
    transformation = PositionAverager(3, check_reset=False)
    u.trajectory.add_transformations(transformation)
    return u


def test_posavging_fwd(posaveraging_universes):
    """
    Test if the position averaging function is returning the correct
    values when iterating forwards over the trajectory.
    """
    ref_matrix_fwd = np.asarray([80.0, 80.0, 80.0])
    size = (
        posaveraging_universes.trajectory.ts.positions.shape[0],
        posaveraging_universes.trajectory.ts.positions.shape[1],
        len(posaveraging_universes.trajectory),
    )
    avgd = np.empty(size)
    for ts in posaveraging_universes.trajectory:
        np.copyto(avgd[..., ts.frame], ts.positions)

    assert_array_almost_equal(ref_matrix_fwd, avgd[1, :, -1], decimal=5)


def test_posavging_bwd(posaveraging_universes):
    """
    Test if the position averaging function is returning the correct
    values when iterating backwards over the trajectory.
    """
    ref_matrix_bwd = np.asarray([10.0, 10.0, 10.0])
    size = (
        posaveraging_universes.trajectory.ts.positions.shape[0],
        posaveraging_universes.trajectory.ts.positions.shape[1],
        len(posaveraging_universes.trajectory),
    )
    back_avgd = np.empty(size)
    for ts in posaveraging_universes.trajectory[::-1]:
        np.copyto(back_avgd[..., 9 - ts.frame], ts.positions)
    assert_array_almost_equal(ref_matrix_bwd, back_avgd[1, :, -1], decimal=5)


def test_posavging_reset(posaveraging_universes):
    """
    Test if the automatic reset is working as intended.
    """
    size = (
        posaveraging_universes.trajectory.ts.positions.shape[0],
        posaveraging_universes.trajectory.ts.positions.shape[1],
        len(posaveraging_universes.trajectory),
    )
    avgd = np.empty(size)
    for ts in posaveraging_universes.trajectory:
        np.copyto(avgd[..., ts.frame], ts.positions)
    after_reset = ts.positions.copy()
    assert_array_almost_equal(avgd[..., 0], after_reset, decimal=5)


def test_posavging_specific(posaveraging_universes):
    """
    Test if the position averaging function is returning the correct values
    when iterating over arbitrary non-sequential frames.
    check_reset=True
    """
    ref_matrix_specr = np.asarray([30.0, 30.0, 30.0])
    fr_list = [0, 1, 7, 3]
    size = (
        posaveraging_universes.trajectory.ts.positions.shape[0],
        posaveraging_universes.trajectory.ts.positions.shape[1],
        len(fr_list),
    )
    specr_avgd = np.empty(size)
    idx = 0
    for ts in posaveraging_universes.trajectory[fr_list]:
        np.copyto(specr_avgd[..., idx], ts.positions)
        idx += 1
    assert_array_almost_equal(
        ref_matrix_specr, specr_avgd[1, :, -1], decimal=5
    )


def test_posavging_specific_noreset(posaveraging_universes_noreset):
    """
    Test if the position averaging function is returning the correct values
    when iterating over arbitrary non-sequential frames.
    check_reset=False
    """
    ref_matrix_specr = np.asarray([36.66667, 36.66667, 36.66667])
    fr_list = [0, 1, 7, 3]
    size = (
        posaveraging_universes_noreset.trajectory.ts.positions.shape[0],
        posaveraging_universes_noreset.trajectory.ts.positions.shape[1],
        len(fr_list),
    )
    specr_avgd = np.empty(size)
    idx = 0
    for ts in posaveraging_universes_noreset.trajectory[fr_list]:
        np.copyto(specr_avgd[..., idx], ts.positions)
        idx += 1
    assert_array_almost_equal(
        ref_matrix_specr, specr_avgd[1, :, -1], decimal=5
    )