chunking module¶
Utilities for chunking.
chunked function¶
chunked(
*args,
chunker_cls=None,
size=None,
min_size=None,
n_chunks=None,
chunk_len=None,
chunk_meta=None,
prepend_chunk_meta=None,
skip_single_chunk=None,
arg_take_spec=None,
template_context=None,
merge_func=None,
merge_kwargs=None,
return_raw_chunks=False,
silence_warnings=None,
forward_kwargs_as=None,
execute_kwargs=None,
merge_to_execute_kwargs=None,
disable=None,
eval_id=None,
**kwargs
)
Decorator that chunks the inputs of a function using Chunker.
Returns a new function with the same signature as the passed one.
Each option can be modified in the options attribute of the wrapper function or directly passed as a keyword argument with a leading underscore.
Keyword arguments **kwargs and execute_kwargs are merged into execute_kwargs if merge_to_execute_kwargs is True, otherwise, **kwargs are passed directly to Chunker.
Chunking can be disabled using disable argument. Additionally, the entire wrapping mechanism can be disabled by using the global setting disable_wrapping (=> returns the wrapped function).
Usage
For testing purposes, let's divide the input array into 2 chunks and compute the mean in a sequential manner:
>>> from vectorbtpro import *
>>> @vbt.chunked(
... n_chunks=2,
... size=vbt.LenSizer(arg_query='a'),
... arg_take_spec=dict(a=vbt.ChunkSlicer())
... )
... def f(a):
... return np.mean(a)
>>> f(np.arange(10))
[2.0, 7.0]
Same can be done using annotations:
>>> @vbt.chunked(n_chunks=2)
... def f(a: vbt.LenSizer() | vbt.ChunkSlicer()):
... return np.mean(a)
>>> f(np.arange(10))
[2.0, 7.0]
Sizer can be omitted most of the time:
>>> @vbt.chunked(n_chunks=2)
... def f(a: vbt.ChunkSlicer()):
... return np.mean(a)
>>> f(np.arange(10))
[2.0, 7.0]
Another way is by using specialized Chunker subclasses that depend on the type of the argument:
>>> @vbt.chunked(n_chunks=2)
... def f(a: vbt.ChunkedArray()):
... return np.mean(a)
>>> f(np.arange(10))
Also, instead of specifying the chunk taking specification beforehand, it can be passed dynamically by wrapping each value to be chunked with Chunked or any of its subclasses:
>>> @vbt.chunked(n_chunks=2)
... def f(a):
... return np.mean(a)
>>> f(vbt.ChunkedArray(np.arange(10)))
[2.0, 7.0]
The chunked() function is a decorator that takes f and creates a function that splits passed arguments, runs each chunk using an engine, and optionally, merges the results. It has the same signature as the original function:
We can change any option at any time:
>>> # Change the option directly on the function
>>> f.options.n_chunks = 3
>>> f(np.arange(10))
[1.5, 5.0, 8.0]
>>> # Pass a new option with a leading underscore
>>> f(np.arange(10), _n_chunks=4)
[1.0, 4.0, 6.5, 8.5]
When we run the wrapped function, it first generates a list of chunk metadata of type ChunkMeta. Chunk metadata contains the chunk index that can be used to split any input:
>>> list(vbt.yield_chunk_meta(n_chunks=2))
[ChunkMeta(uuid='84d64eed-fbac-41e7-ad61-c917e809b3b8', idx=0, start=None, end=None, indices=None),
ChunkMeta(uuid='577817c4-fdee-4ceb-ab38-dcd663d9ab11', idx=1, start=None, end=None, indices=None)]
Additionally, it may contain the start and end index of the space we want to split. The space can be defined by the length of an input array, for example. In our case:
>>> list(vbt.yield_chunk_meta(n_chunks=2, size=10))
[ChunkMeta(uuid='c1593842-dc31-474c-a089-e47200baa2be', idx=0, start=0, end=5, indices=None),
ChunkMeta(uuid='6d0265e7-1204-497f-bc2c-c7b7800ec57d', idx=1, start=5, end=10, indices=None)]
If we know the size of the space in advance, we can pass it as an integer constant. Otherwise, we need to tell chunked() to derive the size from the inputs dynamically by passing any subclass of Sizer. In the example above, we instruct the wrapped function to derive the size from the length of the input array a.
Once all chunks are generated, the wrapped function attempts to split inputs into chunks. The specification for this operation can be provided by the arg_take_spec argument, which in most cases is a dictionary of ChunkTaker instances keyed by the input name. Here's an example of a complex specification:
>>> arg_take_spec = dict(
... a=vbt.ChunkSelector(),
... args=vbt.ArgsTaker(
... None,
... vbt.ChunkSelector()
... ),
... b=vbt.SequenceTaker([
... None,
... vbt.ChunkSelector()
... ]),
... kwargs=vbt.KwargsTaker(
... c=vbt.MappingTaker(dict(
... d=vbt.ChunkSelector(),
... e=None
... ))
... )
... )
>>> @vbt.chunked(
... n_chunks=vbt.LenSizer(arg_query='a'),
... arg_take_spec=arg_take_spec
... )
... def f(a, *args, b=None, **kwargs):
... return a + sum(args) + sum(b) + sum(kwargs['c'].values())
>>> f([1, 2, 3], 10, [1, 2, 3], b=(100, [1, 2, 3]), c=dict(d=[1, 2, 3], e=1000))
[1114, 1118, 1122]
After splitting all inputs into chunks, the wrapped function forwards them to the engine function. The engine argument can be either the name of a supported engine, or a callable. Once the engine has finished all tasks and returned a list of results, we can merge them back using merge_func:
>>> @vbt.chunked(
... n_chunks=2,
... size=vbt.LenSizer(arg_query='a'),
... arg_take_spec=dict(a=vbt.ChunkSlicer()),
... merge_func="concat"
... )
... def f(a):
... return a
>>> f(np.arange(10))
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
The same using annotations:
>>> @vbt.chunked(n_chunks=2)
... def f(a: vbt.ChunkSlicer()) -> vbt.MergeFunc("concat"):
... return a
>>> f(np.arange(10))
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Instead of (or in addition to) specifying arg_take_spec, we can define our function with the first argument being chunk_meta to be able to split the arguments during the execution. The chunked() decorator will automatically recognize and replace it with the actual ChunkMeta object:
>>> @vbt.chunked(
... n_chunks=2,
... size=vbt.LenSizer(arg_query='a'),
... arg_take_spec=dict(a=None),
... merge_func="concat"
... )
... def f(chunk_meta, a):
... return a[chunk_meta.start:chunk_meta.end]
>>> f(np.arange(10))
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
This may be a good idea in multi-threading, but a bad idea in multi-processing.
The same can be accomplished by using templates (here we tell chunked() to not replace the first argument by setting prepend_chunk_meta to False):
>>> @vbt.chunked(
... n_chunks=2,
... size=vbt.LenSizer(arg_query='a'),
... arg_take_spec=dict(a=None),
... merge_func="concat",
... prepend_chunk_meta=False
... )
... def f(chunk_meta, a):
... return a[chunk_meta.start:chunk_meta.end]
>>> f(vbt.Rep('chunk_meta'), np.arange(10))
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Templates in arguments are substituted right before taking a chunk from them.
Keyword arguments to the engine can be provided using execute_kwargs:
>>> @vbt.chunked(
... n_chunks=2,
... size=vbt.LenSizer(arg_query='a'),
... arg_take_spec=dict(a=vbt.ChunkSlicer()),
... show_progress=True
... )
... def f(a):
... return np.mean(a)
>>> f(np.arange(10))
100% |█████████████████████████████████| 2/2 [00:00<00:00, 81.11it/s]
[2.0, 7.0]
resolve_chunked function¶
Decorate with chunked() based on an option.
resolve_chunked_option function¶
Return keyword arguments for chunked().
option can be:
- True: Chunk using default settings
- None or False: Do not chunk
- string: Use
optionas the name of an execution engine (see execute()) - dict: Use
optionas keyword arguments passed to chunked()
For defaults, see option in chunking.
specialize_chunked_option function¶
Resolve option and merge it with kwargs if it's not None so the dict can be passed as an option to other functions.
yield_chunk_meta function¶
Yield meta of each successive chunk from a sequence with a number of elements.
Args
size:int- Size of the space to split.
min_size:int-
Minimum size.
If
sizeis lower than this number, returns a single chunk. n_chunks:intorstr-
Number of chunks.
If "auto", becomes the number of cores.
chunk_len:intorstr-
Length of each chunk.
If "auto", becomes the number of cores.
If size, n_chunks, and chunk_len are None (after resolving them from settings), returns a single chunk. If only n_chunks and chunk_len are None, sets n_chunks to "auto".
ArgChunkMeta class¶
Class for generating chunk metadata from an argument.
Superclasses
Inherited members
- ArgGetter.arg_query
- ArgGetter.get_arg()
- ArgGetter.hash
- ArgGetter.hash_key
- ChunkMetaGenerator.get_chunk_meta()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Hashable.get_hash()
Subclasses
ArgGetter class¶
Class for getting an argument from annotated arguments.
Superclasses
Inherited members
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.hash
- DefineMixin.hash_key
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Hashable.get_hash()
Subclasses
arg_query class variable¶
Query for annotated argument to derive the size from.
get_arg method¶
Get argument using match_ann_arg().
ArgSizer class¶
Class for getting the size from an argument.
Superclasses
Inherited members
- ArgGetter.arg_query
- ArgGetter.get_arg()
- ArgGetter.hash
- ArgGetter.hash_key
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
- Sizer.apply()
- Sizer.eval_id
- Sizer.get_size()
Subclasses
single_type class variable¶
One or multiple types to consider as a single value.
ArgsTaker class¶
Class for taking from a variable arguments container.
Superclasses
Inherited members
- ChunkTaker.apply()
- ChunkTaker.should_take()
- ContainerTaker.check_cont_take_spec()
- ContainerTaker.get_size()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
- SequenceTaker.adapt_cont_take_spec()
- SequenceTaker.cont_take_spec
- SequenceTaker.eval_id
- SequenceTaker.hash
- SequenceTaker.hash_key
- SequenceTaker.ignore_none
- SequenceTaker.mapper
- SequenceTaker.single_type
- SequenceTaker.suggest_size()
- SequenceTaker.take()
ArraySelector class¶
Class for selecting one element from an array's axis based on the chunk index.
Superclasses
- Annotatable
- AxisSpecifier
- ChunkSelector
- ChunkTaker
- DefineMixin
- DimRetainer
- Evaluable
- Hashable
- ShapeSelector
Inherited members
- ChunkSelector.suggest_size()
- ChunkTaker.apply()
- ChunkTaker.should_take()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
- ShapeSelector.axis
- ShapeSelector.eval_id
- ShapeSelector.get_size()
- ShapeSelector.hash
- ShapeSelector.hash_key
- ShapeSelector.ignore_none
- ShapeSelector.keep_dims
- ShapeSelector.mapper
- ShapeSelector.single_type
- ShapeSelector.take()
Subclasses
ArraySizer class¶
Class for getting the size from the length of an axis in an array.
Superclasses
Inherited members
- ArgGetter.get_arg()
- ArgSizer.apply()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
- ShapeSizer.arg_query
- ShapeSizer.axis
- ShapeSizer.eval_id
- ShapeSizer.get_obj_size()
- ShapeSizer.get_size()
- ShapeSizer.hash
- ShapeSizer.hash_key
- ShapeSizer.single_type
Subclasses
ArraySlicer class¶
Class for slicing multiple elements from an array's axis based on the chunk range.
Superclasses
Inherited members
- ChunkTaker.apply()
- ChunkTaker.should_take()
- ChunkTaker.suggest_size()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
- ShapeSlicer.axis
- ShapeSlicer.eval_id
- ShapeSlicer.get_size()
- ShapeSlicer.hash
- ShapeSlicer.hash_key
- ShapeSlicer.ignore_none
- ShapeSlicer.mapper
- ShapeSlicer.single_type
- ShapeSlicer.take()
Subclasses
AxisSpecifier class¶
Class with an attribute for specifying an axis.
Superclasses
Inherited members
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.hash
- DefineMixin.hash_key
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Hashable.get_hash()
Subclasses
axis class variable¶
Axis of the argument to take from.
ChunkMapper class¶
Abstract class for mapping chunk metadata.
Implements the abstract ChunkMapper.map() method.
Supports caching of each pair of incoming and outgoing ChunkMeta instances.
Note
Use ChunkMapper.apply() instead of ChunkMapper.map().
Superclasses
Inherited members
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.hash
- DefineMixin.hash_key
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Hashable.get_hash()
Subclasses
apply method¶
Apply the mapper.
chunk_meta_cache class variable¶
Cache for outgoing ChunkMeta instances keyed by UUID of the incoming ones.
map method¶
Abstract method for mapping chunk metadata.
Takes the chunk metadata of type ChunkMeta and returns a new chunk metadata of the same type.
should_cache class variable¶
Whether should cache.
ChunkMeta class¶
Class that represents a chunk metadata.
Superclasses
Inherited members
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.hash
- DefineMixin.hash_key
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Hashable.get_hash()
end class variable¶
End of the chunk range (excluding). Can be None.
idx class variable¶
Chunk index.
indices class variable¶
Indices included in the chunk range. Can be None.
Has priority over ChunkMeta.start and ChunkMeta.end.
start class variable¶
Start of the chunk range (including). Can be None.
uuid class variable¶
Unique identifier of the chunk.
Used for caching.
ChunkMetaGenerator class¶
Abstract class for generating chunk metadata from annotated arguments.
Subclasses
get_chunk_meta method¶
Get chunk metadata.
ChunkSelector class¶
Class for selecting one element based on the chunk index.
Superclasses
Inherited members
- ChunkTaker.apply()
- ChunkTaker.eval_id
- ChunkTaker.get_size()
- ChunkTaker.hash
- ChunkTaker.hash_key
- ChunkTaker.ignore_none
- ChunkTaker.mapper
- ChunkTaker.should_take()
- ChunkTaker.single_type
- ChunkTaker.suggest_size()
- ChunkTaker.take()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- DimRetainer.keep_dims
- Evaluable.meets_eval_id()
- Hashable.get_hash()
Subclasses
ChunkSlicer class¶
Class for slicing multiple elements based on the chunk range.
Superclasses
Inherited members
- ChunkTaker.apply()
- ChunkTaker.eval_id
- ChunkTaker.get_size()
- ChunkTaker.hash
- ChunkTaker.hash_key
- ChunkTaker.ignore_none
- ChunkTaker.mapper
- ChunkTaker.should_take()
- ChunkTaker.single_type
- ChunkTaker.suggest_size()
- ChunkTaker.take()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
Subclasses
ChunkTaker class¶
Abstract class for taking one or more elements based on the chunk index or range.
Note
Use ChunkTaker.apply() instead of ChunkTaker.take().
Superclasses
Inherited members
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.hash
- DefineMixin.hash_key
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
Subclasses
apply method¶
Apply the taker.
eval_id class variable¶
One or more identifiers at which to evaluate this instance.
get_size method¶
Get the actual size of the argument.
ignore_none class variable¶
Whether to ignore None.
mapper class variable¶
Chunk mapper of type ChunkMapper.
should_take method¶
Check whether to take a chunk or leave the argument as it is.
single_type class variable¶
One or multiple types to consider as a single value.
suggest_size method¶
Suggest a global size based on the argument's size.
take method¶
Abstract method for taking subset of data.
Takes the argument object, the chunk meta (tuple out of the index, start index, and end index of the chunk), and other keyword arguments passed down the stack, such as chunker and silence_warnings.
Chunkable class¶
Abstract class representing a value and a chunk taking specification.
Superclasses
Inherited members
Subclasses
get_take_spec method¶
Get the chunk taking specification.
get_value method¶
Get the value.
Chunked class¶
Class representing a chunkable value.
Can take a variable number of keyword arguments, which will be used as Chunked.take_spec_kwargs.
Superclasses
Inherited members
- Chunkable.get_take_spec()
- Chunkable.get_value()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.hash
- DefineMixin.hash_key
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
Subclasses
eval_id class variable¶
One or more identifiers at which to evaluate this instance.
resolve_take_spec method¶
Resolve take_spec.
select class variable¶
Whether to chunk by selection.
take_spec class variable¶
Chunk taking specification.
take_spec_kwargs class variable¶
Keyword arguments passed to the respective ChunkTaker subclass.
If Chunked.take_spec is an instance rather than a class, will "evolve" it.
take_spec_missing property¶
Check whether Chunked.take_spec is missing.
value class variable¶
Value.
ChunkedArray class¶
Class representing a chunkable array.
Superclasses
Inherited members
- Chunked.eval_id
- Chunked.get_take_spec()
- Chunked.get_value()
- Chunked.hash
- Chunked.hash_key
- Chunked.resolve_take_spec()
- Chunked.select
- Chunked.take_spec
- Chunked.take_spec_kwargs
- Chunked.take_spec_missing
- Chunked.value
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
flex class variable¶
Whether the array is flexible.
ChunkedCount class¶
Class representing a chunkable count.
Superclasses
Inherited members
- Chunked.eval_id
- Chunked.get_take_spec()
- Chunked.get_value()
- Chunked.hash
- Chunked.hash_key
- Chunked.resolve_take_spec()
- Chunked.select
- Chunked.take_spec
- Chunked.take_spec_kwargs
- Chunked.take_spec_missing
- Chunked.value
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
ChunkedShape class¶
Class representing a chunkable shape.
Superclasses
Inherited members
- Chunked.eval_id
- Chunked.get_take_spec()
- Chunked.get_value()
- Chunked.hash
- Chunked.hash_key
- Chunked.resolve_take_spec()
- Chunked.select
- Chunked.take_spec
- Chunked.take_spec_kwargs
- Chunked.take_spec_missing
- Chunked.value
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
Chunker class¶
Chunker(
func,
size=None,
min_size=None,
n_chunks=None,
chunk_len=None,
chunk_meta=None,
prepend_chunk_meta=None,
skip_single_chunk=None,
arg_take_spec=None,
template_context=None,
merge_func=None,
merge_kwargs=None,
return_raw_chunks=None,
silence_warnings=None,
forward_kwargs_as=None,
execute_kwargs=None,
disable=None,
**kwargs
)
Class responsible for chunking arguments of a function and running the function.
Does the following:
- Generates chunk metadata by passing
n_chunks,size,min_size,chunk_len, andchunk_metato Chunker.get_chunk_meta_from_args(). - Splits arguments and keyword arguments by passing chunk metadata,
arg_take_spec, andtemplate_contextto Chunker.yield_tasks(), which yields one chunk at a time. - Executes all chunks by passing
**execute_kwargsto execute(). - Optionally, post-processes and merges the results by passing them and
**merge_kwargstomerge_func.
For defaults, see chunking.
Superclasses
Inherited members
- Cacheable.get_ca_setup()
- Chainable.pipe()
- Configured.config
- Configured.copy()
- Configured.equals()
- Configured.get_writeable_attrs()
- Configured.prettify()
- Configured.rec_state
- Configured.replace()
- Configured.resolve_merge_kwargs()
- Configured.update_config()
- HasSettings.get_path_setting()
- HasSettings.get_path_settings()
- HasSettings.get_setting()
- HasSettings.get_settings()
- HasSettings.has_path_setting()
- HasSettings.has_path_settings()
- HasSettings.has_setting()
- HasSettings.has_settings()
- HasSettings.reset_settings()
- HasSettings.resolve_setting()
- HasSettings.resolve_settings_paths()
- HasSettings.set_settings()
- Pickleable.decode_config()
- Pickleable.decode_config_node()
- Pickleable.dumps()
- Pickleable.encode_config()
- Pickleable.encode_config_node()
- Pickleable.file_exists()
- Pickleable.getsize()
- Pickleable.load()
- Pickleable.loads()
- Pickleable.modify_state()
- Pickleable.resolve_file_path()
- Pickleable.save()
adapt_ann_args class method¶
Adapt annotated arguments.
arg_take_spec property¶
See yield_tasks.
chunk_len property¶
See Chunker.get_chunk_meta_from_args().
chunk_meta property¶
See Chunker.get_chunk_meta_from_args().
disable property¶
Whether to disable chunking.
execute_kwargs property¶
Keyword arguments passed to execute().
fill_arg_take_spec class method¶
Fill the chunk taking specification with None to avoid warnings.
find_take_spec class method¶
Resolve the specification for an argument.
forward_kwargs_as property¶
Map to rename keyword arguments.
Can also pass any variable from the scope of Chunker.run()
func property¶
Function.
get_chunk_meta_from_args class method¶
Chunker.get_chunk_meta_from_args(
ann_args,
size=None,
min_size=None,
n_chunks=None,
chunk_len=None,
chunk_meta=None,
**kwargs
)
Get chunk metadata from annotated arguments.
Args
ann_args:dict- Arguments annotated with annotate_args().
size:int, Sizer,or callable-
See yield_chunk_meta().
Can be an integer, an instance of Sizer, or a callable taking the annotated arguments and returning a value.
min_size:int- See yield_chunk_meta().
n_chunks:int,str, Sizer,or callable-
See yield_chunk_meta().
Can be an integer, a string, an instance of Sizer, or a callable taking the annotated arguments and other keyword arguments and returning a value.
chunk_len:int,str, Sizer,or callable-
See yield_chunk_meta().
Can be an integer, a string, an instance of Sizer, or a callable taking the annotated arguments and returning a value.
chunk_meta:iterableof ChunkMeta, ChunkMetaGenerator,or callable-
Chunk meta.
Can be an iterable of ChunkMeta, an instance of ChunkMetaGenerator, or a callable taking the annotated arguments and other arguments and returning an iterable.
**kwargs- Other keyword arguments passed to any callable.
merge_func property¶
Merging function.
Resolved using resolve_merge_func().
merge_kwargs property¶
Keyword arguments passed to the merging function.
min_size property¶
See Chunker.get_chunk_meta_from_args().
n_chunks property¶
See Chunker.get_chunk_meta_from_args().
parse_sizer_from_func class method¶
Parse the sizer from a function.
parse_spec_from_annotations class method¶
Parse the chunk taking specification from annotations.
parse_spec_from_args class method¶
Parse the chunk taking specification from (annotated) arguments.
parse_spec_from_func class method¶
Parse the chunk taking specification from a function.
prepend_chunk_meta property¶
Whether to prepend an instance of ChunkMeta to the arguments.
If None, prepends automatically if the first argument is named 'chunk_meta'.
resolve_take_spec class method¶
Resolve the chunk taking specification.
return_raw_chunks property¶
Whether to return chunks in a raw format.
run method¶
Chunk arguments and run the function.
silence_warnings property¶
Whether to silence any warnings.
size property¶
See Chunker.get_chunk_meta_from_args().
skip_single_chunk property¶
Whether to execute the function directly if there's only one chunk.
suggest_size class method¶
Suggest a global size given the annotated arguments and the chunk taking specification.
take_from_arg class method¶
Take from the argument given the specification take_spec.
If take_spec is None or it's an instance of NotChunked, returns the original object. Otherwise, must be an instance of ChunkTaker.
**kwargs are passed to ChunkTaker.apply().
take_from_args class method¶
Chunker.take_from_args(
ann_args,
arg_take_spec,
chunk_meta,
silence_warnings=False,
eval_id=None,
**kwargs
)
Take from each in the annotated arguments given the specification using Chunker.take_from_arg().
Additionally, passes to Chunker.take_from_arg() as keyword arguments ann_args and arg_take_spec.
arg_take_spec must be a dictionary, with keys being argument positions or names as generated by annotate_args(). For values, see Chunker.take_from_arg().
Returns arguments and keyword arguments that can be directly passed to the function using func(*args, **kwargs).
template_context property¶
Template context.
Any template in both execute_kwargs and merge_kwargs will be substituted. You can use the keys ann_args, chunk_meta, arg_take_spec, and tasks to be replaced by the actual objects.
yield_tasks class method¶
Chunker.yield_tasks(
func,
ann_args,
chunk_meta,
arg_take_spec=None,
template_context=None,
**kwargs
)
Split annotated arguments into chunks using Chunker.take_from_args() and yield each chunk as a task.
Args
func:callable- Callable.
ann_args:dict- Arguments annotated with annotate_args().
chunk_meta:iterableof ChunkMeta- Chunk metadata.
arg_take_spec:mapping,sequence,callable,or CustomTemplate-
Chunk taking specification.
Can be a dictionary (see Chunker.take_from_args()), or a sequence that will be converted into a dictionary. If a callable, will be called instead of Chunker.take_from_args(), thus it must have the same arguments apart from
arg_take_spec. template_context:mapping- Context used to substitute templates in arguments and specification.
**kwargs- Keyword arguments passed to Chunker.take_from_args() or to
arg_take_specif it's a callable.
ContainerTaker class¶
Class for taking from a container with other chunk takers.
Accepts the specification of the container.
Superclasses
Inherited members
- ChunkTaker.apply()
- ChunkTaker.eval_id
- ChunkTaker.get_size()
- ChunkTaker.hash
- ChunkTaker.hash_key
- ChunkTaker.ignore_none
- ChunkTaker.mapper
- ChunkTaker.should_take()
- ChunkTaker.single_type
- ChunkTaker.suggest_size()
- ChunkTaker.take()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
Subclasses
check_cont_take_spec method¶
Check that ContainerTaker.cont_take_spec is not None.
cont_take_spec class variable¶
Specification of the container.
CountAdapter class¶
Class for adapting a count based on the chunk range.
Superclasses
Inherited members
- ChunkSlicer.eval_id
- ChunkSlicer.get_size()
- ChunkSlicer.hash
- ChunkSlicer.hash_key
- ChunkSlicer.ignore_none
- ChunkSlicer.mapper
- ChunkSlicer.single_type
- ChunkSlicer.take()
- ChunkTaker.apply()
- ChunkTaker.should_take()
- ChunkTaker.suggest_size()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
CountSizer class¶
Class for getting the size from a count.
Superclasses
Inherited members
- ArgGetter.get_arg()
- ArgSizer.apply()
- ArgSizer.arg_query
- ArgSizer.eval_id
- ArgSizer.get_size()
- ArgSizer.hash
- ArgSizer.hash_key
- ArgSizer.single_type
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
get_obj_size class method¶
Get size of an object.
DimRetainer class¶
Class with an attribute for retaining dimensions.
Superclasses
Inherited members
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.hash
- DefineMixin.hash_key
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Hashable.get_hash()
Subclasses
keep_dims class variable¶
Whether to retain dimensions.
KwargsTaker class¶
Class for taking from a variable keyword arguments container.
Superclasses
Inherited members
- ChunkTaker.apply()
- ChunkTaker.should_take()
- ContainerTaker.check_cont_take_spec()
- ContainerTaker.get_size()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
- MappingTaker.adapt_cont_take_spec()
- MappingTaker.cont_take_spec
- MappingTaker.eval_id
- MappingTaker.hash
- MappingTaker.hash_key
- MappingTaker.ignore_none
- MappingTaker.mapper
- MappingTaker.single_type
- MappingTaker.suggest_size()
- MappingTaker.take()
LenChunkMeta class¶
Class for generating chunk metadata from a sequence of chunk lengths.
Superclasses
Inherited members
- ArgChunkMeta.arg_query
- ArgChunkMeta.get_chunk_meta()
- ArgChunkMeta.hash
- ArgChunkMeta.hash_key
- ArgGetter.get_arg()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Hashable.get_hash()
LenSizer class¶
Class for getting the size from the length of an argument.
Superclasses
Inherited members
- ArgGetter.get_arg()
- ArgSizer.apply()
- ArgSizer.arg_query
- ArgSizer.eval_id
- ArgSizer.get_size()
- ArgSizer.hash
- ArgSizer.hash_key
- ArgSizer.single_type
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
get_obj_size class method¶
Get size of an object.
MappingTaker class¶
Class for taking from a mapping container.
Calls Chunker.take_from_arg() on each element.
Superclasses
Inherited members
- ChunkTaker.apply()
- ChunkTaker.should_take()
- ChunkTaker.suggest_size()
- ContainerTaker.check_cont_take_spec()
- ContainerTaker.cont_take_spec
- ContainerTaker.eval_id
- ContainerTaker.get_size()
- ContainerTaker.hash
- ContainerTaker.hash_key
- ContainerTaker.ignore_none
- ContainerTaker.mapper
- ContainerTaker.single_type
- ContainerTaker.take()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
Subclasses
adapt_cont_take_spec method¶
Prepare the specification of the container to the object.
NotChunked class¶
Class that represents an argument that shouldn't be chunked.
Superclasses
Inherited members
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.hash
- DefineMixin.hash_key
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
eval_id class variable¶
One or more identifiers at which to evaluate this instance.
SequenceTaker class¶
Class for taking from a sequence container.
Calls Chunker.take_from_arg() on each element.
Superclasses
Inherited members
- ChunkTaker.apply()
- ChunkTaker.should_take()
- ChunkTaker.suggest_size()
- ContainerTaker.check_cont_take_spec()
- ContainerTaker.cont_take_spec
- ContainerTaker.eval_id
- ContainerTaker.get_size()
- ContainerTaker.hash
- ContainerTaker.hash_key
- ContainerTaker.ignore_none
- ContainerTaker.mapper
- ContainerTaker.single_type
- ContainerTaker.take()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
Subclasses
adapt_cont_take_spec method¶
Prepare the specification of the container to the object.
ShapeSelector class¶
Class for selecting one element from a shape's axis based on the chunk index.
Superclasses
Inherited members
- AxisSpecifier.axis
- ChunkSelector.eval_id
- ChunkSelector.get_size()
- ChunkSelector.hash
- ChunkSelector.hash_key
- ChunkSelector.ignore_none
- ChunkSelector.keep_dims
- ChunkSelector.mapper
- ChunkSelector.single_type
- ChunkSelector.suggest_size()
- ChunkSelector.take()
- ChunkTaker.apply()
- ChunkTaker.should_take()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
Subclasses
ShapeSizer class¶
Class for getting the size from the length of an axis in a shape.
Superclasses
Inherited members
- ArgGetter.get_arg()
- ArgSizer.apply()
- ArgSizer.arg_query
- ArgSizer.eval_id
- ArgSizer.get_size()
- ArgSizer.hash
- ArgSizer.hash_key
- ArgSizer.single_type
- AxisSpecifier.axis
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
Subclasses
get_obj_size class method¶
Get size of an object.
ShapeSlicer class¶
Class for slicing multiple elements from a shape's axis based on the chunk range.
Superclasses
Inherited members
- AxisSpecifier.axis
- ChunkSlicer.eval_id
- ChunkSlicer.get_size()
- ChunkSlicer.hash
- ChunkSlicer.hash_key
- ChunkSlicer.ignore_none
- ChunkSlicer.mapper
- ChunkSlicer.single_type
- ChunkSlicer.take()
- ChunkTaker.apply()
- ChunkTaker.should_take()
- ChunkTaker.suggest_size()
- DefineMixin.asdict()
- DefineMixin.assert_field_not_missing()
- DefineMixin.get_field()
- DefineMixin.is_field_missing()
- DefineMixin.is_field_optional()
- DefineMixin.is_field_required()
- DefineMixin.merge_over()
- DefineMixin.merge_with()
- DefineMixin.replace()
- DefineMixin.resolve()
- DefineMixin.resolve_field()
- Evaluable.meets_eval_id()
- Hashable.get_hash()
Subclasses
Sizer class¶
Abstract class for getting the size from annotated arguments.
Note
Use Sizer.apply() instead of Sizer.get_size().
Superclasses
Inherited members
Subclasses
apply method¶
Apply the sizer.
eval_id class variable¶
One or more identifiers at which to evaluate this instance.
get_size method¶
Get the size given the annotated arguments.