From db4f520c9a2f844eabbb94e4a462c23daa2da71a Mon Sep 17 00:00:00 2001 From: Bing Li Date: Wed, 7 Aug 2024 17:00:26 -0400 Subject: [PATCH] fixed test_sample --- .vscode/settings.json | 7 ++++ pyproject.toml | 40 +++++++++--------- tests/test_sample.py | 94 ++++++++++++++++++++----------------------- tests/test_tas.py | 3 ++ 4 files changed, 74 insertions(+), 70 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..9b388533 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "python.testing.pytestArgs": [ + "tests" + ], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 3ec74dca..cebdbeca 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,49 +1,49 @@ [project] -name = "examplepyapp" -description = "Example Python repo for neutrons" -dynamic = ["version"] -requires-python = ">=3.10" dependencies = [ # list all runtime dependencies here ] -license = { text = "MIT" } +description = "Example Python repo for neutrons" +dynamic = ["version"] +license = {text = "LGPL-3.0"} +name = "examplepyapp" +requires-python = ">=3.10" [project.urls] -homepage = "https://github.com/neutrons/python_project_template/" # if no homepage, use repo url +homepage = "https://github.com/neutrons/python_project_template/" # if no homepage, use repo url [build-system] +build-backend = "setuptools.build_meta" requires = [ "setuptools >= 40.6.0", "wheel", "toml", - "versioningit" + "versioningit", ] -build-backend = "setuptools.build_meta" [tool.black] line-length = 119 [tool.versioningit.vcs] -method = "git" default-tag = "0.0.1" +method = "git" [tool.versioningit.next-version] method = "minor" [tool.versioningit.format] -distance = "{next_version}.dev{distance}" dirty = "{version}+d{build_date:%Y%m%d}" +distance = "{next_version}.dev{distance}" distance-dirty = "{next_version}.dev{distance}+d{build_date:%Y%m%d%H%M}" [tool.versioningit.write] file = "src/packagenamepy/_version.py" [tool.setuptools.packages.find] -where = ["src"] exclude = ["tests*", "scripts*", "docs*", "notebooks*"] +where = ["src"] [tool.setuptools.package-data] -"*" = ["*.yml","*.yaml","*.ini"] +"*" = ["*.yml", "*.yaml", "*.ini"] [project.scripts] packagename-cli = "packagenamepy.packagename:main" @@ -52,17 +52,19 @@ packagename-cli = "packagenamepy.packagename:main" packagenamepy = "packagenamepy.packagename:gui" [tool.pytest.ini_options] -pythonpath = [ - ".", "src", "scripts" +markers = [ + "mymarker: example markers goes here", ] -testpaths = ["tests"] -python_files = ["test*.py"] norecursedirs = [".git", "tmp*", "_tmp*", "__pycache__", "*dataset*", "*data_set*"] -markers = [ - "mymarker: example markers goes here" +python_files = ["test*.py"] +pythonpath = [ + ".", + "src", + "scripts", ] +testpaths = ["tests"] [tool.ruff] line-length = 120 -select = ["A", "ARG","ASYNC","BLE","C90", "E", "F", "I", "N", "UP032", "W"] +select = ["A", "ARG", "ASYNC", "BLE", "C90", "E", "F", "I", "N", "UP032", "W"] # Add additional 3rd party tool configuration here as needed diff --git a/tests/test_sample.py b/tests/test_sample.py index fa3b4809..9ff94f1e 100644 --- a/tests/test_sample.py +++ b/tests/test_sample.py @@ -1,60 +1,20 @@ import numpy as np -from tavi.sample.sample import Sample -from tavi.sample.xtal import Xtal - - -def test_b_matrix(lattice_params, b_matrix): - - sample = Sample(lattice_params=lattice_params) - print(sample.b_mat()) - print(np.allclose(sample.b_mat(), b_matrix, atol=1e-4)) - - -def test_ub_matrix_to_uv(lattice_params, ub_matrix): - - xtal = Xtal(lattice_params=lattice_params) - (u, v) = xtal.ub_matrix_to_uv(ub_matrix) - print(f"u={u}") - print(f"v={v}") - - -def test_ub_matrix_to_lattice_params(ub_matrix): - sample = Xtal() - (a, b, c, alpha, beta, gamma) = sample.ub_matrix_to_lattice_params(ub_matrix) - print(f"a={np.round(a,5)}, b={np.round(b,5)},c={np.round(c,5)}") - print(f"alpha={np.round(alpha,3)}, beta={np.round(beta,3)}, gamma={np.round(gamma,3)}") - - -def test_uv_to_ub_matrix(u, v, lattice_params): - sample = Xtal(lattice_params=lattice_params) - ub_matrix = sample.uv_to_ub_matrix(u, v) - - print(f"ub matrix = {ub_matrix}") +import pytest +from tavi.sample.xtal import Xtal -if __name__ == "__main__": +@pytest.fixture +def xtal_info(): a = 3.574924 b = 3.574924 c = 5.663212 alpha = 90 beta = 90 gamma = 120 + lattice_params = (a, b, c, alpha, beta, gamma) + xtal = Xtal(lattice_params=lattice_params) - # ub_matrix = np.array( - # [ - # [0.053821, 0.107638, 0.166485], - # [0.272815, -0.013290, 0.002566], - # [0.164330, 0.304247, -0.058788], - # ] - # ) - ub_matrix = np.array( - [ - [0.0538, 0.1076, 0.1665], - [0.2728, -0.0133, 0.0026], - [0.1643, 0.3042, -0.0588], - ] - ) b_matrix = np.array( [ [0.3230, 0.1615, 0.0000], @@ -62,13 +22,45 @@ def test_uv_to_ub_matrix(u, v, lattice_params): [0.0000, 0.0000, 0.1766], ] ) + ub_matrix = np.array( + [ + [0.0538, 0.1076, 0.1665], + [0.2728, -0.0133, 0.0026], + [0.1643, 0.3042, -0.0588], + ] + ) u = [0.15623, 2.83819, -1.88465] v = [-0.00060, 1.03219, 5.33915] - lattice_params = (a, b, c, alpha, beta, gamma) + return (xtal, b_matrix, ub_matrix, u, v) + + +def test_b_matrix(xtal_info): + xtal, b_matrix, ub_matrix, u, v = xtal_info + assert np.allclose(xtal.b_mat(), b_matrix, atol=1e-4) + + +def test_ub_matrix_to_uv(xtal_info): + xtal, b_matrix, ub_matrix, u, v = xtal_info + (u_calc, v_calc) = xtal.ub_matrix_to_uv(ub_matrix) + assert np.allclose(u_calc, u, atol=1e-3) + assert np.allclose(v_calc, v, atol=1e-3) + + +def test_ub_matrix_to_lattice_params(xtal_info): + xtal, b_matrix, ub_matrix, u, v = xtal_info + (a, b, c, alpha, beta, gamma) = xtal.ub_matrix_to_lattice_params(ub_matrix) + assert np.allclose(a, xtal.a, atol=1e-2) + assert np.allclose(b, xtal.b, atol=1e-2) + assert np.allclose(c, xtal.c, atol=1e-2) + assert np.allclose(alpha, xtal.alpha, atol=1e-2) + assert np.allclose(beta, xtal.beta, atol=1e-2) + assert np.allclose(gamma, xtal.gamma, atol=1e-2) + + +def test_uv_to_ub_matrix(xtal_info): + xtal, b_matrix, ub_matrix, u, v = xtal_info + ub_matrix_calc = xtal.uv_to_ub_matrix(u, v) - # test_b_matrix(lattice_params, b_matrix) - test_ub_matrix_to_uv(lattice_params, ub_matrix) - # test_ub_matrix_to_lattice_params(ub_matrix) - test_uv_to_ub_matrix(u, v, lattice_params) + assert np.allclose(ub_matrix_calc, ub_matrix, atol=1e-2) diff --git a/tests/test_tas.py b/tests/test_tas.py index ec8489b0..7b679701 100644 --- a/tests/test_tas.py +++ b/tests/test_tas.py @@ -1,8 +1,11 @@ +import pytest + from tavi.instrument.tas import TAS from tavi.sample.xtal import Xtal from tavi.utilities import * +@pytest.fixture def instrument_sample_setup(instrument_config, sample_config): tax = TAS() tax.load_instrument_from_dicts(instrument_config)