Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/DataAnalysisInstanceProvider.cs @ 9462

Last change on this file since 9462 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
30using HeuristicLab.Random;
31
32namespace HeuristicLab.Problems.Instances.DataAnalysis {
33  public abstract class DataAnalysisInstanceProvider<TData, ImportType> : ProblemInstanceProvider<TData>
34    where TData : class, IDataAnalysisProblemData
35    where ImportType : DataAnalysisImportType {
36
37
38    public TData ImportData(string path, ImportType type, DataAnalysisCSVFormat csvFormat) {
39      TableFileParser csvFileParser = new TableFileParser();
40      csvFileParser.Parse(path, csvFormat.NumberFormatInfo, csvFormat.DateTimeFormatInfo, csvFormat.Separator);
41      return ImportData(path, type, csvFileParser);
42    }
43
44    protected virtual TData ImportData(string path, ImportType type, TableFileParser csvFileParser) {
45      throw new NotSupportedException();
46    }
47
48    protected List<IList> Shuffle(List<IList> values) {
49      int count = values.First().Count;
50      int[] indices = Enumerable.Range(0, count).Shuffle(new FastRandom()).ToArray();
51      List<IList> shuffled = new List<IList>(values.Count);
52      for (int col = 0; col < values.Count; col++) {
53
54        if (values[col] is List<double>)
55          shuffled.Add(new List<double>());
56        else if (values[col] is List<DateTime>)
57          shuffled.Add(new List<DateTime>());
58        else if (values[col] is List<string>)
59          shuffled.Add(new List<string>());
60        else
61          throw new InvalidOperationException();
62
63        for (int i = 0; i < count; i++) {
64          shuffled[col].Add(values[col][indices[i]]);
65        }
66      }
67      return shuffled;
68    }
69
70    public override bool CanExportData {
71      get { return true; }
72    }
73    public override void ExportData(TData instance, string path) {
74      var strBuilder = new StringBuilder();
75      var colSep = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
76      foreach (var variable in instance.Dataset.VariableNames) {
77        strBuilder.Append(variable.Replace(colSep, String.Empty) + colSep);
78      }
79      strBuilder.Remove(strBuilder.Length - colSep.Length, colSep.Length);
80      strBuilder.AppendLine();
81
82      var dataset = instance.Dataset;
83
84      for (int i = 0; i < dataset.Rows; i++) {
85        for (int j = 0; j < dataset.Columns; j++) {
86          if (j > 0) strBuilder.Append(colSep);
87          strBuilder.Append(dataset.GetValue(i, j));
88        }
89        strBuilder.AppendLine();
90      }
91
92      using (var writer = new StreamWriter(path)) {
93        writer.Write(strBuilder);
94      }
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.