@@ -0,0 +1,94 | |||
|
1 | import traceback | |
|
2 | from SciQLopBindings import PyDataProvider, Product, VectorTimeSerie, ScalarTimeSerie, DataSeriesType | |
|
3 | import numpy as np | |
|
4 | import math | |
|
5 | from spwc.cache import _cache | |
|
6 | from spwc.common.datetime_range import DateTimeRange | |
|
7 | from functools import partial | |
|
8 | from datetime import datetime, timedelta, timezone | |
|
9 | from spwc.common.variable import SpwcVariable | |
|
10 | ||
|
11 | ||
|
12 | def make_scalar(x): | |
|
13 | y = np.cos(x/10.) | |
|
14 | return SpwcVariable(time=x, data=y) | |
|
15 | ||
|
16 | def make_vector(x): | |
|
17 | v=np.ones((len(x),3)) | |
|
18 | for i in range(3): | |
|
19 | v.transpose()[:][i] = np.cos(x/10. + float(i)) + (100. * np.cos(x/10000. + float(i))) | |
|
20 | return SpwcVariable(time=x, data=v) | |
|
21 | ||
|
22 | ||
|
23 | def make_multicomponent(x): | |
|
24 | v=np.ones((len(x),4)) | |
|
25 | for i in range(4): | |
|
26 | v.transpose()[:][i] = float(i+1) * np.cos(x/10. + float(i)) | |
|
27 | return SpwcVariable(time=x, data=v) | |
|
28 | ||
|
29 | def make_spectrogram(x): | |
|
30 | v=np.ones((len(x),32)) | |
|
31 | for i in range(32): | |
|
32 | v.transpose()[:][i] = 100.*(2.+ float(i+1) * np.cos(x/1024. + float(i))) | |
|
33 | return SpwcVariable(time=x, data=v) | |
|
34 | ||
|
35 | ||
|
36 | def _get_data(p_type, start, stop): | |
|
37 | if type(start) is datetime: | |
|
38 | start = start.timestamp() | |
|
39 | stop = stop.timestamp() | |
|
40 | x = np.arange(math.ceil(start), math.floor(stop))*1. | |
|
41 | if p_type == 'scalar': | |
|
42 | return make_scalar(x) | |
|
43 | if p_type == 'vector': | |
|
44 | return make_vector(x) | |
|
45 | if p_type == 'multicomponent': | |
|
46 | return make_multicomponent(x) | |
|
47 | if p_type == 'spectrogram': | |
|
48 | return make_spectrogram(np.arange(math.ceil(start), math.floor(stop),15.)) | |
|
49 | return None | |
|
50 | ||
|
51 | class MyProvider(PyDataProvider): | |
|
52 | def __init__(self): | |
|
53 | super(MyProvider,self).__init__() | |
|
54 | self.register_products([Product("/tests/without_cache/scalar",[],{"type":"scalar"}), | |
|
55 | Product("/tests/without_cache/vector",[],{"type":"vector"}), | |
|
56 | Product("/tests/without_cache/multicomponent",[],{"type":"multicomponent",'size':'4'}), | |
|
57 | Product("/tests/without_cache/spectrogram",[],{"type":"spectrogram",'size':'32'}), | |
|
58 | Product("/tests/with_cache/scalar",[],{"type":"scalar", "cache":"true"}), | |
|
59 | Product("/tests/with_cache/vector",[],{"type":"vector", "cache":"true"}), | |
|
60 | Product("/tests/with_cache/multicomponent",[],{"type":"multicomponent",'size':'4', "cache":"true"}) | |
|
61 | ]) | |
|
62 | ||
|
63 | def get_data(self,metadata,start,stop): | |
|
64 | ts_type = DataSeriesType.SCALAR | |
|
65 | default_ctor_args = 1 | |
|
66 | use_cache = False | |
|
67 | p_type = 'scalar' | |
|
68 | try: | |
|
69 | for key,value in metadata.items(): | |
|
70 | if key == 'type': | |
|
71 | p_type = value | |
|
72 | if value == 'vector': | |
|
73 | ts_type = DataSeriesType.VECTOR | |
|
74 | elif value == 'multicomponent': | |
|
75 | ts_type = DataSeriesType.MULTICOMPONENT | |
|
76 | elif value == 'spectrogram': | |
|
77 | ts_type = DataSeriesType.SPECTROGRAM | |
|
78 | if key == 'cache' and value == 'true': | |
|
79 | use_cache = True | |
|
80 | if use_cache: | |
|
81 | cache_product = f"tests/{p_type}" | |
|
82 | var = _cache.get_data(cache_product, DateTimeRange(datetime.fromtimestamp(start, tz=timezone.utc), datetime.fromtimestamp(stop, tz=timezone.utc)), partial(_get_data, p_type), fragment_hours=24) | |
|
83 | else: | |
|
84 | print("No Cache") | |
|
85 | var = _get_data(p_type, start, stop) | |
|
86 | return ((var.time,var.data), ts_type) | |
|
87 | except Exception as e: | |
|
88 | print(traceback.format_exc()) | |
|
89 | print("Error in test.py ",str(e)) | |
|
90 | return ((np.array(), np.array()), ts_type) | |
|
91 | ||
|
92 | ||
|
93 | t=MyProvider() | |
|
94 |
@@ -10,6 +10,7 set(BINDINGS_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}") | |||
|
10 | 10 | |
|
11 | 11 | configure_file("${BINDINGS_SRC_DIR}/bindings.xml" "${BINDINGS_BUILD_DIR}/bindings.xml" COPYONLY) |
|
12 | 12 | configure_file("${BINDINGS_SRC_DIR}/main.py" "${BINDINGS_BUILD_DIR}/main.py" COPYONLY) |
|
13 | configure_file("${BINDINGS_SRC_DIR}/TestPlugin.py" "${BINDINGS_BUILD_DIR}/plugins/TestPlugin.py" COPYONLY) | |
|
13 | 14 | |
|
14 | 15 | execute_process(COMMAND "${PYTHON_EXECUTABLE}" "${BINDINGS_SRC_DIR}/src_list.py" cmake "${BINDINGS_BUILD_DIR}" OUTPUT_VARIABLE BINDINGS_SOURCE) |
|
15 | 16 |
@@ -1,5 +1,6 | |||
|
1 | 1 | #pragma once |
|
2 | 2 | #include <Data/DataProviderParameters.h> |
|
3 | #include <Data/DataSeriesType.h> | |
|
3 | 4 | #include <Data/IDataProvider.h> |
|
4 | 5 | #include <DataSource/DataSourceController.h> |
|
5 | 6 | #include <DataSource/DataSourceItem.h> |
@@ -20,7 +21,7 struct Product | |||
|
20 | 21 | : path { path }, components { components }, metadata { metadata } |
|
21 | 22 | { |
|
22 | 23 | } |
|
23 |
|
|
|
24 | ~Product() = default; | |
|
24 | 25 | }; |
|
25 | 26 | |
|
26 | 27 | class PyDataProvider : public IDataProvider |
@@ -34,8 +35,7 public: | |||
|
34 | 35 | |
|
35 | 36 | virtual ~PyDataProvider() {} |
|
36 | 37 | |
|
37 | virtual QPair<NpArray, NpArray> getData( | |
|
38 | const std::string& key, double start_time, double stop_time) | |
|
38 | virtual QPair<QPair<NpArray,NpArray>,DataSeriesType> get_data(const QMap<QString,QString>& key, double start_time, double stop_time) | |
|
39 | 39 | { |
|
40 | 40 | (void)key, (void)start_time, (void)stop_time; |
|
41 | 41 | return {}; |
@@ -45,10 +45,54 public: | |||
|
45 | 45 | { |
|
46 | 46 | if (parameters.m_Data.contains("name")) |
|
47 | 47 | { |
|
48 | auto data = getData(parameters.m_Data["name"].toString().toStdString(), | |
|
48 | QMap<QString,QString> metadata; | |
|
49 | std::for_each(parameters.m_Data.constKeyValueBegin(), parameters.m_Data.constKeyValueEnd(), [&metadata](const auto& item) { | |
|
50 | metadata[item.first] = item.second.toString(); | |
|
51 | }); | |
|
52 | auto [data, type] = get_data(metadata, | |
|
49 | 53 | parameters.m_Range.m_TStart, parameters.m_Range.m_TEnd); |
|
50 | 54 | // TODO add shape/type switch |
|
51 | return new ScalarTimeSerie { data.first.to_std_vect(), data.second.to_std_vect() }; | |
|
55 | //if (builder) | |
|
56 | { | |
|
57 | auto& [t,y]=data; | |
|
58 | switch (type) | |
|
59 | { | |
|
60 | case DataSeriesType::SCALAR: | |
|
61 | return new ScalarTimeSerie { std::move(t.data), | |
|
62 | std::move(y.data) }; | |
|
63 | break; | |
|
64 | case DataSeriesType::VECTOR: | |
|
65 | return new VectorTimeSerie { std::move(t.data), | |
|
66 | y.to_std_vect_vect() }; | |
|
67 | break; | |
|
68 | case DataSeriesType::MULTICOMPONENT: | |
|
69 | { | |
|
70 | auto y_size = y.flat_size(); | |
|
71 | auto t_size = t.flat_size(); | |
|
72 | ||
|
73 | if(t_size && (y_size%t_size)==0) | |
|
74 | { | |
|
75 | return new MultiComponentTimeSerie { std::move(t.data), | |
|
76 | std::move(y.data),{t_size, y_size/t_size} }; | |
|
77 | } | |
|
78 | break; | |
|
79 | } | |
|
80 | case DataSeriesType::SPECTROGRAM: | |
|
81 | { | |
|
82 | auto y_size = y.flat_size(); | |
|
83 | auto t_size = t.flat_size(); | |
|
84 | ||
|
85 | if(t_size && (y_size%t_size)==0) | |
|
86 | { | |
|
87 | return new SpectrogramTimeSerie { std::move(t.data), | |
|
88 | std::move(y.data),{t_size, y_size/t_size} }; | |
|
89 | } | |
|
90 | break; | |
|
91 | } | |
|
92 | default: | |
|
93 | break; | |
|
94 | } | |
|
95 | } | |
|
52 | 96 | } |
|
53 | 97 | return nullptr; |
|
54 | 98 | } |
@@ -65,25 +109,3 public: | |||
|
65 | 109 | }); |
|
66 | 110 | } |
|
67 | 111 | }; |
|
68 | ||
|
69 | ||
|
70 | struct Providers | |
|
71 | { | |
|
72 | Providers() = default; | |
|
73 | virtual ~Providers() = default; | |
|
74 | inline void register_provider(PyDataProvider* provider) | |
|
75 | { | |
|
76 | auto& dataSourceController = sqpApp->dataSourceController(); | |
|
77 | dataSourceController.setDataProvider( | |
|
78 | provider->id(), std::unique_ptr<IDataProvider>(provider)); | |
|
79 | } | |
|
80 | }; | |
|
81 | ||
|
82 | ||
|
83 | inline ScalarTimeSerie test_PyDataProvider(PyDataProvider& prov) | |
|
84 | { | |
|
85 | auto v = prov.getData("", 0., 0.); | |
|
86 | ScalarTimeSerie s; | |
|
87 | s.set_data(v.first.to_std_vect(), v.second.to_std_vect()); | |
|
88 | return s; | |
|
89 | } |
@@ -6,6 +6,8 | |||
|
6 | 6 | #include "numpy_wrappers.h" |
|
7 | 7 | #include <Data/IDataProvider.h> |
|
8 | 8 | #include <Data/ScalarTimeSerie.h> |
|
9 | #include <Data/VectorTimeSerie.h> | |
|
10 | #include <Data/DataSeriesType.h> | |
|
9 | 11 | #include <SqpApplication.h> |
|
10 | 12 | |
|
11 | 13 |
@@ -5,6 +5,7 | |||
|
5 | 5 | <load-typesystem name="typesystem_widgets.xml" generate="no" /> |
|
6 | 6 | <primitive-type name="std::string"/> |
|
7 | 7 | <primitive-type name="std::size_t"/> |
|
8 | <enum-type name="DataSeriesType"/> | |
|
8 | 9 | <container-type name="std::vector" type="vector"> |
|
9 | 10 | <include file-name="vector" location="global"/> |
|
10 | 11 | <conversion-rule> |
@@ -31,19 +32,26 | |||
|
31 | 32 | </target-to-native> |
|
32 | 33 | </conversion-rule> |
|
33 | 34 | </container-type> |
|
34 | <object-type name="PyDataProvider" /> | |
|
35 | <primitive-type name="NpArray" target-lang-api-name="PyObject"> | |
|
36 | <include file-name="numpy_wrappers.h" location="local"/> | |
|
37 | <conversion-rule> | |
|
38 | <native-to-target> | |
|
39 | auto result = %in.py_object(); | |
|
40 | return result; | |
|
41 | </native-to-target> | |
|
42 | <target-to-native> | |
|
43 | <add-conversion type="PyObject" check="NpArray::isNpArray(%in)"> | |
|
44 | %out = %OUTTYPE(%in); | |
|
45 | </add-conversion> | |
|
46 | </target-to-native> | |
|
47 | </conversion-rule> | |
|
48 | </primitive-type> | |
|
49 | <object-type name="PyDataProvider"/> | |
|
35 | 50 | <object-type name="Product" /> |
|
36 | 51 | <object-type name="MainWindow" /> |
|
37 | 52 | <object-type name="SqpApplication"> |
|
38 | 53 | <modify-function signature="SqpApplication(int&,char**)" access="private"/> |
|
39 | 54 | </object-type> |
|
40 | <object-type name="Providers"> | |
|
41 | <modify-function signature="register_provider(PyDataProvider*)"> | |
|
42 | <modify-argument index="1"> | |
|
43 | <define-ownership owner="c++" /> | |
|
44 | </modify-argument> | |
|
45 | </modify-function> | |
|
46 | </object-type> | |
|
47 | 55 | <function signature="SqpApplication_ctor()" return-type="SqpApplication*"/> |
|
48 | 56 | <add-function signature="SqpApplication_ctor(PySequence)" return-type="SqpApplication*"> |
|
49 | 57 | <inject-code class="target"> |
@@ -55,27 +63,29 | |||
|
55 | 63 | </inject-code> |
|
56 | 64 | </add-function> |
|
57 | 65 | <function signature="init_resources()"/> |
|
58 | <primitive-type name="NpArray" target-lang-api-name="PyObject"> | |
|
59 | <include file-name="numpy_wrappers.h" location="local"/> | |
|
60 | <conversion-rule> | |
|
61 | <native-to-target> | |
|
62 | auto result = %in.py_object(); | |
|
63 | return result; | |
|
64 | </native-to-target> | |
|
65 | <target-to-native> | |
|
66 | <add-conversion type="PyObject" check="NpArray::isNpArray(%in)"> | |
|
67 | %out = %OUTTYPE(%in); | |
|
68 | </add-conversion> | |
|
69 | </target-to-native> | |
|
70 | </conversion-rule> | |
|
71 | </primitive-type> | |
|
72 | 66 | <function signature="load_plugins(const SqpApplication&)"/> |
|
73 | 67 | <object-type name="ScalarTimeSerie"> |
|
74 | 68 | <add-function signature="ScalarTimeSerie(NpArray&,NpArray&)" return-type="ScalarTimeSerie"> |
|
75 | 69 | <inject-code class="target"> |
|
76 | 70 | %BEGIN_ALLOW_THREADS |
|
77 | 71 | %0 = new ScalarTimeSerieWrapper(); |
|
78 |
%0.set_data( |
|
|
72 | %0.set_data(std::move(%1.data),std::move(%2.data)); | |
|
73 | %END_ALLOW_THREADS | |
|
74 | </inject-code> | |
|
75 | </add-function> | |
|
76 | <add-function signature="size()" return-type="int" access="public" static="no"> | |
|
77 | <inject-code class="target"> | |
|
78 | %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(); | |
|
79 | %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); | |
|
80 | </inject-code> | |
|
81 | </add-function> | |
|
82 | </object-type> | |
|
83 | <object-type name="VectorTimeSerie"> | |
|
84 | <add-function signature="VectorTimeSerie(NpArray&,NpArray&)" return-type="VectorTimeSerie"> | |
|
85 | <inject-code class="target"> | |
|
86 | %BEGIN_ALLOW_THREADS | |
|
87 | %0 = new VectorTimeSerieWrapper(); | |
|
88 | %0.set_data(std::move(%1.data),%2.to_std_vect_vect()); | |
|
79 | 89 | %END_ALLOW_THREADS |
|
80 | 90 | </inject-code> |
|
81 | 91 | </add-function> |
@@ -86,6 +96,8 | |||
|
86 | 96 | </inject-code> |
|
87 | 97 | </add-function> |
|
88 | 98 | </object-type> |
|
89 | <function signature="test_PyDataProvider(PyDataProvider&)"/> | |
|
90 | 99 | <function signature="test_np_array(NpArray&)"/> |
|
91 | 100 | </typesystem> |
|
101 | ||
|
102 | ||
|
103 |
@@ -99,5 +99,8 if __name__ == "__main__": | |||
|
99 | 99 | term = IPythonDockWidget(available_vars={"app":app, "main_window":main_window}, custom_banner="SciQLop IPython Console ") |
|
100 | 100 | main_window.addDockWidget(Qt.BottomDockWidgetArea, term) |
|
101 | 101 | main_window.show() |
|
102 | for file in os.listdir('plugins'): | |
|
103 | if os.path.isfile(f"plugins/{file}"): | |
|
104 | exec(open(f"plugins/{file}").read()) | |
|
102 | 105 | sys.exit(app.exec_()) |
|
103 | 106 |
@@ -1,6 +1,7 | |||
|
1 | 1 | #ifndef NUMPY_WRAPPERS_H |
|
2 | 2 | #define NUMPY_WRAPPERS_H |
|
3 | 3 | #include <Data/ScalarTimeSerie.h> |
|
4 | #include <Data/VectorTimeSerie.h> | |
|
4 | 5 | #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION |
|
5 | 6 | #if defined(slots) && (defined(__GNUC__) || defined(_MSC_VER) || defined(__clang__)) |
|
6 | 7 | #pragma push_macro("slots") |
@@ -42,10 +43,7 struct PyObjectWrapper | |||
|
42 | 43 | { |
|
43 | 44 | private: |
|
44 | 45 | PyObject* _py_obj = nullptr; |
|
45 | void inc_refcount() | |
|
46 | { | |
|
47 | Py_XINCREF(_py_obj); | |
|
48 | } | |
|
46 | void inc_refcount() { Py_XINCREF(_py_obj); } | |
|
49 | 47 | void dec_refcount() |
|
50 | 48 | { |
|
51 | 49 | Py_XDECREF(_py_obj); |
@@ -56,10 +54,7 public: | |||
|
56 | 54 | PyObjectWrapper() : _py_obj { nullptr } {} |
|
57 | 55 | PyObjectWrapper(const PyObjectWrapper& other) : _py_obj { other._py_obj } { inc_refcount(); } |
|
58 | 56 | PyObjectWrapper(PyObjectWrapper&& other) : _py_obj { other._py_obj } { inc_refcount(); } |
|
59 | explicit PyObjectWrapper(PyObject* obj) : _py_obj { obj } | |
|
60 | { | |
|
61 | inc_refcount(); | |
|
62 | } | |
|
57 | explicit PyObjectWrapper(PyObject* obj) : _py_obj { obj } { inc_refcount(); } | |
|
63 | 58 | ~PyObjectWrapper() { dec_refcount(); } |
|
64 | 59 | PyObjectWrapper& operator=(PyObjectWrapper&& other) |
|
65 | 60 | { |
@@ -81,13 +76,11 public: | |||
|
81 | 76 | inline bool is_null() { return _py_obj == nullptr; } |
|
82 | 77 | }; |
|
83 | 78 | |
|
84 | struct NpArray | |
|
79 | struct NpArray_view | |
|
85 | 80 | { |
|
86 | 81 | private: |
|
87 | 82 | PyObjectWrapper<PyArrayObject> _py_obj; |
|
88 | NpArray(NpArray& other) = delete; | |
|
89 | NpArray(const NpArray& other) = delete; | |
|
90 | NpArray(const NpArray&& other) = delete; | |
|
83 | NpArray_view(const NpArray_view&& other) = delete; | |
|
91 | 84 | |
|
92 | 85 | public: |
|
93 | 86 | static bool isNpArray(PyObject* obj) |
@@ -96,21 +89,22 public: | |||
|
96 | 89 | auto is_c_aray = obj && PyArray_Check(arr) && PyArray_ISCARRAY(arr); |
|
97 | 90 | return is_c_aray; |
|
98 | 91 | } |
|
99 | NpArray() : _py_obj { nullptr } {} | |
|
100 |
NpArray(NpArray |
|
|
101 | explicit NpArray(PyObject* obj) : _py_obj { obj } | |
|
92 | NpArray_view() : _py_obj { nullptr } {} | |
|
93 | NpArray_view(const NpArray_view& other) : _py_obj { other._py_obj } {} | |
|
94 | NpArray_view(NpArray_view&& other) : _py_obj { other._py_obj } {} | |
|
95 | explicit NpArray_view(PyObject* obj) : _py_obj { obj } | |
|
102 | 96 | { |
|
103 | 97 | assert(isNpArray(obj)); |
|
104 | 98 | assert(PyArray_ISFLOAT(_py_obj.get())); |
|
105 | 99 | } |
|
106 | 100 | |
|
107 | NpArray& operator=(const NpArray& other) | |
|
101 | NpArray_view& operator=(const NpArray_view& other) | |
|
108 | 102 | { |
|
109 | 103 | this->_py_obj = other._py_obj; |
|
110 | 104 | return *this; |
|
111 | 105 | } |
|
112 | 106 | |
|
113 | NpArray& operator=(NpArray&& other) | |
|
107 | NpArray_view& operator=(NpArray_view&& other) | |
|
114 | 108 | { |
|
115 | 109 | this->_py_obj = other._py_obj; |
|
116 | 110 | return *this; |
@@ -133,10 +127,32 public: | |||
|
133 | 127 | return shape; |
|
134 | 128 | } |
|
135 | 129 | |
|
130 | std::size_t ndim() | |
|
131 | { | |
|
132 | if (!_py_obj.is_null()) | |
|
133 | { | |
|
134 | return static_cast<std::size_t>(PyArray_NDIM(_py_obj.get())); | |
|
135 | } | |
|
136 | return 0; | |
|
137 | } | |
|
138 | ||
|
139 | std::size_t size(std::size_t index = 0) | |
|
140 | { | |
|
141 | if (!_py_obj.is_null()) | |
|
142 | { | |
|
143 | if (index < static_cast<std::size_t>(PyArray_NDIM(_py_obj.get()))) | |
|
144 | { | |
|
145 | return PyArray_SHAPE(_py_obj.get())[index]; | |
|
146 | } | |
|
147 | } | |
|
148 | return 0; | |
|
149 | } | |
|
150 | ||
|
136 | 151 | std::size_t flat_size() |
|
137 | 152 | { |
|
138 | 153 | auto s = this->shape(); |
|
139 |
return std::accumulate( |
|
|
154 | return std::accumulate( | |
|
155 | std::cbegin(s), std::cend(s), 1, [](const auto& a, const auto& b) { return a * b; }); | |
|
140 | 156 | } |
|
141 | 157 | |
|
142 | 158 | double data(std::size_t pos) |
@@ -150,6 +166,7 public: | |||
|
150 | 166 | |
|
151 | 167 | std::vector<double> to_std_vect() |
|
152 | 168 | { |
|
169 | assert(!this->_py_obj.is_null()); | |
|
153 | 170 | auto sz = flat_size(); |
|
154 | 171 | std::vector<double> v(sz); |
|
155 | 172 | auto d_ptr = reinterpret_cast<double*>(PyArray_DATA(_py_obj.get())); |
@@ -157,12 +174,77 public: | |||
|
157 | 174 | return v; |
|
158 | 175 | } |
|
159 | 176 | |
|
177 | std::vector<VectorTimeSerie::raw_value_type> to_std_vect_vect() | |
|
178 | { | |
|
179 | auto sz = size(0); | |
|
180 | std::vector<VectorTimeSerie::raw_value_type> v(sz); | |
|
181 | if (sz) | |
|
182 | { | |
|
183 | assert(ndim() == 2); | |
|
184 | assert(size(1) == 3); | |
|
185 | auto d_ptr | |
|
186 | = reinterpret_cast<VectorTimeSerie::raw_value_type*>(PyArray_DATA(_py_obj.get())); | |
|
187 | std::copy(d_ptr, d_ptr + sz, std::begin(v)); | |
|
188 | } | |
|
189 | return v; | |
|
190 | } | |
|
191 | ||
|
160 | 192 | PyObject* py_object() { return _py_obj.py_object(); } |
|
161 | 193 | }; |
|
162 | 194 | |
|
195 | struct NpArray | |
|
196 | { | |
|
197 | std::vector<std::size_t> shape; | |
|
198 | std::vector<double> data; | |
|
199 | static bool isNpArray(PyObject* obj) { return NpArray_view::isNpArray(obj); } | |
|
200 | NpArray() = default; | |
|
201 | explicit NpArray(PyObject* obj) | |
|
202 | { | |
|
203 | if (obj) | |
|
204 | { | |
|
205 | NpArray_view view { obj }; | |
|
206 | shape = view.shape(); | |
|
207 | data = view.to_std_vect(); | |
|
208 | } | |
|
209 | } | |
|
210 | ||
|
211 | inline std::size_t ndim() { return shape.size(); } | |
|
212 | ||
|
213 | std::size_t size(std::size_t index = 0) | |
|
214 | { | |
|
215 | if (index < shape.size()) | |
|
216 | return shape[index]; | |
|
217 | return 0; | |
|
218 | } | |
|
219 | ||
|
220 | std::size_t flat_size() | |
|
221 | { | |
|
222 | return std::accumulate(std::cbegin(shape), std::cend(shape), 1, | |
|
223 | [](const auto& a, const auto& b) { return a * b; }); | |
|
224 | } | |
|
225 | ||
|
226 | // TODO temporary hack should find a way to avoid this copy | |
|
227 | std::vector<VectorTimeSerie::raw_value_type> to_std_vect_vect() | |
|
228 | { | |
|
229 | auto sz = size(0); | |
|
230 | std::vector<VectorTimeSerie::raw_value_type> v(sz); | |
|
231 | if (sz) | |
|
232 | { | |
|
233 | assert(ndim() == 2); | |
|
234 | assert(size(1) == 3); | |
|
235 | auto d_ptr = reinterpret_cast<VectorTimeSerie::raw_value_type*>(data.data()); | |
|
236 | std::copy(d_ptr, d_ptr + sz, std::begin(v)); | |
|
237 | } | |
|
238 | return v; | |
|
239 | } | |
|
240 | ||
|
241 | // TODO maybe ;) | |
|
242 | PyObject* py_object() { return nullptr; } | |
|
243 | }; | |
|
244 | ||
|
163 | 245 | inline int test_np_array(NpArray& arr) |
|
164 | 246 | { |
|
165 |
auto shape = arr.shape |
|
|
247 | auto shape = arr.shape; | |
|
166 | 248 | std::cout << "len(shape)=" << shape.size() << std::endl; |
|
167 | 249 | std::for_each(std::cbegin(shape), std::cend(shape), [](auto sz) { |
|
168 | 250 | static int i = 0; |
@@ -171,7 +253,7 inline int test_np_array(NpArray& arr) | |||
|
171 | 253 | auto flatsize = std::accumulate(std::cbegin(shape), std::cend(shape), 0); |
|
172 | 254 | for (auto i = 0; i < flatsize; i++) |
|
173 | 255 | { |
|
174 |
std::cout << "data[" << i << "]=" << arr.data |
|
|
256 | std::cout << "data[" << i << "]=" << arr.data[i] << std::endl; | |
|
175 | 257 | } |
|
176 | 258 | return 1; |
|
177 | 259 | } |
General Comments 0
You need to be logged in to leave comments.
Login now