Skip to content

Commit fa5fe51

Browse files
JChonpcagiovannivolpemirjagranforsJiacheng HuangPwhsky
authored
Features: TakeProperties (#400)
* Update features.py * Update features.py * Update features.py * Update features.py * Update test_features.py * Update features.py * Value Update test_features.py Update features.py Update test_features.py Update features.py * Update test_features.py * remove __gpu_compatible__ * Update features.py * Update test_features.py * Update features.py * Update test_features.py * Update aberrations.py * Update optics.py * Update image.py * Update holography.py * Update features.py * Update features.py * Update test_features.py * Update features.py * Update test_features.py * Update features.py * Update test_features.py * Update features.py * Update test_features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update test_features.py * Update features.py * Update features.py * Update test_features.py * Update test_features.py * Update features.py * Update features.py * Update test_features.py * Update features.py * Update test_features.py * Update test_features.py * Update features.py * Update test_features.py * Update features.py * Update test_features.py * Update features.py * Update features.py * Update test_features.py * Update features.py * Update features.py * Update test_features.py * Update features.py * Update test_features.py * Update features.py * Update features.py * Update test_features.py * Update features.py * Update features.py * Update test_features.py * Update features.py * Update features.py * Update features.py * Update test_features.py * Update features.py * Update test_features.py * Update test_features.py * Update features.py * Update test_features.py * Update features.py * Update test_features.py * Update features.py * Update test_features.py * Update test_features.py * Update features.py * Update test_features.py * Update features.py * Update test_features.py * Update features.py * Update test_features.py * Update features.py * Update test_features.py * Update features.py * Update test_features.py * Update test_features.py * Update features.py * Update features.py * Update test_features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update _config.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update _config.py * Update features.py * Update features.py * Update features.py * Updated the documentation of features/__sub__ (#356) * updated documentation of features/__sub__ * minor changes * remove blank * Update documentation of features/__pow__ (#359) * Update documentation of features/__pow__ * Update * u --------- Co-authored-by: Jiacheng Huang <[email protected]> * Update features.py * Update features.py * Update features.py * Update features.py * added get_backend() as a feature method + unittesting to switch and get backend (#360) * Update features.py * Update test_features.py * Sequences restructuring (#345) * features: added to_sequential() * refactoring: SequentialProperty * typo in type hint * added dependency support for the set methods. * docs for to_sequential() * Update features.py * Update features.py * Add files via upload * edits Update sequences.py Update properties.py Update sequences.py Update sequences.py Update DTAT341_sequences.ipynb * Update test_properties.py * Update properties.py * u * Update properties.py * Update test_properties.py * Update properties.py * Update __init__.py * Update properties.py * Update properties.py * Update features.py * Update features.py * Update sequences.py * documentation and type hints * import Optional, Dict * missing comma... * current attributes * fix: typo in docstring. * documentation for sequences * typo * Update sequences.py * : * future import * documentation * DTAT306: Added SequentialProperty Example. (#350) * Added SequentialProperty example * typo * set length & index in seqprop __init__ * variable rename: sequence_step <- sequence_index * variable rename: sequence_step <- sequence_index * Update test_properties.py with new variable name * update documentation of SequentialProperty * variable name change: current and initialization * Update test_properties.py * Update features.py * Update sequences.py * sampling rule in init. * attributes * Update properties.py * Update features.py * Update properties.py * Update test_sequences.py * Revert "Update test_sequences.py" This reverts commit 503ea57. * Update properties.py * Update test_sequences.py * sequentialproperty docs * markov chain example in properties tutorial * Update seq_prop names * Update features.py * Update properties.py * Update properties.py * Update properties.py * Update properties.py * Update properties.py * Update properties.py * removed duplicate method * added example in docs * Updated tutorial with new names and examples * Update unit test * Update test_properties.py * Variable name changes * Update test_sequences.py * sequentialproperty docs * markov chain example in properties tutorial * new sequentialproperty attribute names * removed duplicate method * added example in docs * Updated tutorial with new names and examples * Update unit test * Update test_properties.py * docs: example of to_sequential() * create_action in __init__ * Added Previous() to SequentialProperty * Removed previous from backend.core * removed upsample from unit tests * Specific imports * u * sequences type hints * Updated SequentialProperty example * comma * Update __init__.py * Update features.py * Update DTAT306_properties.ipynb * Update DTAT306_properties.ipynb * Update DTAT341_sequences.ipynb * Update DTAT306_properties.ipynb * Update properties.py * Update test_properties.py * Update test_properties.py * Update features.py * Update features.py * Update optics.py * Update sequences.py * Update properties.py * Update test_sequences.py * Update features.py --------- Co-authored-by: Giovanni Volpe <[email protected]> Co-authored-by: Giovanni Volpe <[email protected]> * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update features.py * Update augmentations.py * Update types.py * Update utils.py * Update types.py * Update test_features.py * Update test_utils.py * Update DTAT387_types.ipynb * u * Update types.py * Update DTAT301_features.ipynb * Update DTAT387_types.ipynb * Update DTAT306_properties.ipynb * Update DTAT301_features.ipynb * Update DTAT306_properties.ipynb * Update DTAT341_sequences.ipynb * Update features.py * Added more documentation for features/__rsub__ (#363) * Added more documentation for features/__rsub__ * Update features.py --------- Co-authored-by: Giovanni Volpe <[email protected]> * Added more documentation for features/__mul__ (#364) * Added more documentation for features/__mul__ * Update features.py --------- Co-authored-by: Giovanni Volpe <[email protected]> * Added more documentation to features/__rmul__ (#365) * Update features.py * Polish documentationof feature/__pow__ (#366) * Update documentation of features/__pow__ * Update * u * Polish 1. remove >>> 2. remove blank * u * u * Update features.py --------- Co-authored-by: Jiacheng Huang <[email protected]> Co-authored-by: Giovanni Volpe <[email protected]> * Add documentation of features/__rpow__ (#367) * Add documentation of features/__rpow__ * u * u * u * u * Update features.py --------- Co-authored-by: Jiacheng Huang <[email protected]> Co-authored-by: Giovanni Volpe <[email protected]> * polishing of sources initial polishing of sources * Update base.py * Update base.py * Update base.py * Update base.py * Update base.py * Update base.py * Update base.py * Update base.py * features: __truediv__ (#362) * implemented truediv and rtruediv * docstring clarification * removed white spaces and updated example * example update * Implemented feedback * Update features.py --------- Co-authored-by: Giovanni Volpe <[email protected]> * Update base.py * al/features/__rtruediv__ polishing (#369) * removed update(), added dynamic example for rtruediv * Update features.py * Update features.py --------- Co-authored-by: Giovanni Volpe <[email protected]> * Update features.py * features: __floordiv__ (#368) * added __floordiv__ * docs * Implemented feedback from Mirja * minor adjustments * updated example * Update features.py --------- Co-authored-by: mirjagranfors <[email protected]> Co-authored-by: Giovanni Volpe <[email protected]> * Update features.py * Update base.py * Update base.py * Update base.py * Update base.py * Update base.py * Update base.py * Update base.py * Update base.py * Update base.py * Update base.py * Features: ChannelFirst2d (#361) * documented channel2d func * unit tests: channelfirst2d * type hints * Update features.py * NDArray type hint * included Image type hint and xp usage * unit test checks for torch_available * Update features.py * u * docs * linebreak * docs * Update test_features.py * Update test_features.py * deprecation warning & tests * Added support for Image objects * Tests for image objects * docs * tests and axis compatibility * fix for repeated calls * revert changes to upscale * Implemented feedback --------- Co-authored-by: Giovanni Volpe <[email protected]> * Update features.py * Create __init__.py * Create test_base.py * Update test_base.py * Update DTAT391A_sources.base.ipynb * Delete interface_v2.mp4 * Update DTAT391B_sources.folder.ipynb * Update folder.py * Update test_features.py * Update folder.py * Update folder.py * Update folder.py * Update folder.py * Create test_folder.py * Update test_folder.py * Update folder.py * Update folder.py * Update test_folder.py * Add docs of /features/__gt__ (#372) * add docs of feature/__gt__ * u * u code example * polish --------- Co-authored-by: Jiacheng Huang <[email protected]> * Add docs of /features/__rgt__ (#373) * Add docs of features/__rgt__ * fix typo * polish * fix error * u * Update features.py --------- Co-authored-by: Jiacheng Huang <[email protected]> Co-authored-by: Giovanni Volpe <[email protected]> * Add docs of /features/__lt__ (#374) * add docs of /features/__lt__ * u --------- Co-authored-by: Jiacheng Huang <[email protected]> * Add docs of /features/__rlt__ (#375) * add docs of /features/__rlt__ * u * u * Update features.py --------- Co-authored-by: Jiacheng Huang <[email protected]> Co-authored-by: Giovanni Volpe <[email protected]> * Update features.py * Add docs of /features/__le__ (#376) * add docs 0f /featuers/__le__ * u --------- Co-authored-by: Jiacheng Huang <[email protected]> * Update features.py * Add docs of /features/__rle__ (#377) * add docs of /features/__rle__ * u * u * Update features.py --------- Co-authored-by: Jiacheng Huang <[email protected]> Co-authored-by: Giovanni Volpe <[email protected]> * Update features.py * Update features.py * add docs of /features/__ge__ (#378) Co-authored-by: Jiacheng Huang <[email protected]> * Update features.py * Add docs of /features/__rge__ (#379) * add docs 0f /features/__rge__ * u * u * right , * Update features.py --------- Co-authored-by: Jiacheng Huang <[email protected]> Co-authored-by: Giovanni Volpe <[email protected]> * Add docs of /features/__xor__ (#382) * add docs of /features/__xor__ * remove blank spaces --------- Co-authored-by: Jiacheng Huang <[email protected]> * Add docs of /features/__rand__ (#381) * add docs of /features/__rand__ * Update features.py --------- Co-authored-by: Jiacheng Huang <[email protected]> Co-authored-by: Giovanni Volpe <[email protected]> * add docs of /features/__and__ (#380) Co-authored-by: Jiacheng Huang <[email protected]> * Update features.py * Update /features/TakeProperties * U * Revert "U" This reverts commit b137273. * UU * uuu * uu * small changes * Update feedback from Mirja * update from Alex * Update features.py --------- Co-authored-by: Giovanni Volpe <[email protected]> Co-authored-by: Mirja Granfors <[email protected]> Co-authored-by: Jiacheng Huang <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: Giovanni Volpe <[email protected]> Co-authored-by: mirjagranfors <[email protected]>
1 parent 4a79b7a commit fa5fe51

File tree

3 files changed

+81
-39
lines changed

3 files changed

+81
-39
lines changed

deeptrack/features.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def propagate_data_to_dependencies(
230230
"Transpose",
231231
"Permute",
232232
"OneHot",
233-
"TakeProperties", #TODO ***JH***
233+
"TakeProperties",
234234
]
235235

236236

@@ -9440,11 +9440,11 @@ def get(
94409440
class TakeProperties(Feature):
94419441
"""Extract all instances of a set of properties from a pipeline.
94429442
9443-
Only extracts the properties if the feature contains all given
9444-
property-names. The order of the properties is not guaranteed to be the
9443+
Only extracts the properties if the feature contains all given
9444+
property-names. The order of the properties is not guaranteed to be the
94459445
same as the evaluation order.
94469446
9447-
If there is only a single property name, this will return a list of the
9447+
If there is only a single property name, this will return a list of the
94489448
property values.
94499449
94509450
Parameters
@@ -9453,7 +9453,7 @@ class TakeProperties(Feature):
94539453
The feature from which to extract properties.
94549454
names: list[str]
94559455
The names of the properties to extract
9456-
**kwargs:: dict of str to Any
9456+
**kwargs: dict of str to Any
94579457
Additional keyword arguments passed to the parent `Feature` class.
94589458
94599459
Attributes
@@ -9462,18 +9462,19 @@ class TakeProperties(Feature):
94629462
Indicates whether this feature distributes computation across inputs.
94639463
Always `False` for `TakeProperties`, as it processes sequentially.
94649464
__list_merge_strategy__: int
9465-
Specifies how lists of properties are merged. Set to
9465+
Specifies how lists of properties are merged. Set to
94669466
`MERGE_STRATEGY_APPEND` to append values to the result list.
94679467
94689468
Methods
94699469
-------
9470-
`get(image: Any, names: tuple[str, ...], **kwargs: dict[str, Any]) -> np.ndarray | tuple[np.ndarray, ...]`
9470+
`get(image: Any, names: tuple[str, ...], **kwargs: dict[str, Any])
9471+
-> np.ndarray | tuple[np.ndarray, torch.Tensor, ...]`
94719472
Extract the specified properties from the feature pipeline.
9472-
9473+
94739474
Examples
94749475
--------
94759476
>>> import deeptrack as dt
9476-
9477+
94779478
>>> class ExampleFeature(Feature):
94789479
... def __init__(self, my_property, **kwargs):
94799480
... super().__init__(my_property=my_property, **kwargs)
@@ -9489,7 +9490,7 @@ class TakeProperties(Feature):
94899490
94909491
Create a `Gaussian` feature:
94919492
>>> noise_feature = dt.Gaussian(mu=7, sigma=12)
9492-
9493+
94939494
Use `TakeProperties` to extract the property:
94949495
>>> take_properties = dt.TakeProperties(noise_feature)
94959496
>>> output = take_properties.get(image=None, names=["mu"])
@@ -9504,7 +9505,7 @@ class TakeProperties(Feature):
95049505
def __init__(
95059506
self: TakeProperties,
95069507
feature: Feature,
9507-
*names: str,
9508+
*names: PropertyLike[str],
95089509
**kwargs: Any,
95099510
):
95109511
"""Initialize the TakeProperties feature.
@@ -9513,46 +9514,46 @@ def __init__(
95139514
----------
95149515
feature: Feature
95159516
The feature from which to extract properties.
9516-
*names: str
9517+
*names: PropertyLike[str]
95179518
One or more names of the properties to extract.
9518-
= **kwargs: Any, optional
9519+
**kwargs: Any, optional
95199520
Additional keyword arguments passed to the parent `Feature` class.
9520-
9521+
95219522
"""
95229523

95239524
super().__init__(names=names, **kwargs)
95249525
self.feature = self.add_feature(feature)
95259526

95269527
def get(
9527-
self: TakeProperties,
9528-
image: Any,
9528+
self: Feature,
9529+
image: NDArray[Any] | torch.Tensor,
95299530
names: tuple[str, ...],
95309531
_ID: tuple[int, ...] = (),
95319532
**kwargs: Any,
9532-
) -> np.ndarray | tuple[np.ndarray, ...]:
9533+
) -> NDArray[Any] | tuple[NDArray[Any], torch.Tensor, ...]:
95339534
"""Extract the specified properties from the feature pipeline.
95349535
9535-
This method retrieves the values of the specified properties from the
9536+
This method retrieves the values of the specified properties from the
95369537
feature's dependency graph and returns them as NumPy arrays.
95379538
95389539
Parameters
95399540
----------
9540-
image: Any
9541+
image: NDArray[Any] | torch.Tensor
95419542
The input image (unused in this method).
95429543
names: tuple[str, ...]
95439544
The names of the properties to extract.
95449545
_ID: tuple[int, ...], optional
9545-
A unique identifier for the current computation, ensuring that
9546+
A unique identifier for the current computation, ensuring that
95469547
dependencies are correctly matched. It defaults to an empty tuple.
95479548
**kwargs: Any, optional
95489549
Additional keyword arguments (unused in this method).
95499550
95509551
Returns
95519552
-------
9552-
np.ndarray or tuple[np.ndarray, ...]
9553-
If a single property name is provided, a NumPy array containing the
9554-
property values is returned. If multiple property names are
9555-
provided, a tuple of NumPy arrays is returned, where each array
9553+
NDArray[Any] or tuple[NDArray[Any], torch.Tensor, ...]
9554+
If a single property name is provided, a NumPy array containing the
9555+
property values is returned. If multiple property names are
9556+
provided, a tuple of NumPy arrays is returned, where each array
95569557
corresponds to a property.
95579558
95589559
"""
@@ -9578,8 +9579,8 @@ def get(
95789579
if key[:len(_ID)] == _ID:
95799580
res[name].append(value.current_value())
95809581

9581-
# Convert the results to NumPy arrays.
9582-
res = tuple([np.array(res[name]) for name in names])
9582+
# Convert the results to tuple.
9583+
res = tuple([res[name] for name in names])
95839584

95849585
# Return a single array if only one property name is specified.
95859586
if len(res) == 1:

deeptrack/tests/test_features.py

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,28 +2673,62 @@ def __init__(self, my_property, **kwargs):
26732673

26742674
feature = ExampleFeature(my_property=properties.Property(42))
26752675

2676-
take_properties = features.TakeProperties(feature)
26772676
take_properties = features.TakeProperties(feature)
26782677
output = take_properties.get(image=None, names=["my_property"])
26792678
self.assertEqual(output, [42])
26802679

2681-
# with `Gaussian` feature
2680+
# with `Gaussian` feature
26822681
noise_feature = Gaussian(mu=7, sigma=12)
2683-
2682+
26842683
take_properties = features.TakeProperties(noise_feature)
26852684
output = take_properties.get(image=None, names=["mu"])
26862685
self.assertEqual(output, [7])
26872686
output = take_properties.get(image=None, names=["sigma"])
26882687
self.assertEqual(output, [12])
26892688

2690-
# with `Gaussian` feature
2691-
noise_feature = Gaussian(mu=7, sigma=12)
2692-
2689+
# with `Gaussian` feature with float properties
2690+
noise_feature = Gaussian(mu=7.123, sigma=12.123)
2691+
26932692
take_properties = features.TakeProperties(noise_feature)
2694-
output = take_properties.get(image=None, names=["mu"])
2695-
self.assertEqual(output, [7])
2696-
output = take_properties.get(image=None, names=["sigma"])
2697-
self.assertEqual(output, [12])
2693+
output = take_properties.get(image=None, names=["mu", "sigma"])
2694+
self.assertEqual(output, ([7.123], [12.123]))
2695+
self.assertEqual(output[0][0], 7.123)
2696+
self.assertEqual(output[1][0], 12.123)
2697+
2698+
### Test with PyTorch tensor (if available)
2699+
if TORCH_AVAILABLE:
2700+
class ExampleFeature(features.Feature):
2701+
def __init__(self, my_property, **kwargs):
2702+
super().__init__(my_property=my_property, **kwargs)
2703+
2704+
feature = ExampleFeature(my_property=
2705+
properties.Property(torch.tensor(42.123)))
2706+
2707+
take_properties = features.TakeProperties(feature)
2708+
output = take_properties.get(image=None, names=["my_property"])
2709+
torch.testing.assert_close(output[0], torch.tensor(42.123))
2710+
2711+
# with `Gaussian` feature
2712+
noise_feature = Gaussian(
2713+
mu=torch.tensor(7), sigma=torch.tensor(12)
2714+
)
2715+
2716+
take_properties = features.TakeProperties(noise_feature)
2717+
output = take_properties.get(image=None, names=["mu"])
2718+
torch.testing.assert_close(output[0], torch.tensor(7))
2719+
output = take_properties.get(image=None, names=["sigma"])
2720+
torch.testing.assert_close(output[0], torch.tensor(12))
2721+
2722+
# with `Gaussian` feature with float properties
2723+
random_mu = torch.rand(1)
2724+
random_sigma = torch.rand(1)
2725+
noise_feature = Gaussian(mu=random_mu, sigma=random_sigma)
2726+
2727+
take_properties = features.TakeProperties(noise_feature)
2728+
output = take_properties.get(image=None, names=["mu", "sigma"])
2729+
torch.testing.assert_close(output, ([random_mu], [random_sigma]))
2730+
torch.testing.assert_close(output[0][0], random_mu)
2731+
torch.testing.assert_close(output[1][0], random_sigma)
26982732

26992733

27002734
if __name__ == "__main__":

tutorials/3-advanced-topics/DTAT391B_sources.folder.ipynb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
},
7373
{
7474
"cell_type": "code",
75-
"execution_count": 2,
75+
"execution_count": null,
7676
"metadata": {},
7777
"outputs": [],
7878
"source": [
@@ -111,7 +111,7 @@
111111
},
112112
{
113113
"cell_type": "code",
114-
"execution_count": 3,
114+
"execution_count": 2,
115115
"metadata": {},
116116
"outputs": [
117117
{
@@ -174,7 +174,7 @@
174174
},
175175
{
176176
"cell_type": "code",
177-
"execution_count": 4,
177+
"execution_count": 32,
178178
"metadata": {},
179179
"outputs": [],
180180
"source": [
@@ -430,6 +430,13 @@
430430
"List all image paths in train:"
431431
]
432432
},
433+
{
434+
"cell_type": "markdown",
435+
"metadata": {},
436+
"source": [
437+
"Prepare an example path to one of the files:"
438+
]
439+
},
433440
{
434441
"cell_type": "code",
435442
"execution_count": 15,

0 commit comments

Comments
 (0)