From 85f28a641f81cc0f5a3d4323a94223a45a8b231a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steinar=20Elgs=C3=A6ter?= Date: Wed, 27 Nov 2024 14:11:18 +0100 Subject: [PATCH] - fix issue with TimeSeriesData.LoadFromCsv crashin if csv has just dates but no variables. --- .../TestData/test_edgecase_justdates.csv | 110 ++++++++++++++++++ .../TestData/test_edgecase_justdates2.csv | 110 ++++++++++++++++++ TimeSeriesAnalysis.Tests/Tests/CsvTest.cs | 21 ++++ TimeSeriesAnalysis.csproj | 2 +- TimeSeriesAnalysis/TimeSeriesDataSet.cs | 4 + 5 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 TimeSeriesAnalysis.Tests/TestData/test_edgecase_justdates.csv create mode 100644 TimeSeriesAnalysis.Tests/TestData/test_edgecase_justdates2.csv diff --git a/TimeSeriesAnalysis.Tests/TestData/test_edgecase_justdates.csv b/TimeSeriesAnalysis.Tests/TestData/test_edgecase_justdates.csv new file mode 100644 index 0000000..fd3980b --- /dev/null +++ b/TimeSeriesAnalysis.Tests/TestData/test_edgecase_justdates.csv @@ -0,0 +1,110 @@ +Time +2024-11-26 12:29:33 +2024-11-26 12:30:33 +2024-11-26 12:31:33 +2024-11-26 12:32:33 +2024-11-26 12:33:33 +2024-11-26 12:34:33 +2024-11-26 12:35:33 +2024-11-26 12:36:33 +2024-11-26 12:37:33 +2024-11-26 12:38:33 +2024-11-26 12:39:33 +2024-11-26 12:40:33 +2024-11-26 12:41:33 +2024-11-26 12:42:33 +2024-11-26 12:43:33 +2024-11-26 12:44:33 +2024-11-26 12:45:33 +2024-11-26 12:46:33 +2024-11-26 12:47:33 +2024-11-26 12:48:33 +2024-11-26 12:49:33 +2024-11-26 12:50:33 +2024-11-26 12:51:33 +2024-11-26 12:52:33 +2024-11-26 12:53:33 +2024-11-26 12:54:33 +2024-11-26 12:55:33 +2024-11-26 12:56:33 +2024-11-26 12:57:33 +2024-11-26 12:58:33 +2024-11-26 12:59:33 +2024-11-26 13:00:33 +2024-11-26 13:01:33 +2024-11-26 13:02:33 +2024-11-26 13:03:33 +2024-11-26 13:04:33 +2024-11-26 13:05:33 +2024-11-26 13:06:33 +2024-11-26 13:07:33 +2024-11-26 13:08:33 +2024-11-26 13:09:33 +2024-11-26 13:10:33 +2024-11-26 13:11:33 +2024-11-26 13:12:33 +2024-11-26 13:13:33 +2024-11-26 13:14:33 +2024-11-26 13:15:33 +2024-11-26 13:16:33 +2024-11-26 13:17:33 +2024-11-26 13:18:33 +2024-11-26 13:19:33 +2024-11-26 13:20:33 +2024-11-26 13:21:33 +2024-11-26 13:22:33 +2024-11-26 13:23:33 +2024-11-26 13:24:33 +2024-11-26 13:25:33 +2024-11-26 13:26:33 +2024-11-26 13:27:33 +2024-11-26 13:28:33 +2024-11-26 13:29:33 +2024-11-26 13:30:33 +2024-11-26 13:31:33 +2024-11-26 13:32:33 +2024-11-26 13:33:33 +2024-11-26 13:34:33 +2024-11-26 13:35:33 +2024-11-26 13:36:33 +2024-11-26 13:37:33 +2024-11-26 13:38:33 +2024-11-26 13:39:33 +2024-11-26 13:40:33 +2024-11-26 13:41:33 +2024-11-26 13:42:33 +2024-11-26 13:43:33 +2024-11-26 13:44:33 +2024-11-26 13:45:33 +2024-11-26 13:46:33 +2024-11-26 13:47:33 +2024-11-26 13:48:33 +2024-11-26 13:49:33 +2024-11-26 13:50:33 +2024-11-26 13:51:33 +2024-11-26 13:52:33 +2024-11-26 13:53:33 +2024-11-26 13:54:33 +2024-11-26 13:55:33 +2024-11-26 13:56:33 +2024-11-26 13:57:33 +2024-11-26 13:58:33 +2024-11-26 13:59:33 +2024-11-26 14:00:33 +2024-11-26 14:01:33 +2024-11-26 14:02:33 +2024-11-26 14:03:33 +2024-11-26 14:04:33 +2024-11-26 14:05:33 +2024-11-26 14:06:33 +2024-11-26 14:07:33 +2024-11-26 14:08:33 +2024-11-26 14:09:33 +2024-11-26 14:10:33 +2024-11-26 14:11:33 +2024-11-26 14:12:33 +2024-11-26 14:13:33 +2024-11-26 14:14:33 +2024-11-26 14:15:33 +2024-11-26 14:16:33 +2024-11-26 14:17:33 \ No newline at end of file diff --git a/TimeSeriesAnalysis.Tests/TestData/test_edgecase_justdates2.csv b/TimeSeriesAnalysis.Tests/TestData/test_edgecase_justdates2.csv new file mode 100644 index 0000000..e1337d4 --- /dev/null +++ b/TimeSeriesAnalysis.Tests/TestData/test_edgecase_justdates2.csv @@ -0,0 +1,110 @@ +Time; +2024-11-26 12:29:33 +2024-11-26 12:30:33 +2024-11-26 12:31:33 +2024-11-26 12:32:33 +2024-11-26 12:33:33 +2024-11-26 12:34:33 +2024-11-26 12:35:33 +2024-11-26 12:36:33 +2024-11-26 12:37:33 +2024-11-26 12:38:33 +2024-11-26 12:39:33 +2024-11-26 12:40:33 +2024-11-26 12:41:33 +2024-11-26 12:42:33 +2024-11-26 12:43:33 +2024-11-26 12:44:33 +2024-11-26 12:45:33 +2024-11-26 12:46:33 +2024-11-26 12:47:33 +2024-11-26 12:48:33 +2024-11-26 12:49:33 +2024-11-26 12:50:33 +2024-11-26 12:51:33 +2024-11-26 12:52:33 +2024-11-26 12:53:33 +2024-11-26 12:54:33 +2024-11-26 12:55:33 +2024-11-26 12:56:33 +2024-11-26 12:57:33 +2024-11-26 12:58:33 +2024-11-26 12:59:33 +2024-11-26 13:00:33 +2024-11-26 13:01:33 +2024-11-26 13:02:33 +2024-11-26 13:03:33 +2024-11-26 13:04:33 +2024-11-26 13:05:33 +2024-11-26 13:06:33 +2024-11-26 13:07:33 +2024-11-26 13:08:33 +2024-11-26 13:09:33 +2024-11-26 13:10:33 +2024-11-26 13:11:33 +2024-11-26 13:12:33 +2024-11-26 13:13:33 +2024-11-26 13:14:33 +2024-11-26 13:15:33 +2024-11-26 13:16:33 +2024-11-26 13:17:33 +2024-11-26 13:18:33 +2024-11-26 13:19:33 +2024-11-26 13:20:33 +2024-11-26 13:21:33 +2024-11-26 13:22:33 +2024-11-26 13:23:33 +2024-11-26 13:24:33 +2024-11-26 13:25:33 +2024-11-26 13:26:33 +2024-11-26 13:27:33 +2024-11-26 13:28:33 +2024-11-26 13:29:33 +2024-11-26 13:30:33 +2024-11-26 13:31:33 +2024-11-26 13:32:33 +2024-11-26 13:33:33 +2024-11-26 13:34:33 +2024-11-26 13:35:33 +2024-11-26 13:36:33 +2024-11-26 13:37:33 +2024-11-26 13:38:33 +2024-11-26 13:39:33 +2024-11-26 13:40:33 +2024-11-26 13:41:33 +2024-11-26 13:42:33 +2024-11-26 13:43:33 +2024-11-26 13:44:33 +2024-11-26 13:45:33 +2024-11-26 13:46:33 +2024-11-26 13:47:33 +2024-11-26 13:48:33 +2024-11-26 13:49:33 +2024-11-26 13:50:33 +2024-11-26 13:51:33 +2024-11-26 13:52:33 +2024-11-26 13:53:33 +2024-11-26 13:54:33 +2024-11-26 13:55:33 +2024-11-26 13:56:33 +2024-11-26 13:57:33 +2024-11-26 13:58:33 +2024-11-26 13:59:33 +2024-11-26 14:00:33 +2024-11-26 14:01:33 +2024-11-26 14:02:33 +2024-11-26 14:03:33 +2024-11-26 14:04:33 +2024-11-26 14:05:33 +2024-11-26 14:06:33 +2024-11-26 14:07:33 +2024-11-26 14:08:33 +2024-11-26 14:09:33 +2024-11-26 14:10:33 +2024-11-26 14:11:33 +2024-11-26 14:12:33 +2024-11-26 14:13:33 +2024-11-26 14:14:33 +2024-11-26 14:15:33 +2024-11-26 14:16:33 +2024-11-26 14:17:33 \ No newline at end of file diff --git a/TimeSeriesAnalysis.Tests/Tests/CsvTest.cs b/TimeSeriesAnalysis.Tests/Tests/CsvTest.cs index d43874d..f809ee7 100644 --- a/TimeSeriesAnalysis.Tests/Tests/CsvTest.cs +++ b/TimeSeriesAnalysis.Tests/Tests/CsvTest.cs @@ -28,6 +28,27 @@ public void CSV_read(string filename) Assert.AreEqual(new double[,] { { Double.NaN, 1, 2 }, { Double.NaN, 3, 4 }, { Double.NaN, 5, 6 } }, doubleData); } + [TestCase(@"test_edgecase_justdates.csv", Description = "")] + [TestCase(@"test_edgecase_justdates2.csv", Description = "")] + public void CsvLoadAsTimeSeries_Edgecases_DoesNotCrash(string filename) + { + var ret = CSV.LoadDataFromCsvAsTimeSeries(@"..\..\..\TestData\" + filename, ';', out var dateTimes, + out var variables); + } + + [TestCase(@"test_edgecase_justdates.csv", Description = "")] + [TestCase(@"test_edgecase_justdates2.csv", Description = "")] + public void TimeSeriesDataSet_LoadFromCsv_Edgecases_DoesNotCrash(string filename) + { + var csvTxt = File.ReadAllText(@"..\..\..\TestData\"+filename); + var dataset = new TimeSeriesDataSet(); + var ret = dataset.LoadFromCsv(new CsvContent(csvTxt),';'); + Assert.IsFalse(ret); + } + + + + [TestCase, Explicit] public void Xml_load() diff --git a/TimeSeriesAnalysis.csproj b/TimeSeriesAnalysis.csproj index 4f8d940..174f909 100644 --- a/TimeSeriesAnalysis.csproj +++ b/TimeSeriesAnalysis.csproj @@ -14,7 +14,7 @@ False https://github.com/equinor/TimeSeriesAnalysis.git readme.md - 1.3.39 + 1.3.40 Equinor Equinor true diff --git a/TimeSeriesAnalysis/TimeSeriesDataSet.cs b/TimeSeriesAnalysis/TimeSeriesDataSet.cs index 1e31165..4062602 100644 --- a/TimeSeriesAnalysis/TimeSeriesDataSet.cs +++ b/TimeSeriesAnalysis/TimeSeriesDataSet.cs @@ -248,6 +248,10 @@ private bool Fill(DateTime[] dateTimes, Dictionary variableDic { variableDict.Remove("time"); } + // if the file only included the "time" but no values + if (variableDict.Keys.Count() == 0) + return false; + N = variableDict[variableDict.Keys.First()].Length; if (N == 0) {