Skip to content

Commit 9dc7e7c

Browse files
committed
Allow compression for theta sketches
1 parent a8734e4 commit 9dc7e7c

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/theta_wrapper.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ void init_theta(nb::module_ &m) {
104104
.def("__copy__", [](const compact_theta_sketch& sk){ return compact_theta_sketch(sk); })
105105
.def(
106106
"serialize",
107-
[](const compact_theta_sketch& sk) {
108-
auto bytes = sk.serialize();
107+
[](const compact_theta_sketch& sk, bool compress) {
108+
auto bytes = compress ? sk.serialize_compressed() : sk.serialize();
109109
return nb::bytes(reinterpret_cast<const char*>(bytes.data()), bytes.size());
110-
},
111-
"Serializes the sketch into a bytes object"
110+
}, nb::arg("compress")=false,
111+
"Serializes the sketch into a bytes object, optionally compressing the data"
112112
)
113113
.def_static(
114114
"deserialize",

tests/theta_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ def test_theta_basic_example(self):
4848
self.assertFalse(sk.is_empty())
4949
self.assertEqual(sk.get_estimate(), new_sk.get_estimate())
5050

51+
# can also serialze in a compressed format
52+
sk_compresed_bytes = sk.compact().serialize(compress=True)
53+
self.assertLess(len(sk_compresed_bytes), len(sk_bytes))
54+
sk_from_compressed = compact_theta_sketch.deserialize(sk_compresed_bytes)
55+
56+
# compressed and non-compressed sketches should match
57+
self.assertEqual(sk_from_compressed.get_estimate(), new_sk.get_estimate())
58+
self.assertEqual(sk_from_compressed.get_upper_bound(1), new_sk.get_upper_bound(1))
59+
self.assertEqual(sk_from_compressed.get_lower_bound(1), new_sk.get_lower_bound(1))
60+
5161
# check that printing works as expected
5262
self.assertGreater(len(sk.to_string(True)), 0)
5363
self.assertEqual(len(sk.__str__()), len(sk.to_string()))

0 commit comments

Comments
 (0)