astrofrog / fast-histogram

:zap: Fast 1D and 2D histogram functions in Python :zap:
BSD 2-Clause "Simplified" License
266 stars 28 forks source link

Incorrect work for 3d np.array #20

Closed olgadoronina closed 6 years ago

olgadoronina commented 6 years ago

I'm going to use your package because it is exactly what I need (I just need to add normalization). I found out that histogram1d() works incorrectly with 3d np.arrays. Here is an example of my test:

from future import division
from fast_histogram import histogram1d
import numpy as np
import matplotlib.pyplot as plt

def pdf_from_array_np(array, bins, range):
pdf, _ = np.histogram(array, bins=bins, range=range, normed=1)
return pdf

def pdf_from_array(array, bins, range):
pdf = histogram1d(array, bins=bins, range=range)
norm = np.sum(pdf)/bins
return pdf/norm

arr = np.random.uniform(-0.5, 0.5, 64**3)
arr = arr.reshape((64, 64, 64))
x = np.linspace(-0.5, 0.5, 100)
pdf_np = pdf_from_array_np(arr, 100, [-0.5, 0.5])
plt.plot(x, pdf_np, label='np')
pdf_fast = pdf_from_array(arr, 100, [-0.5, 0.5])
plt.plot(x, pdf_fast, label='fast')
plt.legend()
plt.show()
print(np.sum((pdf_fast-pdf_np)**2))

figure_1

astrofrog commented 6 years ago

@olgadoronina - thanks for the bug report! Indeed this is an oversight - basically I think we should add a check somewhere here:

https://github.com/astrofrog/fast-histogram/blob/master/fast_histogram/histogram.py#L51

that if x.ndim is greater than 1, we should do x.ravel().