datasketches 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (237) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/NOTICE +1 -1
  4. data/README.md +0 -2
  5. data/ext/datasketches/cpc_wrapper.cpp +2 -2
  6. data/ext/datasketches/kll_wrapper.cpp +0 -10
  7. data/lib/datasketches/version.rb +1 -1
  8. data/lib/datasketches.rb +1 -1
  9. data/vendor/datasketches-cpp/CMakeLists.txt +1 -0
  10. data/vendor/datasketches-cpp/CODE_OF_CONDUCT.md +3 -0
  11. data/vendor/datasketches-cpp/CONTRIBUTING.md +50 -0
  12. data/vendor/datasketches-cpp/Doxyfile +2827 -0
  13. data/vendor/datasketches-cpp/LICENSE +0 -76
  14. data/vendor/datasketches-cpp/NOTICE +1 -1
  15. data/vendor/datasketches-cpp/README.md +1 -3
  16. data/vendor/datasketches-cpp/common/CMakeLists.txt +12 -11
  17. data/vendor/datasketches-cpp/common/include/common_defs.hpp +11 -8
  18. data/vendor/datasketches-cpp/common/include/count_zeros.hpp +0 -2
  19. data/vendor/datasketches-cpp/common/include/kolmogorov_smirnov.hpp +9 -6
  20. data/vendor/datasketches-cpp/common/include/optional.hpp +148 -0
  21. data/vendor/datasketches-cpp/common/include/quantiles_sorted_view.hpp +95 -2
  22. data/vendor/datasketches-cpp/common/include/quantiles_sorted_view_impl.hpp +1 -1
  23. data/vendor/datasketches-cpp/common/include/serde.hpp +69 -20
  24. data/vendor/datasketches-cpp/common/test/CMakeLists.txt +1 -1
  25. data/vendor/datasketches-cpp/common/test/optional_test.cpp +85 -0
  26. data/vendor/datasketches-cpp/common/test/test_allocator.hpp +14 -14
  27. data/vendor/datasketches-cpp/count/include/count_min.hpp +132 -78
  28. data/vendor/datasketches-cpp/count/include/count_min_impl.hpp +132 -152
  29. data/vendor/datasketches-cpp/count/test/CMakeLists.txt +11 -12
  30. data/vendor/datasketches-cpp/count/test/count_min_allocation_test.cpp +61 -61
  31. data/vendor/datasketches-cpp/count/test/count_min_test.cpp +175 -178
  32. data/vendor/datasketches-cpp/cpc/include/cpc_common.hpp +14 -20
  33. data/vendor/datasketches-cpp/cpc/include/cpc_compressor.hpp +7 -4
  34. data/vendor/datasketches-cpp/cpc/include/cpc_compressor_impl.hpp +17 -17
  35. data/vendor/datasketches-cpp/cpc/include/cpc_sketch.hpp +40 -40
  36. data/vendor/datasketches-cpp/cpc/include/cpc_sketch_impl.hpp +13 -10
  37. data/vendor/datasketches-cpp/cpc/include/cpc_union.hpp +35 -11
  38. data/vendor/datasketches-cpp/cpc/include/cpc_union_impl.hpp +8 -8
  39. data/vendor/datasketches-cpp/cpc/include/u32_table.hpp +3 -2
  40. data/vendor/datasketches-cpp/cpc/include/u32_table_impl.hpp +5 -5
  41. data/vendor/datasketches-cpp/cpc/test/CMakeLists.txt +20 -7
  42. data/vendor/datasketches-cpp/cpc/test/cpc_sketch_deserialize_from_java_test.cpp +60 -0
  43. data/vendor/datasketches-cpp/{python/include/py_object_lt.hpp → cpc/test/cpc_sketch_serialize_for_java.cpp} +15 -14
  44. data/vendor/datasketches-cpp/cpc/test/cpc_sketch_test.cpp +4 -29
  45. data/vendor/datasketches-cpp/cpc/test/cpc_union_test.cpp +4 -4
  46. data/vendor/datasketches-cpp/density/include/density_sketch.hpp +29 -9
  47. data/vendor/datasketches-cpp/density/include/density_sketch_impl.hpp +1 -1
  48. data/vendor/datasketches-cpp/density/test/CMakeLists.txt +0 -1
  49. data/vendor/datasketches-cpp/fi/include/frequent_items_sketch.hpp +21 -9
  50. data/vendor/datasketches-cpp/fi/include/frequent_items_sketch_impl.hpp +6 -4
  51. data/vendor/datasketches-cpp/fi/test/CMakeLists.txt +14 -1
  52. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_deserialize_from_java_test.cpp +95 -0
  53. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_serialize_for_java.cpp +83 -0
  54. data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_test.cpp +3 -42
  55. data/vendor/datasketches-cpp/hll/include/CouponList-internal.hpp +2 -2
  56. data/vendor/datasketches-cpp/hll/include/CouponList.hpp +3 -1
  57. data/vendor/datasketches-cpp/hll/include/HllArray-internal.hpp +3 -3
  58. data/vendor/datasketches-cpp/hll/include/HllArray.hpp +5 -3
  59. data/vendor/datasketches-cpp/hll/include/HllSketch-internal.hpp +4 -4
  60. data/vendor/datasketches-cpp/hll/include/HllSketchImpl.hpp +3 -1
  61. data/vendor/datasketches-cpp/hll/include/HllUtil.hpp +0 -12
  62. data/vendor/datasketches-cpp/hll/include/hll.hpp +70 -57
  63. data/vendor/datasketches-cpp/hll/test/CMakeLists.txt +14 -1
  64. data/vendor/datasketches-cpp/hll/test/ToFromByteArrayTest.cpp +0 -68
  65. data/vendor/datasketches-cpp/hll/test/hll_sketch_deserialize_from_java_test.cpp +69 -0
  66. data/vendor/datasketches-cpp/hll/test/hll_sketch_serialize_for_java.cpp +52 -0
  67. data/vendor/datasketches-cpp/kll/include/kll_helper_impl.hpp +2 -2
  68. data/vendor/datasketches-cpp/kll/include/kll_sketch.hpp +71 -50
  69. data/vendor/datasketches-cpp/kll/include/kll_sketch_impl.hpp +59 -130
  70. data/vendor/datasketches-cpp/kll/test/CMakeLists.txt +14 -1
  71. data/vendor/datasketches-cpp/kll/test/kll_sketch_deserialize_from_java_test.cpp +103 -0
  72. data/vendor/datasketches-cpp/kll/test/kll_sketch_serialize_for_java.cpp +62 -0
  73. data/vendor/datasketches-cpp/kll/test/kll_sketch_test.cpp +3 -38
  74. data/vendor/datasketches-cpp/quantiles/include/quantiles_sketch.hpp +68 -51
  75. data/vendor/datasketches-cpp/quantiles/include/quantiles_sketch_impl.hpp +62 -132
  76. data/vendor/datasketches-cpp/quantiles/test/CMakeLists.txt +14 -1
  77. data/vendor/datasketches-cpp/quantiles/test/quantiles_sketch_deserialize_from_java_test.cpp +84 -0
  78. data/vendor/datasketches-cpp/quantiles/test/quantiles_sketch_serialize_for_java.cpp +52 -0
  79. data/vendor/datasketches-cpp/quantiles/test/quantiles_sketch_test.cpp +14 -38
  80. data/vendor/datasketches-cpp/req/include/req_common.hpp +7 -3
  81. data/vendor/datasketches-cpp/req/include/req_compactor_impl.hpp +2 -2
  82. data/vendor/datasketches-cpp/req/include/req_sketch.hpp +97 -23
  83. data/vendor/datasketches-cpp/req/include/req_sketch_impl.hpp +48 -109
  84. data/vendor/datasketches-cpp/req/test/CMakeLists.txt +14 -1
  85. data/vendor/datasketches-cpp/req/test/req_sketch_deserialize_from_java_test.cpp +55 -0
  86. data/vendor/datasketches-cpp/{tuple/include/array_of_doubles_intersection_impl.hpp → req/test/req_sketch_serialize_for_java.cpp} +12 -7
  87. data/vendor/datasketches-cpp/req/test/req_sketch_test.cpp +3 -89
  88. data/vendor/datasketches-cpp/sampling/CMakeLists.txt +4 -0
  89. data/vendor/datasketches-cpp/sampling/include/ebpps_sample.hpp +210 -0
  90. data/vendor/datasketches-cpp/sampling/include/ebpps_sample_impl.hpp +535 -0
  91. data/vendor/datasketches-cpp/sampling/include/ebpps_sketch.hpp +281 -0
  92. data/vendor/datasketches-cpp/sampling/include/ebpps_sketch_impl.hpp +531 -0
  93. data/vendor/datasketches-cpp/sampling/include/var_opt_sketch.hpp +69 -26
  94. data/vendor/datasketches-cpp/sampling/include/var_opt_sketch_impl.hpp +3 -3
  95. data/vendor/datasketches-cpp/sampling/include/var_opt_union.hpp +10 -11
  96. data/vendor/datasketches-cpp/sampling/include/var_opt_union_impl.hpp +4 -4
  97. data/vendor/datasketches-cpp/sampling/test/CMakeLists.txt +55 -8
  98. data/vendor/datasketches-cpp/sampling/test/ebpps_allocation_test.cpp +96 -0
  99. data/vendor/datasketches-cpp/sampling/test/ebpps_sample_test.cpp +137 -0
  100. data/vendor/datasketches-cpp/sampling/test/ebpps_sketch_test.cpp +266 -0
  101. data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_deserialize_from_java_test.cpp +81 -0
  102. data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_serialize_for_java.cpp +54 -0
  103. data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_test.cpp +0 -37
  104. data/vendor/datasketches-cpp/sampling/test/var_opt_union_deserialize_from_java_test.cpp +50 -0
  105. data/vendor/datasketches-cpp/sampling/test/var_opt_union_serialize_for_java.cpp +56 -0
  106. data/vendor/datasketches-cpp/sampling/test/var_opt_union_test.cpp +0 -18
  107. data/vendor/datasketches-cpp/theta/include/bit_packing.hpp +2608 -2608
  108. data/vendor/datasketches-cpp/theta/include/bounds_on_ratios_in_sampled_sets.hpp +1 -0
  109. data/vendor/datasketches-cpp/theta/include/bounds_on_ratios_in_theta_sketched_sets.hpp +7 -6
  110. data/vendor/datasketches-cpp/theta/include/theta_a_not_b.hpp +20 -5
  111. data/vendor/datasketches-cpp/theta/include/theta_constants.hpp +10 -4
  112. data/vendor/datasketches-cpp/theta/include/theta_helpers.hpp +1 -1
  113. data/vendor/datasketches-cpp/theta/include/theta_intersection.hpp +13 -5
  114. data/vendor/datasketches-cpp/theta/include/theta_intersection_base_impl.hpp +5 -5
  115. data/vendor/datasketches-cpp/theta/include/theta_intersection_impl.hpp +3 -3
  116. data/vendor/datasketches-cpp/theta/include/theta_jaccard_similarity.hpp +2 -1
  117. data/vendor/datasketches-cpp/theta/include/theta_jaccard_similarity_base.hpp +1 -0
  118. data/vendor/datasketches-cpp/theta/include/theta_set_difference_base_impl.hpp +1 -1
  119. data/vendor/datasketches-cpp/theta/include/theta_sketch.hpp +126 -27
  120. data/vendor/datasketches-cpp/theta/include/theta_sketch_impl.hpp +8 -8
  121. data/vendor/datasketches-cpp/theta/include/theta_union.hpp +17 -10
  122. data/vendor/datasketches-cpp/theta/include/theta_union_base_impl.hpp +1 -1
  123. data/vendor/datasketches-cpp/theta/include/theta_union_impl.hpp +3 -3
  124. data/vendor/datasketches-cpp/theta/include/theta_update_sketch_base.hpp +5 -2
  125. data/vendor/datasketches-cpp/theta/include/theta_update_sketch_base_impl.hpp +11 -1
  126. data/vendor/datasketches-cpp/theta/test/CMakeLists.txt +14 -1
  127. data/vendor/datasketches-cpp/theta/test/theta_sketch_deserialize_from_java_test.cpp +57 -0
  128. data/vendor/datasketches-cpp/theta/test/theta_sketch_serialize_for_java.cpp +61 -0
  129. data/vendor/datasketches-cpp/theta/test/theta_sketch_test.cpp +0 -188
  130. data/vendor/datasketches-cpp/tuple/CMakeLists.txt +8 -7
  131. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_sketch.hpp +19 -144
  132. data/vendor/datasketches-cpp/tuple/include/{array_of_doubles_a_not_b.hpp → array_tuple_a_not_b.hpp} +24 -16
  133. data/vendor/datasketches-cpp/tuple/include/{array_of_doubles_a_not_b_impl.hpp → array_tuple_a_not_b_impl.hpp} +4 -4
  134. data/vendor/datasketches-cpp/tuple/include/array_tuple_intersection.hpp +65 -0
  135. data/vendor/datasketches-cpp/{python/include/py_object_ostream.hpp → tuple/include/array_tuple_intersection_impl.hpp} +7 -24
  136. data/vendor/datasketches-cpp/tuple/include/array_tuple_sketch.hpp +237 -0
  137. data/vendor/datasketches-cpp/tuple/include/{array_of_doubles_sketch_impl.hpp → array_tuple_sketch_impl.hpp} +40 -41
  138. data/vendor/datasketches-cpp/tuple/include/array_tuple_union.hpp +81 -0
  139. data/vendor/datasketches-cpp/tuple/include/array_tuple_union_impl.hpp +43 -0
  140. data/vendor/datasketches-cpp/tuple/include/tuple_a_not_b.hpp +11 -2
  141. data/vendor/datasketches-cpp/tuple/include/tuple_intersection.hpp +17 -10
  142. data/vendor/datasketches-cpp/tuple/include/tuple_jaccard_similarity.hpp +2 -1
  143. data/vendor/datasketches-cpp/tuple/include/tuple_sketch.hpp +95 -32
  144. data/vendor/datasketches-cpp/tuple/include/tuple_union.hpp +19 -11
  145. data/vendor/datasketches-cpp/tuple/test/CMakeLists.txt +16 -1
  146. data/vendor/datasketches-cpp/tuple/test/aod_sketch_deserialize_from_java_test.cpp +76 -0
  147. data/vendor/datasketches-cpp/tuple/test/aod_sketch_serialize_for_java.cpp +62 -0
  148. data/vendor/datasketches-cpp/tuple/test/array_of_doubles_sketch_test.cpp +5 -129
  149. data/vendor/datasketches-cpp/tuple/test/engagement_test.cpp +85 -89
  150. data/vendor/datasketches-cpp/tuple/test/tuple_jaccard_similarity_test.cpp +3 -1
  151. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_deserialize_from_java_test.cpp +47 -0
  152. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_serialize_for_java.cpp +38 -0
  153. data/vendor/datasketches-cpp/tuple/test/tuple_sketch_test.cpp +1 -1
  154. data/vendor/datasketches-cpp/version.cfg.in +1 -1
  155. metadata +47 -93
  156. data/vendor/datasketches-cpp/MANIFEST.in +0 -39
  157. data/vendor/datasketches-cpp/fi/test/items_sketch_string_from_java.sk +0 -0
  158. data/vendor/datasketches-cpp/fi/test/items_sketch_string_utf8_from_java.sk +0 -0
  159. data/vendor/datasketches-cpp/fi/test/longs_sketch_from_java.sk +0 -0
  160. data/vendor/datasketches-cpp/hll/test/array6_from_java.sk +0 -0
  161. data/vendor/datasketches-cpp/hll/test/compact_array4_from_java.sk +0 -0
  162. data/vendor/datasketches-cpp/hll/test/compact_set_from_java.sk +0 -0
  163. data/vendor/datasketches-cpp/hll/test/list_from_java.sk +0 -0
  164. data/vendor/datasketches-cpp/hll/test/updatable_array4_from_java.sk +0 -0
  165. data/vendor/datasketches-cpp/hll/test/updatable_set_from_java.sk +0 -0
  166. data/vendor/datasketches-cpp/kll/test/kll_sketch_from_java.sk +0 -0
  167. data/vendor/datasketches-cpp/pyproject.toml +0 -23
  168. data/vendor/datasketches-cpp/python/CMakeLists.txt +0 -87
  169. data/vendor/datasketches-cpp/python/README.md +0 -85
  170. data/vendor/datasketches-cpp/python/datasketches/DensityWrapper.py +0 -87
  171. data/vendor/datasketches-cpp/python/datasketches/KernelFunction.py +0 -35
  172. data/vendor/datasketches-cpp/python/datasketches/PySerDe.py +0 -110
  173. data/vendor/datasketches-cpp/python/datasketches/TuplePolicy.py +0 -77
  174. data/vendor/datasketches-cpp/python/datasketches/TupleWrapper.py +0 -205
  175. data/vendor/datasketches-cpp/python/datasketches/__init__.py +0 -38
  176. data/vendor/datasketches-cpp/python/include/kernel_function.hpp +0 -98
  177. data/vendor/datasketches-cpp/python/include/py_serde.hpp +0 -113
  178. data/vendor/datasketches-cpp/python/include/quantile_conditional.hpp +0 -104
  179. data/vendor/datasketches-cpp/python/include/tuple_policy.hpp +0 -136
  180. data/vendor/datasketches-cpp/python/jupyter/CPCSketch.ipynb +0 -345
  181. data/vendor/datasketches-cpp/python/jupyter/FrequentItemsSketch.ipynb +0 -354
  182. data/vendor/datasketches-cpp/python/jupyter/HLLSketch.ipynb +0 -346
  183. data/vendor/datasketches-cpp/python/jupyter/KLLSketch.ipynb +0 -463
  184. data/vendor/datasketches-cpp/python/jupyter/ThetaSketchNotebook.ipynb +0 -403
  185. data/vendor/datasketches-cpp/python/pybind11Path.cmd +0 -21
  186. data/vendor/datasketches-cpp/python/src/__init__.py +0 -18
  187. data/vendor/datasketches-cpp/python/src/count_wrapper.cpp +0 -101
  188. data/vendor/datasketches-cpp/python/src/cpc_wrapper.cpp +0 -76
  189. data/vendor/datasketches-cpp/python/src/datasketches.cpp +0 -58
  190. data/vendor/datasketches-cpp/python/src/density_wrapper.cpp +0 -95
  191. data/vendor/datasketches-cpp/python/src/fi_wrapper.cpp +0 -182
  192. data/vendor/datasketches-cpp/python/src/hll_wrapper.cpp +0 -126
  193. data/vendor/datasketches-cpp/python/src/kll_wrapper.cpp +0 -158
  194. data/vendor/datasketches-cpp/python/src/ks_wrapper.cpp +0 -68
  195. data/vendor/datasketches-cpp/python/src/py_serde.cpp +0 -112
  196. data/vendor/datasketches-cpp/python/src/quantiles_wrapper.cpp +0 -155
  197. data/vendor/datasketches-cpp/python/src/req_wrapper.cpp +0 -154
  198. data/vendor/datasketches-cpp/python/src/theta_wrapper.cpp +0 -166
  199. data/vendor/datasketches-cpp/python/src/tuple_wrapper.cpp +0 -215
  200. data/vendor/datasketches-cpp/python/src/vector_of_kll.cpp +0 -490
  201. data/vendor/datasketches-cpp/python/src/vo_wrapper.cpp +0 -173
  202. data/vendor/datasketches-cpp/python/tests/__init__.py +0 -16
  203. data/vendor/datasketches-cpp/python/tests/count_min_test.py +0 -86
  204. data/vendor/datasketches-cpp/python/tests/cpc_test.py +0 -64
  205. data/vendor/datasketches-cpp/python/tests/density_test.py +0 -93
  206. data/vendor/datasketches-cpp/python/tests/fi_test.py +0 -149
  207. data/vendor/datasketches-cpp/python/tests/hll_test.py +0 -129
  208. data/vendor/datasketches-cpp/python/tests/kll_test.py +0 -159
  209. data/vendor/datasketches-cpp/python/tests/quantiles_test.py +0 -160
  210. data/vendor/datasketches-cpp/python/tests/req_test.py +0 -159
  211. data/vendor/datasketches-cpp/python/tests/theta_test.py +0 -148
  212. data/vendor/datasketches-cpp/python/tests/tuple_test.py +0 -206
  213. data/vendor/datasketches-cpp/python/tests/vector_of_kll_test.py +0 -148
  214. data/vendor/datasketches-cpp/python/tests/vo_test.py +0 -132
  215. data/vendor/datasketches-cpp/req/test/req_float_empty_from_java.sk +0 -0
  216. data/vendor/datasketches-cpp/req/test/req_float_estimation_from_java.sk +0 -0
  217. data/vendor/datasketches-cpp/req/test/req_float_exact_from_java.sk +0 -0
  218. data/vendor/datasketches-cpp/req/test/req_float_raw_items_from_java.sk +0 -0
  219. data/vendor/datasketches-cpp/req/test/req_float_single_item_from_java.sk +0 -0
  220. data/vendor/datasketches-cpp/sampling/test/binaries_from_java.txt +0 -67
  221. data/vendor/datasketches-cpp/sampling/test/varopt_sketch_long_sampling.sk +0 -0
  222. data/vendor/datasketches-cpp/sampling/test/varopt_sketch_string_exact.sk +0 -0
  223. data/vendor/datasketches-cpp/sampling/test/varopt_union_double_sampling.sk +0 -0
  224. data/vendor/datasketches-cpp/setup.py +0 -110
  225. data/vendor/datasketches-cpp/theta/test/theta_compact_empty_from_java.sk +0 -0
  226. data/vendor/datasketches-cpp/theta/test/theta_compact_estimation_from_java.sk +0 -0
  227. data/vendor/datasketches-cpp/theta/test/theta_compact_exact_from_java.sk +0 -0
  228. data/vendor/datasketches-cpp/theta/test/theta_compact_single_item_from_java.sk +0 -0
  229. data/vendor/datasketches-cpp/tox.ini +0 -26
  230. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_intersection.hpp +0 -52
  231. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_union.hpp +0 -81
  232. data/vendor/datasketches-cpp/tuple/include/array_of_doubles_union_impl.hpp +0 -43
  233. data/vendor/datasketches-cpp/tuple/test/aod_1_compact_empty_from_java.sk +0 -1
  234. data/vendor/datasketches-cpp/tuple/test/aod_1_compact_estimation_from_java.sk +0 -0
  235. data/vendor/datasketches-cpp/tuple/test/aod_1_compact_non_empty_no_entries_from_java.sk +0 -0
  236. data/vendor/datasketches-cpp/tuple/test/aod_2_compact_exact_from_java.sk +0 -0
  237. data/vendor/datasketches-cpp/tuple/test/aod_3_compact_empty_from_java.sk +0 -1
@@ -1,86 +0,0 @@
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 count_min_sketch
20
-
21
- class CountMinTest(unittest.TestCase):
22
- def test_count_min_example(self):
23
- # we'll define target confidence and relative error and use the built-in
24
- # methods to determine how many hashes and buckets to use
25
- confidence = 0.95
26
- num_hashes = count_min_sketch.suggest_num_hashes(confidence)
27
- relative_error = 0.01
28
- num_buckets = count_min_sketch.suggest_num_buckets(relative_error)
29
-
30
- # now we can create a few empty sketches
31
- cm = count_min_sketch(num_hashes, num_buckets)
32
- cm2 = count_min_sketch(num_hashes, num_buckets)
33
- self.assertTrue(cm.is_empty())
34
-
35
- # we'll use a moderate number of distinct items with
36
- # increasing weights, with each item's weight being
37
- # equal to its value
38
- n = 1000
39
- total_wt = 0
40
- for i in range(1, n+1):
41
- cm.update(i, i)
42
- total_wt += i
43
- self.assertFalse(cm.is_empty())
44
- self.assertEqual(cm.get_total_weight(), total_wt)
45
-
46
- # querying the items, each of them should
47
- # have a non-zero count. the estimate should
48
- # be at least i with appropriately behaved bounds.
49
- for i in range(1, n+1):
50
- val = cm.get_estimate(i)
51
- self.assertGreaterEqual(val, i)
52
- self.assertGreaterEqual(val, cm.get_lower_bound(i))
53
- self.assertGreater(cm.get_upper_bound(i), val)
54
-
55
- # values not in the sketch should have lower estimates, but
56
- # are not guaranteed to be zero and will succeed
57
- self.assertIsNotNone(cm.get_estimate("not in set"))
58
-
59
- # we can create another sketch with partial overlap
60
- # and merge them
61
- for i in range(int(n / 2), int(3 * n / 2)):
62
- cm2.update(i, i)
63
- cm.merge(cm2)
64
-
65
- # and the estimated weight for the overlapped meerged values
66
- # (n/2 to n) should now be at least 2x the value
67
- self.assertGreaterEqual(cm.get_estimate(n), 2 * n)
68
-
69
- # finally, serialize and reconstruct
70
- cm_bytes = cm.serialize()
71
- self.assertEqual(cm.get_serialized_size_bytes(), len(cm_bytes))
72
- new_cm = count_min_sketch.deserialize(cm_bytes)
73
-
74
- # and now interrogate the sketch
75
- self.assertFalse(new_cm.is_empty())
76
- self.assertEqual(new_cm.get_num_hashes(), cm.get_num_hashes())
77
- self.assertEqual(new_cm.get_num_buckets(), cm.get_num_buckets())
78
- self.assertEqual(new_cm.get_total_weight(), cm.get_total_weight())
79
-
80
- # we can also iterate through values in and out of the sketch to ensure
81
- # the estimates match
82
- for i in range(0, 2 * n):
83
- self.assertEqual(cm.get_estimate(i), new_cm.get_estimate(i))
84
-
85
- if __name__ == '__main__':
86
- unittest.main()
@@ -1,64 +0,0 @@
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 cpc_sketch, cpc_union
20
-
21
- class CpcTest(unittest.TestCase):
22
- def test_cpc_example(self):
23
- lgk = 12 # 2^k = 4096 rows in the table
24
- n = 1 << 18 # ~256k distinct values
25
-
26
- # create a couple sketches and inject some values
27
- # we'll have 1/4 of the values overlap
28
- cpc = cpc_sketch(lgk)
29
- cpc2 = cpc_sketch(lgk)
30
- offset = int(3 * n / 4) # it's a float w/o cast
31
- # because we hash on the bits, not an abstract numeric value,
32
- # cpc.update(1) and cpc.update(1.0) give different results.
33
- for i in range(0, n):
34
- cpc.update(i)
35
- cpc2.update(i + offset)
36
-
37
- # although we provide get_composite_estimate() and get_estimate(),
38
- # the latter will always give the best available estimate. we
39
- # recommend using get_estimate().
40
- # we can check that the upper and lower bounds bracket the
41
- # estimate, without needing to know the exact value.
42
- self.assertLessEqual(cpc.get_lower_bound(1), cpc.get_estimate())
43
- self.assertGreaterEqual(cpc.get_upper_bound(1), cpc.get_estimate())
44
-
45
- # union is a separate class, so we need to get_result()
46
- # to query the unioned sketches
47
- union = cpc_union(lgk)
48
- union.update(cpc)
49
- union.update(cpc2)
50
- result = union.get_result()
51
-
52
- # since our process here (including post-union CPC) is
53
- # deterministic, we have checked and know the exact
54
- # answer is within one standard deviation of the estimate
55
- self.assertLessEqual(result.get_lower_bound(1), 7 * n / 4)
56
- self.assertGreaterEqual(result.get_upper_bound(1), 7 * n / 4)
57
-
58
- # serialize for storage and reconstruct
59
- sk_bytes = result.serialize()
60
- new_cpc = cpc_sketch.deserialize(sk_bytes)
61
- self.assertFalse(new_cpc.is_empty())
62
-
63
- if __name__ == '__main__':
64
- unittest.main()
@@ -1,93 +0,0 @@
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 density_sketch, KernelFunction
20
- import numpy as np
21
-
22
- class UnitSphereKernel(KernelFunction):
23
- def __call__(self, a: np.array, b: np.array) -> float:
24
- if np.linalg.norm(a - b) < 1.0:
25
- return 1.0
26
- else:
27
- return 0.0
28
-
29
- class densityTest(unittest.TestCase):
30
- def test_density_sketch(self):
31
- k = 10
32
- dim = 3
33
- n = 1000
34
-
35
- sketch = density_sketch(k, dim)
36
-
37
- self.assertEqual(sketch.get_k(), k)
38
- self.assertEqual(sketch.get_dim(), dim)
39
- self.assertTrue(sketch.is_empty())
40
- self.assertFalse(sketch.is_estimation_mode())
41
- self.assertEqual(sketch.get_n(), 0)
42
- self.assertEqual(sketch.get_num_retained(), 0)
43
-
44
- for i in range(n):
45
- sketch.update([i, i, i])
46
-
47
- self.assertFalse(sketch.is_empty())
48
- self.assertTrue(sketch.is_estimation_mode())
49
- self.assertEqual(sketch.get_n(), n)
50
- self.assertGreater(sketch.get_num_retained(), k)
51
- self.assertLess(sketch.get_num_retained(), n)
52
- self.assertGreater(sketch.get_estimate([n - 1, n - 1, n - 1]), 0)
53
-
54
- for tuple in sketch:
55
- vector = tuple[0]
56
- weight = tuple[1]
57
- self.assertEqual(len(vector), dim)
58
- self.assertGreaterEqual(weight, 1)
59
-
60
- sk_bytes = sketch.serialize()
61
- sketch2 = density_sketch.deserialize(sk_bytes)
62
- self.assertEqual(sketch.get_estimate([1.5, 2.5, 3.5]), sketch2.get_estimate([1.5, 2.5, 3.5]))
63
-
64
- def test_density_merge(self):
65
- sketch1 = density_sketch(10, 2)
66
- sketch1.update([0, 0])
67
- sketch2 = density_sketch(10, 2)
68
- sketch2.update([0, 1])
69
- sketch1.merge(sketch2)
70
- self.assertEqual(sketch1.get_n(), 2)
71
- self.assertEqual(sketch1.get_num_retained(), 2)
72
-
73
- def test_custom_kernel(self):
74
- gaussianSketch = density_sketch(10, 2) # default kernel
75
- sphericalSketch = density_sketch(10, 2, UnitSphereKernel())
76
-
77
- p = [1, 1]
78
- gaussianSketch.update(p)
79
- sphericalSketch.update(p)
80
-
81
- # Spherical kernel should return 1.0 for a nearby point, 0 farther
82
- # Gaussian kernel should return something nonzero when farther away
83
- self.assertEqual(sphericalSketch.get_estimate([1.001, 1]), 1.0)
84
- self.assertEqual(sphericalSketch.get_estimate([2, 2]), 0.0)
85
- self.assertGreater(gaussianSketch.get_estimate([2, 2]), 0.0)
86
-
87
- # We can also use a custom kernel when deserializing
88
- sk_bytes = sphericalSketch.serialize()
89
- sphericalRebuilt = density_sketch.deserialize(sk_bytes, UnitSphereKernel())
90
- self.assertEqual(sphericalSketch.get_estimate([1.001, 1]), sphericalRebuilt.get_estimate([1.001, 1]))
91
-
92
- if __name__ == '__main__':
93
- unittest.main()
@@ -1,149 +0,0 @@
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 frequent_strings_sketch, frequent_items_sketch
20
- from datasketches import frequent_items_error_type, PyIntsSerDe
21
-
22
- class FiTest(unittest.TestCase):
23
- def test_fi_strings_example(self):
24
- k = 3 # a small value so we can easily fill the sketch
25
- fi = frequent_strings_sketch(k)
26
-
27
- # we'll use a small number of distinct items so we
28
- # can use exponentially increasing weights and have
29
- # some frequent items, decreasing so we have some
30
- # small items inserted after a purge
31
- n = 8
32
- for i in range(0, n):
33
- fi.update(str(i), 2 ** (n - i))
34
-
35
- # there are two ways to extract items :
36
- # * NO_FALSE_POSITIVES includes all items with a lower bound
37
- # above the a posteriori error
38
- # * NO_FALSE_NEGATIVES includes all items with an uper bound
39
- # above the a posteriori error
40
- # a more complete discussion may be found at
41
- # https://datasketches.github.io/docs/Frequency/FrequentItemsOverview.html
42
- items_no_fp = fi.get_frequent_items(frequent_items_error_type.NO_FALSE_POSITIVES)
43
- items_no_fn = fi.get_frequent_items(frequent_items_error_type.NO_FALSE_NEGATIVES)
44
- self.assertLessEqual(len(items_no_fp), len(items_no_fn))
45
-
46
- # the items list returns a decreasing weight-sorted list, and
47
- # for each item we have (item, estimate, lower_bound, upper_bound)
48
- item = items_no_fp[1]
49
- self.assertLessEqual(item[2], item[1]) # lower bound vs estimate
50
- self.assertLessEqual(item[1], item[3]) # estimate vs upper bound
51
-
52
- # we can also query directly for a specific item
53
- id = items_no_fn[0][0]
54
- est = fi.get_estimate(id)
55
- lb = fi.get_lower_bound(id)
56
- ub = fi.get_upper_bound(id)
57
- self.assertLessEqual(lb, est)
58
- self.assertLessEqual(est, ub)
59
-
60
- # the values are zero if the item isn't in our list
61
- self.assertEqual(fi.get_estimate("NaN"), 0)
62
-
63
- # now create a second sketch with a lot of unique
64
- # values but all with equal weight (of 1) such that
65
- # the total weight is much larger than the first sketch
66
- fi2 = frequent_strings_sketch(k)
67
- wt = fi.get_total_weight()
68
- for i in range(0, 4*wt):
69
- fi2.update(str(i))
70
-
71
- # merge the second sketch into the first
72
- fi.merge(fi2)
73
-
74
- # we can see that the weight is much larger
75
- self.assertEqual(5 * wt, fi.get_total_weight())
76
-
77
- # querying with NO_FALSE_POSITIVES means we don't find anything
78
- # heavy enough to return
79
- items_no_fp = fi.get_frequent_items(frequent_items_error_type.NO_FALSE_POSITIVES)
80
- self.assertEqual(len(items_no_fp), 0)
81
-
82
- # we do, however, find a few potential heavy items
83
- # if querying with NO_FALSE_NEGATIVES
84
- items_no_fn = fi.get_frequent_items(frequent_items_error_type.NO_FALSE_NEGATIVES)
85
- self.assertGreater(len(items_no_fn), 0)
86
-
87
- # finally, serialize and reconstruct
88
- fi_bytes = fi.serialize()
89
- self.assertEqual(len(fi_bytes), fi.get_serialized_size_bytes())
90
- new_fi = frequent_strings_sketch.deserialize(fi_bytes)
91
-
92
- # and now interrogate the sketch
93
- self.assertFalse(new_fi.is_empty())
94
- self.assertGreater(new_fi.get_num_active_items(), 0)
95
- self.assertEqual(5 * wt, new_fi.get_total_weight())
96
-
97
- # This example uses generic objects but is otherwise identical
98
- def test_fi_items_example(self):
99
- k = 3 # a small value so we can easily fill the sketch
100
- fi = frequent_items_sketch(k)
101
-
102
- # as above, but in this case inserting ints
103
- n = 8
104
- for i in range(0, n):
105
- fi.update(i, 2 ** (n - i))
106
-
107
- # everything else works identically, so let's jump straight
108
- # to merging and serialization
109
-
110
- # now create a second sketch with a lot of unique
111
- # values but all with equal weight (of 1) such that
112
- # the total weight is much larger than the first sketch
113
- fi2 = frequent_items_sketch(k)
114
- wt = fi.get_total_weight()
115
- for i in range(0, 4*wt):
116
- fi2.update(i)
117
-
118
- # merge the second sketch into the first
119
- fi.merge(fi2)
120
-
121
- # we can see that the weight is much larger
122
- self.assertEqual(5 * wt, fi.get_total_weight())
123
-
124
- # finally, serialize and reconstruct -- now we need a serde to tell
125
- # (de)serialization how to interpret the objects
126
- fi_bytes = fi.serialize(PyIntsSerDe())
127
- self.assertEqual(len(fi_bytes), fi.get_serialized_size_bytes(PyIntsSerDe()))
128
- new_fi = frequent_items_sketch.deserialize(fi_bytes, PyIntsSerDe())
129
-
130
- # and again interrogate the sketch to check that it's what we serialized
131
- self.assertFalse(new_fi.is_empty())
132
- self.assertGreater(new_fi.get_num_active_items(), 0)
133
- self.assertEqual(5 * wt, new_fi.get_total_weight())
134
-
135
-
136
- def test_fi_sketch(self):
137
- # only testing a few things not used in the above example
138
- k = 12
139
- wt = 10000
140
- fi = frequent_strings_sketch(k)
141
-
142
- self.assertAlmostEqual(fi.get_sketch_epsilon(), 0.0008545, delta=1e-6)
143
-
144
- sk_apriori_error = fi.get_sketch_epsilon() * wt
145
- reference_apriori_error = frequent_strings_sketch.get_apriori_error(k, wt)
146
- self.assertAlmostEqual(sk_apriori_error, reference_apriori_error, delta=1e-6)
147
-
148
- if __name__ == '__main__':
149
- unittest.main()
@@ -1,129 +0,0 @@
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 hll_sketch, hll_union, tgt_hll_type
20
-
21
- class HllTest(unittest.TestCase):
22
- def test_hll_example(self):
23
- lgk = 12 # 2^k = 4096 rows in the table
24
- n = 1 << 18 # ~256k unique values
25
-
26
- # create a couple sketches and inject some values
27
- # we'll have 1/4 of the values overlap
28
- hll = hll_sketch(lgk, tgt_hll_type.HLL_8)
29
- hll2 = hll_sketch(lgk, tgt_hll_type.HLL_6)
30
- offset = int(3 * n / 4) # it's a float w/o cast
31
- # because we hash on the bits, not an abstract numeric value,
32
- # hll.update(1) and hll.update(1.0) give different results.
33
- for i in range(0, n):
34
- hll.update(i)
35
- hll2.update(i + offset)
36
-
37
- # we can check that the upper and lower bounds bracket the
38
- # estimate, without needing to know the exact value.
39
- self.assertLessEqual(hll.get_lower_bound(1), hll.get_estimate())
40
- self.assertGreaterEqual(hll.get_upper_bound(1), hll.get_estimate())
41
-
42
- # union is a separate class, and we can either get a result
43
- # sketch or query the union object directly
44
- union = hll_union(lgk)
45
- union.update(hll)
46
- union.update(hll2)
47
- result = union.get_result()
48
- self.assertEqual(result.get_estimate(), union.get_estimate())
49
-
50
- # since our process here (including post-union HLL) is
51
- # deterministic, we have checked and know the exact
52
- # answer is within one standard deviation of the estimate
53
- self.assertLessEqual(union.get_lower_bound(1), 7 * n / 4)
54
- self.assertGreaterEqual(union.get_upper_bound(1), 7 * n / 4)
55
-
56
- # serialize for storage and reconstruct
57
- sk_bytes = result.serialize_compact()
58
- self.assertEqual(len(sk_bytes), result.get_compact_serialization_bytes())
59
- new_hll = hll_sketch.deserialize(sk_bytes)
60
-
61
- # the sketch can self-report its configuration and status
62
- self.assertEqual(new_hll.lg_config_k, lgk)
63
- self.assertEqual(new_hll.tgt_type, tgt_hll_type.HLL_4)
64
- self.assertFalse(new_hll.is_empty())
65
-
66
- # if we want to reduce some object overhead, we can also reset
67
- new_hll.reset()
68
- self.assertTrue(new_hll.is_empty())
69
-
70
- def test_hll_sketch(self):
71
- lgk = 8
72
- n = 117
73
- hll = self.generate_sketch(n, lgk, tgt_hll_type.HLL_6)
74
- hll.update('string data')
75
- hll.update(3.14159) # double data
76
-
77
- self.assertLessEqual(hll.get_lower_bound(1), hll.get_estimate())
78
- self.assertGreaterEqual(hll.get_upper_bound(1), hll.get_estimate())
79
-
80
- self.assertEqual(hll.lg_config_k, lgk)
81
- self.assertEqual(hll.tgt_type, tgt_hll_type.HLL_6)
82
-
83
- bytes_compact = hll.serialize_compact()
84
- bytes_update = hll.serialize_updatable()
85
- self.assertEqual(len(bytes_compact), hll.get_compact_serialization_bytes())
86
- self.assertEqual(len(bytes_update), hll.get_updatable_serialization_bytes())
87
-
88
- self.assertFalse(hll.is_compact())
89
- self.assertFalse(hll.is_empty())
90
-
91
- self.assertTrue(isinstance(hll_sketch.deserialize(bytes_compact), hll_sketch))
92
- self.assertTrue(isinstance(hll_sketch.deserialize(bytes_update), hll_sketch))
93
-
94
- self.assertIsNotNone(hll_sketch.get_rel_err(True, False, 12, 1))
95
- self.assertIsNotNone(hll_sketch.get_max_updatable_serialization_bytes(20, tgt_hll_type.HLL_6))
96
-
97
- hll.reset()
98
- self.assertTrue(hll.is_empty())
99
-
100
- def test_hll_union(self):
101
- lgk = 7
102
- n = 53
103
- union = hll_union(lgk)
104
-
105
- sk = self.generate_sketch(n, lgk, tgt_hll_type.HLL_4, 0)
106
- union.update(sk)
107
- sk = self.generate_sketch(3 * n, lgk, tgt_hll_type.HLL_4, n)
108
- union.update(sk)
109
- union.update('string data')
110
- union.update(1.4142136)
111
-
112
- self.assertLessEqual(union.get_lower_bound(1), union.get_estimate())
113
- self.assertGreaterEqual(union.get_upper_bound(1), union.get_estimate())
114
-
115
- self.assertEqual(union.lg_config_k, lgk)
116
- self.assertFalse(union.is_empty())
117
-
118
- sk = union.get_result()
119
- self.assertTrue(isinstance(sk, hll_sketch))
120
- self.assertEqual(sk.tgt_type, tgt_hll_type.HLL_4)
121
-
122
- def generate_sketch(self, n, lgk, sk_type=tgt_hll_type.HLL_4, st_idx=0):
123
- sk = hll_sketch(lgk, sk_type)
124
- for i in range(st_idx, st_idx + n):
125
- sk.update(i)
126
- return sk
127
-
128
- if __name__ == '__main__':
129
- unittest.main()
@@ -1,159 +0,0 @@
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 kll_ints_sketch, kll_floats_sketch, kll_doubles_sketch
20
- from datasketches import kll_items_sketch, ks_test, PyStringsSerDe
21
- import numpy as np
22
-
23
- class KllTest(unittest.TestCase):
24
- def test_kll_floats_example(self):
25
- k = 160
26
- n = 2 ** 20
27
-
28
- # create a sketch and inject ~1 million N(0,1) points as an array and as a single item
29
- kll = kll_floats_sketch(k)
30
- kll.update(np.random.normal(size=n-1))
31
- kll.update(0.0)
32
-
33
- # 0 should be near the median
34
- self.assertAlmostEqual(0.5, kll.get_rank(0.0), delta=0.035)
35
-
36
- # the median should be near 0
37
- self.assertAlmostEqual(0.0, kll.get_quantile(0.5), delta=0.035)
38
-
39
- # we also track the min/max independently from the rest of the data
40
- # which lets us know the full observed data range
41
- self.assertLessEqual(kll.get_min_value(), kll.get_quantile(0.01))
42
- self.assertLessEqual(0.0, kll.get_rank(kll.get_min_value()))
43
- self.assertGreaterEqual(kll.get_max_value(), kll.get_quantile(0.99))
44
- self.assertGreaterEqual(1.0, kll.get_rank(kll.get_max_value()))
45
-
46
- # we can also extract a list of values at a time,
47
- # here the values should give us something close to [-2, -1, 0, 1, 2].
48
- # then get the CDF, which will return something close to
49
- # the original values used in get_quantiles()
50
- # finally, can check the normalized rank error bound
51
- pts = kll.get_quantiles([0.0228, 0.1587, 0.5, 0.8413, 0.9772])
52
- cdf = kll.get_cdf(pts) # include 1.0 at end to account for all probability mass
53
- self.assertEqual(len(cdf), len(pts)+1)
54
- err = kll.normalized_rank_error(False)
55
- self.assertEqual(err, kll_floats_sketch.get_normalized_rank_error(k, False))
56
-
57
- # and a few basic queries about the sketch
58
- self.assertFalse(kll.is_empty())
59
- self.assertTrue(kll.is_estimation_mode())
60
- self.assertEqual(kll.get_n(), n)
61
- self.assertEqual(kll.get_k(), k)
62
- self.assertLess(kll.get_num_retained(), n)
63
-
64
- # merging itself will double the number of items the sketch has seen
65
- # but need to do that with a copy
66
- kll_copy = kll_floats_sketch(kll)
67
- kll.merge(kll_copy)
68
- self.assertEqual(kll.get_n(), 2*n)
69
-
70
- # we can then serialize and reconstruct the sketch
71
- kll_bytes = kll.serialize()
72
- new_kll = kll_floats_sketch.deserialize(kll_bytes)
73
- self.assertEqual(kll.get_num_retained(), new_kll.get_num_retained())
74
- self.assertEqual(kll.get_min_value(), new_kll.get_min_value())
75
- self.assertEqual(kll.get_max_value(), new_kll.get_max_value())
76
- self.assertEqual(kll.get_quantile(0.7), new_kll.get_quantile(0.7))
77
- self.assertEqual(kll.get_rank(0.0), new_kll.get_rank(0.0))
78
-
79
- # A Kolmogorov-Smirnov Test of kll and new_kll should match, even for
80
- # a fairly small p-value -- cannot reject the null hypothesis that
81
- # they come from the same distribution (since they do)
82
- self.assertFalse(ks_test(kll, new_kll, 0.001))
83
-
84
- total_weight = 0
85
- for tuple in kll:
86
- item = tuple[0]
87
- weight = tuple[1]
88
- total_weight = total_weight + weight
89
- self.assertEqual(total_weight, kll.get_n())
90
-
91
- def test_kll_ints_sketch(self):
92
- k = 100
93
- n = 10
94
- kll = kll_ints_sketch(k)
95
- for i in range(0, n):
96
- kll.update(i)
97
-
98
- self.assertEqual(kll.get_min_value(), 0)
99
- self.assertEqual(kll.get_max_value(), n-1)
100
- self.assertEqual(kll.get_n(), n)
101
- self.assertFalse(kll.is_empty())
102
- self.assertFalse(kll.is_estimation_mode()) # n < k
103
- self.assertEqual(kll.get_k(), k)
104
-
105
- pmf = kll.get_pmf([round(n/2)])
106
- self.assertIsNotNone(pmf)
107
- self.assertEqual(len(pmf), 2)
108
-
109
- cdf = kll.get_cdf([round(n/2)])
110
- self.assertIsNotNone(cdf)
111
- self.assertEqual(len(cdf), 2)
112
-
113
- self.assertEqual(kll.get_quantile(0.5), round(n/2))
114
- quants = kll.get_quantiles([0.25, 0.5, 0.75])
115
- self.assertIsNotNone(quants)
116
- self.assertEqual(len(quants), 3)
117
-
118
- self.assertEqual(kll.get_rank(round(n/2)), 0.5)
119
-
120
- # merge copy of self
121
- kll_copy = kll_ints_sketch(kll)
122
- kll.merge(kll_copy)
123
- self.assertEqual(kll.get_n(), 2 * n)
124
-
125
- sk_bytes = kll.serialize()
126
- self.assertTrue(isinstance(kll_ints_sketch.deserialize(sk_bytes), kll_ints_sketch))
127
-
128
- def test_kll_doubles_sketch(self):
129
- # already tested float and ints and it's templatized, so just make sure it instantiates properly
130
- k = 75
131
- kll = kll_doubles_sketch(k)
132
- self.assertTrue(kll.is_empty())
133
-
134
- def test_kll_items_sketch(self):
135
- # most functionality has been tested, but we need to ensure objects and sorting work
136
- # as well as serialization
137
- k = 100
138
- n = 2 ** 16
139
-
140
- # create a sketch and inject enough points to force compaction
141
- kll = kll_items_sketch(k)
142
- for i in range(0, n):
143
- kll.update(str(i))
144
-
145
- kll_copy = kll_items_sketch(kll)
146
- kll.merge(kll_copy)
147
- self.assertEqual(kll.get_n(), 2 * n)
148
-
149
- kll_bytes = kll.serialize(PyStringsSerDe())
150
- new_kll = kll_items_sketch.deserialize(kll_bytes, PyStringsSerDe())
151
- self.assertEqual(kll.get_num_retained(), new_kll.get_num_retained())
152
- self.assertEqual(kll.get_min_value(), new_kll.get_min_value())
153
- self.assertEqual(kll.get_max_value(), new_kll.get_max_value())
154
- self.assertEqual(kll.get_quantile(0.7), new_kll.get_quantile(0.7))
155
- self.assertEqual(kll.get_rank(str(n/4)), new_kll.get_rank(str(n/4)))
156
-
157
-
158
- if __name__ == '__main__':
159
- unittest.main()