composer.utils.iter_helpers#
Utilities for iterating over collections.
Functions
Converts |
|
Iterate over a batch iterator and update a |
|
Apply |
Classes
All the operations on a read-only sequence. |
- composer.utils.iter_helpers.ensure_tuple(x)[source]#
Converts
xinto a tuple.If
xisNone, thentuple()is returned.If
xis a tuple, thenxis returned as-is.If
xis a list, thentuple(x)is returned.If
xis a dict, thentuple(v for v in x.values())is returned.
Otherwise, a single element tuple of
(x,)is returned.- Parameters
x (Any) โ The input to convert into a tuple.
- Returns
tuple โ A tuple of
x.
- composer.utils.iter_helpers.iterate_with_pbar(iterator, progress_bar=None)[source]#
Iterate over a batch iterator and update a
tqdm.tqdmprogress bar by the batch size on each step.This function iterates over
iterator, which is expected to yield batches of elements. On each step, the batch is yielded back to the caller, and theprogress_baris updated by the length of each batch.Note
It is expected that the
progress_bar = tqdm.tqdm(total=sum(len(x) for x in iterator)).- Parameters
iterator (Iterator[TSized]) โ An iterator that yields batches of elements.
progress_bar (Optional[tqdm.tqdm], optional) โ A
tqdm.tqdmprogress bar. IfNone(the default), then this function simply yields fromiterator.
- Yields
Iterator[TSized] โ The elements of
iterator.
- composer.utils.iter_helpers.map_collection(collection, map_fn)[source]#
Apply
map_fnon each element incollection.If
collectionis a tuple or list of elements,map_fnis applied on each element, and a tuple or list, respectively, containing mapped values is returned.If
collectionis a dictionary,map_fnis applied on each value, and a dictionary containing the mapped values is returned.If
collectionisNone,Noneis returned.If
collectionis a single element, the result of applyingmap_fnon it is returned.
- Parameters
collection โ The element, or a tuple of elements.
map_fn โ A function to invoke on each element.
- Returns
Collection โ The result of applying
map_fnon each element ofcollection.The type of ``collection`` is preserved.