Skip to content

Commit

Permalink
fixed test_sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Bing Li committed Aug 7, 2024
1 parent 1240782 commit db4f520
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 70 deletions.
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
40 changes: 21 additions & 19 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
94 changes: 43 additions & 51 deletions tests/test_sample.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,66 @@
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],
[0.0000, 0.2797, -0.0000],
[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)
3 changes: 3 additions & 0 deletions tests/test_tas.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit db4f520

Please sign in to comment.