Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/ClassificationProblemData.cs @ 5601

Last change on this file since 5601 was 5601, checked in by mkommend, 14 years ago

#1418: Finished work on ProblemData.

File size: 16.9 KB
RevLine 
[5559]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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;
[5601]24using System.IO;
[5559]25using System.Linq;
26using HeuristicLab.Common;
[5601]27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
[5559]30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis {
33  [StorableClass]
[5601]34  [Item("ClassificationProblemData", "Represents an item containing all data defining a classification problem.")]
[5559]35  public class ClassificationProblemData : DataAnalysisProblemData, IClassificationProblemData {
[5601]36    private const string TargetVariableParameterName = "TargetVariable";
37    private const string ClassNamesParameterName = "ClassNames";
38    private const string ClassificationPenaltiesParameterName = "ClassificationPenalties";
39
[5559]40    #region default data
41    private static string[] defaultVariableNames = new string[] { "sample", "clump thickness", "cell size", "cell shape", "marginal adhesion", "epithelial cell size", "bare nuclei", "chromatin", "nucleoli", "mitoses", "class" };
42    private static double[,] defaultData = new double[,]{
43     {1000025,5,1,1,1,2,1,3,1,1,2      },
44     {1002945,5,4,4,5,7,10,3,2,1,2     },
45     {1015425,3,1,1,1,2,2,3,1,1,2      },
46     {1016277,6,8,8,1,3,4,3,7,1,2      },
47     {1017023,4,1,1,3,2,1,3,1,1,2      },
48     {1017122,8,10,10,8,7,10,9,7,1,4   },
49     {1018099,1,1,1,1,2,10,3,1,1,2     },
50     {1018561,2,1,2,1,2,1,3,1,1,2      },
51     {1033078,2,1,1,1,2,1,1,1,5,2      },
52     {1033078,4,2,1,1,2,1,2,1,1,2      },
53     {1035283,1,1,1,1,1,1,3,1,1,2      },
54     {1036172,2,1,1,1,2,1,2,1,1,2      },
55     {1041801,5,3,3,3,2,3,4,4,1,4      },
56     {1043999,1,1,1,1,2,3,3,1,1,2      },
57     {1044572,8,7,5,10,7,9,5,5,4,4     },
58     {1047630,7,4,6,4,6,1,4,3,1,4      },
59     {1048672,4,1,1,1,2,1,2,1,1,2      },
60     {1049815,4,1,1,1,2,1,3,1,1,2      },
61     {1050670,10,7,7,6,4,10,4,1,2,4    },
62     {1050718,6,1,1,1,2,1,3,1,1,2      },
63     {1054590,7,3,2,10,5,10,5,4,4,4    },
64     {1054593,10,5,5,3,6,7,7,10,1,4    },
65     {1056784,3,1,1,1,2,1,2,1,1,2      },
66     {1057013,8,4,5,1,2,2,7,3,1,4      },
67     {1059552,1,1,1,1,2,1,3,1,1,2      },
68     {1065726,5,2,3,4,2,7,3,6,1,4      },
69     {1066373,3,2,1,1,1,1,2,1,1,2      },
70     {1066979,5,1,1,1,2,1,2,1,1,2      },
71     {1067444,2,1,1,1,2,1,2,1,1,2      },
72     {1070935,1,1,3,1,2,1,1,1,1,2      },
73     {1070935,3,1,1,1,1,1,2,1,1,2      },
74     {1071760,2,1,1,1,2,1,3,1,1,2      },
75     {1072179,10,7,7,3,8,5,7,4,3,4     },
76     {1074610,2,1,1,2,2,1,3,1,1,2      },
77     {1075123,3,1,2,1,2,1,2,1,1,2      },
78     {1079304,2,1,1,1,2,1,2,1,1,2      },
79     {1080185,10,10,10,8,6,1,8,9,1,4   },
80     {1081791,6,2,1,1,1,1,7,1,1,2      },
81     {1084584,5,4,4,9,2,10,5,6,1,4     },
82     {1091262,2,5,3,3,6,7,7,5,1,4      },
83     {1096800,6,6,6,9,6,4,7,8,1,2      },
84     {1099510,10,4,3,1,3,3,6,5,2,4     },
85     {1100524,6,10,10,2,8,10,7,3,3,4   },
86     {1102573,5,6,5,6,10,1,3,1,1,4     },
87     {1103608,10,10,10,4,8,1,8,10,1,4  },
88     {1103722,1,1,1,1,2,1,2,1,2,2      },
89     {1105257,3,7,7,4,4,9,4,8,1,4      },
90     {1105524,1,1,1,1,2,1,2,1,1,2      },
91     {1106095,4,1,1,3,2,1,3,1,1,2      },
92     {1106829,7,8,7,2,4,8,3,8,2,4      },
93     {1108370,9,5,8,1,2,3,2,1,5,4      },
94     {1108449,5,3,3,4,2,4,3,4,1,4      },
95     {1110102,10,3,6,2,3,5,4,10,2,4    },
96     {1110503,5,5,5,8,10,8,7,3,7,4     },
97     {1110524,10,5,5,6,8,8,7,1,1,4     },
98     {1111249,10,6,6,3,4,5,3,6,1,4     },
99     {1112209,8,10,10,1,3,6,3,9,1,4    },
100     {1113038,8,2,4,1,5,1,5,4,4,4      },
101     {1113483,5,2,3,1,6,10,5,1,1,4     },
102     {1113906,9,5,5,2,2,2,5,1,1,4      },
103     {1115282,5,3,5,5,3,3,4,10,1,4     },
104     {1115293,1,1,1,1,2,2,2,1,1,2      },
105     {1116116,9,10,10,1,10,8,3,3,1,4   },
106     {1116132,6,3,4,1,5,2,3,9,1,4      },
107     {1116192,1,1,1,1,2,1,2,1,1,2      },
108     {1116998,10,4,2,1,3,2,4,3,10,4    },
109     {1117152,4,1,1,1,2,1,3,1,1,2      },
110     {1118039,5,3,4,1,8,10,4,9,1,4     },
111     {1120559,8,3,8,3,4,9,8,9,8,4      },
112     {1121732,1,1,1,1,2,1,3,2,1,2      },
113     {1121919,5,1,3,1,2,1,2,1,1,2      },
114     {1123061,6,10,2,8,10,2,7,8,10,4   },
115     {1124651,1,3,3,2,2,1,7,2,1,2      },
116     {1125035,9,4,5,10,6,10,4,8,1,4    },
117     {1126417,10,6,4,1,3,4,3,2,3,4     },
118     {1131294,1,1,2,1,2,2,4,2,1,2      },
119     {1132347,1,1,4,1,2,1,2,1,1,2      },
120     {1133041,5,3,1,2,2,1,2,1,1,2      },
121     {1133136,3,1,1,1,2,3,3,1,1,2      },
122     {1136142,2,1,1,1,3,1,2,1,1,2      },
123     {1137156,2,2,2,1,1,1,7,1,1,2      },
124     {1143978,4,1,1,2,2,1,2,1,1,2      },
125     {1143978,5,2,1,1,2,1,3,1,1,2      },
126     {1147044,3,1,1,1,2,2,7,1,1,2      },
127     {1147699,3,5,7,8,8,9,7,10,7,4     },
128     {1147748,5,10,6,1,10,4,4,10,10,4  },
129     {1148278,3,3,6,4,5,8,4,4,1,4      },
130     {1148873,3,6,6,6,5,10,6,8,3,4     },
131     {1152331,4,1,1,1,2,1,3,1,1,2      },
132     {1155546,2,1,1,2,3,1,2,1,1,2      },
133     {1156272,1,1,1,1,2,1,3,1,1,2      },
134     {1156948,3,1,1,2,2,1,1,1,1,2      },
135     {1157734,4,1,1,1,2,1,3,1,1,2      },
136     {1158247,1,1,1,1,2,1,2,1,1,2      },
137     {1160476,2,1,1,1,2,1,3,1,1,2      },
138     {1164066,1,1,1,1,2,1,3,1,1,2      },
139     {1165297,2,1,1,2,2,1,1,1,1,2      },
140     {1165790,5,1,1,1,2,1,3,1,1,2      },
141     {1165926,9,6,9,2,10,6,2,9,10,4    },
142     {1166630,7,5,6,10,5,10,7,9,4,4    },
143     {1166654,10,3,5,1,10,5,3,10,2,4   },
144     {1167439,2,3,4,4,2,5,2,5,1,4      },
145     {1167471,4,1,2,1,2,1,3,1,1,2      },
146     {1168359,8,2,3,1,6,3,7,1,1,4      },
147     {1168736,10,10,10,10,10,1,8,8,8,4 },
148     {1169049,7,3,4,4,3,3,3,2,7,4      },
149     {1170419,10,10,10,8,2,10,4,1,1,4  },
150     {1170420,1,6,8,10,8,10,5,7,1,4    },
151     {1171710,1,1,1,1,2,1,2,3,1,2      },
152     {1171710,6,5,4,4,3,9,7,8,3,4      },
153     {1171795,1,3,1,2,2,2,5,3,2,2      },
154     {1171845,8,6,4,3,5,9,3,1,1,4      },
155     {1172152,10,3,3,10,2,10,7,3,3,4   },
156     {1173216,10,10,10,3,10,8,8,1,1,4  },
157     {1173235,3,3,2,1,2,3,3,1,1,2      },
158     {1173347,1,1,1,1,2,5,1,1,1,2      },
159     {1173347,8,3,3,1,2,2,3,2,1,2      },
160     {1173509,4,5,5,10,4,10,7,5,8,4    },
161     {1173514,1,1,1,1,4,3,1,1,1,2      },
162     {1173681,3,2,1,1,2,2,3,1,1,2      },
163     {1174057,1,1,2,2,2,1,3,1,1,2      },
164     {1174057,4,2,1,1,2,2,3,1,1,2      },
165     {1174131,10,10,10,2,10,10,5,3,3,4 },
166     {1174428,5,3,5,1,8,10,5,3,1,4     },
167     {1175937,5,4,6,7,9,7,8,10,1,4     },
168     {1176406,1,1,1,1,2,1,2,1,1,2      },
169     {1176881,7,5,3,7,4,10,7,5,5,4        }
170};
171    private static Dataset defaultDataset;
172    private static IEnumerable<string> defaultAllowedInputVariables;
173    private static string defaultTargetVariable;
174    static ClassificationProblemData() {
175      defaultDataset = new Dataset(defaultVariableNames, defaultData);
176      defaultDataset.Name = "Wisconsin classification problem";
177      defaultDataset.Description = "subset from to ..";
178
179      defaultAllowedInputVariables = defaultVariableNames.Except(new List<string>() { "sample", "class" });
180      defaultTargetVariable = "class";
181    }
182    #endregion
183
[5601]184    #region parameter properties
185    public IValueParameter<StringValue> TargetVariableParameter {
186      get { return (IValueParameter<StringValue>)Parameters[TargetVariableParameterName]; }
187    }
188    public IFixedValueParameter<StringMatrix> ClassNamesParameter {
189      get { return (IFixedValueParameter<StringMatrix>)Parameters[ClassNamesParameterName]; }
190    }
191    public IFixedValueParameter<DoubleMatrix> ClassificationPenaltiesParameter {
192      get { return (IFixedValueParameter<DoubleMatrix>)Parameters[ClassificationPenaltiesParameterName]; }
193    }
194    #endregion
195
[5559]196    #region propeties
197    public string TargetVariable {
[5601]198      get { return TargetVariableParameter.Value.Value; }
199    }
[5559]200
[5601]201    private List<double> classValues;
202    public List<double> ClassValues {
203      get {
204        if (classValues == null) {
205          classValues = Dataset.GetEnumeratedVariableValues(TargetVariableParameter.Value.Value).Distinct().ToList();
206          classValues.Sort();
[5559]207        }
[5601]208        return classValues;
[5559]209      }
210    }
[5601]211    IEnumerable<double> IClassificationProblemData.ClassValues {
212      get { return ClassValues; }
[5559]213    }
214
215    public int Classes {
[5601]216      get { return ClassValues.Count; }
[5559]217    }
218
[5601]219    private List<string> classNames;
220    public List<string> ClassNames {
221      get {
222        if (classNames == null) {
223          classNames = new List<string>();
224          for (int i = 0; i < ClassNamesParameter.Value.Rows; i++)
225            classNames.Add(ClassNamesParameter.Value[i, 0]);
226        }
227        return classNames;
[5559]228      }
229    }
[5601]230    IEnumerable<string> IClassificationProblemData.ClassNames {
231      get { return ClassNames; }
[5559]232    }
233
[5601]234    private Dictionary<Tuple<double, double>, double> classificationPenaltiesCache = new Dictionary<Tuple<double, double>, double>();
[5559]235    #endregion
236
237
238    [StorableConstructor]
239    protected ClassificationProblemData(bool deserializing) : base(deserializing) { }
[5601]240    [StorableHook(HookType.AfterDeserialization)]
241    private void AfterDeserialization() {
242      RegisterParameterEvents();
243    }
[5559]244
[5601]245    protected ClassificationProblemData(ClassificationProblemData original, Cloner cloner)
246      : base(original, cloner) {
247      RegisterParameterEvents();
[5559]248    }
[5601]249    public override IDeepCloneable Clone(Cloner cloner) { return new ClassificationProblemData(this, cloner); }
[5559]250
[5601]251    public ClassificationProblemData() : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable) { }
[5559]252    public ClassificationProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable)
253      : base(dataset, allowedInputVariables) {
[5601]254      var variables = InputVariables.Select(x => x.AsReadOnly()).ToList();
255      Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>(variables), variables.Where(x => x.Value == targetVariable).First()));
256      Parameters.Add(new FixedValueParameter<StringMatrix>(ClassNamesParameterName, "", new StringMatrix()));
257      Parameters.Add(new FixedValueParameter<DoubleMatrix>(ClassificationPenaltiesParameterName, "", new DoubleMatrix()));
[5559]258
[5601]259      ResetTargetVariableDependentMembers();
260      RegisterParameterEvents();
[5559]261    }
262
[5601]263    private void ResetTargetVariableDependentMembers() {
264      DergisterParameterEvents();
[5559]265
[5601]266      classNames = null;
267      ((IStringConvertibleMatrix)ClassNamesParameter.Value).Columns = 1;
268      ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValues.Count;
269      for (int i = 0; i < Classes; i++)
270        ClassNamesParameter.Value[i, 0] = "Class " + ClassValues[i];
271      ClassNamesParameter.Value.ColumnNames = new List<string>() { "ClassNames" };
272      ClassNamesParameter.Value.RowNames = ClassValues.Select(s => "ClassValue: " + s);
[5559]273
[5601]274      classificationPenaltiesCache.Clear();
275      ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
276      ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Rows = Classes;
277      ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Columns = Classes;
278      ClassificationPenaltiesParameter.Value.RowNames = ClassNames.Select(name => "Actual " + name);
279      ClassificationPenaltiesParameter.Value.ColumnNames = ClassNames.Select(name => "Estimated " + name);
280      for (int i = 0; i < Classes; i++) {
281        for (int j = 0; j < Classes; j++) {
282          if (i != j) ClassificationPenaltiesParameter.Value[i, j] = 1;
283          else ClassificationPenaltiesParameter.Value[i, j] = 0;
[5559]284        }
285      }
[5601]286      ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = true;
287      RegisterParameterEvents();
[5559]288    }
289
290    public string GetClassName(double classValue) {
[5601]291      if (!ClassValues.Contains(classValue)) throw new ArgumentException();
292      int index = ClassValues.IndexOf(classValue);
293      return ClassNames[index];
[5559]294    }
295    public double GetClassValue(string className) {
[5601]296      if (!ClassNames.Contains(className)) throw new ArgumentException();
297      int index = ClassNames.IndexOf(className);
298      return ClassValues[index];
[5559]299    }
300    public void SetClassName(double classValue, string className) {
301      if (!classValues.Contains(classValue)) throw new ArgumentException();
[5601]302      int index = ClassValues.IndexOf(classValue);
303      ClassNames[index] = className;
304      ClassNamesParameter.Value[index, 0] = className;
[5559]305    }
306
307    public double GetClassificationPenalty(string correctClassName, string estimatedClassName) {
308      return GetClassificationPenalty(GetClassValue(correctClassName), GetClassValue(estimatedClassName));
309    }
310    public double GetClassificationPenalty(double correctClassValue, double estimatedClassValue) {
311      var key = Tuple.Create(correctClassValue, estimatedClassValue);
[5601]312      if (!classificationPenaltiesCache.ContainsKey(key)) {
313        int correctClassIndex = ClassValues.IndexOf(correctClassValue);
314        int estimatedClassIndex = ClassValues.IndexOf(estimatedClassValue);
315        classificationPenaltiesCache[key] = ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex];
316      }
317      return classificationPenaltiesCache[key];
[5559]318    }
319    public void SetClassificationPenalty(string correctClassName, string estimatedClassName, double penalty) {
320      SetClassificationPenalty(GetClassValue(correctClassName), GetClassValue(estimatedClassName), penalty);
321    }
322    public void SetClassificationPenalty(double correctClassValue, double estimatedClassValue, double penalty) {
323      var key = Tuple.Create(correctClassValue, estimatedClassValue);
[5601]324      int correctClassIndex = ClassValues.IndexOf(correctClassValue);
325      int estimatedClassIndex = ClassValues.IndexOf(estimatedClassValue);
326
327      ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex] = penalty;
[5559]328    }
329
[5601]330    #region events
331    private void RegisterParameterEvents() {
332      TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
333      ClassNamesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged);
334      ClassNamesParameter.Value.ItemChanged += new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);
335      ClassificationPenaltiesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged);
336      ClassificationPenaltiesParameter.Value.ItemChanged += new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);
[5559]337    }
[5601]338    private void DergisterParameterEvents() {
339      TargetVariableParameter.ValueChanged -= new EventHandler(TargetVariableParameter_ValueChanged);
340      ClassNamesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged);
341      ClassNamesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);
342      ClassificationPenaltiesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged);
343      ClassificationPenaltiesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);
[5559]344    }
[5601]345
346    private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
347      classValues = null;
348      ResetTargetVariableDependentMembers();
349      OnChanged();
350    }
351    private void Parameter_ValueChanged(object sender, EventArgs e) {
352      OnChanged();
353    }
354    private void MatrixParameter_ItemChanged(object sender, EventArgs<int, int> e) {
355      OnChanged();
356    }
357    #endregion
358
359    #region Import from file
360    public static ClassificationProblemData ImportFromFile(string fileName) {
361      TableFileParser csvFileParser = new TableFileParser();
362      csvFileParser.Parse(fileName);
363
364      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
365      dataset.Name = Path.GetFileName(fileName);
366
367      ClassificationProblemData problemData = new ClassificationProblemData(dataset, dataset.VariableNames.Skip(1), dataset.VariableNames.First());
368      problemData.Name = "Data imported from " + Path.GetFileName(fileName);
369      return problemData;
370    }
371    #endregion
[5559]372  }
373}
Note: See TracBrowser for help on using the repository browser.