[Numpy-discussion] histogram using decending range -- what do the results mean?

Stuart Brorson sdb@cloud9....
Fri Oct 5 19:19:40 CDT 2007


Robert,

Thanks for your answers about histogram's meaning for range=(7, 0)!

>> *  If it truely isn't meaningful, why not catch the case and reject
>> input?  Maybe this is a bug....  ???
>
> Patches are welcome.

OK.  I don't know if you have a patch tracking system, so I'll just
post it here.  If you have a patch tracker, point me to it and I'll
enter the patch there.

----------------------  <patch>  -------------------------
Index: function_base.py
===================================================================
--- function_base.py    (revision 4155)
+++ function_base.py    (working copy)
@@ -108,6 +108,12 @@

      """
      a = asarray(a).ravel()
+
+    if (range is not None):
+        mn, mx = range
+        if (mn > mx):
+            raise AttributeError, 'max must be larger than min in range parameter.'
+
      if not iterable(bins):
          if range is None:
              range = (a.min(), a.max())
@@ -116,6 +122,9 @@
              mn -= 0.5
              mx += 0.5
          bins = linspace(mn, mx, bins, endpoint=False)
+    else:
+        if(any(bins[1:]-bins[:-1] < 0)):
+            raise AttributeError, 'bins must increase monotonically.'

      # best block size probably depends on processor cache size
      block = 65536

-----------------------  </patch>  -------------------------

And here's what it does:

-----------------------  <session log>  ----------------------
/home/sdb> python
Python 2.5 (r25:51908, Feb 19 2007, 04:41:03)
[GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> A = numpy.array([1, 2, 3, 4, 5, 6, 5, 4, 5, 4, 3, 2, 1])
>>>
>>> (x, y) = numpy.histogram(A, range=(0, 7))
>>> x
array([0, 2, 2, 0, 2, 3, 0, 3, 1, 0])
>>>
>>> (x, y) = numpy.histogram(A, range=(7, 0))
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/usr/lib/python2.5/site-packages/numpy/lib/function_base.py", line 115, in histogram
     raise AttributeError, 'max must be larger than min in range parameter.'
AttributeError: max must be larger than min in range parameter.
>>>
>>>
>>> bins = numpy.arange(0, 7)
>>> (x, y) = numpy.histogram(A, bins=bins)
>>>
>>> bins = bins[::-1]
>>> bins
array([6, 5, 4, 3, 2, 1, 0])
>>> (x, y) = numpy.histogram(A, bins=bins)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/usr/lib/python2.5/site-packages/numpy/lib/function_base.py", line 127, in histogram
     raise AttributeError, 'bins must increase monotonically.'
AttributeError: bins must increase monotonically.
>>>
>>>
-------------------------  </session log>  --------------------------

Cheers,

Stuart



More information about the Numpy-discussion mailing list