[10539] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10539] | 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;
|
---|
[10661] | 23 | using System.Collections;
|
---|
[10236] | 24 | using System.Collections.Generic;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.DataPreprocessing {
|
---|
[13508] | 27 | public class SearchLogic {
|
---|
[10586] | 28 | private readonly ITransactionalPreprocessingData preprocessingData;
|
---|
[13508] | 29 | private readonly FilterLogic filterLogic;
|
---|
[10236] | 30 |
|
---|
[10776] | 31 | private Dictionary<int, IList<int>> MissingValueIndicies { get; set; }
|
---|
[10809] | 32 | private Dictionary<int, IList> ValuesWithoutNaN { get; set; }
|
---|
[10661] | 33 |
|
---|
[11002] | 34 | public IEnumerable<string> VariableNames {
|
---|
| 35 | get { return preprocessingData.VariableNames; }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public int Columns {
|
---|
| 39 | get { return preprocessingData.Columns; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public int Rows {
|
---|
| 43 | get { return preprocessingData.Rows; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[13508] | 46 | public SearchLogic(ITransactionalPreprocessingData thePreprocessingData, FilterLogic theFilterLogic) {
|
---|
[10236] | 47 | preprocessingData = thePreprocessingData;
|
---|
[12676] | 48 | filterLogic = theFilterLogic;
|
---|
[10661] | 49 |
|
---|
[10776] | 50 | MissingValueIndicies = new Dictionary<int, IList<int>>();
|
---|
[10809] | 51 | ValuesWithoutNaN = new Dictionary<int, IList>();
|
---|
[10661] | 52 |
|
---|
[12676] | 53 | preprocessingData.Changed += PreprocessingData_Changed;
|
---|
| 54 | filterLogic.FilterChanged += FilterLogic_FilterChanged;
|
---|
[10236] | 55 | }
|
---|
| 56 |
|
---|
[12676] | 57 | void FilterLogic_FilterChanged(object sender, EventArgs e) {
|
---|
| 58 | //recalculate
|
---|
| 59 | for (int i = 0; i < Columns; i++) {
|
---|
| 60 | MissingValueIndicies.Remove(i);
|
---|
| 61 | ValuesWithoutNaN.Remove(i);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void PreprocessingData_Changed(object sender, DataPreprocessingChangedEventArgs e) {
|
---|
[10737] | 66 | switch (e.Type) {
|
---|
| 67 | case DataPreprocessingChangedEventType.DeleteColumn:
|
---|
| 68 | case DataPreprocessingChangedEventType.ChangeColumn:
|
---|
| 69 | MissingValueIndicies.Remove(e.Column);
|
---|
| 70 | ValuesWithoutNaN.Remove(e.Column);
|
---|
| 71 | break;
|
---|
| 72 | case DataPreprocessingChangedEventType.AddColumn:
|
---|
| 73 | //cache does not need to be updated, will be calculated the first time it is requested
|
---|
| 74 | break;
|
---|
| 75 | case DataPreprocessingChangedEventType.DeleteRow:
|
---|
| 76 | case DataPreprocessingChangedEventType.AddRow:
|
---|
| 77 | case DataPreprocessingChangedEventType.ChangeItem:
|
---|
| 78 | case DataPreprocessingChangedEventType.Any:
|
---|
| 79 | case DataPreprocessingChangedEventType.Transformation:
|
---|
[10817] | 80 | default:
|
---|
[10776] | 81 | MissingValueIndicies = new Dictionary<int, IList<int>>();
|
---|
[10809] | 82 | ValuesWithoutNaN = new Dictionary<int, IList>();
|
---|
[10737] | 83 | break;
|
---|
[12676] | 84 | }
|
---|
[10661] | 85 | }
|
---|
| 86 |
|
---|
[10776] | 87 | public IDictionary<int, IList<int>> GetMissingValueIndices() {
|
---|
| 88 | var dic = new Dictionary<int, IList<int>>();
|
---|
| 89 | for (int i = 0; i < preprocessingData.Columns; ++i) {
|
---|
| 90 | dic.Add(i, GetMissingValueIndices(i));
|
---|
[10236] | 91 | }
|
---|
| 92 | return dic;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[13838] | 95 | public IList<int> GetMissingValueIndices(int columnIndex) {
|
---|
| 96 | int index = 0;
|
---|
| 97 | var indices = new List<int>();
|
---|
| 98 |
|
---|
| 99 | if (MissingValueIndicies.ContainsKey(columnIndex)) {
|
---|
| 100 | return MissingValueIndicies[columnIndex];
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[11156] | 103 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
[13838] | 104 | foreach (var v in preprocessingData.GetValues<double>(columnIndex)) {
|
---|
| 105 | if (double.IsNaN(v)) indices.Add(index);
|
---|
| 106 | index++;
|
---|
| 107 | }
|
---|
[11156] | 108 | } else if (preprocessingData.VariableHasType<string>(columnIndex)) {
|
---|
[13838] | 109 | foreach (var v in preprocessingData.GetValues<string>(columnIndex)) {
|
---|
| 110 | if (string.IsNullOrEmpty(v)) indices.Add(index);
|
---|
| 111 | index++;
|
---|
| 112 | }
|
---|
[11156] | 113 | } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
[13838] | 114 | foreach (var v in preprocessingData.GetValues<DateTime>(columnIndex)) {
|
---|
| 115 | if (DateTime.MinValue.Equals(v)) indices.Add(index);
|
---|
| 116 | index++;
|
---|
| 117 | }
|
---|
[10236] | 118 | } else {
|
---|
[13838] | 119 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
[10236] | 120 | }
|
---|
| 121 |
|
---|
[13838] | 122 | MissingValueIndicies[columnIndex] = indices;
|
---|
[12676] | 123 | return MissingValueIndicies[columnIndex];
|
---|
| 124 | }
|
---|
[10661] | 125 |
|
---|
[12676] | 126 |
|
---|
[13838] | 127 | public bool IsMissingValue(int columnIndex, int rowIndex) {
|
---|
| 128 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
| 129 | return double.IsNaN(preprocessingData.GetCell<double>(columnIndex, rowIndex));
|
---|
| 130 | } else if (preprocessingData.VariableHasType<string>(columnIndex)) {
|
---|
| 131 | return string.IsNullOrEmpty(preprocessingData.GetCell<string>(columnIndex, rowIndex));
|
---|
| 132 | } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
| 133 | return preprocessingData.GetCell<DateTime>(columnIndex, rowIndex).Equals(DateTime.MinValue);
|
---|
| 134 | } else {
|
---|
| 135 | throw new ArgumentException("cell in column " + columnIndex + " and row index " + rowIndex + " contains a non supported type.");
|
---|
[10236] | 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[12676] | 139 | public IEnumerable<T> GetValuesWithoutNaN<T>(int columnIndex, bool considerSelection) {
|
---|
| 140 | if (considerSelection) {
|
---|
| 141 | var selectedRows = preprocessingData.Selection[columnIndex];
|
---|
| 142 |
|
---|
[10661] | 143 | List<T> values = new List<T>();
|
---|
[10809] | 144 | foreach (var rowIdx in selectedRows) {
|
---|
| 145 | if (!IsMissingValue(columnIndex, rowIdx)) {
|
---|
| 146 | values.Add(preprocessingData.GetCell<T>(columnIndex, rowIdx));
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | return values;
|
---|
| 150 | } else {
|
---|
| 151 | if (!ValuesWithoutNaN.ContainsKey(columnIndex)) {
|
---|
| 152 | List<T> values = new List<T>();
|
---|
[10661] | 153 |
|
---|
[10809] | 154 | for (int row = 0; row < preprocessingData.Rows; ++row) {
|
---|
| 155 | if (!IsMissingValue(columnIndex, row)) {
|
---|
| 156 | values.Add(preprocessingData.GetCell<T>(columnIndex, row));
|
---|
| 157 | }
|
---|
[10661] | 158 | }
|
---|
[10809] | 159 |
|
---|
| 160 | ValuesWithoutNaN[columnIndex] = values;
|
---|
[10661] | 161 | }
|
---|
[10809] | 162 | return (IEnumerable<T>)ValuesWithoutNaN[columnIndex];
|
---|
[10661] | 163 | }
|
---|
| 164 | }
|
---|
[10236] | 165 | }
|
---|
| 166 | }
|
---|