OneOfDict#
- class deeptrack.features.OneOfDict(collection: dict[Any, Feature], key: Any | None | Callable[[...], Any | None] = None, **kwargs: Any)#
Bases:
FeatureResolve one feature from a dictionary and apply it to an input.
This feature selects a feature from a dictionary and applies it to an input. The selection is made randomly by default, but it can be controlled using the key argument.
If key is not specified, a random key from the dictionary is selected, and the corresponding feature is applied. Otherwise, the feature mapped to key is resolved.
Parameters#
- collection: dict[Any, Feature]
A dictionary where keys are identifiers and values are features.
- key: PropertyLike[Any | None], optional
The key of the feature to resolve from the dictionary. If None, a random key is selected.
- **kwargs: Any
Additional parameters passed to the parent Feature class.
Attributes#
- __distributed__: bool
Set to False, indicating that this feature’s .get() method processes the entire input at once even if it is a list, rather than distributing calls for each item of the list.
Methods#
- _process_properties(propertydict) -> dict
It determines which feature to use based on key.
- get(inputs, key, _ID, **kwargs) -> Any
It resolves the selected feature and applies it to the input.
Examples#
>>> import deeptrack as dt
Define a dictionary of features:
>>> features_dict = { ... "add": dt.Add(value=10), ... "multiply": dt.Multiply(value=2), ... }
Create a OneOfDict feature that randomly selects a transformation:
>>> one_of_dict_feature = dt.OneOfDict(features_dict)
Creare an array:
>>> import numpy as np >>> >>> input_array = np.array([1, 2, 3])
Apply a randomly selected feature to the array:
>>> output_array = one_of_dict_feature(input_array) >>> output_array # The output depends on the randomly selected feature array([2, 4, 6]) # Alternatively: array([11, 12, 13])
Potentially select a different feature:
>>> output_array = one_of_dict_feature.new(input_array) >>> output_array
Use a specific key to apply a predefined feature:
>>> controlled_feature = dt.OneOfDict(features_dict, key="add") >>> output_array = controlled_feature(input_array) >>> output_array array([11, 12, 13])
Methods Summary
get(inputs, key[, _ID])Resolve the selected feature and apply it to the input.
Methods Documentation
- get(inputs: Any, key: Any, _ID: tuple[int, ...] = (), **kwargs: Any) Any#
Resolve the selected feature and apply it to the input.
Parameters#
- inputs: Any
The input data to be processed.
- key: Any
The key of the feature to apply from the dictionary.
- _ID: tuple[int, …], optional
A unique identifier for caching and parallel execution.
- **kwargs: Any
Additional parameters passed to the selected feature.
Returns#
- Any
The output of the selected feature applied to the input.