@@ -1,1 +1,1 | |||
|
1 | Subproject commit 39bf3ff40b41fc01170241f3e471c708c866118b | |
|
1 | Subproject commit 698d7cfa01b05427c2377ce2799f1290b9eab2ca |
@@ -1,523 +1,556 | |||
|
1 | 1 | #include "Visualization/VisualizationGraphHelper.h" |
|
2 | 2 | #include "Visualization/qcustomplot.h" |
|
3 | 3 | |
|
4 | 4 | #include <Data/ScalarTimeSerie.h> |
|
5 | 5 | #include <Data/SpectrogramTimeSerie.h> |
|
6 | 6 | #include <Data/VectorTimeSerie.h> |
|
7 | 7 | |
|
8 | 8 | #include <Variable/Variable2.h> |
|
9 | 9 | |
|
10 | 10 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper") |
|
11 | 11 | |
|
12 | 12 | namespace |
|
13 | 13 | { |
|
14 | 14 | |
|
15 | 15 | class SqpDataContainer : public QCPGraphDataContainer |
|
16 | 16 | { |
|
17 | 17 | public: |
|
18 | 18 | void appendGraphData(const QCPGraphData& data) { mData.append(data); } |
|
19 | 19 | }; |
|
20 | 20 | |
|
21 | 21 | /** |
|
22 | 22 | * Struct used to create plottables, depending on the type of the data series from which to create |
|
23 | 23 | * them |
|
24 | 24 | * @tparam T the data series' type |
|
25 | 25 | * @remarks Default implementation can't create plottables |
|
26 | 26 | */ |
|
27 | 27 | template <typename T, typename Enabled = void> |
|
28 | 28 | struct PlottablesCreator |
|
29 | 29 | { |
|
30 | 30 | static PlottablesMap createPlottables(QCustomPlot&, const std::shared_ptr<T>& dataSeries) |
|
31 | 31 | { |
|
32 | 32 | return {}; |
|
33 | 33 | } |
|
34 | 34 | }; |
|
35 | 35 | |
|
36 | 36 | PlottablesMap createGraphs(QCustomPlot& plot, int nbGraphs) |
|
37 | 37 | { |
|
38 | 38 | PlottablesMap result {}; |
|
39 | 39 | |
|
40 | 40 | // Creates {nbGraphs} QCPGraph to add to the plot |
|
41 | 41 | for (auto i = 0; i < nbGraphs; ++i) |
|
42 | 42 | { |
|
43 | 43 | auto graph = plot.addGraph(); |
|
44 | 44 | result.insert({ i, graph }); |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | plot.replot(); |
|
48 | 48 | |
|
49 | 49 | return result; |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | /** |
|
53 | 53 | * Specialization of PlottablesCreator for scalars |
|
54 | 54 | * @sa ScalarSeries |
|
55 | 55 | */ |
|
56 | 56 | template <typename T> |
|
57 | 57 | struct PlottablesCreator<T, typename std::enable_if_t<std::is_base_of<ScalarTimeSerie, T>::value>> |
|
58 | 58 | { |
|
59 | 59 | static PlottablesMap createPlottables(QCustomPlot& plot, const std::shared_ptr<T>& dataSeries) |
|
60 | 60 | { |
|
61 | 61 | return createGraphs(plot, 1); |
|
62 | 62 | } |
|
63 | 63 | }; |
|
64 | 64 | |
|
65 | 65 | /** |
|
66 | 66 | * Specialization of PlottablesCreator for vectors |
|
67 | 67 | * @sa VectorSeries |
|
68 | 68 | */ |
|
69 | 69 | template <typename T> |
|
70 | 70 | struct PlottablesCreator<T, typename std::enable_if_t<std::is_base_of<VectorTimeSerie, T>::value>> |
|
71 | 71 | { |
|
72 | 72 | static PlottablesMap createPlottables(QCustomPlot& plot, const std::shared_ptr<T>& dataSeries) |
|
73 | 73 | { |
|
74 | 74 | return createGraphs(plot, 3); |
|
75 | 75 | } |
|
76 | 76 | }; |
|
77 | 77 | |
|
78 | 78 | /** |
|
79 | 79 | * Specialization of PlottablesCreator for MultiComponentTimeSeries |
|
80 | 80 | * @sa VectorSeries |
|
81 | 81 | */ |
|
82 | 82 | template <typename T> |
|
83 | 83 | struct PlottablesCreator<T, |
|
84 | 84 | typename std::enable_if_t<std::is_base_of<MultiComponentTimeSerie, T>::value>> |
|
85 | 85 | { |
|
86 | 86 | static PlottablesMap createPlottables(QCustomPlot& plot, const std::shared_ptr<T>& dataSeries) |
|
87 | 87 | { |
|
88 | 88 | return createGraphs(plot, dataSeries->size(1)); |
|
89 | 89 | } |
|
90 | 90 | }; |
|
91 | 91 | |
|
92 | 92 | /** |
|
93 | 93 | * Specialization of PlottablesCreator for spectrograms |
|
94 | 94 | * @sa SpectrogramSeries |
|
95 | 95 | */ |
|
96 | 96 | template <typename T> |
|
97 | 97 | struct PlottablesCreator<T, |
|
98 | 98 | typename std::enable_if_t<std::is_base_of<SpectrogramTimeSerie, T>::value>> |
|
99 | 99 | { |
|
100 | 100 | static PlottablesMap createPlottables(QCustomPlot& plot, const std::shared_ptr<T>& dataSeries) |
|
101 | 101 | { |
|
102 | 102 | PlottablesMap result {}; |
|
103 | 103 | result.insert({ 0, new QCPColorMap { plot.xAxis, plot.yAxis } }); |
|
104 | 104 | |
|
105 | 105 | plot.replot(); |
|
106 | 106 | |
|
107 | 107 | return result; |
|
108 | 108 | } |
|
109 | 109 | }; |
|
110 | 110 | |
|
111 | 111 | /** |
|
112 | 112 | * Struct used to update plottables, depending on the type of the data series from which to update |
|
113 | 113 | * them |
|
114 | 114 | * @tparam T the data series' type |
|
115 | 115 | * @remarks Default implementation can't update plottables |
|
116 | 116 | */ |
|
117 | 117 | template <typename T, typename Enabled = void> |
|
118 | 118 | struct PlottablesUpdater |
|
119 | 119 | { |
|
120 | 120 | static void setPlotYAxisRange(T&, const DateTimeRange&, QCustomPlot&) |
|
121 | 121 | { |
|
122 | 122 | qCCritical(LOG_VisualizationGraphHelper()) |
|
123 | 123 | << QObject::tr("Can't set plot y-axis range: unmanaged data series type"); |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | static void updatePlottables(T&, PlottablesMap&, const DateTimeRange&, bool) |
|
127 | 127 | { |
|
128 | 128 | qCCritical(LOG_VisualizationGraphHelper()) |
|
129 | 129 | << QObject::tr("Can't update plottables: unmanaged data series type"); |
|
130 | 130 | } |
|
131 | 131 | }; |
|
132 | 132 | |
|
133 | 133 | /** |
|
134 | 134 | * Specialization of PlottablesUpdater for scalars and vectors |
|
135 | 135 | * @sa ScalarSeries |
|
136 | 136 | * @sa VectorSeries |
|
137 | 137 | */ |
|
138 | 138 | template <typename T> |
|
139 | 139 | struct PlottablesUpdater<T, typename std::enable_if_t<std::is_base_of<ScalarTimeSerie, T>::value>> |
|
140 | 140 | { |
|
141 | 141 | static void setPlotYAxisRange(T& dataSeries, const DateTimeRange& xAxisRange, QCustomPlot& plot) |
|
142 | 142 | { |
|
143 | 143 | auto minValue = 0., maxValue = 0.; |
|
144 | 144 | if (auto serie = dynamic_cast<ScalarTimeSerie*>(&dataSeries)) |
|
145 | 145 | { |
|
146 | 146 | if (serie->size()) |
|
147 | 147 | { |
|
148 | 148 | maxValue = (*std::max_element(std::begin(*serie), std::end(*serie))).v(); |
|
149 | 149 | minValue = (*std::min_element(std::begin(*serie), std::end(*serie))).v(); |
|
150 | 150 | } |
|
151 | 151 | } |
|
152 | 152 | plot.yAxis->setRange(QCPRange { minValue, maxValue }); |
|
153 | 153 | } |
|
154 | 154 | |
|
155 | 155 | static void updatePlottables( |
|
156 | 156 | T& dataSeries, PlottablesMap& plottables, const DateTimeRange& range, bool rescaleAxes) |
|
157 | 157 | { |
|
158 | 158 | |
|
159 | 159 | // For each plottable to update, resets its data |
|
160 | 160 | for (const auto& plottable : plottables) |
|
161 | 161 | { |
|
162 | 162 | if (auto graph = dynamic_cast<QCPGraph*>(plottable.second)) |
|
163 | 163 | { |
|
164 | 164 | auto dataContainer = QSharedPointer<SqpDataContainer>::create(); |
|
165 | 165 | if (auto serie = dynamic_cast<ScalarTimeSerie*>(&dataSeries)) |
|
166 | 166 | { |
|
167 | 167 | std::for_each( |
|
168 | 168 | std::begin(*serie), std::end(*serie), [&dataContainer](const auto& value) { |
|
169 | 169 | dataContainer->appendGraphData(QCPGraphData(value.t(), value.v())); |
|
170 | 170 | }); |
|
171 | 171 | } |
|
172 | 172 | graph->setData(dataContainer); |
|
173 | 173 | } |
|
174 | 174 | } |
|
175 | 175 | |
|
176 | 176 | if (!plottables.empty()) |
|
177 | 177 | { |
|
178 | 178 | auto plot = plottables.begin()->second->parentPlot(); |
|
179 | 179 | |
|
180 | 180 | if (rescaleAxes) |
|
181 | 181 | { |
|
182 | 182 | plot->rescaleAxes(); |
|
183 | 183 | } |
|
184 | 184 | } |
|
185 | 185 | } |
|
186 | 186 | }; |
|
187 | 187 | |
|
188 | 188 | |
|
189 | 189 | template <typename T> |
|
190 | 190 | struct PlottablesUpdater<T, typename std::enable_if_t<std::is_base_of<VectorTimeSerie, T>::value>> |
|
191 | 191 | { |
|
192 | 192 | static void setPlotYAxisRange(T& dataSeries, const DateTimeRange& xAxisRange, QCustomPlot& plot) |
|
193 | 193 | { |
|
194 | 194 | double minValue = 0., maxValue = 0.; |
|
195 | 195 | if (auto serie = dynamic_cast<VectorTimeSerie*>(&dataSeries)) |
|
196 | 196 | { |
|
197 | 197 | std::for_each( |
|
198 | 198 | std::begin(*serie), std::end(*serie), [&minValue, &maxValue](const auto& v) { |
|
199 | 199 | minValue = std::min({ minValue, v.v().x, v.v().y, v.v().z }); |
|
200 | 200 | maxValue = std::max({ maxValue, v.v().x, v.v().y, v.v().z }); |
|
201 | 201 | }); |
|
202 | 202 | } |
|
203 | 203 | |
|
204 | 204 | plot.yAxis->setRange(QCPRange { minValue, maxValue }); |
|
205 | 205 | } |
|
206 | 206 | |
|
207 | 207 | static void updatePlottables( |
|
208 | 208 | T& dataSeries, PlottablesMap& plottables, const DateTimeRange& range, bool rescaleAxes) |
|
209 | 209 | { |
|
210 | 210 | |
|
211 | 211 | // For each plottable to update, resets its data |
|
212 | 212 | for (const auto& plottable : plottables) |
|
213 | 213 | { |
|
214 | 214 | if (auto graph = dynamic_cast<QCPGraph*>(plottable.second)) |
|
215 | 215 | { |
|
216 | 216 | auto dataContainer = QSharedPointer<SqpDataContainer>::create(); |
|
217 | 217 | if (auto serie = dynamic_cast<VectorTimeSerie*>(&dataSeries)) |
|
218 | 218 | { |
|
219 | 219 | switch (plottable.first) |
|
220 | 220 | { |
|
221 | 221 | case 0: |
|
222 | 222 | std::for_each(std::begin(*serie), std::end(*serie), |
|
223 | 223 | [&dataContainer](const auto& value) { |
|
224 | 224 | dataContainer->appendGraphData( |
|
225 | 225 | QCPGraphData(value.t(), value.v().x)); |
|
226 | 226 | }); |
|
227 | 227 | break; |
|
228 | 228 | case 1: |
|
229 | 229 | std::for_each(std::begin(*serie), std::end(*serie), |
|
230 | 230 | [&dataContainer](const auto& value) { |
|
231 | 231 | dataContainer->appendGraphData( |
|
232 | 232 | QCPGraphData(value.t(), value.v().y)); |
|
233 | 233 | }); |
|
234 | 234 | break; |
|
235 | 235 | case 2: |
|
236 | 236 | std::for_each(std::begin(*serie), std::end(*serie), |
|
237 | 237 | [&dataContainer](const auto& value) { |
|
238 | 238 | dataContainer->appendGraphData( |
|
239 | 239 | QCPGraphData(value.t(), value.v().z)); |
|
240 | 240 | }); |
|
241 | 241 | break; |
|
242 | 242 | default: |
|
243 | 243 | break; |
|
244 | 244 | } |
|
245 | 245 | } |
|
246 | 246 | graph->setData(dataContainer); |
|
247 | 247 | } |
|
248 | 248 | } |
|
249 | 249 | |
|
250 | 250 | if (!plottables.empty()) |
|
251 | 251 | { |
|
252 | 252 | auto plot = plottables.begin()->second->parentPlot(); |
|
253 | 253 | |
|
254 | 254 | if (rescaleAxes) |
|
255 | 255 | { |
|
256 | 256 | plot->rescaleAxes(); |
|
257 | 257 | } |
|
258 | 258 | } |
|
259 | 259 | } |
|
260 | 260 | }; |
|
261 | 261 | |
|
262 | 262 | |
|
263 | 263 | template <typename T> |
|
264 | 264 | struct PlottablesUpdater<T, |
|
265 | 265 | typename std::enable_if_t<std::is_base_of<MultiComponentTimeSerie, T>::value>> |
|
266 | 266 | { |
|
267 | 267 | static void setPlotYAxisRange(T& dataSeries, const DateTimeRange& xAxisRange, QCustomPlot& plot) |
|
268 | 268 | { |
|
269 | 269 | double minValue = 0., maxValue = 0.; |
|
270 | 270 | if (auto serie = dynamic_cast<MultiComponentTimeSerie*>(&dataSeries)) |
|
271 | 271 | { |
|
272 | 272 | std::for_each( |
|
273 | 273 | std::begin(*serie), std::end(*serie), [&minValue, &maxValue](const auto& v) { |
|
274 |
minValue = std::min( |
|
|
275 |
maxValue = std::max( |
|
|
274 | minValue = std::min(minValue, std::min_element(v.begin(), v.end())->v()); | |
|
275 | maxValue = std::max(maxValue, std::max_element(v.begin(), v.end())->v()); | |
|
276 | 276 | }); |
|
277 | 277 | } |
|
278 | 278 | plot.yAxis->setRange(QCPRange { minValue, maxValue }); |
|
279 | 279 | } |
|
280 | 280 | |
|
281 | 281 | static void updatePlottables( |
|
282 | 282 | T& dataSeries, PlottablesMap& plottables, const DateTimeRange& range, bool rescaleAxes) |
|
283 | 283 | { |
|
284 | 284 | for (const auto& plottable : plottables) |
|
285 | 285 | { |
|
286 | 286 | if (auto graph = dynamic_cast<QCPGraph*>(plottable.second)) |
|
287 | 287 | { |
|
288 | 288 | auto dataContainer = QSharedPointer<SqpDataContainer>::create(); |
|
289 | 289 | if (auto serie = dynamic_cast<MultiComponentTimeSerie*>(&dataSeries)) |
|
290 | 290 | { |
|
291 | 291 | // TODO |
|
292 | 292 | std::for_each(std::begin(*serie), std::end(*serie), |
|
293 | 293 | [&dataContainer, component = plottable.first](const auto& value) { |
|
294 | 294 | dataContainer->appendGraphData( |
|
295 | 295 | QCPGraphData(value.t(), value[component])); |
|
296 | 296 | }); |
|
297 | 297 | } |
|
298 | 298 | graph->setData(dataContainer); |
|
299 | 299 | } |
|
300 | 300 | } |
|
301 | 301 | |
|
302 | 302 | if (!plottables.empty()) |
|
303 | 303 | { |
|
304 | 304 | auto plot = plottables.begin()->second->parentPlot(); |
|
305 | 305 | |
|
306 | 306 | if (rescaleAxes) |
|
307 | 307 | { |
|
308 | 308 | plot->rescaleAxes(); |
|
309 | 309 | } |
|
310 | 310 | } |
|
311 | 311 | } |
|
312 | 312 | }; |
|
313 | 313 | |
|
314 | 314 | /** |
|
315 | 315 | * Specialization of PlottablesUpdater for spectrograms |
|
316 | 316 | * @sa SpectrogramSeries |
|
317 | 317 | */ |
|
318 | 318 | template <typename T> |
|
319 | 319 | struct PlottablesUpdater<T, |
|
320 | 320 | typename std::enable_if_t<std::is_base_of<SpectrogramTimeSerie, T>::value>> |
|
321 | 321 | { |
|
322 | 322 | static void setPlotYAxisRange(T& dataSeries, const DateTimeRange& xAxisRange, QCustomPlot& plot) |
|
323 | 323 | { |
|
324 | 324 | // TODO |
|
325 | 325 | // double min, max; |
|
326 | 326 | // std::tie(min, max) = dataSeries.yBounds(); |
|
327 | 327 | |
|
328 | 328 | // if (!std::isnan(min) && !std::isnan(max)) |
|
329 | 329 | // { |
|
330 | 330 | // plot.yAxis->setRange(QCPRange { min, max }); |
|
331 | 331 | // } |
|
332 | double minValue = 0., maxValue = 0.; | |
|
333 | if (auto serie = dynamic_cast<SpectrogramTimeSerie*>(&dataSeries)) | |
|
334 | { | |
|
335 | auto& yAxis = serie->axis(1); | |
|
336 | if (yAxis.size()) | |
|
337 | { | |
|
338 | minValue = *std::min_element(std::cbegin(yAxis), std::cend(yAxis)); | |
|
339 | maxValue = *std::max_element(std::cbegin(yAxis), std::cend(yAxis)); | |
|
340 | } | |
|
341 | } | |
|
342 | plot.yAxis->setRange(QCPRange { minValue, maxValue }); | |
|
332 | 343 | } |
|
333 | 344 | |
|
334 | 345 | static void updatePlottables( |
|
335 | 346 | T& dataSeries, PlottablesMap& plottables, const DateTimeRange& range, bool rescaleAxes) |
|
336 | 347 | { |
|
337 | 348 | // TODO |
|
338 |
|
|
|
339 |
|
|
|
340 |
|
|
|
341 |
|
|
|
342 | // associated"); | |
|
343 | // return; | |
|
344 | // } | |
|
349 | if (plottables.empty()) | |
|
350 | { | |
|
351 | qCDebug(LOG_VisualizationGraphHelper()) | |
|
352 | << QObject::tr("Can't update spectrogram: no colormap has been associated"); | |
|
353 | return; | |
|
354 | } | |
|
345 | 355 | |
|
346 | // // Gets the colormap to update (normally there is only one colormap) | |
|
347 | // Q_ASSERT(plottables.size() == 1); | |
|
348 | // auto colormap = dynamic_cast<QCPColorMap*>(plottables.at(0)); | |
|
349 | // Q_ASSERT(colormap != nullptr); | |
|
350 | 356 | |
|
357 | // // Gets the colormap to update (normally there is only one colormap) | |
|
358 | Q_ASSERT(plottables.size() == 1); | |
|
359 | auto colormap = dynamic_cast<QCPColorMap*>(plottables.at(0)); | |
|
360 | Q_ASSERT(colormap != nullptr); | |
|
361 | if (auto serie = dynamic_cast<SpectrogramTimeSerie*>(&dataSeries)) | |
|
362 | { | |
|
363 | colormap->data()->setSize(serie->shape()[0], serie->shape()[1]); | |
|
364 | if (serie->size(0)) | |
|
365 | { | |
|
366 | colormap->data()->setRange( | |
|
367 | QCPRange { serie->begin()->t(), (serie->end() - 1)->t() }, | |
|
368 | QCPRange { 1., 1000. }); | |
|
369 | for (int x_index = 0; x_index < serie->shape()[0]; x_index++) | |
|
370 | { | |
|
371 | auto pixline = (*serie)[x_index]; | |
|
372 | for (int y_index = 0; y_index < serie->shape()[1]; y_index++) | |
|
373 | { | |
|
374 | auto value = pixline[y_index]; | |
|
375 | colormap->data()->setCell(x_index, y_index, value); | |
|
376 | if (std::isnan(value)) | |
|
377 | { | |
|
378 | colormap->data()->setAlpha(x_index, y_index, 0); | |
|
379 | } | |
|
380 | } | |
|
381 | } | |
|
382 | } | |
|
383 | } | |
|
351 | 384 | // dataSeries.lockRead(); |
|
352 | 385 | |
|
353 | 386 | // // Processing spectrogram data for display in QCustomPlot |
|
354 | 387 | // auto its = dataSeries.xAxisRange(range.m_TStart, range.m_TEnd); |
|
355 | 388 | |
|
356 | 389 | // // Computes logarithmic y-axis resolution for the spectrogram |
|
357 | 390 | // auto yData = its.first->y(); |
|
358 | 391 | // auto yResolution = DataSeriesUtils::resolution(yData.begin(), yData.end(), true); |
|
359 | 392 | |
|
360 | 393 | // // Generates mesh for colormap |
|
361 | 394 | // auto mesh = DataSeriesUtils::regularMesh(its.first, its.second, |
|
362 | 395 | // DataSeriesUtils::Resolution { dataSeries.xResolution() }, yResolution); |
|
363 | 396 | |
|
364 | 397 | // dataSeries.unlock(); |
|
365 | 398 | |
|
366 | 399 | // colormap->data()->setSize(mesh.m_NbX, mesh.m_NbY); |
|
367 | 400 | // if (!mesh.isEmpty()) |
|
368 | 401 | // { |
|
369 | 402 | // colormap->data()->setRange(QCPRange { mesh.m_XMin, mesh.xMax() }, |
|
370 | 403 | // // y-axis range is converted to linear values |
|
371 | 404 | // QCPRange { std::pow(10, mesh.m_YMin), std::pow(10, mesh.yMax()) }); |
|
372 | 405 | |
|
373 | 406 | // // Sets values |
|
374 | 407 | // auto index = 0; |
|
375 | 408 | // for (auto it = mesh.m_Data.begin(), end = mesh.m_Data.end(); it != end; ++it, |
|
376 | 409 | // ++index) |
|
377 | 410 | // { |
|
378 | 411 | // auto xIndex = index % mesh.m_NbX; |
|
379 | 412 | // auto yIndex = index / mesh.m_NbX; |
|
380 | 413 | |
|
381 | 414 | // colormap->data()->setCell(xIndex, yIndex, *it); |
|
382 | 415 | |
|
383 | 416 | // // Makes the NaN values to be transparent in the colormap |
|
384 | 417 | // if (std::isnan(*it)) |
|
385 | 418 | // { |
|
386 | 419 | // colormap->data()->setAlpha(xIndex, yIndex, 0); |
|
387 | 420 | // } |
|
388 | 421 | // } |
|
389 | 422 | // } |
|
390 | 423 | |
|
391 | 424 | // // Rescales axes |
|
392 |
|
|
|
393 | ||
|
394 |
|
|
|
395 |
|
|
|
396 |
|
|
|
397 |
|
|
|
425 | auto plot = colormap->parentPlot(); | |
|
426 | setPlotYAxisRange(dataSeries, {}, *plot); | |
|
427 | if (rescaleAxes) | |
|
428 | { | |
|
429 | plot->rescaleAxes(); | |
|
430 | } | |
|
398 | 431 | } |
|
399 | 432 | }; |
|
400 | 433 | |
|
401 | 434 | /** |
|
402 | 435 | * Helper used to create/update plottables |
|
403 | 436 | */ |
|
404 | 437 | struct IPlottablesHelper |
|
405 | 438 | { |
|
406 | 439 | virtual ~IPlottablesHelper() noexcept = default; |
|
407 | 440 | virtual PlottablesMap create(QCustomPlot& plot) const = 0; |
|
408 | 441 | virtual void setYAxisRange(const DateTimeRange& xAxisRange, QCustomPlot& plot) const = 0; |
|
409 | 442 | virtual void update( |
|
410 | 443 | PlottablesMap& plottables, const DateTimeRange& range, bool rescaleAxes = false) const = 0; |
|
411 | 444 | }; |
|
412 | 445 | |
|
413 | 446 | /** |
|
414 | 447 | * Default implementation of IPlottablesHelper, which takes data series to create/update plottables |
|
415 | 448 | * @tparam T the data series' type |
|
416 | 449 | */ |
|
417 | 450 | template <typename T> |
|
418 | 451 | struct PlottablesHelper : public IPlottablesHelper |
|
419 | 452 | { |
|
420 | 453 | explicit PlottablesHelper(std::shared_ptr<T> dataSeries) : m_DataSeries { dataSeries } {} |
|
421 | 454 | |
|
422 | 455 | PlottablesMap create(QCustomPlot& plot) const override |
|
423 | 456 | { |
|
424 | 457 | return PlottablesCreator<T>::createPlottables(plot, m_DataSeries); |
|
425 | 458 | } |
|
426 | 459 | |
|
427 | 460 | void update( |
|
428 | 461 | PlottablesMap& plottables, const DateTimeRange& range, bool rescaleAxes) const override |
|
429 | 462 | { |
|
430 | 463 | if (m_DataSeries) |
|
431 | 464 | { |
|
432 | 465 | PlottablesUpdater<T>::updatePlottables(*m_DataSeries, plottables, range, rescaleAxes); |
|
433 | 466 | } |
|
434 | 467 | else |
|
435 | 468 | { |
|
436 | 469 | qCCritical(LOG_VisualizationGraphHelper()) << "Can't update plottables: inconsistency " |
|
437 | 470 | "between the type of data series and the " |
|
438 | 471 | "type supposed"; |
|
439 | 472 | } |
|
440 | 473 | } |
|
441 | 474 | |
|
442 | 475 | void setYAxisRange(const DateTimeRange& xAxisRange, QCustomPlot& plot) const override |
|
443 | 476 | { |
|
444 | 477 | if (m_DataSeries) |
|
445 | 478 | { |
|
446 | 479 | PlottablesUpdater<T>::setPlotYAxisRange(*m_DataSeries, xAxisRange, plot); |
|
447 | 480 | } |
|
448 | 481 | else |
|
449 | 482 | { |
|
450 | 483 | qCCritical(LOG_VisualizationGraphHelper()) << "Can't update plottables: inconsistency " |
|
451 | 484 | "between the type of data series and the " |
|
452 | 485 | "type supposed"; |
|
453 | 486 | } |
|
454 | 487 | } |
|
455 | 488 | |
|
456 | 489 | std::shared_ptr<T> m_DataSeries; |
|
457 | 490 | }; |
|
458 | 491 | |
|
459 | 492 | /// Creates IPlottablesHelper according to the type of data series a variable holds |
|
460 | 493 | std::unique_ptr<IPlottablesHelper> createHelper(std::shared_ptr<Variable2> variable) noexcept |
|
461 | 494 | { |
|
462 | 495 | switch (variable->type()) |
|
463 | 496 | { |
|
464 | 497 | case DataSeriesType::SCALAR: |
|
465 | 498 | return std::make_unique<PlottablesHelper<ScalarTimeSerie>>( |
|
466 | 499 | std::dynamic_pointer_cast<ScalarTimeSerie>(variable->data())); |
|
467 | 500 | case DataSeriesType::SPECTROGRAM: |
|
468 | 501 | return std::make_unique<PlottablesHelper<SpectrogramTimeSerie>>( |
|
469 | 502 | std::dynamic_pointer_cast<SpectrogramTimeSerie>(variable->data())); |
|
470 | 503 | case DataSeriesType::VECTOR: |
|
471 | 504 | return std::make_unique<PlottablesHelper<VectorTimeSerie>>( |
|
472 | 505 | std::dynamic_pointer_cast<VectorTimeSerie>(variable->data())); |
|
473 | 506 | case DataSeriesType::MULTICOMPONENT: |
|
474 | 507 | return std::make_unique<PlottablesHelper<MultiComponentTimeSerie>>( |
|
475 | 508 | std::dynamic_pointer_cast<MultiComponentTimeSerie>(variable->data())); |
|
476 | 509 | default: |
|
477 | 510 | // Creates default helper |
|
478 | 511 | break; |
|
479 | 512 | } |
|
480 | 513 | |
|
481 | 514 | return std::make_unique<PlottablesHelper<TimeSeries::ITimeSerie>>(nullptr); |
|
482 | 515 | } |
|
483 | 516 | |
|
484 | 517 | } // namespace |
|
485 | 518 | |
|
486 | 519 | PlottablesMap VisualizationGraphHelper::create( |
|
487 | 520 | std::shared_ptr<Variable2> variable, QCustomPlot& plot) noexcept |
|
488 | 521 | { |
|
489 | 522 | if (variable) |
|
490 | 523 | { |
|
491 | 524 | auto helper = createHelper(variable); |
|
492 | 525 | auto plottables = helper->create(plot); |
|
493 | 526 | return plottables; |
|
494 | 527 | } |
|
495 | 528 | else |
|
496 | 529 | { |
|
497 | 530 | qCDebug(LOG_VisualizationGraphHelper()) |
|
498 | 531 | << QObject::tr("Can't create graph plottables : the variable is null"); |
|
499 | 532 | return PlottablesMap {}; |
|
500 | 533 | } |
|
501 | 534 | } |
|
502 | 535 | |
|
503 | 536 | void VisualizationGraphHelper::setYAxisRange( |
|
504 | 537 | std::shared_ptr<Variable2> variable, QCustomPlot& plot) noexcept |
|
505 | 538 | { |
|
506 | 539 | if (variable) |
|
507 | 540 | { |
|
508 | 541 | auto helper = createHelper(variable); |
|
509 | 542 | helper->setYAxisRange(variable->range(), plot); |
|
510 | 543 | } |
|
511 | 544 | else |
|
512 | 545 | { |
|
513 | 546 | qCDebug(LOG_VisualizationGraphHelper()) |
|
514 | 547 | << QObject::tr("Can't set y-axis range of plot: the variable is null"); |
|
515 | 548 | } |
|
516 | 549 | } |
|
517 | 550 | |
|
518 | 551 | void VisualizationGraphHelper::updateData( |
|
519 | 552 | PlottablesMap& plottables, std::shared_ptr<Variable2> variable, const DateTimeRange& dateTime) |
|
520 | 553 | { |
|
521 | 554 | auto helper = createHelper(variable); |
|
522 | 555 | helper->update(plottables, dateTime); |
|
523 | 556 | } |
@@ -1,65 +1,65 | |||
|
1 | 1 | import traceback |
|
2 | 2 | import os |
|
3 | 3 | from datetime import datetime, timedelta, timezone |
|
4 | 4 | import PythonProviders |
|
5 | 5 | import pysciqlopcore |
|
6 | 6 | import numpy as np |
|
7 | import pandas as pds | |
|
8 | 7 | import requests |
|
9 | 8 | import copy |
|
10 | 9 | from spwc.amda import AMDA |
|
11 | 10 | |
|
12 | 11 | amda = AMDA() |
|
13 | 12 | |
|
14 | 13 | def get_sample(metadata,start,stop): |
|
15 | 14 | ts_type = pysciqlopcore.ScalarTimeSerie |
|
16 | 15 | default_ctor_args = 1 |
|
17 | 16 | try: |
|
18 | 17 | param_id = None |
|
19 | 18 | for key,value in metadata: |
|
20 | 19 | if key == 'xml:id': |
|
21 | 20 | param_id = value |
|
22 | 21 | elif key == 'type': |
|
23 | 22 | if value == 'vector': |
|
24 | 23 | ts_type = pysciqlopcore.VectorTimeSerie |
|
25 | 24 | elif value == 'multicomponent': |
|
26 | 25 | ts_type = pysciqlopcore.MultiComponentTimeSerie |
|
27 | 26 | default_ctor_args = (0,2) |
|
28 | 27 | tstart=datetime.fromtimestamp(start, tz=timezone.utc) |
|
29 | 28 | tend=datetime.fromtimestamp(stop, tz=timezone.utc) |
|
30 |
|
|
|
31 | t = np.array([d.timestamp() for d in df.index]) | |
|
32 | values = df.values | |
|
33 | return ts_type(t,values) | |
|
29 | var = amda.get_parameter(start_time=tstart, stop_time=tend, parameter_id=param_id, method="REST") | |
|
30 | return ts_type(var.time,var.data) | |
|
34 | 31 | except Exception as e: |
|
35 | 32 | print(traceback.format_exc()) |
|
36 | 33 | print("Error in amda.py ",str(e)) |
|
37 | 34 | return ts_type(default_ctor_args) |
|
38 | 35 | |
|
39 | 36 | |
|
40 | 37 | if len(amda.component) is 0: |
|
41 | 38 | amda.update_inventory() |
|
42 | 39 | parameters = copy.deepcopy(amda.parameter) |
|
43 | 40 | for name,component in amda.component.items(): |
|
44 | 41 | if 'components' in parameters[component['parameter']]: |
|
45 | 42 | parameters[component['parameter']]['components'].append(component) |
|
46 | 43 | else: |
|
47 | 44 | parameters[component['parameter']]['components']=[component] |
|
48 | 45 | |
|
49 | 46 | products = [] |
|
50 | 47 | for key,parameter in parameters.items(): |
|
51 | 48 | path = f"/AMDA/{parameter['mission']}/{parameter.get('observatory','')}/{parameter['instrument']}/{parameter['dataset']}/{parameter['name']}" |
|
52 | 49 | components = [component['name'] for component in parameter.get('components',[])] |
|
53 | 50 | metadata = [ (key,item) for key,item in parameter.items() if key is not 'components' ] |
|
54 | 51 | n_components = parameter.get('size',0) |
|
55 | 52 | if n_components is '3': |
|
56 | 53 | metadata.append(("type","vector")) |
|
57 | 54 | elif n_components !=0: |
|
58 | metadata.append(("type","multicomponent")) | |
|
55 | if parameter.get('display_type','')=="spectrogram": | |
|
56 | metadata.append(("type","spectrogram")) | |
|
57 | else: | |
|
58 | metadata.append(("type","multicomponent")) | |
|
59 | 59 | else: |
|
60 | 60 | metadata.append(("type","scalar")) |
|
61 | 61 | products.append( (path, components, metadata)) |
|
62 | 62 | |
|
63 | 63 | PythonProviders.register_product(products, get_sample) |
|
64 | 64 | |
|
65 | 65 |
@@ -1,87 +1,96 | |||
|
1 | 1 | import traceback |
|
2 | 2 | import pandas as pds |
|
3 | 3 | import PythonProviders |
|
4 | 4 | import pysciqlopcore |
|
5 | 5 | import numpy as np |
|
6 | 6 | import math |
|
7 | 7 | from spwc.cache import _cache |
|
8 | 8 | from spwc.common.datetime_range import DateTimeRange |
|
9 | 9 | from functools import partial |
|
10 | 10 | from datetime import datetime, timedelta, timezone |
|
11 | ||
|
12 | someglobal = 1 | |
|
11 | from spwc.common.variable import SpwcVariable | |
|
13 | 12 | |
|
14 | 13 | def make_scalar(x): |
|
15 | 14 | y = np.cos(x/10.) |
|
16 | return pds.DataFrame(index=[datetime.fromtimestamp(t, tz=timezone.utc) for t in x], data=y) | |
|
15 | return SpwcVariable(time=x, data=y) | |
|
17 | 16 | |
|
18 | 17 | def make_vector(x): |
|
19 | 18 | v=np.ones((len(x),3)) |
|
20 | 19 | for i in range(3): |
|
21 | 20 | v.transpose()[:][i] = np.cos(x/10. + float(i)) + (100. * np.cos(x/10000. + float(i))) |
|
22 | return pds.DataFrame(index=[datetime.fromtimestamp(t, tz=timezone.utc) for t in x], data=v) | |
|
21 | return SpwcVariable(time=x, data=v) | |
|
23 | 22 | |
|
24 | 23 | |
|
25 | 24 | def make_multicomponent(x): |
|
26 | 25 | v=np.ones((len(x),4)) |
|
27 | 26 | for i in range(4): |
|
28 | 27 | v.transpose()[:][i] = float(i+1) * np.cos(x/10. + float(i)) |
|
29 | return pds.DataFrame(index=[datetime.fromtimestamp(t, tz=timezone.utc) for t in x], data=v) | |
|
28 | return SpwcVariable(time=x, data=v) | |
|
29 | ||
|
30 | def make_spectrogram(x): | |
|
31 | v=np.ones((len(x),32)) | |
|
32 | for i in range(32): | |
|
33 | v.transpose()[:][i] = 100.*(2.+ float(i+1) * np.cos(x/1024. + float(i))) | |
|
34 | return SpwcVariable(time=x, data=v) | |
|
30 | 35 | |
|
31 | 36 | |
|
32 | 37 | def _get_data(p_type, start, stop): |
|
33 | 38 | if type(start) is datetime: |
|
34 | 39 | start = start.timestamp() |
|
35 | 40 | stop = stop.timestamp() |
|
36 | 41 | x = np.arange(math.ceil(start), math.floor(stop)) |
|
37 | 42 | if p_type == 'scalar': |
|
38 | 43 | return make_scalar(x) |
|
39 | 44 | if p_type == 'vector': |
|
40 | 45 | return make_vector(x) |
|
41 | 46 | if p_type == 'multicomponent': |
|
42 | 47 | return make_multicomponent(x) |
|
48 | if p_type == 'spectrogram': | |
|
49 | return make_spectrogram(np.arange(math.ceil(start), math.floor(stop),15.)) | |
|
43 | 50 | return None |
|
44 | 51 | |
|
45 | 52 | def get_data(metadata,start,stop): |
|
46 | 53 | ts_type = pysciqlopcore.ScalarTimeSerie |
|
47 | 54 | default_ctor_args = 1 |
|
48 | 55 | use_cache = False |
|
49 | 56 | p_type = 'scalar' |
|
50 | 57 | try: |
|
51 | 58 | for key,value in metadata: |
|
52 | 59 | if key == 'type': |
|
53 | 60 | p_type = value |
|
54 | 61 | if value == 'vector': |
|
55 | 62 | ts_type = pysciqlopcore.VectorTimeSerie |
|
56 | 63 | elif value == 'multicomponent': |
|
57 | 64 | ts_type = pysciqlopcore.MultiComponentTimeSerie |
|
58 | 65 | default_ctor_args = (0,2) |
|
66 | elif value == 'spectrogram': | |
|
67 | ts_type = lambda t,values: pysciqlopcore.SpectrogramTimeSerie(t,np.logspace(1,3,32),values) | |
|
68 | default_ctor_args = (0,2) | |
|
59 | 69 | if key == 'cache' and value == 'true': |
|
60 | 70 | use_cache = True |
|
61 | 71 | if use_cache: |
|
62 | 72 | cache_product = f"tests/{p_type}" |
|
63 |
|
|
|
73 | var = _cache.get_data(cache_product, DateTimeRange(datetime.fromtimestamp(start, tz=timezone.utc), datetime.fromtimestamp(stop, tz=timezone.utc)), | |
|
64 | 74 | partial(_get_data, p_type), |
|
65 | 75 | fragment_hours=24) |
|
66 | 76 | else: |
|
67 | 77 | print("No Cache") |
|
68 |
|
|
|
69 | t = np.array([d.timestamp() for d in df.index]) | |
|
70 | values = df.values | |
|
71 | return ts_type(t,values) | |
|
78 | var = _get_data(p_type, start, stop) | |
|
79 | return ts_type(var.time,var.data) | |
|
72 | 80 | except Exception as e: |
|
73 | 81 | print(traceback.format_exc()) |
|
74 | 82 | print("Error in test.py ",str(e)) |
|
75 | 83 | return ts_type(default_ctor_args) |
|
76 | 84 | |
|
77 | 85 | products = [ |
|
78 | 86 | ("/tests/without_cache/scalar",[],[("type","scalar")]), |
|
79 | 87 | ("/tests/without_cache/vector",[],[("type","vector")]), |
|
80 | 88 | ("/tests/without_cache/multicomponent",[],[("type","multicomponent"),('size','4')]), |
|
89 | ("/tests/without_cache/spectrogram",[],[("type","spectrogram"),('size','32')]), | |
|
81 | 90 | ("/tests/with_cache/scalar",[],[("type","scalar"), ("cache","true")]), |
|
82 | 91 | ("/tests/with_cache/vector",[],[("type","vector"), ("cache","true")]), |
|
83 | 92 | ("/tests/with_cache/multicomponent",[],[("type","multicomponent"),('size','4'), ("cache","true")]) |
|
84 | 93 | ] |
|
85 | 94 | |
|
86 | 95 | |
|
87 | 96 | PythonProviders.register_product(products ,get_data) |
General Comments 0
You need to be logged in to leave comments.
Login now