def interactive_calibration(
crystal,
camera,
obs_xy: np.ndarray,
U0: np.ndarray,
image: np.ndarray | None = None,
E_min_eV: float = E_MIN_eV,
E_max_eV: float = E_MAX_eV,
source: str = "bending_magnet",
source_kwargs: dict | None = None,
f2_thresh: float = F2_THRESHOLD,
kb_params=BM32_KB,
max_match_px: float = 30.0,
top_n_sim: int = 80,
rot_range_deg: float = 20.0,
c_rot_range_deg: float = 180.0,
dd_range: float = 10.0,
cen_range_px: float = 150.0,
angle_range_deg: float = 2.0,
space: str = "angular",
figsize: tuple = (14, 6),
) -> CalibrationState:
"""
Open an interactive widget to simultaneously adjust crystal orientation
and camera geometry as a starting point for :meth:`Camera.fit_calibration`.
Two columns of sliders appear below the detector view:
* **Orientation** — incremental rotations around the three crystal axes,
plus a rotation around a clicked reflection's own plane normal (same
as :func:`interactive_orientation`): click a simulated spot to select
it, then drag the "rot. [hkl]" slider to spin the crystal about that
reflection's axis. Right-click, or "✕ Deselect", clears the selection.
Dragging a spot directly (rather than clicking it) instead rotates U
so that spot moves under the cursor.
* **Camera geometry** — additive deltas for `dd`, `xcen`, `ycen`,
`xbet`, `xgam` relative to the current reference camera.
"Set as reference" bakes the current camera + orientation as the new base
and resets all sliders to zero. "✓ Accept" prints the final parameters
and a ready-to-run :meth:`~Camera.fit_calibration` call.
Args:
crystal (xrayutilities Crystal): Calibration standard crystal (not LayeredCrystal).
camera (Camera): Initial camera geometry.
obs_xy ((N, 2) ndarray): Observed spot pixel positions from segmentation.
U0 ((3, 3) ndarray): Initial crystal orientation matrix.
image ((Nv, Nh) ndarray or None): Optional background detector image (log-scaled for display).
max_match_px (float): Pixel radius for match lines and match-rate reporting.
top_n_sim (int): Maximum number of simulated spots rendered.
rot_range_deg (float): Half-range of the [100] / [010] orientation sliders (°).
c_rot_range_deg (float): Half-range of the [001] orientation slider (°).
dd_range (float): Half-range of the Δ dd slider (mm).
cen_range_px (float): Half-range of the Δ xcen / Δ ycen sliders (px).
angle_range_deg (float): Half-range of the Δ xbet / Δ xgam sliders (°).
space (`'angular'`, `'gnomonic'`, or `'detector'`): Coordinate frame for the main panel. `'angular'` (default) plots
2θ (x) vs χ (y) in degrees. `'gnomonic'` plots the gnomonic
projection k_y/k_x vs k_z/k_x — zone axes appear as straight lines.
`'detector'` plots raw pixel positions.
Matching is always done in detector (pixel) space regardless of this flag.
Returns:
CalibrationState
`state.camera` — pass to :meth:`~Camera.fit_calibration`.
`state.U` — pass to :meth:`~Camera.fit_calibration`.
`state.accepted` — True if "✓ Accept" was clicked.
"""
import ipywidgets as ipw
from IPython.display import display as _ipy_display
from .camera import Camera as _Camera
U0 = np.asarray(U0, dtype=float)
obs_use = np.asarray(obs_xy, dtype=float)
state = CalibrationState(camera, U0)
source_kwargs = source_kwargs or {}
# ── Simulation ────────────────────────────────────────────────────────────
def _simulate(U: np.ndarray, cam) -> list:
return simulate_laue(
crystal, U, cam,
E_min=E_min_eV, E_max=E_max_eV,
source=source, source_kwargs=source_kwargs,
f2_thresh=f2_thresh, kb_params=kb_params,
)
# ── Figure ────────────────────────────────────────────────────────────────
with plt.ioff():
fig = plt.figure(figsize=figsize, facecolor=_BG)
try:
fig.canvas.manager.set_window_title("Laue — interactive calibration")
except Exception:
pass
gs = gridspec.GridSpec(
1, 2,
figure=fig,
left=0.05, right=0.98, bottom=0.06, top=0.96,
wspace=0.06,
width_ratios=[2.6, 1.0],
)
ax_det = fig.add_subplot(gs[0, 0])
ax_info = fig.add_subplot(gs[0, 1])
for ax in (ax_det, ax_info):
ax.set_facecolor(_BG2)
ax.tick_params(colors=_GRAY, labelsize=7)
for sp in ax.spines.values():
sp.set_edgecolor(_GRAY)
ax_det.set_title(
"Laue — interactive calibration "
"○ observed ◆ simulated — matched",
color=_FG, fontsize=9, pad=6,
)
if space == "detector":
ax_det.set_xlim(0, camera.Nh)
ax_det.set_ylim(camera.Nv, 0)
ax_det.set_aspect("equal")
ax_det.set_xlabel("xcam (px)", color=_FG, fontsize=8)
ax_det.set_ylabel("ycam (px)", color=_FG, fontsize=8)
if image is not None:
img_arr = np.asarray(image, dtype=float)
vmax = np.percentile(img_arr[img_arr > 0], 99) if img_arr.max() > 0 else 1.0
ax_det.imshow(
np.log1p(img_arr / vmax * 1000),
origin="upper",
extent=[0, camera.Nh, camera.Nv, 0],
cmap="gray",
aspect="auto",
alpha=0.85,
zorder=0,
)
else:
ax_det.add_patch(plt.Rectangle(
(0, 0), camera.Nh, camera.Nv,
linewidth=0.8, edgecolor=_GRAY, facecolor="none", zorder=0,
))
elif space == "gnomonic":
ax_det.set_aspect("equal")
ax_det.set_xlabel("gnomonic X", color=_FG, fontsize=8)
ax_det.set_ylabel("gnomonic Y", color=_FG, fontsize=8)
ax_det.grid(True, ls=":", lw=0.35, color="#181e2e", zorder=0)
ax_det.axhline(0, color=_GRAY, lw=0.5, zorder=1)
ax_det.axvline(0, color=_GRAY, lw=0.5, zorder=1)
else: # angular
ax_det.set_aspect("auto")
ax_det.set_xlabel("2θ (°)", color=_FG, fontsize=8)
ax_det.set_ylabel("χ (°)", color=_FG, fontsize=8)
ax_det.grid(True, ls=":", lw=0.35, color="#181e2e", zorder=0)
# Initial observed scatter; angular/gnomonic coords update each frame in
# _do_update since the camera changes with slider movement.
_uf0 = camera.pixel_to_kf(obs_use[:, 0], obs_use[:, 1])
if space == "detector":
_obs_init = obs_use
elif space == "gnomonic":
_tth0_c = np.degrees(np.arccos(np.clip(_uf0[:, 0], -1.0, 1.0)))
_chi0_c = np.degrees(np.arctan2(_uf0[:, 1], _uf0[:, 2] + 1e-17))
_gX0c, _gY0c = _gnomonic(_tth0_c, _chi0_c)
_obs_init = np.column_stack([_gX0c, _gY0c])
else: # angular
_obs_init = np.column_stack([
np.degrees(np.arccos(np.clip(_uf0[:, 0], -1.0, 1.0))),
np.degrees(np.arctan2(_uf0[:, 1], _uf0[:, 2] + 1e-17)),
])
sc_obs = ax_det.scatter(
_obs_init[:, 0], _obs_init[:, 1],
s=45, c="none", edgecolors=_OBS, linewidths=0.9,
zorder=4, label=f"observed ({len(obs_use)})",
)
sc_sim = ax_det.scatter(
[], [], s=28, c=_SIM, marker="D", linewidths=0,
zorder=5, label="simulated",
)
_lines: list = []
sc_sel = ax_det.scatter( # selected-spot ring
[], [], s=130, c="none", edgecolors="#ffff00",
linewidths=1.8, zorder=7, marker="o",
)
sc_drag = ax_det.scatter( # drag target ring
[], [], s=180, c="none", edgecolors="#ff4400",
linewidths=1.8, zorder=8, marker="o",
)
_drag_line, = ax_det.plot( # guide line during drag
[], [], color="#ff4400", lw=1.0,
linestyle="--", zorder=7, alpha=0.7,
)
ax_det.legend(loc="upper right", fontsize=7,
facecolor=_BG2, edgecolor=_GRAY, labelcolor=_FG)
ax_info.set_axis_off()
_info_txt = ax_info.text(
0.06, 0.98, "",
transform=ax_info.transAxes,
color=_FG, fontsize=8.0, va="top", family="monospace",
linespacing=1.5,
)
_last = {"disp": np.empty((0, 2)), "on_det": []}
# ── Sliders ───────────────────────────────────────────────────────────────
_sk_o = dict(
step=0.02, readout_format=".2f", continuous_update=False,
style={"description_width": "120px"},
layout=ipw.Layout(width="98%"),
)
_sk_c = dict(
continuous_update=False,
style={"description_width": "120px"},
layout=ipw.Layout(width="98%"),
)
# Orientation — delta rotations around crystal axes
s_ca = ipw.FloatSlider(value=0.0, min=-rot_range_deg, max=+rot_range_deg,
description="Cry [100] (a)", **_sk_o)
s_cb = ipw.FloatSlider(value=0.0, min=-rot_range_deg, max=+rot_range_deg,
description="Cry [010] (b)", **_sk_o)
s_cc = ipw.FloatSlider(value=0.0, min=-c_rot_range_deg, max=+c_rot_range_deg,
description="Cry [001] (c)", **_sk_o)
s_hkl = ipw.FloatSlider(value=0.0, min=-rot_range_deg, max=+rot_range_deg,
description="rot. [—]", disabled=True, **_sk_o)
# State shared between _do_update and the click handler
_selected: list = [None] # dict with keys: hkl, tth, chi
_hkl_html = ipw.HTML(
"<span style='color:#666;font-style:italic'>click a simulated spot to select it</span>",
layout=ipw.Layout(margin="0 0 2px 4px"),
)
btn_desel = ipw.Button(
description="✕ Deselect",
button_style="warning",
layout=ipw.Layout(width="100px", height="26px"),
)
btn_desel.on_click(lambda _: _deselect())
# Camera geometry — additive deltas from camera0
s_dd = ipw.FloatSlider(value=0.0, min=-dd_range, max=+dd_range,
step=0.05, readout_format=".2f",
description="Δ dd (mm)", **_sk_c)
s_xcen = ipw.FloatSlider(value=0.0, min=-cen_range_px, max=+cen_range_px,
step=0.5, readout_format=".1f",
description="Δ xcen (px)", **_sk_c)
s_ycen = ipw.FloatSlider(value=0.0, min=-cen_range_px, max=+cen_range_px,
step=0.5, readout_format=".1f",
description="Δ ycen (px)", **_sk_c)
s_xbet = ipw.FloatSlider(value=0.0, min=-angle_range_deg, max=+angle_range_deg,
step=0.01, readout_format=".3f",
description="Δ xbet (°)", **_sk_c)
s_xgam = ipw.FloatSlider(value=0.0, min=-angle_range_deg, max=+angle_range_deg,
step=0.01, readout_format=".3f",
description="Δ xgam (°)", **_sk_c)
_all_sliders = (s_ca, s_cb, s_cc, s_hkl, s_dd, s_xcen, s_ycen, s_xbet, s_xgam)
# ── Update loop ───────────────────────────────────────────────────────────
_updating = [False]
def _do_update() -> None:
# Current orientation: U0 + slider rotations around crystal axes
U = state.U0
for angle_deg, cry_ax in (
(s_ca.value, np.array([1., 0., 0.])),
(s_cb.value, np.array([0., 1., 0.])),
(s_cc.value, np.array([0., 0., 1.])),
):
if angle_deg == 0.0:
continue
ax_lab = U @ cry_ax
ax_lab /= np.linalg.norm(ax_lab)
U = Rotation.from_rotvec(np.radians(angle_deg) * ax_lab).as_matrix() @ U
# Rotation around the selected crystal-plane normal
if _selected[0] is not None and s_hkl.value != 0.0:
h, k, l = _selected[0]["hkl"]
G_cry = crystal.Q(h, k, l)
g_norm = np.linalg.norm(G_cry)
if g_norm > 1e-12:
ax_hkl = U @ (G_cry / g_norm)
ax_hkl /= np.linalg.norm(ax_hkl)
U = Rotation.from_rotvec(
np.radians(s_hkl.value) * ax_hkl
).as_matrix() @ U
state.U = U
# Current camera: camera0 + delta sliders
cam0 = state.camera0
cam = _Camera(
dd=cam0.dd + s_dd.value,
xcen=cam0.xcen + s_xcen.value,
ycen=cam0.ycen + s_ycen.value,
xbet=cam0.xbet + s_xbet.value,
xgam=cam0.xgam + s_xgam.value,
pixelsize=cam0.pixel_mm,
n_pix_h=cam0.Nh,
n_pix_v=cam0.Nv,
kf_direction=cam0.kf_direction,
)
state.camera = cam
dR = U @ np.linalg.inv(state.U0)
dw = float(np.degrees(Rotation.from_matrix(dR).magnitude()))
spots = _simulate(U, cam)
sim_xy = _extract_sim_xy(spots)[:top_n_sim]
on_det = [s for s in spots if s.get("pix") is not None][:top_n_sim]
# Build display coordinates (angular, gnomonic, or detector)
if space == "angular":
uf = cam.pixel_to_kf(obs_use[:, 0], obs_use[:, 1])
tth_o = np.degrees(np.arccos(np.clip(uf[:, 0], -1.0, 1.0)))
chi_o = np.degrees(np.arctan2(uf[:, 1], uf[:, 2] + 1e-17))
obs_disp = np.column_stack([tth_o, chi_o])
sim_disp = (
np.array([[s["tth"], s["chi"]] for s in on_det])
if on_det else np.empty((0, 2))
)
elif space == "gnomonic":
uf = cam.pixel_to_kf(obs_use[:, 0], obs_use[:, 1])
_tth_o = np.degrees(np.arccos(np.clip(uf[:, 0], -1.0, 1.0)))
_chi_o = np.degrees(np.arctan2(uf[:, 1], uf[:, 2] + 1e-17))
gX_o, gY_o = _gnomonic(_tth_o, _chi_o)
obs_disp = np.column_stack([gX_o, gY_o])
if on_det:
gX, gY = _gnomonic([s["tth"] for s in on_det], [s["chi"] for s in on_det])
sim_disp = np.column_stack([gX, gY])
else:
sim_disp = np.empty((0, 2))
else:
obs_disp = obs_use
sim_disp = sim_xy
_last["disp"] = sim_disp
_last["on_det"] = on_det
sc_obs.set_offsets(obs_disp)
sc_sim.set_offsets(sim_disp if len(sim_disp) else np.empty((0, 2)))
# Keep selected-spot marker in sync
sel_pos = np.empty((0, 2))
if _selected[0] is not None and len(sim_disp):
sel_hkl = _selected[0]["hkl"]
for i, s in enumerate(on_det):
if s.get("hkl") == sel_hkl:
sel_pos = sim_disp[i:i + 1]
break
sc_sel.set_offsets(sel_pos)
for ln in _lines:
ln.remove()
_lines.clear()
n_matched = 0
rms_px = float("nan")
sc_obs.set_edgecolors([_OBS] * len(obs_use))
if len(sim_xy) > 0 and len(obs_use) > 0:
row_ind, col_ind, dist_px = _match_spots(obs_use, sim_xy, max_match_px)
ok_mask = dist_px < max_match_px
n_matched = int(ok_mask.sum())
if n_matched > 0:
rms_px = float(np.sqrt((dist_px[ok_mask] ** 2).mean()))
edge_colors = [_OBS] * len(obs_use)
for r, c, ok in zip(row_ind, col_ind, ok_mask):
if ok:
edge_colors[r] = _MATCH
_lines.append(ax_det.plot(
[obs_disp[r, 0], sim_disp[c, 0]],
[obs_disp[r, 1], sim_disp[c, 1]],
color=_MATCH, lw=0.7, alpha=0.6, zorder=3,
)[0])
sc_obs.set_edgecolors(edge_colors)
rms_s = f"{rms_px:.1f} px" if np.isfinite(rms_px) else "—"
rate = n_matched / max(min(len(obs_use), len(sim_xy)), 1)
u_rows = [
f" [{U[i,0]:+.4f} {U[i,1]:+.4f} {U[i,2]:+.4f}]"
for i in range(3)
]
_info_txt.set_text(
f"Camera\n"
f"{'─' * 22}\n"
f" dd ={cam.dd:9.3f} mm\n"
f" xcen ={cam.xcen:9.2f} px\n"
f" ycen ={cam.ycen:9.2f} px\n"
f" xbet ={cam.xbet:9.4f} °\n"
f" xgam ={cam.xgam:9.4f} °\n"
f"\n"
f"Orientation (U matrix)\n"
f"{'─' * 22}\n"
+ "\n".join(u_rows) + "\n"
f" |δω| = {dw:.4f}°\n"
f"\n"
f"Match ({max_match_px:.0f} px window)\n"
f"{'─' * 22}\n"
f" matched : {n_matched} / {len(obs_use)}\n"
f" rate : {rate:.0%}\n"
f" rms : {rms_s}\n"
f"\n"
f" sim={len(sim_xy)} obs={len(obs_use)}\n"
)
fig.canvas.draw_idle()
def _reset_sliders() -> None:
_updating[0] = True
for s in _all_sliders:
s.value = 0.0
_updating[0] = False
_do_update()
def _on_slider(change) -> None: # noqa: ARG001
if not _updating[0]:
_do_update()
for s in _all_sliders:
s.observe(_on_slider, names="value")
def _deselect() -> None:
"""Clear the current spot selection and disable the HKL slider."""
_selected[0] = None
s_hkl.disabled = True
s_hkl.description = "rot. [—]"
_hkl_html.value = (
"<span style='color:#666;font-style:italic'>"
"click a simulated spot to select it"
"</span>"
)
_updating[0] = True
s_hkl.value = 0.0
_updating[0] = False
_do_update()
def _select_spot(idx: int) -> None:
"""Mark spot *idx* as selected and enable the HKL slider."""
od = _last["on_det"]
spot = od[idx]
_selected[0] = spot
h, k, l = spot["hkl"]
_hkl_html.value = (
f"<b style='color:#ffff00'>({h:+d} {k:+d} {l:+d})</b>"
f" 2θ = {spot['tth']:.2f}°"
f" χ = {spot['chi']:.2f}°"
)
s_hkl.description = f"rot. [{h} {k} {l}]"
s_hkl.disabled = False
_updating[0] = True
s_hkl.value = 0.0
_updating[0] = False
_do_update()
# ── Click / drag handlers ─────────────────────────────────────────────────
# A short click selects a simulated spot (enables the HKL rotation slider,
# same as interactive_orientation). Pressing and dragging a spot rotates U
# so that spot moves to the drop position. Uses state.camera for
# detector-space kf so the mapping is consistent with the live
# (slider-adjusted) view.
_drag = {
"active": False,
"spot_idx": None,
"orig_disp": None,
"press_screen": None,
}
def _display_to_kf_cal(x: float, y: float) -> np.ndarray:
if space == "angular":
tth = np.radians(x)
chi = np.radians(y)
return np.array([
np.cos(tth),
np.sin(tth) * np.sin(chi),
np.sin(tth) * np.cos(chi),
])
elif space == "gnomonic":
return _gnomonic_inv(x, y)
else: # detector — use current camera to match the live view
kf = state.camera.pixel_to_kf(np.array([x]), np.array([y]))[0]
return kf / np.linalg.norm(kf)
def _nearest_sim_idx_cal(xdata: float, ydata: float) -> int | None:
disp = _last["disp"]
if len(disp) == 0:
return None
dx = disp[:, 0] - xdata
dy = disp[:, 1] - ydata
return int(np.argmin(dx ** 2 + dy ** 2))
def _on_press(event) -> None:
if event.inaxes is not ax_det:
return
if event.button == 3: # right-click → deselect (always works)
_deselect()
return
if event.button != 1:
return
# When the matplotlib toolbar zoom/pan tool is active, left-click events
# belong to the toolbar — don't treat them as spot drags.
try:
if fig.canvas.toolbar.mode != "":
return
except Exception:
pass
idx = _nearest_sim_idx_cal(event.xdata, event.ydata)
if idx is None:
return
_drag["spot_idx"] = idx
_drag["orig_disp"] = _last["disp"][idx].copy()
_drag["press_screen"] = np.array([event.x, event.y])
_drag["active"] = False
def _on_motion(event) -> None:
if event.inaxes is not ax_det or _drag["spot_idx"] is None:
return
if event.xdata is None or event.ydata is None:
return
screen_dist = np.linalg.norm(
np.array([event.x, event.y]) - _drag["press_screen"]
)
if screen_dist < 6.0:
return
_drag["active"] = True
orig = _drag["orig_disp"]
cur = np.array([event.xdata, event.ydata])
sc_drag.set_offsets([cur])
_drag_line.set_data([orig[0], cur[0]], [orig[1], cur[1]])
fig.canvas.draw_idle()
def _on_release(event) -> None:
if event.button != 1 or _drag["spot_idx"] is None:
_drag["spot_idx"] = None
return
if not _drag["active"]:
# Short click → select the spot
_select_spot(_drag["spot_idx"])
elif event.xdata is not None and event.ydata is not None:
orig = _drag["orig_disp"]
drop = np.array([event.xdata, event.ydata])
try:
ki = np.array([1.0, 0.0, 0.0])
q_orig = _display_to_kf_cal(orig[0], orig[1]) - ki
q_target = _display_to_kf_cal(drop[0], drop[1]) - ki
n_o, n_t = np.linalg.norm(q_orig), np.linalg.norm(q_target)
if n_o > 1e-10 and n_t > 1e-10:
q_o_hat = q_orig / n_o
q_t_hat = q_target / n_t
axis = np.cross(q_o_hat, q_t_hat)
sin_a = np.linalg.norm(axis)
cos_a = float(np.clip(
np.dot(q_o_hat, q_t_hat), -1.0, 1.0
))
if sin_a > 1e-10:
R = Rotation.from_rotvec(
np.arctan2(sin_a, cos_a) * axis / sin_a
).as_matrix()
state.U0 = R @ state.U
_reset_sliders()
except Exception:
pass
sc_drag.set_offsets(np.empty((0, 2)))
_drag_line.set_data([], [])
_drag["spot_idx"] = None
_drag["active"] = False
fig.canvas.draw_idle()
fig.canvas.mpl_connect("button_press_event", _on_press)
fig.canvas.mpl_connect("motion_notify_event", _on_motion)
fig.canvas.mpl_connect("button_release_event", _on_release)
# ── Buttons ───────────────────────────────────────────────────────────────
_bkw = dict(layout=ipw.Layout(width="160px", height="32px"))
btn_reset = ipw.Button(description="Reset", button_style="warning", **_bkw)
btn_setref = ipw.Button(description="Set as reference", button_style="info", **_bkw)
btn_accept = ipw.Button(description="✓ Accept", button_style="success", **_bkw)
def _cb_reset(_b) -> None: # noqa: ARG001
state.U0 = state._U0_orig.copy()
state.camera0 = state._cam_orig
_reset_sliders()
def _cb_setref(_b) -> None: # noqa: ARG001
state.U0 = state.U.copy()
state.camera0 = state.camera
_reset_sliders()
def _cb_accept(_b) -> None: # noqa: ARG001
state.accepted = True
cam = state.camera
print("\n✓ Calibration initial guess accepted")
print(f" Camera:")
print(f" dd={cam.dd:.5g} xcen={cam.xcen:.5g} ycen={cam.ycen:.5g}")
print(f" xbet={cam.xbet:.5g} xgam={cam.xgam:.5g}")
print(f" U =\n{np.array2string(state.U, precision=8)}")
print(
f"\n Re-create camera:\n"
f" Camera(dd={cam.dd:.5g}, xcen={cam.xcen:.5g},"
f" ycen={cam.ycen:.5g},\n"
f" xbet={cam.xbet:.5g}, xgam={cam.xgam:.5g},"
f" pixelsize={cam.pixel_mm:.6g},\n"
f" n_pix_h={cam.Nh}, n_pix_v={cam.Nv},"
f" kf_direction={cam.kf_direction!r})"
)
print(
"\n Pass to fitter:\n"
" result = state.camera.fit_calibration(crystal, state.U, obs_xy)"
)
btn_reset.on_click(_cb_reset)
btn_setref.on_click(_cb_setref)
btn_accept.on_click(_cb_accept)
# ── Quick-fit button ──────────────────────────────────────────────────────
btn_fit = ipw.Button(
description="⚡ Quick Fit", button_style="primary", **_bkw
)
def _cb_fit(_b) -> None: # noqa: ARG001
import asyncio
import queue as _qmod
import threading
from scipy.optimize import minimize
from scipy.spatial import cKDTree
from .simulation import precompute_allowed_hkl
fit_params = ["dd", "xcen", "ycen", "xbet", "xgam"]
cam0 = state.camera # snapshot at click time
U_fit = state.U.copy()
x0 = np.array([getattr(cam0, p) for p in fit_params], dtype=float)
_allowed = precompute_allowed_hkl(crystal, E_max_eV=E_max_eV, f2_thresh=f2_thresh)
_q: _qmod.Queue = _qmod.Queue()
def _build(x: np.ndarray):
kw = dict(
dd=cam0.dd, xcen=cam0.xcen, ycen=cam0.ycen,
xbet=cam0.xbet, xgam=cam0.xgam,
pixelsize=cam0.pixel_mm, n_pix_h=cam0.Nh, n_pix_v=cam0.Nv,
kf_direction=cam0.kf_direction,
)
for i, p in enumerate(fit_params):
kw[p] = float(x[i])
return _Camera(**kw)
def _cost(x: np.ndarray) -> float:
try:
spots = simulate_laue(
crystal, U_fit, _build(x),
E_min=E_min_eV, E_max=E_max_eV,
source=source, source_kwargs=source_kwargs,
f2_thresh=f2_thresh, kb_params=kb_params,
allowed_hkl=_allowed,
)
except Exception:
return float(max_match_px ** 2)
sim_c = np.array([s["pix"] for s in spots if s.get("pix") is not None])
if not len(sim_c):
return float(max_match_px ** 2)
dists, _ = cKDTree(sim_c).query(obs_use, k=1)
return float(np.mean(np.minimum(dists, max_match_px) ** 2))
_n = [0]
def _opt_cb(xk: np.ndarray) -> None:
_n[0] += 1
if _n[0] % 3 == 0: # update display every 3 Nelder-Mead iterations
_q.put(xk.copy())
def _optimize() -> None:
_steps = {"dd": 2.0, "xcen": 50.0, "ycen": 50.0, "xbet": 0.5, "xgam": 0.5}
n = len(x0)
simplex = np.tile(x0, (n + 1, 1))
for i, p in enumerate(fit_params):
simplex[i + 1, i] += _steps[p]
res = minimize(
_cost, x0, method="Nelder-Mead",
callback=_opt_cb,
options={"maxiter": 500, "xatol": 0.02, "fatol": 1e-4,
"initial_simplex": simplex},
)
_q.put(res.x.copy())
_q.put(None) # sentinel: done
async def _ui_loop() -> None:
btn_fit.disabled = True
btn_fit.description = "Fitting…"
t = threading.Thread(target=_optimize, daemon=True)
t.start()
while True:
await asyncio.sleep(0.15) # poll every 150 ms
latest, done = None, False
while True:
try:
item = _q.get_nowait()
if item is None:
done = True
break
latest = item
except _qmod.Empty:
break
if latest is not None:
state.camera0 = _build(latest)
state.U0 = U_fit.copy()
_updating[0] = True
for s in _all_sliders:
s.value = 0.0
_updating[0] = False
_do_update()
if done:
btn_fit.description = "⚡ Quick Fit"
btn_fit.disabled = False
return
asyncio.ensure_future(_ui_loop())
btn_fit.on_click(_cb_fit)
# ── "Center at hkl" preset buttons ───────────────────────────────────────
_presets: list[tuple[str, tuple[int, int, int]]] = [
("100", ( 1, 0, 0)),
("010", ( 0, 1, 0)),
("001", ( 0, 0, 1)),
("111", ( 1, 1, 1)),
("-110", (-1, 1, 0)),
("110", ( 1, 1, 0)),
]
def _make_center_cb(hkl: tuple[int, int, int]):
h, k, l_idx = hkl
def _cb(_b) -> None: # noqa: ARG001
G_cry = crystal.Q(h, k, l_idx)
g_norm = np.linalg.norm(G_cry)
if g_norm < 1e-12:
return
G_lab = state.U @ (G_cry / g_norm)
G_lab /= np.linalg.norm(G_lab)
# Target: momentum-transfer direction pointing at the detector centre.
# camera.IOlab is in LT2 frame; convert to LT (x // ki).
IO_LT2 = state.camera.IOlab
IO_LT = np.array([IO_LT2[1], -IO_LT2[0], IO_LT2[2]])
d_hat = IO_LT / np.linalg.norm(IO_LT)
target = d_hat - np.array([1.0, 0.0, 0.0])
t_norm = np.linalg.norm(target)
if t_norm < 1e-12:
return
target_hat = target / t_norm
ax_r = np.cross(G_lab, target_hat)
sin_a = np.linalg.norm(ax_r)
cos_a = float(np.dot(G_lab, target_hat))
if sin_a < 1e-10:
if cos_a > 0:
R_ctr = np.eye(3)
else:
perp = np.array([0., 0., 1.] if abs(G_lab[2]) < 0.9
else [0., 1., 0.])
v = np.cross(G_lab, perp)
R_ctr = Rotation.from_rotvec(
np.pi * v / np.linalg.norm(v)
).as_matrix()
else:
R_ctr = Rotation.from_rotvec(
np.arctan2(sin_a, cos_a) * (ax_r / sin_a)
).as_matrix()
state.U0 = R_ctr @ state.U
_reset_sliders()
return _cb
_ckw = dict(layout=ipw.Layout(width="68px", height="28px"))
_center_btns = []
for _lbl, _hkl in _presets:
_b = ipw.Button(description=_lbl, **_ckw)
_b.on_click(_make_center_cb(_hkl))
_center_btns.append(_b)
# ── Layout and display ────────────────────────────────────────────────────
_do_update()
_col_orient = ipw.VBox(
[ipw.HTML("<b style='font-size:13px'>Orientation</b>",
layout=ipw.Layout(margin="0 0 4px 0")),
s_ca, s_cb, s_cc, s_hkl],
layout=ipw.Layout(width="50%", padding="4px 12px 4px 4px"),
)
_col_cam = ipw.VBox(
[ipw.HTML("<b style='font-size:13px'>Camera geometry</b>",
layout=ipw.Layout(margin="0 0 4px 0")),
s_dd, s_xcen, s_ycen, s_xbet, s_xgam],
layout=ipw.Layout(width="50%", padding="4px 4px 4px 12px"),
)
_controls = ipw.VBox([
ipw.HBox([_col_orient, _col_cam],
layout=ipw.Layout(width="100%")),
ipw.HBox([_hkl_html, btn_desel],
layout=ipw.Layout(align_items="center", gap="6px")),
ipw.HBox(
[btn_reset, btn_setref, btn_accept, btn_fit],
layout=ipw.Layout(margin="8px 0 6px 0", gap="6px"),
),
ipw.HBox(
[ipw.HTML("<b>Center at hkl:</b>",
layout=ipw.Layout(align_self="center", margin="0 10px 0 0"))]
+ _center_btns,
layout=ipw.Layout(gap="4px"),
),
], layout=ipw.Layout(width="100%", padding="4px 8px"))
_ipy_display(ipw.VBox([fig.canvas, _controls]))
return state