xdas.atoms.atomized#

xdas.atoms.atomized(func)[source]#

Make the function return an Atom if or an atom is passed as argument.

In case is passed as a positional argument, the function is wrapped into a Partial object. If an Atom object is passed as a positional argument, the function is wrapped into a Sequential object. Otherwise, the function is called as is.

Parameters:

func (callable) – The function to wrap as a Partial atom if any or input atom is a passed. It must handle the argument as a placeholder for the input data and for the passing states. It must return a unique output except if the function is stateful. In that case, the function must return the processed data as first output and the updated state as additional outputs.

Returns:

output or atom – if no or Atom object is passed as a positional argument, returns the output of the function. If an Atom object is passed as a positional argument, returns a Sequential object containing the Atom object and the atomized function. If is passed as a positional argument, returns a Partial object containing the atomized function. This latter has the same documentation and names than the original function.

Return type:

Any or (Partial or Sequential)

Examples

>>> import numpy as np
>>> from xdas.atoms import atomized

Basic usage:

>>> @atomized
... def square(x):
...     return x ** 2
>>> square(2)
4
>>> square(...)
square(...)

Passing an Atom object as input:

>>> square(square(...))
Sequence:
  0: square(...)
  1: square(...)

Passing a stateful function:

>>> @atomized
... def cumsum(x, cum=None):
...     return_state = cum is not None
...     if cum is None or cum is ...:
...         cum = 0.0
...     out = np.cumsum(x) + cum
...     cum += out[-1]
...     if return_state:
...         return out, cum
...     else:
...         return out
>>> cumsum(..., cum=...)
cumsum(...)  [stateful]