File indexing completed on 2024-04-28 03:48:04

0001 /*
0002     File                 : SmoothTest.cpp
0003     Project              : LabPlot
0004     Description          : Tests for data smoothing
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2020 Stefan Gerlach <stefan.gerlach@uni.kn>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "SmoothTest.h"
0012 #include "backend/core/column/Column.h"
0013 #include "backend/worksheet/plots/cartesian/XYSmoothCurve.h"
0014 
0015 extern "C" {
0016 #include "backend/nsl/nsl_smooth.h"
0017 }
0018 
0019 // ##############################################################################
0020 
0021 void SmoothTest::testPercentile() {
0022     // data
0023     QVector<int> xData{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
0024     QVector<double> yData{47.7, 44., 43., 44.96, 45., 43.73, 38., 47.1, 44., 38.3};
0025     // p=0.5, d=5   (check with LibreOffice and Origin 2020a)
0026     QVector<double> result50_5{47.7, 44, 44.96, 44, 43.73, 44.96, 44, 43.73, 44, 38.3};
0027 
0028     // data source columns
0029     Column xDataColumn(QStringLiteral("x"), AbstractColumn::ColumnMode::Integer);
0030     xDataColumn.replaceInteger(0, xData);
0031 
0032     Column yDataColumn(QStringLiteral("y"), AbstractColumn::ColumnMode::Double);
0033     yDataColumn.replaceValues(0, yData);
0034 
0035     XYSmoothCurve smoothCurve(QStringLiteral("smooth"));
0036     smoothCurve.setXDataColumn(&xDataColumn);
0037     smoothCurve.setYDataColumn(&yDataColumn);
0038 
0039     // prepare the smooth
0040     XYSmoothCurve::SmoothData smoothData = smoothCurve.smoothData();
0041     smoothData.type = nsl_smooth_type_percentile;
0042     // default
0043     // smoothData.points = 5;
0044     // smoothData.percentile = 0.5;
0045     smoothCurve.setSmoothData(smoothData);
0046 
0047     // perform the smooth
0048     smoothCurve.recalculate();
0049     const auto& smoothResult = smoothCurve.result();
0050 
0051     // check the results
0052     QCOMPARE(smoothResult.available, true);
0053     QCOMPARE(smoothResult.valid, true);
0054 
0055     const auto* resultXDataColumn{smoothCurve.xColumn()};
0056     const auto* resultYDataColumn{smoothCurve.yColumn()};
0057 
0058     const int np{resultXDataColumn->rowCount()};
0059     QCOMPARE(np, 10);
0060 
0061     for (int i = 0; i < np; i++) {
0062         QCOMPARE(resultXDataColumn->valueAt(i), (double)i + 1);
0063         QCOMPARE(resultYDataColumn->valueAt(i), result50_5.at(i));
0064     }
0065 }
0066 
0067 QTEST_MAIN(SmoothTest)