idxDecorator

utilipy.data_utils.idxDecorator(function: Optional[Callable] = None, *, as_ind: Union[bool, Literal[flatten]] = False, _doc_fmt: Optional[dict] = None, _doc_style: Optional[Union[str, Callable]] = None) → Callable[source]

Control whether to return boolean array or indices.

for functions which return bool arrays adds as_ind as a kwarg to decorated function

Parameters
functionCallable, optional

(default None) the function to be decoratored if None, then returns decorator to apply.

as_indbool or “flatten”, optional

(default False) whether to return bool array or indices (where(bool array == np.True_)) if “flatten”, flattens a nested list with only 1 element ie ([0], ) -> [0] sets the default behavior for the wrapped function

Returns
wrapperCallable

wrapper for function includes the original function in a method .__wrapped__

Other Parameters
_doc_fmtdict, optional

docstring formatter argument into wraps()

_doc_stylestr or Callable, optional

docstring style argument into wraps()

Notes

Adds as_ind and other parameters to the function signature and docstring.

Examples

Use the Standard Decorator:

>>> x = np.array([0, 2])
>>> @idxDecorator
... def func1(x):
...     return x < 1
calling normally
>>> func1(x) 
array([ True, False], dtype=bool))
using added kwarg
>>> func1(x, as_ind=True)
(array([0]),)
and flattening
>>> func1(x, as_ind="flatten")
array([0])

Set a Different Default:

>>> @idxDecorator(as_ind=True)
... def func2(x):
...     return x < 1
>>> func2(x)
(array([0]),)
>>> func2(x, as_ind=False) 
array([ True, False])

Making a New Decorator:

>>> trueidxdec = idxDecorator(as_ind="flatten")
>>> @trueidxdec
... def func3(x):
...     return x < 1
>>> func3(x)
array([0])
>>> func3(x, as_ind=False) 
array([ True, False])

Wrapping Existing Functions

>>> def func(x):
...     return x < 1
>>> newfunc = idxDecorator(func, as_ind=True)
>>> newfunc(x)
(array([0]),)
>>> newfunc(x, as_ind=False) 
array([ True, False])