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