Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8877 was 8877, checked in by mkommend, 11 years ago

#1942: Reintegrated branch for CSV import.

File size: 3.8 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;
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    // has to be implemented, if CanImportData is true
38    public TData ImportData(string path, ImportType type) {
39      TableFileParser csvFileParser = new TableFileParser();
40      csvFileParser.Parse(path);
41      return ImportData(path, type, csvFileParser);
42    }
43    public TData ImportData(string path, ImportType type, DataAnalysisCSVFormat csvFormat) {
44      TableFileParser csvFileParser = new TableFileParser();
45      csvFileParser.Parse(path, csvFormat.NumberFormatInfo, csvFormat.DateTimeFormatInfo, csvFormat.Separator);
46      return ImportData(path, type, csvFileParser);
47    }
48
49    protected abstract TData ImportData(string path, ImportType type, TableFileParser csvFileParser);
50
51    protected List<IList> Shuffle(List<IList> values) {
52      int count = values.First().Count;
53      int[] indices = Enumerable.Range(0, count).Shuffle(new FastRandom()).ToArray();
54      List<IList> shuffeledValues = new List<IList>(values.Count);
55      for (int col = 0; col < values.Count; col++) {
56
57        if (values[col] is List<double>)
58          shuffeledValues.Add(new List<double>());
59        else if (values[col] is List<DateTime>)
60          shuffeledValues.Add(new List<DateTime>());
61        else if (values[col] is List<string>)
62          shuffeledValues.Add(new List<string>());
63        else
64          throw new InvalidOperationException();
65
66        for (int i = 0; i < count; i++) {
67          shuffeledValues[col].Add(values[col][indices[i]]);
68        }
69      }
70      return shuffeledValues;
71    }
72
73    public override bool CanExportData {
74      get { return true; }
75    }
76    public override void ExportData(TData instance, string path) {
77      var strBuilder = new StringBuilder();
78      var colSep = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
79      foreach (var variable in instance.Dataset.VariableNames) {
80        strBuilder.Append(variable.Replace(colSep, String.Empty) + colSep);
81      }
82      strBuilder.Remove(strBuilder.Length - colSep.Length, colSep.Length);
83      strBuilder.AppendLine();
84
85      var dataset = instance.Dataset;
86
87      for (int i = 0; i < dataset.Rows; i++) {
88        for (int j = 0; j < dataset.Columns; j++) {
89          if (j > 0) strBuilder.Append(colSep);
90          strBuilder.Append(dataset.GetValue(i, j));
91        }
92        strBuilder.AppendLine();
93      }
94
95      using (var writer = new StreamWriter(path)) {
96        writer.Write(strBuilder);
97      }
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.