Changeset 7805 for branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Classification
- Timestamp:
- 05/14/12 15:21:28 (13 years ago)
- 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 36 36 protected abstract int TestPartitionEnd { get; } 37 37 38 public ClassificationProblemData GenerateClassificationData() {38 public IClassificationProblemData GenerateClassificationData() { 39 39 Dataset dataset = new Dataset(InputVariables, this.GenerateValues()); 40 40 -
branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Classification/3.4/ArtificialClassificationInstanceProvider.cs
r7759 r7805 24 24 namespace HeuristicLab.Problems.Instances.Classification { 25 25 public abstract class ArtificialClassificationInstanceProvider : ClassificationInstanceProvider { 26 public override ClassificationProblemData LoadData(IDataDescriptor descriptor) {26 public override IClassificationProblemData LoadData(IDataDescriptor descriptor) { 27 27 return ((ArtificialClassificationDataDescriptor)descriptor).GenerateClassificationData(); 28 28 } -
branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Classification/3.4/ClassificationInstanceProvider.cs
r7759 r7805 23 23 using System.Collections; 24 24 using System.Collections.Generic; 25 using System.Globalization; 25 26 using System.IO; 26 27 using System.Linq; … … 29 30 30 31 namespace 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); 35 38 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); 41 40 int pos = path.LastIndexOf('\\'); 42 41 if (pos < 0) … … 46 45 claData.Name = path.Substring(pos, path.Length - pos); 47 46 } 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); 48 61 49 62 int trainingPartEnd = csvFileParser.Rows * 2 / 3; … … 55 68 } 56 69 57 public void SaveData( ClassificationProblemData instance, string path) {70 public void SaveData(IClassificationProblemData instance, string path) { 58 71 StringBuilder strBuilder = new StringBuilder(); 59 72 … … 80 93 81 94 public abstract IEnumerable<IDataDescriptor> GetDataDescriptors(); 82 public abstract ClassificationProblemData LoadData(IDataDescriptor descriptor);95 public abstract IClassificationProblemData LoadData(IDataDescriptor descriptor); 83 96 84 97 public abstract string Name { get; } -
branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Classification/3.4/RealWorld/RealWorldInstanceProvider.cs
r7758 r7805 52 52 if (!String.IsNullOrEmpty(solutionsArchiveName)) { 53 53 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)) { 55 60 string prettyName = Path.GetFileNameWithoutExtension(entry); 56 61 IDataDescriptor desc = descriptorList.Where(x => x.Name.Equals(prettyName)).FirstOrDefault(); -
branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Classification/3.4/ResourceClassificationInstanceProvider.cs
r7759 r7805 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Globalization; 24 25 using System.IO; 25 26 using System.Linq; … … 38 39 if (!String.IsNullOrEmpty(solutionsArchiveName)) { 39 40 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)) { 41 47 yield return new ResourceClassificationDataDescriptor(Path.GetFileNameWithoutExtension(entry), Description, entry); 48 } 42 49 } 43 50 } 44 51 } 45 52 46 public override ClassificationProblemData LoadData(IDataDescriptor id) {53 public override IClassificationProblemData LoadData(IDataDescriptor id) { 47 54 var descriptor = (ResourceClassificationDataDescriptor)id; 48 55 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 } 52 65 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 } 54 74 } 55 75 … … 58 78 .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault(); 59 79 } 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 }88 80 } 89 81 }
Note: See TracChangeset
for help on using the changeset viewer.