feat: add beam centre from detector coords
CI / lint (pull_request) Successful in 30s
CI / test (3.12) (pull_request) Successful in 24s
CI / test (3.11) (pull_request) Successful in 25s
CI / test (3.13) (pull_request) Successful in 33s
CI / lint (push) Canceled after 9s
CI / test (3.11) (push) Canceled after 0s
CI / test (3.12) (push) Canceled after 0s
CI / test (3.13) (push) Canceled after 0s
Build and Publish / release (push) Successful in 10s
CI / lint (pull_request) Successful in 30s
CI / test (3.12) (pull_request) Successful in 24s
CI / test (3.11) (pull_request) Successful in 25s
CI / test (3.13) (pull_request) Successful in 33s
CI / lint (push) Canceled after 9s
CI / test (3.11) (push) Canceled after 0s
CI / test (3.12) (push) Canceled after 0s
CI / test (3.13) (push) Canceled after 0s
Build and Publish / release (push) Successful in 10s
This commit was merged in pull request #22.
This commit is contained in:
1 parent
ebc3053e9a
commit
2ff521661b
5 files changed
+626
No files matched your search
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"x10sa": {
|
||||
"measured": {
|
||||
"det_z": [
|
||||
170, 190, 210, 230, 250, 300, 350, 400, 250, 250, 190, 210, 240, 280,
|
||||
320, 360, 400, 190, 210, 250, 300, 200
|
||||
],
|
||||
"det_y": [
|
||||
62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 100, 100, 100, 100, 100, 100,
|
||||
100, 120, 120, 120, 120, 62
|
||||
],
|
||||
|
||||
"beam_x": [
|
||||
2094.4, 2081.0, 2105.2, 2096.3, 2079.9, 2081.0, 2103.0, 2100.0, 2052.0,
|
||||
2063.4, 2080.0, 2072.6, 2079.6, 2063.3, 2083.7, 2101.6, 2034.7, 2074.6,
|
||||
2083.5, 2087.9, 2114, 2080
|
||||
],
|
||||
|
||||
"beam_y": [
|
||||
2259.9, 2222.0, 2188.5, 2209.3, 2193.4, 2222.0, 2252.5, 2231.7, 2247.3,
|
||||
2221.8, 3254.0, 3259.5, 3267.5, 3279.1, 3285.5, 3290.6, 3258.9, 3791.0,
|
||||
3798.2, 3806.6, 3742, 2242
|
||||
]
|
||||
}
|
||||
},
|
||||
"x06da": {
|
||||
"measured": {
|
||||
"det_z": [95, 120, 150, 180, 210, 250, 210, 180, 150, 120, 95],
|
||||
|
||||
"det_y": [0, 0, 0, 0, 0, 100, 100, 100, 100, 100, 100],
|
||||
|
||||
"beam_x": [
|
||||
765.06, 764.76, 764.09, 764.23, 764.13, 768.32, 765.03, 764.16, 764.42,
|
||||
765.31, 765.24
|
||||
],
|
||||
|
||||
"beam_y": [
|
||||
850.75, 850.41, 849.88, 849.83, 846.17, 1382.51, 1383.2, 1382.99,
|
||||
1384.16, 1384.64, 1385.15
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
"""Affine model for where a fixed collimated beam pierces a moving 2D detector.
|
||||
|
||||
The detector translates on two motors, det_z_mm (along the beam) and det_y_mm.
|
||||
The beam pierce point is read out as pixel coordinates beam_x_px, beam_y_px.
|
||||
|
||||
beam_px = matrix_px_per_mm @ (det_z_mm, det_y_mm) + offset_px
|
||||
|
||||
Under rigid translation of the detector this relation is exact, and it absorbs
|
||||
any linear stage error -- scale, non-orthogonality, cross-coupling -- along with
|
||||
the detector's pixel pitch and roll. None of those need to be known separately.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import cast
|
||||
|
||||
import numpy as np
|
||||
from numpy.typing import ArrayLike, NDArray
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
|
||||
def fit_beam_centre_model(
|
||||
det_z_mm: ArrayLike, det_y_mm: ArrayLike, beam_x_px: ArrayLike, beam_y_px: ArrayLike
|
||||
) -> BeamCenterFromDetectorStage:
|
||||
"""Fit the model to measured stage positions and beam centre pixel coordinates."""
|
||||
stage_mm = np.column_stack([np.ravel(det_z_mm), np.ravel(det_y_mm)]).astype(float)
|
||||
beam_px = np.column_stack([np.ravel(beam_x_px), np.ravel(beam_y_px)]).astype(float)
|
||||
|
||||
if stage_mm.shape[0] != beam_px.shape[0]:
|
||||
raise ValueError("stage and pixel inputs must have the same length")
|
||||
|
||||
is_finite = np.isfinite(stage_mm).all(axis=1) & np.isfinite(beam_px).all(axis=1)
|
||||
stage_mm, beam_px = stage_mm[is_finite], beam_px[is_finite]
|
||||
if stage_mm.shape[0] < 3:
|
||||
raise ValueError("need at least 3 finite samples")
|
||||
|
||||
stage_mean_mm: NDArray[np.float64] = stage_mm.mean(axis=0)
|
||||
design = BeamCenterFromDetectorStage.design_matrix(
|
||||
stage_mean_mm, stage_mm[:, 0], stage_mm[:, 1]
|
||||
)
|
||||
|
||||
coefficients, *_ = np.linalg.lstsq(design, beam_px, rcond=None)
|
||||
matrix_px_per_mm = cast(NDArray[np.float64], coefficients[:2].T)
|
||||
offset_px = coefficients[2]
|
||||
residuals_px = beam_px - design @ coefficients
|
||||
return BeamCenterFromDetectorStage(
|
||||
stage_mean_mm=stage_mean_mm,
|
||||
matrix_px_per_mm=matrix_px_per_mm,
|
||||
offset_px=offset_px,
|
||||
residuals_px=residuals_px,
|
||||
)
|
||||
|
||||
|
||||
class BeamCenterFromDetectorStage(BaseModel):
|
||||
"""Least-squares fit of beam centre pixel position against the two stage axes."""
|
||||
|
||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||
|
||||
stage_mean_mm: NDArray[np.float64]
|
||||
matrix_px_per_mm: NDArray[np.float64]
|
||||
offset_px: NDArray[np.float64]
|
||||
residuals_px: NDArray[np.float64]
|
||||
|
||||
@staticmethod
|
||||
def design_matrix(
|
||||
stage_mean_mm, det_z_mm: NDArray[np.float64], det_y_mm: NDArray[np.float64]
|
||||
) -> NDArray[np.float64]:
|
||||
return np.column_stack(
|
||||
[det_z_mm - stage_mean_mm[0], det_y_mm - stage_mean_mm[1], np.ones(det_z_mm.size)]
|
||||
)
|
||||
|
||||
def predict(
|
||||
self, det_z_mm: ArrayLike, det_y_mm: ArrayLike
|
||||
) -> tuple[NDArray[np.float64], NDArray[np.float64]]:
|
||||
"""Predict (beam_x_px, beam_y_px) at arbitrary stage positions."""
|
||||
det_z_mm, det_y_mm = (
|
||||
np.atleast_1d(det_z_mm).astype(float),
|
||||
np.atleast_1d(det_y_mm).astype(float),
|
||||
)
|
||||
det_z_mm, det_y_mm = np.broadcast_arrays(det_z_mm, det_y_mm)
|
||||
|
||||
coefficients = np.vstack([self.matrix_px_per_mm.T, self.offset_px])
|
||||
beam_px = (
|
||||
self.design_matrix(self.stage_mean_mm, det_z_mm.ravel(), det_y_mm.ravel())
|
||||
@ coefficients
|
||||
)
|
||||
return beam_px[:, 0], beam_px[:, 1]
|
||||
@@ -0,0 +1,44 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from pydantic import AliasChoices, BaseModel, ConfigDict, Field, model_validator
|
||||
|
||||
from aarecommon.math.beam_center import BeamCenterFromDetectorStage, fit_beam_centre_model
|
||||
|
||||
|
||||
class BeamCentreMeasurements(BaseModel):
|
||||
"""Raw stage positions and the beam pierce point measured at each of them.
|
||||
|
||||
The short aliases (det_z, det_y, beam_x, beam_y) are the key names used in
|
||||
config/beamline_configs/beam_centres.json.
|
||||
"""
|
||||
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
det_z_mm: list[float] = Field(validation_alias=AliasChoices("det_z_mm", "det_z"))
|
||||
det_y_mm: list[float] = Field(validation_alias=AliasChoices("det_y_mm", "det_y"))
|
||||
beam_x_px: list[float] = Field(validation_alias=AliasChoices("beam_x_px", "beam_x"))
|
||||
beam_y_px: list[float] = Field(validation_alias=AliasChoices("beam_y_px", "beam_y"))
|
||||
|
||||
|
||||
class BeamCentre(BaseModel):
|
||||
measurements: BeamCentreMeasurements
|
||||
model: BeamCenterFromDetectorStage
|
||||
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def _generate_model(cls, data: Any) -> Any:
|
||||
if not isinstance(data, dict) or "model" in data or "measurements" not in data:
|
||||
return data
|
||||
measurements = BeamCentreMeasurements.model_validate(data["measurements"])
|
||||
return {
|
||||
**data,
|
||||
"measurements": measurements,
|
||||
"model": fit_beam_centre_model(
|
||||
det_z_mm=measurements.det_z_mm,
|
||||
det_y_mm=measurements.det_y_mm,
|
||||
beam_x_px=measurements.beam_x_px,
|
||||
beam_y_px=measurements.beam_y_px,
|
||||
),
|
||||
}
|
||||
Reference in new issue
Block a user