code = r""" /* * SPlot.h * * Created on: 19. aug. 2010 * Author: nordmoen */ #ifndef SPLOT_H_ #define SPLOT_H_ #include //DGRIM #include "matlib.hpp" #include #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #include #include //DGRIM #include "armadillo/armadillo" class PyEngine { static void _initialize() { static bool initialized = false; if (!initialized) { Py_SetProgramName((char*)"splot"); Py_Initialize(); _import_array(); initialized = true; } } public: // Wrapper class which takes both row/col vectors, as well as initializer lists template class arma_vec { public: const std::vector v; arma_vec() : v() {} arma_vec(const std::initializer_list &c) : v(c) {} arma_vec(const std::vector &vec) : v(vec) {} arma_vec(const arma::Col &vec) : v(vec.begin(), vec.end()) {} arma_vec(const arma::Row &vec) : v(vec.begin(), vec.end()) {} arma_vec(const arma::subview_col &vec) : arma_vec(arma::Col{vec}) {} arma_vec(const arma::subview_row &vec) : arma_vec(arma::Row{vec}) {} template arma_vec(const arma::eOp, U> &vec) : arma_vec(arma::Col{vec}) {} template arma_vec(const arma::eOp, U> &vec) : arma_vec(arma::Row{vec}) {} }; // Wrapper class to limit arma::Mat implicit conversions (from string, for example). // For parameter-passing use only, since it borrows reference in some cases. template class arma_mat { const arma::Mat _m; public: const arma::Mat &m; arma_mat(const arma::Mat &mat) : m(mat) {} arma_mat(const arma::subview &view) : _m(view), m(_m) {} template arma_mat(const arma::eOp, U> &op) : _m(op), m(_m) {} }; class py_obj { friend class PyEngine; PyObject *obj; template static int npy_typenum(); template static PyObject *create(const std::initializer_list &in_dims, const InputIt &data, int flags=0) { std::vector dims; for (auto it=in_dims.begin(); it!=in_dims.end(); ++it) dims.push_back((npy_intp)*it); PyObject *obj = PyArray_NewFromDescr(&PyArray_Type, PyArray_DescrFromType(npy_typenum()), dims.size(), (npy_intp*)dims.data(), /*strides*/nullptr, /*data*/nullptr, flags, /*obj*/nullptr); std::copy_n(data, PyArray_Size(obj), (NPY_T*)PyArray_DATA((PyArrayObject*)obj)); return obj; } explicit py_obj(PyObject *o, bool steal_reference) : obj(o) { if (!steal_reference) Py_XINCREF(obj); } public: /* Basic constructors */ py_obj() : obj(nullptr) {} py_obj(const py_obj &other) : obj(other.obj) { Py_XINCREF(obj); } py_obj(const void *); // Trigger link error, to avoid implicit cast to bool /* Construct from primitive types, strings */ py_obj(bool const& b) : obj(PyBool_FromLong(b)) {} py_obj(int const& i) : obj(PyInt_FromLong(i)) {} py_obj(double const& d) : obj(PyFloat_FromDouble(d)) {} py_obj(const char* const& s) : obj(PyString_FromString(s)) {} py_obj(std::string const& s) : obj(PyString_FromString(s.c_str())) {} /* Construct from various sequences of double/float/int type */ py_obj(std::initializer_list c) : obj(create({c.size()}, c.begin())) {} py_obj(std::vector const& vec) : obj(create({vec.size()}, vec.begin())) {} py_obj(arma_vec const& vec) : py_obj(vec.v) {} py_obj(arma_mat const& mat) : obj(create({mat.m.n_rows, mat.m.n_cols}, mat.m.begin(), NPY_ARRAY_F_CONTIGUOUS)) {} py_obj(std::initializer_list c) : obj(create({c.size()}, c.begin())) {} py_obj(std::vector const& vec) : obj(create({vec.size()}, vec.begin())) {} py_obj(arma_vec const& vec) : py_obj(vec.v) {} py_obj(arma_mat const& mat) : obj(create({mat.m.n_rows, mat.m.n_cols}, mat.m.begin(), NPY_ARRAY_F_CONTIGUOUS)) {} py_obj(std::initializer_list c) : obj(create({c.size()}, c.begin())) {} py_obj(std::vector const& vec) : obj(create({vec.size()}, vec.begin())) {} py_obj(arma_vec const& vec) : py_obj(vec.v) {} py_obj(arma_mat const& mat) : obj(create({mat.m.n_rows, mat.m.n_cols}, mat.m.begin(), NPY_ARRAY_F_CONTIGUOUS)) {} py_obj(std::initializer_list c) : obj(create({c.size()}, c.begin())) {} py_obj(std::vector const& vec) : obj(create({vec.size()}, vec.begin())) {} py_obj(arma_vec const& vec) : py_obj(vec.v) {} py_obj(arma_mat const& mat) : obj(create({mat.m.n_rows, mat.m.n_cols}, mat.m.begin(), NPY_ARRAY_F_CONTIGUOUS)) {} py_obj(std::initializer_list c) : obj(create({c.size()}, c.begin())) {} py_obj(std::vector const& vec) : obj(create({vec.size()}, vec.begin())) {} py_obj(arma_vec const& vec) : py_obj(vec.v) {} py_obj(arma_mat const& mat) : obj(create({mat.m.n_rows, mat.m.n_cols}, mat.m.begin(), NPY_ARRAY_F_CONTIGUOUS)) {} py_obj(std::initializer_list c) : obj(create({c.size()}, c.begin())) {} py_obj(std::vector const& vec) : obj(create({vec.size()}, vec.begin())) {} py_obj(arma_vec const& vec) : py_obj(vec.v) {} py_obj(arma_mat const& mat) : obj(create({mat.m.n_rows, mat.m.n_cols}, mat.m.begin(), NPY_ARRAY_F_CONTIGUOUS)) {} /* Destructor */ ~py_obj() { Py_XDECREF(obj); } /* Assignment and equality operators */ py_obj &operator=(const py_obj &other) { Py_XDECREF(obj); obj = other.obj; Py_XINCREF(obj); return *this; } bool operator==(const py_obj &other) const { return obj==other.obj; } bool valid() const { return (obj!=nullptr); } private: /* Cast to PyObject */ operator PyObject*() { return obj; } }; static py_obj None() { return py_obj(Py_None, false); } typedef std::vector args_t; typedef std::map kwargs_t; protected: PyObject *main_module; py_obj py_call_object(py_obj object, const char *func, const args_t &args={}, const kwargs_t &kwargs={}) { py_obj pyFunc(PyObject_GetAttrString(object, func), true); if (!pyFunc.valid()) { std::cerr << "No such method: " << func << std::endl; return py_obj(nullptr, true); } py_obj pyArgs(PyTuple_New(args.size()), true); for (size_t i=0; i 1: self.setFontSize(self.pt - 1) def wheelEvent(self, event): if event.modifiers() == QtCore.Qt.ControlModifier: delta = event.delta() if qt_backend==4 else event.angleDelta().y() if delta > 1: self.zoom_in() else: self.zoom_out() else: QtWidgets.QTableView.wheelEvent(self, event) def closeEvent(self, event): del TableView._views[self] QtWidgets.QTableView.closeEvent(self, event) def table(mat, title=None): if QtWidgets.QApplication.instance() is None: import sys table.app = QtWidgets.QApplication(sys.argv) table.seq += 1 if title is None: title = 'Table %d '%table.seq title += ' (%dx%d %s)'%(mat.shape+(mat.dtype.name,)) model = TableModel(mat) view = TableView() view.setMonospaceFont() if np.iscomplexobj(mat): view.chars *= 2 view.setAttribute(QtCore.Qt.WA_DeleteOnClose) view.setModel(model) view.setFontSize(8) view.resize(600, 400) view.setWindowTitle(title) view.show() table.seq = 0 )"; } else { splot_py = static_cast(std::stringstream() << in.rdbuf()).str(); } py_code(splot_py.c_str()); } py_obj figure(const py_obj &figno) { return py_call("figure", {figno}); } py_obj hold(bool onoff) { return py_call("hold", {onoff}); } py_obj clf() { return py_call("clf"); } py_obj cla() { return py_call("cla"); } py_obj show(bool block=true) { return py_call("show", {block}); } py_obj xlabel(const char *label) { return py_call("xlabel", {label}); } py_obj ylabel(const char *label) { return py_call("ylabel", {label}); } py_obj title(const char *title) { return py_call("title", {title}); } py_obj plot(const arma_vec &x, const arma_vec &y, const kwargs_t &kwargs) { return py_call("plot", {x, y}, kwargs); } py_obj plot(const arma_vec &x, const arma_vec &y, const std::string& spec={}) { return py_call("plot", {x, y, spec}); } py_obj plot(const arma_vec &x1, const arma_vec &y1, const arma_vec &x2, const arma_vec &y2, const std::string& spec2={}) { return py_call("plot", {x1, y1, std::string(), x2, y2, spec2}); } py_obj plot(const arma_vec &x1, const arma_vec &y1, const std::string& spec1, const arma_vec &x2, const arma_vec &y2, const std::string& spec2={}) { return py_call("plot", {x1, y1, spec1, x2, y2, spec2}); } py_obj imshow(const arma_mat &A, const kwargs_t &kwargs={}) { kwargs_t new_kwargs(kwargs); new_kwargs.emplace("aspect", "auto"); new_kwargs.emplace("interpolation", "nearest"); //new_kwargs.emplace("clim", py_obj({A.min(), A.max()})); return py_call("imshow", {A}, new_kwargs); } py_obj imagesc(const arma_mat &A, const kwargs_t &kwargs={}) { return imshow(A, kwargs); } py_obj imagesc(const arma_vec &x, const arma_vec &y, const arma_mat &A, const kwargs_t &kwargs={}) { if (x.v.empty() && y.v.empty()) return imagesc(A, kwargs); if ((x.v.size() != 2 && x.v.size() != A.m.n_cols) || (y.v.size() != 2 && y.v.size() != A.m.n_rows)) return error("imagesc: length of x/y bounds must be 2 or matrix dimensions"); kwargs_t new_kwargs(kwargs); new_kwargs["extent"] = {x.v[0], x.v[x.v.size()-1], y.v[0], y.v[y.v.size()-1]}; return imagesc(A, new_kwargs); } py_obj imagesc(const arma_mat &A, const arma_vec &clims, const kwargs_t &kwargs={}) { return imagesc(arma_vec(), arma_vec(), A, clims, kwargs); } py_obj imagesc(const arma_vec &x, const arma_vec &y, const arma_mat &A, const arma_vec &clims, const kwargs_t &kwargs={}) { if (clims.v.empty()) return imagesc(x, y, A, kwargs); if (clims.v.size() != 2) return error("imagesc: length of c bounds must be 2"); kwargs_t new_kwargs(kwargs); new_kwargs["clim"] = clims; return imagesc(x, y, A, new_kwargs); } py_obj wigb(const arma_mat &A, double scale=1.0, const arma_vec &x={}, const arma_vec &z={}, double amx=0.0, const kwargs_t &kwargs={}) { return py_call("wigb", {A, scale, x, z, amx}, kwargs); } py_obj colorbar() { return py_call("colorbar"); } py_obj colorbar(py_obj mappable) { return py_call("colorbar", {mappable}); } py_obj colorbar(py_obj mappable, py_obj ax) { return py_call("colorbar", {mappable, ax}); } py_obj xlim(double xmin, double xmax) { return py_call("xlim", {xmin, xmax}); } py_obj ylim(double ymin, double ymax) { return py_call("ylim", {ymin, ymax}); } py_obj caxis(double cmin, double cmax) { return py_call("clim", {cmin, cmax}); } py_obj axis(double xmin, double xmax, double ymin, double ymax) { return py_call("axis", {{xmin, xmax, ymin, ymax}}); } py_obj grid(const kwargs_t &kwargs={}) { return py_call("grid", {}, kwargs); } py_obj subplot(int xyi, const kwargs_t &kwargs={}) { return py_call("subplot", {xyi}, kwargs); } py_obj subplot(int x, int y, int i, const kwargs_t &kwargs={}) { return py_call("subplot", {x, y, i}, kwargs); } py_obj colormap(py_obj fig, const std::string &name, int levels=64) { const size_t start = name.find('('); if (start != std::string::npos) { const size_t end = name.find(')'); if (end != std::string::npos && end>start) { const std::string name_part(name.substr(start)); const int levels_part = atoi(name.substr(start+1, end).c_str()); return colormap(fig, name_part, levels_part); } } py_obj cmap = py_call("get_cmap", {(name=="default" ? "jet" : name), levels}); return py_call_object(fig, "set_cmap", {cmap}); } py_obj colormap(const std::string &name, int levels=64) { return colormap(py_call("gci"), name, levels); } py_obj colormap(const py_obj &fig, const arma_mat &segments) { py_obj cmap = py_call_object(py_get_module("matplotlib.colors"), "ListedColormap", {segments}); return py_call_object(fig, "set_cmap", {cmap}); } py_obj colormap(const arma_mat &segments) { return colormap(py_call("gci"), segments); } void table(const arma_mat &mat, const py_obj &title=None()) { py_call("table", {mat, title}); } void table(const arma_mat &mat, const py_obj &title=None()) { py_call("table", {mat, title}); } }; template <> int PyEngine::py_obj::npy_typenum() { return NPY_DOUBLE; } template <> int PyEngine::py_obj::npy_typenum() { return NPY_FLOAT; } template <> int PyEngine::py_obj::npy_typenum() { return NPY_INT32; } template <> int PyEngine::py_obj::npy_typenum() { return NPY_UINT32; } template <> int PyEngine::py_obj::npy_typenum() { return NPY_INT64; } template <> int PyEngine::py_obj::npy_typenum() { return NPY_UINT64; } template <> int PyEngine::py_obj::npy_typenum() { return NPY_COMPLEX64; } template <> int PyEngine::py_obj::npy_typenum() { return NPY_COMPLEX128; } #endif /* SPLOT_H_ */ """