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
- array
array-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. uselamda x: x / 2
to divide values by 2. A sequence of functions is applied per axis separately. It must have lengtharray.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. Iffunc
is a list of functions, theaxis_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 withonly_once
and a function list, this determines which function is evaluated in the points that are potentially processed multiple times.- out
numpy.ndarray
, optional Location in which to store the result, can be the same as
array
. Default: copy ofarray
- 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