|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "matplotlibcpp.h" |
| 4 | + |
| 5 | +#include <Python.h> |
| 6 | +#include <datetime.h> |
| 7 | + |
| 8 | +#include <string> |
| 9 | +#include <cstdlib> |
| 10 | +#include <map> |
| 11 | +#include <ranges> |
| 12 | + |
| 13 | +// Convenience functions for converting C/C++ time objects to datetime.datetime |
| 14 | +// objects. These are outside the matplotlibcpp namespace because they do not |
| 15 | +// exist in matplotlib.pyplot. |
| 16 | +template<class TimePoint> |
| 17 | +PyObject* toPyDateTime(const TimePoint& t, int dummy=0) |
| 18 | +{ |
| 19 | + using namespace std::chrono; |
| 20 | + auto tsec=time_point_cast<seconds>(t); |
| 21 | + auto us=duration_cast<microseconds>(t-tsec); |
| 22 | + |
| 23 | + time_t tt=system_clock::to_time_t(t); |
| 24 | + PyObject* obj=toPyDateTime(tt, us.count()); |
| 25 | + |
| 26 | + return obj; |
| 27 | +} |
| 28 | + |
| 29 | +template <> |
| 30 | +PyObject* toPyDateTime(const time_t& t, int us) |
| 31 | +{ |
| 32 | + tm tm {}; |
| 33 | + gmtime_r(&t,&tm); // compatible with matlab, inverse of datenum. |
| 34 | + |
| 35 | + if (!PyDateTimeAPI) { |
| 36 | + PyDateTime_IMPORT; |
| 37 | + } |
| 38 | + |
| 39 | + PyObject* obj=PyDateTime_FromDateAndTime(tm.tm_year+1900, |
| 40 | + tm.tm_mon+1, |
| 41 | + tm.tm_mday, |
| 42 | + tm.tm_hour, |
| 43 | + tm.tm_min, |
| 44 | + tm.tm_sec, |
| 45 | + us); |
| 46 | + if (obj) { |
| 47 | + PyDateTime_Check(obj); |
| 48 | + Py_INCREF(obj); |
| 49 | + } |
| 50 | + |
| 51 | + return obj; |
| 52 | +} |
| 53 | + |
| 54 | +template <class Time_t> |
| 55 | +PyObject* toPyDateTimeList(const Time_t* t, size_t nt) |
| 56 | +{ |
| 57 | + PyObject* tlist=PyList_New(nt); |
| 58 | + if (tlist==nullptr) |
| 59 | + return nullptr; |
| 60 | + |
| 61 | + // Py_INCREF(tlist); |
| 62 | + |
| 63 | + if (!PyDateTimeAPI) { |
| 64 | + PyDateTime_IMPORT; |
| 65 | + } |
| 66 | + |
| 67 | + for (size_t i=0; i<nt; i++) { |
| 68 | + PyObject* ti=toPyDateTime(t[i], 0); |
| 69 | + PyList_SET_ITEM(tlist, i, ti); |
| 70 | + } |
| 71 | + |
| 72 | + return tlist; |
| 73 | +} |
| 74 | + |
| 75 | +template <class Time_t> |
| 76 | +class DateTimeList |
| 77 | +{ |
| 78 | +public: |
| 79 | + |
| 80 | + DateTimeList(const Time_t* t, size_t nt) { |
| 81 | + tlist=(PyListObject*) toPyDateTimeList(t, nt); |
| 82 | + } |
| 83 | + |
| 84 | + ~DateTimeList() { if (tlist) Py_DECREF((PyObject*) tlist); } |
| 85 | + |
| 86 | + PyListObject* get() const { return tlist; } |
| 87 | + size_t size() const { return tlist ? PyList_Size((PyObject*)tlist) : 0; } |
| 88 | + |
| 89 | +private: |
| 90 | + mutable PyListObject* tlist=nullptr; |
| 91 | +}; |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +namespace matplotlibcpp { |
| 96 | + |
| 97 | +// special purpose function to plot against python datetime objects. |
| 98 | +template <class Time_t, std::ranges::contiguous_range ContainerY> |
| 99 | +bool plot(const DateTimeList<Time_t>& t, const ContainerY& y, |
| 100 | + const std::string& fmt="") |
| 101 | +{ |
| 102 | + detail::_interpreter::get(); |
| 103 | + |
| 104 | + // DECREF decrements the ref counts of all objects in the plot_args tuple, |
| 105 | + // In particular, it decreasesthe ref count of the time array x. |
| 106 | + // We want to maintain that unchanged though, so we can reuse it. |
| 107 | + PyListObject* tarray=t.get(); |
| 108 | + Py_INCREF(tarray); |
| 109 | + |
| 110 | + NPY_TYPES ytype=detail::select_npy_type<typename ContainerY::value_type>::type; |
| 111 | + |
| 112 | + npy_intp tsize=PyList_Size((PyObject*)tarray); |
| 113 | + assert(y.size()%tsize == 0 && "length of y must be a multiple of length of x!"); |
| 114 | + |
| 115 | + npy_intp yrows=tsize, ycols=y.size()/yrows; |
| 116 | + npy_intp ysize[]={yrows, ycols}; // ysize[0] must equal tsize |
| 117 | + |
| 118 | + PyObject* yarray = PyArray_New(&PyArray_Type, |
| 119 | + 2, ysize, ytype, nullptr, (void*) y.data(), |
| 120 | + 0, NPY_ARRAY_FARRAY, nullptr); // col major |
| 121 | + |
| 122 | + PyObject* pystring = PyString_FromString(fmt.c_str()); |
| 123 | + |
| 124 | + PyObject* plot_args = PyTuple_New(3); |
| 125 | + PyTuple_SetItem(plot_args, 0, (PyObject*)tarray); |
| 126 | + PyTuple_SetItem(plot_args, 1, yarray); |
| 127 | + PyTuple_SetItem(plot_args, 2, pystring); |
| 128 | + |
| 129 | + PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_plot, plot_args); |
| 130 | + |
| 131 | + Py_DECREF(plot_args); |
| 132 | + if(res) Py_DECREF(res); |
| 133 | + |
| 134 | + return true; |
| 135 | +} |
| 136 | + |
| 137 | +} |
0 commit comments