Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pytest for test_analysis and fix test #678

Open
wants to merge 3 commits into
base: protos
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 63 additions & 66 deletions MDANSE/Tests/UnitTests/test_analysis.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,64 @@
import unittest

import numpy as np

from MDANSE.MolecularDynamics.Analysis import *


class TestAnalysis(unittest.TestCase):
def test_mean_square_deviation_valid_no_masses_no_root(self):
coords1 = np.array([[1, 1, 1], [2, 1, 1], [3, 1, 1]])
coords2 = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]])

msd = mean_square_deviation(coords1, coords2)
self.assertEqual(20 / 3, msd)

def test_mean_square_deviation_masses(self):
coords1 = np.array([[1, 1, 1], [2, 1, 1], [3, 1, 1]])
coords2 = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
masses = np.array([3, 10, 1])

msd = mean_square_deviation(coords1, coords2, masses)
self.assertEqual(80 / 14, msd)

def test_mean_square_displacement_root(self):
coords1 = np.array([[1, 1, 1], [2, 1, 1], [8, 1, 1]])
coords2 = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]])

msd = mean_square_deviation(coords1, coords2, root=True)
self.assertEqual(5, msd)

def test_mean_square_deviation_invalid(self):
coords1 = np.zeros((3, 3))
coords2 = np.zeros((4, 3))

with self.assertRaises(AnalysisError):
mean_square_deviation(coords1, coords2)

def test_mean_square_displacement(self):
coords = np.array([[1, 1, 1], [2, 1, 1], [3, 1, 1]])
msd = mean_square_displacement(coords, 1)
self.assertTrue(np.allclose([0.0, 1.0, 4.0], msd), f"\nactual = {msd}")

def test_mean_square_fluctuation_no_root(self):
coords = np.array([[1, 2, 1], [2, 1, 1], [10, 5, 5], [1, 1, 2]])
msf = mean_square_fluctuation(coords)
self.assertEqual(19.625, msf)

def test_mean_square_fluctuation_root(self):
coords = np.array([[1, 2, 1], [2, 1, 1], [10, 5, 5], [1, 1, 2]])
msf = mean_square_fluctuation(coords, True)
self.assertEqual(np.sqrt(19.625), msf)

def test_radius_of_gyration_no_masses_no_root(self):
coords = np.array([[1, 2, 1], [2, 1, 1], [10, 5, 5], [1, 1, 2]])
rog = radius_of_gyration(coords)
self.assertEqual(19.625, rog)

def test_radius_of_gyration_masses(self):
coords = np.array([[1, 2, 1], [2, 1, 1], [10, 5, 5], [1, 1, 2]])
masses = np.array([1, 1, 5, 1])
rog = radius_of_gyration(coords, masses)
self.assertEqual(24.15625, rog)

def test_radius_of_gyration_root(self):
coords = np.array([[1, 2, 1], [2, 1, 1], [10, 5, 5], [1, 1, 2]])
msf = mean_square_fluctuation(coords, True)
self.assertEqual(np.sqrt(19.625), msf)
import pytest
from MDANSE.MolecularDynamics.Analysis import (AnalysisError,
mean_square_deviation,
mean_square_displacement,
mean_square_fluctuation,
radius_of_gyration)

COORDS = [
np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]], dtype=float),
np.array([[1, 1, 1], [2, 1, 1], [3, 1, 1]], dtype=float),
np.array([[1, 1, 1], [2, 1, 1], [8, 1, 1]], dtype=float),
np.array([[1, 2, 1], [2, 1, 1], [10, 5, 5], [1, 1, 2]], dtype=float),
np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0],
[0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]], dtype=float),
np.array([[1, 1, 1], [2, 1, 1], [2, 2, 1], [1, 2, 1],
[1, 1, 2], [2, 1, 2], [2, 2, 2], [1, 2, 2]], dtype=float),
]

@pytest.mark.parametrize("coords,masses,root,expected", [
((COORDS[1], COORDS[0]), None, False, 20/3),
((COORDS[1], COORDS[0]), [3, 10, 1], False, 80/14),
((COORDS[2], COORDS[0]), None, True, 5),
((COORDS[4], COORDS[5]), None, True, np.sqrt(3)),
((COORDS[4], COORDS[5]), None, False, 3),
])
def test_mean_square_deviation(coords, masses, root, expected):
msd = mean_square_deviation(*coords, masses, root)
assert msd == expected

@pytest.mark.parametrize("coords,masses,root,expected", [
([np.zeros((3, 3)), np.zeros((3, 4))], None, False, AnalysisError),
])
def test_invalid(coords, masses, root, expected):
with pytest.raises(expected):
mean_square_deviation(*coords, masses, root)

@pytest.mark.parametrize("coords, n_configs, expected", [
(COORDS[1], 1, [0., 1., 4.])
])
def test_mean_square_displacement(coords, n_configs, expected):
msd = mean_square_displacement(coords, n_configs)
assert np.allclose(msd, expected)

@pytest.mark.parametrize("coords, root, expected", [
(COORDS[3], False, 19.625),
(COORDS[3], True, np.sqrt(19.625)),
(COORDS[4], False, 0.75),
])
def test_mean_square_fluctuation(coords, root, expected):
msf = mean_square_fluctuation(coords, root=root)
assert msf == expected

@pytest.mark.parametrize("coords, masses, root, expected", [
(COORDS[3], None, False, 19.625),
(COORDS[3], [1, 1, 5, 1], False, 24.15625),
(COORDS[3], None, True, np.sqrt(19.625)),
(COORDS[3], [1, 1, 5, 1], True, np.sqrt(24.15625)),
(COORDS[4], None, True, np.sqrt(0.75)),
(COORDS[4], [1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0], True, np.sqrt(0.5))
])
def test_radius_of_gyration(coords, masses, root, expected):
rog = radius_of_gyration(coords, masses, root=root)
assert rog == expected
105 changes: 0 additions & 105 deletions MDANSE/Tests/UnitTests/test_molecular_dynamics.py

This file was deleted.