APS-4ID-POLAR / ipython-polar

4-ID-Polar ipython configuration for bluesky (and other)
1 stars 3 forks source link

Issues with hklpy #146

Open gfabbris opened 2 years ago

gfabbris commented 2 years ago

In [21]: %run start_macro.py
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /home/sector4/4idd/2022_2/Ir_Rodolfo/start_macro.py:22, in <module>
     19 undulator.downstream.tracking.put(True)
     21 orientation = run_orientation_info(cat[63])["fourc"]
---> 22 restore_orientation(orientation, fourc)
     23 fourc.engine.mode = "constant_phi"

File ~/.conda/envs/bluesky_2022_2/lib/python3.9/site-packages/hkl/util.py:444, in restore_orientation(orientation, diffractometer)
    432 """
    433 Restore all orientation information.
    434 
   (...)
    441     Diffractometer object.
    442 """
    443 _check_geometry(orientation, diffractometer)
--> 444 restore_energy(orientation, diffractometer)
    445 restore_sample(orientation, diffractometer)
    446 restore_constraints(orientation, diffractometer)

File ~/.conda/envs/bluesky_2022_2/lib/python3.9/site-packages/hkl/util.py:381, in restore_energy(orientation, diffractometer)
    367 """
    368 Restore energy from orientation information.
    369 
   (...)
    378     Diffractometer object.
    379 """
    380 for attr in "energy energy_units energy_offset".split():
--> 381     _smart_signal_update(orientation[attr], getattr(diffractometer, attr))

KeyError: 'energy'

In [23]: orientation
Out[23]: 
{'energy_units': 'keV',
 'energy_offset': 0,
 'geometry_name': 'E4CV',
 'class_name': 'FourCircleDiffractometer',
 'sample_name': 'Na2IrO3',
 'lattice': [5.427, 9.395, 5.614, 90.0, 109.04, 90.0],
 'lattice_reciprocal': [1.2247698817691968,
  0.6687797027333248,
  1.1839733075100518,
  90.00000000000001,
  70.96,
  90.00000000000001],
 'U': [[0.39704160761964563, -0.11147619865712856, 0.9110054988592351],
  [0.16370892526169198, -0.9680764729221553, -0.1898086677795866],
  [0.9030821389045789, 0.22450166972108723, -0.36611699043335155]],
 'UB': [[0.48628460282176517, -0.07455301899975553, 1.172952300853581],
  [0.20050576103732476, -0.6474298957840046, -0.14920191582564885],
  [1.1060678044940344, 0.1501421599392037, -0.06094654323699006]],
 'reflections_details': [{'reflection': {'h': 0.0, 'k': 2.0, 'l': 0.0},
   'flag': 1,
   'wavelength': 1.1169734383241103,
   'position': {'omega': 0.4593000000000131,
    'chi': -76.94362000000001,
    'phi': -5.684341886080802e-14,
    'tth': 13.71943789610387},
   'orientation_reflection': False},
  {'reflection': {'h': 0.0, 'k': 2.0, 'l': 0.0},
   'flag': 1,
   'wavelength': 1.1169734383241103,
   'position': {'omega': 0.4593000000000131,
    'chi': -76.94362000000001,
    'phi': -5.684341886080802e-14,
    'tth': 13.71943789610387},
   'orientation_reflection': True},
  {'reflection': {'h': 2.0, 'k': 0.0, 'l': 0.0},
   'flag': 1,
   'wavelength': 1.1169734383241103,
   'position': {'omega': -8.208399999999983,
    'chi': -47.47651999999994,
    'phi': -5.684341886080802e-14,
    'tth': 25.09473789610388},
   'orientation_reflection': False},
  {'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},
  {'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},
  {'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': True}],
 'ux': 152.5960965960107,
 'uy': 65.64467593378099,
 'uz': 15.682991677770966,
 'diffractometer_name': 'fourc',
 '_hklpy_version': '1.0.1',
 '_pseudos': ['h', 'k', 'l'],
 '_reals': ['theta', 'chi', 'phi', 'tth'],
 '_constraints': [[-180.0, 180.0, -3.5, 1.0],
  [-180.0, 180.0, -54.57585999999992, 1.0],
  [-180.0, 180.0, -5.684341886080802e-14, 1.0],
  [-180.0, 180.0, 25.072637896103885, 1.0]],
 '_mode': 'constant_phi',
 'orientation_attrs': ['orientation_attrs',
  'geometry_name',
  'class_name',
  'UB',
  'U',
  'ux',
  'uy',
  'uz',
  'energy',
  'energy_units',
  'energy_offset',
  'sample_name',
  'lattice',
  'lattice_reciprocal',
  'reflections_details',
  '_pseudos',
  '_reals',
  '_constraints',
  '_mode',
  'diffractometer_name',
  '_hklpy_version'],
 'theta_user_offset_dir': 0,
 'chi_user_offset': -1175.63732,
 'phi_acceleration': 0.25,
 'tth_velocity': 0.5,
 'x_velocity': 200.0,
 'y_motor_egu': 'microns',
 'z_velocity': 100.0,
 'baseth_user_offset': -179.5502,
 'basetth_velocity': 0.4,
 'tablex_user_offset': 94.14855,
 'tabley_user_offset_dir': 0,
 'ath_acceleration': 0.25,
 'achi_velocity': 4.0,
 'atth_velocity': 4.0}
gfabbris commented 2 years ago

In [1]: %run start_macro.py
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /home/sector4/4idd/2022_2/Ir_Rodolfo/start_macro.py:26, in <module>
     24 restore_sample(orientation, fourc)
     25 restore_constraints(orientation, fourc)
---> 26 restore_reflections(orientation, fourc)
     28 fourc.engine.mode = "constant_phi"

File ~/.conda/envs/bluesky_2022_2/lib/python3.9/site-packages/hkl/util.py:413, in restore_reflections(orientation, diffractometer)
    409 # Order of the items is important.
    410 # Can't just use the dictionaries in ``orientation``.
    411 # Get the canonical order from the orientation data.
    412 miller_indices = [ref_base["reflection"][key] for key in pseudos]
--> 413 positions = [ref_base["position"][key] for key in reals]
    414 ppp = namedtuple("PositionTuple", tuple(reals))(*positions)
    416 # assemble the final form of the reflection for add_reflection()

File ~/.conda/envs/bluesky_2022_2/lib/python3.9/site-packages/hkl/util.py:413, in <listcomp>(.0)
    409 # Order of the items is important.
    410 # Can't just use the dictionaries in ``orientation``.
    411 # Get the canonical order from the orientation data.
    412 miller_indices = [ref_base["reflection"][key] for key in pseudos]
--> 413 positions = [ref_base["position"][key] for key in reals]
    414 ppp = namedtuple("PositionTuple", tuple(reals))(*positions)
    416 # assemble the final form of the reflection for add_reflection()

KeyError: 'theta'
gfabbris commented 2 years ago

The first problem may happen because fourc.energy.kind = omitted? https://github.com/APS-4ID-POLAR/ipython-polar/blob/fa20b5420b2b2fa51c577c861bd589abcfac7967/profile_bluesky/startup/instrument/devices/huber.py#L94

gfabbris commented 2 years ago

The second problem may be because we rename omega to theta. https://github.com/APS-4ID-POLAR/ipython-polar/blob/fa20b5420b2b2fa51c577c861bd589abcfac7967/profile_bluesky/startup/instrument/devices/huber.py#L84

gfabbris commented 2 years ago

@prjemian: FYI

prjemian commented 2 years ago

And I imagine, for the second problem, the rename map is not stored. Definitely an issue for hklpy.

prjemian commented 2 years ago

Why was the energy.kind="omitted" setting applied?

prjemian commented 2 years ago

Because it is certain that setting kind="omitted" was a misconfiguration from the expected kind="config".

gfabbris commented 2 years ago

The reason for the omitted energy is because we are reading the monochromator directly and I was concerned about having two Devices read the same EpicsSignal. But, actually, it probably doesn't make sense to restore the energy for us as it should be set by the beamline energy.

prjemian commented 2 years ago

When a reflection is defined, it uses the wavelength from the diffractometer. So the wavelength (from the X-ray photon energy) must be set before the hkl is defined by the reflection angles.

In the future, this should be made more flexible, so that an arbitrary wavelength could be supplied without the diffractometer actually being set to it. But that is not how the code (in the libhkl C code) works today.

prjemian commented 2 years ago

Here's the upgrade issue in hklpy: https://github.com/bluesky/hklpy/issues/189

gfabbris commented 2 years ago

I agree that hklpy needs to be able to use the energy at which the reflection was defined, not the current energy. The UB matrix is energy independent, so I think I could tie the fourc energy to the Beamline's in a subscription, and then add restore_UB to the end of a local version of restore_orientation. Does that sound correct?

prjemian commented 2 years ago

Yes

On Sat, Jun 4, 2022, 2:25 PM Gilberto Fabbris @.***> wrote:

I agree that hklpy needs to be able to use the energy at which the reflection was defined, not the current energy. The UB matrix is energy independent, so I think I could tie the fourc energy to the Beamline's in a subscription, and then add restore_UB to the end of a local version of restore_orientation. Does that sound correct?

— Reply to this email directly, view it on GitHub https://github.com/APS-4ID-POLAR/ipython-polar/issues/146#issuecomment-1146673009, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARMUMBXIH2JGVTN3Y4A2BDVNOUUFANCNFSM5XRXTRIA . You are receiving this because you were mentioned.Message ID: @.***>