From 2052da29582cefdfe7c212f75cd6e159fc306264 Mon Sep 17 00:00:00 2001 From: mjwen Date: Sun, 30 Jul 2023 16:46:51 -0500 Subject: [PATCH] Update UQ tests --- tests/descriptors/test_bispectrum.py | 2 - tests/descriptors/test_descriptor.py | 1 - tests/descriptors/test_symmetry_function.py | 1 - tests/test_neighbor.py | 2 +- tests/test_parallel.py | 2 +- tests/test_scipy_optimize.py | 2 - tests/uq/test_bootstrap_empirical.py | 2 +- tests/uq/test_bootstrap_nn.py | 2 +- .../uq/test_bootstrap_nn_separate_species.py | 2 +- tests/uq/test_mcmc.py | 88 +++++++++---------- 10 files changed, 48 insertions(+), 56 deletions(-) diff --git a/tests/descriptors/test_bispectrum.py b/tests/descriptors/test_bispectrum.py index 2b2846d7..cb5dc5ee 100644 --- a/tests/descriptors/test_bispectrum.py +++ b/tests/descriptors/test_bispectrum.py @@ -1,5 +1,3 @@ -from pathlib import Path - import numpy as np from kliff.dataset import Configuration diff --git a/tests/descriptors/test_descriptor.py b/tests/descriptors/test_descriptor.py index 1d1d2800..ff76aacc 100644 --- a/tests/descriptors/test_descriptor.py +++ b/tests/descriptors/test_descriptor.py @@ -1,5 +1,4 @@ import itertools -import os import numpy as np diff --git a/tests/descriptors/test_symmetry_function.py b/tests/descriptors/test_symmetry_function.py index 2d2ef5bf..40c43e34 100644 --- a/tests/descriptors/test_symmetry_function.py +++ b/tests/descriptors/test_symmetry_function.py @@ -1,4 +1,3 @@ -"""Test symmetry functions values.""" import itertools from collections import OrderedDict diff --git a/tests/test_neighbor.py b/tests/test_neighbor.py index f7f8689f..2f096685 100644 --- a/tests/test_neighbor.py +++ b/tests/test_neighbor.py @@ -66,7 +66,7 @@ def test_neigh(test_data_dir): def test_1D(): """ - Simple test of a dimer, nonperiodic. + Simple test of a dimer, non-periodic. """ cell = np.asarray([[200.0, 0.0, 0.0], [0.0, 200.0, 0.0], [0.0, 0.0, 200.0]]) diff --git a/tests/test_parallel.py b/tests/test_parallel.py index 7b2bfec2..2594b040 100644 --- a/tests/test_parallel.py +++ b/tests/test_parallel.py @@ -7,7 +7,7 @@ def func(x, y, z=1): return x + y + z -def test_main(): +def test_parmap(): X = range(3) Y = range(3) Xp2 = [x + 2 for x in X] diff --git a/tests/test_scipy_optimize.py b/tests/test_scipy_optimize.py index fcb1ec5a..1638ee6d 100644 --- a/tests/test_scipy_optimize.py +++ b/tests/test_scipy_optimize.py @@ -1,5 +1,3 @@ -from pathlib import Path - import numpy as np from kliff.calculators import Calculator diff --git a/tests/uq/test_bootstrap_empirical.py b/tests/uq/test_bootstrap_empirical.py index 504e6e2b..ef25bf93 100644 --- a/tests/uq/test_bootstrap_empirical.py +++ b/tests/uq/test_bootstrap_empirical.py @@ -25,7 +25,7 @@ # training set FILE_DIR = Path(__file__).absolute().parent # Directory of test file -path = FILE_DIR.parent.joinpath("configs_extxyz/Si_4") +path = FILE_DIR.parent.joinpath("test_data/configs/Si_4") data = Dataset(path) configs = data.get_configs() diff --git a/tests/uq/test_bootstrap_nn.py b/tests/uq/test_bootstrap_nn.py index 7c018586..27fc337b 100644 --- a/tests/uq/test_bootstrap_nn.py +++ b/tests/uq/test_bootstrap_nn.py @@ -40,7 +40,7 @@ # training set FILE_DIR = Path(__file__).absolute().parent # Directory of test file -path = FILE_DIR.parent.joinpath("configs_extxyz/Si_4") +path = FILE_DIR.parent.joinpath("test_data/configs/Si_4") data = Dataset(path) configs = data.get_configs() diff --git a/tests/uq/test_bootstrap_nn_separate_species.py b/tests/uq/test_bootstrap_nn_separate_species.py index 38a99dd3..9b678a8b 100644 --- a/tests/uq/test_bootstrap_nn_separate_species.py +++ b/tests/uq/test_bootstrap_nn_separate_species.py @@ -42,7 +42,7 @@ # training set FILE_DIR = Path(__file__).absolute().parent # Directory of test file -path = FILE_DIR.parent.joinpath("configs_extxyz/SiC_4") +path = FILE_DIR.parent.joinpath("test_data/configs/SiC_4") data = Dataset(path) configs = data.get_configs() diff --git a/tests/uq/test_mcmc.py b/tests/uq/test_mcmc.py index 59b6bd69..aa0d7fff 100644 --- a/tests/uq/test_mcmc.py +++ b/tests/uq/test_mcmc.py @@ -14,14 +14,14 @@ from kliff.uq.mcmc import PtemceeSampler ptemcee_avail = True -except ModuleNotFoundError: +except ImportError: ptemcee_avail = False try: from kliff.uq.mcmc import EmceeSampler emcee_avail = True -except ModuleNotFoundError: +except ImportError: emcee_avail = False @@ -95,71 +95,69 @@ def test_T0(): """ # Using internal function T0_internal = get_T0(loss) + # Compute manually xopt = calc.get_opt_params() T0_manual = 2 * loss._get_loss(xopt) / len(xopt) assert T0_internal == T0_manual, "Internal function to compute T0 doesn't work" -def test_MCMC_wrapper(): +@pytest.mark.skipif(not ptemcee_avail, reason="ptemcee is not found") +def test_MCMC_wrapper1(): """Test if the MCMC wrapper class returns the correct sampler instance.""" - if ptemcee_avail: - assert ( - type(ptsampler) == PtemceeSampler - ), "MCMC should return ``PtemceeSampler`` instance" - if emcee_avail: - assert ( - type(sampler) == EmceeSampler - ), "MCMC should return ``EmceeSampler`` instance" + assert ( + type(ptsampler) == PtemceeSampler + ), "MCMC should return ``PtemceeSampler`` instance" + + +@pytest.mark.skipif(not emcee_avail, reason="emcee is not found") +def test_MCMC_wrapper2(): + assert type(sampler) == EmceeSampler, "MCMC should return ``EmceeSampler`` instance" -def test_dimensionality(): +@pytest.mark.skipif(not ptemcee_avail, reason="ptemcee is not found") +def test_dimensionality1(): """Test the number of temperatures, walkers, steps, and parameters. This is done by comparing the shape of the resulting MCMC chains and the variables used to set these dimensions. """ # Test for ptemcee wrapper - if ptemcee_avail: - p0 = np.random.uniform(0, 10, (ntemps, nwalkers, ndim)) - ptsampler.run_mcmc(p0=p0, iterations=nsteps) - assert ptsampler.chain.shape == ( - ntemps, - nwalkers, - nsteps, - ndim, - ), "Dimensionality from the ptemcee wrapper is not right" - else: - print("Skip testing ptemcee; ptemcee is not found") - + p0 = np.random.uniform(0, 10, (ntemps, nwalkers, ndim)) + ptsampler.run_mcmc(p0=p0, iterations=nsteps) + assert ptsampler.chain.shape == ( + ntemps, + nwalkers, + nsteps, + ndim, + ), "Dimensionality from the ptemcee wrapper is not right" + + +@pytest.mark.skipif(not emcee_avail, reason="emcee is not found") +def test_dimensionality2(): # Test for emcee wrapper - if emcee_avail: - p0 = np.random.uniform(0, 10, (nwalkers, ndim)) - sampler.run_mcmc(initial_state=p0, nsteps=nsteps) - assert sampler.get_chain().shape == ( - nsteps, - nwalkers, - ndim, - ), "Dimensionality from the emcee wrapper is not right" - else: - print("Skip testing emcee; emcee is not found") + p0 = np.random.uniform(0, 10, (nwalkers, ndim)) + sampler.run_mcmc(initial_state=p0, nsteps=nsteps) + assert sampler.get_chain().shape == ( + nsteps, + nwalkers, + ndim, + ), "Dimensionality from the emcee wrapper is not right" +@pytest.mark.skipif(not ptemcee_avail, reason="ptemcee is not found") def test_pool_exception(): """Test if an exception is raised when declaring the pool prior to instantiating ``kliff.uq.MCMC``. """ - if ptemcee_avail: - with pytest.raises(ValueError): - _ = MCMC( - loss, - ntemps=ntemps, - nwalkers=nwalkers, - logprior_args=(prior_bounds,), - pool=Pool(1), - ) - else: - print("Skip the test; ptemcee is not found") + with pytest.raises(ValueError): + _ = MCMC( + loss, + ntemps=ntemps, + nwalkers=nwalkers, + logprior_args=(prior_bounds,), + pool=Pool(1), + ) def test_sampler_exception():