Skip to content

Commit 867e9a1

Browse files
authored
feat: add properties field to PositionDict and update _iter_sequence for position properties (#266)
1 parent c330ba1 commit 867e9a1

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/useq/_iter_sequence.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from useq._channel import Channel # noqa: TC001 # noqa: TCH001
1010
from useq._enums import AXES, Axis
1111
from useq._mda_event import Channel as EventChannel
12-
from useq._mda_event import MDAEvent, ReadOnlyDict
12+
from useq._mda_event import MDAEvent, PropertyTuple, ReadOnlyDict
1313
from useq._utils import _has_axes
1414
from useq._z import AnyZPlan # noqa: TC001 # noqa: TCH001
1515

@@ -30,7 +30,7 @@ class MDAEventDict(TypedDict, total=False):
3030
y_pos: float | None
3131
z_pos: float | None
3232
sequence: MDASequence | None
33-
# properties: list[tuple] | None
33+
properties: list[PropertyTuple] | None
3434
metadata: dict
3535
reset_event_timer: bool
3636

@@ -113,6 +113,7 @@ def _iter_sequence(
113113
event_kwarg_overrides: MDAEventDict | None = None,
114114
position_offsets: PositionDict | None = None,
115115
_last_t_idx: int = -1,
116+
_last_p_idx: int = -1,
116117
) -> Iterator[MDAEvent]:
117118
"""Helper function for `iter_sequence`.
118119
@@ -174,6 +175,11 @@ def _iter_sequence(
174175
event_kwargs.update(_xyzpos(position, channel, sequence.z_plan, grid, z_pos))
175176
if position and position.name:
176177
event_kwargs["pos_name"] = position.name
178+
# include position properties only when p-index changes
179+
p_idx = index.get(Axis.POSITION, -1)
180+
if position and position.properties and p_idx != _last_p_idx:
181+
event_kwargs["properties"] = list(position.properties)
182+
_last_p_idx = p_idx
177183
if channel:
178184
event_kwargs["channel"] = EventChannel.model_construct(
179185
config=channel.config, group=channel.group
@@ -223,6 +229,7 @@ def _iter_sequence(
223229
event_kwarg_overrides=pos_overrides,
224230
position_offsets=_offsets,
225231
_last_t_idx=_last_t_idx,
232+
_last_p_idx=_last_p_idx,
226233
)
227234
continue
228235
# note that position.sequence may be Falsey even if not None, for example

src/useq/_position.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pydantic import Field, model_validator
77

88
from useq._base_model import FrozenModel, MutableModel
9+
from useq._mda_event import PropertyTuple
910

1011
if TYPE_CHECKING:
1112
from matplotlib.axes import Axes
@@ -44,6 +45,7 @@ class PositionBase(MutableModel):
4445
z: float | None = None
4546
name: str | None = None
4647
sequence: Optional["MDASequence"] = None
48+
properties: list[PropertyTuple] | None = None
4749

4850
# excluded from serialization
4951
row: int | None = Field(default=None, exclude=True)

tests/fixtures/mda.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@
6262
"stage_positions": [
6363
{
6464
"name": null,
65+
"properties": null,
6566
"sequence": null,
6667
"x": 10.0,
6768
"y": 20.0,
6869
"z": null
6970
},
7071
{
7172
"name": "test_name",
73+
"properties": null,
7274
"sequence": {
7375
"autofocus_plan": null,
7476
"axis_order": [
@@ -103,6 +105,7 @@
103105
"step": 1.0
104106
}
105107
},
108+
"properties": null,
106109
"x": 10.0,
107110
"y": 20.0,
108111
"z": 50.0

0 commit comments

Comments
 (0)