-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathsequences.py
More file actions
375 lines (284 loc) · 11.3 KB
/
sequences.py
File metadata and controls
375 lines (284 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""Tools for evaluating and propagating sequences of features.
This module enables sequential evaluation of DeepTrack2 features by
resolving them over multiple time steps. It provides tools for propagating
values like `sequence_index` and `sequence_length` to all dependent
`SequentialProperty` attributes, allowing simulation of dynamic behaviors
(e.g., microsocpy videos).
Key Features
------------
- **Temporal Simulation via SequentialProperty**
Features can be annotated with sampling rules that evolve over a sequence
of time steps, enabling animations and simulations of time-dependent
systems.
- **Graph-wide Sequential Data Propagation**
Sequential information is passed to all relevant nodes in the feature
graph.
Module Structure
----------------
Classes:
- `Sequence`
It resolves a feature over multiple time steps, using a defined
`sequence_length`. Injects sequential arguments into all dependent
`SequentialProperty` attributes before each evaluation.
Functions:
- `Sequential(feature, **kwargs)`
.. deprecated:: 2.0
def Sequential(
feature: Feature,
**kwargs: Any,
) -> Feature
Converts a feature to be resolved as a sequence. Replaced by
`Feature.to_sequence()` and will be removed in a future release.
- `_propagate_sequential_data(feature, **kwargs)`
def _propagate_sequential_data(
feature: Feature,
**kwargs: Any,
) -> None
Recursively propagates keyword arguments like `sequence_index` and
`sequence_length` to all `SequentialProperty` nodes in a feature graph.
Examples
--------
>>> import deeptrack as dt
Simulating a spinning ellipsoid.
Define imaging system:
>>> optics = dt.optics.Fluorescence(output_region=(0, 0, 32, 32))
Define a static ellipse:
>>> ellipse = dt.scatterers.Ellipse(
... radius=(1e-6,0.5e-6),
... position=(16, 16),
... rotation=0.78, # Initial rotation
... )
Define a rotation function that increments the previous angle:
>>> def rotate(sequence_length, previous_value):
... return previous_value + 6.28 / sequence_length
Convert the ellipse to a sequential feature:
>>> rotating_ellipse = ellipse.to_sequential(rotation=rotate)
Compose with the optics:
>>> imaged_rotating_ellipse = optics(rotating_ellipse)
Wrap the full feature in a Sequence:
>>> imaged_rotating_ellipse_sequence = dt.Sequence(
... imaged_rotating_ellipse,
... sequence_length=50,
... )
Generate and display the result
>>> imaged_rotating_ellipse_sequence.update().plot();
"""
from __future__ import annotations
from typing import Any
from deeptrack.features import Feature
from deeptrack.properties import SequentialProperty
from deeptrack.types import PropertyLike
__all__ = ["Sequence"]
class Sequence(Feature):
"""Resolves a feature as a sequence.
The `Sequence` class repeatedly evaluates a given feature
`sequence_length` times. During each evaluation, the keyword arguments
`sequence_length` and `sequence_index` are propagated to all
`SequentialProperty` attributes of the feature, enabling dynamic updates at
each timestep.
This allows for temporal simulations or animations, where the same feature
(e.g., a rotating particle or moving object) evolves over time with
properties defined as sequential functions.
Parameters
----------
feature: Feature
The feature to resolve as a sequence.
sequence_length: int
The number of times to evaluate the feature. It defaults to 1.
kwargs: Any
Additional keyword arguments to be passed to the base `Feature`.
Attributes
----------
feature: Feature
The feature that is resolved multiple times to generate the sequence.
__distributed__: bool
This feature is not distributed across processes or devices.
Always set to False.
Methods
-------
`get(input_list: list[Feature], sequence_length: int, **kwargs: Any) -> list[Any] or tuple[list[Any], ...]`
Resolves the wrapped feature `sequence_length` times. It returns a list
(or tuple of lists) of resolved outputs.
Examples
--------
>>> import deeptrack as dt
Simulating a spinning ellipsoid.
Define imaging system:
>>> optics = dt.Fluorescence(output_region=(0, 0, 32, 32))
Define a static ellipse:
>>> ellipse = dt.Ellipse(
... radius=(1e-6,0.5e-6),
... position=(16, 16),
... rotation=0.78, # Initial rotation
... )
Define a rotation function that increments the previous angle:
>>> def rotate(sequence_length, previous_value):
... return previous_value + 6.28 / sequence_length
Convert the ellipse to a sequential feature:
>>> rotating_ellipse = ellipse.to_sequential(rotation=rotate)
Compose with the optics:
>>> imaged_rotating_ellipse = optics(rotating_ellipse)
Wrap the full feature in a Sequence:
>>> imaged_rotating_ellipse_sequence = dt.Sequence(
... imaged_rotating_ellipse,
... sequence_length=50,
... )
Generate and display the result
>>> imaged_rotating_ellipse_sequence.update().plot();
"""
__distributed__ = False
feature: Feature
def __init__(
self: Sequence,
feature: Feature,
sequence_length: PropertyLike[int] = 1,
**kwargs: Any,
) -> None:
"""Initialize a Sequence object.
This constructor wraps a feature to be resolved multiple times,
propagating sequential information to any `SequentialProperty`
attributes.
Parameters
----------
feature: Feature
The feature to be resolved as a sequence.
sequence_length: PropertyLike[int], optional
Number of steps in the sequence. It defaults to 1.
**kwargs: Any
Additional keyword arguments passed to the base `Feature`.
"""
super().__init__(sequence_length=sequence_length, **kwargs)
self.feature = self.add_feature(feature)
def get(
self: Sequence,
input_list: list[Feature],
sequence_length: int | None = None,
**kwargs: Any,
) -> list[Any] | tuple[list[Any], ...]:
"""Resolve the wrapped feature as a sequence of outputs.
The method evaluates the feature `sequence_length` times, each time
updating the `sequence_index` and propagating it to all dependent
`SequentialProperty` attributes. The results are collected into a list.
Parameters
----------
input_list: list[Feature]
A list of previously resolved outputs to extend. If empty, a new
list is initialized.
sequence_length: int, optional
Number of times to evaluate the feature. If None, it is assumed
to be handled externally or will raise an error.
**kwargs: Any
Unused, included for compatibility.
Returns
-------
list[Any] or tuple[list[Any], ...]
The sequence of resolved feature outputs. If the output of the
feature is a tuple or list, the return is transposed into a tuple
of lists.
"""
outputs = input_list or []
for sequence_index in range(sequence_length):
#TODO ***BM*** ***AL*** Can this be erased?
# np.random.seed(random.randint(0, 1000000))
_propagate_sequential_data(
self.feature,
sequence_index=sequence_index,
sequence_length=sequence_length,
)
out = self.feature()
outputs.append(out)
if isinstance(outputs[0], (tuple, list)):
outputs = tuple(zip(*outputs))
return outputs
def _propagate_sequential_data(
feature: Feature,
**kwargs: Any,
) -> None:
"""Propagate sequential data through the computational graph.
This function updates the attributes of all `SequentialProperty` instances
in the computational graph rooted at the given feature. It works by
recursively traversing the feature's dependencies and setting the values
of matching attributes using the provided keyword arguments.
Parameters
----------
feature: Feature
The root feature whose dependent sequential properties will be updated.
**kwargs: Any
Attribute-value pairs to assign to matching fields in each
`SequentialProperty`.
"""
for dep in feature.recurse_dependencies():
if isinstance(dep, SequentialProperty):
for key, value in kwargs.items():
if hasattr(dep, key):
getattr(dep, key).set_value(value)
def Sequential(
feature: Feature,
**kwargs: Any,
) -> Feature: # DEPRECATED
"""Converts a feature to be resolved as a sequence.
.. deprecated:: 2.0
This function has been substituted by the `Feature.to_sequence()`
method and will be removed in a future release.
Should be called on individual features, not combinations of features. All
keyword arguments will be treated as sequential properties and will be
passed to the parent feature.
If a property from the keyword argument already exists on the feature, the
existing property will be used to initialize the passed property (that is,
it will be used for the first timestep).
Parameters
----------
feature: Feature
Feature to make sequential.
kwargs: Any
Keyword arguments to pass on as sequential properties of `feature`.
Returns
-------
Feature
The modified feature with sequential behavior.
"""
import warnings
warnings.warn(
"The `Sequential()` function is deprecated and will be removed in a "
"future release. Please use `Feature.to_sequence()` instead.",
category=DeprecationWarning,
)
for property_name in kwargs.keys():
if property_name in feature.properties:
# Insert property with initialized value
feature.properties[property_name] = SequentialProperty(
feature.properties[property_name], **feature.properties
)
else:
# insert empty property
feature.properties[property_name] = SequentialProperty()
feature.properties.add_dependency(feature.properties[property_name])
feature.properties[property_name].add_child(feature.properties)
for property_name, sampling_rule in kwargs.items():
prop = feature.properties[property_name]
all_kwargs = dict(
previous_value=prop.previous_value,
previous_values=prop.previous_values,
sequence_length=prop.sequence_length,
sequence_index=prop.sequence_index,
)
for key, val in feature.properties.items():
if key == property_name:
continue
if isinstance(val, SequentialProperty):
all_kwargs[key] = val
all_kwargs["previous_" + key] = val.previous_values
else:
all_kwargs[key] = val
if not prop.initial_sampling_rule:
prop.initial_sampling_rule = prop.create_action(
sampling_rule,
**{
k:all_kwargs[k]
for k
in all_kwargs
if k != "previous_value"
}
)
prop.current = prop.create_action(sampling_rule, **all_kwargs)
return feature