diff --git a/MDANSE/Tests/UnitTests/test_analysis.py b/MDANSE/Tests/UnitTests/test_analysis.py index c009289d7..8727fb9ca 100644 --- a/MDANSE/Tests/UnitTests/test_analysis.py +++ b/MDANSE/Tests/UnitTests/test_analysis.py @@ -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 diff --git a/MDANSE/Tests/UnitTests/test_molecular_dynamics.py b/MDANSE/Tests/UnitTests/test_molecular_dynamics.py deleted file mode 100644 index 58cb6a46d..000000000 --- a/MDANSE/Tests/UnitTests/test_molecular_dynamics.py +++ /dev/null @@ -1,105 +0,0 @@ -# This file is part of MDANSE_GUI. -# -# MDANSE_GUI is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -import unittest - -import numpy - -from MDANSE.MolecularDynamics.Analysis import ( - radius_of_gyration, - mean_square_deviation, - mean_square_fluctuation, -) - - -class TestMolecularDynamics(unittest.TestCase): - """ - Unittest for the geometry-related functions - """ - - def test_radius_of_gyration(self): - coords = numpy.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=numpy.float64, - ) - self.assertEqual(radius_of_gyration(coords, root=True), numpy.sqrt(0.75)) - - masses = numpy.array( - [1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0], dtype=numpy.float64 - ) - self.assertEqual( - radius_of_gyration(coords, masses=masses, root=True), numpy.sqrt(0.5) - ) - - def test_mean_square_deviation(self): - coords1 = numpy.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=numpy.float64, - ) - - coords2 = numpy.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=numpy.float64, - ) - - self.assertEqual( - mean_square_deviation(coords1, coords2, root=True), numpy.sqrt(3) - ) - - self.assertEqual(mean_square_deviation(coords1, coords2, root=False), 3) - - def test_mean_square_fluctuation(self): - coords = numpy.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=numpy.float64, - ) - - self.assertEqual(mean_square_fluctuation(coords, root=False), 0.75)