##// END OF EJS Templates
Mini code clean on PyDataProvider...
jeandet -
r1491:317c38eb0f17
parent child
Show More
@@ -1,111 +1,129
1 1 #pragma once
2 2 #include <Data/DataProviderParameters.h>
3 3 #include <Data/DataSeriesType.h>
4 4 #include <Data/IDataProvider.h>
5 5 #include <DataSource/DataSourceController.h>
6 6 #include <DataSource/DataSourceItem.h>
7 7 #include <DataSource/DataSourceItemAction.h>
8 8 #include <QPair>
9 9 #include <SqpApplication.h>
10 10 // must be included last because of Python/Qt definition of slots
11 11 #include "numpy_wrappers.h"
12 12
13 13 struct Product
14 14 {
15 15 QString path;
16 16 std::vector<std::string> components;
17 17 QMap<QString, QString> metadata;
18 18 Product() = default;
19 19 explicit Product(const QString& path, const std::vector<std::string>& components,
20 20 const QMap<QString, QString>& metadata)
21 21 : path { path }, components { components }, metadata { metadata }
22 22 {
23 23 }
24 24 ~Product() = default;
25 25 };
26 26
27 template <typename T>
28 ScalarTimeSerie* make_scalar(T& t, T& y)
29 {
30 return new ScalarTimeSerie { std::move(t.data), std::move(y.data) };
31 }
32
33 template <typename T>
34 VectorTimeSerie* make_vector(T& t, T& y)
35 {
36 return new VectorTimeSerie { std::move(t.data), y.to_std_vect_vect() };
37 }
38
39 template <typename T>
40 MultiComponentTimeSerie* make_multi_comp(T& t, T& y)
41 {
42 auto y_size = y.flat_size();
43 auto t_size = t.flat_size();
44 if (t_size && (y_size % t_size) == 0)
45 {
46 return new MultiComponentTimeSerie { std::move(t.data), std::move(y.data),
47 { t_size, y_size / t_size } };
48 }
49 return nullptr;
50 }
51
52 template <typename T>
53 SpectrogramTimeSerie* make_spectro(T& t, T& y)
54 {
55 auto y_size = y.flat_size();
56 auto t_size = t.flat_size();
57 if (t_size && (y_size % t_size) == 0)
58 {
59 return new SpectrogramTimeSerie { std::move(t.data), std::move(y.data),
60 { t_size, y_size / t_size } };
61 }
62 return nullptr;
63 }
64
65
27 66 class PyDataProvider : public IDataProvider
28 67 {
29 68 public:
30 69 PyDataProvider()
31 70 {
32 71 auto& dataSourceController = sqpApp->dataSourceController();
33 72 dataSourceController.registerProvider(this);
34 73 }
35 74
36 75 virtual ~PyDataProvider() {}
37 76
38 virtual QPair<QPair<NpArray,NpArray>,DataSeriesType> get_data(const QMap<QString,QString>& key, double start_time, double stop_time)
77 virtual QPair<QPair<NpArray, NpArray>, DataSeriesType> get_data(
78 const QMap<QString, QString>& key, double start_time, double stop_time)
39 79 {
40 80 (void)key, (void)start_time, (void)stop_time;
41 81 return {};
42 82 }
43 83
44 84 virtual TimeSeries::ITimeSerie* getData(const DataProviderParameters& parameters) override
45 85 {
86 TimeSeries::ITimeSerie* ts = nullptr;
46 87 if (parameters.m_Data.contains("name"))
47 88 {
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,
53 parameters.m_Range.m_TStart, parameters.m_Range.m_TEnd);
54 // TODO add shape/type switch
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();
89 QMap<QString, QString> metadata;
90 std::for_each(parameters.m_Data.constKeyValueBegin(),
91 parameters.m_Data.constKeyValueEnd(),
92 [&metadata](const auto& item) { metadata[item.first] = item.second.toString(); });
93 auto [data, type]
94 = get_data(metadata, parameters.m_Range.m_TStart, parameters.m_Range.m_TEnd);
72 95
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 }
96 auto& [t, y] = data;
97 switch (type)
98 {
99 case DataSeriesType::SCALAR:
100 ts = make_scalar(t, y);
101 break;
102 case DataSeriesType::VECTOR:
103 ts = make_vector(t, y);
104 break;
105 case DataSeriesType::MULTICOMPONENT:
106 ts = make_multi_comp(t, y);
107 break;
108 case DataSeriesType::SPECTROGRAM:
109 ts = make_spectro(t, y);
110 break;
111 default:
112 break;
95 113 }
96 114 }
97 return nullptr;
115 return ts;
98 116 }
99 117
100 118
101 119 inline void register_products(const QVector<Product*>& products)
102 120 {
103 121 auto& dataSourceController = sqpApp->dataSourceController();
104 122 auto id = this->id();
105 123 auto data_source_name = this->name();
106 124 std::for_each(std::cbegin(products), std::cend(products),
107 125 [&id, &dataSourceController](const Product* product) {
108 126 dataSourceController.setDataSourceItem(id, product->path, product->metadata);
109 127 });
110 128 }
111 129 };
General Comments 0
You need to be logged in to leave comments. Login now