Changeset 16308 for branches/2845_EnhancedProgress/HeuristicLab.DataPreprocessing/3.4/Data/FilteredPreprocessingData.cs
- Timestamp:
- 11/20/18 13:52:40 (5 years ago)
- Location:
- branches/2845_EnhancedProgress
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2845_EnhancedProgress
- Property svn:mergeinfo changed
/stable reverse-merged: 15587-15588 /trunk/sources removed
- Property svn:mergeinfo changed
-
branches/2845_EnhancedProgress/HeuristicLab.DataPreprocessing
- Property svn:mergeinfo changed
/stable/HeuristicLab.DataPreprocessing reverse-merged: 15587 /branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing removed /trunk/sources/HeuristicLab.DataPreprocessing removed
- Property svn:mergeinfo changed
-
branches/2845_EnhancedProgress/HeuristicLab.DataPreprocessing/3.4
- Property svn:mergeinfo changed
/stable/HeuristicLab.DataPreprocessing/3.4 reverse-merged: 15587 /branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4 removed /trunk/sources/HeuristicLab.DataPreprocessing/3.4 removed
- Property svn:mergeinfo changed
-
branches/2845_EnhancedProgress/HeuristicLab.DataPreprocessing/3.4/Data/FilteredPreprocessingData.cs
r16307 r16308 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; 27 26 using HeuristicLab.Data; 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;29 27 using HeuristicLab.Problems.DataAnalysis; 30 28 31 29 namespace HeuristicLab.DataPreprocessing { 32 [Item("FilteredPreprocessingData", "Represents filtered data used for preprocessing.")] 33 [StorableClass] 34 public sealed class FilteredPreprocessingData : NamedItem, IFilteredPreprocessingData { 35 36 [Storable] 37 private readonly IPreprocessingData originalData; 38 [Storable] 39 private IPreprocessingData filteredData; 40 41 public IPreprocessingData ActiveData { 30 public 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 { 42 67 get { return IsFiltered ? filteredData : originalData; } 43 68 } 44 69 45 #region Constructor, Cloning & Persistence 46 public FilteredPreprocessingData(IPreprocessingData preprocessingData) 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) 47 80 : base() { 48 originalData = prep rocessingData;81 originalData = preporcessingData; 49 82 filteredData = null; 50 83 } 51 84 52 pr ivateFilteredPreprocessingData(FilteredPreprocessingData original, Cloner cloner)85 protected FilteredPreprocessingData(FilteredPreprocessingData original, Cloner cloner) 53 86 : base(original, cloner) { 54 87 originalData = original.originalData; … … 59 92 } 60 93 61 [StorableConstructor]62 private FilteredPreprocessingData(bool deserializing)63 : base(deserializing) { }64 #endregion65 66 #region Cells67 public bool IsCellEmpty(int columnIndex, int rowIndex) {68 return ActiveData.IsCellEmpty(columnIndex, rowIndex);69 }70 71 94 public T GetCell<T>(int columnIndex, int rowIndex) { 72 95 return ActiveData.GetCell<T>(columnIndex, rowIndex); … … 94 117 } 95 118 96 public bool SetValue(string value, int columnIndex, int rowIndex) {97 if (IsFiltered)98 throw new InvalidOperationException("SetValue not possible while data is filtered");99 return originalData.SetValue(value, columnIndex, rowIndex);100 }101 102 public int Columns {103 get { return ActiveData.Columns; }104 }105 106 public int Rows {107 get { return ActiveData.Rows; }108 }109 #endregion110 111 #region Rows112 119 public void InsertRow(int rowIndex) { 113 120 if (IsFiltered) … … 122 129 123 130 originalData.DeleteRow(rowIndex); 124 }125 126 public void DeleteRowsWithIndices(IEnumerable<int> rows) {127 if (IsFiltered)128 throw new InvalidOperationException("DeleteRowsWithIndices not possible while data is filtered");129 130 originalData.DeleteRowsWithIndices(rows);131 131 } 132 132 … … 156 156 } 157 157 158 public bool AreAllStringColumns(IEnumerable<int> columnIndices) {159 return originalData.AreAllStringColumns(columnIndices);160 }161 #endregion162 163 #region Variables164 public IEnumerable<string> VariableNames {165 get { return ActiveData.VariableNames; }166 }167 public IEnumerable<string> GetDoubleVariableNames() {168 return originalData.GetDoubleVariableNames();169 }170 158 public string GetVariableName(int columnIndex) { 171 159 return ActiveData.GetVariableName(columnIndex); … … 180 168 } 181 169 182 public Type GetVariableType(int columnIndex) {183 return ActiveData.GetVariableType(columnIndex);184 }185 186 public IList<string> InputVariables {187 get { return ActiveData.InputVariables; }188 }189 190 public string TargetVariable {191 get { return ActiveData.TargetVariable; }192 } // optional193 #endregion194 195 #region Partitions196 public IntRange TrainingPartition {197 get { return originalData.TrainingPartition; }198 }199 200 public IntRange TestPartition {201 get { return originalData.TestPartition; }202 }203 #endregion204 205 #region Transformations206 public IList<ITransformation> Transformations {207 get { return originalData.Transformations; }208 }209 #endregion210 211 #region Validation212 public bool Validate(string value, out string errorMessage, int columnIndex) {213 return originalData.Validate(value, out errorMessage, columnIndex);214 }215 #endregion216 217 #region Import & Export218 public void Import(IDataAnalysisProblemData problemData) {219 if (IsFiltered)220 throw new InvalidOperationException("Import not possible while data is filtered");221 originalData.Import(problemData);222 }223 224 170 public Dataset ExportToDataset() { 225 171 return originalData.ExportToDataset(); 226 172 } 227 #endregion 228 229 #region Selection 230 public IDictionary<int, IList<int>> Selection { 231 get { return originalData.Selection; } 232 set { originalData.Selection = value; } 233 } 234 235 public void ClearSelection() { 236 originalData.ClearSelection(); 237 } 238 239 public event EventHandler SelectionChanged { 240 add { originalData.SelectionChanged += value; } 241 remove { originalData.SelectionChanged -= value; } 242 } 243 #endregion 244 245 #region Transactions 246 public event DataPreprocessingChangedEventHandler Changed { 247 add { originalData.Changed += value; } 248 remove { originalData.Changed -= value; } 249 } 250 251 public bool IsUndoAvailable { 252 get { return IsFiltered ? false : originalData.IsUndoAvailable; } 253 } 254 255 public void Undo() { 256 if (IsFiltered) 257 throw new InvalidOperationException("Undo not possible while data is filtered"); 258 259 originalData.Undo(); 260 } 261 262 public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) { 263 if (IsFiltered) 264 throw new InvalidOperationException("Transaction not possible while data is filtered"); 265 originalData.InTransaction(action, type); 266 } 267 268 public void BeginTransaction(DataPreprocessingChangedEventType type) { 269 if (IsFiltered) 270 throw new InvalidOperationException("Transaction not possible while data is filtered"); 271 originalData.BeginTransaction(type); 272 } 273 274 public void EndTransaction() { 275 originalData.EndTransaction(); 276 } 277 #endregion 278 279 #region Statistics 280 public T GetMin<T>(int columnIndex, bool considerSelection = false, T emptyValue = default(T)) { 281 return ActiveData.GetMin<T>(columnIndex, considerSelection, emptyValue); 282 } 283 public T GetMax<T>(int columnIndex, bool considerSelection = false, T emptyValue = default(T)) { 284 return ActiveData.GetMax<T>(columnIndex, considerSelection, emptyValue); 285 } 286 public T GetMean<T>(int columnIndex, bool considerSelection = false, T emptyValue = default(T)) { 287 return ActiveData.GetMean<T>(columnIndex, considerSelection, emptyValue); 288 } 289 public T GetMedian<T>(int columnIndex, bool considerSelection = false, T emptyValue = default(T)) where T : IComparable<T> { 290 return ActiveData.GetMedian<T>(columnIndex, considerSelection, emptyValue); 291 } 292 public T GetMode<T>(int columnIndex, bool considerSelection = false, T emptyValue = default(T)) where T : IEquatable<T> { 293 return ActiveData.GetMode<T>(columnIndex, considerSelection, emptyValue); 294 } 295 public T GetStandardDeviation<T>(int columnIndex, bool considerSelection = false, T emptyValue = default(T)) { 296 return ActiveData.GetStandardDeviation<T>(columnIndex, considerSelection, emptyValue); 297 } 298 public T GetVariance<T>(int columnIndex, bool considerSelection = false, T emptyValue = default(T)) { 299 return ActiveData.GetVariance<T>(columnIndex, considerSelection, emptyValue); 300 } 301 public T GetQuantile<T>(double alpha, int columnIndex, bool considerSelection = false, T emptyValue = default(T)) where T : IComparable<T> { 302 return ActiveData.GetQuantile<T>(alpha, columnIndex, considerSelection, emptyValue); 303 } 304 public int GetDistinctValues<T>(int columnIndex, bool considerSelection = false) { 305 return ActiveData.GetDistinctValues<T>(columnIndex, considerSelection); 306 } 307 308 public int GetMissingValueCount() { 309 return ActiveData.GetMissingValueCount(); 310 } 311 public int GetMissingValueCount(int columnIndex) { 312 return ActiveData.GetMissingValueCount(columnIndex); 313 } 314 public int GetRowMissingValueCount(int rowIndex) { 315 return ActiveData.GetRowMissingValueCount(rowIndex); 316 } 317 #endregion 318 319 #region Filters 320 public void SetFilter(bool[] remainingRows) { 321 filteredData = (IPreprocessingData)originalData.Clone(); 173 174 public void SetFilter(bool[] rowFilters) { 175 filteredData = (ITransactionalPreprocessingData)originalData.Clone(); 322 176 filteredData.InTransaction(() => { 323 var remainingIndices = Enumerable.Range(0, remainingRows.Length).Where(x => remainingRows[x]); 324 325 foreach (var v in filteredData.VariableNames) { 326 var ci = filteredData.GetColumnIndex(v); 327 if (filteredData.VariableHasType<double>(ci)) { 328 var values = filteredData.GetValues<double>(ci); 329 var filteredValues = remainingIndices.Select(x => values[x]).ToList(); 330 filteredData.SetValues(ci, filteredValues); 331 } else if (filteredData.VariableHasType<DateTime>(ci)) { 332 var values = filteredData.GetValues<DateTime>(ci); 333 var filteredValues = remainingIndices.Select(x => values[x]).ToList(); 334 filteredData.SetValues(ci, filteredValues); 335 } else if (filteredData.VariableHasType<string>(ci)) { 336 var values = filteredData.GetValues<string>(ci); 337 var filteredValues = remainingIndices.Select(x => values[x]).ToList(); 338 filteredData.SetValues(ci, filteredValues); 177 for (int row = (rowFilters.Length - 1); row >= 0; --row) { 178 if (rowFilters[row]) { 179 filteredData.DeleteRow(row); 339 180 } 340 181 } … … 365 206 } 366 207 367 public bool IsFiltered {368 get { return filteredData != null; }369 }370 371 public event EventHandler FilterChanged;372 373 208 private void OnFilterChanged() { 374 209 if (FilterChanged != null) { … … 376 211 } 377 212 } 213 214 public event DataPreprocessingChangedEventHandler Changed { 215 add { originalData.Changed += value; } 216 remove { originalData.Changed -= value; } 217 } 218 219 public bool SetValue(string value, int columnIndex, int rowIndex) { 220 if (IsFiltered) 221 throw new InvalidOperationException("SetValue not possible while data is filtered"); 222 return originalData.SetValue(value, columnIndex, rowIndex); 223 } 224 225 public bool AreAllStringColumns(IEnumerable<int> columnIndices) { 226 return originalData.AreAllStringColumns(columnIndices); 227 } 228 229 public void DeleteRowsWithIndices(IEnumerable<int> rows) { 230 if (IsFiltered) 231 throw new InvalidOperationException("DeleteRowsWithIndices not possible while data is filtered"); 232 233 originalData.DeleteRowsWithIndices(rows); 234 } 235 236 public void Undo() { 237 if (IsFiltered) 238 throw new InvalidOperationException("Undo not possible while data is filtered"); 239 240 originalData.Undo(); 241 } 242 243 public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) { 244 if (IsFiltered) 245 throw new InvalidOperationException("Transaction not possible while data is filtered"); 246 originalData.InTransaction(action, type); 247 } 248 249 public void BeginTransaction(DataPreprocessingChangedEventType type) { 250 if (IsFiltered) 251 throw new InvalidOperationException("Transaction not possible while data is filtered"); 252 originalData.BeginTransaction(type); 253 } 254 255 public void EndTransaction() { 256 originalData.EndTransaction(); 257 } 258 259 public IEnumerable<string> GetDoubleVariableNames() { 260 return originalData.GetDoubleVariableNames(); 261 } 262 263 public void ClearSelection() { 264 originalData.ClearSelection(); 265 } 266 267 public event EventHandler SelectionChanged { 268 add { originalData.SelectionChanged += value; } 269 remove { originalData.SelectionChanged -= value; } 270 } 271 272 #region IPreprocessingData Members 273 public bool Validate(string value, out string errorMessage, int columnIndex) { 274 return originalData.Validate(value, out errorMessage, columnIndex); 275 } 276 277 public event EventHandler FilterChanged; 378 278 #endregion 379 279 }
Note: See TracChangeset
for help on using the changeset viewer.