Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/18/19 17:16:38 (6 years ago)
Author:
abeham
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs

    r16796 r16820  
    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
Note: See TracChangeset for help on using the changeset viewer.