Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.DataPreprocessing/3.4/Logic/SearchLogic.cs @ 15899

Last change on this file since 15899 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
22using System;
23using System.Collections;
24using System.Collections.Generic;
25
26namespace 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 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
103      if (preprocessingData.VariableHasType<double>(columnIndex)) {
104        foreach (var v in preprocessingData.GetValues<double>(columnIndex)) {
105          if (double.IsNaN(v)) indices.Add(index);
106          index++;
107        }
108      } else if (preprocessingData.VariableHasType<string>(columnIndex)) {
109        foreach (var v in preprocessingData.GetValues<string>(columnIndex)) {
110          if (string.IsNullOrEmpty(v)) indices.Add(index);
111          index++;
112        }
113      } else if (preprocessingData.VariableHasType<DateTime>(columnIndex)) {
114        foreach (var v in preprocessingData.GetValues<DateTime>(columnIndex)) {
115          if (DateTime.MinValue.Equals(v)) indices.Add(index);
116          index++;
117        }
118      } else {
119        throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
120      }
121
122      MissingValueIndicies[columnIndex] = indices;
123      return MissingValueIndicies[columnIndex];
124    }
125
126
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.");
136      }
137    }
138
139    public IEnumerable<T> GetValuesWithoutNaN<T>(int columnIndex, bool considerSelection) {
140      if (considerSelection) {
141        var selectedRows = preprocessingData.Selection[columnIndex];
142
143        List<T> values = new List<T>();
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>();
153
154          for (int row = 0; row < preprocessingData.Rows; ++row) {
155            if (!IsMissingValue(columnIndex, row)) {
156              values.Add(preprocessingData.GetCell<T>(columnIndex, row));
157            }
158          }
159
160          ValuesWithoutNaN[columnIndex] = values;
161        }
162        return (IEnumerable<T>)ValuesWithoutNaN[columnIndex];
163      }
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.