Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.Instances.DataAnalysis/3.3/DataAnalysisInstanceProvider.cs @ 17252

Last change on this file since 17252 was 17252, checked in by abeham, 5 years ago

#2521: made QAP problem data readonly

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