/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ #include #include #include #include #include "kernel_function.hpp" #include "density_sketch.hpp" namespace py = pybind11; template void bind_density_sketch(py::module &m, const char* name) { using namespace datasketches; py::class_>(m, name) .def( py::init([](uint16_t k, uint32_t dim, std::shared_ptr kernel) { kernel_function_holder holder(kernel); return density_sketch(k, dim, holder); }), py::arg("k"), py::arg("dim"), py::arg("kernel")) .def("update", static_cast::*)(const std::vector&)>(&density_sketch::update), "Updates the sketch with the given vector") .def("merge", static_cast::*)(const density_sketch&)>(&density_sketch::merge), py::arg("sketch"), "Merges the provided sketch into this one") .def("is_empty", &density_sketch::is_empty, "Returns True if the sketch is empty, otherwise False") .def("get_k", &density_sketch::get_k, "Returns the configured parameter k") .def("get_dim", &density_sketch::get_dim, "Returns the configured parameter dim") .def("get_n", &density_sketch::get_n, "Returns the length of the input stream") .def("get_num_retained", &density_sketch::get_num_retained, "Returns the number of retained items (samples) in the sketch") .def("is_estimation_mode", &density_sketch::is_estimation_mode, "Returns True if the sketch is in estimation mode, otherwise False") .def("get_estimate", &density_sketch::get_estimate, py::arg("point"), "Returns an approximate density at the given point") .def("__str__", &density_sketch::to_string, py::arg("print_levels")=false, py::arg("print_items")=false, "Produces a string summary of the sketch") .def("to_string", &density_sketch::to_string, py::arg("print_levels")=false, py::arg("print_items")=false, "Produces a string summary of the sketch") .def("__iter__", [](const density_sketch& s){ return py::make_iterator(s.begin(), s.end()); }) .def("serialize", [](const density_sketch& sk) { auto bytes = sk.serialize(); return py::bytes(reinterpret_cast(bytes.data()), bytes.size()); }, "Serializes the sketch into a bytes object" ) .def_static( "deserialize", [](const std::string& bytes, std::shared_ptr kernel) { kernel_function_holder holder(kernel); return density_sketch::deserialize(bytes.data(), bytes.size(), holder); }, py::arg("bytes"), py::arg("kernel"), "Reads a bytes object and returns the corresponding density_sketch" );; } void init_density(py::module &m) { using namespace datasketches; // generic kernel function py::class_>(m, "KernelFunction") .def(py::init()) .def("__call__", &kernel_function::operator(), py::arg("a"), py::arg("b")) ; // the old sketch names can almost be defined, but the kernel_function_holder won't work in init() //bind_density_sketch>(m, "density_floats_sketch"); //bind_density_sketch>(m, "density_doubles_sketch"); bind_density_sketch(m, "_density_sketch"); }