[Numpy-discussion] PR 7918 apply_along_axis()

Ben Rowland bennyrowland at mac.com
Mon Aug 8 11:35:25 EDT 2016

Hi list,

This is both my first post to this list and first pull request for numpy so apologies if this is not the right list or for any other mistakes.

Here is the link to my pull request: https://github.com/numpy/numpy/pull/7918 <https://github.com/numpy/numpy/pull/7918>

It is a simple modification to the numpy.apply_along_axis() function in numpy/lib/shape_base.py to allow it to deal better with ndarray subclasses. My use case is this: I have an ndarray subclass storing sets of temporal data which has additional metadata properties such as the time between data points. I frequently want to apply the same function to each time course in a set and apply_along_axis() is a compact and efficient way to do this. However there are two behaviours that I require which are not provided by the current numpy master:
1) apply_along_axis returns the same subclass as it was called upon, so that I don’t lose all the metadata that it went in with.
2) fund1d calls inside apply_along_axis should receive 1d slices of the same subclass as the supplied whole array, so that they can make use of the metadata in performing their function.

To achieve these two behaviours requires modifying only three lines of code:
1) At the start of the function, the input is converted to a bare ndarray by the function numpy.asarray(), I replace this with the subclass friendly numpy.asanyarray()
2) Depending on whether func1d returns a scalar or a vector there are two different code paths constructing and returning the output array. In each case I call __array_wrap__ on the input array, passing the output array to allow it to be updated with all the metadata of the original array.

I have also implemented two new tests for this functionality. The first is very simple and merely calls apply_along_axis on a numpy.matrix class and checks that the result is also a numpy.matrix. The second is slightly more involved as it requires defining a MinimalSubclass class which adds a data member to the bare ndarray, then a function which returns that member. apply_along_axis() is then called passing in an instance of the subclass and the function to show that the slices passed to func1d preserve the data member.

I would welcome any comment or constructive criticism.

Ben Rowland
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160808/f3ced424/attachment.html>

More information about the NumPy-Discussion mailing list