Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/03/12 16:46:35 (12 years ago)
Author:
gkronber
Message:

#1847: merged r8084:8205 from trunk into GP move operators branch

Location:
branches/GP-MoveOperators
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/GP-MoveOperators

  • branches/GP-MoveOperators/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/CSV/RegressionCSVInstanceProvider.cs

    r7860 r8206  
    2020#endregion
    2121
    22 
    2322using System;
    2423using System.Collections.Generic;
     24using System.IO;
     25using System.Linq;
     26using System.Text;
    2527using HeuristicLab.Problems.DataAnalysis;
     28
    2629namespace HeuristicLab.Problems.Instances.DataAnalysis {
    2730  public class RegressionCSVInstanceProvider : RegressionInstanceProvider {
    2831    public override string Name {
    29       get { return "CSV Problem Provider"; }
     32      get { return "Comma-separated Values File"; }
    3033    }
    3134    public override string Description {
     
    4447      return new List<IDataDescriptor>();
    4548    }
    46 
    4749    public override IRegressionProblemData LoadData(IDataDescriptor descriptor) {
    4850      throw new NotImplementedException();
    4951    }
     52
     53    public override bool CanImportData {
     54      get { return true; }
     55    }
     56    public override IRegressionProblemData ImportData(string path) {
     57      TableFileParser csvFileParser = new TableFileParser();
     58      csvFileParser.Parse(path);
     59
     60      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
     61      string targetVar = csvFileParser.VariableNames.Where(x => dataset.DoubleVariables.Contains(x)).Last();
     62
     63      IEnumerable<string> allowedInputVars = dataset.DoubleVariables.Where(x => !x.Equals(targetVar));
     64
     65      IRegressionProblemData regData = new RegressionProblemData(dataset, allowedInputVars, targetVar);
     66
     67      int trainingPartEnd = csvFileParser.Rows * 2 / 3;
     68      regData.TrainingPartition.Start = 0;
     69      regData.TrainingPartition.End = trainingPartEnd;
     70      regData.TestPartition.Start = trainingPartEnd;
     71      regData.TestPartition.End = csvFileParser.Rows;
     72
     73      int pos = path.LastIndexOf('\\');
     74      if (pos < 0)
     75        regData.Name = path;
     76      else {
     77        pos++;
     78        regData.Name = path.Substring(pos, path.Length - pos);
     79      }
     80      return regData;
     81    }
     82
     83    public override bool CanExportData {
     84      get { return true; }
     85    }
     86    public override void ExportData(IRegressionProblemData instance, string path) {
     87      StringBuilder strBuilder = new StringBuilder();
     88
     89      foreach (var variable in instance.InputVariables) {
     90        strBuilder.Append(variable + ";");
     91      }
     92      strBuilder.Remove(strBuilder.Length - 1, 1);
     93      strBuilder.AppendLine();
     94
     95      Dataset dataset = instance.Dataset;
     96
     97      for (int i = 0; i < dataset.Rows; i++) {
     98        for (int j = 0; j < dataset.Columns; j++) {
     99          strBuilder.Append(dataset.GetValue(i, j) + ";");
     100        }
     101        strBuilder.Remove(strBuilder.Length - 1, 1);
     102        strBuilder.AppendLine();
     103      }
     104
     105      using (StreamWriter writer = new StreamWriter(path)) {
     106        writer.Write(strBuilder);
     107      }
     108    }
    50109  }
    51110}
Note: See TracChangeset for help on using the changeset viewer.