- Timestamp:
- 07/19/17 11:24:43 (7 years ago)
- Location:
- branches/DataPreprocessing Cleanup
- Files:
-
- 2 deleted
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing.Views/3.4/DataGridContentView.cs
r15110 r15270 167 167 168 168 ReplaceTransaction(() => { 169 Content.Pre ProcessingData.InTransaction(() => {169 Content.PreprocessingData.InTransaction(() => { 170 170 for (int row = containsHeader ? 1 : 0; row < values.GetLength(1); row++) { 171 171 for (int col = 0; col < values.GetLength(0); col++) { … … 177 177 if (string.IsNullOrWhiteSpace(firstRow[i])) 178 178 firstRow[i] = string.Format("<{0}>", i); 179 Content.Pre ProcessingData.RenameColumns(firstRow);179 Content.PreprocessingData.RenameColumns(firstRow); 180 180 } 181 181 }); … … 224 224 if (e.Button == MouseButtons.Middle) { 225 225 int newIndex = e.ColumnIndex >= 0 ? e.ColumnIndex : 0; 226 Content.Pre ProcessingData.InsertColumn<double>(newIndex.ToString(), newIndex);226 Content.PreprocessingData.InsertColumn<double>(newIndex.ToString(), newIndex); 227 227 } else if (e.Button == MouseButtons.Right && Content.SortableView) { 228 228 SortColumn(e.ColumnIndex); … … 235 235 if (e.Button == MouseButtons.Middle) { 236 236 int newIndex = e.RowIndex >= 0 ? e.RowIndex : 0; 237 Content.Pre ProcessingData.InsertRow(newIndex);237 Content.PreprocessingData.InsertRow(newIndex); 238 238 } 239 239 } … … 531 531 medianToolStripMenuItem_Selection.Enabled = 532 532 randomToolStripMenuItem_Column.Enabled = 533 randomToolStripMenuItem_Selection.Enabled = !Content.Pre ProcessingData.AreAllStringColumns(columnIndices);533 randomToolStripMenuItem_Selection.Enabled = !Content.PreprocessingData.AreAllStringColumns(columnIndices); 534 534 535 535 smoothingToolStripMenuItem_Column.Enabled = 536 536 interpolationToolStripMenuItem_Column.Enabled = !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, 0]) 537 537 && !dataGridView.SelectedCells.Contains(dataGridView[e.ColumnIndex, Content.Rows - 1]) 538 && !Content.Pre ProcessingData.AreAllStringColumns(columnIndices);538 && !Content.PreprocessingData.AreAllStringColumns(columnIndices); 539 539 540 540 replaceValueOverColumnToolStripMenuItem.Visible = true; … … 675 675 676 676 private void addRowButton_Click(object sender, EventArgs e) { 677 Content.Pre ProcessingData.InsertRow(Content.Rows);677 Content.PreprocessingData.InsertRow(Content.Rows); 678 678 } 679 679 680 680 private void addColumnButton_Click(object sender, EventArgs e) { 681 Content.Pre ProcessingData.InsertColumn<double>(Content.Columns.ToString(), Content.Columns);681 Content.PreprocessingData.InsertColumn<double>(Content.Columns.ToString(), Content.Columns); 682 682 } 683 683 … … 686 686 687 687 if (renameDialog.ShowDialog(this) == DialogResult.OK) { 688 Content.Pre ProcessingData.RenameColumns(renameDialog.ColumnNames);688 Content.PreprocessingData.RenameColumns(renameDialog.ColumnNames); 689 689 } 690 690 } … … 693 693 foreach (DataGridViewColumn column in DataGridView.Columns) { 694 694 var variable = column.HeaderText; 695 bool isInputTarget = Content.Pre ProcessingData.InputVariables.Contains(variable)696 || Content.Pre ProcessingData.TargetVariable == variable;695 bool isInputTarget = Content.PreprocessingData.InputVariables.Contains(variable) 696 || Content.PreprocessingData.TargetVariable == variable; 697 697 column.Visible = isInputTarget; 698 698 } -
branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Content/CorrelationMatrixContent.cs
r15110 r15270 33 33 34 34 public PreprocessingContext Context { get; private set; } 35 public I TransactionalPreprocessingData PreprocessingData {35 public IPreprocessingData PreprocessingData { 36 36 get { return Context.Data; } 37 37 } -
branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Content/DataCompletenessChartContent.cs
r15269 r15270 32 32 } 33 33 34 public I TransactionalPreprocessingData PreprocessingData { get; private set; }34 public IPreprocessingData PreprocessingData { get; private set; } 35 35 36 public DataCompletenessChartContent(I TransactionalPreprocessingData preprocessingData) {36 public DataCompletenessChartContent(IPreprocessingData preprocessingData) { 37 37 PreprocessingData = preprocessingData; 38 38 } -
branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Content/DataGridContent.cs
r15110 r15270 36 36 } 37 37 38 public I TransactionalPreprocessingData PreProcessingData { get; private set; }38 public IPreprocessingData PreprocessingData { get; private set; } 39 39 40 40 public ManipulationLogic ManipulationLogic { get; private set; } … … 42 42 43 43 public int Rows { 44 get { return Pre ProcessingData.Rows; }44 get { return PreprocessingData.Rows; } 45 45 set { } 46 46 } 47 47 48 48 public int Columns { 49 get { return Pre ProcessingData.Columns; }49 get { return PreprocessingData.Columns; } 50 50 set { } 51 51 } 52 52 53 53 public IEnumerable<string> ColumnNames { 54 get { return Pre ProcessingData.VariableNames; }54 get { return PreprocessingData.VariableNames; } 55 55 set { } 56 56 } … … 71 71 72 72 public IDictionary<int, IList<int>> Selection { 73 get { return Pre ProcessingData.Selection; }74 set { Pre ProcessingData.Selection = value; }73 get { return PreprocessingData.Selection; } 74 set { PreprocessingData.Selection = value; } 75 75 } 76 76 77 public DataGridContent(I TransactionalPreprocessingData preProcessingData, ManipulationLogic theManipulationLogic, FilterLogic theFilterLogic) {77 public DataGridContent(IPreprocessingData preprocessingData, ManipulationLogic theManipulationLogic, FilterLogic theFilterLogic) { 78 78 ManipulationLogic = theManipulationLogic; 79 79 FilterLogic = theFilterLogic; 80 Pre ProcessingData = preProcessingData;80 PreprocessingData = preprocessingData; 81 81 } 82 82 … … 90 90 91 91 public void DeleteRows(IEnumerable<int> rows) { 92 Pre ProcessingData.DeleteRowsWithIndices(rows);92 PreprocessingData.DeleteRowsWithIndices(rows); 93 93 } 94 94 95 95 public void DeleteColumn(int column) { 96 Pre ProcessingData.DeleteColumn(column);96 PreprocessingData.DeleteColumn(column); 97 97 } 98 98 99 99 public bool Validate(string value, out string errorMessage, int columnIndex) { 100 return Pre ProcessingData.Validate(value, out errorMessage, columnIndex);100 return PreprocessingData.Validate(value, out errorMessage, columnIndex); 101 101 } 102 102 103 103 public string GetValue(int rowIndex, int columnIndex) { 104 return Pre ProcessingData.GetCellAsString(columnIndex, rowIndex);104 return PreprocessingData.GetCellAsString(columnIndex, rowIndex); 105 105 } 106 106 107 107 public bool SetValue(string value, int rowIndex, int columnIndex) { 108 return Pre ProcessingData.SetValue(value, columnIndex, rowIndex);108 return PreprocessingData.SetValue(value, columnIndex, rowIndex); 109 109 } 110 110 111 111 public event DataPreprocessingChangedEventHandler Changed { 112 add { Pre ProcessingData.Changed += value; }113 remove { Pre ProcessingData.Changed -= value; }112 add { PreprocessingData.Changed += value; } 113 remove { PreprocessingData.Changed -= value; } 114 114 } 115 115 -
branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Content/StatisticsContent.cs
r15110 r15270 31 31 } 32 32 33 public I TransactionalPreprocessingData PreprocessingData { get; private set; }33 public IPreprocessingData PreprocessingData { get; private set; } 34 34 public StatisticsLogic StatisticsLogic { get; private set; } 35 35 36 public StatisticsContent(I TransactionalPreprocessingData preProcessingData, StatisticsLogic statisticsLogic) {36 public StatisticsContent(IPreprocessingData preProcessingData, StatisticsLogic statisticsLogic) { 37 37 PreprocessingData = preProcessingData; 38 38 StatisticsLogic = statisticsLogic; -
branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Data/FilteredPreprocessingData.cs
r15269 r15270 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 using HeuristicLab.Problems.DataAnalysis; 28 29 29 30 namespace HeuristicLab.DataPreprocessing { 31 [Item("FilteredPreprocessingData", "Represents filtered data used for preprocessing.")] 32 [StorableClass] 30 33 public sealed class FilteredPreprocessingData : NamedItem, IFilteredPreprocessingData { 31 private readonly ITransactionalPreprocessingData originalData; 32 private ITransactionalPreprocessingData filteredData; 33 34 public IntRange TrainingPartition { 35 get { return originalData.TrainingPartition; } 36 } 37 38 public IntRange TestPartition { 39 get { return originalData.TestPartition; } 40 } 41 42 public IList<ITransformation> Transformations { 43 get { return originalData.Transformations; } 44 } 45 46 public IEnumerable<string> VariableNames { 47 get { return ActiveData.VariableNames; } 48 } 49 50 public IList<string> InputVariables { get { return ActiveData.InputVariables; } } 51 public string TargetVariable { get { return ActiveData.TargetVariable; } } // optional 52 53 public IDictionary<int, IList<int>> Selection { 54 get { return originalData.Selection; } 55 set { originalData.Selection = value; } 56 } 57 58 public int Columns { 59 get { return ActiveData.Columns; } 60 } 61 62 public int Rows { 63 get { return ActiveData.Rows; } 64 } 65 66 public ITransactionalPreprocessingData ActiveData { 34 35 [Storable] 36 private readonly IPreprocessingData originalData; 37 [Storable] 38 private IPreprocessingData filteredData; 39 40 public IPreprocessingData ActiveData { 67 41 get { return IsFiltered ? filteredData : originalData; } 68 42 } 69 43 70 public bool IsUndoAvailable { 71 get { return IsFiltered ? false : originalData.IsUndoAvailable; } 72 } 73 74 public bool IsFiltered { 75 get { return filteredData != null; } 76 } 77 78 79 public FilteredPreprocessingData(ITransactionalPreprocessingData preporcessingData) 44 #region Constructor, Cloning & Persistance 45 public FilteredPreprocessingData(IPreprocessingData preporcessingData) 80 46 : base() { 81 47 originalData = preporcessingData; … … 92 58 } 93 59 60 [StorableConstructor] 61 private FilteredPreprocessingData(bool deserializing) 62 : base(deserializing) { } 63 #endregion 64 65 #region Cells 94 66 public bool IsCellEmpty(int columnIndex, int rowIndex) { 95 67 return ActiveData.IsCellEmpty(columnIndex, rowIndex); … … 121 93 } 122 94 95 public bool SetValue(string value, int columnIndex, int rowIndex) { 96 if (IsFiltered) 97 throw new InvalidOperationException("SetValue not possible while data is filtered"); 98 return originalData.SetValue(value, columnIndex, rowIndex); 99 } 100 101 public int Columns { 102 get { return ActiveData.Columns; } 103 } 104 105 public int Rows { 106 get { return ActiveData.Rows; } 107 } 108 #endregion 109 110 #region Rows 123 111 public void InsertRow(int rowIndex) { 124 112 if (IsFiltered) … … 133 121 134 122 originalData.DeleteRow(rowIndex); 123 } 124 125 public void DeleteRowsWithIndices(IEnumerable<int> rows) { 126 if (IsFiltered) 127 throw new InvalidOperationException("DeleteRowsWithIndices not possible while data is filtered"); 128 129 originalData.DeleteRowsWithIndices(rows); 135 130 } 136 131 … … 160 155 } 161 156 157 public bool AreAllStringColumns(IEnumerable<int> columnIndices) { 158 return originalData.AreAllStringColumns(columnIndices); 159 } 160 #endregion 161 162 #region Variables 163 public IEnumerable<string> VariableNames { 164 get { return ActiveData.VariableNames; } 165 } 166 public IEnumerable<string> GetDoubleVariableNames() { 167 return originalData.GetDoubleVariableNames(); 168 } 162 169 public string GetVariableName(int columnIndex) { 163 170 return ActiveData.GetVariableName(columnIndex); … … 172 179 } 173 180 181 public IList<string> InputVariables { 182 get { return ActiveData.InputVariables; } 183 } 184 185 public string TargetVariable { 186 get { return ActiveData.TargetVariable; } 187 } // optional 188 #endregion 189 190 #region Partitions 191 public IntRange TrainingPartition { 192 get { return originalData.TrainingPartition; } 193 } 194 195 public IntRange TestPartition { 196 get { return originalData.TestPartition; } 197 } 198 #endregion 199 200 #region Transformations 201 public IList<ITransformation> Transformations { 202 get { return originalData.Transformations; } 203 } 204 #endregion 205 206 #region Validation 207 public bool Validate(string value, out string errorMessage, int columnIndex) { 208 return originalData.Validate(value, out errorMessage, columnIndex); 209 } 210 #endregion 211 212 #region Import & Export 213 public void Import(IDataAnalysisProblemData problemData) { 214 if (IsFiltered) 215 throw new InvalidOperationException("Import not possible while data is filtered"); 216 originalData.Import(problemData); 217 } 218 174 219 public Dataset ExportToDataset() { 175 220 return originalData.ExportToDataset(); 176 221 } 177 222 #endregion 223 224 #region Selection 225 public IDictionary<int, IList<int>> Selection { 226 get { return originalData.Selection; } 227 set { originalData.Selection = value; } 228 } 229 230 public void ClearSelection() { 231 originalData.ClearSelection(); 232 } 233 234 public event EventHandler SelectionChanged { 235 add { originalData.SelectionChanged += value; } 236 remove { originalData.SelectionChanged -= value; } 237 } 238 #endregion 239 240 #region Transactions 241 public event DataPreprocessingChangedEventHandler Changed { 242 add { originalData.Changed += value; } 243 remove { originalData.Changed -= value; } 244 } 245 246 public bool IsUndoAvailable { 247 get { return IsFiltered ? false : originalData.IsUndoAvailable; } 248 } 249 250 public void Undo() { 251 if (IsFiltered) 252 throw new InvalidOperationException("Undo not possible while data is filtered"); 253 254 originalData.Undo(); 255 } 256 257 public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) { 258 if (IsFiltered) 259 throw new InvalidOperationException("Transaction not possible while data is filtered"); 260 originalData.InTransaction(action, type); 261 } 262 263 public void BeginTransaction(DataPreprocessingChangedEventType type) { 264 if (IsFiltered) 265 throw new InvalidOperationException("Transaction not possible while data is filtered"); 266 originalData.BeginTransaction(type); 267 } 268 269 public void EndTransaction() { 270 originalData.EndTransaction(); 271 } 272 #endregion 273 274 #region Filters 178 275 public void SetFilter(bool[] rowFilters) { 179 filteredData = (I TransactionalPreprocessingData)originalData.Clone();276 filteredData = (IPreprocessingData)originalData.Clone(); 180 277 filteredData.InTransaction(() => { 181 278 for (int row = (rowFilters.Length - 1); row >= 0; --row) { … … 210 307 } 211 308 309 public bool IsFiltered { 310 get { return filteredData != null; } 311 } 312 313 public event EventHandler FilterChanged; 314 212 315 private void OnFilterChanged() { 213 316 if (FilterChanged != null) { … … 215 318 } 216 319 } 217 218 public event DataPreprocessingChangedEventHandler Changed {219 add { originalData.Changed += value; }220 remove { originalData.Changed -= value; }221 }222 223 public bool SetValue(string value, int columnIndex, int rowIndex) {224 if (IsFiltered)225 throw new InvalidOperationException("SetValue not possible while data is filtered");226 return originalData.SetValue(value, columnIndex, rowIndex);227 }228 229 public bool AreAllStringColumns(IEnumerable<int> columnIndices) {230 return originalData.AreAllStringColumns(columnIndices);231 }232 233 public void DeleteRowsWithIndices(IEnumerable<int> rows) {234 if (IsFiltered)235 throw new InvalidOperationException("DeleteRowsWithIndices not possible while data is filtered");236 237 originalData.DeleteRowsWithIndices(rows);238 }239 240 public void Undo() {241 if (IsFiltered)242 throw new InvalidOperationException("Undo not possible while data is filtered");243 244 originalData.Undo();245 }246 247 public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {248 if (IsFiltered)249 throw new InvalidOperationException("Transaction not possible while data is filtered");250 originalData.InTransaction(action, type);251 }252 253 public void BeginTransaction(DataPreprocessingChangedEventType type) {254 if (IsFiltered)255 throw new InvalidOperationException("Transaction not possible while data is filtered");256 originalData.BeginTransaction(type);257 }258 259 public void EndTransaction() {260 originalData.EndTransaction();261 }262 263 public IEnumerable<string> GetDoubleVariableNames() {264 return originalData.GetDoubleVariableNames();265 }266 267 public void ClearSelection() {268 originalData.ClearSelection();269 }270 271 public event EventHandler SelectionChanged {272 add { originalData.SelectionChanged += value; }273 remove { originalData.SelectionChanged -= value; }274 }275 276 #region IPreprocessingData Members277 public bool Validate(string value, out string errorMessage, int columnIndex) {278 return originalData.Validate(value, out errorMessage, columnIndex);279 }280 281 public event EventHandler FilterChanged;282 320 #endregion 283 321 } -
branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Data/IFilteredPreprocessingData.cs
r14185 r15270 23 23 24 24 namespace HeuristicLab.DataPreprocessing { 25 public interface IFilteredPreprocessingData : ITransactionalPreprocessingData { 25 public interface IFilteredPreprocessingData : IPreprocessingData { 26 #region Filters 26 27 void SetFilter(bool[] rowFilters); 27 28 void PersistFilter(); … … 30 31 31 32 event EventHandler FilterChanged; 33 #endregion 32 34 } 33 35 } -
branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Data/IPreprocessingData.cs
r15269 r15270 28 28 namespace HeuristicLab.DataPreprocessing { 29 29 public interface IPreprocessingData : INamedItem { 30 #region Cells 30 31 bool IsCellEmpty(int columnIndex, int rowIndex); 31 32 T GetCell<T>(int columnIndex, int rowIndex); … … 40 41 bool SetValue(string value, int columnIndex, int rowIndex); 41 42 43 int Columns { get; } 44 int Rows { get; } 45 #endregion 46 47 #region Rows 42 48 void InsertRow(int rowIndex); 43 49 void DeleteRow(int rowIndex); … … 51 57 52 58 bool AreAllStringColumns(IEnumerable<int> columnIndices); 53 bool Validate(string value, out string errorMessage, int columnIndex);59 #endregion 54 60 55 IntRange TrainingPartition { get; } 56 IntRange TestPartition { get; } 57 58 IList<ITransformation> Transformations { get; } 59 61 #region Variables 60 62 IEnumerable<string> VariableNames { get; } 61 63 IEnumerable<string> GetDoubleVariableNames(); … … 67 69 IList<string> InputVariables { get; } 68 70 string TargetVariable { get; } // optional 71 #endregion 69 72 70 int Columns { get; } 71 int Rows { get; } 73 #region Partitions 74 IntRange TrainingPartition { get; } 75 IntRange TestPartition { get; } 76 #endregion 72 77 78 #region Transformations 79 IList<ITransformation> Transformations { get; } 80 #endregion 81 82 #region Validation 83 bool Validate(string value, out string errorMessage, int columnIndex); 84 #endregion 85 86 #region Import & Export 87 void Import(IDataAnalysisProblemData problemData); 73 88 Dataset ExportToDataset(); 89 #endregion 74 90 91 #region Selection 75 92 IDictionary<int, IList<int>> Selection { get; set; } 76 93 void ClearSelection(); 77 94 78 95 event EventHandler SelectionChanged; 96 #endregion 97 98 #region Transactions 99 event DataPreprocessingChangedEventHandler Changed; 100 101 bool IsUndoAvailable { get; } 102 void Undo(); 103 void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any); 104 void BeginTransaction(DataPreprocessingChangedEventType type); 105 void EndTransaction(); 106 #endregion 79 107 } 80 108 } -
branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Data/PreprocessingData.cs
r15269 r15270 23 23 using System.Collections; 24 24 using System.Collections.Generic; 25 using System.Globalization; 25 26 using System.Linq; 26 27 using HeuristicLab.Common; 27 28 using HeuristicLab.Core; 28 29 using HeuristicLab.Data; 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 31 using HeuristicLab.Problems.DataAnalysis; 30 32 … … 32 34 33 35 [Item("PreprocessingData", "Represents data used for preprocessing.")] 34 public abstract class PreprocessingData : NamedItem, IPreprocessingData { 35 public IntRange TrainingPartition { get; set; } 36 public IntRange TestPartition { get; set; } 37 38 public IList<ITransformation> Transformations { get; protected set; } 39 36 [StorableClass] 37 public class PreprocessingData : NamedItem, IPreprocessingData { 38 39 [Storable] 40 40 protected IList<IList> variableValues; 41 [Storable] 41 42 protected IList<string> variableNames; 42 43 43 public IEnumerable<string> VariableNames { 44 get { return variableNames; } 45 } 46 47 public IEnumerable<string> GetDoubleVariableNames() { 48 var doubleVariableNames = new List<string>(); 49 for (int i = 0; i < Columns; ++i) { 50 if (VariableHasType<double>(i)) { 51 doubleVariableNames.Add(variableNames[i]); 52 } 53 } 54 return doubleVariableNames; 55 } 56 57 public IList<string> InputVariables { get; private set; } 58 public string TargetVariable { get; private set; } // optional 59 60 public int Columns { 61 get { return variableNames.Count; } 62 } 63 64 public int Rows { 65 get { return variableValues.Count > 0 ? variableValues[0].Count : 0; } 66 } 67 68 protected IDictionary<int, IList<int>> selection; 69 public IDictionary<int, IList<int>> Selection { 70 get { return selection; } 71 set { 72 selection = value; 73 OnSelectionChanged(); 74 } 44 #region Constructor, Cloning & Persistance 45 public PreprocessingData(IDataAnalysisProblemData problemData) 46 : base() { 47 Name = "Preprocessing Data"; 48 49 Transformations = new List<ITransformation>(); 50 selection = new Dictionary<int, IList<int>>(); 51 52 Import(problemData); 53 54 RegisterEventHandler(); 75 55 } 76 56 … … 88 68 RegisterEventHandler(); 89 69 } 90 91 protected PreprocessingData(IDataAnalysisProblemData problemData) 92 : base() { 93 Name = "Preprocessing Data"; 94 95 Transformations = new List<ITransformation>(); 96 selection = new Dictionary<int, IList<int>>(); 97 98 Import(problemData); 99 70 public override IDeepCloneable Clone(Cloner cloner) { 71 return new PreprocessingData(this, cloner); 72 } 73 74 [StorableConstructor] 75 protected PreprocessingData(bool deserializing) 76 : base(deserializing) { } 77 [StorableHook(HookType.AfterDeserialization)] 78 private void AfterDeserialization() { 100 79 RegisterEventHandler(); 101 80 } 102 81 82 private void RegisterEventHandler() { 83 Changed += (s, e) => { 84 switch (e.Type) { 85 case DataPreprocessingChangedEventType.DeleteRow: 86 case DataPreprocessingChangedEventType.Any: 87 case DataPreprocessingChangedEventType.Transformation: 88 int maxRowIndex = Math.Max(0, Rows); 89 TrainingPartition.Start = Math.Min(TrainingPartition.Start, maxRowIndex); 90 TrainingPartition.End = Math.Min(TrainingPartition.End, maxRowIndex); 91 TestPartition.Start = Math.Min(TestPartition.Start, maxRowIndex); 92 TestPartition.End = Math.Min(TestPartition.End, maxRowIndex); 93 break; 94 } 95 }; 96 } 97 #endregion 98 99 #region Cells 100 public bool IsCellEmpty(int columnIndex, int rowIndex) { 101 var value = variableValues[columnIndex][rowIndex]; 102 return IsMissingValue(value); 103 } 104 105 public T GetCell<T>(int columnIndex, int rowIndex) { 106 return (T)variableValues[columnIndex][rowIndex]; 107 } 108 109 public void SetCell<T>(int columnIndex, int rowIndex, T value) { 110 SaveSnapshot(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex); 111 112 for (int i = Rows; i <= rowIndex; i++) 113 InsertRow(i); 114 for (int i = Columns; i <= columnIndex; i++) 115 InsertColumn<T>(i.ToString(), i); 116 117 variableValues[columnIndex][rowIndex] = value; 118 if (!IsInTransaction) 119 OnChanged(DataPreprocessingChangedEventType.ChangeItem, columnIndex, rowIndex); 120 } 121 122 public string GetCellAsString(int columnIndex, int rowIndex) { 123 return variableValues[columnIndex][rowIndex].ToString(); 124 } 125 126 public IList<T> GetValues<T>(int columnIndex, bool considerSelection) { 127 if (considerSelection) { 128 var list = new List<T>(); 129 foreach (var rowIdx in selection[columnIndex]) { 130 list.Add((T)variableValues[columnIndex][rowIdx]); 131 } 132 return list; 133 } else { 134 return (IList<T>)variableValues[columnIndex]; 135 } 136 } 137 138 public void SetValues<T>(int columnIndex, IList<T> values) { 139 SaveSnapshot(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1); 140 if (VariableHasType<T>(columnIndex)) { 141 variableValues[columnIndex] = (IList)values; 142 } else { 143 throw new ArgumentException("The datatype of column " + columnIndex + " must be of type " + variableValues[columnIndex].GetType().Name + " but was " + typeof(T).Name); 144 } 145 if (!IsInTransaction) 146 OnChanged(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1); 147 } 148 149 public bool SetValue(string value, int columnIndex, int rowIndex) { 150 bool valid = false; 151 if (VariableHasType<double>(columnIndex)) { 152 double val; 153 valid = double.TryParse(value, out val); 154 if (valid) 155 SetCell(columnIndex, rowIndex, val); 156 } else if (VariableHasType<string>(columnIndex)) { 157 valid = value != null; 158 if (valid) 159 SetCell(columnIndex, rowIndex, value); 160 } else if (VariableHasType<DateTime>(columnIndex)) { 161 DateTime date; 162 valid = DateTime.TryParse(value, out date); 163 if (valid) 164 SetCell(columnIndex, rowIndex, date); 165 } else { 166 throw new ArgumentException("column " + columnIndex + " contains a non supported type."); 167 } 168 169 if (!IsInTransaction) 170 OnChanged(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1); 171 172 return valid; 173 } 174 175 public int Columns { 176 get { return variableNames.Count; } 177 } 178 179 public int Rows { 180 get { return variableValues.Count > 0 ? variableValues[0].Count : 0; } 181 } 182 183 public static bool IsMissingValue(object value) { 184 if (value is double) return double.IsNaN((double)value); 185 if (value is string) return string.IsNullOrEmpty((string)value); 186 if (value is DateTime) return ((DateTime)value).Equals(DateTime.MinValue); 187 throw new ArgumentException(); 188 } 189 #endregion 190 191 #region Rows 192 public void InsertRow(int rowIndex) { 193 SaveSnapshot(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex); 194 foreach (IList column in variableValues) { 195 Type type = column.GetType().GetGenericArguments()[0]; 196 column.Insert(rowIndex, type.IsValueType ? Activator.CreateInstance(type) : null); 197 } 198 if (TrainingPartition.Start <= rowIndex && rowIndex <= TrainingPartition.End) { 199 TrainingPartition.End++; 200 if (TrainingPartition.End <= TestPartition.Start) { 201 TestPartition.Start++; 202 TestPartition.End++; 203 } 204 } else if (TestPartition.Start <= rowIndex && rowIndex <= TestPartition.End) { 205 TestPartition.End++; 206 if (TestPartition.End <= TrainingPartition.Start) { 207 TestPartition.Start++; 208 TestPartition.End++; 209 } 210 } 211 if (!IsInTransaction) 212 OnChanged(DataPreprocessingChangedEventType.AddRow, -1, rowIndex); 213 } 214 public void DeleteRow(int rowIndex) { 215 SaveSnapshot(DataPreprocessingChangedEventType.AddRow, -1, rowIndex); 216 foreach (IList column in variableValues) { 217 column.RemoveAt(rowIndex); 218 } 219 if (TrainingPartition.Start <= rowIndex && rowIndex <= TrainingPartition.End) { 220 TrainingPartition.End--; 221 if (TrainingPartition.End <= TestPartition.Start) { 222 TestPartition.Start--; 223 TestPartition.End--; 224 } 225 } else if (TestPartition.Start <= rowIndex && rowIndex <= TestPartition.End) { 226 TestPartition.End--; 227 if (TestPartition.End <= TrainingPartition.Start) { 228 TestPartition.Start--; 229 TestPartition.End--; 230 } 231 } 232 if (!IsInTransaction) 233 OnChanged(DataPreprocessingChangedEventType.DeleteRow, -1, rowIndex); 234 } 235 public void DeleteRowsWithIndices(IEnumerable<int> rows) { 236 SaveSnapshot(DataPreprocessingChangedEventType.AddRow, -1, -1); 237 foreach (int rowIndex in rows.OrderByDescending(x => x)) { 238 foreach (IList column in variableValues) { 239 column.RemoveAt(rowIndex); 240 } 241 if (TrainingPartition.Start <= rowIndex && rowIndex <= TrainingPartition.End) { 242 TrainingPartition.End--; 243 if (TrainingPartition.End <= TestPartition.Start) { 244 TestPartition.Start--; 245 TestPartition.End--; 246 } 247 } else if (TestPartition.Start <= rowIndex && rowIndex <= TestPartition.End) { 248 TestPartition.End--; 249 if (TestPartition.End <= TrainingPartition.Start) { 250 TestPartition.Start--; 251 TestPartition.End--; 252 } 253 } 254 } 255 if (!IsInTransaction) 256 OnChanged(DataPreprocessingChangedEventType.DeleteRow, -1, -1); 257 } 258 259 public void InsertColumn<T>(string variableName, int columnIndex) { 260 SaveSnapshot(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1); 261 variableValues.Insert(columnIndex, new List<T>(Enumerable.Repeat(default(T), Rows))); 262 variableNames.Insert(columnIndex, variableName); 263 if (!IsInTransaction) 264 OnChanged(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1); 265 } 266 267 public void DeleteColumn(int columnIndex) { 268 SaveSnapshot(DataPreprocessingChangedEventType.AddColumn, columnIndex, -1); 269 variableValues.RemoveAt(columnIndex); 270 variableNames.RemoveAt(columnIndex); 271 if (!IsInTransaction) 272 OnChanged(DataPreprocessingChangedEventType.DeleteColumn, columnIndex, -1); 273 } 274 275 public void RenameColumn(int columnIndex, string name) { 276 SaveSnapshot(DataPreprocessingChangedEventType.ChangeColumn, columnIndex, -1); 277 if (columnIndex < 0 || columnIndex > variableNames.Count) 278 throw new ArgumentOutOfRangeException("columnIndex"); 279 variableNames[columnIndex] = name; 280 281 if (!IsInTransaction) 282 OnChanged(DataPreprocessingChangedEventType.ChangeColumn, -1, -1); 283 } 284 285 public void RenameColumns(IList<string> names) { 286 if (names == null) throw new ArgumentNullException("names"); 287 if (names.Count != variableNames.Count) throw new ArgumentException("number of names must match the number of columns.", "names"); 288 289 SaveSnapshot(DataPreprocessingChangedEventType.ChangeColumn, -1, -1); 290 for (int i = 0; i < names.Count; i++) 291 variableNames[i] = names[i]; 292 293 if (!IsInTransaction) 294 OnChanged(DataPreprocessingChangedEventType.ChangeColumn, -1, -1); 295 } 296 297 public bool AreAllStringColumns(IEnumerable<int> columnIndices) { 298 return columnIndices.All(x => VariableHasType<string>(x)); 299 } 300 #endregion 301 302 #region Variables 303 public IEnumerable<string> VariableNames { 304 get { return variableNames; } 305 } 306 307 public IEnumerable<string> GetDoubleVariableNames() { 308 var doubleVariableNames = new List<string>(); 309 for (int i = 0; i < Columns; ++i) { 310 if (VariableHasType<double>(i)) { 311 doubleVariableNames.Add(variableNames[i]); 312 } 313 } 314 return doubleVariableNames; 315 } 316 317 public string GetVariableName(int columnIndex) { 318 return variableNames[columnIndex]; 319 } 320 321 public int GetColumnIndex(string variableName) { 322 return variableNames.IndexOf(variableName); 323 } 324 325 public bool VariableHasType<T>(int columnIndex) { 326 return columnIndex >= variableValues.Count || variableValues[columnIndex] is List<T>; 327 } 328 329 public IList<string> InputVariables { get; private set; } 330 public string TargetVariable { get; private set; } // optional 331 #endregion 332 333 #region Partitions 334 [Storable] 335 public IntRange TrainingPartition { get; set; } 336 [Storable] 337 public IntRange TestPartition { get; set; } 338 #endregion 339 340 #region Transformations 341 [Storable] 342 public IList<ITransformation> Transformations { get; protected set; } 343 #endregion 344 345 #region Validation 346 public bool Validate(string value, out string errorMessage, int columnIndex) { 347 if (columnIndex < 0 || columnIndex > VariableNames.Count()) { 348 throw new ArgumentOutOfRangeException("column index is out of range"); 349 } 350 351 bool valid = false; 352 errorMessage = string.Empty; 353 if (VariableHasType<double>(columnIndex)) { 354 double val; 355 valid = double.TryParse(value, out val); 356 if (!valid) { 357 errorMessage = "Invalid Value (Valid Value Format: \"" + FormatPatterns.GetDoubleFormatPattern() + "\")"; 358 } 359 } else if (VariableHasType<string>(columnIndex)) { 360 valid = value != null; 361 if (!valid) { 362 errorMessage = "Invalid Value (string must not be null)"; 363 } 364 } else if (VariableHasType<DateTime>(columnIndex)) { 365 DateTime date; 366 valid = DateTime.TryParse(value, out date); 367 if (!valid) { 368 errorMessage = "Invalid Value (Valid Value Format: \"" + CultureInfo.CurrentCulture.DateTimeFormat + "\""; 369 } 370 } else { 371 throw new ArgumentException("column " + columnIndex + " contains a non supported type."); 372 } 373 374 return valid; 375 } 376 #endregion 377 378 #region Import & Export 103 379 public void Import(IDataAnalysisProblemData problemData) { 104 380 Dataset dataset = (Dataset)problemData.Dataset; … … 107 383 TargetVariable = (problemData is IRegressionProblemData) ? ((IRegressionProblemData)problemData).TargetVariable 108 384 : (problemData is IClassificationProblemData) ? ((IClassificationProblemData)problemData).TargetVariable 109 : null;385 : null; 110 386 111 387 int columnIndex = 0; … … 128 404 } 129 405 130 private void RegisterEventHandler() { 131 Changed += (s, e) => { 132 switch (e.Type) { 133 case DataPreprocessingChangedEventType.DeleteRow: 134 CheckPartitionRanges(); 135 break; 136 case DataPreprocessingChangedEventType.Any: 137 CheckPartitionRanges(); 138 break; 139 case DataPreprocessingChangedEventType.Transformation: 140 CheckPartitionRanges(); 141 break; 142 } 143 }; 144 } 145 146 private void CheckPartitionRanges() { 147 int maxRowIndex = Math.Max(0, Rows); 148 TrainingPartition.Start = Math.Min(TrainingPartition.Start, maxRowIndex); 149 TrainingPartition.End = Math.Min(TrainingPartition.End, maxRowIndex); 150 TestPartition.Start = Math.Min(TestPartition.Start, maxRowIndex); 151 TestPartition.End = Math.Min(TestPartition.End, maxRowIndex); 152 } 153 154 protected IList<IList> CopyVariableValues(IList<IList> original) { 155 var copy = new List<IList>(original); 156 for (int i = 0; i < original.Count; ++i) { 157 copy[i] = (IList)Activator.CreateInstance(original[i].GetType(), original[i]); 158 } 159 return copy; 160 } 161 162 public static bool IsMissingValue(object value) { 163 if (value is double) return double.IsNaN((double)value); 164 if (value is string) return string.IsNullOrEmpty((string)value); 165 if (value is DateTime) return ((DateTime)value).Equals(DateTime.MinValue); 166 throw new ArgumentException(); 167 } 168 169 #region IPreprocessingData Members 170 public abstract bool IsCellEmpty(int columnIndex, int rowIndex); 171 public abstract T GetCell<T>(int columnIndex, int rowIndex); 172 173 public abstract void SetCell<T>(int columnIndex, int rowIndex, T value); 174 175 public abstract string GetCellAsString(int columnIndex, int rowIndex); 176 177 public abstract string GetVariableName(int columnIndex); 178 179 public abstract int GetColumnIndex(string variableName); 180 181 public abstract bool VariableHasType<T>(int columnIndex); 182 183 public abstract IList<T> GetValues<T>(int columnIndex, bool considerSelection); 184 185 public abstract void SetValues<T>(int columnIndex, IList<T> values); 186 187 public abstract bool SetValue(string value, int columnIndex, int rowIndex); 188 189 public abstract bool Validate(string value, out string errorMessage, int columnIndex); 190 191 public abstract bool AreAllStringColumns(IEnumerable<int> columnIndices); 192 193 public abstract void DeleteRowsWithIndices(IEnumerable<int> rows); 194 195 public abstract void InsertRow(int rowIndex); 196 197 public abstract void DeleteRow(int rowIndex); 198 199 public abstract void InsertColumn<T>(string variableName, int columnIndex); 200 201 public abstract void DeleteColumn(int columnIndex); 202 203 public abstract void RenameColumn(int columnIndex, string name); 204 public abstract void RenameColumns(IList<string> list); 205 206 public abstract Dataset ExportToDataset(); 207 208 public abstract void ClearSelection(); 209 210 public abstract event EventHandler SelectionChanged; 211 protected abstract void OnSelectionChanged(); 406 public Dataset ExportToDataset() { 407 IList<IList> values = new List<IList>(); 408 409 for (int i = 0; i < Columns; ++i) { 410 values.Add(variableValues[i]); 411 } 412 413 var dataset = new Dataset(variableNames, values); 414 return dataset; 415 } 416 #endregion 417 418 #region Selection 419 [Storable] 420 protected IDictionary<int, IList<int>> selection; 421 public IDictionary<int, IList<int>> Selection { 422 get { return selection; } 423 set { 424 selection = value; 425 OnSelectionChanged(); 426 } 427 } 428 public void ClearSelection() { 429 Selection = new Dictionary<int, IList<int>>(); 430 } 431 432 public event EventHandler SelectionChanged; 433 protected void OnSelectionChanged() { 434 var listeners = SelectionChanged; 435 if (listeners != null) listeners(this, EventArgs.Empty); 436 } 437 #endregion 438 439 #region Transactions 440 // Stapshot/History are nost storable/cloneable on purpose 441 private class Snapshot { 442 public IList<IList> VariableValues { get; set; } 443 public IList<string> VariableNames { get; set; } 444 445 public IntRange TrainingPartition { get; set; } 446 public IntRange TestPartition { get; set; } 447 public IList<ITransformation> Transformations { get; set; } 448 public DataPreprocessingChangedEventType ChangedType { get; set; } 449 450 public int ChangedColumn { get; set; } 451 public int ChangedRow { get; set; } 452 } 212 453 213 454 public event DataPreprocessingChangedEventHandler Changed; … … 216 457 if (listeners != null) listeners(this, new DataPreprocessingChangedEventArgs(type, column, row)); 217 458 } 459 460 private const int MAX_UNDO_DEPTH = 5; 461 462 private readonly IList<Snapshot> undoHistory = new List<Snapshot>(); 463 private readonly Stack<DataPreprocessingChangedEventType> eventStack = new Stack<DataPreprocessingChangedEventType>(); 464 465 public bool IsInTransaction { get { return eventStack.Count > 0; } } 466 467 private void SaveSnapshot(DataPreprocessingChangedEventType changedType, int column, int row) { 468 if (IsInTransaction) return; 469 470 var currentSnapshot = new Snapshot { 471 VariableValues = CopyVariableValues(variableValues), 472 VariableNames = new List<string>(variableNames), 473 TrainingPartition = new IntRange(TrainingPartition.Start, TrainingPartition.End), 474 TestPartition = new IntRange(TestPartition.Start, TestPartition.End), 475 Transformations = new List<ITransformation>(Transformations), 476 ChangedType = changedType, 477 ChangedColumn = column, 478 ChangedRow = row 479 }; 480 481 if (undoHistory.Count >= MAX_UNDO_DEPTH) 482 undoHistory.RemoveAt(0); 483 484 undoHistory.Add(currentSnapshot); 485 } 486 487 public bool IsUndoAvailable { 488 get { return undoHistory.Count > 0; } 489 } 490 491 public void Undo() { 492 if (IsUndoAvailable) { 493 Snapshot previousSnapshot = undoHistory[undoHistory.Count - 1]; 494 variableValues = previousSnapshot.VariableValues; 495 variableNames = previousSnapshot.VariableNames; 496 TrainingPartition = previousSnapshot.TrainingPartition; 497 TestPartition = previousSnapshot.TestPartition; 498 Transformations = previousSnapshot.Transformations; 499 undoHistory.Remove(previousSnapshot); 500 OnChanged(previousSnapshot.ChangedType, 501 previousSnapshot.ChangedColumn, 502 previousSnapshot.ChangedRow); 503 } 504 } 505 506 public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) { 507 BeginTransaction(type); 508 action(); 509 EndTransaction(); 510 } 511 512 public void BeginTransaction(DataPreprocessingChangedEventType type) { 513 SaveSnapshot(type, -1, -1); 514 eventStack.Push(type); 515 } 516 517 public void EndTransaction() { 518 if (eventStack.Count == 0) 519 throw new InvalidOperationException("There is no open transaction that can be ended."); 520 521 var @event = eventStack.Pop(); 522 OnChanged(@event, -1, -1); 523 } 524 #endregion 525 526 #region Helpers 527 private static IList<IList> CopyVariableValues(IList<IList> original) { 528 var copy = new List<IList>(original); 529 for (int i = 0; i < original.Count; ++i) { 530 copy[i] = (IList)Activator.CreateInstance(original[i].GetType(), original[i]); 531 } 532 return copy; 533 } 218 534 #endregion 219 535 } -
branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/HeuristicLab.DataPreprocessing-3.4.csproj
r15269 r15270 145 145 <Compile Include="Content\DataGridContent.cs" /> 146 146 <Compile Include="PreprocessingContext.cs" /> 147 <Compile Include="Data\TransactionalPreprocessingData.cs" />148 147 <Compile Include="Logic\StatisticsLogic.cs" /> 149 <Compile Include="Data\ITransactionalPreprocessingData.cs" />150 148 <Compile Include="Plugin.cs" /> 151 149 <Compile Include="Properties\AssemblyInfo.cs" /> -
branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Logic/ManipulationLogic.cs
r15269 r15270 28 28 namespace HeuristicLab.DataPreprocessing { 29 29 public class ManipulationLogic { 30 private readonly I TransactionalPreprocessingData preprocessingData;30 private readonly IPreprocessingData preprocessingData; 31 31 private readonly StatisticsLogic statisticsLogic; 32 32 … … 35 35 } 36 36 37 public I TransactionalPreprocessingData PreProcessingData {37 public IPreprocessingData PreProcessingData { 38 38 get { return preprocessingData; } 39 39 } 40 40 41 public ManipulationLogic(I TransactionalPreprocessingData preprocessingData, StatisticsLogic theStatisticsLogic) {41 public ManipulationLogic(IPreprocessingData preprocessingData, StatisticsLogic theStatisticsLogic) { 42 42 this.preprocessingData = preprocessingData; 43 43 statisticsLogic = theStatisticsLogic; -
branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Logic/StatisticsLogic.cs
r15269 r15270 27 27 namespace HeuristicLab.DataPreprocessing { 28 28 public class StatisticsLogic { 29 private readonly I TransactionalPreprocessingData preprocessingData;30 31 public StatisticsLogic(I TransactionalPreprocessingData preprocessingData) {29 private readonly IPreprocessingData preprocessingData; 30 31 public StatisticsLogic(IPreprocessingData preprocessingData) { 32 32 this.preprocessingData = preprocessingData; 33 33 } -
branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/PreprocessingContext.cs
r15110 r15270 71 71 if (namedSource != null) 72 72 Name = "Preprocessing " + namedSource.Name; 73 Data = new FilteredPreprocessingData(new TransactionalPreprocessingData(problemData));73 Data = new FilteredPreprocessingData(new PreprocessingData(problemData)); 74 74 OnReset(); 75 75 // Reset GUI: -
branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/PreprocessingTransformator.cs
r14843 r15270 29 29 namespace HeuristicLab.DataPreprocessing { 30 30 public class PreprocessingTransformator { 31 private readonly I TransactionalPreprocessingData preprocessingData;31 private readonly IPreprocessingData preprocessingData; 32 32 33 33 private readonly IDictionary<string, IList<double>> originalColumns; … … 36 36 37 37 public PreprocessingTransformator(IPreprocessingData preprocessingData) { 38 this.preprocessingData = (ITransactionalPreprocessingData)preprocessingData;38 this.preprocessingData = preprocessingData; 39 39 originalColumns = new Dictionary<string, IList<double>>(); 40 40 renamedColumns = new Dictionary<string, string>(); … … 68 68 preprocessingData.Undo(); 69 69 } 70 } 71 catch (Exception e) { 70 } catch (Exception e) { 72 71 preprocessingData.Undo(); 73 72 if (string.IsNullOrEmpty(errorMsg)) errorMsg = e.Message; 74 } 75 finally { 73 } finally { 76 74 preprocessingData.EndTransaction(); 77 75 }
Note: See TracChangeset
for help on using the changeset viewer.