Skip to content

Commit 31fcde7

Browse files
authored
set length & index in seqprop __init__
1 parent 768e5c4 commit 31fcde7

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

deeptrack/properties.py

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@
6868
6969
>>> seq_prop = dt.SequentialProperty()
7070
>>> seq_prop.sequence_length.store(5)
71-
>>> seq_prop.current = lambda _ID=(): seq_prop.sequence_step() + 1
71+
>>> seq_prop.current = lambda _ID=(): seq_prop.sequence_index() + 1
7272
>>> for step in range(seq_prop.sequence_length()):
73-
... seq_prop.sequence_step.store(step)
73+
... seq_prop.sequence_index.store(step)
7474
... seq_prop.store(seq_prop.current())
7575
... print(seq_prop.data[()].current_value())
7676
@@ -561,8 +561,8 @@ def __getitem__(
561561

562562
class SequentialProperty(Property):
563563
"""Property that yields different values for sequential steps.
564-
SequantialProperty lets the user encapsulate feature sampling rules and
565-
iterator logic in a single object to evaluate these sequentially.
564+
SequentialProperty lets the user encapsulate feature sampling rules and
565+
iterator logic in a single object to evaluate them sequentially.
566566
567567
The `SequentialProperty` class extends the standard `Property` to handle
568568
scenarios where the property’s value evolves over discrete steps, such as
@@ -592,7 +592,7 @@ class SequentialProperty(Property):
592592
sequence_length: Property
593593
A `Property` holding the total number of steps in the sequence.
594594
Initialized to 0 by default.
595-
sequence_step: Property
595+
sequence_index: Property
596596
A `Property` holding the index of the current step (starting at 0).
597597
previous_values: Property
598598
A `Property` returning all previously stored values up to, but not
@@ -608,13 +608,13 @@ class SequentialProperty(Property):
608608
`None`.
609609
action: Callable[..., Any]
610610
Overrides the default `Property.action` to select between
611-
`initialization` (if `sequence_step` is 0) or `current` (otherwise).
611+
`initialization` (if `sequence_index` is 0) or `current` (otherwise).
612612
613613
Methods
614614
-------
615615
_action_override(_ID: tuple[int, ...]) -> Any
616616
Internal logic to pick which function (`initialization` or `current`)
617-
to call based on the `sequence_step`.
617+
to call based on the `sequence_index`.
618618
store(value: Any, _ID: tuple[int, ...] = ()) -> None
619619
Store a newly computed `value` in the property’s internal list of
620620
previously generated values.
@@ -639,9 +639,9 @@ class SequentialProperty(Property):
639639
640640
>>> seq_prop = dt.SequentialProperty()
641641
>>> seq_prop.sequence_length.store(5)
642-
>>> seq_prop.current = lambda _ID=(): seq_prop.sequence_step() + 1
642+
>>> seq_prop.current = lambda _ID=(): seq_prop.sequence_index() + 1
643643
>>> for step in range(seq_prop.sequence_length()):
644-
... seq_prop.sequence_step.store(step)
644+
... seq_prop.sequence_index.store(step)
645645
... current_value = seq_prop.current()
646646
... seq_prop.store(current_value)
647647
... print(seq_prop.data[()].current_value())
@@ -656,7 +656,7 @@ class SequentialProperty(Property):
656656
initialization: Optional[Callable[..., Any]]
657657
current: Optional[Callable[..., Any]]
658658
sequence_length: Property
659-
sequence_step: Property
659+
sequence_index: Property
660660
previous_values: Property
661661
previous_value: Property
662662
action: Callable[..., Any]
@@ -665,6 +665,8 @@ def __init__(
665665
self: SequentialProperty,
666666
initialization: Optional[Any] = None,
667667
current_value: Optional[Any] = None,
668+
sequence_length: Optional[int] = None,
669+
sequence_index: Optional[int] = None,
668670
**kwargs: Dict[str, Property],
669671
):
670672
"""Create a SequentialProperty with optional initialization.
@@ -676,6 +678,10 @@ def __init__(
676678
current_value: Any, optional
677679
The sampling rule (value or callable) for the current step.
678680
Defaults to None.
681+
sequence_length: int, optional
682+
The length of the sequence.
683+
sequence_index: int, optional
684+
The current index of the sequence.
679685
**kwargs: dict[str, Property]
680686
Additional named dependencies for `initialization` and `current`.
681687
@@ -685,39 +691,46 @@ def __init__(
685691
# It overrides action below with _action_override().
686692
super().__init__(sampling_rule=None)
687693

688-
# 1) Initialize sequence length to 0.
689-
self.sequence_length = Property(0)
694+
695+
# 1) Initialize sequence length.
696+
if isinstance(sequence_length, int):
697+
self.sequence_length = Property(sequence_length)
698+
else:
699+
self.sequence_length = Property(0)
690700
self.sequence_length.add_child(self)
691-
# self.add_dependency(self.sequence_length) # Done by add_child
701+
# self.add_dependency(self.sequence_length) # Done by add_child.
692702

693-
# 2) Initialize current sequence step to 0.
694-
self.sequence_step = Property(0)
695-
self.sequence_step.add_child(self)
696-
# self.add_dependency(self.sequence_step) # Done by add_child
703+
# 2) Initialize sequence index.
704+
if isinstance(sequence_index, int):
705+
self.sequence_index = Property(sequence_index)
706+
else:
707+
self.sequence_index = Property(0)
708+
self.sequence_index.add_child(self)
709+
# self.add_dependency(self.sequence_index) # Done by add_child.
697710

698711
# 3) Store all previous values if sequence step > 0.
699712
self.previous_values = Property(
700-
lambda _ID=(): self.previous(_ID=_ID)[: self.sequence_step() - 1]
701-
if self.sequence_step(_ID=_ID)
713+
lambda _ID=(): self.previous(_ID=_ID)[: self.sequence_index() - 1]
714+
if self.sequence_index(_ID=_ID)
702715
else []
703716
)
704717
self.previous_values.add_child(self)
705718

706719
# self.add_dependency(self.previous_values) # Done by add_child
707-
self.sequence_step.add_child(self.previous_values)
720+
self.sequence_index.add_child(self.previous_values)
708721

709-
# self.previous_values.add_dependency(self.sequence_step) # Done
722+
# self.previous_values.add_dependency(self.sequence_index) # Done
710723

711724
# 4) Store the previous value.
712725
self.previous_value = Property(
713-
lambda _ID=(): self.previous(_ID=_ID)[self.sequence_step() - 1]
726+
lambda _ID=(): self.previous(_ID=_ID)[self.sequence_index() - 1]
714727
if self.previous(_ID=_ID)
715728
else None
716729
)
717730
self.previous_value.add_child(self)
718731
# self.add_dependency(self.previous_value) # Done by add_child
719-
self.sequence_step.add_child(self.previous_value)
720-
# self.previous_value.add_dependency(self.sequence_step) # Done
732+
self.sequence_index.add_child(self.previous_value)
733+
# self.previous_value.add_dependency(self.sequence_index) # Done
721734

722735
# 5) Create an action for initializing the sequence.
723736
if initialization is not None:
@@ -755,7 +768,7 @@ def _action_override(
755768
756769
"""
757770

758-
if self.sequence_step(_ID=_ID) == 0:
771+
if self.sequence_index(_ID=_ID) == 0:
759772
if self.initialization:
760773
return self.initialization(_ID=_ID)
761774
return None
@@ -811,7 +824,7 @@ def current_value(
811824
Returns
812825
-------
813826
Any
814-
The value stored at the index = `self.sequence_step(_ID=_ID)`.
827+
The value stored at the index = `self.sequence_index(_ID=_ID)`.
815828
816829
Raises
817830
------
@@ -820,7 +833,7 @@ def current_value(
820833
821834
"""
822835

823-
return super().current_value(_ID=_ID)[self.sequence_step(_ID=_ID)]
836+
return super().current_value(_ID=_ID)[self.sequence_index(_ID=_ID)]
824837

825838
def __call__(
826839
self: SequentialProperty,
@@ -875,22 +888,22 @@ def set_current_step(
875888
value: Any,
876889
_ID: tuple[int, ...] = (),
877890
) -> None:
878-
"""Sets the `sequence_step` attribute of a sequence to be resolved.
891+
"""Sets the `sequence_index` attribute of a sequence to be resolved.
879892
880893
It supports dependencies if `value` is a `Property`.
881894
882895
Parameters
883896
----------
884897
value: Any
885-
The value to store in `sequence_step`.
898+
The value to store in `sequence_index`.
886899
_ID: tuple[int, ...], optional
887900
A unique identifier that allows the property to keep separate
888901
histories for different parallel evaluations.
889902
890903
"""
891904

892905
if isinstance(value, Property): # For dependencies
893-
self.sequence_step = Property(lambda _ID: value(_ID))
894-
self.sequence_step.add_dependency(value)
906+
self.sequence_index = Property(lambda _ID: value(_ID))
907+
self.sequence_index.add_dependency(value)
895908
else:
896-
self.sequence_step = Property(value, _ID=_ID)
909+
self.sequence_index = Property(value, _ID=_ID)

0 commit comments

Comments
 (0)