corelay.processor.base
A module that contains the abstract base class for all processors, Processor, as well as a basic processor,
FunctionProcessor, which invokes a specified function. Furthermore, the module contains a function, which ensures
that a specified argument is of type Processor and, if it is not, but callable, makes it a
FunctionProcessor.
Functions
Ensures that the specified processor or callable argument |
Classes
A |
|
The abstract base class of processors, which perform specific tasks in a |
- class corelay.processor.base.Processor[source]
-
The abstract base class of processors, which perform specific tasks in a
corelay.pipeline.base.Pipelineinstance.- is_output: Annotated[bool, Param]
Contains a value indicating whether this
Processoris the output of aPipeline.
- is_checkpoint: Annotated[bool, Param]
Contains a value indicating whether check-pointed pipeline computations should start at this point, if there exists a previously computed checkpoint value.
- io: Annotated[Storable, Param]
Contains an IO object that is used to cache intermediate results of the
Pipeline, which can then be re-used in this run or in subsequent runs of thePipeline.
- __init__(*args: Any, is_output: bool | None = None, is_checkpoint: bool | None = None, io: Storable | None = None, **kwargs: Any) None[source]
Initializes a new
Processorinstance. All definedParamclass attributes are initialized either to their respective default values or, if supplied as keyword argument, to the value supplied.- Parameters:
*args (Any) – A
listof the positional arguments, which will be used to initialize the parameters of theProcessorthat were marked as positional.is_output (bool | None) – A value indicating whether this
Processoris the output of aPipeline. IfNoneis specified, the correspondingParamwill default to its defined default value, which isFalse.is_checkpoint (bool | None) – A value indicating whether check-pointed pipeline computations should start at this point, if there exists a previously computed checkpoint value. If
Noneis specified, the correspondingParamwill default to its defined default value, which isFalse.io (Storable | None) – An IO object that is used to cache intermediate results of the
Pipeline, which can then be re-used in this run or in subsequent runs of thePipeline. IfNoneis specified, the correspondingParamwill default to its defined default value, which is an instance ofcorelay.io.NoStorage.**kwargs (Any) – A
dictof keyword arguments, which will be used to initialize the parameters of theProcessorthat were marked as keyword arguments. The keys of thedictare the names of the parameters, and the values are the values to be assigned to those parameters.
- Raises:
TypeError – The number of positional arguments supplied is greater than the number of parameters that were marked as positional or a parameter was defined as both positional and a keyword argument.
- Return type:
None
- checkpoint_data: Any
If this
Processoris a checkpoint, and if the processor was called at least once, stores the output of this processor.
- abstractmethod function(data: Any) Any[source]
Applies a function to the input data. This function should be implemented by subclasses of
Processor.- Parameters:
- Raises:
NotImplementedError – This is an abstract method and should be implemented by subclasses of
Processorand therefore always raises theNotImplementedErrorexception.- Returns:
Returns the output of the function applied to the input data.
- Return type:
- __call__(data: Any) Any[source]
Applies
function()on the input data and saves the output if theis_checkpointParamwas set toTrue.
- param_values() dict[str, Any][source]
Get values for all parameters defined through
Paramattributes.
- identifiers() OrderedDict[str, Any][source]
Returns a dict containing the class qualifier name, as well all parameters marked as identifiers with their values.
- Returns:
Returns an
collections.OrderedDict, containing the class qualifier name and all parameters marked as identifiers with their values.- Return type:
- copy() Processor[source]
Copies this processor, by creating a new
Processorinstance with the same values for the parameters defined asParamclass attributes and the same checkpoint data.
- __repr__() str[source]
Generates a
strrepresentation of theProcessorinstance, including the class name, the parameters and their values, and the output representation, e.g., ProcessorName(metric=sqeuclidean, function=lambda x: x.mean(1)) -> numpy.ndarray.
- __tracked__: collections.OrderedDict[str, Any]
An
collections.OrderedDictwith all public class attributes, i.e., all class attributes not enclosed with double underscores.
- class corelay.processor.base.FunctionProcessor[source]
Bases:
ProcessorA
Processorthat executes a user-defined function.- Parameters:
processing_function (FunctionType) – The function around which to create the
FunctionProcessor. This function will be invoked when thefunction()method is invoked or theFunctionProcessorobject is called like a function. Depending on whetherbind_methodisTrueorFalse, it wil be bound as a method to theFunctionProcessorobject.is_output (bool) – A value indicating whether this
FunctionProcessoris the output of aPipeline. Defaults toFalse.is_checkpoint (bool | None) – A value indicating whether check-pointed pipeline computations should start at this point, if there exists a previously computed checkpoint value. Defaults to
False.io (Storable | None) – An IO object that is used to cache intermediate results of the
Pipeline, which can then be re-used in this run or in subsequent runs of thePipeline. Defaults to an instance ofNoStorage.bind_method (bool) – A value indicating whether the
processing_functionwill be bound to this class, enabling it to access self. Defaults toFalse.
- __tracked__: collections.OrderedDict[str, Any]
An
collections.OrderedDictwith all public class attributes, i.e., all class attributes not enclosed with double underscores.
- processing_function: Annotated[LambdaType, Param]
The function around which to create the
FunctionProcessor. This function will be invoked when thefunction()method is invoked or theFunctionProcessorobject is called like a function. Depending on whetherbind_methodisTrueorFalse, it wil be bound as a method to theFunctionProcessorobject.
- bind_method: Annotated[bool, Param]
A value indicating whether the
processing_functionwill be bound to this class, enabling it to access self.
- function(data: Any) Any[source]
Invokes the function bound to this class with the input data.
Note
In a previous version of CoRelAy, the
processing_functionwas actually bound to the class in the__call__()method, but this caused typing issues, as static type checkers like MyPy believed that theFunctionProcessorclass was still abstract, as it did not explicitly override thefunction()method. Theprocessing_functionused to be called justfunction(), which meant, that during runtime, functionally, thefunction()method was overridden, as its slot would have been taken by thefunction()parameter. Statically, however, this was not the case. Overriding thefunction()method and still binding theprocessing_functionto the class in the__call__()method causes more typing issues, as the static type checker does not allow us to write to a method slot. For this reason, thefunction()method was overridden and internally calls theprocessing_functionmethod with self as the first argument. Functionally, this should be equivalent to the previous version, but it is not guaranteed that it is in every use case. This might have rethought and changed in the future.
- corelay.processor.base.ensure_processor(processor_or_callable: Processor | Callable[[...], Any], **kwargs: Any) Processor[source]
Ensures that the specified processor or callable argument
processor_or_callableis of typeProcessorand, if it is not, but callable, make it aFunctionProcessor. Sets the attributes of resulting processor as stated in **kwargs.- Parameters:
- Raises:
TypeError – The supplied processor or callable
processor_or_callableis neither aProcessornor callable.- Returns:
Returns the original
processor_or_callableif it was aProcessor, or a newFunctionProcessor, which calls it if it was a callable. The attributes of the resulting processor are set as stated in **kwargs.- Return type: