Skip to content

Commit

Permalink
fixes the ibis histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcSkovMadsen committed Dec 1, 2022
1 parent ba03e62 commit a344388
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
18 changes: 16 additions & 2 deletions holoviews/core/data/ibis.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,23 @@ def values(
def histogram(cls, expr, bins, density=True, weights=None):
bins = numpy.asarray(bins)
bins = [int(v) if bins.dtype.kind in 'iu' else float(v) for v in bins]
binned = expr.bucket(bins).name('bucket')

# See https://github.com/ibis-project/ibis/issues/4940#issuecomment-1334181645
df = expr.to_projection()
try:
hist_bins = (
df
.mutate(bucket=expr.bucket(bins))
.bucket
.value_counts()
.sort_by('bucket')
).execute()
except NotImplementedError:
# See https://github.com/ibis-project/ibis/issues/4939
array = expr.execute()
return numpy.histogram(array, bins=bins, density=density, weights=weights)

hist = numpy.zeros(len(bins)-1)
hist_bins = binned.value_counts().sort_by('bucket').execute()
for b, v in zip(hist_bins['bucket'], hist_bins['count']):
if numpy.isnan(b):
continue
Expand Down
7 changes: 7 additions & 0 deletions holoviews/tests/core/data/test_ibisinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ def test_range(dimension, expected, dataset):
def test_dimension_type(dimension, expected, dataset):
assert IbisInterface.dimension_type(dataset, dimension) is expected

def test_histogram(data):
expr = data[data.actual.notnull()].actual
bins = [90.0, 113.33333333333333, 136.66666666666666, 160.0]
result = IbisInterface.histogram(expr, bins, density=False)
np.testing.assert_array_equal(result[0], np.array([1, 3, 3]))
np.testing.assert_array_equal(result[1], np.array(bins))

@pytest.mark.parametrize(["kdims", "vdims", "xaxis_type", "yaxis_type"], [
("date", "actual", bokeh_axes.DatetimeAxis, bokeh_axes.LinearAxis),
("string", "actual", bokeh_axes.CategoricalAxis, bokeh_axes.LinearAxis),
Expand Down

0 comments on commit a344388

Please sign in to comment.