[13502] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[13502] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[10783] | 23 | using System.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 28 |
|
---|
[13502] | 29 | namespace HeuristicLab.DataPreprocessing {
|
---|
[10804] | 30 | public class FilteredPreprocessingData : NamedItem, IFilteredPreprocessingData {
|
---|
[10978] | 31 | private readonly ITransactionalPreprocessingData originalData;
|
---|
| 32 | private ITransactionalPreprocessingData filteredData;
|
---|
[10783] | 33 |
|
---|
[10999] | 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 |
|
---|
[14381] | 50 | public IList<string> InputVariables { get { return ActiveData.InputVariables; } }
|
---|
| 51 | public string TargetVariable { get { return ActiveData.TargetVariable; } } // optional
|
---|
| 52 |
|
---|
[10992] | 53 | public IDictionary<int, IList<int>> Selection {
|
---|
[10978] | 54 | get { return originalData.Selection; }
|
---|
| 55 | set { originalData.Selection = value; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[10999] | 58 | public int Columns {
|
---|
| 59 | get { return ActiveData.Columns; }
|
---|
[10783] | 60 | }
|
---|
| 61 |
|
---|
[10999] | 62 | public int Rows {
|
---|
| 63 | get { return ActiveData.Rows; }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public ITransactionalPreprocessingData ActiveData {
|
---|
| 67 | get { return IsFiltered ? filteredData : originalData; }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 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 |
|
---|
[10783] | 79 | public FilteredPreprocessingData(ITransactionalPreprocessingData preporcessingData)
|
---|
| 80 | : base() {
|
---|
[10804] | 81 | originalData = preporcessingData;
|
---|
| 82 | filteredData = null;
|
---|
[10783] | 83 | }
|
---|
| 84 |
|
---|
[10999] | 85 | protected FilteredPreprocessingData(FilteredPreprocessingData original, Cloner cloner)
|
---|
| 86 | : base(original, cloner) {
|
---|
| 87 | originalData = original.originalData;
|
---|
| 88 | filteredData = original.filteredData;
|
---|
| 89 | }
|
---|
[10804] | 90 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 91 | return new FilteredPreprocessingData(this, cloner);
|
---|
| 92 | }
|
---|
[10783] | 93 |
|
---|
[10804] | 94 | public T GetCell<T>(int columnIndex, int rowIndex) {
|
---|
| 95 | return ActiveData.GetCell<T>(columnIndex, rowIndex);
|
---|
| 96 | }
|
---|
[10783] | 97 |
|
---|
[10804] | 98 | public void SetCell<T>(int columnIndex, int rowIndex, T value) {
|
---|
[10978] | 99 | if (IsFiltered)
|
---|
[11003] | 100 | throw new InvalidOperationException("SetValues not possible while data is filtered");
|
---|
[10978] | 101 | originalData.SetCell<T>(columnIndex, rowIndex, value);
|
---|
[10804] | 102 | }
|
---|
[10783] | 103 |
|
---|
[10804] | 104 | public string GetCellAsString(int columnIndex, int rowIndex) {
|
---|
| 105 | return ActiveData.GetCellAsString(columnIndex, rowIndex);
|
---|
| 106 | }
|
---|
[10783] | 107 |
|
---|
[10809] | 108 | public IList<T> GetValues<T>(int columnIndex, bool considerSelection) {
|
---|
| 109 | return ActiveData.GetValues<T>(columnIndex, considerSelection);
|
---|
[10804] | 110 | }
|
---|
[10783] | 111 |
|
---|
[10804] | 112 | public void SetValues<T>(int columnIndex, IList<T> values) {
|
---|
[10978] | 113 | if (IsFiltered)
|
---|
| 114 | throw new InvalidOperationException("SetValues not possible while data is filtered");
|
---|
| 115 |
|
---|
| 116 | originalData.SetValues<T>(columnIndex, values);
|
---|
[10804] | 117 | }
|
---|
[10783] | 118 |
|
---|
[10804] | 119 | public void InsertRow(int rowIndex) {
|
---|
[10978] | 120 | if (IsFiltered)
|
---|
| 121 | throw new InvalidOperationException("InsertRow not possible while data is filtered");
|
---|
| 122 |
|
---|
| 123 | originalData.InsertRow(rowIndex);
|
---|
[10804] | 124 | }
|
---|
[10783] | 125 |
|
---|
[10804] | 126 | public void DeleteRow(int rowIndex) {
|
---|
[10978] | 127 | if (IsFiltered)
|
---|
| 128 | throw new InvalidOperationException("DeleteRow not possible while data is filtered");
|
---|
| 129 |
|
---|
| 130 | originalData.DeleteRow(rowIndex);
|
---|
[10804] | 131 | }
|
---|
[10783] | 132 |
|
---|
[10804] | 133 | public void InsertColumn<T>(string variableName, int columnIndex) {
|
---|
[10978] | 134 | if (IsFiltered)
|
---|
| 135 | throw new InvalidOperationException("InsertColumn not possible while data is filtered");
|
---|
| 136 |
|
---|
| 137 | originalData.InsertColumn<T>(variableName, columnIndex);
|
---|
[10804] | 138 | }
|
---|
[10783] | 139 |
|
---|
[10804] | 140 | public void DeleteColumn(int columnIndex) {
|
---|
[10978] | 141 | if (IsFiltered)
|
---|
| 142 | throw new InvalidOperationException("DeleteColumn not possible while data is filtered");
|
---|
| 143 | originalData.DeleteColumn(columnIndex);
|
---|
[10804] | 144 | }
|
---|
[10783] | 145 |
|
---|
[13252] | 146 | public void RenameColumn(int columnIndex, string name) {
|
---|
| 147 | if (IsFiltered)
|
---|
| 148 | throw new InvalidOperationException("RenameColumn not possible while data is filtered");
|
---|
| 149 | originalData.RenameColumn(columnIndex, name);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | public void RenameColumns(IList<string> names) {
|
---|
| 153 | if (IsFiltered)
|
---|
| 154 | throw new InvalidOperationException("RenameColumns not possible while data is filtered");
|
---|
| 155 | originalData.RenameColumns(names);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[10804] | 158 | public string GetVariableName(int columnIndex) {
|
---|
| 159 | return ActiveData.GetVariableName(columnIndex);
|
---|
| 160 | }
|
---|
[10783] | 161 |
|
---|
[10804] | 162 | public int GetColumnIndex(string variableName) {
|
---|
| 163 | return ActiveData.GetColumnIndex(variableName);
|
---|
| 164 | }
|
---|
[10783] | 165 |
|
---|
[11156] | 166 | public bool VariableHasType<T>(int columnIndex) {
|
---|
| 167 | return originalData.VariableHasType<T>(columnIndex);
|
---|
[10804] | 168 | }
|
---|
[10783] | 169 |
|
---|
[10804] | 170 | public Dataset ExportToDataset() {
|
---|
| 171 | return originalData.ExportToDataset();
|
---|
| 172 | }
|
---|
[10783] | 173 |
|
---|
[10804] | 174 | public void SetFilter(bool[] rowFilters) {
|
---|
| 175 | filteredData = (ITransactionalPreprocessingData)originalData.Clone();
|
---|
[10939] | 176 | filteredData.InTransaction(() => {
|
---|
| 177 | for (int row = (rowFilters.Length - 1); row >= 0; --row) {
|
---|
| 178 | if (rowFilters[row]) {
|
---|
| 179 | filteredData.DeleteRow(row);
|
---|
| 180 | }
|
---|
[10804] | 181 | }
|
---|
[10939] | 182 | });
|
---|
[10900] | 183 | OnFilterChanged();
|
---|
[10804] | 184 | }
|
---|
[10783] | 185 |
|
---|
[10804] | 186 | public void PersistFilter() {
|
---|
[10900] | 187 | originalData.InTransaction(() => {
|
---|
| 188 | for (int i = 0; i < filteredData.Columns; ++i) {
|
---|
[11156] | 189 | if (filteredData.VariableHasType<double>(i)) {
|
---|
[10900] | 190 | originalData.SetValues<double>(i, filteredData.GetValues<double>(i));
|
---|
[11156] | 191 | } else if (filteredData.VariableHasType<string>(i)) {
|
---|
[10900] | 192 | originalData.SetValues<string>(i, filteredData.GetValues<string>(i));
|
---|
[11156] | 193 | } else if (filteredData.VariableHasType<DateTime>(i)) {
|
---|
[10900] | 194 | originalData.SetValues<DateTime>(i, filteredData.GetValues<DateTime>(i));
|
---|
| 195 | } else {
|
---|
| 196 | throw new ArgumentException("Data types of columns do not match");
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | });
|
---|
[10804] | 200 | ResetFilter();
|
---|
| 201 | }
|
---|
[10783] | 202 |
|
---|
[10804] | 203 | public void ResetFilter() {
|
---|
| 204 | filteredData = null;
|
---|
[10900] | 205 | OnFilterChanged();
|
---|
[10804] | 206 | }
|
---|
[10783] | 207 |
|
---|
[10900] | 208 | private void OnFilterChanged() {
|
---|
| 209 | if (FilterChanged != null) {
|
---|
| 210 | FilterChanged(this, new EventArgs());
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[11003] | 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 |
|
---|
[10804] | 236 | public void Undo() {
|
---|
[10978] | 237 | if (IsFiltered)
|
---|
| 238 | throw new InvalidOperationException("Undo not possible while data is filtered");
|
---|
| 239 |
|
---|
| 240 | originalData.Undo();
|
---|
[10804] | 241 | }
|
---|
[10783] | 242 |
|
---|
[10804] | 243 | public void InTransaction(Action action, DataPreprocessingChangedEventType type = DataPreprocessingChangedEventType.Any) {
|
---|
[10978] | 244 | if (IsFiltered)
|
---|
| 245 | throw new InvalidOperationException("Transaction not possible while data is filtered");
|
---|
| 246 | originalData.InTransaction(action, type);
|
---|
[10804] | 247 | }
|
---|
[10783] | 248 |
|
---|
[10804] | 249 | public void BeginTransaction(DataPreprocessingChangedEventType type) {
|
---|
[10978] | 250 | if (IsFiltered)
|
---|
| 251 | throw new InvalidOperationException("Transaction not possible while data is filtered");
|
---|
| 252 | originalData.BeginTransaction(type);
|
---|
[10804] | 253 | }
|
---|
| 254 |
|
---|
| 255 | public void EndTransaction() {
|
---|
| 256 | originalData.EndTransaction();
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[10999] | 259 | public IEnumerable<string> GetDoubleVariableNames() {
|
---|
| 260 | return originalData.GetDoubleVariableNames();
|
---|
| 261 | }
|
---|
[10900] | 262 |
|
---|
[10804] | 263 | public void ClearSelection() {
|
---|
| 264 | originalData.ClearSelection();
|
---|
| 265 | }
|
---|
| 266 |
|
---|
[10847] | 267 | public event EventHandler SelectionChanged {
|
---|
| 268 | add { originalData.SelectionChanged += value; }
|
---|
| 269 | remove { originalData.SelectionChanged -= value; }
|
---|
| 270 | }
|
---|
[10804] | 271 |
|
---|
[11003] | 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 |
|
---|
[10999] | 277 | public event EventHandler FilterChanged;
|
---|
[11003] | 278 | #endregion
|
---|
[10783] | 279 | }
|
---|
| 280 | }
|
---|