提交 143f8248 编写于 作者: M mindspore-ci-bot 提交者: Gitee

!398 fix ZeroDivisionError when original bucket width is 0 by checking the width.

Merge pull request !398 from wenkai/wk1_0629_fix_zero_width_bucket
......@@ -225,7 +225,10 @@ class HistogramContainer:
intersection = self._calc_intersection_len(
min1=cur_left, max1=cur_right,
min2=original_bucket.left, max2=original_bucket.right)
estimated_count = (intersection / original_bucket.width) * original_bucket.count
if not original_bucket.width:
estimated_count = original_bucket.count
else:
estimated_count = (intersection / original_bucket.width) * original_bucket.count
cur_estimated_count += estimated_count
if cur_right > original_bucket.right:
......
......@@ -83,3 +83,23 @@ class TestHistogram:
assert buckets == (
(-1.0, 0.8, 0), (-0.19999999999999996, 0.8, 1), (0.6000000000000001, 0.8, 5), (1.4000000000000004, 0.8, 6),
(2.2, 0.8, 0))
def test_re_sample_buckets_zero_width(self):
"""Test zero width bucket when re-sampling."""
mocked_input = mock.MagicMock()
mocked_bucket = mock.MagicMock()
mocked_bucket.left = 0
mocked_bucket.width = 1
mocked_bucket.count = 1
mocked_bucket2 = mock.MagicMock()
mocked_bucket2.left = 1
mocked_bucket2.width = 0
mocked_bucket2.count = 2
mocked_input.buckets = [mocked_bucket, mocked_bucket2]
histogram = hist.HistogramContainer(mocked_input)
histogram.set_visual_range(max_val=2, min_val=0, bins=3)
buckets = histogram.buckets()
assert buckets == (
(0.0, 0.6666666666666666, 1),
(0.6666666666666666, 0.6666666666666666, 3),
(1.3333333333333333, 0.6666666666666666, 0))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册