nx_parallel.utils.chunk#
Functions#
|
Yield chunks from input iterable. |
|
Get the positive value of n_jobs |
|
Create an iterable of function inputs for parallel computation |
Module Contents#
- nx_parallel.utils.chunk.chunks(iterable, n_chunks, *, max_chunk_size=None)[source]#
Yield chunks from input iterable.
If max_chunk_size is None (default), the iterable is split into exactly n_chunks equally sized chunks.
If max_chunk_size is specified and the default split would create chunks larger than this size, the iterable is instead divided into smaller chunks, each containing at most max_chunk_size items.
Parameters#
- iterableIterable
An iterable of inputs to be divided.
- n_chunksint
The number of chunks the iterable is divided into. Ignored if chunks’ size exceed the max_chunk_size value.
- max_chunk_sizeint, optional (default = None)
Maximum number of items allowed in each chunk. If None, it divides the iterable into n_chunks chunks.
Examples#
>>> import nx_parallel as nxp >>> data = list(range(10)) >>> list(nxp.chunks(data, 3)) [(0, 1, 2, 3), (4, 5, 6), (7, 8, 9)] >>> list(nxp.chunks(data, 3, max_chunk_size=2)) [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)] >>> list(nxp.chunks(data, 5, max_chunk_size=3)) [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]
- nx_parallel.utils.chunk.get_n_jobs(n_jobs=None)[source]#
Get the positive value of n_jobs
Returns the positive value of n_jobs by either extracting it from the active configuration system or modifying the passed-in value, similar to joblib’s behavior.
If running under pytest, it returns 2 jobs.
If the active configuration in NetworkX’s config is True, n_jobs is extracted from the NetworkX config.
Otherwise, n_jobs is obtained from joblib’s active backend.
ValueError is raised if n_jobs is 0.
- nx_parallel.utils.chunk.create_iterables(G, iterator, n_jobs, list_of_iterator=None)[source]#
Create an iterable of function inputs for parallel computation based on the provided iterator type.
Parameters#
- GNetworkX graph
The NetworkX graph.
- iteratorstr
Type of iterator. Valid values are ‘node’, ‘edge’, ‘isolate’
- n_jobsint
The number of parallel jobs to run.
- list_of_iteratorlist, optional
A precomputed list of items to iterate over. If None, it will be generated based on the iterator type.
Returns#
- iterableIterable
An iterable of function inputs.
Raises#
- ValueError
If the iterator type is not one of “node”, “edge” or “isolate”.