Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/FeatureCorrelation/FeatureCorrelationEnums.cs @ 8578

Last change on this file since 8578 was 8578, checked in by sforsten, 12 years ago

#1292:

  • added ProblemDataView which has a button to open the feature correlation
  • added abstract base class for feature correlations
  • added caches for the feature correlation
  • created own class for calculation of feature correlation
  • changed SelectedItemChanged to SelectionChangeCommitted events, so the correlation is only calculated if the user changes the selection
File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Generic;
24using System.ComponentModel;
25using System.Linq;
26using System.Reflection;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.Problems.DataAnalysis {
30  // see http://blog.spontaneouspublicity.com/associating-strings-with-enums-in-c
31  public class FeatureCorrelationEnums {
32    public enum Partitions {
33      [Description("All Samples")]
34      AllSamples,
35      [Description("Training Samples")]
36      TrainingSamples,
37      [Description("Test Samples")]
38      TestSamples
39    }
40
41    public enum CorrelationCalculators {
42      [Description("Pearsons R")]
43      PearsonsR,
44      [Description("Pearsons R Squared")]
45      PearsonsRSquared,
46      [Description("Hoeffdings Dependence")]
47      HoeffdingsDependence,
48      [Description("Spearmans Rank")]
49      SpearmansRank
50    }
51
52    public static readonly Dictionary<CorrelationCalculators, DoubleRange> calculatorInterval = new Dictionary<CorrelationCalculators, DoubleRange>(){
53      { CorrelationCalculators.PearsonsR, new DoubleRange(1.0, -1.0)},
54      { CorrelationCalculators.SpearmansRank, new DoubleRange(1.0, -1.0)},
55      { CorrelationCalculators.HoeffdingsDependence, new DoubleRange(1.0, -0.5)},
56      { CorrelationCalculators.PearsonsRSquared, new DoubleRange(1.0, 0.0)}
57    };
58
59    public static string GetEnumDescription(object value) {
60      FieldInfo fi = value.GetType().GetField(value.ToString());
61
62      DescriptionAttribute[] attributes =
63          (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
64
65      if (attributes != null && attributes.Length > 0)
66        return attributes[0].Description;
67      else
68        return value.ToString();
69    }
70
71    public static IEnumerable<T> EnumToList<T>() {
72      Type enumType = typeof(T);
73
74      // Can't use generic type constraints on value types,
75      // so have to do check like this
76      if (enumType.BaseType != typeof(Enum)) {
77        throw new ArgumentException("T must be of type System.Enum");
78      }
79
80      Array enumValArray = Enum.GetValues(enumType);
81      return Enum.GetValues(enumType).Cast<int>().Select(x => (T)Enum.Parse(enumType, x.ToString())).ToList();
82    }
83
84    public static T GetEnumOfDescription<T>(string desc) {
85      Type enumType = typeof(T);
86
87      if (enumType.BaseType != typeof(Enum))
88        throw new ArgumentException("T must be of type System.Enum");
89
90      Array enumValArray = Enum.GetValues(enumType);
91      foreach (int val in enumValArray) {
92
93        T e = (T)Enum.Parse(enumType, val.ToString());
94        if (GetEnumDescription(e).Equals(desc)) {
95          return (T)e;
96        }
97      }
98      throw new ArgumentException("Description is not in the given type T");
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.