Skip to content

Commit

Permalink
Merge pull request #315 from bluesky/303-v2-forward-solution
Browse files Browse the repository at this point in the history
forward() should pick answer consistent with forward_solutions_table() and default decision function
  • Loading branch information
prjemian authored Dec 20, 2023
2 parents 863dd70 + a7ed9ca commit 7b57153
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions hkl/diffract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
30 changes: 30 additions & 0 deletions hkl/tests/test_diffract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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.
position = e4cv.forward(refl)
assert pytest.approx(position.phi, abs=1e-4) == CONSTANT_PHI

e4cv.reset_constraints()
4 changes: 2 additions & 2 deletions hkl/tests/test_tardis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 7b57153

Please sign in to comment.