apply_on_boundary

odl.util.numerics.apply_on_boundary(array, func, only_once=True, which_boundaries=None, axis_order=None, out=None)[source]

Apply a function of the boundary of an n-dimensional array.

All other values are preserved as-is.

Parameters
arrayarray-like

Modify the boundary of this array

funccallable or sequence of callables

If a single function is given, assign array[slice] = func(array[slice]) on the boundary slices, e.g. use lamda x: x / 2 to divide values by 2. A sequence of functions is applied per axis separately. It must have length array.ndim and may consist of one function or a 2-tuple of functions per axis. None entries in a sequence cause the axis (side) to be skipped.

only_oncebool, optional

If True, ensure that each boundary point appears in exactly one slice. If func is a list of functions, the axis_order determines which functions are applied to nodes which appear in multiple slices, according to the principle “first-come, first-served”.

which_boundariessequence, optional

If provided, this sequence determines per axis whether to apply the function at the boundaries in each axis. The entry in each axis may consist in a single bool or a 2-tuple of bool. In the latter case, the first tuple entry decides for the left, the second for the right boundary. The length of the sequence must be array.ndim. None is interpreted as “all boundaries”.

axis_ordersequence of ints, optional

Permutation of range(array.ndim) defining the order in which to process the axes. If combined with only_once and a function list, this determines which function is evaluated in the points that are potentially processed multiple times.

outnumpy.ndarray, optional

Location in which to store the result, can be the same as array. Default: copy of array

Examples

>>> arr = np.ones((3, 3))
>>> apply_on_boundary(arr, lambda x: x / 2)
array([[ 0.5,  0.5,  0.5],
       [ 0.5,  1. ,  0.5],
       [ 0.5,  0.5,  0.5]])

If called with only_once=False, the function is applied repeatedly:

>>> apply_on_boundary(arr, lambda x: x / 2, only_once=False)
array([[ 0.25,  0.5 ,  0.25],
       [ 0.5 ,  1.  ,  0.5 ],
       [ 0.25,  0.5 ,  0.25]])
>>> apply_on_boundary(arr, lambda x: x / 2, only_once=True,
...                   which_boundaries=((True, False), True))
array([[ 0.5,  0.5,  0.5],
       [ 0.5,  1. ,  0.5],
       [ 0.5,  1. ,  0.5]])

Use the out parameter to store the result in an existing array:

>>> out = np.empty_like(arr)
>>> result = apply_on_boundary(arr, lambda x: x / 2, out=out)
>>> result
array([[ 0.5,  0.5,  0.5],
       [ 0.5,  1. ,  0.5],
       [ 0.5,  0.5,  0.5]])
>>> result is out
True