[Numpy-discussion] Would it be sensible to augment `casting` policies with a `clip` policy?

2023-03-16 Thread Sergio Callegari
I think it is a not so infrequent requirement to do castings from float types 
to integer types of from float types to lower precision float types where it is 
OK to have a loss of precision, but you want to be sure not to get values that 
are completely unrelated to the input ones because of wrapping effects.

A typical application can be in sound/image processing, where you do filtering 
with floating point, but then you want to go back to integer types for the 
final result and if you have values too large for the integer type, then you 
want the values to saturate to the maximum ones and not certainly a positive 
value to become negative because of wrapping.  Missing the clipping step in 
some cases can even be dangerous (e.g. resulting in sound signals that once 
played through a headset can get so loud to damage the hear).

As of today, I think that this conversion can only be done with 
`.clip().astype(...)`. However, this is a bit inconvenient: on one hand, 
for the clip step you need to have the minimum and maximum values, which could 
be automatically determined from the type you want (but clip knows nothing 
about it yet). Secondly, this method concatenation creates temporary copies 
that would be unneeded if the operation could be done in just one step.

Ideally, I'd like to be able to write `a.astype(np.int16, casting=`clip`).
___
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] `dtype` parameter in `clip` method

2023-03-16 Thread Sergio Callegari
I am trying to use the `clip` method to transform floats to integers with 
clipping. I was expecting to be able to do both the clipping and the type 
conversion at once, passing the `dtype` parameter to `clip`, together with a 
suitable `casting` param. Such expectation came from reading the documentation 
of the `dtype` param for u-funcs that states that this parameter "Overrides the 
DType of the output arrays". Unfortunately, using the `dtype` parameter with 
`clip` causes the type conversion to be practiced before the clipping, not 
after it.  Is this how it is expected to operate?
___
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