Hi all, I've noticed a change in numpy.histogram2d between (possibly very much) older versions and the current one: The function can no longer handle the situation where bin edges decrease instead of increasing monotonically.
The reason for this seems to be the handling of outliers histogramdd, see the output of minimal example below. If I understand correctly, this is the only place where histogramdd implicitly assumes monotonically increasing bin edges. If so, this could be fixed to work with increasing and decreasing bin edges by taking abs(dedges[i]).min() when calculating the rounding precision. If not, it might be more consistent, and produce a more meaningful error message, if histogram2d asserted that bin edges increase monotonically and otherwise raised an AttributeError as the 1-d histogram() function does in that case (see below) Matthias In [1]: import numpy In [2]: numpy.__version__ Out[2]: '1.5.1' In [3]: ascending=numpy.array([0,1]) In [4]: descending=numpy.array([1,0]) In [5]: numpy.histogram2d([0.5],[0.5],bins=(ascending,ascending)) Out[5]: (array([[ 1.]]), array([ 0., 1.]), array([ 0., 1.])) In [6]: numpy.histogram2d([0.5],[0.5],bins=(descending,descending)) Warning: invalid value encountered in log10 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) /lib/python2.6/site-packages/numpy/lib/twodim_base.pyc in histogram2d(x, y, bins, range, normed, weights) 613 xedges = yedges = asarray(bins, float) 614 bins = [xedges, yedges] --> 615 hist, edges = histogramdd([x,y], bins, range, normed, weights) 616 return hist, edges[0], edges[1] 617 /lib/python2.6/site-packages/numpy/lib/function_base.pyc in histogramdd(sample, bins, range, normed, weights) 312 for i in arange(D): 313 # Rounding precision --> 314 decimal = int(-log10(dedges[i].min())) +6 315 # Find which points are on the rightmost edge. 316 on_edge = where(around(sample[:,i], decimal) == around(edges[i][-1], ValueError: cannot convert float NaN to integer Behavior of the 1-d histogram() In [8]: numpy.histogram([0.5],bins=ascending) Out[8]: (array([1]), array([0, 1])) In [9]: numpy.histogram([0.5],bins=descending) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /lib/python2.6/site-packages/numpy/lib/function_base.pyc in histogram(a, bins, range, normed, weights) 160 if (np.diff(bins) < 0).any(): 161 raise AttributeError( --> 162 'bins must increase monotonically.') 163 164 # Histogram is an integer or a float array depending on the weights. AttributeError: bins must increase monotonically. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion