Пример #1
0
void addArrayAccessor2Scalar(py::module m, const char* name) {
    py::class_<ArrayAccessor2<T>>(m, name, py::buffer_protocol())
            .def_buffer([](ArrayAccessor2<T>& m) -> py::buffer_info {
                return py::buffer_info(m.data(), sizeof(T),
                                       py::format_descriptor<T>::format(), 2,
                                       {m.height(), m.width()}, {sizeof(T) * m.width(), sizeof(T)});
            });
};
Пример #2
0
void addArrayAccessor1Vector(py::module m, const char* name) {
    py::class_<ArrayAccessor1<Vector<T, N>>>(m, name, py::buffer_protocol())
        .def_buffer([](ArrayAccessor1<Vector<T, N>>& m) -> py::buffer_info {
            return py::buffer_info(m.data(), sizeof(T),
                                   py::format_descriptor<T>::format(), 2,
                                   {m.size(), N}, {sizeof(T) * N, sizeof(T)});
        });
};
Пример #3
0
void metapy_bind_stats(py::module& m)
{
    auto m_stats = m.def_submodule("stats");

    py::class_<py_multinomial>{m_stats, "Multinomial"}
        .def("increment", &py_multinomial::increment)
        .def("decrement", &py_multinomial::decrement)
        .def("counts", [](const py_multinomial& dist,
                          py::object obj) { return dist.counts(obj); })
        .def("counts", [](const py_multinomial& dist) { return dist.counts(); })
        .def("unique_events", &py_multinomial::unique_events)
        .def("each_seen_event", &py_multinomial::each_seen_event)
        .def("clear", &py_multinomial::clear)
        .def("probability", &py_multinomial::probability)
        .def("__repr__", [](const py_multinomial& mult) {
            const auto size = mult.unique_events();
            uint64_t i = 0;
            std::string result = "<metapy.stats.Multinomial {";
            mult.each_seen_event([&](const py::object& obj) {
                result += obj.attr("__repr__")().cast<std::string>();
                result += ": ";
                result += std::to_string(mult.probability(obj));
                if (++i != size)
                    result += ", ";
            });
            result += "}>";
            return result;
        });
}
Пример #4
0
void init_pykmstest(py::module &m)
{
    py::class_<RGB>(m, "RGB")
    .def(py::init<>())
    .def(py::init<uint8_t, uint8_t, uint8_t&>())
    .def(py::init<uint8_t, uint8_t, uint8_t, uint8_t&>())
    .def_property_readonly("rgb888", &RGB::rgb888)
    .def_property_readonly("argb8888", &RGB::argb8888)
    .def_property_readonly("abgr8888", &RGB::abgr8888)
    .def_property_readonly("rgb565", &RGB::rgb565)
    ;

    py::class_<ResourceManager>(m, "ResourceManager")
    .def(py::init<Card&>())
    .def("reset", &ResourceManager::reset)
    .def("reserve_connector", &ResourceManager::reserve_connector,
         py::arg("name") = string())
    .def("reserve_crtc", &ResourceManager::reserve_crtc)
    .def("reserve_plane", &ResourceManager::reserve_plane,
         py::arg("crtc"),
         py::arg("type"),
         py::arg("format") = PixelFormat::Undefined)
    .def("reserve_primary_plane", &ResourceManager::reserve_primary_plane,
         py::arg("crtc"),
         py::arg("format") = PixelFormat::Undefined)
    .def("reserve_overlay_plane", &ResourceManager::reserve_overlay_plane,
         py::arg("crtc"),
         py::arg("format") = PixelFormat::Undefined)
    ;

    // Use lambdas to handle IMappedFramebuffer
    m.def("draw_test_pattern", [](MappedFramebuffer& fb) {
        draw_test_pattern(fb);
    } );
    m.def("draw_color_bar", [](MappedFramebuffer& fb, int old_xpos, int xpos, int width) {
        draw_color_bar(fb, old_xpos, xpos, width);
    } );
    m.def("draw_rect", [](MappedFramebuffer& fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color) {
        draw_rect(fb, x, y, w, h, color);
    } );
}
Пример #5
0
void addConstants(py::module& m) {
    m.attr("DIRECTION_NONE") = py::int_(kDirectionNone);
    m.attr("DIRECTION_LEFT") = py::int_(kDirectionLeft);
    m.attr("DIRECTION_RIGHT") = py::int_(kDirectionRight);
    m.attr("DIRECTION_DOWN") = py::int_(kDirectionDown);
    m.attr("DIRECTION_UP") = py::int_(kDirectionUp);
    m.attr("DIRECTION_BACK") = py::int_(kDirectionBack);
    m.attr("DIRECTION_FRONT") = py::int_(kDirectionFront);
    m.attr("DIRECTION_ALL") = py::int_(kDirectionAll);
}
Пример #6
0
void bind_numpy_returns(py::module &m)
{
    m.def("load_rgb_image", &load_rgb_image, 
	"Takes a path and returns a numpy array (RGB) containing the image",
	py::arg("path")
    );

    m.def("save_image", &save_image<rgb_pixel>, 
	"Saves the given image to the specified path. Determines the file type from the file extension specified in the path",
	py::arg("img"), py::arg("path")
    );
    m.def("save_image", &save_image<unsigned char>, 
	"Saves the given image to the specified path. Determines the file type from the file extension specified in the path",
	py::arg("img"), py::arg("path")
    );

    m.def("jitter_image", &get_jitter_images, 
    "Takes an image and returns a list of jittered images."
    "The returned list contains num_jitters images (default is 1)."
    "If disturb_colors is set to True, the colors of the image are disturbed (default is False)", 
    py::arg("img"), py::arg("num_jitters")=1, py::arg("disturb_colors")=false
    );

    m.def("get_face_chip", &get_face_chip, 
	"Takes an image and a full_object_detection that references a face in that image and returns the face as a Numpy array representing the image.  The face will be rotated upright and scaled to 150x150 pixels or with the optional specified size and padding.", 
	py::arg("img"), py::arg("face"), py::arg("size")=150, py::arg("padding")=0.25
    );

    m.def("get_face_chips", &get_face_chips, 
	"Takes an image and a full_object_detections object that reference faces in that image and returns the faces as a list of Numpy arrays representing the image.  The faces will be rotated upright and scaled to 150x150 pixels or with the optional specified size and padding.",
	py::arg("img"), py::arg("faces"), py::arg("size")=150, py::arg("padding")=0.25
    );
}
Пример #7
0
void for_python_result_set(pybind11::module & module)
{
	// expose base result set with explicit holder class to allow passing of
	// shared pointer arguments
	pybind11::class_<base_result_set, std::shared_ptr<base_result_set>>(module, "BaseResultSet");

	pybind11::class_<python_result_set>(module, "ResultSet")
			.def("get_column_info", &python_result_set::get_column_info)
			.def("fetch_row", &python_result_set::fetch_row)
		;

	module.def("make_row_based_result_set", make_python_result_set);
}
Пример #8
0
void export_common(py::module& m)
{
  py::enum_<sc::DType>(m, "dtype")
      .value("float32", sc::DType::FLOAT_TYPE)
      .value("float64", sc::DType::DOUBLE_TYPE)
      .export_values();

  m.def("size_of", sc::size_of);

  py::class_<sc::scalar>(m, "Scalar")
      .def(py::init<float, sc::DType>())
      .def(py::init<double, sc::DType>())
      .def_property_readonly("dtype", &sc::scalar::dtype);
}
Пример #9
0
void bind_svm_rank_trainer(py::module& m)
{
    py::class_<ranking_pair<sample_type> >(m, "ranking_pair")
        .def(py::init())
        .def_readwrite("relevant", &ranking_pair<sample_type>::relevant)
        .def_readwrite("nonrelevant", &ranking_pair<sample_type>::nonrelevant)
        .def(py::pickle(&getstate<ranking_pair<sample_type>>, &setstate<ranking_pair<sample_type>>));

    py::class_<ranking_pair<sparse_vect> >(m, "sparse_ranking_pair")
        .def(py::init())
        .def_readwrite("relevant", &ranking_pair<sparse_vect>::relevant)
        .def_readwrite("nonrelevant", &ranking_pair<sparse_vect>::nonrelevant)
        .def(py::pickle(&getstate<ranking_pair<sparse_vect>>, &setstate<ranking_pair<sparse_vect>>));

    py::bind_vector<ranking_pairs>(m, "ranking_pairs")
        .def("clear", &ranking_pairs::clear)
        .def("resize", resize<ranking_pairs>)
        .def("extend", extend_vector_with_python_list<ranking_pair<sample_type>>)
        .def(py::pickle(&getstate<ranking_pairs>, &setstate<ranking_pairs>));

    py::bind_vector<sparse_ranking_pairs>(m, "sparse_ranking_pairs")
        .def("clear", &sparse_ranking_pairs::clear)
        .def("resize", resize<sparse_ranking_pairs>)
        .def("extend", extend_vector_with_python_list<ranking_pair<sparse_vect>>)
        .def(py::pickle(&getstate<sparse_ranking_pairs>, &setstate<sparse_ranking_pairs>));

    add_ranker<svm_rank_trainer<linear_kernel<sample_type> > >(m, "svm_rank_trainer");
    add_ranker<svm_rank_trainer<sparse_linear_kernel<sparse_vect> > >(m, "svm_rank_trainer_sparse");

    m.def("cross_validate_ranking_trainer", &_cross_ranking_validate_trainer<
                svm_rank_trainer<linear_kernel<sample_type> >,sample_type>,
                py::arg("trainer"), py::arg("samples"), py::arg("folds") );
    m.def("cross_validate_ranking_trainer", &_cross_ranking_validate_trainer<
                svm_rank_trainer<sparse_linear_kernel<sparse_vect> > ,sparse_vect>,
                py::arg("trainer"), py::arg("samples"), py::arg("folds") );
}
void wrapper_95d0d4d7e8215bf98789264a4aeb8c71(pybind11::module& module)
{

    module.def("set_seed", function_pointer_95d0d4d7e8215bf98789264a4aeb8c71, "");
}
Пример #11
0
void exportPyLazyCaller(py::module m)
{
    m.def("callLater", [](py::function func){ cnoid::callLater(PyFunc(func)); });
    m.def("callSynchronously", [](py::function func){ cnoid::callSynchronously(PyFunc(func)); });
}
Пример #12
0
void fifi_utils(pybind11::module& m, const std::string& field)
{
    using namespace pybind11;

    m.def((field + std::string("_elements_to_length")).c_str(),
          &fifi::elements_to_length<Field>,
          arg("elements"),
          "Returns the number of value_type elements needed to store a "
          "certain number of field elements.\n\n"
          "elemenets.\n\n"
          "\t:param elements: The number of elements.\n"
          "\t:return: The number of value_type elements needed.\n");
    m.def((field + std::string("_elements_to_size")).c_str(),
          &fifi::elements_to_size<Field>,
          arg("elements"),
          "Returns the minimum size in bytes required to accommodate a "
          "certain number of field elements.\n\n"
          "\t:param elements: the number of field elements.\n"
          "\t:return: the size in bytes needed to store the field "
          "elements.\n");
    m.def((field + std::string("_size_to_length")).c_str(),
          &fifi::size_to_length<Field>, arg("bytes"),
          "Returns the number of value_type elements needed to store "
          "a certain number of bytes.\n\n"
          "\t:param bytes: the number of bytes to store.\n"
          "\t:return: the number of value_type elements that need to be "
          "stored.\n");
    m.def((field + std::string("_length_to_size")).c_str(),
          &fifi::length_to_size<Field>, arg("length"),
          "Returns the size in bytes needed to store a certain "
          "number of value_type elements.\n\n"
          "\t:param length: the number of value_type elements to store.\n"
          "\t:return: the size in bytes needed to store the value_type "
          "elements.\n");
    m.def((field + std::string("_length_to_elements")).c_str(),
          &fifi::length_to_elements<Field>,
          arg("length"),
          "Returns the number of field elements needed to store a certain "
          "number of value_type elements.\n\n"
          "\t:param length: the number of value_type elements.\n"
          "\t:return: the number of field elements needed.\n");
    m.def((field + std::string("_size_to_elements")).c_str(),
          &fifi::size_to_elements<Field>, arg("bytes"),
          "Returns the number of field elements that can fit within a "
          "certain number of bytes.\n\n"
          "\t:param bytes: the number of bytes to store the field elements.\n"
          "\t:return: the number of field elements stored within the bytes.\n");
    m.def((field + std::string("_get_value")).c_str(),
          &get_value<Field>,
          arg("elements"), arg("index"),
          "Useful abstraction function for accessing field elements in a "
          "buffer. Note this function assumes that values are packed.\n\n"
          "\t:param elements: elements to get value from.\n"
          "\t:param index: index of element to access in the packed buffer.\n"
          "\t:return: the value of the element at specified index.\n");
    m.def((field + std::string("_set_value")).c_str(),
          &set_value<Field>,
          arg("elements"), arg("index"), arg("value"),
          "Useful abstraction function for assigning field elements in a "
          "buffer a specific value. Note this function assumes that values "
          "are packed.\n\n"
          "\t:param elements: elements to manipulate.\n"
          "\t:param index: index of element.\n"
          "\t:param value: value to assign element.\n"
          "\t:return: The modified buffer.\n");
    m.def((field + std::string("_pack_constant")).c_str(),
          &fifi::pack_constant<Field>, arg("constant"),
          "Helper function for creating packed constants. In Fifi we refer "
          "to values as packed if they utilize the entire underlying data "
          "type. As an example binary4 uses four bits for every field "
          "element but since no 4-bit data types are available we store it's "
          "value in an uint8_t. When using APIs requiring packed data we "
          "therefore have to pack the constant which ensures that both the "
          "high an low 4 bits have the right constant value (in the case of "
          "binary4).\n\n"
          "\t:param constant: The constant to pack.\n"
          "\t:return: The packed constant.\n");
}
void wrapper_cfd7d1ec1fbc514b9feba31d16e55cf6(pybind11::module& module)
{

    module.def("get_nn", function_pointer_cfd7d1ec1fbc514b9feba31d16e55cf6, pybind11::return_value_policy::copy, "");
}
Пример #14
0
void install_toolmain(pybind11::module& m)
{
    m.def("toolmain", &toolmain);
}
void wrapper_1a16c32a3d8f5d01b8d739fb757db381(pybind11::module& module)
{

    module.def("set_seed", function_pointer_1a16c32a3d8f5d01b8d739fb757db381, "");
}
Пример #16
0
void metapy_bind_topics(py::module& m)
{
    auto m_topics = m.def_submodule("topics");

    py::class_<topics::lda_model>{m_topics, "LDAModel"}
        .def("run",
             [](topics::lda_model& model, uint64_t num_iters,
                double convergence) {
                 py::gil_scoped_release release;
                 model.run(num_iters, convergence);
             })
        .def("save_doc_topic_distributions",
             [](const topics::lda_model& model, const std::string& filename) {
                 std::ofstream output{filename, std::ios::binary};
                 model.save_doc_topic_distributions(output);
             })
        .def("save_topic_term_distributions",
             [](const topics::lda_model& model, const std::string& filename) {
                 std::ofstream output{filename, std::ios::binary};
                 model.save_topic_term_distributions(output);
             })
        .def("save", &topics::lda_model::save)
        .def("compute_term_topic_probability",
             &topics::lda_model::compute_term_topic_probability)
        .def("compute_doc_topic_probability",
             &topics::lda_model::compute_doc_topic_probability)
        .def("topic_distribution",
             [](const topics::lda_model& model, doc_id doc) {
                 return py_multinomial{model.topic_distribution(doc)};
             })
        .def("term_distribution",
             [](const topics::lda_model& model, topic_id k) {
                 return py_multinomial{model.term_distribution(k)};
             })
        .def("num_topics", &topics::lda_model::num_topics);

    py::class_<topics::inferencer>{m_topics, "LDAInferencer"}
        .def("term_distribution",
             [](const topics::inferencer& inf, topic_id k) {
                 return py_multinomial{inf.term_distribution(k)};
             },
             py::arg("k"))
        .def("num_topics", &topics::inferencer::num_topics);

    py::class_<topics::lda_cvb, topics::lda_model>{m_topics, "LDACollapsedVB"}
        .def(py::init<const learn::dataset&, std::size_t, double, double>(),
             py::keep_alive<0, 1>(), py::arg("docs"), py::arg("num_topics"),
             py::arg("alpha"), py::arg("beta"))
        .def("run",
             [](topics::lda_cvb& lda, uint64_t num_iters, double convergence) {
                 py::gil_scoped_release release;
                 lda.run(num_iters, convergence);
             },
             py::arg("num_iters"), py::arg("convergence") = 1e-3);

    py::class_<topics::lda_cvb::inferencer, topics::inferencer>{m_topics,
                                                                "CVBInferencer"}
        .def("__init__",
             [](topics::inferencer& inf, const std::string& cfgfile) {
                 py::gil_scoped_release release;
                 auto config = cpptoml::parse_file(cfgfile);
                 new (&inf) topics::inferencer(*config);
             },
             py::arg("cfg_file"))
        .def("__init__",
             [](topics::inferencer& inf, const std::string& topicsfile,
                double alpha) {
                 py::gil_scoped_release release;
                 std::ifstream topics_stream{topicsfile, std::ios::binary};
                 new (&inf) topics::inferencer(topics_stream, alpha);
             },
             py::arg("topics_file"), py::arg("alpha"))
        .def("infer",
             [](const topics::lda_cvb::inferencer& inf,
                const learn::feature_vector& doc, std::size_t max_iters,
                double convergence) {
                 return py_multinomial{inf(doc, max_iters, convergence)};
             },
             py::arg("doc"), py::arg("max_iters"), py::arg("convergence"));

    py::class_<topics::lda_gibbs, topics::lda_model>{m_topics, "LDAGibbs"}
        .def(py::init<const learn::dataset&, std::size_t, double, double>(),
             py::keep_alive<0, 1>(), py::arg("docs"), py::arg("num_topics"),
             py::arg("alpha"), py::arg("beta"))
        .def(
            "run",
            [](topics::lda_gibbs& lda, uint64_t num_iters, double convergence) {
                py::gil_scoped_release release;
                lda.run(num_iters, convergence);
            },
            py::arg("num_iters"), py::arg("convergence") = 1e-6);

    py::class_<topics::lda_gibbs::inferencer, topics::inferencer>{
        m_topics, "GibbsInferencer"}
        .def("__init__",
             [](topics::inferencer& inf, const std::string& cfgfile) {
                 py::gil_scoped_release release;
                 auto config = cpptoml::parse_file(cfgfile);
                 new (&inf) topics::inferencer(*config);
             },
             py::arg("cfg_file"))
        .def("__init__",
             [](topics::inferencer& inf, const std::string& topicsfile,
                double alpha) {
                 py::gil_scoped_release release;
                 std::ifstream topics_stream{topicsfile, std::ios::binary};
                 new (&inf) topics::inferencer(topics_stream, alpha);
             },
             py::arg("topics_file"), py::arg("alpha"))

        .def("infer",
             [](const topics::lda_gibbs::inferencer& inf,
                const learn::feature_vector& doc, std::size_t num_iters,
                std::size_t seed) {
                 random::xoroshiro128 rng{seed};
                 return py_multinomial{inf(doc, num_iters, rng)};
             },
             py::arg("doc"), py::arg("max_iters"), py::arg("rng_seed"));

    py::class_<topics::parallel_lda_gibbs, topics::lda_gibbs>{
        m_topics, "LDAParallelGibbs"}
        .def(py::init<const learn::dataset&, std::size_t, double, double>(),
             py::keep_alive<0, 1>(), py::arg("docs"), py::arg("num_topics"),
             py::arg("alpha"), py::arg("beta"));

    py::class_<topics::lda_scvb, topics::lda_model>{m_topics,
                                                    "LDAStochasticCVB"}
        .def(py::init<const learn::dataset&, std::size_t, double, double,
                      uint64_t>(),
             py::keep_alive<0, 1>(), py::arg("docs"), py::arg("num_topics"),
             py::arg("alpha"), py::arg("beta"), py::arg("minibatch_size") = 100)
        .def("run",
             [](topics::lda_scvb& lda, uint64_t num_iters, double convergence) {
                 py::gil_scoped_release release;
                 lda.run(num_iters, convergence);
             },
             py::arg("num_iters"), py::arg("convergence") = 0);

    py::class_<topics::topic_model>{m_topics, "TopicModel"}
        .def("__init__",
             [](topics::topic_model& model, const std::string& prefix) {
                 py::gil_scoped_release release;

                 std::ifstream theta{prefix + ".theta.bin", std::ios::binary};

                 if (!theta)
                 {
                     throw topics::topic_model_exception{
                         "missing document topic probabilities file: " + prefix
                         + ".theta.bin"};
                 }

                 std::ifstream phi{prefix + ".phi.bin", std::ios::binary};
                 if (!phi)
                 {
                     throw topics::topic_model_exception{
                         "missing topic term probabilities file: " + prefix
                         + ".phi.bin"};
                 }

                 new (&model) topics::topic_model(theta, phi);
             })
        .def("top_k",
             [](const topics::topic_model& model, topic_id tid, std::size_t k) {
                 return model.top_k(tid, k);
             },
             py::arg("tid"), py::arg("k") = 10)
        .def("top_k",
             [](const topics::topic_model& model, topic_id tid, std::size_t k,
                std::function<double(topic_id, term_id)> scorer) {
                 return model.top_k(tid, k, scorer);
             },
             py::arg("tid"), py::arg("k") = 10, py::arg("scorer"))
        .def("top_k",
             [](const topics::topic_model& model, topic_id tid, std::size_t k,
                const topics::bl_term_scorer& scorer) {
                 return model.top_k(tid, k, scorer);
             },
             py::arg("tid"), py::arg("k") = 10, py::arg("scorer"))
        .def("topic_distribution",
             [](const topics::topic_model& self, doc_id did) {
                 return py_multinomial{self.topic_distribution(did)};
             })
        .def("term_distribution",
             [](const topics::topic_model& self, topic_id k) {
                 return py_multinomial{self.term_distribution(k)};
             })
        .def("term_probability", &topics::topic_model::term_probability)
        .def("topic_probability", &topics::topic_model::topic_probability)
        .def("num_topics", &topics::topic_model::num_topics)
        .def("num_words", &topics::topic_model::num_words)
        .def("num_docs", &topics::topic_model::num_docs);

    m_topics.def("load_topic_model", [](const std::string& config_path) {
        py::gil_scoped_release release;
        auto config = cpptoml::parse_file(config_path);
        return topics::load_topic_model(*config);
    });

    py::class_<topics::bl_term_scorer>{m_topics, "BLTermScorer"}
        .def(py::init<const topics::topic_model&>(), py::keep_alive<0, 1>())
        .def("__call__", &topics::bl_term_scorer::operator());
}