forked from lava/matplotlib-cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot.h
More file actions
245 lines (185 loc) · 7.4 KB
/
Copy pathplot.h
File metadata and controls
245 lines (185 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#pragma once
#include "_python.h"
#include "interpreter.h"
#include "helpers.h"
namespace matplotlibcpp {
template<typename Numeric>
bool plot(const std::vector<Numeric> &x, const std::vector<Numeric> &y, const std::map<std::string, std::string>& keywords)
{
assert(x.size() == y.size());
// using numpy arrays
PyObject* xarray = get_array(x);
PyObject* yarray = get_array(y);
// construct positional args
PyObject* args = PyTuple_New(2);
PyTuple_SetItem(args, 0, xarray);
PyTuple_SetItem(args, 1, yarray);
// construct keyword args
PyObject* kwargs = helpers::to_dict(keywords);
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, args, kwargs);
Py_DECREF(args);
Py_DECREF(kwargs);
if(res != nullptr) Py_DECREF(res);
return res != nullptr;
}
template<typename NumericX, typename NumericY>
bool plot(const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::string& s = "")
{
assert(x.size() == y.size());
PyObject* xarray = get_array(x);
PyObject* yarray = get_array(y);
PyObject* pystring = PyString_FromString(s.c_str());
PyObject* plot_args = PyTuple_New(3);
PyTuple_SetItem(plot_args, 0, xarray);
PyTuple_SetItem(plot_args, 1, yarray);
PyTuple_SetItem(plot_args, 2, pystring);
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_plot, plot_args);
Py_DECREF(plot_args);
if(res != nullptr) Py_DECREF(res);
return res != nullptr;
}
template<typename Numeric>
bool named_plot(const std::string& name, const std::vector<Numeric>& y, const std::string& format = "")
{
PyObject* kwargs = PyDict_New();
PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
PyObject* yarray = get_array(y);
PyObject* pystring = PyString_FromString(format.c_str());
PyObject* plot_args = PyTuple_New(2);
PyTuple_SetItem(plot_args, 0, yarray);
PyTuple_SetItem(plot_args, 1, pystring);
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs);
Py_DECREF(kwargs);
Py_DECREF(plot_args);
if (res != nullptr) Py_DECREF(res);
return res != nullptr;
}
template<typename Numeric>
bool named_plot(const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = "")
{
PyObject* kwargs = PyDict_New();
PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
PyObject* xarray = get_array(x);
PyObject* yarray = get_array(y);
PyObject* pystring = PyString_FromString(format.c_str());
PyObject* plot_args = PyTuple_New(3);
PyTuple_SetItem(plot_args, 0, xarray);
PyTuple_SetItem(plot_args, 1, yarray);
PyTuple_SetItem(plot_args, 2, pystring);
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs);
Py_DECREF(kwargs);
Py_DECREF(plot_args);
if (res != nullptr) Py_DECREF(res);
return res != nullptr;
}
template<typename Numeric>
bool plot(const std::vector<Numeric>& y, const std::string& format = "")
{
std::vector<Numeric> x(y.size());
for(size_t i=0; i<x.size(); ++i) x.at(i) = static_cast<Numeric>(i);
return plot(x,y,format);
}
#if __cplusplus > 199711L || _MSC_VER > 1800
// C++11-exclusive content starts here (variadic plot() and initializer list support)
namespace detail {
template<typename T>
using is_function = typename std::is_function<std::remove_pointer<std::remove_reference<T>>>::type;
template<bool obj, typename T>
struct is_callable_impl;
template<typename T>
struct is_callable_impl<false, T>
{
typedef is_function<T> type;
}; // a non-object is callable iff it is a function
template<typename T>
struct is_callable_impl<true, T>
{
struct Fallback { void operator()(); };
struct Derived : T, Fallback { };
template<typename U, U> struct Check;
template<typename U>
static std::true_type test( ... ); // use a variadic function to make sure (1) it accepts everything and (2) its always the worst match
template<typename U>
static std::false_type test( Check<void(Fallback::*)(), &U::operator()>* );
public:
typedef decltype(test<Derived>(nullptr)) type;
typedef decltype(&Fallback::operator()) dtype;
static constexpr bool value = type::value;
}; // an object is callable iff it defines operator()
template<typename T>
struct is_callable
{
// dispatch to is_callable_impl<true, T> or is_callable_impl<false, T> depending on whether T is of class type or not
typedef typename is_callable_impl<std::is_class<T>::value, T>::type type;
};
template<typename IsYDataCallable>
struct plot_impl { };
template<>
struct plot_impl<std::false_type>
{
template<typename IterableX, typename IterableY>
bool operator()(const IterableX& x, const IterableY& y, const std::string& format)
{
// 2-phase lookup for distance, begin, end
using std::distance;
using std::begin;
using std::end;
auto xs = distance(begin(x), end(x));
auto ys = distance(begin(y), end(y));
assert(xs == ys && "x and y data must have the same number of elements!");
PyObject* xlist = PyList_New(xs);
PyObject* ylist = PyList_New(ys);
PyObject* pystring = PyString_FromString(format.c_str());
auto itx = begin(x), ity = begin(y);
for(size_t i = 0; i < static_cast<size_t>(xs); ++i) {
PyList_SetItem(xlist, i, PyFloat_FromDouble(*itx++));
PyList_SetItem(ylist, i, PyFloat_FromDouble(*ity++));
}
PyObject* plot_args = PyTuple_New(3);
PyTuple_SetItem(plot_args, 0, xlist);
PyTuple_SetItem(plot_args, 1, ylist);
PyTuple_SetItem(plot_args, 2, pystring);
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_plot, plot_args);
Py_DECREF(plot_args);
if(res != nullptr) Py_DECREF(res);
return res != nullptr;
}
};
template<>
struct plot_impl<std::true_type>
{
template<typename Iterable, typename Callable>
bool operator()(const Iterable& ticks, const Callable& f, const std::string& format)
{
if(begin(ticks) == end(ticks)) return true;
// We could use additional meta-programming to deduce the correct element type of y,
// but all values have to be convertible to double anyways
std::vector<double> y;
for(auto x : ticks) y.push_back(f(x));
return plot_impl<std::false_type>()(ticks,y,format);
}
};
} // end namespace detail
// recursion stop for the above
template<typename... Args>
bool plot() { return true; }
template<typename A, typename B, typename... Args>
bool plot(const A& a, const B& b, const std::string& format, Args... args)
{
return detail::plot_impl<typename detail::is_callable<B>::type>()(a,b,format) && plot(args...);
}
/*
* This group of plot() functions is needed to support initializer lists, i.e. calling
* plot( {1,2,3,4} )
*/
inline bool plot(const std::vector<double>& x, const std::vector<double>& y, const std::string& format = "") {
return plot<double,double>(x,y,format);
}
inline bool plot(const std::vector<double>& y, const std::string& format = "") {
return plot<double>(y,format);
}
inline bool plot(const std::vector<double>& x, const std::vector<double>& y, const std::map<std::string, std::string>& keywords) {
return plot<double>(x,y,keywords);
}
#endif
} // end namespace matplotlibcpp