##// END OF EJS Templates
Fixes subData() method and adds unit tests
Alexandre Leroux -
r550:70ca5d366d01
parent child
Show More
@@ -1,293 +1,293
1 1 #ifndef SCIQLOP_DATASERIES_H
2 2 #define SCIQLOP_DATASERIES_H
3 3
4 4 #include "CoreGlobal.h"
5 5
6 6 #include <Common/SortUtils.h>
7 7
8 8 #include <Data/ArrayData.h>
9 9 #include <Data/IDataSeries.h>
10 10
11 11 #include <QLoggingCategory>
12 12 #include <QReadLocker>
13 13 #include <QReadWriteLock>
14 14 #include <memory>
15 15
16 16 // We don't use the Qt macro since the log is used in the header file, which causes multiple log
17 17 // definitions with inheritance. Inline method is used instead
18 18 inline const QLoggingCategory &LOG_DataSeries()
19 19 {
20 20 static const QLoggingCategory category{"DataSeries"};
21 21 return category;
22 22 }
23 23
24 24 /**
25 25 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
26 26 *
27 27 * It proposes to set a dimension for the values ​​data.
28 28 *
29 29 * A DataSeries is always sorted on its x-axis data.
30 30 *
31 31 * @tparam Dim The dimension of the values data
32 32 *
33 33 */
34 34 template <int Dim>
35 35 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
36 36 public:
37 37 class IteratorValue {
38 38 public:
39 39 explicit IteratorValue(const DataSeries &dataSeries, bool begin)
40 40 : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()),
41 41 m_ValuesIt(begin ? dataSeries.valuesData()->cbegin()
42 42 : dataSeries.valuesData()->cend())
43 43 {
44 44 }
45 45
46 46 double x() const { return m_XIt->at(0); }
47 47 double value() const { return m_ValuesIt->at(0); }
48 48 double value(int componentIndex) const { return m_ValuesIt->at(componentIndex); }
49 49
50 50 void next()
51 51 {
52 52 ++m_XIt;
53 53 ++m_ValuesIt;
54 54 }
55 55
56 56 bool operator==(const IteratorValue &other) const
57 57 {
58 58 return std::tie(m_XIt, m_ValuesIt) == std::tie(other.m_XIt, other.m_ValuesIt);
59 59 }
60 60
61 61 private:
62 62 ArrayData<1>::Iterator m_XIt;
63 63 typename ArrayData<Dim>::Iterator m_ValuesIt;
64 64 };
65 65
66 66 class Iterator {
67 67 public:
68 68 using iterator_category = std::forward_iterator_tag;
69 69 using value_type = const IteratorValue;
70 70 using difference_type = std::ptrdiff_t;
71 71 using pointer = value_type *;
72 72 using reference = value_type &;
73 73
74 74 Iterator(const DataSeries &dataSeries, bool begin) : m_CurrentValue{dataSeries, begin} {}
75 75 virtual ~Iterator() noexcept = default;
76 76 Iterator(const Iterator &) = default;
77 77 Iterator(Iterator &&) = default;
78 78 Iterator &operator=(const Iterator &) = default;
79 79 Iterator &operator=(Iterator &&) = default;
80 80
81 81 Iterator &operator++()
82 82 {
83 83 m_CurrentValue.next();
84 84 return *this;
85 85 }
86 86
87 87 pointer operator->() const { return &m_CurrentValue; }
88 88
89 89 reference operator*() const { return m_CurrentValue; }
90 90
91 91 bool operator==(const Iterator &other) const
92 92 {
93 93 return m_CurrentValue == other.m_CurrentValue;
94 94 }
95 95
96 96 bool operator!=(const Iterator &other) const { return !(*this == other); }
97 97
98 98 private:
99 99 IteratorValue m_CurrentValue;
100 100 };
101 101
102 102 /// @sa IDataSeries::xAxisData()
103 103 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
104 104 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
105 105
106 106 /// @sa IDataSeries::xAxisUnit()
107 107 Unit xAxisUnit() const override { return m_XAxisUnit; }
108 108
109 109 /// @return the values dataset
110 110 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
111 111 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
112 112
113 113 /// @sa IDataSeries::valuesUnit()
114 114 Unit valuesUnit() const override { return m_ValuesUnit; }
115 115
116 116
117 117 SqpRange range() const override
118 118 {
119 119 if (!m_XAxisData->cdata().isEmpty()) {
120 120 return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()};
121 121 }
122 122
123 123 return SqpRange{};
124 124 }
125 125
126 126 void clear()
127 127 {
128 128 m_XAxisData->clear();
129 129 m_ValuesData->clear();
130 130 }
131 131
132 132 /// Merges into the data series an other data series
133 133 /// @remarks the data series to merge with is cleared after the operation
134 134 void merge(IDataSeries *dataSeries) override
135 135 {
136 136 dataSeries->lockWrite();
137 137 lockWrite();
138 138
139 139 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
140 140 const auto &otherXAxisData = other->xAxisData()->cdata();
141 141 const auto &xAxisData = m_XAxisData->cdata();
142 142
143 143 // As data series are sorted, we can improve performances of merge, by call the sort
144 144 // method only if the two data series overlap.
145 145 if (!otherXAxisData.empty()) {
146 146 auto firstValue = otherXAxisData.front();
147 147 auto lastValue = otherXAxisData.back();
148 148
149 149 auto xAxisDataBegin = xAxisData.cbegin();
150 150 auto xAxisDataEnd = xAxisData.cend();
151 151
152 152 bool prepend;
153 153 bool sortNeeded;
154 154
155 155 if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) {
156 156 // Other data series if after data series
157 157 prepend = false;
158 158 sortNeeded = false;
159 159 }
160 160 else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue)
161 161 == xAxisDataBegin) {
162 162 // Other data series if before data series
163 163 prepend = true;
164 164 sortNeeded = false;
165 165 }
166 166 else {
167 167 // The two data series overlap
168 168 prepend = false;
169 169 sortNeeded = true;
170 170 }
171 171
172 172 // Makes the merge
173 173 m_XAxisData->add(*other->xAxisData(), prepend);
174 174 m_ValuesData->add(*other->valuesData(), prepend);
175 175
176 176 if (sortNeeded) {
177 177 sort();
178 178 }
179 179 }
180 180
181 181 // Clears the other data series
182 182 other->clear();
183 183 }
184 184 else {
185 185 qCWarning(LOG_DataSeries())
186 186 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
187 187 }
188 188 unlock();
189 189 dataSeries->unlock();
190 190 }
191 191
192 192 // ///////// //
193 193 // Iterators //
194 194 // ///////// //
195 195
196 196 Iterator cbegin() const { return Iterator{*this, true}; }
197 197
198 198 Iterator cend() const { return Iterator{*this, false}; }
199 199
200 200 std::pair<Iterator, Iterator> subData(double min, double max) const
201 201 {
202 202 if (min > max) {
203 203 std::swap(min, max);
204 204 }
205 205
206 206 auto begin = cbegin();
207 207 auto end = cend();
208 208
209 209 auto lowerIt
210 210 = std::lower_bound(begin, end, min, [](const auto &itValue, const auto &value) {
211 return itValue.x() == value;
211 return itValue.x() < value;
212 212 });
213 213 auto upperIt
214 214 = std::upper_bound(begin, end, max, [](const auto &value, const auto &itValue) {
215 return itValue.x() == value;
215 return value < itValue.x();
216 216 });
217 217
218 218 return std::make_pair(lowerIt, upperIt);
219 219 }
220 220
221 221 // /////// //
222 222 // Mutexes //
223 223 // /////// //
224 224
225 225 virtual void lockRead() { m_Lock.lockForRead(); }
226 226 virtual void lockWrite() { m_Lock.lockForWrite(); }
227 227 virtual void unlock() { m_Lock.unlock(); }
228 228
229 229 protected:
230 230 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
231 231 /// DataSeries with no values will be created.
232 232 /// @remarks data series is automatically sorted on its x-axis data
233 233 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
234 234 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
235 235 : m_XAxisData{xAxisData},
236 236 m_XAxisUnit{xAxisUnit},
237 237 m_ValuesData{valuesData},
238 238 m_ValuesUnit{valuesUnit}
239 239 {
240 240 if (m_XAxisData->size() != m_ValuesData->size()) {
241 241 clear();
242 242 }
243 243
244 244 // Sorts data if it's not the case
245 245 const auto &xAxisCData = m_XAxisData->cdata();
246 246 if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) {
247 247 sort();
248 248 }
249 249 }
250 250
251 251 /// Copy ctor
252 252 explicit DataSeries(const DataSeries<Dim> &other)
253 253 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
254 254 m_XAxisUnit{other.m_XAxisUnit},
255 255 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
256 256 m_ValuesUnit{other.m_ValuesUnit}
257 257 {
258 258 // Since a series is ordered from its construction and is always ordered, it is not
259 259 // necessary to call the sort method here ('other' is sorted)
260 260 }
261 261
262 262 /// Assignment operator
263 263 template <int D>
264 264 DataSeries &operator=(DataSeries<D> other)
265 265 {
266 266 std::swap(m_XAxisData, other.m_XAxisData);
267 267 std::swap(m_XAxisUnit, other.m_XAxisUnit);
268 268 std::swap(m_ValuesData, other.m_ValuesData);
269 269 std::swap(m_ValuesUnit, other.m_ValuesUnit);
270 270
271 271 return *this;
272 272 }
273 273
274 274 private:
275 275 /**
276 276 * Sorts data series on its x-axis data
277 277 */
278 278 void sort() noexcept
279 279 {
280 280 auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>());
281 281 m_XAxisData = m_XAxisData->sort(permutation);
282 282 m_ValuesData = m_ValuesData->sort(permutation);
283 283 }
284 284
285 285 std::shared_ptr<ArrayData<1> > m_XAxisData;
286 286 Unit m_XAxisUnit;
287 287 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
288 288 Unit m_ValuesUnit;
289 289
290 290 QReadWriteLock m_Lock;
291 291 };
292 292
293 293 #endif // SCIQLOP_DATASERIES_H
@@ -1,164 +1,232
1 1 #include "Data/DataSeries.h"
2 2 #include "Data/ScalarSeries.h"
3 3
4 4 #include <QObject>
5 5 #include <QtTest>
6 6
7 7 Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>)
8 8
9 9 class TestDataSeries : public QObject {
10 10 Q_OBJECT
11 11 private slots:
12 12 /// Input test data
13 13 /// @sa testCtor()
14 14 void testCtor_data();
15 15
16 16 /// Tests construction of a data series
17 17 void testCtor();
18 18
19 19 /// Input test data
20 20 /// @sa testMerge()
21 21 void testMerge_data();
22 22
23 23 /// Tests merge of two data series
24 24 void testMerge();
25
26 /// Input test data
27 /// @sa testSubdata()
28 void testSubdata_data();
29
30 /// Tests get subdata of two data series
31 void testSubdata();
25 32 };
26 33
27 34 void TestDataSeries::testCtor_data()
28 35 {
29 36 // ////////////// //
30 37 // Test structure //
31 38 // ////////////// //
32 39
33 40 // x-axis data
34 41 QTest::addColumn<QVector<double> >("xAxisData");
35 42 // values data
36 43 QTest::addColumn<QVector<double> >("valuesData");
37 44
38 45 // expected x-axis data
39 46 QTest::addColumn<QVector<double> >("expectedXAxisData");
40 47 // expected values data
41 48 QTest::addColumn<QVector<double> >("expectedValuesData");
42 49
43 50 // ////////// //
44 51 // Test cases //
45 52 // ////////// //
46 53
47 54 QTest::newRow("invalidData (different sizes of vectors)")
48 55 << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 200., 300.}
49 56 << QVector<double>{} << QVector<double>{};
50 57
51 58 QTest::newRow("sortedData") << QVector<double>{1., 2., 3., 4., 5.}
52 59 << QVector<double>{100., 200., 300., 400., 500.}
53 60 << QVector<double>{1., 2., 3., 4., 5.}
54 61 << QVector<double>{100., 200., 300., 400., 500.};
55 62
56 63 QTest::newRow("unsortedData") << QVector<double>{5., 4., 3., 2., 1.}
57 64 << QVector<double>{100., 200., 300., 400., 500.}
58 65 << QVector<double>{1., 2., 3., 4., 5.}
59 66 << QVector<double>{500., 400., 300., 200., 100.};
60 67
61 68 QTest::newRow("unsortedData2")
62 69 << QVector<double>{1., 4., 3., 5., 2.} << QVector<double>{100., 200., 300., 400., 500.}
63 70 << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 500., 300., 200., 400.};
64 71 }
65 72
66 73 void TestDataSeries::testCtor()
67 74 {
68 75 // Creates series
69 76 QFETCH(QVector<double>, xAxisData);
70 77 QFETCH(QVector<double>, valuesData);
71 78
72 79 auto series = std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData),
73 80 Unit{}, Unit{});
74 81
75 82 // Validates results : we check that the data series is sorted on its x-axis data
76 83 QFETCH(QVector<double>, expectedXAxisData);
77 84 QFETCH(QVector<double>, expectedValuesData);
78 85
79 86 auto seriesXAxisData = series->xAxisData()->data();
80 87 auto seriesValuesData = series->valuesData()->data();
81 88
82 89 QVERIFY(
83 90 std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin()));
84 91 QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(),
85 92 seriesValuesData.cbegin()));
86 93 }
87 94
88 95 namespace {
89 96
90 97 std::shared_ptr<ScalarSeries> createSeries(QVector<double> xAxisData, QVector<double> valuesData)
91 98 {
92 99 return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{},
93 100 Unit{});
94 101 }
95 102
96 103 } // namespace
97 104
98 105 void TestDataSeries::testMerge_data()
99 106 {
100 107 // ////////////// //
101 108 // Test structure //
102 109 // ////////////// //
103 110
104 111 // Data series to merge
105 112 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
106 113 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries2");
107 114
108 115 // Expected values in the first data series after merge
109 116 QTest::addColumn<QVector<double> >("expectedXAxisData");
110 117 QTest::addColumn<QVector<double> >("expectedValuesData");
111 118
112 119 // ////////// //
113 120 // Test cases //
114 121 // ////////// //
115 122
116 123 QTest::newRow("sortedMerge")
117 124 << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
118 125 << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.})
119 126 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
120 127 << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
121 128
122 129 QTest::newRow("unsortedMerge")
123 130 << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.})
124 131 << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
125 132 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
126 133 << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
127 134
128 135 QTest::newRow("unsortedMerge2")
129 136 << createSeries({1., 2., 8., 9., 10}, {100., 200., 300., 400., 500.})
130 137 << createSeries({3., 4., 5., 6., 7.}, {600., 700., 800., 900., 1000.})
131 138 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
132 139 << QVector<double>{100., 200., 600., 700., 800., 900., 1000., 300., 400., 500.};
133 140
134 141 QTest::newRow("unsortedMerge3")
135 142 << createSeries({3., 5., 8., 7., 2}, {100., 200., 300., 400., 500.})
136 143 << createSeries({6., 4., 9., 10., 1.}, {600., 700., 800., 900., 1000.})
137 144 << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.}
138 145 << QVector<double>{1000., 500., 100., 700., 200., 600., 400., 300., 800., 900.};
139 146 }
140 147
141 148 void TestDataSeries::testMerge()
142 149 {
143 150 // Merges series
144 151 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
145 152 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries2);
146 153
147 154 dataSeries->merge(dataSeries2.get());
148 155
149 156 // Validates results : we check that the merge is valid and the data series is sorted on its
150 157 // x-axis data
151 158 QFETCH(QVector<double>, expectedXAxisData);
152 159 QFETCH(QVector<double>, expectedValuesData);
153 160
154 161 auto seriesXAxisData = dataSeries->xAxisData()->data();
155 162 auto seriesValuesData = dataSeries->valuesData()->data();
156 163
157 164 QVERIFY(
158 165 std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin()));
159 166 QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(),
160 167 seriesValuesData.cbegin()));
161 168 }
162 169
170 void TestDataSeries::testSubdata_data()
171 {
172 // ////////////// //
173 // Test structure //
174 // ////////////// //
175
176 // Data series to get subdata
177 QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries");
178
179 // Min/max values
180 QTest::addColumn<double>("min");
181 QTest::addColumn<double>("max");
182
183 // Expected values after subdata
184 QTest::addColumn<QVector<double> >("expectedXAxisData");
185 QTest::addColumn<QVector<double> >("expectedValuesData");
186
187 // ////////// //
188 // Test cases //
189 // ////////// //
190
191 QTest::newRow("subData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
192 << -1. << 3.2 << QVector<double>{1., 2., 3.}
193 << QVector<double>{100., 200., 300.};
194 QTest::newRow("subData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
195 << 1. << 4. << QVector<double>{1., 2., 3., 4.}
196 << QVector<double>{100., 200., 300., 400.};
197 QTest::newRow("subData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
198 << 1. << 3.9 << QVector<double>{1., 2., 3.}
199 << QVector<double>{100., 200., 300.};
200 QTest::newRow("subData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
201 << 0. << 0.9 << QVector<double>{} << QVector<double>{};
202 QTest::newRow("subData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
203 << 0. << 1. << QVector<double>{1.} << QVector<double>{100.};
204 QTest::newRow("subData6") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
205 << 2.1 << 6. << QVector<double>{3., 4., 5.}
206 << QVector<double>{300., 400., 500.};
207 QTest::newRow("subData7") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
208 << 6. << 9. << QVector<double>{} << QVector<double>{};
209 QTest::newRow("subData8") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.})
210 << 5. << 9. << QVector<double>{5.} << QVector<double>{500.};
211 }
212
213 void TestDataSeries::testSubdata()
214 {
215 QFETCH(std::shared_ptr<ScalarSeries>, dataSeries);
216 QFETCH(double, min);
217 QFETCH(double, max);
218
219 QFETCH(QVector<double>, expectedXAxisData);
220 QFETCH(QVector<double>, expectedValuesData);
221
222 auto bounds = dataSeries->subData(min, max);
223 QVERIFY(std::equal(bounds.first, bounds.second, expectedXAxisData.cbegin(),
224 expectedXAxisData.cend(),
225 [](const auto &it, const auto &expectedX) { return it.x() == expectedX; }));
226 QVERIFY(std::equal(
227 bounds.first, bounds.second, expectedValuesData.cbegin(), expectedValuesData.cend(),
228 [](const auto &it, const auto &expectedVal) { return it.value() == expectedVal; }));
229 }
230
163 231 QTEST_MAIN(TestDataSeries)
164 232 #include "TestDataSeries.moc"
General Comments 0
You need to be logged in to leave comments. Login now