Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/14/12 15:21:28 (13 years ago)
Author:
sforsten
Message:

#1784: changes have been applied, according to the review comments of mkommend

Location:
branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Classification/3.4
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Classification/3.4/ArtificialClassificationDataDescriptor.cs

    r7759 r7805  
    3636    protected abstract int TestPartitionEnd { get; }
    3737
    38     public ClassificationProblemData GenerateClassificationData() {
     38    public IClassificationProblemData GenerateClassificationData() {
    3939      Dataset dataset = new Dataset(InputVariables, this.GenerateValues());
    4040
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Classification/3.4/ArtificialClassificationInstanceProvider.cs

    r7759 r7805  
    2424namespace HeuristicLab.Problems.Instances.Classification {
    2525  public abstract class ArtificialClassificationInstanceProvider : ClassificationInstanceProvider {
    26     public override ClassificationProblemData LoadData(IDataDescriptor descriptor) {
     26    public override IClassificationProblemData LoadData(IDataDescriptor descriptor) {
    2727      return ((ArtificialClassificationDataDescriptor)descriptor).GenerateClassificationData();
    2828    }
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Classification/3.4/ClassificationInstanceProvider.cs

    r7759 r7805  
    2323using System.Collections;
    2424using System.Collections.Generic;
     25using System.Globalization;
    2526using System.IO;
    2627using System.Linq;
     
    2930
    3031namespace HeuristicLab.Problems.Instances.Classification {
    31   public abstract class ClassificationInstanceProvider : IProblemInstanceProvider<ClassificationProblemData> {
    32     public ClassificationProblemData LoadData(string path) {
    33       TableFileParser csvFileParser = new TableFileParser();
    34       csvFileParser.Parse(path);
     32  public abstract class ClassificationInstanceProvider : IProblemInstanceProvider<IClassificationProblemData> {
     33    public IClassificationProblemData LoadData(string path) {
     34      NumberFormatInfo numberFormat;
     35      DateTimeFormatInfo dateFormat;
     36      char separator;
     37      TableFileParser.DetermineFileFormat(new FileStream(path, FileMode.Open), out numberFormat, out dateFormat, out separator);
    3538
    36       Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
    37       string targetVar = csvFileParser.VariableNames.Last();
    38       IEnumerable<string> allowedInputVars = csvFileParser.VariableNames.Where(x => !x.Equals(targetVar));
    39 
    40       ClassificationProblemData claData = new ClassificationProblemData(dataset, allowedInputVars, targetVar);
     39      IClassificationProblemData claData = LoadData(new FileStream(path, FileMode.Open), numberFormat, dateFormat, separator);
    4140      int pos = path.LastIndexOf('\\');
    4241      if (pos < 0)
     
    4645        claData.Name = path.Substring(pos, path.Length - pos);
    4746      }
     47
     48      return claData;
     49    }
     50
     51    protected IClassificationProblemData LoadData(Stream stream, NumberFormatInfo numberFormat, DateTimeFormatInfo dateFormat, char separator) {
     52      TableFileParser csvFileParser = new TableFileParser();
     53
     54      csvFileParser.Parse(stream, numberFormat, dateFormat, separator);
     55
     56      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
     57      string targetVar = csvFileParser.VariableNames.Last();
     58      IEnumerable<string> allowedInputVars = csvFileParser.VariableNames.Where(x => !x.Equals(targetVar));
     59
     60      ClassificationProblemData claData = new ClassificationProblemData(dataset, allowedInputVars, targetVar);
    4861
    4962      int trainingPartEnd = csvFileParser.Rows * 2 / 3;
     
    5568    }
    5669
    57     public void SaveData(ClassificationProblemData instance, string path) {
     70    public void SaveData(IClassificationProblemData instance, string path) {
    5871      StringBuilder strBuilder = new StringBuilder();
    5972
     
    8093
    8194    public abstract IEnumerable<IDataDescriptor> GetDataDescriptors();
    82     public abstract ClassificationProblemData LoadData(IDataDescriptor descriptor);
     95    public abstract IClassificationProblemData LoadData(IDataDescriptor descriptor);
    8396
    8497    public abstract string Name { get; }
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Classification/3.4/RealWorld/RealWorldInstanceProvider.cs

    r7758 r7805  
    5252      if (!String.IsNullOrEmpty(solutionsArchiveName)) {
    5353        using (var solutionsZipFile = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
    54           foreach (var entry in GetZipContents(solutionsZipFile).OrderBy(x => x)) {
     54          IList<string> entries = new List<string>();
     55          ZipEntry curEntry;
     56          while ((curEntry = solutionsZipFile.GetNextEntry()) != null) {
     57            entries.Add(curEntry.Name);
     58          }
     59          foreach (var entry in entries.OrderBy(x => x)) {
    5560            string prettyName = Path.GetFileNameWithoutExtension(entry);
    5661            IDataDescriptor desc = descriptorList.Where(x => x.Name.Equals(prettyName)).FirstOrDefault();
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Classification/3.4/ResourceClassificationInstanceProvider.cs

    r7759 r7805  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Globalization;
    2425using System.IO;
    2526using System.Linq;
     
    3839      if (!String.IsNullOrEmpty(solutionsArchiveName)) {
    3940        using (var solutionsZipFile = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
    40           foreach (var entry in GetZipContents(solutionsZipFile).OrderBy(x => x))
     41          IList<string> entries = new List<string>();
     42          ZipEntry curEntry;
     43          while ((curEntry = solutionsZipFile.GetNextEntry()) != null) {
     44            entries.Add(curEntry.Name);
     45          }
     46          foreach (var entry in entries.OrderBy(x => x)) {
    4147            yield return new ResourceClassificationDataDescriptor(Path.GetFileNameWithoutExtension(entry), Description, entry);
     48          }
    4249        }
    4350      }
    4451    }
    4552
    46     public override ClassificationProblemData LoadData(IDataDescriptor id) {
     53    public override IClassificationProblemData LoadData(IDataDescriptor id) {
    4754      var descriptor = (ResourceClassificationDataDescriptor)id;
    4855
    49       ClassificationProblemData claData = LoadData(GetTempFileForResource(descriptor.ResourceName));
    50       claData.Name = descriptor.Name;
    51       claData.Description = descriptor.Description;
     56      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
     57      using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
     58        var entry = instancesZipFile.GetEntry(descriptor.ResourceName);
     59        NumberFormatInfo numberFormat;
     60        DateTimeFormatInfo dateFormat;
     61        char separator;
     62        using (Stream stream = instancesZipFile.GetInputStream(entry)) {
     63          TableFileParser.DetermineFileFormat(stream, out numberFormat, out dateFormat, out separator);
     64        }
    5265
    53       return claData;
     66        IClassificationProblemData claData;
     67        using (Stream stream = instancesZipFile.GetInputStream(entry)) {
     68          claData = LoadData(stream, numberFormat, dateFormat, separator);
     69        }
     70        claData.Name = descriptor.Name;
     71        claData.Description = descriptor.Description;
     72        return claData;
     73      }
    5474    }
    5575
     
    5878              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
    5979    }
    60 
    61     protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
    62       ZipEntry entry;
    63       while ((entry = zipFile.GetNextEntry()) != null) {
    64         yield return entry.Name;
    65       }
    66     }
    67 
    68     private string GetTempFileForResource(string resourceName) {
    69       var instanceArchiveName = GetResourceName(FileName + @"\.zip");
    70       using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
    71         var entry = instancesZipFile.GetEntry(resourceName);
    72         string path = Path.GetTempFileName();
    73         using (var stream = instancesZipFile.GetInputStream(entry)) {
    74           WriteStreamToTempFile(stream, path);
    75         }
    76         return path;
    77       }
    78     }
    79 
    80     private void WriteStreamToTempFile(Stream stream, string path) {
    81       using (FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write)) {
    82         int cnt = 0;
    83         byte[] buffer = new byte[32 * 1024];
    84         while ((cnt = stream.Read(buffer, 0, buffer.Length)) != 0)
    85           output.Write(buffer, 0, cnt);
    86       }
    87     }
    8880  }
    8981}
Note: See TracChangeset for help on using the changeset viewer.