numpy.fix and subclasses

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 Darren

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

On Sun, Feb 22, 2009 at 11:49 PM, Darren Dale <dsdale24@gmail.com> wrote:
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?
I was wondering if we could consider applying a decorator to functions like fix that do not tie into the ufunc machinery that determines an appropriate __array_wrap__ to call. It would be simple enough to write a decorator that does the same thing as _find_array_wrap in umath_ufunc_object.inc, and if an array_wrap method is identified, apply it to the output of the existing function. This way numpy's functions would be more cooperative with ndarray subclasses. I don't mind writing the decorator and some unit tests, but I don't have a lot of free time so I would like to discuss it first. Does it sound reasonable? Thanks, Darren
participants (1)
-
Darren Dale