ConditionalSetFeature#
- class deeptrack.features.ConditionalSetFeature(on_false: Feature | None = None, on_true: Feature | None = None, condition: str | bool | Callable[[...], str | bool] = True, **kwargs: Any)#
Bases:
StructuralFeatureConditionally resolve one of two features.
Deprecated since version 2.0: This feature is deprecated and may be removed in a future release. It is recommended to use Arguments instead.
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.
It is advisable to use Arguments instead when possible.
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 | 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: Any
Additional keyword arguments passed to the parent StructuralFeature.
Methods#
- get(image: Any, condition: str or bool, **kwargs: Any) -> Any
Resolves the appropriate feature based on the condition.
Examples#
>>> import deeptrack as dt
Define an image:
>>> import numpy as np >>> >>> image = np.ones((512, 512))
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, the condition is assumed to be True:
>>> conditional_feature = dt.ConditionalSetFeature( ... on_true=true_feature, ... on_false=false_feature, ... )
Resolve based on the condition. If not specified, default is True:
>>> clean_image = conditional_feature(image) >>> round(clean_image.std(), 1) 0.0
>>> noisy_image = conditional_feature(image, condition=False) >>> round(noisy_image.std(), 1) 5.0
>>> clean_image = conditional_feature(image, condition=True) >>> round(clean_image.std(), 1) 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) >>> round(noisy_image.std(), 1) 5.0
>>> clean_image = conditional_feature(image, is_noisy=True) >>> round(clean_image.std(), 1) 0.0
Methods Summary
get(inputs, *, condition, **kwargs)Resolve the appropriate feature based on the condition.
Methods Documentation
- get(inputs: Any, *, condition: str | bool, **kwargs: Any)#
Resolve the appropriate feature based on the condition.
Parameters#
- inputs: Any
The inputs 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:: Any
Additional keyword arguments to pass to the resolved feature.
Returns#
- Any
The processed data after resolving the appropriate feature. If neither on_true nor on_false is provided for the corresponding condition, the input is returned unchanged.