Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/PreprocessingDataManipulation.cs @ 10255

Last change on this file since 10255 was 10255, checked in by mleitner, 10 years ago

add reOrderToIndices which takes enumerable

File size: 6.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Data;
5
6namespace HeuristicLab.DataPreprocessing {
7  class PreprocessingDataManipulation : IPreprocessingDataManipulation {
8    private IPreprocessingData preprocessingData;
9    private IStatisticsLogic statisticInfo;
10    private ISearchLogic searchLogic;
11
12    public PreprocessingDataManipulation(IPreprocessingData _prepocessingData, ISearchLogic theSearchLogic, IStatisticsLogic theStatisticsLogic) {
13      preprocessingData = _prepocessingData;
14      searchLogic = theSearchLogic;
15      statisticInfo = theStatisticsLogic;
16    }
17
18    public void ReplaceIndicesByValue<T>(string variableName, IEnumerable<int> indices, T value) {
19      foreach (int index in indices) {
20        preprocessingData.SetCell<T>(variableName, index, value);
21      }
22    }
23
24    public void ReplaceIndicesByAverageValue(string variableName, IEnumerable<int> indices) {
25      double average = statisticInfo.GetAverage(variableName);
26      ReplaceIndicesByValue<double>(variableName, indices, average);
27    }
28
29    public void ReplaceIndicesByMedianValue(string variableName, IEnumerable<int> indices) {
30      double median = statisticInfo.GetMedian(variableName);
31      ReplaceIndicesByValue<double>(variableName, indices, median);
32    }
33
34    public void ReplaceIndicesByRandomValue(string variableName, IEnumerable<int> indices) {
35      Random r = new Random();
36
37      double max = statisticInfo.GetMax<double>(variableName);
38      double min = statisticInfo.GetMin<double>(variableName);
39      double randMultiplier = (max - min);
40      foreach (int index in indices) {
41        double rand = r.NextDouble() * randMultiplier + min;
42        preprocessingData.SetCell<double>(variableName, index, rand);
43      }
44    }
45
46    public void ReplaceIndicesByLinearInterpolationOfNeighbours(string variableName, IEnumerable<int> indices) {
47      int countValues = preprocessingData.GetValues<double>(variableName).Count();
48      foreach (int index in indices) {
49        // dont replace first or last values
50        if (index > 0 && index < countValues) {
51          int prevIndex = indexOfPrevPresentValue(variableName, index);
52          int nextIndex = indexOfNextPresentValue(variableName, index);
53
54          // no neighbours found
55          if (prevIndex < 0 && nextIndex >= countValues) {
56            continue;
57          }
58          double prev = preprocessingData.GetCell<double>(variableName, prevIndex);
59          double next = preprocessingData.GetCell<double>(variableName, 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>(variableName, i, interpolated);
68          }
69        }
70      }
71    }
72
73    private int indexOfPrevPresentValue(string variableName, int start) {
74      int offset = start - 1;
75      while (offset >= 0 && searchLogic.IsMissingValue(variableName, offset)) {
76        offset--;
77      }
78
79      return offset;
80    }
81
82    private int indexOfNextPresentValue(string variableName, int start) {
83      int offset = start + 1;
84      while (offset < preprocessingData.Rows && searchLogic.IsMissingValue(variableName, offset)) {
85        offset++;
86      }
87
88      return offset;
89    }
90
91    public void ReplaceIndicesByMostCommonValue(string variableName, IEnumerable<int> indices) {
92      if (preprocessingData.IsType<double>(variableName)) {
93        ReplaceIndicesByValue<double>(variableName, indices, statisticInfo.GetMostCommonValue<double>(variableName));
94      } else if (preprocessingData.IsType<string>(variableName)) {
95        ReplaceIndicesByValue<string>(variableName, indices, statisticInfo.GetMostCommonValue<string>(variableName));
96      } else if (preprocessingData.IsType<DateTime>(variableName)) {
97        ReplaceIndicesByValue<DateTime>(variableName, indices, statisticInfo.GetMostCommonValue<DateTime>(variableName));
98      } else {
99        throw new ArgumentException("column with index: " + variableName + " 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    {
123        List<Tuple<int, int>> indicesTuple = new List<Tuple<int, int>>();
124 
125        for (int i = 0; i < indices.Count(); ++i) {
126            new Tuple<int, int>(i, indices.ElementAt(i));
127        }
128
129        reOrdertoIndices(indicesTuple);
130    }
131
132    public void reOrdertoIndices(IList<Tuple<int, int>> indices) {
133      foreach (string variableName in preprocessingData.VariableNames) {
134        if (preprocessingData.IsType<double>(variableName)) {
135          reOrderToIndices<double>(variableName, indices);
136        } else if (preprocessingData.IsType<string>(variableName)) {
137          reOrderToIndices<string>(variableName, indices);
138        } else if (preprocessingData.IsType<DateTime>(variableName)) {
139          reOrderToIndices<DateTime>(variableName, indices);
140        }
141      }
142    }
143
144    private void reOrderToIndices<T>(string variableName, IList<Tuple<int, int>> indices) {
145      // process all columns equally
146      foreach (Tuple<int, int> index in indices) {
147        int originalIndex = index.Item1;
148        int replaceIndex = index.Item2;
149
150        T tmp = preprocessingData.GetCell<T>(variableName, originalIndex);
151        T replaceValue = preprocessingData.GetCell<T>(variableName, replaceIndex);
152
153        preprocessingData.SetCell<T>(variableName, originalIndex, replaceValue);
154        preprocessingData.SetCell<T>(variableName, replaceIndex, tmp);
155      }
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.