File indexing completed on 2024-04-28 15:14:07
0001 /*************************************************************************** 0002 File : SmoothTest.cpp 0003 Project : LabPlot 0004 Description : Tests for data smoothing 0005 -------------------------------------------------------------------- 0006 Copyright : (C) 2020 Stefan Gerlach (stefan.gerlach@uni.kn) 0007 ***************************************************************************/ 0008 0009 /*************************************************************************** 0010 * * 0011 * This program is free software; you can redistribute it and/or modify * 0012 * it under the terms of the GNU General Public License as published by * 0013 * the Free Software Foundation; either version 2 of the License, or * 0014 * (at your option) any later version. * 0015 * * 0016 * This program is distributed in the hope that it will be useful, * 0017 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0019 * GNU General Public License for more details. * 0020 * * 0021 * You should have received a copy of the GNU General Public License * 0022 * along with this program; if not, write to the Free Software * 0023 * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 0024 * Boston, MA 02110-1301 USA * 0025 * * 0026 ***************************************************************************/ 0027 0028 #include "SmoothTest.h" 0029 #include "backend/core/column/Column.h" 0030 #include "backend/worksheet/plots/cartesian/XYSmoothCurve.h" 0031 0032 extern "C" { 0033 #include "backend/nsl/nsl_smooth.h" 0034 } 0035 0036 //############################################################################## 0037 0038 void SmoothTest::testPercentile() { 0039 // data 0040 QVector<int> xData{1,2,3,4,5,6,7,8,9,10}; 0041 QVector<double> yData{47.7,44.,43.,44.96,45.,43.73,38.,47.1,44.,38.3}; 0042 // p=0.5, d=5 (check with LibreOffice and Origin 2020a) 0043 QVector<double> result50_5{47.7,44,44.96,44,43.73,44.96,44,43.73,44,38.3}; 0044 0045 //data source columns 0046 Column xDataColumn("x", AbstractColumn::ColumnMode::Integer); 0047 xDataColumn.replaceInteger(0, xData); 0048 0049 Column yDataColumn("y", AbstractColumn::ColumnMode::Numeric); 0050 yDataColumn.replaceValues(0, yData); 0051 0052 XYSmoothCurve smoothCurve("smooth"); 0053 smoothCurve.setXDataColumn(&xDataColumn); 0054 smoothCurve.setYDataColumn(&yDataColumn); 0055 0056 //prepare the smooth 0057 XYSmoothCurve::SmoothData smoothData = smoothCurve.smoothData(); 0058 smoothData.type = nsl_smooth_type_percentile; 0059 // default 0060 //smoothData.points = 5; 0061 //smoothData.percentile = 0.5; 0062 smoothCurve.setSmoothData(smoothData); 0063 0064 //perform the smooth 0065 smoothCurve.recalculate(); 0066 const XYSmoothCurve::SmoothResult& smoothResult = smoothCurve.smoothResult(); 0067 0068 //check the results 0069 QCOMPARE(smoothResult.available, true); 0070 QCOMPARE(smoothResult.valid, true); 0071 0072 const auto* resultXDataColumn{smoothCurve.xColumn()}; 0073 const auto* resultYDataColumn{smoothCurve.yColumn()}; 0074 0075 const int np{resultXDataColumn->rowCount()}; 0076 QCOMPARE(np, 10); 0077 0078 for (int i = 0; i < np; i++) { 0079 QCOMPARE(resultXDataColumn->valueAt(i), (double)i + 1); 0080 QCOMPARE(resultYDataColumn->valueAt(i), result50_5.at(i)); 0081 } 0082 } 0083 0084 QTEST_MAIN(SmoothTest)