pykanto.utils.compute
pykanto.utils.compute#
A collection of functions and decorators used to manage common operations (e.g., parallelisation, timing).
Functions
|
Calculate chunk size to optimise parallel computing. |
|
Builds a dictionary from a list of dictionaries. |
|
Flattens a list using chain. |
|
Yields successive n-sized chunks from list. |
|
Pretty print a class __dict__ attribute. |
|
Prints information about a parallel process for the user. |
|
Custom timer decorator. |
|
Use ray paralell with a progress bar. |
|
Returns a custom progress bar. |
- pykanto.utils.compute.to_iterator(obj_ids, breaks: bool = True)[source]#
Use ray paralell with a progress bar. Modified from https://git.io/JMv6r.
- pykanto.utils.compute.with_pbar(iterable: Iterable[Any], desc: str | None = None, **kwargs) tqdm [source]#
Returns a custom progress bar. This is just a wrapper around tqdm.
- Parameters
iterable (Iterable[Any]) – Object to iterate on.
desc (str, optional) – Description of what’s going on. Defaults to None.
- Returns
progress bar.
- Return type
tqdm
- pykanto.utils.compute.print_dict(dictionary: Dict) str [source]#
Pretty print a class __dict__ attribute.
- Parameters
dictionary (Dict) – __dict__ attribute containing an object’s writable attributes.
- Returns
Dictionary contents in a legible way.
- Return type
str
- pykanto.utils.compute.timing(f)[source]#
Custom timer decorator. Prints time info unless used within a KantoData where parameters.verbose = False.
- pykanto.utils.compute.calc_chunks(len_iterable: int, factor: int = 2, n_workers: float | None = None, verbose: bool = False) Chunkinfo [source]#
Calculate chunk size to optimise parallel computing. Adapted from https://stackoverflow.com/a/54032744.
- returns [‘n_workers’, ‘len_iterable’, ‘n_chunks’,
‘chunksize’, ‘last_chunk’]
- pykanto.utils.compute.get_chunks(lst: List[Any], n: int) Iterator[Any] [source]#
Yields successive n-sized chunks from list. Last chunk will be shorter if len(lst) % n != 0.
- Parameters
lst (List[Any]) – List to return chunks from.
n (int) – Number of chunks
- Yields
Iterator[Any] – n-sized chunks.
- pykanto.utils.compute.dictlist_to_dict(dictlist: List[Dict[str, Any]]) Dict[str, Any] [source]#
Builds a dictionary from a list of dictionaries.
- Parameters
dictlist (List[Dict[str, Any]]) – List of dictionaries.
- Returns
Dictionary with items == list elements.
- Return type
Dict[str, Any]
- pykanto.utils.compute.print_parallel_info(n: int, iterable_name: str, n_chunks: int, chunk_length: int) None [source]#
Prints information about a parallel process for the user.
- Parameters
n (int) – Total length of iterable.
iterable_name (str) – Description of iterable.
n_chunks (int) – Number of chunks that will be used.
chunk_length (int) – Length of each chunk.