From 914cea8572b15f5753020f145c7dd7280a233772 Mon Sep 17 00:00:00 2001 From: Pete R Jemian Date: Tue, 19 Dec 2023 00:41:08 -0600 Subject: [PATCH 1/5] TST #303 test for the correct answer --- hkl/tests/test_diffract.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/hkl/tests/test_diffract.py b/hkl/tests/test_diffract.py index 0342d6c4..0c5cfca0 100644 --- a/hkl/tests/test_diffract.py +++ b/hkl/tests/test_diffract.py @@ -357,3 +357,33 @@ def test_get_constraint(axis, fourc): else: with pytest.raises(KeyError): fourc.get_axis_constraints(axis) + + +def test_i303_forward_solution_original_problem(e4cv): + assert e4cv is not None + + # Approximate the code presented as the example problem. + refl = (1, 1, 1) + e4cv.reset_constraints() + + e4cv.calc.engine.mode = "constant_phi" + assert e4cv.calc.engine.mode == "constant_phi" + CONSTANT_PHI = 23.4567 + + e4cv.apply_constraints({"phi": Constraint(-180, 180, CONSTANT_PHI, True)}) + assert pytest.approx(e4cv.calc["phi"].value, abs=1e-4) == CONSTANT_PHI + + # Check that phi is held constant in forward_solutions_table() + # Returns a pyRestTable.Table object. + table = e4cv.forward_solutions_table([refl], full=True, digits=4) + assert len(table.rows) == 2 + assert table.labels[4] == "phi" + assert pytest.approx(table.rows[0][4], abs=1e-4) == CONSTANT_PHI + assert pytest.approx(table.rows[1][4], abs=1e-4) == CONSTANT_PHI + + # Check that phi is held constant in forward() + # Returns a position namedtuple. + position = e4cv.forward(refl) + assert pytest.approx(position.phi, abs=1e-4) == CONSTANT_PHI + + e4cv.reset_constraints() From 217f767d0453859f404b44234f0e18b3b439e9f5 Mon Sep 17 00:00:00 2001 From: Pete R Jemian Date: Tue, 19 Dec 2023 00:41:24 -0600 Subject: [PATCH 2/5] MNT #303 try the simpler solution first --- hkl/diffract.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/hkl/diffract.py b/hkl/diffract.py index 67bcb312..e1ec7f3f 100644 --- a/hkl/diffract.py +++ b/hkl/diffract.py @@ -456,9 +456,12 @@ def forward(self, pseudo): Return the default solution using the ``_decision_fcn()``. """ - solutions = self.calc.forward_iter( - start=self.position, end=pseudo, max_iters=self.max_forward_iterations.get() - ) + try: + solutions = self.calc.forward(list(pseudo)) + except ValueError: + solutions = self.calc.forward_iter( + start=self.position, end=pseudo, max_iters=self.max_forward_iterations.get() + ) logger.debug("pseudo to real: %s", solutions) return self._decision_fcn(pseudo, solutions) From 82ce4f695523f495ea70fcb7d7956bcc524d8b0d Mon Sep 17 00:00:00 2001 From: Pete R Jemian Date: Tue, 19 Dec 2023 00:41:45 -0600 Subject: [PATCH 3/5] DOC #303 --- RELEASE_NOTES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE_NOTES.rst b/RELEASE_NOTES.rst index c08ec4ad..24359bb7 100644 --- a/RELEASE_NOTES.rst +++ b/RELEASE_NOTES.rst @@ -38,6 +38,7 @@ New Features and/or Enhancements Fixes ----- +* ``diffract.forward()`` should pick solution consistent with ``diffract.forward_solution_table()``, if it can. Otherwise, fall back to previous iterative method. * ``util.restore_reflections()`` use renamed motor axes if so defined. Maintenance From 100643fd78c20843e2f4562c578115807ceb006a Mon Sep 17 00:00:00 2001 From: Pete R Jemian Date: Tue, 19 Dec 2023 00:42:13 -0600 Subject: [PATCH 4/5] CI #303 set numerical precision for this tardis test --- hkl/tests/test_tardis.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hkl/tests/test_tardis.py b/hkl/tests/test_tardis.py index 85cdf625..424f2b57 100644 --- a/hkl/tests/test_tardis.py +++ b/hkl/tests/test_tardis.py @@ -132,8 +132,8 @@ def test_reachable(tardis, kcf_sample, constrain): tardis.move(ppos) print("tardis position is", tardis.position) print("tardis position is", tardis.calc.physical_positions) - numpy.testing.assert_almost_equal(tardis.position, ppos) - numpy.testing.assert_almost_equal(tardis.calc.physical_positions, rpos) + numpy.testing.assert_almost_equal(tardis.position, ppos, decimal=4) + numpy.testing.assert_almost_equal(tardis.calc.physical_positions, rpos, decimal=4) def test_inversion(tardis, kcf_sample, constrain): From a7ed9ca2ea3f4c64e56672d7da0c2145dbc15147 Mon Sep 17 00:00:00 2001 From: Pete R Jemian Date: Tue, 19 Dec 2023 17:13:06 -0600 Subject: [PATCH 5/5] Update hkl/tests/test_diffract.py LGTM Co-authored-by: Padraic Shafer <76011594+padraic-shafer@users.noreply.github.com> --- hkl/tests/test_diffract.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/hkl/tests/test_diffract.py b/hkl/tests/test_diffract.py index 0c5cfca0..9f069fa9 100644 --- a/hkl/tests/test_diffract.py +++ b/hkl/tests/test_diffract.py @@ -376,10 +376,10 @@ def test_i303_forward_solution_original_problem(e4cv): # Check that phi is held constant in forward_solutions_table() # Returns a pyRestTable.Table object. table = e4cv.forward_solutions_table([refl], full=True, digits=4) - assert len(table.rows) == 2 - assert table.labels[4] == "phi" - assert pytest.approx(table.rows[0][4], abs=1e-4) == CONSTANT_PHI - assert pytest.approx(table.rows[1][4], abs=1e-4) == CONSTANT_PHI + phi_column = table.labels.index("phi") + assert len(table.rows) > 0 + for row in table.rows: + assert pytest.approx(row[phi_column], abs=1e-4) == CONSTANT_PHI # Check that phi is held constant in forward() # Returns a position namedtuple.