Split out matix calculations from CSM, and continue testing recentre
This commit is contained in:
parent
c659a556cc
commit
a5a372b6c0
3 changed files with 122 additions and 14 deletions
|
|
@ -31,8 +31,9 @@ from camera_stage_mapping.exceptions import MappingError
|
|||
|
||||
import labthings_fastapi as lt
|
||||
from labthings_fastapi.types.numpy import DenumpifyingDict
|
||||
|
||||
from camera_stage_mapping.camera_stage_tracker import Tracker
|
||||
|
||||
from openflexure_microscope_server.utilities import apply_2d_matrix_to_mapping
|
||||
from .camera import CameraDependency as CameraClient
|
||||
from .stage import StageDependency as Stage
|
||||
|
||||
|
|
@ -218,8 +219,8 @@ class CameraStageMapper(lt.Thing):
|
|||
.. code-block:: python
|
||||
|
||||
stage_disp = np.dot(
|
||||
np.array(image_to_stage_displacement_matrix),
|
||||
np.array([dy,dx]),
|
||||
np.array(image_to_stage_displacement_matrix),
|
||||
)
|
||||
|
||||
"""
|
||||
|
|
@ -281,22 +282,20 @@ class CameraStageMapper(lt.Thing):
|
|||
) -> Mapping[str, int]:
|
||||
"""Convert image coordinates to stage coordinates. Only x and y are returned."""
|
||||
self.assert_calibrated()
|
||||
relative_move: np.ndarray = np.dot(
|
||||
np.array([y, x]), np.array(self.image_to_stage_displacement_matrix)
|
||||
return apply_2d_matrix_to_mapping(
|
||||
self.image_to_stage_displacement_matrix, x=x, y=y, to_int=True
|
||||
)
|
||||
return {"x": int(relative_move[0]), "y": int(relative_move[1])}
|
||||
|
||||
@lt.thing_action
|
||||
def convert_stage_to_image_coordinates(
|
||||
self, x: float, y: float, **_kwargs: float
|
||||
) -> Mapping[str, int]:
|
||||
self, x: int, y: int, **_kwargs: int
|
||||
) -> Mapping[str, float]:
|
||||
"""Convert stage coordinates to image coordinates. Only x and y are returned."""
|
||||
self.assert_calibrated()
|
||||
inverse_matrix = np.linalg.inv(
|
||||
np.array(self.image_to_stage_displacement_matrix)
|
||||
)
|
||||
relative_move = np.dot(np.array([x, y]), inverse_matrix)
|
||||
return {"x": int(relative_move[1]), "y": int(relative_move[0])}
|
||||
return apply_2d_matrix_to_mapping(inverse_matrix, x=x, y=y, to_int=False)
|
||||
|
||||
@lt.thing_property
|
||||
def thing_state(self) -> Mapping[str, Any]:
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ from typing import (
|
|||
overload,
|
||||
TypeAlias,
|
||||
Literal,
|
||||
Mapping,
|
||||
)
|
||||
import os
|
||||
import re
|
||||
|
|
@ -22,6 +23,7 @@ import tomllib
|
|||
from functools import wraps
|
||||
import json
|
||||
|
||||
import numpy as np
|
||||
from pydantic import BaseModel
|
||||
|
||||
T = TypeVar("T")
|
||||
|
|
@ -475,3 +477,54 @@ def resolve_path_from_dir(path: str, directory: str) -> str:
|
|||
if not os.path.isabs(path):
|
||||
path = os.path.join(directory, path)
|
||||
return os.path.normpath(path)
|
||||
|
||||
|
||||
# Use overload to that the dict is either of integers or of float depending on the
|
||||
# to_int argument
|
||||
@overload
|
||||
def apply_2d_matrix_to_mapping(
|
||||
matrix: np.ndarray,
|
||||
*,
|
||||
x: float | int,
|
||||
y: float | int,
|
||||
to_int: Literal[False],
|
||||
**_kwargs: int,
|
||||
) -> Mapping[str, float]: ...
|
||||
@overload
|
||||
def apply_2d_matrix_to_mapping(
|
||||
matrix: np.ndarray,
|
||||
*,
|
||||
x: float | int,
|
||||
y: float | int,
|
||||
to_int: Literal[True],
|
||||
**_kwargs: int,
|
||||
) -> Mapping[str, int]: ...
|
||||
@overload
|
||||
def apply_2d_matrix_to_mapping(
|
||||
matrix: np.ndarray, *, x: float | int, y: float | int, **_kwargs: int
|
||||
) -> Mapping[str, float]: ...
|
||||
|
||||
|
||||
def apply_2d_matrix_to_mapping(
|
||||
matrix: np.ndarray,
|
||||
*,
|
||||
x: float | int,
|
||||
y: float | int,
|
||||
to_int: bool = False,
|
||||
**_kwargs: int,
|
||||
) -> Mapping[str, int] | Mapping[str, float]:
|
||||
"""Perform a dot product with a (x,y) vector and a matrix. The result is a mapping.
|
||||
|
||||
This is designed x and y must be kwargs and extra kwargs are ignored allowing:
|
||||
``apply_2d_matrix_to_mapping(matrix, **position)`` to run for a mapping position.
|
||||
|
||||
:param matrix: The matrix to use in the calculation
|
||||
:param x: the x coordinate (keyword only)
|
||||
:param y: the y coordinate (keyword only)
|
||||
:param to_int: Whether to convert the resulting coordinates to integers
|
||||
:return: The resulting coordinates as a mapping.
|
||||
"""
|
||||
relative_move: np.ndarray = np.dot(np.array([y, x]), np.array(matrix))
|
||||
if to_int:
|
||||
return {"x": round(relative_move[0]), "y": round(relative_move[1])}
|
||||
return {"x": float(relative_move[0]), "y": float(relative_move[1])}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue