Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationProblemData.cs @ 6232

Last change on this file since 6232 was 6232, checked in by epitzer, 12 years ago

rebased on current trunk r6223 (#1530)

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