-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add Position plate_row/plate_col and configurable name_pattern to grid plans #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2a92639
feat: add plate_row and plate_col fields to PositionBase
ieivanov fa4f009
feat: add plate_row and plate_col fields to PositionBase
ieivanov e1fdddc
feat: add configurable name_pattern to grid plans
ieivanov 436d880
style(pre-commit.ci): auto fixes [...]
pre-commit-ci[bot] 3ad1742
feat: auto-generate position name from plate_row/plate_col
ieivanov e29b780
style(pre-commit.ci): auto fixes [...]
pre-commit-ci[bot] c44ebc9
docs: update docstrings for plate_row, plate_col, and name_pattern
ieivanov 4161d7d
feat: enforce name matches plate_row/plate_col
ieivanov b8dde70
feat: allow plate_row and plate_col to be int or str
ieivanov 3e2de02
test: add tests for plate_row/plate_col, name_pattern, and composite …
ieivanov 0d8a19a
style(pre-commit.ci): auto fixes [...]
pre-commit-ci[bot] 439849c
feat: relax name validation for string plate_row/plate_col
ieivanov 013dfda
style(pre-commit.ci): auto fixes [...]
pre-commit-ci[bot] 5cbb6f1
fix: resolve mypy type narrowing for plate_row/plate_col
ieivanov 710ee3c
style(pre-commit.ci): auto fixes [...]
pre-commit-ci[bot] 4b15a8a
Merge branch 'add-plate-row-col' of https://github.com/ieivanov/useq-…
ieivanov 3fe9125
Apply suggestion from @tlambert03
tlambert03 61efb76
Merge branch 'main' into add-plate-row-col
tlambert03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,15 @@ | |
| from useq import MDASequence | ||
|
|
||
|
|
||
| def _index_to_row_name(index: int) -> str: | ||
| """Convert a zero-based row index to name (A, B, ..., Z, AA, AB, ...).""" | ||
| name = "" | ||
| while index >= 0: | ||
| name = chr(index % 26 + 65) + name | ||
| index = index // 26 - 1 | ||
| return name | ||
|
|
||
|
|
||
| class PositionBase(MutableModel): | ||
| """Define a position in 3D space. | ||
|
|
||
|
|
@@ -32,13 +41,23 @@ class PositionBase(MutableModel): | |
| z : float | None | ||
| Z position in microns. | ||
| name : str | None | ||
| Optional name for the position. | ||
| Optional name for the position. When `plate_row` and `plate_col` are | ||
| both int, the name is derived from them (e.g., "A1") and providing a | ||
| mismatched name raises ValueError. When either is a str (custom | ||
| naming), the name defaults to ``f"{plate_row}{plate_col}"`` but can | ||
| be freely overridden. | ||
| sequence : MDASequence | None | ||
| Optional MDASequence relative this position. | ||
| plate_row : int | None | ||
| Optional 0-based row index for well plate positions. | ||
| plate_col : int | None | ||
| Optional 0-based column index for well plate positions. | ||
| plate_row : int | str | None | ||
| Row for well plate positions. Can be a 0-based index (e.g., 0 → "A", | ||
| 1 → "B") or a string name (e.g., "A", "B") used as-is. In YAML, | ||
| unquoted letters are parsed as strings (``plate_row: A`` works). | ||
| plate_col : int | str | None | ||
| Column for well plate positions. Can be a 0-based index (e.g., 0 → "1", | ||
| 1 → "2") or a string name (e.g., "1", "2") used as-is. In YAML, | ||
| unquoted numbers are parsed as int, so use quotes for string columns | ||
| (``plate_col: "1"`` for column name "1", vs ``plate_col: 1`` for | ||
| 0-based index 1 → column name "2"). | ||
| grid_row : int | None | ||
| Optional row index, when used in a grid. | ||
| grid_col : int | None | ||
|
|
@@ -51,8 +70,8 @@ class PositionBase(MutableModel): | |
| name: str | None = None | ||
| sequence: Optional["MDASequence"] = None | ||
| properties: list[PropertyTuple] | None = None | ||
| plate_row: int | None = None | ||
| plate_col: int | None = None | ||
| plate_row: int | str | None = None | ||
| plate_col: int | str | None = None | ||
| grid_row: int | None = None | ||
| grid_col: int | None = None | ||
|
|
||
|
|
@@ -108,6 +127,34 @@ def __round__(self, ndigits: "SupportsIndex | None" = None) -> "Self": | |
| # not sure why these Self types are not working | ||
| return type(self).model_construct(**kwargs) # type: ignore [return-value] | ||
|
|
||
| @model_validator(mode="after") | ||
| def _name_from_plate(self) -> "Self": | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this all feels like application-level logic to me. I don't think we should auto-calculate names from other fields. Just store the data, and leave this sort of concern (autogeneration of naming) to whoever is using the data, like a writer, or a GUI. |
||
| """Set or validate name from plate_row/plate_col. | ||
|
|
||
| When both plate_row and plate_col are int (standard well-plate indices), | ||
| the name is derived as e.g. "A1" and an explicit mismatch raises | ||
| ValueError. When either is a str (custom naming), the name is only | ||
| auto-generated if not provided; explicit names are accepted as-is. | ||
| """ | ||
| if self.plate_row is not None and self.plate_col is not None: | ||
| if isinstance(self.plate_row, int) and isinstance(self.plate_col, int): | ||
| well_name = f"{_index_to_row_name(self.plate_row)}{self.plate_col + 1}" | ||
| if self.name is not None and self.name != well_name: | ||
| raise ValueError( | ||
| f"Position name {self.name!r} does not match " | ||
| f"plate_row={self.plate_row}, plate_col=" | ||
| f"{self.plate_col} (expected {well_name!r}). " | ||
| f"Remove the name to auto-generate it from " | ||
| f"plate coordinates." | ||
| ) | ||
| object.__setattr__(self, "name", well_name) | ||
| elif self.name is None: | ||
| # String plate coords: auto-generate only if no name provided | ||
| row_str = str(self.plate_row) | ||
| col_str = str(self.plate_col) | ||
| object.__setattr__(self, "name", f"{row_str}{col_str}") | ||
| return self | ||
|
|
||
| @model_validator(mode="before") | ||
| @classmethod | ||
| def _cast(cls, value: Any) -> Any: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit uncertain about
int | strhere. I see in your tests that you've used this for things likePosition(plate_row="fish0", plate_col="neuromast0")... but that feels like it's overloading one concept (which row/col) to do double duty to store metadata that should arguably go elsewhere (either in the position name itself (which is already a totally open string) or in the sequence metadata somewhere (where you could map your row/col indices to treatments/metadata). Also, thinking ahead to how this information would be used to write an OME-Zarr, we have to know the row/col index (https://imaging-formats.github.io/yaozarrs/API_Reference/yaozarrs.v05/?h=row#yaozarrs.v05._plate.PlateWell) ... so allowing the user to provide a string here means we would have difficulty guaranteeing the ability to write ome-zarr.I'm inclined to reduce this back to just
intfor now.