2018-10-03 18:59:01 +00:00
|
|
|
# https://github.com/iskandr/parakeet/blob/master/benchmarks/nd_local_maxima.py
|
|
|
|
# setup: import numpy as np ; shape = (3,2,3,2) ; x = np.arange(36, dtype=np.float64).reshape(*shape) # noqa
|
|
|
|
# run: local_maxima(x)
|
2018-04-05 22:07:33 +00:00
|
|
|
|
2018-10-03 18:59:01 +00:00
|
|
|
# pythran export local_maxima(float [][][][])
|
2018-04-05 22:07:33 +00:00
|
|
|
import numpy as np
|
|
|
|
|
2018-10-03 12:38:48 +00:00
|
|
|
|
2018-04-05 22:07:33 +00:00
|
|
|
def wrap(pos, offset, bound):
|
2018-10-03 12:38:48 +00:00
|
|
|
return (pos + offset) % bound
|
|
|
|
|
2018-04-05 22:07:33 +00:00
|
|
|
|
|
|
|
def clamp(pos, offset, bound):
|
2018-10-03 18:59:01 +00:00
|
|
|
return min(bound - 1, max(0, pos + offset))
|
2018-10-03 12:38:48 +00:00
|
|
|
|
2018-04-05 22:07:33 +00:00
|
|
|
|
|
|
|
def reflect(pos, offset, bound):
|
2018-10-03 18:59:01 +00:00
|
|
|
idx = pos + offset
|
|
|
|
return min(2 * (bound - 1) - idx, max(idx, -idx))
|
2018-04-05 22:07:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def local_maxima(data, mode=wrap):
|
2018-10-03 12:38:48 +00:00
|
|
|
wsize = data.shape
|
|
|
|
result = np.ones(data.shape, bool)
|
|
|
|
for pos in np.ndindex(data.shape):
|
|
|
|
myval = data[pos]
|
|
|
|
for offset in np.ndindex(wsize):
|
2020-06-28 18:24:40 +00:00
|
|
|
neighbor_idx = tuple(
|
|
|
|
mode(p, o - w // 2, w) for (p, o, w) in zip(pos, offset, wsize)
|
|
|
|
)
|
|
|
|
result[pos] &= data[neighbor_idx] <= myval
|
2018-10-03 12:38:48 +00:00
|
|
|
return result
|