diff --git a/mindinsight/datavisual/data_transform/histogram_container.py b/mindinsight/datavisual/data_transform/histogram_container.py index ac60d8d383e530ba64657ec6673c188985c33fd7..09fcb4e21f8e7b370d3a00e02a2652be38714e42 100644 --- a/mindinsight/datavisual/data_transform/histogram_container.py +++ b/mindinsight/datavisual/data_transform/histogram_container.py @@ -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: diff --git a/tests/ut/datavisual/data_transform/test_histogram_container.py b/tests/ut/datavisual/data_transform/test_histogram_container.py index e68154fb3fcb1c839b5231ad698741333cf3d14b..615ff5648c40d102131f55236b89f65bd7df06c1 100644 --- a/tests/ut/datavisual/data_transform/test_histogram_container.py +++ b/tests/ut/datavisual/data_transform/test_histogram_container.py @@ -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))