Is there a filter similar to ndimage
's generic_filter that supports vector output? I did not manage to make scipy.ndimage.filters.generic_filter
return more than a scalar. Uncomment the line in the code below to get the error: TypeError: only length-1 arrays can be converted to Python scalars
.
I'm looking for a generic filter that process 2D or 3D arrays and returns a vector at each point. Thus the output would have one added dimension. For the example below I'd expect something like this:
m.shape # (10,10)
res.shape # (10,10,2)
Example Code
import numpy as np
from scipy import ndimage
a = np.ones((10, 10)) * np.arange(10)
footprint = np.array([[1,1,1],
[1,0,1],
[1,1,1]])
def myfunc(x):
r = sum(x)
#r = np.array([1,1]) # uncomment this
return r
res = ndimage.generic_filter(a, myfunc, footprint=footprint)
The
generic_filter
expectsmyfunc
to return a scalar, never a vector. However, there is nothing that precludesmyfunc
from also adding information to, say, a list which is passed tomyfunc
as an extra argument.Instead of using the array returned by
generic_filter
, we can generate our vector-valued array by reshaping this list.For example,
I think I get what you're asking, but I'm not completely sure how does the
ndimage.generic_filter
work (how abstruse is the source!).Here's just a simple wrapper function. This function will take in an array, all the parameters
ndimage.generic_filter
needs. Function returns an array where each element of the former array is now represented by an array with shape (2,), result of the function is stored as the second element of that array.Output, where
res
denotes just thegeneric_filter
and res2 denotesgeneric_expand_filter
, of this function is:Of course you probably don't want to save the old array, but instead have both fields as new results. Except I don't know what exactly you had in mind, if the two values you want stored are unrelated, just add a
temp2
andfunc2
and call anothergeneric_filter
with the same**kwargs
and store that as the first value.However if you want an actual vector quantity that is calculated using multiple
inarr
elements, meaning that the two new created fields aren't independent, you are just going to have to write that kind of a function, one that takes in an array,idx
,idy
indices and returns a tuple\list\array value which you can then unpack and assign to the result.