Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/SearchLogic.cs @ 10809

Last change on this file since 10809 was 10809, checked in by sbreuer, 10 years ago
  • selected average and co. implemented
  • SelectionChanged NullPointer fixed
File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
25using System.Linq;
26
27namespace HeuristicLab.DataPreprocessing {
28  public class SearchLogic : ISearchLogic {
29    private readonly ITransactionalPreprocessingData preprocessingData;
30
31    private Dictionary<int, IList<int>> MissingValueIndicies { get; set; }
32    private Dictionary<int, IList> ValuesWithoutNaN { get; set; }
33
34    public SearchLogic(ITransactionalPreprocessingData thePreprocessingData) {
35      preprocessingData = thePreprocessingData;
36
37      MissingValueIndicies = new Dictionary<int, IList<int>>();
38      ValuesWithoutNaN = new Dictionary<int, IList>();
39
40      preprocessingData.Changed += preprocessingData_Changed;
41    }
42
43    void preprocessingData_Changed(object sender, DataPreprocessingChangedEventArgs e)
44    {
45      switch (e.Type) {
46        case DataPreprocessingChangedEventType.DeleteColumn:
47        case DataPreprocessingChangedEventType.ChangeColumn:
48          MissingValueIndicies.Remove(e.Column);
49          ValuesWithoutNaN.Remove(e.Column);
50          break;
51        case DataPreprocessingChangedEventType.AddColumn:
52          //cache does not need to be updated, will be calculated the first time it is requested
53          break;
54        case DataPreprocessingChangedEventType.DeleteRow:
55        case DataPreprocessingChangedEventType.AddRow:
56        case DataPreprocessingChangedEventType.ChangeItem:
57        case DataPreprocessingChangedEventType.Any:
58        case DataPreprocessingChangedEventType.Transformation:
59          MissingValueIndicies = new Dictionary<int, IList<int>>();
60          ValuesWithoutNaN = new Dictionary<int, IList>();
61          break;
62      }
63    }
64
65    public IDictionary<int, IList<int>> GetMissingValueIndices() {
66      var dic = new Dictionary<int, IList<int>>();
67      for (int i = 0; i < preprocessingData.Columns; ++i) {
68        dic.Add(i, GetMissingValueIndices(i));
69      }
70      return dic;
71    }
72
73    public bool IsMissingValue(int columnIndex, int rowIndex) {
74      if (preprocessingData.IsType<double>(columnIndex)) {
75        return double.IsNaN(preprocessingData.GetCell<double>(columnIndex, rowIndex));
76      } else if (preprocessingData.IsType<string>(columnIndex)) {
77        return string.IsNullOrEmpty(preprocessingData.GetCell<string>(columnIndex, rowIndex));
78      } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
79        return preprocessingData.GetCell<DateTime>(columnIndex, rowIndex).Equals(DateTime.MinValue);
80      } else {
81        throw new ArgumentException("cell in column " + columnIndex + " and row index " + rowIndex + " contains a non supported type.");
82      }
83    }
84
85    public IList<int> GetMissingValueIndices(int columnIndex) {
86      if (!MissingValueIndicies.ContainsKey(columnIndex)){       
87          if (preprocessingData.IsType<double>(columnIndex)) {
88            MissingValueIndicies[columnIndex] = GetMissingValueIndices<double>(columnIndex);
89          } else if (preprocessingData.IsType<string>(columnIndex)) {
90            MissingValueIndicies[columnIndex] = GetMissingValueIndices<string>(columnIndex);
91          } else if (preprocessingData.IsType<DateTime>(columnIndex)) {
92            MissingValueIndicies[columnIndex] = GetMissingValueIndices<DateTime>(columnIndex);
93          } else {
94            throw new ArgumentException("column " + columnIndex + " contains a non supported type.");
95          }
96      }
97
98      return MissingValueIndicies[columnIndex];
99   }
100    private IList<int> GetMissingValueIndices<T>(int columnIndex) {
101      List<int> missingIndices = new List<int>();
102     
103      for(int row = 0; row < preprocessingData.Rows; ++row) {
104        if (IsMissingValue(columnIndex, row)) {
105          missingIndices.Add(row);
106        }
107      }
108
109      return missingIndices;
110    }
111
112    public IEnumerable<T> GetValuesWithoutNaN<T>(int columnIndex, bool considerSelection)
113    {
114      if (considerSelection) {     
115        var selectedRows =  preprocessingData.GetSelection()[columnIndex];
116       
117        List<T> values = new List<T>();
118        foreach (var rowIdx in selectedRows) {
119          if (!IsMissingValue(columnIndex, rowIdx)) {
120            values.Add(preprocessingData.GetCell<T>(columnIndex, rowIdx));
121          }
122        }
123        return values;
124      } else {
125        if (!ValuesWithoutNaN.ContainsKey(columnIndex)) {
126          List<T> values = new List<T>();
127
128          for (int row = 0; row < preprocessingData.Rows; ++row) {
129            if (!IsMissingValue(columnIndex, row)) {
130              values.Add(preprocessingData.GetCell<T>(columnIndex, row));
131            }
132          }
133
134          ValuesWithoutNaN[columnIndex] = values;
135        }
136        return (IEnumerable<T>)ValuesWithoutNaN[columnIndex];
137      }
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.