xdas.atoms.Partial#

class xdas.atoms.Partial(func: Callable, *args: Any, name: str | None = None, **kwargs: Any)[source]#

Wraps a function into an Atom.

It works similarly to functools.partial but with additional features. If the input is not the first argument, an Ellipsis () can be used to indicate the position of the input. A name argument can be used to better identify the resulting atom.

Some level of state passing can be achieved by passing for one or several keyword arguments. In that case, the function is expected to accept as keyword arguments, to properly initialize the corresponding states and to return as many additional outputs as there are stateful arguments.

Partial uses several reserved keyword arguments that cannot by passed to func: ‘func’, ‘name’ and ‘state’.

Parameters:
  • func (Callable) – The function that is called. One of its argument is used as the input of the resulting Atom object while other parameters are fixed. The position of the input is by default the first argument. Otherwise, an Ellipsis () must be provided in the *args parameters to indicate the position of the input. The function 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.

  • *args (Any) – Positional arguments to pass to func. If the data to process is passed as the nth argument, the nth element of args must contain an Ellipsis ().

  • name (str) – Name to identify the function.

  • **kwargs (Any) – Keyword arguments to pass to func. If one of the keyword arguments is , it will be treated as a passing state and initialized or updated at each call.

Examples

>>> import numpy as np
>>> import scipy.signal as sp
>>> import xdas.signal as xs
>>> from xdas.atoms import Partial

Examples of a stateless atom:

>>> Partial(xs.decimate, 2, dim="time")
decimate(..., 2, dim=time)
>>> Partial(np.square)
square(...)

Examples of a stateful atom with input data as second argument:

>>> sos = sp.iirfilter(4, 0.1, btype="lowpass", output="sos")
>>> Partial(xs.sosfilt, sos, ..., dim="time", zi=...)
sosfilt(<ndarray>, ..., dim=time)  [stateful]
__init__(func: Callable, *args: Any, name: str | None = None, **kwargs: Any) None[source]#

Methods

__init__(func, *args[, name])

call(x, **flags)

Call the wrapped function with x substituted at the ... position.

from_state(state)

Reconstruct a Partial from a serialised state dict.

get_state()

Return a JSON-serialisable dict describing the wrapped function and args.

initialize(x, **flags)

Initialise the atom from a first chunks of data.

initialize_from_state()

Initialise the atom from its current state.

load_state(path)

Load the atom state from the NetCDF4 file at path.

reset()

Reset all state entries to ... (uninitialised sentinel).

save_state(path)

Serialise the current state to a NetCDF4 file at path.

set_state(state)

Restore the atom state from a previously saved state dict.

Attributes

initialized

True if every state key has been initialised (no ... sentinels remain).

state

Dict of the current state, including nested atom states.

stateful

True if any keyword argument is being passed as state.