PyDataProvider.h
136 lines
| 4.1 KiB
| text/x-c
|
CLexer
r1477 | #pragma once | |||
r1478 | #include <Data/DataProviderParameters.h> | |||
r1479 | #include <Data/DataSeriesType.h> | |||
r1477 | #include <Data/IDataProvider.h> | |||
r1478 | #include <DataSource/DataSourceItem.h> | |||
#include <DataSource/DataSourceItemAction.h> | ||||
r1495 | #include <DataSource/datasources.h> | |||
r1478 | #include <QPair> | |||
r1498 | #include <QList> | |||
r1478 | #include <SqpApplication.h> | |||
// must be included last because of Python/Qt definition of slots | ||||
#include "numpy_wrappers.h" | ||||
struct Product | ||||
{ | ||||
QString path; | ||||
std::vector<std::string> components; | ||||
QMap<QString, QString> metadata; | ||||
Product() = default; | ||||
explicit Product(const QString& path, const std::vector<std::string>& components, | ||||
const QMap<QString, QString>& metadata) | ||||
: path { path }, components { components }, metadata { metadata } | ||||
{ | ||||
} | ||||
r1479 | ~Product() = default; | |||
r1478 | }; | |||
r1477 | ||||
r1491 | template <typename T> | |||
ScalarTimeSerie* make_scalar(T& t, T& y) | ||||
{ | ||||
return new ScalarTimeSerie { std::move(t.data), std::move(y.data) }; | ||||
} | ||||
template <typename T> | ||||
VectorTimeSerie* make_vector(T& t, T& y) | ||||
{ | ||||
return new VectorTimeSerie { std::move(t.data), y.to_std_vect_vect() }; | ||||
} | ||||
template <typename T> | ||||
MultiComponentTimeSerie* make_multi_comp(T& t, T& y) | ||||
{ | ||||
auto y_size = y.flat_size(); | ||||
auto t_size = t.flat_size(); | ||||
if (t_size && (y_size % t_size) == 0) | ||||
{ | ||||
return new MultiComponentTimeSerie { std::move(t.data), std::move(y.data), | ||||
{ t_size, y_size / t_size } }; | ||||
} | ||||
return nullptr; | ||||
} | ||||
template <typename T> | ||||
r1498 | SpectrogramTimeSerie* make_spectro(T& t, T& y, T& z) | |||
r1491 | { | |||
r1498 | auto z_size = z.flat_size(); | |||
r1491 | auto t_size = t.flat_size(); | |||
r1498 | if (t_size && (z_size % t_size) == 0) | |||
r1491 | { | |||
r1498 | return new SpectrogramTimeSerie { std::move(t.data), std::move(y.data), std::move(z.data), | |||
{ t_size, z_size / t_size }, std::nan("1"), std::nan("1") }; | ||||
r1491 | } | |||
return nullptr; | ||||
} | ||||
r1477 | class PyDataProvider : public IDataProvider | |||
{ | ||||
public: | ||||
r1478 | PyDataProvider() | |||
{ | ||||
r1495 | auto& dataSources = sqpApp->dataSources(); | |||
dataSources.addProvider(this); | ||||
r1478 | } | |||
virtual ~PyDataProvider() {} | ||||
r1477 | ||||
r1498 | virtual QPair<QPair<QPair<NpArray,NpArray>,NpArray>, DataSeriesType> get_data( | |||
r1491 | const QMap<QString, QString>& key, double start_time, double stop_time) | |||
r1478 | { | |||
(void)key, (void)start_time, (void)stop_time; | ||||
return {}; | ||||
} | ||||
r1477 | ||||
r1478 | virtual TimeSeries::ITimeSerie* getData(const DataProviderParameters& parameters) override | |||
r1477 | { | |||
r1491 | TimeSeries::ITimeSerie* ts = nullptr; | |||
r1478 | if (parameters.m_Data.contains("name")) | |||
{ | ||||
r1491 | QMap<QString, QString> metadata; | |||
std::for_each(parameters.m_Data.constKeyValueBegin(), | ||||
parameters.m_Data.constKeyValueEnd(), | ||||
[&metadata](const auto& item) { metadata[item.first] = item.second.toString(); }); | ||||
auto [data, type] | ||||
= get_data(metadata, parameters.m_Range.m_TStart, parameters.m_Range.m_TEnd); | ||||
r1479 | ||||
r1498 | auto& [axes, values] = data; | |||
auto& [x, y] = axes; | ||||
r1491 | switch (type) | |||
{ | ||||
case DataSeriesType::SCALAR: | ||||
r1498 | ts = make_scalar(x, values); | |||
r1491 | break; | |||
case DataSeriesType::VECTOR: | ||||
r1498 | ts = make_vector(x, values); | |||
r1491 | break; | |||
case DataSeriesType::MULTICOMPONENT: | ||||
r1498 | ts = make_multi_comp(x, values); | |||
r1491 | break; | |||
case DataSeriesType::SPECTROGRAM: | ||||
r1498 | ts = make_spectro(x, y, values); | |||
r1491 | break; | |||
default: | ||||
break; | ||||
r1479 | } | |||
r1478 | } | |||
r1491 | return ts; | |||
r1477 | } | |||
r1478 | ||||
r1496 | inline void set_icon(const QString& path, const QString& name) | |||
{ | ||||
sqpApp->dataSources().setIcon(path, name); | ||||
} | ||||
r1478 | ||||
inline void register_products(const QVector<Product*>& products) | ||||
{ | ||||
r1495 | auto& dataSources = sqpApp->dataSources(); | |||
r1478 | auto id = this->id(); | |||
auto data_source_name = this->name(); | ||||
std::for_each(std::cbegin(products), std::cend(products), | ||||
r1495 | [&id, &dataSources](const Product* product) { | |||
dataSources.addDataSourceItem(id, product->path, product->metadata); | ||||
r1478 | }); | |||
} | ||||
r1477 | }; | |||