ndarray.clip only with lower or upper values?
Hi again,
I noticed that clip() needs two parameters, but wouldn't it be nice and straightforward to just pass min= or max= as keyword arg?
In [2]: a = arange(10)
In [3]: a.clip(min = 2, max = 5) Out[3]: array([2, 2, 2, 3, 4, 5, 5, 5, 5, 5])
In [4]: a.clip(min = 2)  exceptions.TypeError Traceback (most recent call last)
/home/meine/<ipython console>
TypeError: function takes at least 2 arguments (1 given)
(I could simulate that by passing max = maximum_value_of(a.dtype), if that existed, see my other mail.)
Ciao, / / // / / ANS
On Dec 10, 2007 7:21 AM, Hans Meine meine@informatik.unihamburg.de wrote:
Hi again,
I noticed that clip() needs two parameters, but wouldn't it be nice and straightforward to just pass min= or max= as keyword arg?
In [2]: a = arange(10)
In [3]: a.clip(min = 2, max = 5) Out[3]: array([2, 2, 2, 3, 4, 5, 5, 5, 5, 5])
In [4]: a.clip(min = 2)
exceptions.TypeError Traceback (most recent call last)
/home/meine/<ipython console>
TypeError: function takes at least 2 arguments (1 given)
(I could simulate that by passing max = maximum_value_of(a.dtype), if that existed, see my other mail.)
Why not just use minimum or maximum as needed instead of overloading clip?
Am Montag, 10. Dezember 2007 23:46:17 schrieb Timothy Hochberg:
TypeError: function takes at least 2 arguments (1 given)
(I could simulate that by passing max = maximum_value_of(a.dtype), if that existed, see my other mail.)
Why not just use minimum or maximum as needed instead of overloading clip?
You mean one of the following? a.clip(min = 10, max = numpy.finfo(a.dtype).max) a.clip(min = 10, max = numpy.iinfo(a.dtype).max)
Three reasons:  this is not very concise  it is less efficient than specialized clip variants (not much / too important though)  I would have to discriminate integral and floating point types
How is the latter done in numpy? Is there a good reason not to have numpy.rangetraits(sometype) with min/max as in iinfo/finfo? Should I use isinstance(mytype, int)?
On Dec 11, 2007 2:32 AM, Hans Meine meine@informatik.unihamburg.de wrote:
Am Montag, 10. Dezember 2007 23:46:17 schrieb Timothy Hochberg:
TypeError: function takes at least 2 arguments (1 given)
(I could simulate that by passing max = maximum_value_of(a.dtype), if that existed, see my other mail.)
Why not just use minimum or maximum as needed instead of overloading
clip?
You mean one of the following? a.clip(min = 10, max = numpy.finfo(a.dtype).max) a.clip(min = 10, max = numpy.iinfo(a.dtype).max)
No. I mean:
numpy.maximum(a, 10)
To correspond to the above example.
Three reasons:
 this is not very concise
 it is less efficient than specialized clip variants
(not much / too important though)
 I would have to discriminate integral and floating point types
How is the latter done in numpy? Is there a good reason not to have numpy.rangetraits(sometype) with min/max as in iinfo/finfo? Should I use isinstance(mytype, int)?
 Ciao, / / // / / ANS _______________________________________________ Numpydiscussion mailing list Numpydiscussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpydiscussion
On Dienstag 11 Dezember 2007, Timothy Hochberg wrote:
You mean one of the following? a.clip(min = 10, max = numpy.finfo(a.dtype).max) a.clip(min = 10, max = numpy.iinfo(a.dtype).max)
No. I mean:
numpy.maximum(a, 10)
To correspond to the above example.
Great, thanks for the hints. That's good enough; could be pythonic to let clip() forward calls to minimum/maximum if only one bound is given though. I had a look at the code, but I am not used enough to the Python/C API, let alone to numpy's internals, to *quickly* hack this.
Ciao, / / .o. // ..o / / ANS ooo
Hans Meine wrote:
On Dienstag 11 Dezember 2007, Timothy Hochberg wrote:
You mean one of the following? a.clip(min = 10, max = numpy.finfo(a.dtype).max) a.clip(min = 10, max = numpy.iinfo(a.dtype).max)
No. I mean:
numpy.maximum(a, 10)
To correspond to the above example.
Great, thanks for the hints. That's good enough; could be pythonic to let clip() forward calls to minimum/maximum if only one bound is given though. I had a look at the code, but I am not used enough to the Python/C API, let alone to numpy's internals, to *quickly* hack this.
It is done, now in SVN.
Travis
participants (3)

Hans Meine

Timothy Hochberg

Travis E. Oliphant