Skip to content

Commit

Permalink
added test for cooper-nathans method
Browse files Browse the repository at this point in the history
  • Loading branch information
Bing Li committed Aug 8, 2024
1 parent 737c1f6 commit cba1dea
Show file tree
Hide file tree
Showing 7 changed files with 762 additions and 61 deletions.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,80 +1,41 @@
import matplotlib.pylab as plt
import numpy as np

from tavi.instrument.resolution.cooper_nathans import CN

# from tavi.instrument.instrument_params.python_dicts.takin_test import instrument_params
# from test_data_folder.test_samples.python_samples.sample_test import test_xtal

# from tavi.instrument.instrument_params.python_dicts.cg4c import cg4c_config_params
# from tests.test_data_folder.test_samples.python_samples.nitio3 import nitio3

np.set_printoptions(floatmode="fixed", precision=4)


def test_copper_nathans_localQ(tas_params):
tas, ei, ef, hkl, _, R0 = tas_params

rez = tas.cooper_nathans(
ei=ei,
ef=ef,
hkl=hkl,
projection=None,
R0=R0,
)
print(rez.mat)
print(rez.r0)

# describe and plot ellipses
# rez.calc_ellipses()
rez = tas.cooper_nathans(ei=ei, ef=ef, hkl=hkl, projection=None, R0=R0)
rez.plot()


def test_copper_nathans_hkl(tas_params):
tas, ei, ef, hkl, _, R0 = tas_params

rez = tas.cooper_nathans(
ei=ei,
ef=ef,
hkl=hkl,
R0=R0,
)
print(rez.mat)

# describe and plot ellipses
# rez.calc_ellipses()
rez = tas.cooper_nathans(ei=ei, ef=ef, hkl=hkl, R0=R0)
rez.plot()


def test_copper_nathans_projection(tas_params):
tas, ei, ef, hkl, projection, R0 = tas_params

rez = tas.cooper_nathans(
ei=ei,
ef=ef,
hkl=hkl,
projection=projection,
R0=R0,
)
print(rez.mat)

# describe and plot ellipses
# rez.calc_ellipses()
rez = tas.cooper_nathans(ei=ei, ef=ef, hkl=hkl, projection=projection, R0=R0)

rez.plot()


def test_cooper_nathans_compare_3():
tas = CN()

instrument_config_json_path = "./src/tavi/instrument/instrument_params/takin_test.json"
sample_json_path = "./test_data/test_samples/test_xtal.json"
sample_json_path = "./test_data/test_samples/sample_test.json"

tas.load_instrument_from_json(instrument_config_json_path)
tas.load_sample_from_json(sample_json_path)

# tas.load_instrument_from_(instrument_params)
# tas.load_sample(test_xtal)

if tas.sample.ub_matrix is None:
peak_list = [(0, 0, 2), (0, 2, 0)]
angles_list = [
Expand All @@ -96,8 +57,6 @@ def test_cooper_nathans_compare_3():
test_copper_nathans_hkl(tas_params)
test_copper_nathans_projection(tas_params)

plt.show()


def test_cooper_nathans_CTAX():
tas = CN()
Expand All @@ -108,15 +67,13 @@ def test_cooper_nathans_CTAX():
tas.load_instrument_from_json(instrument_config_json_path)
tas.load_sample_from_json(sample_json_path)

# tas.load_instrument_from_dicts(cg4c_config_params)
# tas.load_sample(nitio3)
print(f"u={tas.sample.u}")
print(f"v={tas.sample.v}")

# reset UB
# ----------- reset UB -----------------------------------
# print(f"u={tas.sample.u}")
# print(f"v={tas.sample.v}")
# tas.sample.ub_matrix = tas.sample.uv_to_ub_matrix(u=[1, 1, 0], v=[0, 0, 1])
# print(f"u={tas.sample.u}")
# print(f"v={tas.sample.v}")
# ----------------------------------------------------------

ei = 4.8
ef = 4.8
Expand All @@ -131,10 +88,10 @@ def test_cooper_nathans_CTAX():
test_copper_nathans_hkl(tas_params)
test_copper_nathans_projection(tas_params)

plt.show()


if __name__ == "__main__":
# test_cooper_nathans_compare_3()
test_cooper_nathans_compare_3()

test_cooper_nathans_CTAX()

plt.show()
1 change: 0 additions & 1 deletion scripts/test_data_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

def test_conversion(exp_numer):
spice_folder = f"./test_data/exp{exp_numer}/"
# h5_file_name = "./tests/test_data/tavi_exp758.h5"
nexus_file_name = f"./test_data/nexus_exp{exp_numer}.h5"
convert_spice_to_nexus(spice_folder, nexus_file_name)

Expand Down
8 changes: 3 additions & 5 deletions scripts/test_fit.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import matplotlib.pyplot as plt
from tavi.tavi_data.tavi_data import TAVI_Data
from tavi.tavi_data.fit import Fit
from tavi.data.fit import Fit
from tavi.data.tavi import TAVI
from tavi.plotter import Plot1DManager


def test_fit_scan(tavi):

p1 = Plot1DManager()

datasets = list(tavi.data.keys())[0]
Expand All @@ -29,8 +28,7 @@ def test_fit_scan(tavi):


if __name__ == "__main__":

tavi = TAVI_Data()
tavi = TAVI()

tavi_file_name = "./tests/test_data_folder/tavi_test_exp424.h5"
tavi.open_tavi_file(tavi_file_name)
Expand Down
73 changes: 73 additions & 0 deletions tests/test_cooper_nathans.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import numpy as np
import pytest
from tavi.instrument.resolution.cooper_nathans import CN

np.set_printoptions(floatmode="fixed", precision=4)


def test_copper_nathans_localQ(tas_params):
tas, ei, ef, hkl, _, R0 = tas_params

rez = tas.cooper_nathans(ei=ei, ef=ef, hkl=hkl, projection=None, R0=R0)
mat = np.array(
[
[9583.2881, -4671.0614, -0.0000, 986.5610],
[-4671.0614, 21359.2992, 0.0000, -4129.1553],
[0.0000, 0.0000, 77.7036, 0.0000],
[986.5610, -4129.1553, -0.0000, 864.3494],
]
)
assert np.allclose(rez.mat, mat, atol=1e-2)


def test_copper_nathans_hkl(tas_params):
tas, ei, ef, hkl, _, R0 = tas_params

rez = tas.cooper_nathans(ei=ei, ef=ef, hkl=hkl, R0=R0)
mat = np.array(
[
[33305.0843, 33224.4963, -2651.8290, -5152.9962],
[33224.4963, 33305.2609, -2651.8526, -5153.0102],
[-2651.8290, -2651.8526, 1983.2037, 448.8024],
[-5152.9962, -5153.0102, 448.8024, 864.3494],
]
)
assert np.allclose(rez.mat, mat, atol=1e-2)


def test_copper_nathans_projection(tas_params):
tas, ei, ef, hkl, projection, R0 = tas_params

rez = tas.cooper_nathans(ei=ei, ef=ef, hkl=hkl, projection=projection, R0=R0)
mat = np.array(
[
[1.3306e05, -5.3037e03, -1.7660e-01, -1.0306e04],
[-5.3037e03, 1.9832e03, 2.3558e-02, 4.4880e02],
[-1.7660e-01, 2.3558e-02, 1.6135e02, 1.4003e-02],
[-1.0306e04, 4.4880e02, 1.4003e-02, 8.6435e02],
]
)
assert np.allclose(rez.mat, mat, atol=1e-2)


@pytest.fixture
def tas_params():
# cooper_nathans_CTAX
tas = CN()

instrument_config_json_path = "./src/tavi/instrument/instrument_params/cg4c.json"
sample_json_path = "./test_data/test_samples/nitio3.json"

tas.load_instrument_from_json(instrument_config_json_path)
tas.load_sample_from_json(sample_json_path)

ei = 4.8
ef = 4.8
hkl = (0, 0, 3)

projection = ((1, 1, 0), (0, 0, 1), (1, -1, 0))
R0 = False

tas_params = (tas, ei, ef, hkl, projection, R0)

return tas_params

0 comments on commit cba1dea

Please sign in to comment.