You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by al...@apache.org on 2022/12/22 18:45:26 UTC

[datasketches-cpp] 01/01: added iterator, rearranged and simplified existing code

This is an automated email from the ASF dual-hosted git repository.

alsay pushed a commit to branch python_wrapper_improvement
in repository https://gitbox.apache.org/repos/asf/datasketches-cpp.git

commit 9fd500c0448ab7afe4bf32184f96ee78cdacc6a9
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Thu Dec 22 10:45:17 2022 -0800

    added iterator, rearranged and simplified existing code
---
 python/src/kll_wrapper.cpp | 255 ++++++++++++++++++++-------------------------
 python/tests/kll_test.py   |   6 ++
 2 files changed, 118 insertions(+), 143 deletions(-)

diff --git a/python/src/kll_wrapper.cpp b/python/src/kll_wrapper.cpp
index 4a9d0dd..0fc1ed8 100644
--- a/python/src/kll_wrapper.cpp
+++ b/python/src/kll_wrapper.cpp
@@ -17,97 +17,15 @@
  * under the License.
  */
 
-#include "kll_sketch.hpp"
-
 #include <pybind11/pybind11.h>
 #include <pybind11/stl.h>
 #include <pybind11/numpy.h>
-#include <sstream>
 #include <vector>
 #include <stdexcept>
 
-namespace py = pybind11;
-
-namespace datasketches {
-
-namespace python {
-
-template<typename T>
-kll_sketch<T> kll_sketch_deserialize(py::bytes skBytes) {
-  std::string skStr = skBytes; // implicit cast  
-  return kll_sketch<T>::deserialize(skStr.c_str(), skStr.length());
-}
-
-template<typename T>
-py::object kll_sketch_serialize(const kll_sketch<T>& sk) {
-  auto serResult = sk.serialize();
-  return py::bytes((char*)serResult.data(), serResult.size());
-}
-
-// maybe possible to disambiguate the static vs method rank error calls, but
-// this is easier for now
-template<typename T>
-double kll_sketch_generic_normalized_rank_error(uint16_t k, bool pmf) {
-  return kll_sketch<T>::get_normalized_rank_error(k, pmf);
-}
-
-template<typename T>
-py::list kll_sketch_get_quantiles(const kll_sketch<T>& sk,
-                                  std::vector<double>& ranks,
-                                  bool inclusive) {
-  size_t nQuantiles = ranks.size();
-  auto result = sk.get_quantiles(ranks.data(), nQuantiles, inclusive);
-  // returning as std::vector<> would copy values to a list anyway
-  py::list list(nQuantiles);
-  for (size_t i = 0; i < nQuantiles; ++i) {
-      list[i] = result[i];
-  }
-  return list;
-}
-
-template<typename T>
-py::list kll_sketch_get_pmf(const kll_sketch<T>& sk,
-                            std::vector<T>& split_points,
-                            bool inclusive) {
-  size_t nPoints = split_points.size();
-  auto result = sk.get_PMF(split_points.data(), nPoints, inclusive);
-  py::list list(nPoints + 1);
-  for (size_t i = 0; i <= nPoints; ++i) {
-    list[i] = result[i];
-  }
-  return list;
-}
-
-template<typename T>
-py::list kll_sketch_get_cdf(const kll_sketch<T>& sk,
-                            std::vector<T>& split_points,
-                            bool inclusive) {
-  size_t nPoints = split_points.size();
-  auto result = sk.get_CDF(split_points.data(), nPoints, inclusive);
-  py::list list(nPoints + 1);
-  for (size_t i = 0; i <= nPoints; ++i) {
-    list[i] = result[i];
-  }
-  return list;
-}
-
-template<typename T>
-void kll_sketch_update(kll_sketch<T>& sk, py::array_t<T, py::array::c_style | py::array::forcecast> items) {
-  if (items.ndim() != 1) {
-    throw std::invalid_argument("input data must have only one dimension. Found: "
-          + std::to_string(items.ndim()));
-  }
-  
-  auto data = items.template unchecked<1>();
-  for (uint32_t i = 0; i < data.size(); ++i) {
-    sk.update(data(i));
-  }
-}
-
-}
-}
+#include "kll_sketch.hpp"
 
-namespace dspy = datasketches::python;
+namespace py = pybind11;
 
 template<typename T>
 void bind_kll_sketch(py::module &m, const char* name) {
@@ -116,41 +34,62 @@ void bind_kll_sketch(py::module &m, const char* name) {
   py::class_<kll_sketch<T>>(m, name)
     .def(py::init<uint16_t>(), py::arg("k")=kll_constants::DEFAULT_K)
     .def(py::init<const kll_sketch<T>&>())
-    .def("update", (void (kll_sketch<T>::*)(const T&)) &kll_sketch<T>::update, py::arg("item"),
-         "Updates the sketch with the given value")
-    .def("update", &dspy::kll_sketch_update<T>, py::arg("array"),
-         "Updates the sketch with the values in the given array")
+    .def(
+        "update",
+        static_cast<void (kll_sketch<T>::*)(const T&)>(&kll_sketch<T>::update),
+        py::arg("item"),
+        "Updates the sketch with the given value"
+    )
+    .def(
+        "update",
+        [](kll_sketch<T>& sk, py::array_t<T, py::array::c_style | py::array::forcecast> items) {
+          if (items.ndim() != 1) {
+            throw std::invalid_argument("input data must have only one dimension. Found: "
+              + std::to_string(items.ndim()));
+          }
+          auto array = items.template unchecked<1>();
+          for (uint32_t i = 0; i < array.size(); ++i) sk.update(array(i));
+        },
+        py::arg("array"),
+        "Updates the sketch with the values in the given array"
+    )
     .def("merge", (void (kll_sketch<T>::*)(const kll_sketch<T>&)) &kll_sketch<T>::merge, py::arg("sketch"),
-         "Merges the provided sketch into the this one")
+        "Merges the provided sketch into this one")
     .def("__str__", &kll_sketch<T>::to_string, py::arg("print_levels")=false, py::arg("print_items")=false,
-         "Produces a string summary of the sketch")
+        "Produces a string summary of the sketch")
     .def("to_string", &kll_sketch<T>::to_string, py::arg("print_levels")=false, py::arg("print_items")=false,
-         "Produces a string summary of the sketch")
+        "Produces a string summary of the sketch")
     .def("is_empty", &kll_sketch<T>::is_empty,
-         "Returns True if the sketch is empty, otherwise False")
+        "Returns True if the sketch is empty, otherwise False")
     .def("get_k", &kll_sketch<T>::get_k,
-         "Returns the configured parameter k")
+        "Returns the configured parameter k")
     .def("get_n", &kll_sketch<T>::get_n,
-         "Returns the length of the input stream")
+        "Returns the length of the input stream")
     .def("get_num_retained", &kll_sketch<T>::get_num_retained,
-         "Returns the number of retained items (samples) in the sketch")
+        "Returns the number of retained items (samples) in the sketch")
     .def("is_estimation_mode", &kll_sketch<T>::is_estimation_mode,
-         "Returns True if the sketch is in estimation mode, otherwise False")
+        "Returns True if the sketch is in estimation mode, otherwise False")
     .def("get_min_value", &kll_sketch<T>::get_min_item,
-         "Returns the minimum value from the stream. If empty, kll_floats_sketch returns nan; kll_ints_sketch throws a RuntimeError")
+        "Returns the minimum value from the stream. If empty, kll_floats_sketch returns nan; kll_ints_sketch throws a RuntimeError")
     .def("get_max_value", &kll_sketch<T>::get_max_item,
-         "Returns the maximum value from the stream. If empty, kll_floats_sketch returns nan; kll_ints_sketch throws a RuntimeError")
+        "Returns the maximum value from the stream. If empty, kll_floats_sketch returns nan; kll_ints_sketch throws a RuntimeError")
     .def("get_quantile", &kll_sketch<T>::get_quantile, py::arg("rank"), py::arg("inclusive")=false,
-         "Returns an approximation to the data value "
-         "associated with the given normalized rank in a hypothetical sorted "
-         "version of the input stream so far.\n"
-         "For kll_floats_sketch: if the sketch is empty this returns nan. "
-         "For kll_ints_sketch: if the sketch is empty this throws a RuntimeError.")
-    .def("get_quantiles", &dspy::kll_sketch_get_quantiles<T>, py::arg("ranks"), py::arg("inclusive")=false,
-         "This returns an array that could have been generated by using get_quantile() for each "
-         "normalized rank separately.\n"
-         "If the sketch is empty this returns an empty vector.\n"
-         "Deprecated. Will be removed in the next major version. Use get_quantile() instead.")
+        "Returns an approximation to the data value "
+        "associated with the given normalized rank in a hypothetical sorted "
+        "version of the input stream so far.\n"
+        "For kll_floats_sketch: if the sketch is empty this returns nan. "
+        "For kll_ints_sketch: if the sketch is empty this throws a RuntimeError.")
+    .def(
+        "get_quantiles",
+        [](const kll_sketch<T>& sk, const std::vector<double>& ranks, bool inclusive) {
+          return sk.get_quantiles(ranks.data(), ranks.size(), inclusive);
+        },
+        py::arg("ranks"), py::arg("inclusive")=false,
+        "This returns an array that could have been generated by using get_quantile() for each "
+        "normalized rank separately.\n"
+        "If the sketch is empty this returns an empty vector.\n"
+        "Deprecated. Will be removed in the next major version. Use get_quantile() instead."
+    )
     .def("get_rank", &kll_sketch<T>::get_rank, py::arg("value"), py::arg("inclusive")=false,
          "Returns an approximation to the normalized rank of the given value from 0 to 1, inclusive.\n"
          "The resulting approximation has a probabilistic guarantee that can be obtained from the "
@@ -158,49 +97,79 @@ void bind_kll_sketch(py::module &m, const char* name) {
          "With the parameter inclusive=true the weight of the given value is included into the rank."
          "Otherwise the rank equals the sum of the weights of values less than the given value.\n"
          "If the sketch is empty this returns nan.")
-    .def("get_pmf", &dspy::kll_sketch_get_pmf<T>, py::arg("split_points"), py::arg("inclusive")=false,
-         "Returns an approximation to the Probability Mass Function (PMF) of the input stream "
-         "given a set of split points (values).\n"
-         "The resulting approximations have a probabilistic guarantee that can be obtained from the "
-         "get_normalized_rank_error(True) function.\n"
-         "If the sketch is empty this returns an empty vector.\n"
-         "split_points is an array of m unique, monotonically increasing float values "
-         "that divide the real number line into m+1 consecutive disjoint intervals.\n"
-         "If the parameter inclusive=false, the definition of an 'interval' is inclusive of the left split point (or minimum value) and "
-         "exclusive of the right split point, with the exception that the last interval will include "
-         "the maximum value.\n"
-         "If the parameter inclusive=true, the definition of an 'interval' is exclusive of the left split point (or minimum value) and "
-         "inclusive of the right split point.\n"
-         "It is not necessary to include either the min or max values in these split points.")
-    .def("get_cdf", &dspy::kll_sketch_get_cdf<T>, py::arg("split_points"), py::arg("inclusive")=false,
-         "Returns an approximation to the Cumulative Distribution Function (CDF), which is the "
-         "cumulative analog of the PMF, of the input stream given a set of split points (values).\n"
-         "The resulting approximations have a probabilistic guarantee that can be obtained from the "
-         "get_normalized_rank_error(True) function.\n"
-         "If the sketch is empty this returns an empty vector.\n"
-         "split_points is an array of m unique, monotonically increasing float values "
-         "that divide the real number line into m+1 consecutive disjoint intervals.\n"
-         "If the parameter inclusive=false, the definition of an 'interval' is inclusive of the left split point (or minimum value) and "
-         "exclusive of the right split point, with the exception that the last interval will include "
-         "the maximum value.\n"
-         "If the parameter inclusive=true, the definition of an 'interval' is exclusive of the left split point (or minimum value) and "
-         "inclusive of the right split point.\n"
-         "It is not necessary to include either the min or max values in these split points.")
-    .def("normalized_rank_error", (double (kll_sketch<T>::*)(bool) const) &kll_sketch<T>::get_normalized_rank_error,
+    .def(
+        "get_pmf",
+        [](const kll_sketch<T>& sk, const std::vector<T>& split_points, bool inclusive) {
+          return sk.get_PMF(split_points.data(), split_points.size(), inclusive);
+        },
+        py::arg("split_points"), py::arg("inclusive")=false,
+        "Returns an approximation to the Probability Mass Function (PMF) of the input stream "
+        "given a set of split points (values).\n"
+        "The resulting approximations have a probabilistic guarantee that can be obtained from the "
+        "get_normalized_rank_error(True) function.\n"
+        "If the sketch is empty this returns an empty vector.\n"
+        "split_points is an array of m unique, monotonically increasing float values "
+        "that divide the real number line into m+1 consecutive disjoint intervals.\n"
+        "If the parameter inclusive=false, the definition of an 'interval' is inclusive of the left split point (or minimum value) and "
+        "exclusive of the right split point, with the exception that the last interval will include "
+        "the maximum value.\n"
+        "If the parameter inclusive=true, the definition of an 'interval' is exclusive of the left split point (or minimum value) and "
+        "inclusive of the right split point.\n"
+        "It is not necessary to include either the min or max values in these split points."
+    )
+    .def(
+        "get_cdf",
+        [](const kll_sketch<T>& sk, const std::vector<T>& split_points, bool inclusive) {
+          return sk.get_CDF(split_points.data(), split_points.size(), inclusive);
+        },
+        py::arg("split_points"), py::arg("inclusive")=false,
+        "Returns an approximation to the Cumulative Distribution Function (CDF), which is the "
+        "cumulative analog of the PMF, of the input stream given a set of split points (values).\n"
+        "The resulting approximations have a probabilistic guarantee that can be obtained from the "
+        "get_normalized_rank_error(True) function.\n"
+        "If the sketch is empty this returns an empty vector.\n"
+        "split_points is an array of m unique, monotonically increasing float values "
+        "that divide the real number line into m+1 consecutive disjoint intervals.\n"
+        "If the parameter inclusive=false, the definition of an 'interval' is inclusive of the left split point (or minimum value) and "
+        "exclusive of the right split point, with the exception that the last interval will include "
+        "the maximum value.\n"
+        "If the parameter inclusive=true, the definition of an 'interval' is exclusive of the left split point (or minimum value) and "
+        "inclusive of the right split point.\n"
+        "It is not necessary to include either the min or max values in these split points."
+    )
+    .def(
+        "normalized_rank_error",
+        static_cast<double (kll_sketch<T>::*)(bool) const>(&kll_sketch<T>::get_normalized_rank_error),
          py::arg("as_pmf"),
          "Gets the normalized rank error for this sketch.\n"
          "If pmf is True, returns the 'double-sided' normalized rank error for the get_PMF() function.\n"
          "Otherwise, it is the 'single-sided' normalized rank error for all the other queries.\n"
-         "Constants were derived as the best fit to 99 percentile empirically measured max error in thousands of trials")
-    .def_static("get_normalized_rank_error", &dspy::kll_sketch_generic_normalized_rank_error<T>,
+         "Constants were derived as the best fit to 99 percentile empirically measured max error in thousands of trials"
+    )
+    .def_static(
+        "get_normalized_rank_error",
+        [](uint16_t k, bool pmf) { return kll_sketch<T>::get_normalized_rank_error(k, pmf); },
          py::arg("k"), py::arg("as_pmf"),
          "Gets the normalized rank error given parameters k and the pmf flag.\n"
          "If pmf is True, returns the 'double-sided' normalized rank error for the get_PMF() function.\n"
          "Otherwise, it is the 'single-sided' normalized rank error for all the other queries.\n"
-         "Constants were derived as the best fit to 99 percentile empirically measured max error in thousands of trials")
-    .def("serialize", &dspy::kll_sketch_serialize<T>, "Serializes the sketch into a bytes object")
-    .def_static("deserialize", &dspy::kll_sketch_deserialize<T>, "Deserializes the sketch from a bytes object")
-    ;
+         "Constants were derived as the best fit to 99 percentile empirically measured max error in thousands of trials"
+    )
+    .def(
+        "serialize",
+        [](const kll_sketch<T>& sk) {
+          auto bytes = sk.serialize();
+          return py::bytes(reinterpret_cast<const char*>(bytes.data()), bytes.size());
+        },
+        "Serializes the sketch into a bytes object"
+    )
+    .def_static(
+        "deserialize",
+        [](const std::string& bytes) { return kll_sketch<T>::deserialize(bytes.data(), bytes.size()); },
+        py::arg("bytes"),
+        "Deserializes the sketch from a bytes object"
+    )
+    .def("__iter__", [](const kll_sketch<T>& s) { return py::make_iterator(s.begin(), s.end()); });
 }
 
 void init_kll(py::module &m) {
diff --git a/python/tests/kll_test.py b/python/tests/kll_test.py
index 0f7168b..da8751d 100644
--- a/python/tests/kll_test.py
+++ b/python/tests/kll_test.py
@@ -78,6 +78,12 @@ class KllTest(unittest.TestCase):
       # they come from the same distribution (since they do)
       self.assertFalse(ks_test(kll, new_kll, 0.001))
 
+      total_weight = 0
+      for tuple in kll:
+        item = tuple[0]
+        weight = tuple[1]
+        total_weight = total_weight + weight
+      self.assertEqual(total_weight, kll.get_n())
 
     def test_kll_ints_sketch(self):
         k = 100


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org