Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10586 was 10586, checked in by tsteinre, 10 years ago
  • divided/refactored PreprocessingData into TransactionalPreprocessingData and preprocessingData
File size: 7.2 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 ITransactionalPreprocessingData preprocessingData;
30    private IStatisticsLogic statisticInfo;
31    private ISearchLogic searchLogic;
32
33    public ManipulationLogic(ITransactionalPreprocessingData _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      preprocessingData.BeginTransaction();
129      // process all given ranges - e.g. TrainingPartition, Trainingpartition
130      foreach (IntRange range in ranges) {
131        List<Tuple<int, int>> shuffledIndices = new List<Tuple<int, int>>();
132
133        // generate random indices used for shuffeling each column
134        for (int i = range.End; i > range.Start; --i) {
135          int rand = random.Next(range.Start, i);
136          shuffledIndices.Add(new Tuple<int, int>(i, rand));
137        }
138
139        ReOrderToIndices(shuffledIndices);
140      }
141      preprocessingData.EndTransaction();
142    }
143
144    public void ReOrderToIndices(IEnumerable<int> indices) {
145      List<Tuple<int, int>> indicesTuple = new List<Tuple<int, int>>();
146
147      for (int i = 0; i < indices.Count(); ++i) {
148        indicesTuple.Add(new Tuple<int, int>(i, indices.ElementAt(i)));
149      }
150
151      ReOrderToIndices(indicesTuple);
152    }
153
154    public void ReOrderToIndices(IList<System.Tuple<int, int>> indices) {
155      preprocessingData.BeginTransaction();
156      for (int i = 0; i < preprocessingData.Columns; ++i) {
157        if (preprocessingData.IsType<double>(i)) {
158          reOrderToIndices<double>(i, indices);
159        } else if (preprocessingData.IsType<string>(i)) {
160          reOrderToIndices<string>(i, indices);
161        } else if (preprocessingData.IsType<DateTime>(i)) {
162          reOrderToIndices<DateTime>(i, indices);
163        }
164      }
165      preprocessingData.EndTransaction();
166    }
167
168    private void reOrderToIndices<T>(int columnIndex, IList<Tuple<int, int>> indices) {
169
170      List<T> originalData = new List<T>(preprocessingData.GetValues<T>(columnIndex));
171
172      // process all columns equally
173      foreach (Tuple<int, int> index in indices) {
174        int originalIndex = index.Item1;
175        int replaceIndex = index.Item2;
176
177        T replaceValue = originalData.ElementAt<T>(replaceIndex);
178        preprocessingData.SetCell<T>(columnIndex, originalIndex, replaceValue);
179      }
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.