as_list#
- deeptrack.utils.as_list(obj: Any) list[Any]#
Ensure that the input is a list.
Converts the input to a list if it is iterable and not a string or bytes; otherwise, it wraps it in a list.
Note: If obj is a PyTorch Tensor, this function will return a list of its elements along the first dimension (e.g., for a 2D tensor, the result will be a list of 1D tensors). If you want to wrap the entire tensor in a list, use [obj] explicitly.
Parameters#
- obj: Any
The object to be converted or wrapped in a list.
Returns#
- list[Any]
The input object as a list.
Examples#
from deeptrack.utils import as_list
Wrap a scalar in a list:
>>> as_list(5) [5]
>>> as_list(None) [None]
Pass through a list unchanged:
>>> as_list([1, 2, 3]) [1, 2, 3]
Convert a tuple or set to a list:
>>> as_list((1, 2, 3)) [1, 2, 3]
>>> sorted(as_list({3, 2, 1})) [1, 2, 3]
Convert a generator to a list:
>>> generator = (x * 2 for x in range(3)) >>> as_list(generator) [0, 2, 4]
Strings and bytes are treated as atomic (not split):
>>> as_list("abc") ['abc']
>>> as_list(b"xyz") [b'xyz']
NumPy arrays become lists of elements:
>>> import numpy as np >>> as_list(np.array([1, 2, 3])) [1, 2, 3]
PyTorch tensors become lists of elements along the first dimension (if PyTorch is available):
>>> import torch >>> t = torch.tensor([[1, 2], [3, 4]]) >>> as_list(t) [tensor([1, 2]), tensor([3, 4])]