How to compare an array of arrays elementwise to None in

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to compare an array of arrays elementwise to None in

Martin.Gfeller
Thank you for your help!

Sebastian, I couldn't agree more with someone's bug being someone else's feature! A fast identity ufunc would be useful, though.

Actually, numpy.frompyfunc(operator.is_,2,1) is much faster than the numpy.vectorize approach - only about 35% slower on quick measurement
than the direct ==, as opposed to 62% slower with vectorize (with otypes hint).

Robert, yes, that's what I already did provisionally.

Eric, that is a nice puzzle - but I agree with Robert about understanding by code maintainers.

Thanks again, and best regards,
Martin




On Mon, 17 Jul 2017 11:41 Sebastian Berg <[hidden email]> write

> Yes, I guess ones bug is someone elses feature :(, if it is very bad, we could delay the deprecation probably. For a solutions, maybe
> we could add a ufunc  for elementwise `is` on object arrays (dunno about the name, maybe `object_identity`.

> Just some quick thoughts.

> - Sebastian

On Mon, 17 Jul 2017 at 17:45 Robert Kern <[hidden email]> wrote:

> Wrap the clumsiness up in a documented, tested utility function with a descriptive name and use that function everywhere instead.
> Robert Kern

On Mon, Jul 17, 2017 at 10:52 AM, Eric Wieser <[hidden email]>
wrote:

> Here's a hack that lets you keep using ==:
>
> class IsCompare:
>     __array_priority__ = 999999  # needed to make it work on either side of `==`
>     def __init__(self, val): self._val = val
>     def __eq__(self, other): return other is self._val
>     def __neq__(self, other): return other is not self._val
>
> a == IsCompare(None)  # a is None
> a == np.array(IsCompare(None)) # broadcasted a is None
>
>


_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to compare an array of arrays elementwise to None in

Sebastian Berg
On Wed, 2017-07-19 at 08:31 +0000, [hidden email] wrote:
> Thank you for your help!
>
> Sebastian, I couldn't agree more with someone's bug being someone
> else's feature! A fast identity ufunc would be useful, though. 
>

An `object_identity` ufunc should be very easy to implement, the bigger
work is likely to actually decide on it and the name. Also should
probably check back with the PyPy guys to make sure it would also work
on PyPy.

- Sebastian


> Actually, numpy.frompyfunc(operator.is_,2,1) is much faster than the
> numpy.vectorize approach - only about 35% slower on quick
> measurement 
> than the direct ==, as opposed to 62% slower with vectorize (with
> otypes hint). 
>
> Robert, yes, that's what I already did provisionally. 
>
> Eric, that is a nice puzzle - but I agree with Robert about
> understanding by code maintainers. 
>
> Thanks again, and best regards,
> Martin
>
>
>
>
> On Mon, 17 Jul 2017 11:41 Sebastian Berg <[hidden email]>
> write
>
> > Yes, I guess ones bug is someone elses feature :(, if it is very
> > bad, we could delay the deprecation probably. For a solutions,
> > maybe 
> > we could add a ufunc  for elementwise `is` on object arrays (dunno
> > about the name, maybe `object_identity`.
> > Just some quick thoughts.
> > - Sebastian
>
> On Mon, 17 Jul 2017 at 17:45 Robert Kern <[hidden email]>
> wrote:
>
> > Wrap the clumsiness up in a documented, tested utility function
> > with a descriptive name and use that function everywhere instead.
> > Robert Kern
>
> On Mon, Jul 17, 2017 at 10:52 AM, Eric Wieser <wieser.eric+numpy@gmai
> l.com>
> wrote:
>
> > Here's a hack that lets you keep using ==:
> >
> > class IsCompare:
> >     __array_priority__ = 999999  # needed to make it work on either
> > side of `==`
> >     def __init__(self, val): self._val = val
> >     def __eq__(self, other): return other is self._val
> >     def __neq__(self, other): return other is not self._val
> >
> > a == IsCompare(None)  # a is None
> > a == np.array(IsCompare(None)) # broadcasted a is None
> >
> >
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/numpy-discussion

signature.asc (817 bytes) Download Attachment
Loading...