#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.DataPreprocessing { [Item("PreprocessingData", "Represents data used for preprocessing.")] public class PreprocessingData : NamedItem, IPreprocessingData { private IDictionary variableValues; private IList variableNames; private IDictionary variableNameIndices; private double trainingToTestRatio; private PreprocessingData(PreprocessingData original, Cloner cloner) : base(original, cloner) { variableValues = new Dictionary(variableValues); variableNameIndices = new Dictionary(variableNameIndices); } public PreprocessingData(IDataAnalysisProblemData problemData) : base() { Name = "-"; variableNames = new List(problemData.Dataset.VariableNames); // create dictionary from variable name to index variableNameIndices = new Dictionary(); var variableNamesList = problemData.Dataset.VariableNames.ToList(); for (int i = 0; i < variableNamesList.Count; i++) { variableNameIndices.Add(variableNamesList[i], i); } // copy values variableValues = new Dictionary(); foreach (var variableName in problemData.Dataset.VariableNames) { if (problemData.Dataset.IsType(variableName)) { variableValues[variableName] = problemData.Dataset.GetDoubleValues(variableName).ToList(); } else if (problemData.Dataset.IsType(variableName)) { variableValues[variableName] = CreateColumn(problemData.Dataset, variableNameIndices[variableName], x => x); } else if (problemData.Dataset.IsType(variableName)) { variableValues[variableName] = CreateColumn(problemData.Dataset, variableNameIndices[variableName], x => DateTime.Parse(x)); } else { throw new ArgumentException("The datatype of column " + variableName + " must be of type List, List or List"); } } trainingToTestRatio = (double)problemData.TrainingPartition.Size / problemData.TestPartition.Size; Columns = problemData.Dataset.Columns; Rows = problemData.Dataset.Rows; } private static IList CreateColumn(Dataset ds, int column, Func selector) { var list = new List(ds.Rows); for (int row = 0; row < ds.Rows; row++) { list[row] = selector(ds.GetValue(row, column)); } return list; } #region NamedItem abstract Member Implementations public override IDeepCloneable Clone(Cloner cloner) { return new PreprocessingData(this, cloner); } #endregion #region IPreprocessingData Members public T GetCell(int columnIndex, int rowIndex) { return (T)variableValues[variableNames[columnIndex]][rowIndex]; } public void SetCell(int columnIndex, int rowIndex, T value) { variableValues[variableNames[columnIndex]][rowIndex] = value; } public T GetCell(string variableName, int row) { return (T)variableValues[variableName][row]; } public void SetCell(string variableName, int row, T value) { variableValues[variableName][row] = value; } public IEnumerable GetValues(int columnIndex) { return (IEnumerable)variableValues[variableNames[columnIndex]]; } public IEnumerable GetValues(string variableName) { return (IEnumerable)variableValues[variableName]; } public void SetValues(int columnIndex, IEnumerable values) { variableValues[variableNames[columnIndex]] = values.ToList(); } public void SetValues(string variableName, IEnumerable values) { variableValues[variableName] = values.ToList(); } public void InsertRow(int rowIndex) { throw new NotImplementedException(); } public void DeleteRow(int rowIndex) { throw new NotImplementedException(); } public void InsertColumn(string variableName, int columnIndex) { throw new NotImplementedException(); } public void DeleteColumn(int columnIndex) { throw new NotImplementedException(); } public void DeleteColumn(string variableName) { throw new NotImplementedException(); } IEnumerable IPreprocessingData.VariableNames { get { throw new NotImplementedException(); } } public bool IsType(int columnIndex) { throw new NotImplementedException(); } public bool IsType(string variableName) { throw new NotImplementedException(); } public int Columns { get; private set; } public int Rows { get; private set; } public void ExportTo(IDataAnalysisProblemData problemData) { throw new NotImplementedException(); } #endregion } }