Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Classification/ClassificationInstanceProvider.cs @ 7849

Last change on this file since 7849 was 7849, checked in by sforsten, 12 years ago

#1784:

  • added project HeuristicLab.Problem.Instances.DataAnalysis and deleted HeuristicLab.Problem.Instances.Classification and HeuristicLab.Problem.Instances.Regression
  • buttons are now big enough for the icons
File size: 3.9 KB
Line 
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
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Globalization;
26using System.IO;
27using System.Linq;
28using System.Text;
29using HeuristicLab.Problems.DataAnalysis;
30
31namespace HeuristicLab.Problems.Instances.DataAnalysis {
32  public abstract class ClassificationInstanceProvider : IProblemInstanceProvider<IClassificationProblemData> {
33    public IClassificationProblemData LoadData(string path) {
34      NumberFormatInfo numberFormat;
35      DateTimeFormatInfo dateFormat;
36      char separator;
37      TableFileParser.DetermineFileFormat(new FileStream(path, FileMode.Open), out numberFormat, out dateFormat, out separator);
38
39      IClassificationProblemData claData = LoadData(new FileStream(path, FileMode.Open), numberFormat, dateFormat, separator);
40      int pos = path.LastIndexOf('\\');
41      if (pos < 0)
42        claData.Name = path;
43      else {
44        pos++;
45        claData.Name = path.Substring(pos, path.Length - pos);
46      }
47
48      return claData;
49    }
50
51    protected IClassificationProblemData LoadData(Stream stream, NumberFormatInfo numberFormat, DateTimeFormatInfo dateFormat, char separator) {
52      TableFileParser csvFileParser = new TableFileParser();
53
54      csvFileParser.Parse(stream, numberFormat, dateFormat, separator);
55
56      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
57      string targetVar = csvFileParser.VariableNames.Last();
58      IEnumerable<string> allowedInputVars = csvFileParser.VariableNames.Where(x => !x.Equals(targetVar));
59
60      ClassificationProblemData claData = new ClassificationProblemData(dataset, allowedInputVars, targetVar);
61
62      int trainingPartEnd = csvFileParser.Rows * 2 / 3;
63      claData.TrainingPartition.Start = 0;
64      claData.TrainingPartition.End = trainingPartEnd;
65      claData.TestPartition.Start = trainingPartEnd;
66      claData.TestPartition.End = csvFileParser.Rows;
67      return claData;
68    }
69
70    public void SaveData(IClassificationProblemData instance, string path) {
71      StringBuilder strBuilder = new StringBuilder();
72
73      foreach (var variable in instance.InputVariables) {
74        strBuilder.Append(variable + ";");
75      }
76      strBuilder.Remove(strBuilder.Length - 1, 1);
77      strBuilder.AppendLine();
78
79      Dataset dataset = instance.Dataset;
80
81      for (int i = 0; i < dataset.Rows; i++) {
82        for (int j = 0; j < dataset.Columns; j++) {
83          strBuilder.Append(dataset.GetValue(i, j) + ";");
84        }
85        strBuilder.Remove(strBuilder.Length - 1, 1);
86        strBuilder.AppendLine();
87      }
88
89      using (StreamWriter writer = new StreamWriter(path)) {
90        writer.Write(strBuilder);
91      }
92    }
93
94    public abstract IEnumerable<IDataDescriptor> GetDataDescriptors();
95    public abstract IClassificationProblemData LoadData(IDataDescriptor descriptor);
96
97    public abstract string Name { get; }
98    public abstract string Description { get; }
99    public abstract Uri WebLink { get; }
100    public abstract string ReferencePublication { get; }
101  }
102}
Note: See TracBrowser for help on using the repository browser.