1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Collections.Generic;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.DataPreprocessing {
|
---|
27 | public class SearchLogic {
|
---|
28 | private readonly ITransactionalPreprocessingData preprocessingData;
|
---|
29 | private readonly FilterLogic filterLogic;
|
---|
30 |
|
---|
31 | private Dictionary<int, IList<int>> MissingValueIndicies { get; set; }
|
---|
32 | private Dictionary<int, IList> ValuesWithoutNaN { get; set; }
|
---|
33 |
|
---|
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 |
|
---|
46 | public SearchLogic(ITransactionalPreprocessingData thePreprocessingData, FilterLogic theFilterLogic) {
|
---|
47 | preprocessingData = thePreprocessingData;
|
---|
48 | filterLogic = theFilterLogic;
|
---|
49 |
|
---|
50 | MissingValueIndicies = new Dictionary<int, IList<int>>();
|
---|
51 | ValuesWithoutNaN = new Dictionary<int, IList>();
|
---|
52 |
|
---|
53 | preprocessingData.Changed += PreprocessingData_Changed;
|
---|
54 | filterLogic.FilterChanged += FilterLogic_FilterChanged;
|
---|
55 | }
|
---|
56 |
|
---|
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) {
|
---|
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:
|
---|
80 | default:
|
---|
81 | MissingValueIndicies = new Dictionary<int, IList<int>>();
|
---|
82 | ValuesWithoutNaN = new Dictionary<int, IList>();
|
---|
83 | break;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
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));
|
---|
91 | }
|
---|
92 | return dic;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public bool IsMissingValue(int columnIndex, int rowIndex) {
|
---|
96 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
97 | return double.IsNaN(preprocessingData.GetCell<double>(columnIndex, rowIndex));
|
---|
98 | } else if (preprocessingData.VariableHasType<string>(columnIndex)) {
|
---|
99 | return string.IsNullOrEmpty(preprocessingData.GetCell<string>(columnIndex, rowIndex));
|
---|
100 | } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
101 | return preprocessingData.GetCell<DateTime>(columnIndex, rowIndex).Equals(DateTime.MinValue);
|
---|
102 | } else {
|
---|
103 | throw new ArgumentException("cell in column " + columnIndex + " and row index " + rowIndex + " contains a non supported type.");
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | public IList<int> GetMissingValueIndices(int columnIndex) {
|
---|
108 | if (!MissingValueIndicies.ContainsKey(columnIndex)) {
|
---|
109 | if (preprocessingData.VariableHasType<double>(columnIndex)) {
|
---|
110 | MissingValueIndicies[columnIndex] = GetMissingValueIndices<double>(columnIndex);
|
---|
111 | } else if (preprocessingData.VariableHasType<string>(columnIndex)) {
|
---|
112 | MissingValueIndicies[columnIndex] = GetMissingValueIndices<string>(columnIndex);
|
---|
113 | } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
|
---|
114 | MissingValueIndicies[columnIndex] = GetMissingValueIndices<DateTime>(columnIndex);
|
---|
115 | } else {
|
---|
116 | throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
|
---|
117 | }
|
---|
118 | }
|
---|
119 | return MissingValueIndicies[columnIndex];
|
---|
120 | }
|
---|
121 |
|
---|
122 | private IList<int> GetMissingValueIndices<T>(int columnIndex) {
|
---|
123 | List<int> missingIndices = new List<int>();
|
---|
124 |
|
---|
125 | for (int row = 0; row < preprocessingData.Rows; ++row) {
|
---|
126 | if (IsMissingValue(columnIndex, row)) {
|
---|
127 | missingIndices.Add(row);
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | return missingIndices;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public IEnumerable<T> GetValuesWithoutNaN<T>(int columnIndex, bool considerSelection) {
|
---|
135 | if (considerSelection) {
|
---|
136 | var selectedRows = preprocessingData.Selection[columnIndex];
|
---|
137 |
|
---|
138 | List<T> values = new List<T>();
|
---|
139 | foreach (var rowIdx in selectedRows) {
|
---|
140 | if (!IsMissingValue(columnIndex, rowIdx)) {
|
---|
141 | values.Add(preprocessingData.GetCell<T>(columnIndex, rowIdx));
|
---|
142 | }
|
---|
143 | }
|
---|
144 | return values;
|
---|
145 | } else {
|
---|
146 | if (!ValuesWithoutNaN.ContainsKey(columnIndex)) {
|
---|
147 | List<T> values = new List<T>();
|
---|
148 |
|
---|
149 | for (int row = 0; row < preprocessingData.Rows; ++row) {
|
---|
150 | if (!IsMissingValue(columnIndex, row)) {
|
---|
151 | values.Add(preprocessingData.GetCell<T>(columnIndex, row));
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | ValuesWithoutNaN[columnIndex] = values;
|
---|
156 | }
|
---|
157 | return (IEnumerable<T>)ValuesWithoutNaN[columnIndex];
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|