Browse Source

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

tags/v0.6.0-beta
wenkai 5 years ago
parent
commit
c610544905
2 changed files with 24 additions and 1 deletions
  1. +4
    -1
      mindinsight/datavisual/data_transform/histogram_container.py
  2. +20
    -0
      tests/ut/datavisual/data_transform/test_histogram_container.py

+ 4
- 1
mindinsight/datavisual/data_transform/histogram_container.py View File

@@ -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:


+ 20
- 0
tests/ut/datavisual/data_transform/test_histogram_container.py View File

@@ -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))

Loading…
Cancel
Save