[Numpy-discussion] numpy.fix and subclasses

Darren Dale dsdale24@gmail....
Sun Feb 22 21:49:33 CST 2009


On Sun, Feb 22, 2009 at 10:35 PM, Darren Dale <dsdale24@gmail.com> wrote:

> I've been finding some numpy functions that could maybe be improved to work
> better with ndarray subclasses. For example:
>
> def fix(x, y=None):
>     x = nx.asanyarray(x)
>     if y is None:
>         y = nx.zeros_like(x)
>     y1 = nx.floor(x)
>     y2 = nx.ceil(x)
>     y[...] = nx.where(x >= 0, y1, y2)
>     return y
>
> This implementation is a problematic for subclasses, since it does not
> allow metadata to propagate using the usual ufunc machinery of
> __array_wrap__, like ceil and floor do. nx.zeros_like does yield another
> instance of type(x), but y does not get x's metadata (such as units or a
> mask). Would it be possible to do something like:
>
>     if y is None:
>         y = x*0
>
> "where" is another function that could maybe be improved to work with the
> rules established by array_priority, but I'm a lousy C programmer and I
> haven't actually looked into how this would work. If "where" respected
> array_priority, fix could be implemented as:
>
> def fix(x, y=None):
>     x = nx.asanyarray(x)
>     y1 = nx.floor(x)
>     y2 = nx.ceil(x)
>     if y is None:
>         return nx.where(x >= 0, y1, y2)
>     y[...] = nx.where(x >= 0, y1, y2)
>     return y


Actually, I just remembered that quantities tries to prevent things like
([1,2,3,4]*m)[:2] = [0,1], since the units dont match, so setting y=x*0 and
then setting data to a slice of y would be problematic. It would be most
desirable for "where" to respect __array_priority__, if possible. Any
comments?

Darren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20090222/585617d8/attachment-0001.html 


More information about the Numpy-discussion mailing list