From 33793fec2acc720c03fa03e7ef053d29b98eb72d Mon Sep 17 00:00:00 2001 From: Kyle Ma Date: Thu, 9 Jan 2025 11:34:16 -0500 Subject: [PATCH 1/7] tests for SC model --- tests/hppt_model/test_single_crystal_model.py | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tests/hppt_model/test_single_crystal_model.py diff --git a/tests/hppt_model/test_single_crystal_model.py b/tests/hppt_model/test_single_crystal_model.py new file mode 100644 index 0000000..22b6537 --- /dev/null +++ b/tests/hppt_model/test_single_crystal_model.py @@ -0,0 +1,91 @@ +import hyspecppt.hppt.hppt_model as hppt_model +import numpy as np + +def test_single_crystal_parameter_set_parameters(): + """Test SingleCrystalParameters set_parameters function""" + scp = hppt_model.SingleCrystalParameters() + sc_data = {"a":1.0, "b":1.0,"c":1.0, "alpha":90.0, "beta":90, "gamma":90, "h": 0, "k":0, "l":0} + scp.set_parameters(sc_data) + + assert scp.a == 1.0 + assert scp.b == 1.0 + assert scp.c == 1.0 + assert scp.alpha == 90.0 + assert scp.beta == 90.0 + assert scp.gamma == 90.0 + assert scp.h == 0 + assert scp.k == 0 + assert scp.l == 0 + +def test_single_crystal_parameter_get_parameters(): + """Test SingleCrystalParameters get_parameters function""" + scp = hppt_model.SingleCrystalParameters() + sc_data = {"a":1.0, "b":1.0,"c":1.0, "alpha":90.0, "beta":90, "gamma":90, "h": 0, "k":0, "l":0} + scp.set_parameters(sc_data) + + assert scp.get_parameters()["a"] == 1.0 + assert scp.get_parameters()["b"] == 1.0 + assert scp.get_parameters()["c"] == 1.0 + assert scp.get_parameters()["alpha"] == 90.0 + assert scp.get_parameters()["beta"] == 90.0 + assert scp.get_parameters()["gamma"] == 90.0 + assert scp.get_parameters()["h"] == 0 + assert scp.get_parameters()["k"] == 0 + assert scp.get_parameters()["l"] == 0 + +def test_single_crystal_parameter_calculate_modQ(): + """Test SingleCrystalParameters calculate_modQ function""" + scp = hppt_model.SingleCrystalParameters() + sc_data = {"a":1.0, "b":1.0,"c":1.0, "alpha":90.0, "beta":90, "gamma":90, "h": 0, "k":0, "l":0} + scp.set_parameters(sc_data) + assert scp.calculate_modQ() == 0.0 + + sc_data["h"] = 1 + sc_data["k"] = 2 + sc_data["l"] = 3 + scp.set_parameters(sc_data) + assert np.isclose(scp.calculate_modQ(), 23.5095267) + + sc_data["alpha"] = 60 + sc_data["beta"] = 60 + sc_data["gamma"] = 90 + scp.set_parameters(sc_data) + assert np.isclose(scp.calculate_modQ(), 19.3660777) + + sc_data["a"] = 10 + sc_data["b"] = 10 + sc_data["c"] = 15.12312 + scp.set_parameters(sc_data) + assert np.isclose(scp.calculate_modQ(), 1.469240) + +def test_cross_hair_parameters_set_crosshair(): + """Test Crosshair set_crosshair function""" + cp = hppt_model.CrosshairParameters() + current_experiment_type = "single_crystal" + DeltaE = 10.0 + modQ = 1.23 + cp.set_crosshair(current_experiment_type) + assert cp.current_experiment_type == "single_crystal" + + cp.set_crosshair(current_experiment_type, DeltaE = DeltaE) + assert cp.DeltaE == 10.0 + + cp.set_crosshair(current_experiment_type, modQ = modQ) + assert cp.DeltaE == 10.0 + assert cp.modQ == 1.23 + +def test_get_cross_hair_parameters_set_crosshair(): + """Test Crosshair get_crosshair function""" + cp = hppt_model.CrosshairParameters() + current_experiment_type = "single_crystal" + DeltaE = 10.0 + modQ = 1.23 + + cp.set_crosshair(current_experiment_type, DeltaE = DeltaE, modQ = modQ) + cp.sc_parameters.set_parameters({"a":1.0, "b":1.0,"c":1.0, "alpha":90.0, "beta":90, "gamma":90, "h": 0, "k":0, "l":0}) + assert cp.get_crosshair()["DeltaE"] == 10.0 + assert cp.get_crosshair()["modQ"] == 0.0 + + cp.set_crosshair("powder", DeltaE = DeltaE, modQ = modQ) + assert cp.get_crosshair()["DeltaE"] == 10.0 + assert cp.get_crosshair()["modQ"] == 1.23 \ No newline at end of file From 3523bd05c883fbc06cc73433ca201a1927fa3d1f Mon Sep 17 00:00:00 2001 From: Kyle Ma Date: Thu, 9 Jan 2025 12:44:37 -0500 Subject: [PATCH 2/7] initalization and modQ change --- src/hyspecppt/hppt/experiment_settings.py | 1 + src/hyspecppt/hppt/hppt_model.py | 18 ++++++----- tests/hppt_model/test_HyspecPPTModel.py | 6 ++++ tests/hppt_model/test_single_crystal_model.py | 30 ++++++++++++------- 4 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 tests/hppt_model/test_HyspecPPTModel.py diff --git a/src/hyspecppt/hppt/experiment_settings.py b/src/hyspecppt/hppt/experiment_settings.py index 99ef1dc..9eaca18 100644 --- a/src/hyspecppt/hppt/experiment_settings.py +++ b/src/hyspecppt/hppt/experiment_settings.py @@ -18,6 +18,7 @@ DEFAULT_LATTICE = dict(a=1, b=1, c=1, alpha=90, beta=90, gamma=90, h=0, k=0, l=0) DEFAULT_EXPERIMENT = dict(Ei=20, S2=30, alpha_p=0, plot_type=PLOT_TYPES[1]) DEFAULT_CROSSHAIR = dict(DeltaE=0, modQ=0) +MaxQ = 15 # invalid style INVALID_QLINEEDIT = """ diff --git a/src/hyspecppt/hppt/hppt_model.py b/src/hyspecppt/hppt/hppt_model.py index 3da8adf..c20bf05 100644 --- a/src/hyspecppt/hppt/hppt_model.py +++ b/src/hyspecppt/hppt/hppt_model.py @@ -5,7 +5,7 @@ import numpy as np from scipy.constants import e, hbar, m_n -from .experiment_settings import PLOT_TYPES +from .experiment_settings import DEFAULT_EXPERIMENT, PLOT_TYPES, MaxQ logger = logging.getLogger("hyspecppt") @@ -118,17 +118,21 @@ def set_crosshair(self, current_experiment_type: str, DeltaE: float = None, modQ def get_crosshair(self) -> dict[str, float]: """Get the crosshair""" if self.current_experiment_type == "single_crystal": - self.modQ = self.sc_parameters.calculate_modQ() - return dict(DeltaE=self.DeltaE, modQ=self.modQ) + modQ = self.sc_parameters.calculate_modQ() + if modQ < MaxQ: + self.modQ = modQ + return dict(DeltaE=self.DeltaE, modQ=modQ) + else: + return dict(DeltaE=self.DeltaE, modQ=self.modQ) class HyspecPPTModel: """Main model""" - Ei: float = 0 - S2: float = 0 - alpha_p: float = 0 - plot_type: str = "" + Ei: float = DEFAULT_EXPERIMENT["Ei"] + S2: float = DEFAULT_EXPERIMENT["S2"] + alpha_p: float = DEFAULT_EXPERIMENT["alpha_p"] + plot_type: str = DEFAULT_EXPERIMENT["plot_type"] cp: CrosshairParameters def __init__(self): diff --git a/tests/hppt_model/test_HyspecPPTModel.py b/tests/hppt_model/test_HyspecPPTModel.py new file mode 100644 index 0000000..e72bca7 --- /dev/null +++ b/tests/hppt_model/test_HyspecPPTModel.py @@ -0,0 +1,6 @@ +import hyspecppt.hppt.hppt_model as hppt_model +import numpy as np + +def test_single_crystal_parameter_set_parameters(): + """Test SingleCrystalParameters set_parameters function""" + #saving for next story \ No newline at end of file diff --git a/tests/hppt_model/test_single_crystal_model.py b/tests/hppt_model/test_single_crystal_model.py index 22b6537..b4cd299 100644 --- a/tests/hppt_model/test_single_crystal_model.py +++ b/tests/hppt_model/test_single_crystal_model.py @@ -1,12 +1,14 @@ -import hyspecppt.hppt.hppt_model as hppt_model import numpy as np +import hyspecppt.hppt.hppt_model as hppt_model + + def test_single_crystal_parameter_set_parameters(): """Test SingleCrystalParameters set_parameters function""" scp = hppt_model.SingleCrystalParameters() - sc_data = {"a":1.0, "b":1.0,"c":1.0, "alpha":90.0, "beta":90, "gamma":90, "h": 0, "k":0, "l":0} + sc_data = {"a": 1.0, "b": 1.0, "c": 1.0, "alpha": 90.0, "beta": 90, "gamma": 90, "h": 0, "k": 0, "l": 0} scp.set_parameters(sc_data) - + assert scp.a == 1.0 assert scp.b == 1.0 assert scp.c == 1.0 @@ -17,10 +19,11 @@ def test_single_crystal_parameter_set_parameters(): assert scp.k == 0 assert scp.l == 0 + def test_single_crystal_parameter_get_parameters(): """Test SingleCrystalParameters get_parameters function""" scp = hppt_model.SingleCrystalParameters() - sc_data = {"a":1.0, "b":1.0,"c":1.0, "alpha":90.0, "beta":90, "gamma":90, "h": 0, "k":0, "l":0} + sc_data = {"a": 1.0, "b": 1.0, "c": 1.0, "alpha": 90.0, "beta": 90, "gamma": 90, "h": 0, "k": 0, "l": 0} scp.set_parameters(sc_data) assert scp.get_parameters()["a"] == 1.0 @@ -33,10 +36,11 @@ def test_single_crystal_parameter_get_parameters(): assert scp.get_parameters()["k"] == 0 assert scp.get_parameters()["l"] == 0 + def test_single_crystal_parameter_calculate_modQ(): """Test SingleCrystalParameters calculate_modQ function""" scp = hppt_model.SingleCrystalParameters() - sc_data = {"a":1.0, "b":1.0,"c":1.0, "alpha":90.0, "beta":90, "gamma":90, "h": 0, "k":0, "l":0} + sc_data = {"a": 1.0, "b": 1.0, "c": 1.0, "alpha": 90.0, "beta": 90, "gamma": 90, "h": 0, "k": 0, "l": 0} scp.set_parameters(sc_data) assert scp.calculate_modQ() == 0.0 @@ -58,6 +62,7 @@ def test_single_crystal_parameter_calculate_modQ(): scp.set_parameters(sc_data) assert np.isclose(scp.calculate_modQ(), 1.469240) + def test_cross_hair_parameters_set_crosshair(): """Test Crosshair set_crosshair function""" cp = hppt_model.CrosshairParameters() @@ -67,13 +72,14 @@ def test_cross_hair_parameters_set_crosshair(): cp.set_crosshair(current_experiment_type) assert cp.current_experiment_type == "single_crystal" - cp.set_crosshair(current_experiment_type, DeltaE = DeltaE) + cp.set_crosshair(current_experiment_type, DeltaE=DeltaE) assert cp.DeltaE == 10.0 - cp.set_crosshair(current_experiment_type, modQ = modQ) + cp.set_crosshair(current_experiment_type, modQ=modQ) assert cp.DeltaE == 10.0 assert cp.modQ == 1.23 + def test_get_cross_hair_parameters_set_crosshair(): """Test Crosshair get_crosshair function""" cp = hppt_model.CrosshairParameters() @@ -81,11 +87,13 @@ def test_get_cross_hair_parameters_set_crosshair(): DeltaE = 10.0 modQ = 1.23 - cp.set_crosshair(current_experiment_type, DeltaE = DeltaE, modQ = modQ) - cp.sc_parameters.set_parameters({"a":1.0, "b":1.0,"c":1.0, "alpha":90.0, "beta":90, "gamma":90, "h": 0, "k":0, "l":0}) + cp.set_crosshair(current_experiment_type, DeltaE=DeltaE, modQ=modQ) + cp.sc_parameters.set_parameters( + {"a": 1.0, "b": 1.0, "c": 1.0, "alpha": 90.0, "beta": 90, "gamma": 90, "h": 0, "k": 0, "l": 0} + ) assert cp.get_crosshair()["DeltaE"] == 10.0 assert cp.get_crosshair()["modQ"] == 0.0 - cp.set_crosshair("powder", DeltaE = DeltaE, modQ = modQ) + cp.set_crosshair("powder", DeltaE=DeltaE, modQ=modQ) assert cp.get_crosshair()["DeltaE"] == 10.0 - assert cp.get_crosshair()["modQ"] == 1.23 \ No newline at end of file + assert cp.get_crosshair()["modQ"] == 1.23 From 19906b7d42f84b464534c43fae729a94aa680c84 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 9 Jan 2025 17:50:20 +0000 Subject: [PATCH 3/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/hppt_model/test_HyspecPPTModel.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/hppt_model/test_HyspecPPTModel.py b/tests/hppt_model/test_HyspecPPTModel.py index e72bca7..6111cd3 100644 --- a/tests/hppt_model/test_HyspecPPTModel.py +++ b/tests/hppt_model/test_HyspecPPTModel.py @@ -1,6 +1,8 @@ -import hyspecppt.hppt.hppt_model as hppt_model import numpy as np +import hyspecppt.hppt.hppt_model as hppt_model + + def test_single_crystal_parameter_set_parameters(): """Test SingleCrystalParameters set_parameters function""" - #saving for next story \ No newline at end of file + # saving for next story From 83fc97dd06b4dd3dc414fa261046b69b38d94655 Mon Sep 17 00:00:00 2001 From: Kyle Ma Date: Thu, 9 Jan 2025 12:53:54 -0500 Subject: [PATCH 4/7] pre-commit fix --- tests/hppt_model/test_HyspecPPTModel.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/hppt_model/test_HyspecPPTModel.py b/tests/hppt_model/test_HyspecPPTModel.py index 6111cd3..c02eced 100644 --- a/tests/hppt_model/test_HyspecPPTModel.py +++ b/tests/hppt_model/test_HyspecPPTModel.py @@ -1,7 +1,5 @@ -import numpy as np - -import hyspecppt.hppt.hppt_model as hppt_model - +import hyspecppt.hppt.hppt_model as hppt_model #F401 +import numpy as np # F401 def test_single_crystal_parameter_set_parameters(): """Test SingleCrystalParameters set_parameters function""" From 801cd60d4deca2f88c3c48eb0fc9076a78872ec0 Mon Sep 17 00:00:00 2001 From: Kyle Ma Date: Thu, 9 Jan 2025 12:55:35 -0500 Subject: [PATCH 5/7] precommit fix --- tests/hppt_model/test_HyspecPPTModel.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/hppt_model/test_HyspecPPTModel.py b/tests/hppt_model/test_HyspecPPTModel.py index c02eced..ef72c15 100644 --- a/tests/hppt_model/test_HyspecPPTModel.py +++ b/tests/hppt_model/test_HyspecPPTModel.py @@ -1,5 +1,7 @@ -import hyspecppt.hppt.hppt_model as hppt_model #F401 -import numpy as np # F401 +import numpy as np # noqa: F401 + +import hyspecppt.hppt.hppt_model as hppt_model # noqa: F401 + def test_single_crystal_parameter_set_parameters(): """Test SingleCrystalParameters set_parameters function""" From c760b9c6344246f315e6fa6129b50c6b85727db8 Mon Sep 17 00:00:00 2001 From: Kyle Ma Date: Thu, 9 Jan 2025 15:16:43 -0500 Subject: [PATCH 6/7] initialization and minor changes --- src/hyspecppt/hppt/hppt_model.py | 26 +++++++++---------- ...specPPTModel.py => test_hyspecpptmodel.py} | 2 +- tests/hppt_model/test_single_crystal_model.py | 12 ++++----- 3 files changed, 20 insertions(+), 20 deletions(-) rename tests/hppt_model/{test_HyspecPPTModel.py => test_hyspecpptmodel.py} (72%) diff --git a/src/hyspecppt/hppt/hppt_model.py b/src/hyspecppt/hppt/hppt_model.py index 002bd71..788eef1 100644 --- a/src/hyspecppt/hppt/hppt_model.py +++ b/src/hyspecppt/hppt/hppt_model.py @@ -5,7 +5,7 @@ import numpy as np from scipy.constants import e, hbar, m_n -from .experiment_settings import DEFAULT_EXPERIMENT, PLOT_TYPES, MaxQ +from .experiment_settings import DEFAULT_CROSSHAIR, DEFAULT_EXPERIMENT, DEFAULT_LATTICE, DEFAULT_MODE, PLOT_TYPES, MaxQ logger = logging.getLogger("hyspecppt") @@ -13,15 +13,15 @@ class SingleCrystalParameters: """Model for single crystal calculations""" - a: float - b: float - c: float - alpha: float - beta: float - gamma: float - h: float - k: float - l: float + a: float = DEFAULT_LATTICE["a"] + b: float = DEFAULT_LATTICE["b"] + c: float = DEFAULT_LATTICE["c"] + alpha: float = DEFAULT_LATTICE["alpha"] + beta: float = DEFAULT_LATTICE["beta"] + gamma: float = DEFAULT_LATTICE["gamma"] + h: float = DEFAULT_LATTICE["h"] + k: float = DEFAULT_LATTICE["k"] + l: float = DEFAULT_LATTICE["l"] def __init__(self) -> None: """Constructor""" @@ -97,9 +97,9 @@ def calculate_modQ(self) -> float: class CrosshairParameters: """Model for the crosshair parameters""" - modQ: float - DeltaE: float - current_experiment_type: str + modQ: float = DEFAULT_CROSSHAIR["modQ"] + DeltaE: float = DEFAULT_CROSSHAIR["DeltaE"] + current_experiment_type: str = DEFAULT_MODE["current_experiment_type"] sc_parameters: SingleCrystalParameters experiment_types = ["single_crystal", "powder"] diff --git a/tests/hppt_model/test_HyspecPPTModel.py b/tests/hppt_model/test_hyspecpptmodel.py similarity index 72% rename from tests/hppt_model/test_HyspecPPTModel.py rename to tests/hppt_model/test_hyspecpptmodel.py index ef72c15..7f43c4c 100644 --- a/tests/hppt_model/test_HyspecPPTModel.py +++ b/tests/hppt_model/test_hyspecpptmodel.py @@ -1,6 +1,6 @@ import numpy as np # noqa: F401 -import hyspecppt.hppt.hppt_model as hppt_model # noqa: F401 +from hyspecppt.hppt.hppt_model import HyspecPPTModel # noqa: F401 def test_single_crystal_parameter_set_parameters(): diff --git a/tests/hppt_model/test_single_crystal_model.py b/tests/hppt_model/test_single_crystal_model.py index b4cd299..3164883 100644 --- a/tests/hppt_model/test_single_crystal_model.py +++ b/tests/hppt_model/test_single_crystal_model.py @@ -1,11 +1,11 @@ import numpy as np -import hyspecppt.hppt.hppt_model as hppt_model +from hyspecppt.hppt.hppt_model import CrosshairParameters, SingleCrystalParameters def test_single_crystal_parameter_set_parameters(): """Test SingleCrystalParameters set_parameters function""" - scp = hppt_model.SingleCrystalParameters() + scp = SingleCrystalParameters() sc_data = {"a": 1.0, "b": 1.0, "c": 1.0, "alpha": 90.0, "beta": 90, "gamma": 90, "h": 0, "k": 0, "l": 0} scp.set_parameters(sc_data) @@ -22,7 +22,7 @@ def test_single_crystal_parameter_set_parameters(): def test_single_crystal_parameter_get_parameters(): """Test SingleCrystalParameters get_parameters function""" - scp = hppt_model.SingleCrystalParameters() + scp = SingleCrystalParameters() sc_data = {"a": 1.0, "b": 1.0, "c": 1.0, "alpha": 90.0, "beta": 90, "gamma": 90, "h": 0, "k": 0, "l": 0} scp.set_parameters(sc_data) @@ -39,7 +39,7 @@ def test_single_crystal_parameter_get_parameters(): def test_single_crystal_parameter_calculate_modQ(): """Test SingleCrystalParameters calculate_modQ function""" - scp = hppt_model.SingleCrystalParameters() + scp = SingleCrystalParameters() sc_data = {"a": 1.0, "b": 1.0, "c": 1.0, "alpha": 90.0, "beta": 90, "gamma": 90, "h": 0, "k": 0, "l": 0} scp.set_parameters(sc_data) assert scp.calculate_modQ() == 0.0 @@ -65,7 +65,7 @@ def test_single_crystal_parameter_calculate_modQ(): def test_cross_hair_parameters_set_crosshair(): """Test Crosshair set_crosshair function""" - cp = hppt_model.CrosshairParameters() + cp = CrosshairParameters() current_experiment_type = "single_crystal" DeltaE = 10.0 modQ = 1.23 @@ -82,7 +82,7 @@ def test_cross_hair_parameters_set_crosshair(): def test_get_cross_hair_parameters_set_crosshair(): """Test Crosshair get_crosshair function""" - cp = hppt_model.CrosshairParameters() + cp = CrosshairParameters() current_experiment_type = "single_crystal" DeltaE = 10.0 modQ = 1.23 From 81205ed8c45d2007bfd5326ef780e3f700ea7c10 Mon Sep 17 00:00:00 2001 From: Kyle Ma Date: Fri, 10 Jan 2025 09:32:59 -0500 Subject: [PATCH 7/7] minor comments --- tests/hppt_model/test_hyspecpptmodel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/hppt_model/test_hyspecpptmodel.py b/tests/hppt_model/test_hyspecpptmodel.py index 7f43c4c..c10e9b9 100644 --- a/tests/hppt_model/test_hyspecpptmodel.py +++ b/tests/hppt_model/test_hyspecpptmodel.py @@ -1,4 +1,4 @@ -import numpy as np # noqa: F401 +# import numpy as np # noqa: F401 from hyspecppt.hppt.hppt_model import HyspecPPTModel # noqa: F401