Evolution of the generic_filter function

Hi, first, like it is my first post here, I would like to congratulate you for your great job... thank you very much... Now, my question. It seems that the function "scipy.ndimage.generic_filter" can only operate onto scalar objets like float or int. Moreover, it returns only float objects. Do you think that, in the near future, this function will be able to treat generic objects like tuples or user classes ? In that case, we will be able to treat directly, for example, vector fields or color images. We will also be able to apply a function onto multiple arrays at the same time, with the same windowing. Then, to my mind, this method will become very powerful for image processing tools. If this evolution is not scheduled, how I can substitute this method ? For example, I would like to call a function fct(win_a, win_b) with "win_a" a window extracted from an array "a" and "win_b" the same window extracted from "b". Example : # The original fields : two 2D vector fields with 2 components for each vector field_size = (5,5) a = numpy.random.random(field_size + (2,)) b = numpy.random.random(field_size + (2,)) # A simple function : the result is the sum of the element-wise product def fct(win1,win2): return (win1 * win2).sum() # And now, the new generic_filter() which operates simultaneously onto sub-windows of multiple arrays # "c" is the result field : a 2D scalar field with the same size "field_size" c = scipy.ndimage.generic_filter( {"win1":a, "win2":b}, fct, size=(3,3), ...) Thank you. Martin

Now, my question. It seems that the function "scipy.ndimage.generic_filter" can only operate onto scalar objets like float or int. Moreover, it returns only float objects. Do you think that, in the near future, this function will be able to treat generic objects like tuples or user classes ? In that case, we will be able to treat directly, for example, vector fields or color images. We will also be able to apply a function onto multiple arrays at the same time, with the same windowing. Then, to my mind, this method will become very powerful for image processing tools.
This change doesn't appear to be on anyone's radar yet... though I'm sure patches might be accepted! (But beware: ndimage is a pretty tangled codebase on the C side of things, which is why lots of improvements like this haven't been made.) However, note that the generic_filter function is a performance disaster: it applies a *python* function to every element in an array... very slow. (As would be a vectorized replacement which took a "vector_axis" keyword, or one which worked on structured arrays.) You might want to think about implementing in cython a replacement "generic_filter" (with vector arguments, of course), that could apply cdef'd functions (all within cython), if performance matters. There's some new mechanisms in the numpy C api for neighborhood iterators that would be useful for filtering like this, too. (But with limited options for boundary conditions.) Alternately, here's a sketch of a technique I have used for "in- parallel" filtering in pure python. Note that there are no boundary conditions handled here, but you could deal with that: a = numpy.random((100, 100, 2)) center = a[1:-1, 1:-1] left = a[:-2, 1:-1] right = a[2:, 1:-1] up = a[1:-1, :-2] down = a[1:-1, 2:] boxfiltered_a = (center + left + right + up + down) / 5. (obviously you could do this more elegantly / generically, but this is the idea...) Or, perhaps you could figure out how to decompose the vectorized filtering operations you need into filtering on the individual components, then combining the components in parallel? Zach
participants (2)
-
Martin DRUON
-
Zachary Pincus