Skip to content

Commit

Permalink
Merge pull request #1806 from astrofrog/use-fast-histogram
Browse files Browse the repository at this point in the history
Use fast-histogram instead of np.histogram
  • Loading branch information
astrofrog authored Jul 3, 2018
2 parents 488bb7d + d07da49 commit 9d8760e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ v0.14.0 (unreleased)

* Deprecated ``Data.visible_components`` and ``Data.primary_components``. [#1788]

* Speed up histogram calculations by using the fast-histogram package instead of
np.histogram. [#1806]

* In the case of categorical attributes, ``Data[name]`` now returns a
``categorical_ndarray`` object rather than the indices of the categories. You
can access the indices with ``Data[name].codes`` and the unique categories
Expand Down
17 changes: 12 additions & 5 deletions glue/core/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import numpy as np
import pandas as pd

from fast_histogram import histogram1d

from glue.external import six
from glue.core.message import (DataUpdateMessage, DataRemoveComponentMessage,
DataAddComponentMessage, NumericalDataChangedMessage,
Expand Down Expand Up @@ -1559,12 +1561,17 @@ def compute_histogram(self, cids, weights=None, range=None, bins=None, log=None,
return np.zeros(bins)

if log:
range = None
bins = np.logspace(np.log10(xmin), np.log10(xmax), bins + 1)
else:
range = (xmin, xmax)
xmin = np.log10(xmin)
xmax = np.log10(xmax)
x = np.log10(x)

# By default fast-histogram drops values that are exactly xmax, so we
# increase xmax very slightly to make sure that this doesn't happen
xmax += 10 * np.spacing(xmax)

range = (xmin, xmax)

return np.histogram(x, range=range, bins=bins, weights=w)[0]
return histogram1d(x, range=range, bins=bins, weights=w)

# DEPRECATED

Expand Down
4 changes: 2 additions & 2 deletions glue/viewers/histogram/qt/tests/test_data_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,14 @@ def test_update_when_limits_unchanged(self):
self.viewer.add_data(self.data)

viewer_state.x_att = self.data.id['y']
viewer_state.hist_x_min = -10
viewer_state.hist_x_min = -10.1
viewer_state.hist_x_max = +10
viewer_state.hist_n_bin = 5

assert_allclose(self.viewer.layers[0].state.histogram[1], [0, 0, 3, 1, 0])

viewer_state.x_att = self.data.id['x']
viewer_state.hist_x_min = -10
viewer_state.hist_x_min = -10.1
viewer_state.hist_x_max = +10
viewer_state.hist_n_bin = 5

Expand Down

0 comments on commit 9d8760e

Please sign in to comment.