Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Views/3.4/FeatureCorrelation/FeatureCorrelationEnums.cs @ 9844

Last change on this file since 9844 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

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