From 8f45505166a8c9eddf9c41233b8e46c29a7016e4 Mon Sep 17 00:00:00 2001 From: Nyite Date: Thu, 29 Feb 2024 09:05:24 +0300 Subject: [PATCH 1/3] Mode sanity checks linux only --- matplotlibcpp.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index d95d46a..0c86e25 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -350,10 +350,12 @@ template <> struct select_npy_type { const static NPY_TYPES type = NPY // Sanity checks; comment them out or change the numpy type below if you're compiling on // a platform where they don't apply -static_assert(sizeof(long long) == 8); -template <> struct select_npy_type { const static NPY_TYPES type = NPY_INT64; }; -static_assert(sizeof(unsigned long long) == 8); -template <> struct select_npy_type { const static NPY_TYPES type = NPY_UINT64; }; +#ifdef __linux__ + static_assert(sizeof(long long) == 8); + template <> struct select_npy_type { const static NPY_TYPES type = NPY_INT64; }; + static_assert(sizeof(unsigned long long) == 8); + template <> struct select_npy_type { const static NPY_TYPES type = NPY_UINT64; }; +#endif template PyObject* get_array(const std::vector& v) From 7c66331dd7a9f8b33b44ae21627c374ec0397190 Mon Sep 17 00:00:00 2001 From: Nyite Date: Thu, 29 Feb 2024 11:05:23 +0300 Subject: [PATCH 2/3] Fixed win python debug bin import error --- matplotlibcpp.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index 0c86e25..a3bda58 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -2,7 +2,17 @@ // Python headers must be included before any system headers, since // they define _POSIX_C_SOURCE -#include +#ifdef _WIN32 +# ifdef _DEBUG +# undef _DEBUG +# include +# define _DEBUG +# else +# include +# endif +#else +# include +#endif #include #include From 2fd0feaf5b4f2d2b1f8f2809de46791af8ef8efa Mon Sep 17 00:00:00 2001 From: Nyite Date: Sun, 10 Mar 2024 11:42:09 +0300 Subject: [PATCH 3/3] Any arg mapping in plot keywords --- matplotlibcpp.h | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index a3bda58..85b4bf8 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -15,6 +15,7 @@ #endif #include +#include #include #include #include @@ -451,7 +452,7 @@ PyObject* get_listlist(const std::vector>& ll) /// /// See: https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.plot.html template -bool plot(const std::vector &x, const std::vector &y, const std::map& keywords) +bool plot(const std::vector &x, const std::vector &y, const std::map& keywords) { assert(x.size() == y.size()); @@ -467,10 +468,24 @@ bool plot(const std::vector &x, const std::vector &y, const st PyTuple_SetItem(args, 1, yarray); // construct keyword args - PyObject* kwargs = PyDict_New(); - for(std::map::const_iterator it = keywords.begin(); it != keywords.end(); ++it) - { - PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str())); + PyObject *kwargs = PyDict_New(); + for (const auto &[key, value] : keywords) { + if (auto py_value = std::any_cast(&value)) { + PyDict_SetItemString(kwargs, key.c_str(), PyInt_FromLong(*py_value)); + } else if (auto py_value = std::any_cast(&value)) { + PyDict_SetItemString(kwargs, key.c_str(), PyLong_FromLong(*py_value)); + } else if (auto py_value = std::any_cast(&value)) { + PyDict_SetItemString(kwargs, key.c_str(), PyLong_FromUnsignedLong(*py_value)); + } else if (auto py_value = std::any_cast(&value)) { + PyDict_SetItemString(kwargs, key.c_str(), PyLong_FromSize_t(*py_value)); + } else if (auto py_value = std::any_cast(&value)) { + PyDict_SetItemString(kwargs, key.c_str(), PyLong_FromDouble(*py_value)); + } else if (auto py_value = std::any_cast(&value)) { + PyDict_SetItemString(kwargs, key.c_str(), PyString_FromString(py_value->c_str())); + } else if (auto py_value = std::any_cast(&value)) { + PyDict_SetItemString(kwargs, key.c_str(), PyString_FromString(*py_value)); + } else + throw std::invalid_argument("Unkown plot arg type"); } PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, args, kwargs); @@ -2889,7 +2904,7 @@ inline bool plot(const std::vector& y, const std::string& format = "") { return plot(y,format); } -inline bool plot(const std::vector& x, const std::vector& y, const std::map& keywords) { +inline bool plot(const std::vector& x, const std::vector& y, const std::map& keywords) { return plot(x,y,keywords); }