Skip to content

Commit

Permalink
Merge pull request #273 from bluesky/215-restore_reflections
Browse files Browse the repository at this point in the history
restore_reflections() use renamed motor axes if so defined
  • Loading branch information
prjemian authored Oct 19, 2023
2 parents b941e83 + 81fe2f0 commit 299d39a
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
12 changes: 11 additions & 1 deletion RELEASE_NOTES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ Release History
Deprecations
Contributors
v1.1 (expected 2024)
v1.2 (expected 2024)
======================================

User-requested changes

v1.1 (expected 2023-12)
======================================

Add new geometries from upstream *libhkl*.

Fixes
-----

* ``util.restore_reflections()`` use renamed motor axes if so defined.

v1.0.4 (released 2023-10-06)
======================================

Expand Down
119 changes: 119 additions & 0 deletions hkl/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,75 @@
NO_SUCH_PACKAGE_NAME = "no-such-package"


@pytest.fixture(scope="function")
def cat():
import databroker

yield databroker.temp().v2


@pytest.fixture(scope="function")
def RE(cat):
import bluesky

engine = bluesky.RunEngine()
engine.subscribe(cat.v1.insert)
yield engine


@pytest.fixture(scope="function")
def fourc():
"""4-circle with renamed axes and oriented sample."""
from hkl import E4CV, SimMixin
from ophyd import Component
from ophyd import SoftPositioner

class FourCircle(SimMixin, E4CV):
theta = Component(SoftPositioner, kind="hinted", init_pos=0)
chi = Component(SoftPositioner, kind="hinted", init_pos=0)
phi = Component(SoftPositioner, kind="hinted", init_pos=0)
ttheta = Component(SoftPositioner, kind="hinted", init_pos=0)

fourc = FourCircle("", name="fourc")
# rename the physical axes
fourc.calc.physical_axis_names = {
# E4CV: local
"omega": "theta",
"chi": "chi",
"phi": "phi",
"tth": "ttheta",
}

# fourc.wait_for_connection()
fourc._update_calc_energy()
crystal_setup(fourc)

yield fourc


def crystal_setup(diffractometer):
from hkl import Lattice

diffractometer.calc.wavelength = 1.0
a0 = 5.4321
# fmt: off
diffractometer.calc.new_sample(
"vibranium",
lattice=Lattice(a=a0, b=a0, c=a0, alpha=90, beta=90, gamma=90)
)

diffractometer.calc.sample.add_reflection(
1, 2, 3,
position=diffractometer.calc.Position(
ttheta=60,
theta=40,
chi=0,
phi=0,
),
)
# fmt: on


def test__package_info_states():
assert hkl.util._package_info is None
hkl.util.get_package_info("hkl")
Expand Down Expand Up @@ -74,3 +143,53 @@ def test_software_versions_items(package_name, minimum_version):
assert v_package >= version.parse(minimum_version)
else:
assert package_name in ("hklpy", NO_SUCH_PACKAGE_NAME)


def test_issue215(cat, RE, fourc):
"""restore_reflections(orientation, fourc) cannot find renamed positioner."""
from bluesky import plans as bp

canonical_names = "omega chi phi tth".split()
our_names = "theta chi phi ttheta".split()
assert our_names != canonical_names
assert fourc.calc.physical_axis_names != canonical_names
assert fourc.calc.physical_axis_names == our_names

problem_reflection_dict = {
"reflection": {"h": 1.0, "k": -3.0, "l": -1.0},
"flag": 1,
"wavelength": 1.1169734383241103,
"position": {
"omega": -8.208399999999983,
"chi": -47.47651999999994,
"phi": -5.684341886080802e-14,
"tth": 25.09473789610388,
},
"orientation_reflection": False,
}
problem_positioner_names = list(problem_reflection_dict["position"].keys())
assert problem_positioner_names == canonical_names

assert len(cat) == 0
uids = RE(bp.count([fourc]))
assert len(uids) == 1
assert len(cat) == 1

orientation = hkl.util.run_orientation_info(cat[-1])
assert isinstance(orientation, dict)
assert fourc.name in orientation
# assert list(orientation.keys()) == []

orient = orientation[fourc.name]
assert orient["_reals"] != canonical_names
assert orient["_reals"] == our_names
for reflection in orient["reflections_details"]:
assert list(reflection["position"].keys()) == canonical_names

success = False
try:
# since it is just a problem of motor names in reflections ...
hkl.util.restore_reflections(orient, fourc)
success = True
finally:
assert success, "Could not restore orientation reflections."
4 changes: 4 additions & 0 deletions hkl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ def restore_reflections(orientation, diffractometer):
pseudos = orientation["_pseudos"]
reals = orientation["_reals"]
orientation_reflections = []
# might be renamed axes
renaming = diffractometer.calc._axis_name_to_original
if len(renaming) > 0:
reals = [renaming[k] for k in reals]

for ref_base in orientation["reflections_details"]:
# every reflection has its own wavelength
Expand Down

0 comments on commit 299d39a

Please sign in to comment.