Skip to content

Commit

Permalink
fix: density was not working with special storages (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii authored Mar 3, 2021
1 parent 5a94994 commit b086c8c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
10 changes: 9 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
====================


Version 2.1.1
--------------------

* Fix density (and density based previews)
`#134 <https://github.com/scikit-hep/hist/pull/134>`_


Version 2.1.0
--------------------

Expand All @@ -26,7 +34,7 @@ Version 2.0.1
* Fixed ``plot2d_full`` incorrectly mirroring the y-axis.
`#105 <https://github.com/scikit-hep/hist/pull/105>`_

* `Hist.plot_pull`: more suitable bands in the pull bands 1sigma, 2 sigma, etc.
* ``Hist.plot_pull``: more suitable bands in the pull bands 1sigma, 2 sigma, etc.
`#102 <https://github.com/scikit-hep/hist/pull/102>`_

* Fixed classichist's usage of `get_terminal_size` to support not running in a terminal
Expand Down
2 changes: 1 addition & 1 deletion src/hist/basehist.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def density(self) -> np.ndarray:
"""
Density numpy array.
"""
total = self.sum() * functools.reduce(operator.mul, self.axes.widths)
total = np.sum(self.values()) * functools.reduce(operator.mul, self.axes.widths)
return self.values() / np.where(total > 0, total, 1)

def show(self, **kwargs):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,14 @@ def test_general_density():
assert pytest.approx(sum(h.density()), 2) == pytest.approx(10 / 6, 2)


def test_weighted_density():
for data in range(10, 20, 10):
h = Hist(axis.Regular(10, -3, 3, name="x"), storage="weight").fill(
np.random.randn(data)
)
assert pytest.approx(sum(h.density()), 2) == pytest.approx(10 / 6, 2)


def test_general_axestuple():
"""
Test general axes tuple -- whether Hist axes tuple work properly.
Expand Down

0 comments on commit b086c8c

Please sign in to comment.