#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HeuristicLab.Common;
using HeuristicLab.Problems.DataAnalysis;
using HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api.Types;
using HeuristicLab.Random;
namespace HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab {
///
/// This is a RegressionInstanceProvider which imports data for a regression problem from a matlab script.
///
public class RegressionMatlabInstanceProvider : RegressionInstanceProvider {
public override string Name {
get {
return "Matlab Script";
}
}
public override string Description {
get { return ""; }
}
public override Uri WebLink {
get { return new Uri("http://dev.heuristiclab.com"); }
}
public override string ReferencePublication {
get { return ""; }
}
public override IEnumerable GetDataDescriptors() {
return new List();
}
public override IRegressionProblemData LoadData(IDataDescriptor descriptor) {
throw new NotImplementedException();
}
public override bool CanImportData {
get { return true; }
}
private Dataset GetValues(RegressionMatlabImportType type) {
if (type.Shuffle) {
type.Values = type.Values.Shuffle(new MersenneTwister());
}
return type.Values;
}
public IRegressionProblemData ImportData(string path, RegressionMatlabImportType type, IEnumerable variableNames) {
var dataset = GetValues(type);
var targetVar = type.TargetVariable;
// turn off input variables that are constant in the training partition
var rows = dataset.Rows;
var allowedInputVars = new List();
int trainingPartEnd = (rows * type.TrainingPercentage) / 100;
trainingPartEnd = trainingPartEnd > 0 ? trainingPartEnd : 1;
var trainingIndizes = Enumerable.Range(0, trainingPartEnd);
if (trainingIndizes.Count() >= 2) {
foreach (var variableName in dataset.DoubleVariables) {
if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0 && variableName != type.TargetVariable) {
allowedInputVars.Add(variableName);
}
}
} else {
allowedInputVars.AddRange(dataset.DoubleVariables.Where(x => !x.Equals(type.TargetVariable)));
}
IRegressionProblemData regressionData = new RegressionProblemData(dataset, allowedInputVars, targetVar);
regressionData.TrainingPartition.Start = trainingIndizes.First();
regressionData.TrainingPartition.End = trainingPartEnd;
regressionData.TestPartition.Start = trainingPartEnd;
regressionData.TestPartition.End = rows;
regressionData.Name = Path.GetFileName(path);
return regressionData;
}
}
}