From 3b5523f881ebdace824b60156e49015013dfc210 2017-08-03 08:29:18 From: Alexandre Leroux Date: 2017-08-03 08:29:18 Subject: [PATCH] Merge branch 'feature/HandleNaNAmda' into develop --- diff --git a/plugins/amda/src/AmdaResultParser.cpp b/plugins/amda/src/AmdaResultParser.cpp index abe18c1..ba5b907 100644 --- a/plugins/amda/src/AmdaResultParser.cpp +++ b/plugins/amda/src/AmdaResultParser.cpp @@ -93,15 +93,23 @@ QPair, QVector > readResults(QTextStream &stream) bool valueOk; auto value = lineData.at(1).toDouble(&valueOk); - // Adds result only if x and value are valid - if (!std::isnan(x) && !std::isnan(value) && valueOk) { + // Adds result only if x is valid. Then, if value is invalid, it is set to NaN + if (!std::isnan(x)) { xData.push_back(x); + + if (!valueOk) { + qCWarning(LOG_AmdaResultParser()) + << QObject::tr( + "Value from line %1 is invalid and will be converted to NaN") + .arg(line); + value = std::numeric_limits::quiet_NaN(); + } + valuesData.push_back(value); } else { qCWarning(LOG_AmdaResultParser()) - << QObject::tr( - "Can't retrieve results from line %1: x and/or value are invalid") + << QObject::tr("Can't retrieve results from line %1: x is invalid") .arg(line); } } diff --git a/plugins/amda/tests-resources/TestAmdaResultParser/NaNX.txt b/plugins/amda/tests-resources/TestAmdaResultParser/NaNX.txt new file mode 100644 index 0000000..e90bbca --- /dev/null +++ b/plugins/amda/tests-resources/TestAmdaResultParser/NaNX.txt @@ -0,0 +1,6 @@ +#Sampling Time : 60 +#Time Format : YYYY-MM-DDThh:mm:ss.mls +#imf(0) - Type : Local Parameter @ CDPP/AMDA - Name : bx_gse - Units : nT - Size : 1 - Frame : GSE - Mission : ACE - Instrument : MFI - Dataset : mfi_final-prelim +NaN -3.01425 +2013-09-23T09:01:30.000 -2.71850 +2013-09-23T09:02:30.000 -2.52150 \ No newline at end of file diff --git a/plugins/amda/tests/TestAmdaResultParser.cpp b/plugins/amda/tests/TestAmdaResultParser.cpp index 702b367..9b21c44 100644 --- a/plugins/amda/tests/TestAmdaResultParser.cpp +++ b/plugins/amda/tests/TestAmdaResultParser.cpp @@ -11,6 +11,26 @@ namespace { const auto TESTS_RESOURCES_PATH = QFileInfo{QString{AMDA_TESTS_RESOURCES_DIR}, "TestAmdaResultParser"}.absoluteFilePath(); +/// Compares two vectors that can potentially contain NaN values +bool compareVectors(const QVector &v1, const QVector &v2) +{ + if (v1.size() != v2.size()) { + return false; + } + + auto result = true; + auto v2It = v2.cbegin(); + for (auto v1It = v1.cbegin(), v1End = v1.cend(); v1It != v1End && result; ++v1It, ++v2It) { + auto v1Value = *v1It; + auto v2Value = *v2It; + + // If v1 is NaN, v2 has to be NaN too + result = std::isnan(v1Value) ? std::isnan(v2Value) : (v1Value == v2Value); + } + + return result; +} + QString inputFilePath(const QString &inputFileName) { return QFileInfo{TESTS_RESOURCES_PATH, inputFileName}.absoluteFilePath(); @@ -47,9 +67,10 @@ struct ExpectedResults { QVERIFY(scalarSeries->xAxisUnit() == m_XAxisUnit); QVERIFY(scalarSeries->valuesUnit() == m_ValuesUnit); - // Checks values - QVERIFY(scalarSeries->xAxisData()->data() == m_XAxisData); - QVERIFY(scalarSeries->valuesData()->data() == m_ValuesData); + // Checks values : as the vectors can potentially contain NaN values, we must use a + // custom vector comparison method + QVERIFY(compareVectors(scalarSeries->xAxisData()->data(), m_XAxisData)); + QVERIFY(compareVectors(scalarSeries->valuesData()->data(), m_ValuesData)); } else { QVERIFY(results == nullptr); @@ -102,7 +123,7 @@ void TestAmdaResultParser::testReadTxt_data() return QDateTime{{year, month, day}, {hours, minutes, seconds}, Qt::UTC}; }; - // Valid file + // Valid files QTest::newRow("Valid file") << QStringLiteral("ValidScalar1.txt") << ExpectedResults{ @@ -115,6 +136,22 @@ void TestAmdaResultParser::testReadTxt_data() QVector{-2.83950, -2.71850, -2.52150, -2.57633, -2.58050, -2.48325, -2.63025, -2.55800, -2.43250, -2.42200}}; + QTest::newRow("Valid file (value of first line is invalid but it is converted to NaN") + << QStringLiteral("WrongValue.txt") + << ExpectedResults{ + Unit{QStringLiteral("nT"), true}, Unit{}, + QVector{dateTime(2013, 9, 23, 9, 0, 30), dateTime(2013, 9, 23, 9, 1, 30), + dateTime(2013, 9, 23, 9, 2, 30)}, + QVector{std::numeric_limits::quiet_NaN(), -2.71850, -2.52150}}; + + QTest::newRow("Valid file that contains NaN values") + << QStringLiteral("NaNValue.txt") + << ExpectedResults{ + Unit{QStringLiteral("nT"), true}, Unit{}, + QVector{dateTime(2013, 9, 23, 9, 0, 30), dateTime(2013, 9, 23, 9, 1, 30), + dateTime(2013, 9, 23, 9, 2, 30)}, + QVector{std::numeric_limits::quiet_NaN(), -2.71850, -2.52150}}; + // Valid files but with some invalid lines (wrong unit, wrong values, etc.) QTest::newRow("No unit file") << QStringLiteral("NoUnit.txt") << ExpectedResults{Unit{QStringLiteral(""), true}, Unit{}, @@ -141,15 +178,8 @@ void TestAmdaResultParser::testReadTxt_data() QVector{dateTime(2013, 9, 23, 9, 1, 30), dateTime(2013, 9, 23, 9, 2, 30)}, QVector{-2.71850, -2.52150}}; - QTest::newRow("Wrong results file (value of first line is invalid") - << QStringLiteral("WrongValue.txt") - << ExpectedResults{ - Unit{QStringLiteral("nT"), true}, Unit{}, - QVector{dateTime(2013, 9, 23, 9, 1, 30), dateTime(2013, 9, 23, 9, 2, 30)}, - QVector{-2.71850, -2.52150}}; - - QTest::newRow("Wrong results file (value of first line is NaN") - << QStringLiteral("NaNValue.txt") + QTest::newRow("Wrong results file (x of first line is NaN") + << QStringLiteral("NaNX.txt") << ExpectedResults{ Unit{QStringLiteral("nT"), true}, Unit{}, QVector{dateTime(2013, 9, 23, 9, 1, 30), dateTime(2013, 9, 23, 9, 2, 30)},