[Numpy-discussion] Re: Automatic Clipping of array to upper / lower bounds of dtype

2024-03-10 Thread Ralf Gommers
On Sat, Mar 9, 2024 at 11:23 PM Dom Grigonis  wrote:

> Hello,
>
> Can't find answer to this anywhere.
>
> What I would like is to automatically clip the values if they breach the
> bounds.
>
> I have done a simple clipping, and overwritten __iadd__, __isub__,
> __setitem__, …
>
> But I am wandering if there is a specified way to do this. Or maybe at
> least a centralised place exists to do such thing? E.g. Only 1 method to
> override?
>

That centralized method is `__array_wrap__`; a subclass that implements
`__array_wrap__` by applying `np.clip` and then returning self should do
this I think.

Cheers,
Ralf
___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com


[Numpy-discussion] Re: Automatic Clipping of array to upper / lower bounds of dtype

2024-03-10 Thread Dom Grigonis
Much thanks!

Another related question while I am at it. It says clip is supposed to be 
faster than np.maximum(mp.minumum(arr, max), min). However:
a = np.arange(100)
%timeit a.clip(4, 20)# 8.48 µs
%timeit np.maximum(np.minimum(a, 20), 4)# 2.09 µs
Is this expected?

Regards,
dg


> On 10 Mar 2024, at 09:59, Ralf Gommers  wrote:
> 
> 
> 
> On Sat, Mar 9, 2024 at 11:23 PM Dom Grigonis  > wrote:
> Hello,
> 
> Can't find answer to this anywhere.
> 
> What I would like is to automatically clip the values if they breach the 
> bounds.
> 
> I have done a simple clipping, and overwritten __iadd__, __isub__, 
> __setitem__, …
> 
> But I am wandering if there is a specified way to do this. Or maybe at least 
> a centralised place exists to do such thing? E.g. Only 1 method to override?
> 
> That centralized method is `__array_wrap__`; a subclass that implements 
> `__array_wrap__` by applying `np.clip` and then returning self should do this 
> I think.
> 
> Cheers,
> Ralf
> ___
> NumPy-Discussion mailing list -- numpy-discussion@python.org
> To unsubscribe send an email to numpy-discussion-le...@python.org
> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
> Member address: dom.grigo...@gmail.com

___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com


[Numpy-discussion] Re: Automatic Clipping of array to upper / lower bounds of dtype

2024-03-10 Thread Ralf Gommers
On Sun, Mar 10, 2024 at 9:14 AM Dom Grigonis  wrote:

> Much thanks!
>
> Another related question while I am at it. It says clip is supposed to be
> faster than np.maximum(mp.minumum(arr, max), min). However:
>
> a = np.arange(100)%timeit a.clip(4, 20)# 8.48 µs%timeit 
> np.maximum(np.minimum(a, 20), 4)# 2.09 µs
>
> Is this expected?
>

Make sure that you're not benchmarking with very small arrays (2 us is on
the order of function call overhead) and that the timing are reproducible.
`clip` is more efficient:

>>> %timeit np.clip(a, 4, 20)
70 µs ± 304 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
>>> %timeit np.clip(a, 4, 20)
72.8 µs ± 161 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
>>> %timeit np.maximum(np.minimum(a, 20), 4)
742 µs ± 8.45 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

Ralf



>
> Regards,
> dg
>
>
> On 10 Mar 2024, at 09:59, Ralf Gommers  wrote:
>
>
>
> On Sat, Mar 9, 2024 at 11:23 PM Dom Grigonis 
> wrote:
>
>> Hello,
>>
>> Can't find answer to this anywhere.
>>
>> What I would like is to automatically clip the values if they breach the
>> bounds.
>>
>> I have done a simple clipping, and overwritten __iadd__, __isub__,
>> __setitem__, …
>>
>> But I am wandering if there is a specified way to do this. Or maybe at
>> least a centralised place exists to do such thing? E.g. Only 1 method to
>> override?
>>
>
> That centralized method is `__array_wrap__`; a subclass that implements
> `__array_wrap__` by applying `np.clip` and then returning self should do
> this I think.
>
> Cheers,
> Ralf
> ___
> NumPy-Discussion mailing list -- numpy-discussion@python.org
> To unsubscribe send an email to numpy-discussion-le...@python.org
> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
> Member address: dom.grigo...@gmail.com
>
>
> ___
> NumPy-Discussion mailing list -- numpy-discussion@python.org
> To unsubscribe send an email to numpy-discussion-le...@python.org
> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
> Member address: ralf.gomm...@googlemail.com
>
___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com


[Numpy-discussion] Re: Automatic Clipping of array to upper / lower bounds of dtype

2024-03-10 Thread Dom Grigonis
Thanks,

True, clip does get faster, but threshold is around 10k on my PC.

Also, can’t get __array_wrap__ to work. The arguments it receives after 
__iadd__ are all post-operation. Decided not to do it this way this time so not 
to hardcode such functionality into the class, but if there is a way to 
robustly achieve this it would be good to know.

Regards,
dg

> On 10 Mar 2024, at 18:43, Ralf Gommers  wrote:
> 
> 
> 
> On Sun, Mar 10, 2024 at 9:14 AM Dom Grigonis  > wrote:
> Much thanks!
> 
> Another related question while I am at it. It says clip is supposed to be 
> faster than np.maximum(mp.minumum(arr, max), min). However:
> a = np.arange(100)
> %timeit a.clip(4, 20)# 8.48 µs
> %timeit np.maximum(np.minimum(a, 20), 4)# 2.09 µs
> Is this expected?
> 
> Make sure that you're not benchmarking with very small arrays (2 us is on the 
> order of function call overhead) and that the timing are reproducible. `clip` 
> is more efficient:
> 
> >>> %timeit np.clip(a, 4, 20)
> 70 µs ± 304 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
> >>> %timeit np.clip(a, 4, 20)
> 72.8 µs ± 161 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
> >>> %timeit np.maximum(np.minimum(a, 20), 4)
> 742 µs ± 8.45 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
> 
> Ralf
> 
>  
> 
> Regards,
> dg
> 
> 
>> On 10 Mar 2024, at 09:59, Ralf Gommers > > wrote:
>> 
>> 
>> 
>> On Sat, Mar 9, 2024 at 11:23 PM Dom Grigonis > > wrote:
>> Hello,
>> 
>> Can't find answer to this anywhere.
>> 
>> What I would like is to automatically clip the values if they breach the 
>> bounds.
>> 
>> I have done a simple clipping, and overwritten __iadd__, __isub__, 
>> __setitem__, …
>> 
>> But I am wandering if there is a specified way to do this. Or maybe at least 
>> a centralised place exists to do such thing? E.g. Only 1 method to override?
>> 
>> That centralized method is `__array_wrap__`; a subclass that implements 
>> `__array_wrap__` by applying `np.clip` and then returning self should do 
>> this I think.
>> 
>> Cheers,
>> Ralf
>> ___
>> NumPy-Discussion mailing list -- numpy-discussion@python.org 
>> 
>> To unsubscribe send an email to numpy-discussion-le...@python.org 
>> 
>> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ 
>> 
>> Member address: dom.grigo...@gmail.com 
> 
> ___
> NumPy-Discussion mailing list -- numpy-discussion@python.org 
> 
> To unsubscribe send an email to numpy-discussion-le...@python.org 
> 
> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ 
> 
> Member address: ralf.gomm...@googlemail.com 
> 
> ___
> NumPy-Discussion mailing list -- numpy-discussion@python.org 
> 
> To unsubscribe send an email to numpy-discussion-le...@python.org 
> 
> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ 
> 
> Member address: dom.grigo...@gmail.com 
___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com


[Numpy-discussion] Re: Automatic Clipping of array to upper / lower bounds of dtype

2024-03-10 Thread Marten van Kerkwijk
> Also, can’t get __array_wrap__ to work. The arguments it receives after 
> __iadd__ are all
> post-operation. Decided not to do it this way this time so not to hardcode 
> such functionality
> into the class, but if there is a way to robustly achieve this it would be 
> good to know.

It is non-trivial to clip in the operation, since internally the code
just wraps like C does (actually, I think for some C implementations it
clips...). I think you'd be stuck with evaluating with more bits and
then clipping before casting back (probably easiest by definining your
own __array_ufunc__) -- or writing your own inner loops.

-- Marten

___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com


[Numpy-discussion] Re: next Documentation team meeting

2024-03-10 Thread Mukulika Pahari
Hi all,

Our next Documentation Team meeting will happen on *Monday, March 11* at *11PM 
UTC*. If this time slot is inconvenient for you to join, please let me know in 
the replies or Slack and we will work something out.

All are welcome - you don't need to already be a contributor to join. If you 
have questions or are curious about what we're doing, we'll be happy to meet 
you!

If you wish to join on Zoom, use this (updated) link:
https://numfocus-org.zoom.us/j/85016474448?pwd=TWEvaWJ1SklyVEpwNXUrcHV1YmFJQ...

Here's the permanent hackmd document with the meeting notes (still being
updated):
https://hackmd.io/oB_boakvRqKR-_2jRV-Qjg

Hope to see you around!

Best wishes,
Mukulika
___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com