From 7d557fe3bc0dc95a5906be7c672126b9442092ee Mon Sep 17 00:00:00 2001 From: Jacob Wilkins Date: Fri, 21 Feb 2025 15:54:04 +0000 Subject: [PATCH 1/3] Use pytest for test_analysis and fix test --- MDANSE/Tests/UnitTests/test_analysis.py | 118 +++++++++++------------- 1 file changed, 54 insertions(+), 64 deletions(-) diff --git a/MDANSE/Tests/UnitTests/test_analysis.py b/MDANSE/Tests/UnitTests/test_analysis.py index c009289d76..c1dcdcb4a2 100644 --- a/MDANSE/Tests/UnitTests/test_analysis.py +++ b/MDANSE/Tests/UnitTests/test_analysis.py @@ -1,67 +1,57 @@ 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]]), + np.array([[1, 1, 1], [2, 1, 1], [3, 1, 1]]), + np.array([[1, 1, 1], [2, 1, 1], [8, 1, 1]]), + np.array([[1, 2, 1], [2, 1, 1], [10, 5, 5], [1, 1, 2]]), +] + +@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), + ]) +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)), +]) +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)), +]) +def test_radius_of_gyration_no_masses_no_root(coords, masses, root, expected): + rog = radius_of_gyration(coords, masses, root=root) + assert rog == expected From 0a9f7ee613c156f8112d21e8ba302ff3f598669a Mon Sep 17 00:00:00 2001 From: Jacob Wilkins <46597752+oerc0122@users.noreply.github.com> Date: Mon, 24 Feb 2025 12:25:19 +0000 Subject: [PATCH 2/3] Apply suggestions from code review Signed-off-by: Jacob Wilkins <46597752+oerc0122@users.noreply.github.com> --- MDANSE/Tests/UnitTests/test_analysis.py | 1 - 1 file changed, 1 deletion(-) diff --git a/MDANSE/Tests/UnitTests/test_analysis.py b/MDANSE/Tests/UnitTests/test_analysis.py index c1dcdcb4a2..fce0494bdd 100644 --- a/MDANSE/Tests/UnitTests/test_analysis.py +++ b/MDANSE/Tests/UnitTests/test_analysis.py @@ -1,4 +1,3 @@ -import unittest import numpy as np import pytest From a64008394e87819e653843018e9d47d9b1a9b4fc Mon Sep 17 00:00:00 2001 From: Jacob Wilkins Date: Tue, 25 Feb 2025 11:55:04 +0000 Subject: [PATCH 3/3] Merge test_molecular_dynamics --- MDANSE/Tests/UnitTests/test_analysis.py | 20 +++- .../UnitTests/test_molecular_dynamics.py | 105 ------------------ 2 files changed, 14 insertions(+), 111 deletions(-) delete mode 100644 MDANSE/Tests/UnitTests/test_molecular_dynamics.py diff --git a/MDANSE/Tests/UnitTests/test_analysis.py b/MDANSE/Tests/UnitTests/test_analysis.py index fce0494bdd..8727fb9ca2 100644 --- a/MDANSE/Tests/UnitTests/test_analysis.py +++ b/MDANSE/Tests/UnitTests/test_analysis.py @@ -1,4 +1,3 @@ - import numpy as np import pytest from MDANSE.MolecularDynamics.Analysis import (AnalysisError, @@ -8,16 +7,22 @@ radius_of_gyration) COORDS = [ - np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]]), - np.array([[1, 1, 1], [2, 1, 1], [3, 1, 1]]), - np.array([[1, 1, 1], [2, 1, 1], [8, 1, 1]]), - np.array([[1, 2, 1], [2, 1, 1], [10, 5, 5], [1, 1, 2]]), + 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) @@ -40,6 +45,7 @@ def test_mean_square_displacement(coords, n_configs, 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) @@ -50,7 +56,9 @@ def test_mean_square_fluctuation(coords, root, expected): (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_no_masses_no_root(coords, masses, root, expected): +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 58cb6a46d0..0000000000 --- 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)