
Dear Thomas, I think you might be able to do this with https://numpy.org/doc/stable/reference/generated/numpy.lib.stride_tricks.sli... Kind regards Klaus On Fri, Jul 26, 2024 at 2:51 PM <langthom@forwiss.uni-passau.de> wrote:
Dear all,
my goal would be to apply some function on a local environment of size K x K x K where K is bigger than 1 and odd. For example, if K = 3, it would be nice to apply some function "func" on such an environment around each n-dimensional position within the n-dimensional array. So, for K = 3 and the position (1,1,1) if a 3D array, one would collect the array values at the positions X = [(0,0,0),(0,0,1),(0,0,2),(0,1,0),...(2,2,2)] (K**3 = 27 in total in this example) and yield those values to "func". The result value at position (1,1,1) in the output array would be y = func(X). The same would apply for all entries excluding the padding area (or according to some padding policy).
While I coded this many times on plain buffers in C++, I was wondering if there would be an *efficient* way to do this in numpy? Up to now I relied on the ndenumerate way of iterating n-dimensional arrays and aggregating the values using slices, but this turns out to be unbearably slow even for quite small arrays :-/ Is there a better "numpy-isque" way to do this? I thought of writing a custom view or subclassing, but the efficient aggregation of local environments using the ndenumerate and slice approach is slow, yet in C/C++ random access and native support for parallelism (by means of OpenMP) would drastically accelerate this.
Or would it even be preferred to add this functionality to the library? I could imagine a special view, e.g., "LocalEnvironmentArrayView" or just a simple function with the intended usage something like the following:
* a = ... # some n-dimensional numpy array * func = lambda env: np.mean(env) - np.std(env) * w = numpy.apply_on_local_environments(a, func, environment_size=3)
So, each entry in the interior (or also the boundary, depending on the padding policy) of w would correspond to the evaluation of func on that local environment.
Best regards, Thomas _______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-leave@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: kzimmermann@quansight.com