ConditionalSetFeature#
- class deeptrack.features.ConditionalSetFeature(on_false: Feature | None = None, on_true: Feature | None = None, condition: PropertyLike[str | bool] = True, **kwargs: dict[str, Any])#
Bases:
StructuralFeatureConditionally resolves one of two features based on a condition.
This feature allows dynamically selecting and resolving one of two child features depending on whether a specified condition evaluates to True or False.
The condition parameter specifies either: - A boolean value (default is True). - The name of a property to listen to. For example, if condition=”is_label”, the selected feature can be toggled as follows:
>>> feature.resolve(is_label=True) # Resolves `on_true` >>> feature.resolve(is_label=False) # Resolves `on_false` >>> feature.update(is_label=True) # Updates both features
Both on_true and on_false are updated during each call, even if only one is resolved.
Parameters#
- on_false: Feature, optional
The feature to resolve if the condition is False. If not provided, the input image remains unchanged.
- on_true: Feature, optional
The feature to resolve if the condition is True. If not provided, the input image remains unchanged.
- condition: str or bool, optional
The name of the conditional property or a boolean value. If a string is provided, its value is retrieved from kwargs or self.properties. If not found, the default value is True.
- **kwargs: dict of str to Any
Additional keyword arguments passed to the parent StructuralFeature.
Attributes#
- __distributed__: bool
Indicates whether this feature distributes computation across inputs.
Methods#
- get(image: Any, condition: str | bool, **kwargs: dict[str, Any]) -> Any
Resolves the appropriate feature based on the condition.
Examples#
>>> import deeptrack as dt >>> import numpy as np
Define two Gaussian noise features: >>> true_feature = dt.Gaussian(sigma=0) >>> false_feature = dt.Gaussian(sigma=5)
— Using a boolean condition — Combine the features into a conditional set feature. If not provided explicitely, condition is assumed to be True: >>> conditional_feature = dt.ConditionalSetFeature( … on_true=true_feature, … on_false=false_feature, … )
Define an image: >>> image = np.ones((512, 512))
Resolve based on the condition: >>> clean_image = conditional_feature(image) # If not specified, default is True >>> print(clean_image.std()) # Should be 0 0.0
>>> noisy_image = conditional_feature(image, condition=False) >>> print(noisy_image.std()) # Should be ~5 4.987707046984823
>>> clean_image = conditional_feature(image, condition=True) >>> print(clean_image.std()) # Should be 0 0.0
— Using a string-based condition — Define condition as a string: >>> conditional_feature = dt.ConditionalSetFeature( … on_true=true_feature, … on_false=false_feature, … condition = “is_noisy”, … )
Resolve based on the conditions: >>> noisy_image = conditional_feature(image, is_noisy=False) >>> print(noisy_image.std()) # Should be ~5 5.006310381139811
>>> clean_image = conditional_feature(image, is_noisy=True) >>> print(clean_image.std()) # Should be 0 0.0
Methods Summary
get(image, *, condition, **kwargs)Resolve the appropriate feature based on the condition.
Methods Documentation
- get(image: Any, *, condition: str | bool, **kwargs: dict[str, Any])#
Resolve the appropriate feature based on the condition.
Parameters#
- image: Any
The input image to process.
- condition: str or bool
The name of the conditional property or a boolean value. If a string is provided, it is looked up in kwargs to get the actual boolean value.
- **kwargs:: dict of str to Any
Additional keyword arguments to pass to the resolved feature.
Returns#
- Any
The processed image after resolving the appropriate feature. If neither on_true nor on_false is provided for the corresponding condition, the input image is returned unchanged.