tpcp.misc.BaseTypedIterator#

class tpcp.misc.BaseTypedIterator(data_type: type[DataclassT], aggregations: Sequence[tuple[str, Callable[[list, list], Any]]] = cf([]))[source]#

A Base class to implement custom typed iterators.

This class is missing the iterate method, which needs to be implemented by the child class. It has a _iterate method though that does most of the heavy lifting. The actual iterate method should handle turning the inputs into an iterable and then call _iterate.

TypedIterator provides a “dummy” implementation that expects any type of iterator for the iterate method. Custom base classes could provide more elaborate preprocessing of the inputs before iteration. For example, cutting sections out of a dataframe based on a list of start and end indices, and then iterating over the cut sections.

Parameters:
data_type

A dataclass that defines the result type you expect from each iteration.

aggregations

An optional list of aggregations to apply to the results. This has the form [(result_name, aggregation_function), ...]. If a result-name is in the list, the aggregation will be applied to it, when accessing the respective result attribute (i.e. {result_name}_). If no aggregation is defined for a result, a simple list of all results will be returned.

NULL_VALUE

(Class attribute) The value that is used to initialize the result dataclass and will remain in the results, if no result was for a specific attribute in one or more iterations.

Attributes:
inputs_

List of all input elements that were iterated over.

raw_results_

List of all results as dataclass instances. The attribute of the dataclass instance will have the a value of _NOT_SET if no result was set. To check for this, you can use isinstance(val, TypedIterator.NULL_VALUE).

results_

The aggregated results.

{result_name}_

The aggregated results for the respective result name.

done_

True, if the iterator is done. If the iterator is not done, but you try to access the results, a warning will be raised.

Methods

clone()

Create a new instance of the class with all parameters copied over.

get_params([deep])

Get parameters for this algorithm.

set_params(**params)

Set the parameters of this Algorithm.

clone() Self[source]#

Create a new instance of the class with all parameters copied over.

This will create a new instance of the class itself and all nested objects

get_params(deep: bool = True) dict[str, Any][source]#

Get parameters for this algorithm.

Parameters:
deep

Only relevant if object contains nested algorithm objects. If this is the case and deep is True, the params of these nested objects are included in the output using a prefix like nested_object_name__ (Note the two “_” at the end)

Returns:
params

Parameter names mapped to their values.

property results_: DataclassT#

The aggregated results.

Note, that this returns an instance of the result object, even-though the datatypes of the attributes might be different depending on the aggregation function. We still decided it makes sense to return an instance of the result object, as it will allow to autocomplete the attributes, even-though the associated times might not be correct.

set_params(**params: Any) Self[source]#

Set the parameters of this Algorithm.

To set parameters of nested objects use nested_object_name__para_name=.