towhee.trainer.utils.trainer_utils.recursively_apply

towhee.trainer.utils.trainer_utils.recursively_apply(func: ~typing.Callable, data: ~typing.Any, *args, test_type: ~typing.Callable = <function is_torch_tensor>, error_on_other_type: bool = False, **kwargs)[source]

Recursively apply a function on a data structure that is a nested list/tuple/dictionary of a given base type.

Parameters:
  • func (callable) – The function to recursively apply.

  • data (nested list/tuple/dictionary of main_type) – The data on which to apply func

  • *args – Positional arguments that will be passed to func when applied on the unpacked data.

  • main_type (type, optional, defaults to torch.Tensor) – The base type of the objects to which apply func.

  • error_on_other_type (bool, optional, defaults to False) – Whether to return an error or not if after unpacking data, we get on an object that is not of type main_type. If False, the function will leave objects of types different than main_type unchanged.

  • **kwargs – Keyword arguments that will be passed to func when applied on the unpacked data.

Returns:

The same data structure as data with func applied to every object of type main_type.