Changeset 10590
- Timestamp:
- 03/12/14 17:42:42 (11 years ago)
- Location:
- branches/DataPreprocessing
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataPreprocessing/HeuristicLab.DataPreprocessing.Views/3.3/DataGridContentView.cs
r10585 r10590 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Drawing;25 24 using System.Linq; 26 25 using System.Windows.Forms; 26 using HeuristicLab.Data.Views; 27 27 using HeuristicLab.MainForm; 28 using System.Drawing; 28 29 29 30 namespace HeuristicLab.DataPreprocessing.Views { … … 31 32 [Content(typeof(IDataGridContent), true)] 32 33 public partial class DataGridContentView : CopyOfStringConvertibleMatrixView { 33 34 private int lastClickedRow;35 private int lastClickedColumn;36 34 37 35 private bool notOwnEvent = true; … … 123 121 contextMenu.Show(MousePosition); 124 122 } else { 123 if (!dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, e.RowIndex])) { 124 dataGridView.ClearSelection(); 125 dataGridView[e.ColumnIndex, e.RowIndex].Selected = true; 126 } 125 127 interpolationToolStripMenuItem.Enabled = !(e.RowIndex == 0 || e.RowIndex == Content.Rows); 126 128 contextMenuCell.Show(MousePosition); 127 lastClickedColumn = e.ColumnIndex;128 lastClickedRow = e.RowIndex;129 129 } 130 130 } … … 175 175 } 176 176 177 private Dictionary<int, List<int>> GetSelectedCells() { 178 var selectedCells = new Dictionary<int, List<int>>(); 179 for (int i = 0; i < dataGridView.SelectedCells.Count; i++) { 180 var columnIndex=dataGridView.SelectedCells[i].ColumnIndex; 181 if(!selectedCells.ContainsKey(columnIndex)){ 182 selectedCells.Add(columnIndex,new List<int>()); 183 } 184 selectedCells[columnIndex].Add(dataGridView.SelectedCells[i].RowIndex); 185 } 186 return selectedCells; 187 } 188 177 189 private void ReplaceWithAverage_Click(object sender, EventArgs e) { 178 Content.PreprocessingDataManipulation.ReplaceIndicesByAverageValue( lastClickedColumn, new List<int>() { lastClickedRow });190 Content.PreprocessingDataManipulation.ReplaceIndicesByAverageValue(GetSelectedCells()); 179 191 } 180 192 181 193 private void ReplaceWithMedian_Click(object sender, EventArgs e) { 182 Content.PreprocessingDataManipulation.ReplaceIndicesByMedianValue( lastClickedColumn, new List<int>() { lastClickedRow });194 Content.PreprocessingDataManipulation.ReplaceIndicesByMedianValue(GetSelectedCells()); 183 195 } 184 196 185 197 private void ReplaceWithRandom_Click(object sender, EventArgs e) { 186 Content.PreprocessingDataManipulation.ReplaceIndicesByRandomValue( lastClickedColumn, new List<int>() { lastClickedRow });198 Content.PreprocessingDataManipulation.ReplaceIndicesByRandomValue(GetSelectedCells()); 187 199 } 188 200 189 201 private void ReplaceWithMostCommon_Click(object sender, EventArgs e) { 190 Content.PreprocessingDataManipulation.ReplaceIndicesByMostCommonValue( lastClickedColumn, new List<int>() { lastClickedRow });202 Content.PreprocessingDataManipulation.ReplaceIndicesByMostCommonValue(GetSelectedCells()); 191 203 } 192 204 193 205 private void ReplaceWithInterpolation_Click(object sender, EventArgs e) { 194 Content.PreprocessingDataManipulation.ReplaceIndicesByLinearInterpolationOfNeighbours( lastClickedColumn, new List<int>() { lastClickedRow });206 Content.PreprocessingDataManipulation.ReplaceIndicesByLinearInterpolationOfNeighbours(GetSelectedCells()); 195 207 } 196 208 } -
branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/ManipulationLogic.cs
r10586 r10590 43 43 } 44 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) { 45 public void ReplaceIndicesByAverageValue(Dictionary<int, List<int>> cells) { 46 preprocessingData.BeginTransaction(); 47 foreach (var column in cells) { 48 double average = statisticInfo.GetAverage(column.Key); 49 ReplaceIndicesByValue<double>(column.Key, column.Value, average); 50 } 51 preprocessingData.EndTransaction(); 52 } 53 54 public void ReplaceIndicesByMedianValue(Dictionary<int, List<int>> cells) { 55 preprocessingData.BeginTransaction(); 56 foreach (var column in cells) { 57 double median = statisticInfo.GetMedian(column.Key); 58 ReplaceIndicesByValue<double>(column.Key, column.Value, median); 59 } 60 preprocessingData.EndTransaction(); 61 } 62 63 public void ReplaceIndicesByRandomValue(Dictionary<int, List<int>> cells) { 64 preprocessingData.BeginTransaction(); 56 65 Random r = new Random(); 57 66 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; 67 foreach (var column in cells) { 68 double max = statisticInfo.GetMax<double>(column.Key); 69 double min = statisticInfo.GetMin<double>(column.Key); 70 double randMultiplier = (max - min); 71 foreach (int index in column.Value) { 72 double rand = r.NextDouble() * randMultiplier + min; 73 preprocessingData.SetCell<double>(column.Key, index, rand); 74 } 75 } 76 preprocessingData.EndTransaction(); 77 } 78 79 public void ReplaceIndicesByLinearInterpolationOfNeighbours(Dictionary<int, List<int>> cells) { 80 preprocessingData.BeginTransaction(); 81 foreach (var column in cells) { 82 int countValues = preprocessingData.GetValues<double>(column.Key).Count(); 83 foreach (int index in column.Value) { 84 // dont replace first or last values 85 if (index > 0 && index < countValues) { 86 int prevIndex = indexOfPrevPresentValue(column.Key, index); 87 int nextIndex = indexOfNextPresentValue(column.Key, index); 88 89 // no neighbours found 90 if (prevIndex < 0 && nextIndex >= countValues) { 91 continue; 92 } 93 double prev = preprocessingData.GetCell<double>(column.Key, prevIndex); 94 double next = preprocessingData.GetCell<double>(column.Key, nextIndex); 95 96 int valuesToInterpolate = nextIndex - prevIndex; 97 98 double interpolationStep = (prev + next) / valuesToInterpolate; 99 100 for (int i = prevIndex; i < nextIndex; ++i) { 101 double interpolated = prev + (interpolationStep * (i - prevIndex)); 102 preprocessingData.SetCell<double>(column.Key, i, interpolated); 103 } 78 104 } 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 } 105 } 106 } 107 preprocessingData.EndTransaction(); 92 108 } 93 109 … … 110 126 } 111 127 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 } 128 public void ReplaceIndicesByMostCommonValue(Dictionary<int, List<int>> cells) { 129 preprocessingData.BeginTransaction(); 130 foreach (var column in cells) { 131 if (preprocessingData.IsType<double>(column.Key)) { 132 ReplaceIndicesByValue<double>(column.Key, column.Value, statisticInfo.GetMostCommonValue<double>(column.Key)); 133 } else if (preprocessingData.IsType<string>(column.Key)) { 134 ReplaceIndicesByValue<string>(column.Key, column.Value, statisticInfo.GetMostCommonValue<string>(column.Key)); 135 } else if (preprocessingData.IsType<DateTime>(column.Key)) { 136 ReplaceIndicesByValue<DateTime>(column.Key, column.Value, statisticInfo.GetMostCommonValue<DateTime>(column.Key)); 137 } else { 138 throw new ArgumentException("column with index: " + column.Key + " contains a non supported type."); 139 } 140 } 141 preprocessingData.EndTransaction(); 122 142 } 123 143 -
branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Interfaces/IManipulationLogic.cs
r10558 r10590 27 27 void ReOrderToIndices(IEnumerable<int> indices); 28 28 void ReOrderToIndices(IList<Tuple<int, int>> indices); 29 void ReplaceIndicesByAverageValue( int columnIndex, IEnumerable<int> rowIndices);30 void ReplaceIndicesByLinearInterpolationOfNeighbours( int columnIndex, IEnumerable<int> rowIndices);31 void ReplaceIndicesByMedianValue( int columnIndex, IEnumerable<int> rowIndices);32 void ReplaceIndicesByMostCommonValue( int columnIndex, IEnumerable<int> rowIndices);33 void ReplaceIndicesByRandomValue( int columnIndex, IEnumerable<int> rowIndices);29 void ReplaceIndicesByAverageValue(Dictionary<int, List<int>> cells); 30 void ReplaceIndicesByLinearInterpolationOfNeighbours(Dictionary<int, List<int>> cells); 31 void ReplaceIndicesByMedianValue(Dictionary<int, List<int>> cells); 32 void ReplaceIndicesByMostCommonValue(Dictionary<int, List<int>> cells); 33 void ReplaceIndicesByRandomValue(Dictionary<int, List<int>> cells); 34 34 void ReplaceIndicesByValue<T>(int columnIndex, IEnumerable<int> rowIndices, T value); 35 35 void ShuffleWithRanges(IEnumerable<HeuristicLab.Data.IntRange> ranges);
Note: See TracChangeset
for help on using the changeset viewer.