Skip to content

Commit

Permalink
fix: fix units for correct WellPlatePlan iteration (pymmcore-plus#172)
Browse files Browse the repository at this point in the history
* fix: use same unit in plot

* fix: unit

* test: fix

* fix: image_positions

* fix: update plot

* fix: fix comment
  • Loading branch information
fdrgsp authored Jul 5, 2024
1 parent 10e861b commit 0b1ecdf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
32 changes: 21 additions & 11 deletions src/useq/_plate.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def _transorm_coords(self, coords: np.ndarray) -> np.ndarray:
def all_well_positions(self) -> Sequence[Position]:
"""Return all wells (centers) as Position objects."""
return [
Position(x=x, y=y, name=name)
Position(x=x * 1000, y=y * 1000, name=name) # convert to µm
for (y, x), name in zip(
self.all_well_coordinates, self.all_well_names.reshape(-1)
)
Expand All @@ -338,7 +338,7 @@ def all_well_positions(self) -> Sequence[Position]:
def selected_well_positions(self) -> Sequence[Position]:
"""Return selected wells (centers) as Position objects."""
return [
Position(x=x, y=y, name=name)
Position(x=x * 1000, y=y * 1000, name=name) # convert to µm
for (y, x), name in zip(
self.selected_well_coordinates, self.selected_well_names
)
Expand All @@ -356,12 +356,21 @@ def image_positions(self) -> Sequence[Position]:
offsets: Iterable[Position | GridPosition] = (
[wpp] if isinstance(wpp, Position) else wpp
)
# TODO: note that all positions within the same well will currently have the
# same name. This could be improved by modifying `Position.__add__` and
# adding a `name` to GridPosition.
return [
well + offset for well in self.selected_well_positions for offset in offsets
]
pos: List[Position] = []
for offset in offsets:
if isinstance(offset, GridPosition):
# invert y axis to use the cartesian coordinate system
# (y is up, -y is down, -x is left, +x is right)
offset = GridPosition(
x=offset.x,
y=-offset.y,
row=offset.row,
col=offset.col,
is_relative=offset.is_relative,
name=offset.name,
)
pos.extend(well + offset for well in self.selected_well_positions)
return pos

@property
def affine_transform(self) -> np.ndarray:
Expand Down Expand Up @@ -395,7 +404,8 @@ def plot(self, show_axis: bool = True) -> None:
ax.axis("off")

# ################ draw outline of all wells ################
height, width = self.plate.well_size
height, width = self.plate.well_size # mm
height, width = height * 1000, width * 1000 # µm

kwargs = {}
offset_x, offset_y = 0.0, 0.0
Expand All @@ -420,13 +430,13 @@ def plot(self, show_axis: bool = True) -> None:
)
ax.add_patch(sh)

# ################ plot image positions ################
################ plot image positions ################
w = h = None
if isinstance(self.well_points_plan, _PointsPlan):
w, h = self.well_points_plan.fov_width, self.well_points_plan.fov_height

for img_point in self.image_positions:
x, y = float(img_point.x), -float(img_point.y) # type: ignore[arg-type]
x, y = float(img_point.x), -float(img_point.y) # type: ignore[arg-type] # µm
if w and h:
ax.add_patch(
patches.Rectangle(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_well_plate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def test_plate_plan() -> None:
assert isinstance(pos0, useq.Position)
assert pos0 == pp[0]
assert pos0.name == "B1"
assert round(pos0.x, 2) == 500.78 # first row, but rotataed slightly
assert round(pos0.y, 2) == 208.97 # second column
assert round(pos0.x, 2) == 500784.4 # first row, but rotataed slightly, µm
assert round(pos0.y, 2) == 208965.75 # second column, µm

js = pp.model_dump_json()

Expand Down

0 comments on commit 0b1ecdf

Please sign in to comment.