Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis-3.4/DatasetTest.cs @ 16820

Last change on this file since 16820 was 16820, checked in by abeham, 5 years ago

#3002: Implement static methods to build dataset from row-wise data and added unit test

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Problems.DataAnalysis;
27using Microsoft.VisualStudio.TestTools.UnitTesting;
28
29namespace HeuristicLab.Tests.HeuristicLab.Problems.DataAnalysis {
30
31  [TestClass()]
32  public class ClassificationVariableImpactCalculationTest {
33    private TestContext testContextInstance;
34    /// <summary>
35    ///Gets or sets the test context which provides
36    ///information about and functionality for the current test run.
37    ///</summary>
38    public TestContext TestContext {
39      get { return testContextInstance; }
40      set { testContextInstance = value; }
41    }
42
43    [TestMethod]
44    [TestCategory("Problems.DataAnalysis")]
45    [TestProperty("Time", "short")]
46    public void TestImportRowiseData() {
47      var matrix = new double[,] { { 1, 2, 3 }, { 2, 3, 1 }, { 3, 1, 2 }, { 1, 2, 3 }, { 2, 3, 1 } };
48      var ds = Dataset.FromRowData(new[] { "X1", "X2", "X3" }, matrix);
49      Assert.AreEqual(3, ds.Columns);
50      Assert.AreEqual(5, ds.Rows);
51      Assert.IsTrue(ds.GetDoubleValues("X1").SequenceEqual(new double[] { 1, 2, 3, 1, 2 }));
52
53      var listoflits = new List<List<double>>() {
54        new List<double>() { 1, 2, 3 },
55        new List<double>() { 2, 3, 1 },
56        new List<double>() { 3, 1, 2 },
57        new List<double>() { 1, 2, 3 },
58        new List<double>() { 2, 3, 1 }
59      };
60      ds = Dataset.FromRowData(new[] { "X1", "X2", "X3" }, listoflits);
61      Assert.AreEqual(3, ds.Columns);
62      Assert.AreEqual(5, ds.Rows);
63      Assert.IsTrue(ds.GetDoubleValues("X1").SequenceEqual(new double[] { 1, 2, 3, 1, 2 }));
64
65      var complexData = new List<IList>() {
66        new object[] { 1.0, new DateTime(2019, 1, 1), "a" },
67        new object[] { 2.0, new DateTime(2019, 1, 2), "b" },
68        new object[] { 3.0, new DateTime(2019, 1, 3), "c" },
69        new object[] { 4.0, new DateTime(2019, 1, 4), "d" },
70        new object[] { 5.0, new DateTime(2019, 1, 5), "e" },
71      };
72      ds = Dataset.FromRowData(new[] { "X1", "X2", "X3" }, complexData);
73      Assert.AreEqual(3, ds.Columns);
74      Assert.AreEqual(5, ds.Rows);
75      Assert.IsTrue(ds.GetDoubleValues("X1").SequenceEqual(new double[] { 1, 2, 3, 4, 5 }));
76      Assert.IsTrue(ds.GetDateTimeValues("X2").SequenceEqual(Enumerable.Range(1, 5).Select(d => new DateTime(2019, 1, d))));
77      Assert.IsTrue(ds.GetStringValues("X3").SequenceEqual(new string[] { "a", "b", "c", "d", "e" }));
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.