Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Problems.DataAnalysis.Classification/3.3/ClassificationProblemData.cs @ 10355

Last change on this file since 10355 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 13.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Classification {
33  [Item("ClassificationProblemData", "Represents an item containing all data defining a classification problem.")]
34  [StorableClass]
35  public class ClassificationProblemData : DataAnalysisProblemData {
36    #region default data
37    private static string[] defaultInputs = new string[] { "sample", "clump thickness", "cell size", "cell shape", "marginal adhesion", "epithelial cell size", "bare nuclei", "chromatin", "nucleoli", "mitoses", "class" };
38    private static double[,] defaultData = new double[,]{
39     {1000025,5,1,1,1,2,1,3,1,1,2      },
40     {1002945,5,4,4,5,7,10,3,2,1,2     },
41     {1015425,3,1,1,1,2,2,3,1,1,2      },
42     {1016277,6,8,8,1,3,4,3,7,1,2      },
43     {1017023,4,1,1,3,2,1,3,1,1,2      },
44     {1017122,8,10,10,8,7,10,9,7,1,4   },
45     {1018099,1,1,1,1,2,10,3,1,1,2     },
46     {1018561,2,1,2,1,2,1,3,1,1,2      },
47     {1033078,2,1,1,1,2,1,1,1,5,2      },
48     {1033078,4,2,1,1,2,1,2,1,1,2      },
49     {1035283,1,1,1,1,1,1,3,1,1,2      },
50     {1036172,2,1,1,1,2,1,2,1,1,2      },
51     {1041801,5,3,3,3,2,3,4,4,1,4      },
52     {1043999,1,1,1,1,2,3,3,1,1,2      },
53     {1044572,8,7,5,10,7,9,5,5,4,4     },
54     {1047630,7,4,6,4,6,1,4,3,1,4      },
55     {1048672,4,1,1,1,2,1,2,1,1,2      },
56     {1049815,4,1,1,1,2,1,3,1,1,2      },
57     {1050670,10,7,7,6,4,10,4,1,2,4    },
58     {1050718,6,1,1,1,2,1,3,1,1,2      },
59     {1054590,7,3,2,10,5,10,5,4,4,4    },
60     {1054593,10,5,5,3,6,7,7,10,1,4    },
61     {1056784,3,1,1,1,2,1,2,1,1,2      },
62     {1057013,8,4,5,1,2,2,7,3,1,4      },
63     {1059552,1,1,1,1,2,1,3,1,1,2      },
64     {1065726,5,2,3,4,2,7,3,6,1,4      },
65     {1066373,3,2,1,1,1,1,2,1,1,2      },
66     {1066979,5,1,1,1,2,1,2,1,1,2      },
67     {1067444,2,1,1,1,2,1,2,1,1,2      },
68     {1070935,1,1,3,1,2,1,1,1,1,2      },
69     {1070935,3,1,1,1,1,1,2,1,1,2      },
70     {1071760,2,1,1,1,2,1,3,1,1,2      },
71     {1072179,10,7,7,3,8,5,7,4,3,4     },
72     {1074610,2,1,1,2,2,1,3,1,1,2      },
73     {1075123,3,1,2,1,2,1,2,1,1,2      },
74     {1079304,2,1,1,1,2,1,2,1,1,2      },
75     {1080185,10,10,10,8,6,1,8,9,1,4   },
76     {1081791,6,2,1,1,1,1,7,1,1,2      },
77     {1084584,5,4,4,9,2,10,5,6,1,4     },
78     {1091262,2,5,3,3,6,7,7,5,1,4      },
79     {1096800,6,6,6,9,6,4,7,8,1,2      },
80     {1099510,10,4,3,1,3,3,6,5,2,4     },
81     {1100524,6,10,10,2,8,10,7,3,3,4   },
82     {1102573,5,6,5,6,10,1,3,1,1,4     },
83     {1103608,10,10,10,4,8,1,8,10,1,4  },
84     {1103722,1,1,1,1,2,1,2,1,2,2      },
85     {1105257,3,7,7,4,4,9,4,8,1,4      },
86     {1105524,1,1,1,1,2,1,2,1,1,2      },
87     {1106095,4,1,1,3,2,1,3,1,1,2      },
88     {1106829,7,8,7,2,4,8,3,8,2,4      },
89     {1108370,9,5,8,1,2,3,2,1,5,4      },
90     {1108449,5,3,3,4,2,4,3,4,1,4      },
91     {1110102,10,3,6,2,3,5,4,10,2,4    },
92     {1110503,5,5,5,8,10,8,7,3,7,4     },
93     {1110524,10,5,5,6,8,8,7,1,1,4     },
94     {1111249,10,6,6,3,4,5,3,6,1,4     },
95     {1112209,8,10,10,1,3,6,3,9,1,4    },
96     {1113038,8,2,4,1,5,1,5,4,4,4      },
97     {1113483,5,2,3,1,6,10,5,1,1,4     },
98     {1113906,9,5,5,2,2,2,5,1,1,4      },
99     {1115282,5,3,5,5,3,3,4,10,1,4     },
100     {1115293,1,1,1,1,2,2,2,1,1,2      },
101     {1116116,9,10,10,1,10,8,3,3,1,4   },
102     {1116132,6,3,4,1,5,2,3,9,1,4      },
103     {1116192,1,1,1,1,2,1,2,1,1,2      },
104     {1116998,10,4,2,1,3,2,4,3,10,4    },
105     {1117152,4,1,1,1,2,1,3,1,1,2      },
106     {1118039,5,3,4,1,8,10,4,9,1,4     },
107     {1120559,8,3,8,3,4,9,8,9,8,4      },
108     {1121732,1,1,1,1,2,1,3,2,1,2      },
109     {1121919,5,1,3,1,2,1,2,1,1,2      },
110     {1123061,6,10,2,8,10,2,7,8,10,4   },
111     {1124651,1,3,3,2,2,1,7,2,1,2      },
112     {1125035,9,4,5,10,6,10,4,8,1,4    },
113     {1126417,10,6,4,1,3,4,3,2,3,4     },
114     {1131294,1,1,2,1,2,2,4,2,1,2      },
115     {1132347,1,1,4,1,2,1,2,1,1,2      },
116     {1133041,5,3,1,2,2,1,2,1,1,2      },
117     {1133136,3,1,1,1,2,3,3,1,1,2      },
118     {1136142,2,1,1,1,3,1,2,1,1,2      },
119     {1137156,2,2,2,1,1,1,7,1,1,2      },
120     {1143978,4,1,1,2,2,1,2,1,1,2      },
121     {1143978,5,2,1,1,2,1,3,1,1,2      },
122     {1147044,3,1,1,1,2,2,7,1,1,2      },
123     {1147699,3,5,7,8,8,9,7,10,7,4     },
124     {1147748,5,10,6,1,10,4,4,10,10,4  },
125     {1148278,3,3,6,4,5,8,4,4,1,4      },
126     {1148873,3,6,6,6,5,10,6,8,3,4     },
127     {1152331,4,1,1,1,2,1,3,1,1,2      },
128     {1155546,2,1,1,2,3,1,2,1,1,2      },
129     {1156272,1,1,1,1,2,1,3,1,1,2      },
130     {1156948,3,1,1,2,2,1,1,1,1,2      },
131     {1157734,4,1,1,1,2,1,3,1,1,2      },
132     {1158247,1,1,1,1,2,1,2,1,1,2      },
133     {1160476,2,1,1,1,2,1,3,1,1,2      },
134     {1164066,1,1,1,1,2,1,3,1,1,2      },
135     {1165297,2,1,1,2,2,1,1,1,1,2      },
136     {1165790,5,1,1,1,2,1,3,1,1,2      },
137     {1165926,9,6,9,2,10,6,2,9,10,4    },
138     {1166630,7,5,6,10,5,10,7,9,4,4    },
139     {1166654,10,3,5,1,10,5,3,10,2,4   },
140     {1167439,2,3,4,4,2,5,2,5,1,4      },
141     {1167471,4,1,2,1,2,1,3,1,1,2      },
142     {1168359,8,2,3,1,6,3,7,1,1,4      },
143     {1168736,10,10,10,10,10,1,8,8,8,4 },
144     {1169049,7,3,4,4,3,3,3,2,7,4      },
145     {1170419,10,10,10,8,2,10,4,1,1,4  },
146     {1170420,1,6,8,10,8,10,5,7,1,4    },
147     {1171710,1,1,1,1,2,1,2,3,1,2      },
148     {1171710,6,5,4,4,3,9,7,8,3,4      },
149     {1171795,1,3,1,2,2,2,5,3,2,2      },
150     {1171845,8,6,4,3,5,9,3,1,1,4      },
151     {1172152,10,3,3,10,2,10,7,3,3,4   },
152     {1173216,10,10,10,3,10,8,8,1,1,4  },
153     {1173235,3,3,2,1,2,3,3,1,1,2      },
154     {1173347,1,1,1,1,2,5,1,1,1,2      },
155     {1173347,8,3,3,1,2,2,3,2,1,2      },
156     {1173509,4,5,5,10,4,10,7,5,8,4    },
157     {1173514,1,1,1,1,4,3,1,1,1,2      },
158     {1173681,3,2,1,1,2,2,3,1,1,2      },
159     {1174057,1,1,2,2,2,1,3,1,1,2      },
160     {1174057,4,2,1,1,2,2,3,1,1,2      },
161     {1174131,10,10,10,2,10,10,5,3,3,4 },
162     {1174428,5,3,5,1,8,10,5,3,1,4     },
163     {1175937,5,4,6,7,9,7,8,10,1,4     },
164     {1176406,1,1,1,1,2,1,2,1,1,2      },
165     {1176881,7,5,3,7,4,10,7,5,5,4        }
166};
167    #endregion
168
169    private const int MaximumClasses = 20;
170    private const string ClassNamesParameterName = "ClassNames";
171    private const string MisclassificationMatrixParameterName = "MisClassificationMatrix";
172
173    public StringArray ClassNames {
174      get { return ClassNamesParameter.Value; }
175      protected set { ClassNamesParameter.Value = value; }
176    }
177    public IValueParameter<StringArray> ClassNamesParameter {
178      get { return (IValueParameter<StringArray>)Parameters[ClassNamesParameterName]; }
179    }
180
181    public DoubleMatrix MisclassificationMatrix {
182      get { return MisclassificationMatrixParameter.Value; }
183      protected set { MisclassificationMatrixParameter.Value = value; }
184    }
185    public IValueParameter<DoubleMatrix> MisclassificationMatrixParameter {
186      get { return (IValueParameter<DoubleMatrix>)Parameters[MisclassificationMatrixParameterName]; }
187    }
188
189    [Storable]
190    private List<double> sortedClassValues;
191    public IEnumerable<double> SortedClassValues {
192      get { return sortedClassValues; }
193    }
194    public int NumberOfClasses {
195      get { return sortedClassValues.Count; }
196    }
197
198    [StorableConstructor]
199    protected ClassificationProblemData(bool deserializing) : base(deserializing) { }
200    protected ClassificationProblemData(ClassificationProblemData original, Cloner cloner)
201      : base(original, cloner) {
202      RegisterParameterEvents();
203      UpdateClassValues();
204    }
205    public ClassificationProblemData()
206      : base(new Dataset(defaultInputs, defaultData), defaultInputs, defaultInputs[defaultInputs.Length - 1], 0, 60, 60, 120) {
207      Parameters.Add(new ValueParameter<StringArray>(ClassNamesParameterName, "An array of the names for all class values."));
208      Parameters.Add(new ValueParameter<DoubleMatrix>(MisclassificationMatrixParameterName, "A matrix that describles the penalties for misclassifaction between the single classes."));
209      sortedClassValues = new List<double>();
210
211      InputVariables.SetItemCheckedState(InputVariables[InputVariables.Count - 1], false);
212      RegisterParameterEvents();
213      UpdateClassValues();
214    }
215
216    public override IDeepCloneable Clone(Cloner cloner) {
217      return new ClassificationProblemData(this, cloner);
218    }
219
220    [StorableHook(HookType.AfterDeserialization)]
221    private void AfterDeserialization() {
222      RegisterParameterEvents();
223      RegisterParameterValueEvents();
224    }
225
226    public override void ImportFromFile(string fileName) {
227      var csvFileParser = new CsvFileParser();
228      csvFileParser.Parse(fileName);
229      suppressEvents = true;
230      Name = "Data imported from " + Path.GetFileName(fileName);
231      Dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
232      Dataset.Name = Path.GetFileName(fileName);
233      var variableNames = Dataset.VariableNames.Select(x => new StringValue(x).AsReadOnly()).ToList();
234      var validTargetVariables = variableNames.Select(variable => new { Variable = variable, DistinctValues = Dataset.GetVariableValues(variable.Value, 0, 50).Distinct().Count() })
235         .OrderBy(x => x.DistinctValues).Where(x => x.DistinctValues <= MaximumClasses).Select(x => x.Variable);
236      if (!validTargetVariables.Any())
237        throw new ArgumentException("Import of classification problem data was not successfull, because no target variable was found." +
238          " A target varialbe must have at most " + MaximumClasses + " distinct values to be applicable to classification.");
239
240      ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Clear();
241      foreach (var variableName in validTargetVariables)
242        ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Add(variableName);
243      TargetVariable = validTargetVariables.FirstOrDefault();
244
245      InputVariables = new CheckedItemList<StringValue>(variableNames).AsReadOnly();
246      InputVariables.SetItemCheckedState(validTargetVariables.First(), false);
247      int middle = (int)(csvFileParser.Rows * 0.5);
248      TrainingSamplesStart = new IntValue(0);
249      TrainingSamplesEnd = new IntValue(middle);
250      TestSamplesStart = new IntValue(middle);
251      TestSamplesEnd = new IntValue(csvFileParser.Rows);
252      UpdateClassValues();
253      suppressEvents = false;
254      OnProblemDataChanged(EventArgs.Empty);
255    }
256
257    private void UpdateClassValues() {
258      sortedClassValues = Dataset.GetVariableValues(TargetVariable.Value).Distinct().ToList();
259      sortedClassValues.Sort();
260      ResetMisclassificationMatrix();
261      UpdateClassNames();
262    }
263
264    private void UpdateClassNames() {
265      if (ClassNames != null) DeregisterParameterValueEvents();
266      StringArray array = new StringArray(NumberOfClasses);
267      int i = 0;
268      foreach (double classValue in SortedClassValues) {
269        array[i] = "Class " + classValue;
270        i++;
271      }
272      ClassNames = array;
273      RegisterParameterValueEvents();
274    }
275
276    private void RegisterParameterEvents() {
277      ClassNamesParameter.ValueChanged += new EventHandler(ClassNamesChanged);
278    }
279    private void RegisterParameterValueEvents() {
280      ClassNames.ItemChanged += new EventHandler<EventArgs<int>>(ClassNamesChanged);
281    }
282    private void DeregisterParameterValueEvents() {
283      ClassNames.ItemChanged -= new EventHandler<EventArgs<int>>(ClassNamesChanged);
284    }
285
286    private void ClassNamesChanged(object sender, EventArgs e) {
287      UpdateMisclassifciationMatrixHeaders();
288    }
289
290    private void ResetMisclassificationMatrix() {
291      double[,] matrix = new double[NumberOfClasses, NumberOfClasses];
292      for (int i = 0; i < NumberOfClasses; i++) {
293        for (int j = 0; j < NumberOfClasses; j++)
294          if (i != j) matrix[i, j] = 1;
295      }
296
297      if (MisclassificationMatrix == null)
298        MisclassificationMatrix = new DoubleMatrix(matrix);
299      if (MisclassificationMatrix.Rows != NumberOfClasses)
300        MisclassificationMatrix = new DoubleMatrix(matrix);
301      if (MisclassificationMatrix.Columns != NumberOfClasses)
302        MisclassificationMatrix = new DoubleMatrix(matrix);
303    }
304
305    private void UpdateMisclassifciationMatrixHeaders() {
306      MisclassificationMatrix.RowNames = ClassNames.Select(name => "Estimated " + name);
307      MisclassificationMatrix.ColumnNames = ClassNames.Select(name => "Actual " + name);
308    }
309  }
310}
Note: See TracBrowser for help on using the repository browser.