OneOf#

class deeptrack.features.OneOf(collection: Iterable[Feature], key: int | None | Callable[[...], int | None] = None, **kwargs: Any)#

Bases: Feature

Resolve one feature from a given collection.

This feature selects and applies one of multiple features from a given collection. The default behavior selects a feature randomly, but this behavior can be controlled by specifying a key, which determines the index of the feature to apply.

The collection should be an iterable (e.g., list, tuple, or set), and it will be converted to a tuple internally to ensure consistent indexing.

Parameters#

collection: Iterable[Feature]

A collection of features to choose from.

key: PropertyLike[int | None], optional

The index of the feature to resolve from the collection. If not provided, a feature is selected randomly at each execution.

**kwargs: Any

Additional keyword arguments 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 processes the properties to determine the selected feature index.

get(image, key, _ID, **kwargs) -> Any

It applies the selected feature to the input.

Examples#

>>> import deeptrack as dt

Define multiple features:

>>> feature_1 = dt.Add(b=10)
>>> feature_2 = dt.Multiply(b=2)

Create a OneOf feature that randomly selects a transformation:

>>> one_of_feature = dt.OneOf([feature_1, feature_2])

Create an input array:

>>> import numpy as np
>>>
>>> input_array = np.array([1, 2, 3])

Apply the OneOf feature to the input image:

>>> output_array = one_of_feature(input_array)
>>> output_array  # The output depends on the randomly selected feature
array([2, 4, 6])  # Alternative: array([11, 12, 13])

Potentially selects a different feature:

>>> output_array = one_of_feature.new(input_array)
>>> output_array

Use key to apply a specific feature:

>>> controlled_feature = dt.OneOf([feature_1, feature_2], key=0)
>>> output_array = controlled_feature(input_array)
>>> output_array
array([11, 12, 13])
>>> controlled_feature.key.set_value(1)
>>> output_array = controlled_feature(input_array)
>>> output_array
array([2, 4, 6])

Methods Summary

get(inputs, key[, _ID])

Apply the selected feature to the input data.

Methods Documentation

get(inputs: Any, key: int, _ID: tuple[int, ...] = (), **kwargs: Any) Any#

Apply the selected feature to the input data.

Parameters#

inputs: Any

The input data to process.

key: int

The index of the feature to apply from the collection.

_ID: tuple[int, …], optional

A unique identifier for caching and parallel processing.

**kwargs: Any

Additional parameters passed to the selected feature.

Returns#

Any

The output of the selected feature applied to the input.