feat(sim): rotating structured pattern for simulated X-ray-eye camera
The simulated cam_xeye (SimIDSCamera/_SimIDSBackend) served a single static Gaussian blob, cached at construction -- rotating lsamrot in a simulated session produced no visible motion, so the smear/composite rotation-center calibration aid had nothing to smear into an arc. Add an opt-in sim_rotation_coupling config (mirrors the existing sim_coarse_coupling idiom in sim_lamni.py's measured_positions()): when set, _SimIDSBackend regenerates a fresh frame on every get_image_data() call with an eccentric blob (make_rotating_test_pattern) tracking the coupled axis' live simulated Galil position instead of serving the cached static frame. Wired into LamNI's cam_xeye sim config only (host/port/axis matching lsamrot's own sim config); flomni's cam_xeye and any other SimIDSCamera user are unaffected since the new params default to None (byte-identical output to before, confirmed by test). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
"""Unit tests for the simulated IDS camera frame source (_SimIDSBackend /
|
||||
make_rotating_test_pattern / SimIDSCamera's optional rotation coupling)."""
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from csaxs_bec.devices.sim.sim_cameras import (
|
||||
SimIDSCamera,
|
||||
_SimIDSBackend,
|
||||
make_rotating_test_pattern,
|
||||
make_test_pattern,
|
||||
)
|
||||
from csaxs_bec.devices.sim.sim_galil import SimGalilState
|
||||
from csaxs_bec.devices.sim.sim_socket import SimStateRegistry
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def reset_sim_state():
|
||||
"""Avoid cross-test leakage of shared (host, port)-keyed simulation state."""
|
||||
SimStateRegistry.reset()
|
||||
yield
|
||||
SimStateRegistry.reset()
|
||||
|
||||
|
||||
def test_sim_ids_backend_default_matches_static_pattern():
|
||||
"""No rotation_coupling (the default) -> byte-identical to today's static frame.
|
||||
|
||||
Regression guard: this is what keeps flomni's cam_xeye and other existing
|
||||
SimIDSCamera users completely unaffected by the new opt-in feature.
|
||||
"""
|
||||
backend = _SimIDSBackend(width=64, height=48, rgb=True)
|
||||
expected = make_test_pattern(48, 64, rgb=True)
|
||||
assert np.array_equal(backend.get_image_data(), expected)
|
||||
# calling it again returns the exact same cached frame, not a fresh one
|
||||
assert np.array_equal(backend.get_image_data(), expected)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("angle_deg", [30.0, 120.0, 210.0, 300.0])
|
||||
def test_make_rotating_test_pattern_blob_position(angle_deg):
|
||||
height, width = 200, 300
|
||||
radius_px = 60.0
|
||||
frame = make_rotating_test_pattern(
|
||||
height, width, rgb=False, angle_deg=angle_deg, radius_px=radius_px
|
||||
)
|
||||
|
||||
cy, cx = height / 2.0, width / 2.0
|
||||
angle_rad = np.deg2rad(angle_deg)
|
||||
expected_y = cy - radius_px * np.sin(angle_rad)
|
||||
expected_x = cx + radius_px * np.cos(angle_rad)
|
||||
|
||||
# mask out the fixed center crosshair so it can't win the argmax
|
||||
masked = frame.astype(np.int32)
|
||||
masked[int(cy) - 2 : int(cy) + 3, :] = 0
|
||||
masked[:, int(cx) - 2 : int(cx) + 3] = 0
|
||||
py, px = np.unravel_index(np.argmax(masked), masked.shape)
|
||||
|
||||
assert abs(py - expected_y) <= 2
|
||||
assert abs(px - expected_x) <= 2
|
||||
|
||||
|
||||
def test_sim_ids_backend_rotation_coupling_tracks_live_angle():
|
||||
host, port, axis_letter = "test-galil.example", 9000, "C"
|
||||
galil = SimStateRegistry.get(SimGalilState, host, port)
|
||||
ax = galil.axis(ord(axis_letter.lower()) - 97)
|
||||
ax.stppermm = 1000.0
|
||||
ax.pos_steps = 0.0 # 0 deg
|
||||
|
||||
coupling = {"galil_host": host, "galil_port": port, "axis": axis_letter}
|
||||
backend = _SimIDSBackend(
|
||||
width=200, height=200, rgb=False, rotation_coupling=coupling, feature_radius_px=50.0
|
||||
)
|
||||
|
||||
frame0 = backend.get_image_data()
|
||||
assert np.array_equal(
|
||||
frame0, make_rotating_test_pattern(200, 200, rgb=False, angle_deg=0.0, radius_px=50.0)
|
||||
)
|
||||
|
||||
# move the simulated axis and confirm the frame is regenerated fresh, not cached
|
||||
ax.pos_steps = 90.0 * ax.stppermm
|
||||
frame90 = backend.get_image_data()
|
||||
assert np.array_equal(
|
||||
frame90, make_rotating_test_pattern(200, 200, rgb=False, angle_deg=90.0, radius_px=50.0)
|
||||
)
|
||||
assert not np.array_equal(frame0, frame90)
|
||||
|
||||
|
||||
def test_sim_ids_camera_threads_rotation_coupling_through():
|
||||
"""SimIDSCamera's sim_rotation_coupling/sim_feature_radius_px reach _SimIDSBackend."""
|
||||
host, port, axis_letter = "test-galil-2.example", 9100, "C"
|
||||
galil = SimStateRegistry.get(SimGalilState, host, port)
|
||||
ax = galil.axis(ord(axis_letter.lower()) - 97)
|
||||
ax.stppermm = 1000.0
|
||||
ax.pos_steps = 0.0
|
||||
|
||||
camera = SimIDSCamera(
|
||||
name="test_sim_cam",
|
||||
camera_id=1,
|
||||
prefix="test:",
|
||||
scan_info=None,
|
||||
m_n_colormode=1,
|
||||
bits_per_pixel=24,
|
||||
live_mode=False,
|
||||
sim_shape=(64, 64),
|
||||
sim_rotation_coupling={"galil_host": host, "galil_port": port, "axis": axis_letter},
|
||||
sim_feature_radius_px=20.0,
|
||||
)
|
||||
|
||||
frame0 = camera.cam.get_image_data()
|
||||
ax.pos_steps = 180.0 * ax.stppermm
|
||||
frame180 = camera.cam.get_image_data()
|
||||
assert not np.array_equal(frame0, frame180)
|
||||
Reference in New Issue
Block a user