@@ -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 |
@@ -1,79 +1,80 | |||
|
1 | 1 | find_package(PythonLibs 3 REQUIRED) |
|
2 | 2 | find_package(PythonInterp 3 REQUIRED) |
|
3 | 3 | find_package(PySide2 REQUIRED) |
|
4 | 4 | find_package(Shiboken2 REQUIRED) |
|
5 | 5 | include(PythonInfo) |
|
6 | 6 | find_python_site_packages(PYTHON_SITE_PACKAGES) |
|
7 | 7 | |
|
8 | 8 | set(BINDINGS_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}") |
|
9 | 9 | 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 | |
|
16 | 17 | set_property(SOURCE ${BINDINGS_SOURCE} PROPERTY SKIP_AUTOGEN ON) |
|
17 | 18 | |
|
18 | 19 | list(APPEND BINDINGS_INCLUDE_DIRS |
|
19 | 20 | ${PYTHON_INCLUDE_DIRS} |
|
20 | 21 | ${Qt5Core_INCLUDE_DIRS} |
|
21 | 22 | ${Qt5Widgets_INCLUDE_DIRS} |
|
22 | 23 | ${Qt5Gui_INCLUDE_DIRS} |
|
23 | 24 | ${CMAKE_CURRENT_SOURCE_DIR}/../../gui/include |
|
24 | 25 | ${CMAKE_CURRENT_SOURCE_DIR}/../../core/include |
|
25 | 26 | ${CMAKE_CURRENT_SOURCE_DIR}/../../core/external/TimeSeries/include |
|
26 | 27 | ) |
|
27 | 28 | list(REMOVE_DUPLICATES BINDINGS_INCLUDE_DIRS) |
|
28 | 29 | foreach(DIR ${BINDINGS_INCLUDE_DIRS}) |
|
29 | 30 | list(APPEND BINDINGS_INCLUDE_DIRS_ARGS "-I${DIR}") |
|
30 | 31 | endforeach() |
|
31 | 32 | |
|
32 | 33 | set(SHIBOKEN_OPTIONS --generator-set=shiboken |
|
33 | 34 | --enable-parent-ctor-heuristic |
|
34 | 35 | --enable-return-value-heuristic |
|
35 | 36 | --use-isnull-as-nb_nonzero |
|
36 | 37 | --avoid-protected-hack |
|
37 | 38 | --enable-pyside-extensions |
|
38 | 39 | -std=c++17) |
|
39 | 40 | add_custom_command( |
|
40 | 41 | OUTPUT ${BINDINGS_SOURCE} |
|
41 | 42 | COMMAND Shiboken2::shiboken2 ${SHIBOKEN_OPTIONS} |
|
42 | 43 | ${BINDINGS_INCLUDE_DIRS_ARGS} |
|
43 | 44 | --typesystem-paths=${PYSIDE_TYPESYSTEMS} |
|
44 | 45 | --output-directory=${CMAKE_CURRENT_BINARY_DIR} |
|
45 | 46 | ${CMAKE_CURRENT_SOURCE_DIR}/bindings.h ${CMAKE_CURRENT_SOURCE_DIR}/bindings.xml |
|
46 | 47 | |
|
47 | 48 | DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/bindings.xml" |
|
48 | 49 | IMPLICIT_DEPENDS CXX "${CMAKE_CURRENT_SOURCE_DIR}/bindings.h" |
|
49 | 50 | WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} |
|
50 | 51 | COMMENT "Generating Python bindings with shiboken2") |
|
51 | 52 | |
|
52 | 53 | include_directories( |
|
53 | 54 | ${PYSIDE_INCLUDE_DIR}/QtCore |
|
54 | 55 | ${PYSIDE_INCLUDE_DIR}/QtGui |
|
55 | 56 | ${PYSIDE_INCLUDE_DIR}/QtWidgets) |
|
56 | 57 | |
|
57 | 58 | include_directories( |
|
58 | 59 | ${PYTHON_SITE_PACKAGES}/numpy/core/include/ |
|
59 | 60 | ${PYTHON_INCLUDE_DIRS} |
|
60 | 61 | ${SHIBOKEN_INCLUDE_DIR} |
|
61 | 62 | ${PYSIDE_INCLUDE_DIR} |
|
62 | 63 | ${PYSIDE_INCLUDE_DIR}/QtCore |
|
63 | 64 | ${PYSIDE_INCLUDE_DIR}/QtGui |
|
64 | 65 | ${PYSIDE_INCLUDE_DIR}/QtWidgets) |
|
65 | 66 | |
|
66 | 67 | add_library(SciQLopBindings MODULE ${BINDINGS_SOURCE} numpy_wrappers.h PyDataProvider.h) |
|
67 | 68 | set_target_properties( |
|
68 | 69 | SciQLopBindings |
|
69 | 70 | PROPERTIES |
|
70 | 71 | PREFIX "" |
|
71 | 72 | OUTPUT_NAME "SciQLopBindings" |
|
72 | 73 | ) |
|
73 | 74 | target_link_libraries(SciQLopBindings sciqlopapp) |
|
74 | 75 | target_link_libraries(SciQLopBindings Shiboken2::libshiboken) |
|
75 | 76 | target_link_libraries(SciQLopBindings PySide2::pyside2) |
|
76 | 77 | |
|
77 | 78 | add_executable(debug_sciqlop_app main.cpp ) |
|
78 | 79 | find_package (Python3 COMPONENTS Development) |
|
79 | 80 | target_link_libraries(debug_sciqlop_app PRIVATE Python3::Python) |
@@ -1,89 +1,111 | |||
|
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> |
|
6 | 7 | #include <DataSource/DataSourceItemAction.h> |
|
7 | 8 | #include <QPair> |
|
8 | 9 | #include <SqpApplication.h> |
|
9 | 10 | // must be included last because of Python/Qt definition of slots |
|
10 | 11 | #include "numpy_wrappers.h" |
|
11 | 12 | |
|
12 | 13 | struct Product |
|
13 | 14 | { |
|
14 | 15 | QString path; |
|
15 | 16 | std::vector<std::string> components; |
|
16 | 17 | QMap<QString, QString> metadata; |
|
17 | 18 | Product() = default; |
|
18 | 19 | explicit Product(const QString& path, const std::vector<std::string>& components, |
|
19 | 20 | const QMap<QString, QString>& metadata) |
|
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 |
|
27 | 28 | { |
|
28 | 29 | public: |
|
29 | 30 | PyDataProvider() |
|
30 | 31 | { |
|
31 | 32 | auto& dataSourceController = sqpApp->dataSourceController(); |
|
32 | 33 | dataSourceController.registerProvider(this); |
|
33 | 34 | } |
|
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 {}; |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | virtual TimeSeries::ITimeSerie* getData(const DataProviderParameters& parameters) override |
|
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 | } |
|
55 | 99 | |
|
56 | 100 | |
|
57 | 101 | inline void register_products(const QVector<Product*>& products) |
|
58 | 102 | { |
|
59 | 103 | auto& dataSourceController = sqpApp->dataSourceController(); |
|
60 | 104 | auto id = this->id(); |
|
61 | 105 | auto data_source_name = this->name(); |
|
62 | 106 | std::for_each(std::cbegin(products), std::cend(products), |
|
63 | 107 | [&id, &dataSourceController](const Product* product) { |
|
64 | 108 | dataSourceController.setDataSourceItem(id, product->path, product->metadata); |
|
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 | } |
@@ -1,12 +1,14 | |||
|
1 | 1 | #ifndef SCIQLOP_BINDINGS_H |
|
2 | 2 | #define SCIQLOP_BINDINGS_H |
|
3 | 3 | #define QT_ANNOTATE_ACCESS_SPECIFIER(a) __attribute__((annotate(#a))) |
|
4 | 4 | #include "../include/MainWindow.h" |
|
5 | 5 | #include "PyDataProvider.h" |
|
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 | |
|
12 | 14 | #endif // SCIQLOP_BINDINGS_H |
@@ -1,91 +1,103 | |||
|
1 | 1 | <?xml version="1.0"?> |
|
2 | 2 | <typesystem package="SciQLopBindings"> |
|
3 | 3 | <load-typesystem name="typesystem_core.xml" generate="no" /> |
|
4 | 4 | <load-typesystem name="typesystem_gui.xml" generate="no" /> |
|
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> |
|
11 | 12 | <native-to-target> |
|
12 | 13 | %INTYPE::size_type vectorSize = %in.size(); |
|
13 | 14 | PyObject* %out = PyList_New((int) vectorSize); |
|
14 | 15 | for (%INTYPE::size_type idx = 0; idx < vectorSize; ++idx) { |
|
15 | 16 | %INTYPE_0 cppItem(%in[idx]); |
|
16 | 17 | PyList_SET_ITEM(%out, idx, %CONVERTTOPYTHON[%INTYPE_0](cppItem)); |
|
17 | 18 | } |
|
18 | 19 | return %out; |
|
19 | 20 | </native-to-target> |
|
20 | 21 | <target-to-native> |
|
21 | 22 | <add-conversion type="PySequence"> |
|
22 | 23 | Shiboken::AutoDecRef seq(PySequence_Fast(%in, 0)); |
|
23 | 24 | int vectorSize = PySequence_Fast_GET_SIZE(seq.object()); |
|
24 | 25 | %out.reserve(vectorSize); |
|
25 | 26 | for (int idx = 0; idx < vectorSize; ++idx ) { |
|
26 | 27 | PyObject* pyItem = PySequence_Fast_GET_ITEM(seq.object(), idx); |
|
27 | 28 | %OUTTYPE_0 cppItem = %CONVERTTOCPP[%OUTTYPE_0](pyItem); |
|
28 | 29 | %out.push_back(cppItem); |
|
29 | 30 | } |
|
30 | 31 | </add-conversion> |
|
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"> |
|
50 | 58 | static int argc; |
|
51 | 59 | static char **argv; |
|
52 | 60 | Shiboken::listToArgcArgv(%1, &argc, &argv, "PySideApp"); |
|
53 | 61 | auto retval = new SqpApplication(argc,argv); |
|
54 | 62 | %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](retval); |
|
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> |
|
82 | 92 | <add-function signature="size()" return-type="int" access="public" static="no"> |
|
83 | 93 | <inject-code class="target"> |
|
84 | 94 | %RETURN_TYPE %0 = %CPPSELF.%FUNCTION_NAME(); |
|
85 | 95 | %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%0); |
|
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 |
@@ -1,103 +1,106 | |||
|
1 | 1 | # This Python file uses the following encoding: utf-8 |
|
2 | 2 | import os |
|
3 | 3 | print(os.getcwd()) |
|
4 | 4 | import sys |
|
5 | 5 | from PySide2.QtWidgets import QApplication, QMainWindow, QDockWidget |
|
6 | 6 | from PySide2.QtCore import QSize, Qt |
|
7 | 7 | from PySide2 import QtGui |
|
8 | 8 | import os |
|
9 | 9 | sys.path.append(os.getcwd()) |
|
10 | 10 | from SciQLopBindings import SqpApplication, MainWindow, init_resources, load_plugins, SqpApplication_ctor |
|
11 | 11 | from qtconsole.rich_ipython_widget import RichJupyterWidget |
|
12 | 12 | from qtconsole.inprocess import QtInProcessKernelManager |
|
13 | 13 | |
|
14 | 14 | |
|
15 | 15 | class IPythonWidget(RichJupyterWidget): |
|
16 | 16 | """Live IPython console widget. |
|
17 | 17 | |
|
18 | 18 | .. image:: img/IPythonWidget.png |
|
19 | 19 | |
|
20 | 20 | :param custom_banner: Custom welcome message to be printed at the top of |
|
21 | 21 | the console. |
|
22 | 22 | """ |
|
23 | 23 | |
|
24 | 24 | def __init__(self, parent=None, custom_banner=None, *args, **kwargs): |
|
25 | 25 | if parent is not None: |
|
26 | 26 | kwargs["parent"] = parent |
|
27 | 27 | super(IPythonWidget, self).__init__(*args, **kwargs) |
|
28 | 28 | if custom_banner is not None: |
|
29 | 29 | self.banner = custom_banner |
|
30 | 30 | self.setWindowTitle(self.banner) |
|
31 | 31 | self.kernel_manager = kernel_manager = QtInProcessKernelManager() |
|
32 | 32 | kernel_manager.start_kernel() |
|
33 | 33 | self.kernel_client = kernel_client = self._kernel_manager.client() |
|
34 | 34 | kernel_client.start_channels() |
|
35 | 35 | |
|
36 | 36 | def stop(): |
|
37 | 37 | kernel_client.stop_channels() |
|
38 | 38 | kernel_manager.shutdown_kernel() |
|
39 | 39 | self.exit_requested.connect(stop) |
|
40 | 40 | |
|
41 | 41 | def sizeHint(self): |
|
42 | 42 | """Return a reasonable default size for usage in :class:`PlotWindow`""" |
|
43 | 43 | return QSize(500, 300) |
|
44 | 44 | |
|
45 | 45 | def pushVariables(self, variable_dict): |
|
46 | 46 | """ Given a dictionary containing name / value pairs, push those |
|
47 | 47 | variables to the IPython console widget. |
|
48 | 48 | |
|
49 | 49 | :param variable_dict: Dictionary of variables to be pushed to the |
|
50 | 50 | console's interactive namespace (```{variable_name: object, β¦}```) |
|
51 | 51 | """ |
|
52 | 52 | self.kernel_manager.kernel.shell.push(variable_dict) |
|
53 | 53 | |
|
54 | 54 | |
|
55 | 55 | class IPythonDockWidget(QDockWidget): |
|
56 | 56 | """Dock Widget including a :class:`IPythonWidget` inside |
|
57 | 57 | a vertical layout. |
|
58 | 58 | |
|
59 | 59 | .. image:: img/IPythonDockWidget.png |
|
60 | 60 | |
|
61 | 61 | :param available_vars: Dictionary of variables to be pushed to the |
|
62 | 62 | console's interactive namespace: ``{"variable_name": object, β¦}`` |
|
63 | 63 | :param custom_banner: Custom welcome message to be printed at the top of |
|
64 | 64 | the console |
|
65 | 65 | :param title: Dock widget title |
|
66 | 66 | :param parent: Parent :class:`qt.QMainWindow` containing this |
|
67 | 67 | :class:`qt.QDockWidget` |
|
68 | 68 | """ |
|
69 | 69 | def __init__(self, parent=None, available_vars=None, custom_banner=None, |
|
70 | 70 | title="Console"): |
|
71 | 71 | super(IPythonDockWidget, self).__init__(title, parent) |
|
72 | 72 | |
|
73 | 73 | self.ipyconsole = IPythonWidget(custom_banner=custom_banner) |
|
74 | 74 | |
|
75 | 75 | self.layout().setContentsMargins(0, 0, 0, 0) |
|
76 | 76 | self.setWidget(self.ipyconsole) |
|
77 | 77 | |
|
78 | 78 | if available_vars is not None: |
|
79 | 79 | self.ipyconsole.pushVariables(available_vars) |
|
80 | 80 | self.ipyconsole.pushVariables({"blah":self}) |
|
81 | 81 | |
|
82 | 82 | def showEvent(self, event): |
|
83 | 83 | """Make sure this widget is raised when it is shown |
|
84 | 84 | (when it is first created as a tab in PlotWindow or when it is shown |
|
85 | 85 | again after hiding). |
|
86 | 86 | """ |
|
87 | 87 | self.raise_() |
|
88 | 88 | |
|
89 | 89 | def print_process_id(): |
|
90 | 90 | print ('Process ID is:', os.getpid()) |
|
91 | 91 | |
|
92 | 92 | |
|
93 | 93 | if __name__ == "__main__": |
|
94 | 94 | init_resources() |
|
95 | 95 | app = SqpApplication_ctor(sys.argv) |
|
96 | 96 | QtGui.qApp = app |
|
97 | 97 | load_plugins(app) |
|
98 | 98 | main_window = MainWindow() |
|
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,179 +1,261 | |||
|
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") |
|
7 | 8 | #undef slots |
|
8 | 9 | extern "C" |
|
9 | 10 | { |
|
10 | 11 | /* |
|
11 | 12 | * Python 2 uses the "register" keyword, which is deprecated in C++ 11 |
|
12 | 13 | * and forbidden in C++17. |
|
13 | 14 | */ |
|
14 | 15 | #if defined(__clang__) |
|
15 | 16 | #pragma clang diagnostic push |
|
16 | 17 | #pragma clang diagnostic ignored "-Wdeprecated-register" |
|
17 | 18 | #endif |
|
18 | 19 | |
|
19 | 20 | #include <Python.h> |
|
20 | 21 | #include <numpy/arrayobject.h> |
|
21 | 22 | |
|
22 | 23 | #if defined(__clang__) |
|
23 | 24 | #pragma clang diagnostic pop |
|
24 | 25 | #endif |
|
25 | 26 | } |
|
26 | 27 | #else |
|
27 | 28 | #include <Python.h> |
|
28 | 29 | #include <numpy/arrayobject.h> |
|
29 | 30 | #endif |
|
30 | 31 | #include <assert.h> |
|
31 | 32 | |
|
32 | 33 | #include <map> |
|
33 | 34 | |
|
34 | 35 | inline int init_numpy() |
|
35 | 36 | { |
|
36 | 37 | import_array(); // PyError if not successful |
|
37 | 38 | return 0; |
|
38 | 39 | } |
|
39 | 40 | const static int numpy_initialized = init_numpy(); |
|
40 | 41 | template <typename dest_type = PyObject> |
|
41 | 42 | 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); |
|
52 | 50 | _py_obj = nullptr; |
|
53 | 51 | } |
|
54 | 52 | |
|
55 | 53 | 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 | { |
|
66 | 61 | dec_refcount(); |
|
67 | 62 | this->_py_obj = other._py_obj; |
|
68 | 63 | inc_refcount(); |
|
69 | 64 | return *this; |
|
70 | 65 | } |
|
71 | 66 | PyObjectWrapper& operator=(const PyObjectWrapper& other) |
|
72 | 67 | { |
|
73 | 68 | dec_refcount(); |
|
74 | 69 | this->_py_obj = other._py_obj; |
|
75 | 70 | inc_refcount(); |
|
76 | 71 | return *this; |
|
77 | 72 | } |
|
78 | 73 | |
|
79 | 74 | PyObject* py_object() { return _py_obj; } |
|
80 | 75 | inline dest_type* get() { return reinterpret_cast<dest_type*>(_py_obj); } |
|
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) |
|
94 | 87 | { |
|
95 | 88 | auto arr = reinterpret_cast<PyArrayObject*>(obj); |
|
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; |
|
117 | 111 | } |
|
118 | 112 | |
|
119 | 113 | std::vector<std::size_t> shape() |
|
120 | 114 | { |
|
121 | 115 | std::vector<std::size_t> shape; |
|
122 | 116 | if (!_py_obj.is_null()) |
|
123 | 117 | { |
|
124 | 118 | if (int ndim = PyArray_NDIM(_py_obj.get()); ndim > 0) |
|
125 | 119 | { |
|
126 | 120 | if (ndim < 10) |
|
127 | 121 | { |
|
128 | 122 | shape.resize(ndim); |
|
129 | 123 | std::copy_n(PyArray_SHAPE(_py_obj.get()), ndim, std::begin(shape)); |
|
130 | 124 | } |
|
131 | 125 | } |
|
132 | 126 | } |
|
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) |
|
143 | 159 | { |
|
144 | 160 | if (!_py_obj.is_null()) |
|
145 | 161 | { |
|
146 | 162 | return reinterpret_cast<double*>(PyArray_DATA(_py_obj.get()))[pos]; |
|
147 | 163 | } |
|
148 | 164 | return nan("NAN"); |
|
149 | 165 | } |
|
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())); |
|
156 | 173 | std::copy(d_ptr, d_ptr + sz, std::begin(v)); |
|
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; |
|
169 | 251 | std::cout << "shape[" << i++ << "]=" << sz << std::endl; |
|
170 | 252 | }); |
|
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 | } |
|
178 | 260 | |
|
179 | 261 | #endif //#ifndef NUMPY_WRAPPERS_H |
General Comments 0
You need to be logged in to leave comments.
Login now