Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13847 was 13584, checked in by mkommend, 9 years ago

#2071: Added possibility to specify the encoding when importing files with the table file parser and changed export functionality to use the system's default encoding for creating CSV files.

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