spekk.transformations.reduce.Reduce#
- class spekk.transformations.reduce.Reduce(dimension: str, reduce_fn: ~typing.Callable[[~spekk.transformations.reduce.T_reduction_result, ~spekk.transformations.reduce.T_f_result], ~spekk.transformations.reduce.T_reduction_result], initial_value: ~spekk.transformations.reduce.T_reduction_result | None = None, enumerate: bool = False, extra_args: ~typing.Sequence[~typing.Any] = <factory>, extra_kwargs: ~typing.Dict[str, ~typing.Any] = <factory>, reduce_impl: ~typing.Callable[[~typing.Callable[[~spekk.transformations.reduce.T_reduction_result, ~spekk.transformations.reduce.T_f_result], ~spekk.transformations.reduce.T_reduction_result], ~typing.Iterable, ~spekk.transformations.reduce.T_reduction_result], ~spekk.transformations.reduce.T_reduction_result] | None = None)[source]#
Bases:
TransformationTransform a function to make it reduce the values of a dimension iteratively.
A
Reducetransformation is generally aForAllandApplytransformation combined, if theApplytransformation somehow aggregates the result (for example by summing over the vectorized axis).As a concrete example:
ForAll("transmits")followed byApply(np.sum, Axis("transmits")is equivalent toReduce.Sum("transmits"), but usingReduce.Sumwill likely allocate a lot less memory, potentially at the cost of processing time.- __init__(dimension: str, reduce_fn: ~typing.Callable[[~spekk.transformations.reduce.T_reduction_result, ~spekk.transformations.reduce.T_f_result], ~spekk.transformations.reduce.T_reduction_result], initial_value: ~spekk.transformations.reduce.T_reduction_result | None = None, enumerate: bool = False, extra_args: ~typing.Sequence[~typing.Any] = <factory>, extra_kwargs: ~typing.Dict[str, ~typing.Any] = <factory>, reduce_impl: ~typing.Callable[[~typing.Callable[[~spekk.transformations.reduce.T_reduction_result, ~spekk.transformations.reduce.T_f_result], ~spekk.transformations.reduce.T_reduction_result], ~typing.Iterable, ~spekk.transformations.reduce.T_reduction_result], ~spekk.transformations.reduce.T_reduction_result] | None = None) None#
Methods
Product(dimension[, initial_value, reduce_impl])Transformation that iteratively multiplies the results of the wrapped function for each item in the given dimension.
Sum(dimension[, initial_value, reduce_impl])Transformation that iteratively adds the results of the wrapped function for each item in the given dimension.
__init__(dimension, reduce_fn[, ...])transform_function(to_be_transformed, ...)Transform the wrapped function given the spec of the input arguments and the spec of the returned value of the wrapped function.
transform_input_spec(spec)Return a new spec that represent the input arguments that are passed down to the wrapped function after the transformation has been applied.
transform_output_spec(spec)Return a new spec that represent the returned value of the final transformed function.
Attributes
If True, each item is a tuple of (index, value), similar to Python's built-in
enumerate().The initial value for the reduction.
The
reduceimplementation to use.The dimension to reduce over.
The function to reduce with.
Extra arguments to pass to the reduce function.
Extra keyword arguments to pass to the reduce function.
- classmethod Product(dimension: str, initial_value: T_reduction_result | None = None, reduce_impl: Callable[[Callable[[T_reduction_result, T_f_result], T_reduction_result], Iterable, T_reduction_result], T_reduction_result] | None = None) T_reduce_cls[source]#
Transformation that iteratively multiplies the results of the wrapped function for each item in the given dimension.
- classmethod Sum(dimension: str, initial_value: T_reduction_result | None = None, reduce_impl: Callable[[Callable[[T_reduction_result, T_f_result], T_reduction_result], Iterable, T_reduction_result], T_reduction_result] | None = None) T_reduce_cls[source]#
Transformation that iteratively adds the results of the wrapped function for each item in the given dimension.
- dimension: str#
The dimension to reduce over.
- enumerate: bool = False#
If True, each item is a tuple of (index, value), similar to Python’s built-in
enumerate().
- extra_args: Sequence[Any]#
Extra arguments to pass to the reduce function.
- extra_kwargs: Dict[str, Any]#
Extra keyword arguments to pass to the reduce function.
- initial_value: T_reduction_result | None = None#
The initial value for the reduction. For example 0 for summation.
- reduce_fn: Callable[[T_reduction_result, T_f_result], T_reduction_result]#
The function to reduce with. For example operator.add for summation.
- reduce_impl: Callable[[Callable[[T_reduction_result, T_f_result], T_reduction_result], Iterable, T_reduction_result], T_reduction_result] | None = None#
The
reduceimplementation to use. Defaults to Python’s built-infunctools.reduce().
- transform_function(to_be_transformed: callable, input_spec: Spec, output_spec: Spec) callable[source]#
Transform the wrapped function given the spec of the input arguments and the spec of the returned value of the wrapped function.
- transform_input_spec(spec: Spec) Spec[source]#
Return a new spec that represent the input arguments that are passed down to the wrapped function after the transformation has been applied.
For example, if the transformation vectorizes the wrapped function over a dimension, the wrapped function would only see single items of the dimension at a time. Therefore, the input spec will have one less dimension when passed down to the wrapped function.