|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import unittest |
| 19 | +from datasketches import tdigest_float, tdigest_double |
| 20 | +import numpy as np |
| 21 | + |
| 22 | +class TdigestTest(unittest.TestCase): |
| 23 | + def test_tdigest_double_example(self): |
| 24 | + n = 2 ** 20 |
| 25 | + |
| 26 | + # create a tdigest and inject ~1 million N(0,1) points, both using a vector |
| 27 | + # update as well as a single value |
| 28 | + td = tdigest_double() |
| 29 | + td.update(np.random.normal(size=n-1)) |
| 30 | + td.update(0.0) |
| 31 | + |
| 32 | + # 0 should be near the median |
| 33 | + self.assertAlmostEqual(0.5, td.get_rank(0.0), delta=0.1) |
| 34 | + |
| 35 | + # the median should be near 0 |
| 36 | + self.assertAlmostEqual(0.0, td.get_quantile(0.5), delta=0.1) |
| 37 | + |
| 38 | + # note that with t-digest, while it typically performs quite well in practice, |
| 39 | + # we do not have any sort of theoretical guarantees on the error bounds |
| 40 | + # or even an estimate of what bounds we may expect. |
| 41 | + |
| 42 | + # we also track the min/max independently from the rest of the data |
| 43 | + # which lets us know the full observed data range |
| 44 | + self.assertLessEqual(td.get_min_value(), td.get_quantile(0.01)) |
| 45 | + self.assertLessEqual(0.0, td.get_rank(td.get_min_value())) |
| 46 | + self.assertGreaterEqual(td.get_max_value(), td.get_quantile(0.99)) |
| 47 | + self.assertGreaterEqual(1.0, td.get_rank(td.get_max_value())) |
| 48 | + |
| 49 | + # and a few basic queries about the sketch |
| 50 | + self.assertFalse(td.is_empty()) |
| 51 | + self.assertEqual(td.get_total_weight(), n) |
| 52 | + |
| 53 | + # we can define a new tdiget with a different distribution, then merge them |
| 54 | + td2 = tdigest_double() |
| 55 | + td2.update(np.random.normal(loc=2.0, size=n)) |
| 56 | + td.merge(td2) |
| 57 | + |
| 58 | + # the new median should be near 1.0, and 1.0 should be near the median although |
| 59 | + # the error distribution is not well-characterized so we allow generous margins |
| 60 | + self.assertAlmostEqual(0.5, td.get_rank(1.0), delta=0.2) |
| 61 | + self.assertAlmostEqual(1.0, td.get_quantile(0.5), delta=0.2) |
| 62 | + self.assertEqual(td.get_total_weight(), 2 * n) |
| 63 | + |
| 64 | + # finally, can serialize and deserialize the sketch |
| 65 | + td_bytes = td.serialize() |
| 66 | + new_td = tdigest_double.deserialize(td_bytes) |
| 67 | + self.assertEqual(td.get_total_weight(), new_td.get_total_weight()) |
| 68 | + self.assertEqual(td.get_min_value(), new_td.get_min_value()) |
| 69 | + self.assertEqual(td.get_max_value(), new_td.get_max_value()) |
| 70 | + self.assertEqual(td.get_quantile(0.7), new_td.get_quantile(0.7)) |
| 71 | + self.assertEqual(td.get_rank(0.0), new_td.get_rank(0.0)) |
| 72 | + |
| 73 | + |
| 74 | + # the same tests as above, but with tdigest_float |
| 75 | + def test_tdigest_float_example(self): |
| 76 | + n = 2 ** 20 |
| 77 | + td = tdigest_float() |
| 78 | + td.update(np.random.normal(size=n-1)) |
| 79 | + td.update(0.0) |
| 80 | + |
| 81 | + self.assertAlmostEqual(0.5, td.get_rank(0.0), delta=0.1) |
| 82 | + self.assertAlmostEqual(0.0, td.get_quantile(0.5), delta=0.1) |
| 83 | + |
| 84 | + self.assertLessEqual(td.get_min_value(), td.get_quantile(0.01)) |
| 85 | + self.assertLessEqual(0.0, td.get_rank(td.get_min_value())) |
| 86 | + self.assertGreaterEqual(td.get_max_value(), td.get_quantile(0.99)) |
| 87 | + self.assertGreaterEqual(1.0, td.get_rank(td.get_max_value())) |
| 88 | + |
| 89 | + self.assertFalse(td.is_empty()) |
| 90 | + self.assertEqual(td.get_total_weight(), n) |
| 91 | + |
| 92 | + td2 = tdigest_float() |
| 93 | + td2.update(np.random.normal(loc=2.0, size=n)) |
| 94 | + td.merge(td2) |
| 95 | + |
| 96 | + self.assertAlmostEqual(0.5, td.get_rank(1.0), delta=0.2) |
| 97 | + self.assertAlmostEqual(1.0, td.get_quantile(0.5), delta=0.2) |
| 98 | + self.assertEqual(td.get_total_weight(), 2 * n) |
| 99 | + |
| 100 | + td_bytes = td.serialize() |
| 101 | + new_td = tdigest_float.deserialize(td_bytes) |
| 102 | + self.assertEqual(td.get_total_weight(), new_td.get_total_weight()) |
| 103 | + self.assertEqual(td.get_min_value(), new_td.get_min_value()) |
| 104 | + self.assertEqual(td.get_max_value(), new_td.get_max_value()) |
| 105 | + self.assertEqual(td.get_quantile(0.7), new_td.get_quantile(0.7)) |
| 106 | + self.assertEqual(td.get_rank(0.0), new_td.get_rank(0.0)) |
0 commit comments