Changeset 17113


Ignore:
Timestamp:
07/08/19 15:40:39 (13 days ago)
Author:
abeham
Message:

#3002: merged to stable (16820, 16821)

Location:
stable
Files:
6 edited
1 copied

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.DataAnalysis

  • stable/HeuristicLab.Problems.DataAnalysis/3.4

  • stable/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs

    r17105 r17113  
    2525using System.Collections.ObjectModel;
    2626using System.Linq;
     27using HEAL.Attic;
    2728using HeuristicLab.Common;
    2829using HeuristicLab.Core;
    2930using HeuristicLab.Data;
    30 using HEAL.Attic;
    3131
    3232namespace HeuristicLab.Problems.DataAnalysis {
     
    113113        this.variableValues.Add(columName, values);
    114114      }
     115    }
     116
     117    public static Dataset FromRowData(IEnumerable<string> variableNames, double[,] data) {
     118      var colWise = new List<IList>(data.GetLength(1));
     119      for (var col = 0; col < data.GetLength(1); col++) {
     120        var column = new List<double>(data.GetLength(0));
     121        for (var row = 0; row < data.GetLength(0); row++) {
     122          column.Add(data[row, col]);
     123        }
     124        colWise.Add(column);
     125      }
     126      return new Dataset(variableNames, colWise);
     127    }
     128
     129    public static Dataset FromRowData(IEnumerable<string> variableNames, IEnumerable<IList> data) {
     130      var vnames = variableNames.ToList();
     131      var transposed = new List<IList>();
     132      var iter = data.GetEnumerator();
     133      if (!iter.MoveNext()) throw new ArgumentException("Data does not contain any rows", nameof(data));
     134      for (var i = 0; i < iter.Current.Count; i++) {
     135        if (i >= vnames.Count) throw new ArgumentException("There are more variables in data, than variable names.", nameof(variableNames));
     136        if (iter.Current[i] == null) throw new ArgumentException("Null values are not supported.", nameof(data));
     137        if (!IsAllowedType(iter.Current[i].GetType())) throw new ArgumentException("Data contains types that are not allowed.", nameof(data));
     138        if (iter.Current[i] is double d)
     139          transposed.Add(new List<double>() { d });
     140        else if (iter.Current[i] is DateTime dt)
     141          transposed.Add(new List<DateTime>() { dt });
     142        else if (iter.Current[i] is string s)
     143          transposed.Add(new List<string>() { s });
     144        else throw new NotSupportedException(string.Format("Variable {0} has type {1}. This is not supported when converting from row-wise data.", vnames[i], iter.Current[i].GetType()));
     145      }
     146      if (transposed.Count < vnames.Count) throw new ArgumentException("There are less variables in data, than variable names.", nameof(variableNames));
     147      while (iter.MoveNext()) {
     148        for (var i = 0; i < iter.Current.Count; i++)
     149          if (transposed[i].Add(iter.Current[i]) < 0)
     150            throw new ArgumentException(string.Format("Variable {0} has invalid value ({1})", vnames[i], iter.Current[i]), nameof(data));
     151      }
     152      return new Dataset(vnames, transposed);
    115153    }
    116154
  • stable/HeuristicLab.Tests

  • stable/HeuristicLab.Tests/HeuristicLab.Tests.csproj

    r17105 r17113  
    592592    <Compile Include="HeuristicLab.PluginInfraStructure-3.3\TypeExtensionsTest.cs" />
    593593    <Compile Include="HeuristicLab.Problems.DataAnalysis-3.4\ClassificationVariableImpactCalculationTest.cs" />
     594    <Compile Include="HeuristicLab.Problems.DataAnalysis-3.4\DatasetTest.cs" />
    594595    <Compile Include="HeuristicLab.Problems.DataAnalysis-3.4\IntervalTest.cs" />
    595596    <Compile Include="HeuristicLab.Problems.DataAnalysis-3.4\RegressionVariableImpactCalculationTest.cs" />
Note: See TracChangeset for help on using the changeset viewer.