Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/ManipulationLogic.cs @ 10699

Last change on this file since 10699 was 10672, checked in by sbreuer, 11 years ago
  • change function headers in manipulationlogic to interface datatypes
  • implemented find and replace logic
File size: 10.4 KB
RevLine 
[10539]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
[10193]23using System.Collections.Generic;
24using System.Linq;
[10249]25using HeuristicLab.Data;
[10193]26
[10249]27namespace HeuristicLab.DataPreprocessing {
[10369]28  public class ManipulationLogic : IManipulationLogic {
[10586]29    private ITransactionalPreprocessingData preprocessingData;
[10615]30    private IStatisticsLogic statisticsLogic;
[10249]31    private ISearchLogic searchLogic;
[10672]32    private IDataGridLogic dataGridLogic;
[10193]33
[10672]34    public ManipulationLogic(ITransactionalPreprocessingData _prepocessingData, ISearchLogic theSearchLogic, IStatisticsLogic theStatisticsLogic, IDataGridLogic theDataGridLogic) {
[10249]35      preprocessingData = _prepocessingData;
36      searchLogic = theSearchLogic;
[10615]37      statisticsLogic = theStatisticsLogic;
[10672]38      dataGridLogic = theDataGridLogic;
[10249]39    }
[10193]40
[10367]41    public void ReplaceIndicesByValue<T>(int columnIndex, IEnumerable<int> rowIndices, T value) {
42      foreach (int index in rowIndices) {
43        preprocessingData.SetCell<T>(columnIndex, index, value);
[10249]44      }
45    }
[10193]46
[10672]47    public void ReplaceIndicesByAverageValue(IDictionary<int, IList<int>> cells) {
[10612]48      preprocessingData.InTransaction(() => {
49        foreach (var column in cells) {
[10615]50          if (preprocessingData.IsType<double>(column.Key)) {
51            double average = statisticsLogic.GetAverage(column.Key);
52            ReplaceIndicesByValue<double>(column.Key, column.Value, average);
53          } else if (preprocessingData.IsType<DateTime>(column.Key)) {
54            DateTime average = statisticsLogic.GetAverageDateTime(column.Key);
55            ReplaceIndicesByValue<DateTime>(column.Key, column.Value, average);
56          }
[10612]57        }
58      });
[10249]59    }
[10193]60
[10672]61    public void ReplaceIndicesByMedianValue(IDictionary<int, IList<int>> cells) {
[10612]62      preprocessingData.InTransaction(() => {
63        foreach (var column in cells) {
[10615]64          if (preprocessingData.IsType<double>(column.Key)) {
65            double median = statisticsLogic.GetMedian(column.Key);
66            ReplaceIndicesByValue<double>(column.Key, column.Value, median);
67          } else if (preprocessingData.IsType<DateTime>(column.Key)) {
68            DateTime median = statisticsLogic.GetMedianDateTime(column.Key);
69            ReplaceIndicesByValue<DateTime>(column.Key, column.Value, median);
70          }
[10612]71        }
72      });
[10249]73    }
[10193]74
[10672]75    public void ReplaceIndicesByRandomValue(IDictionary<int, IList<int>> cells) {
[10612]76      preprocessingData.InTransaction(() => {
77        Random r = new Random();
[10193]78
[10612]79        foreach (var column in cells) {
[10615]80          if (preprocessingData.IsType<double>(column.Key)) {
81            double max = statisticsLogic.GetMax<double>(column.Key);
82            double min = statisticsLogic.GetMin<double>(column.Key);
83            double randMultiplier = (max - min);
84            foreach (int index in column.Value) {
85              double rand = r.NextDouble() * randMultiplier + min;
86              preprocessingData.SetCell<double>(column.Key, index, rand);
87            }
88          } else if (preprocessingData.IsType<DateTime>(column.Key)) {
89            DateTime min = statisticsLogic.GetMin<DateTime>(column.Key);
90            DateTime max = statisticsLogic.GetMax<DateTime>(column.Key);
91            double randMultiplier = (max - min).TotalSeconds;
92            foreach (int index in column.Value) {
93              double rand = r.NextDouble() * randMultiplier;
94              preprocessingData.SetCell<DateTime>(column.Key, index, min.AddSeconds(rand));
95            }
[10612]96          }
[10590]97        }
[10612]98      });
[10249]99    }
[10193]100
[10672]101    public void ReplaceIndicesByLinearInterpolationOfNeighbours(IDictionary<int, IList<int>> cells) {
[10612]102      preprocessingData.InTransaction(() => {
103        foreach (var column in cells) {
[10621]104          int countValues = 0;
[10615]105          if (preprocessingData.IsType<double>(column.Key)) {
[10621]106            countValues = preprocessingData.GetValues<double>(column.Key).Count();
107          } else if (preprocessingData.IsType<DateTime>(column.Key)) {
108            countValues = preprocessingData.GetValues<DateTime>(column.Key).Count();
109          }
[10193]110
[10621]111          foreach (int index in column.Value) {
112            // dont replace first or last values
113            if (index > 0 && index < countValues) {
114              int prevIndex = indexOfPrevPresentValue(column.Key, index);
115              int nextIndex = indexOfNextPresentValue(column.Key, index);
116
117              // no neighbours found
118              if (prevIndex < 0 && nextIndex >= countValues) {
119                continue;
120              }
121
122              int valuesToInterpolate = nextIndex - prevIndex;
123
124              if (preprocessingData.IsType<double>(column.Key)) {
[10615]125                double prev = preprocessingData.GetCell<double>(column.Key, prevIndex);
126                double next = preprocessingData.GetCell<double>(column.Key, nextIndex);
127                double interpolationStep = (next - prev) / valuesToInterpolate;
128
129                for (int i = prevIndex; i < nextIndex; ++i) {
130                  double interpolated = prev + (interpolationStep * (i - prevIndex));
131                  preprocessingData.SetCell<double>(column.Key, i, interpolated);
132                }
[10621]133              } else if (preprocessingData.IsType<DateTime>(column.Key)) {
[10615]134                DateTime prev = preprocessingData.GetCell<DateTime>(column.Key, prevIndex);
135                DateTime next = preprocessingData.GetCell<DateTime>(column.Key, nextIndex);
136                double interpolationStep = (next - prev).TotalSeconds / valuesToInterpolate;
137
138                for (int i = prevIndex; i < nextIndex; ++i) {
139                  DateTime interpolated = prev.AddSeconds(interpolationStep * (i - prevIndex));
140                  preprocessingData.SetCell<DateTime>(column.Key, i, interpolated);
141                }
[10612]142              }
[10590]143            }
[10249]144          }
[10193]145        }
[10612]146      });
[10249]147    }
[10193]148
[10367]149    private int indexOfPrevPresentValue(int columnIndex, int start) {
[10249]150      int offset = start - 1;
[10367]151      while (offset >= 0 && searchLogic.IsMissingValue(columnIndex, offset)) {
[10249]152        offset--;
153      }
[10234]154
[10249]155      return offset;
156    }
[10234]157
[10367]158    private int indexOfNextPresentValue(int columnIndex, int start) {
[10249]159      int offset = start + 1;
[10367]160      while (offset < preprocessingData.Rows && searchLogic.IsMissingValue(columnIndex, offset)) {
[10249]161        offset++;
162      }
[10234]163
[10249]164      return offset;
165    }
[10234]166
[10672]167    public void ReplaceIndicesByMostCommonValue(IDictionary<int, IList<int>> cells) {
[10612]168      preprocessingData.InTransaction(() => {
169        foreach (var column in cells) {
170          if (preprocessingData.IsType<double>(column.Key)) {
[10615]171            ReplaceIndicesByValue<double>(column.Key, column.Value, statisticsLogic.GetMostCommonValue<double>(column.Key));
[10612]172          } else if (preprocessingData.IsType<string>(column.Key)) {
[10615]173            ReplaceIndicesByValue<string>(column.Key, column.Value, statisticsLogic.GetMostCommonValue<string>(column.Key));
[10612]174          } else if (preprocessingData.IsType<DateTime>(column.Key)) {
[10615]175            ReplaceIndicesByValue<DateTime>(column.Key, column.Value, statisticsLogic.GetMostCommonValue<DateTime>(column.Key));
[10612]176          } else {
177            throw new ArgumentException("column with index: " + column.Key + " contains a non supported type.");
178          }
[10590]179        }
[10612]180      });
[10249]181    }
[10218]182
[10249]183    public void ShuffleWithRanges(IEnumerable<IntRange> ranges) {
184      // init random outside loop
185      Random random = new Random();
[10218]186
[10612]187      preprocessingData.InTransaction(() => {
188        // process all given ranges - e.g. TrainingPartition, Trainingpartition
189        foreach (IntRange range in ranges) {
190          List<Tuple<int, int>> shuffledIndices = new List<Tuple<int, int>>();
[10218]191
[10612]192          // generate random indices used for shuffeling each column
193          for (int i = range.End; i > range.Start; --i) {
194            int rand = random.Next(range.Start, i);
195            shuffledIndices.Add(new Tuple<int, int>(i, rand));
196          }
197
198          ReOrderToIndices(shuffledIndices);
[10218]199        }
[10612]200      });
[10253]201    }
202
[10535]203    public void ReOrderToIndices(IEnumerable<int> indices) {
[10256]204      List<Tuple<int, int>> indicesTuple = new List<Tuple<int, int>>();
[10255]205
[10256]206      for (int i = 0; i < indices.Count(); ++i) {
[10311]207        indicesTuple.Add(new Tuple<int, int>(i, indices.ElementAt(i)));
[10256]208      }
209
[10535]210      ReOrderToIndices(indicesTuple);
[10255]211    }
212
[10535]213    public void ReOrderToIndices(IList<System.Tuple<int, int>> indices) {
[10612]214      preprocessingData.InTransaction(() => {
215        for (int i = 0; i < preprocessingData.Columns; ++i) {
216          if (preprocessingData.IsType<double>(i)) {
217            reOrderToIndices<double>(i, indices);
218          } else if (preprocessingData.IsType<string>(i)) {
219            reOrderToIndices<string>(i, indices);
220          } else if (preprocessingData.IsType<DateTime>(i)) {
221            reOrderToIndices<DateTime>(i, indices);
222          }
[10249]223        }
[10612]224      });
[10249]225    }
[10218]226
[10367]227    private void reOrderToIndices<T>(int columnIndex, IList<Tuple<int, int>> indices) {
[10308]228
[10367]229      List<T> originalData = new List<T>(preprocessingData.GetValues<T>(columnIndex));
[10308]230
[10249]231      // process all columns equally
232      foreach (Tuple<int, int> index in indices) {
233        int originalIndex = index.Item1;
234        int replaceIndex = index.Item2;
[10218]235
[10308]236        T replaceValue = originalData.ElementAt<T>(replaceIndex);
[10367]237        preprocessingData.SetCell<T>(columnIndex, originalIndex, replaceValue);
[10249]238      }
[10193]239    }
[10672]240
241    public void ReplaceIndicesByValue(IDictionary<int, IList<int>> cells, string value) {
242      preprocessingData.InTransaction(() => {
243        foreach (var column in cells) {
244          foreach (var rowIdx in column.Value) {
245            dataGridLogic.SetValue(value, column.Key, rowIdx);
246          }
247        }
248      });
249    }
[10249]250  }
[10193]251}
Note: See TracBrowser for help on using the repository browser.