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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Reflection;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 |
|
---|
29 | namespace 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 | }
|
---|