Free cookie consent management tool by TermsFeed Policy Generator

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

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