Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10573 was 10539, checked in by rstoll, 11 years ago

Added License notice

File size: 7.0 KB
Line 
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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Data;
26
27namespace HeuristicLab.DataPreprocessing {
28  public class ManipulationLogic : IManipulationLogic {
29    private IPreprocessingData preprocessingData;
30    private IStatisticsLogic statisticInfo;
31    private ISearchLogic searchLogic;
32
33    public ManipulationLogic(IPreprocessingData _prepocessingData, ISearchLogic theSearchLogic, IStatisticsLogic theStatisticsLogic) {
34      preprocessingData = _prepocessingData;
35      searchLogic = theSearchLogic;
36      statisticInfo = theStatisticsLogic;
37    }
38
39    public void ReplaceIndicesByValue<T>(int columnIndex, IEnumerable<int> rowIndices, T value) {
40      foreach (int index in rowIndices) {
41        preprocessingData.SetCell<T>(columnIndex, index, value);
42      }
43    }
44
45    public void ReplaceIndicesByAverageValue(int columnIndex, IEnumerable<int> rowIndices) {
46      double average = statisticInfo.GetAverage(columnIndex);
47      ReplaceIndicesByValue<double>(columnIndex, rowIndices, average);
48    }
49
50    public void ReplaceIndicesByMedianValue(int columnIndex, IEnumerable<int> rowIndices) {
51      double median = statisticInfo.GetMedian(columnIndex);
52      ReplaceIndicesByValue<double>(columnIndex, rowIndices, median);
53    }
54
55    public void ReplaceIndicesByRandomValue(int columnIndex, IEnumerable<int> rowIndices) {
56      Random r = new Random();
57
58      double max = statisticInfo.GetMax<double>(columnIndex);
59      double min = statisticInfo.GetMin<double>(columnIndex);
60      double randMultiplier = (max - min);
61      foreach (int index in rowIndices) {
62        double rand = r.NextDouble() * randMultiplier + min;
63        preprocessingData.SetCell<double>(columnIndex, index, rand);
64      }
65    }
66
67    public void ReplaceIndicesByLinearInterpolationOfNeighbours(int columnIndex, IEnumerable<int> rowIndices) {
68      int countValues = preprocessingData.GetValues<double>(columnIndex).Count();
69      foreach (int index in rowIndices) {
70        // dont replace first or last values
71        if (index > 0 && index < countValues) {
72          int prevIndex = indexOfPrevPresentValue(columnIndex, index);
73          int nextIndex = indexOfNextPresentValue(columnIndex, index);
74
75          // no neighbours found
76          if (prevIndex < 0 && nextIndex >= countValues) {
77            continue;
78          }
79          double prev = preprocessingData.GetCell<double>(columnIndex, prevIndex);
80          double next = preprocessingData.GetCell<double>(columnIndex, nextIndex);
81
82          int valuesToInterpolate = nextIndex - prevIndex;
83
84          double interpolationStep = (prev + next) / valuesToInterpolate;
85
86          for (int i = prevIndex; i < nextIndex; ++i) {
87            double interpolated = prev + (interpolationStep * (i - prevIndex));
88            preprocessingData.SetCell<double>(columnIndex, i, interpolated);
89          }
90        }
91      }
92    }
93
94    private int indexOfPrevPresentValue(int columnIndex, int start) {
95      int offset = start - 1;
96      while (offset >= 0 && searchLogic.IsMissingValue(columnIndex, offset)) {
97        offset--;
98      }
99
100      return offset;
101    }
102
103    private int indexOfNextPresentValue(int columnIndex, int start) {
104      int offset = start + 1;
105      while (offset < preprocessingData.Rows && searchLogic.IsMissingValue(columnIndex, offset)) {
106        offset++;
107      }
108
109      return offset;
110    }
111
112    public void ReplaceIndicesByMostCommonValue(int columnIndex, IEnumerable<int> rowIndices) {
113      if (preprocessingData.IsType<double>(columnIndex)) {
114        ReplaceIndicesByValue<double>(columnIndex, rowIndices, statisticInfo.GetMostCommonValue<double>(columnIndex));
115      } else if (preprocessingData.IsType<string>(columnIndex)) {
116        ReplaceIndicesByValue<string>(columnIndex, rowIndices, statisticInfo.GetMostCommonValue<string>(columnIndex));
117      } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
118        ReplaceIndicesByValue<DateTime>(columnIndex, rowIndices, statisticInfo.GetMostCommonValue<DateTime>(columnIndex));
119      } else {
120        throw new ArgumentException("column with index: " + columnIndex + " contains a non supported type.");
121      }
122    }
123
124    public void ShuffleWithRanges(IEnumerable<IntRange> ranges) {
125      // init random outside loop
126      Random random = new Random();
127
128      // process all given ranges - e.g. TrainingPartition, Trainingpartition
129      foreach (IntRange range in ranges) {
130        List<Tuple<int, int>> shuffledIndices = new List<Tuple<int, int>>();
131
132        // generate random indices used for shuffeling each column
133        for (int i = range.End; i > range.Start; --i) {
134          int rand = random.Next(range.Start, i);
135          shuffledIndices.Add(new Tuple<int, int>(i, rand));
136        }
137
138        ReOrderToIndices(shuffledIndices);
139      }
140    }
141
142    public void ReOrderToIndices(IEnumerable<int> indices) {
143      List<Tuple<int, int>> indicesTuple = new List<Tuple<int, int>>();
144
145      for (int i = 0; i < indices.Count(); ++i) {
146        indicesTuple.Add(new Tuple<int, int>(i, indices.ElementAt(i)));
147      }
148
149      ReOrderToIndices(indicesTuple);
150    }
151
152    public void ReOrderToIndices(IList<System.Tuple<int, int>> indices) {
153      for (int i = 0; i < preprocessingData.Columns; ++i) {
154        if (preprocessingData.IsType<double>(i)) {
155          reOrderToIndices<double>(i, indices);
156        } else if (preprocessingData.IsType<string>(i)) {
157          reOrderToIndices<string>(i, indices);
158        } else if (preprocessingData.IsType<DateTime>(i)) {
159          reOrderToIndices<DateTime>(i, indices);
160        }
161      }
162    }
163
164    private void reOrderToIndices<T>(int columnIndex, IList<Tuple<int, int>> indices) {
165
166      List<T> originalData = new List<T>(preprocessingData.GetValues<T>(columnIndex));
167
168      // process all columns equally
169      foreach (Tuple<int, int> index in indices) {
170        int originalIndex = index.Item1;
171        int replaceIndex = index.Item2;
172
173        T replaceValue = originalData.ElementAt<T>(replaceIndex);
174        preprocessingData.SetCell<T>(columnIndex, originalIndex, replaceValue);
175      }
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.