Changeset 16243


Ignore:
Timestamp:
10/19/18 13:27:06 (9 months ago)
Author:
mkommend
Message:

#2955: Added IsProblemDataCompatible and IsDatasetCompatible to all DataAnalysisModels.

Location:
trunk
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs

    r16086 r16243  
    262262
    263263
     264    public bool IsProblemDataCompatible(IRegressionProblemData problemData, out string errorMessage) {
     265      return RegressionModel.IsProblemDataCompatible(this, problemData, out errorMessage);
     266    }
     267
     268    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
     269      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
     270
     271      var regressionProblemData = problemData as IRegressionProblemData;
     272      if (regressionProblemData != null)
     273        return IsProblemDataCompatible(regressionProblemData, out errorMessage);
     274
     275      var classificationProblemData = problemData as IClassificationProblemData;
     276      if (classificationProblemData != null)
     277        return IsProblemDataCompatible(classificationProblemData, out errorMessage);
     278
     279      throw new ArgumentException("The problem data is not a regression nor a classification problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
     280    }
     281
    264282    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
    265283      return new NearestNeighbourRegressionSolution(this, new RegressionProblemData(problemData));
  • trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkEnsembleModel.cs

    r16168 r16243  
    130130    }
    131131
     132
     133    public bool IsProblemDataCompatible(IRegressionProblemData problemData, out string errorMessage) {
     134      return RegressionModel.IsProblemDataCompatible(this, problemData, out errorMessage);
     135    }
     136
     137    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
     138      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
     139
     140      var regressionProblemData = problemData as IRegressionProblemData;
     141      if (regressionProblemData != null)
     142        return IsProblemDataCompatible(regressionProblemData, out errorMessage);
     143
     144      var classificationProblemData = problemData as IClassificationProblemData;
     145      if (classificationProblemData != null)
     146        return IsProblemDataCompatible(classificationProblemData, out errorMessage);
     147
     148      throw new ArgumentException("The problem data is not a regression nor a classification problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
     149    }
     150
    132151    public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
    133152      return new NeuralNetworkEnsembleRegressionSolution(this, new RegressionEnsembleProblemData(problemData));
  • trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/NeuralNetwork/NeuralNetworkModel.cs

    r16168 r16243  
    134134    }
    135135
     136    public bool IsProblemDataCompatible(IRegressionProblemData problemData, out string errorMessage) {
     137      return RegressionModel.IsProblemDataCompatible(this, problemData, out errorMessage);
     138    }
     139
     140    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
     141      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
     142
     143      var regressionProblemData = problemData as IRegressionProblemData;
     144      if (regressionProblemData != null)
     145        return IsProblemDataCompatible(regressionProblemData, out errorMessage);
     146
     147      var classificationProblemData = problemData as IClassificationProblemData;
     148      if (classificationProblemData != null)
     149        return IsProblemDataCompatible(classificationProblemData, out errorMessage);
     150
     151      throw new ArgumentException("The problem data is not a regression nor a classification problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
     152    }
     153
    136154    public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
    137155      return new NeuralNetworkRegressionSolution(this, new RegressionProblemData(problemData));
  • trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModel.cs

    r15786 r16243  
    286286    }
    287287
     288    public bool IsProblemDataCompatible(IRegressionProblemData problemData, out string errorMessage) {
     289      return RegressionModel.IsProblemDataCompatible(this, problemData, out errorMessage);
     290    }
     291
     292    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
     293      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
     294
     295      var regressionProblemData = problemData as IRegressionProblemData;
     296      if (regressionProblemData != null)
     297        return IsProblemDataCompatible(regressionProblemData, out errorMessage);
     298
     299      var classificationProblemData = problemData as IClassificationProblemData;
     300      if (classificationProblemData != null)
     301        return IsProblemDataCompatible(classificationProblemData, out errorMessage);
     302
     303      throw new ArgumentException("The problem data is not a regression nor a classification problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
     304    }
     305
    288306    public static RandomForestModel CreateRegressionModel(IRegressionProblemData problemData, int nTrees, double r, double m, int seed,
    289307      out double rmsError, out double outOfBagRmsError, out double avgRelError, out double outOfBagAvgRelError) {
  • trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs

    r15854 r16243  
    126126      return new SupportVectorRegressionSolution(this, new RegressionProblemData(problemData));
    127127    }
    128     #endregion
     128
     129    public bool IsProblemDataCompatible(IRegressionProblemData problemData, out string errorMessage) {
     130      return RegressionModel.IsProblemDataCompatible(this, problemData, out errorMessage);
     131    }
     132    #endregion
     133
     134    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
     135      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
     136
     137      var regressionProblemData = problemData as IRegressionProblemData;
     138      if (regressionProblemData != null)
     139        return IsProblemDataCompatible(regressionProblemData, out errorMessage);
     140
     141      var classificationProblemData = problemData as IClassificationProblemData;
     142      if (classificationProblemData != null)
     143        return IsProblemDataCompatible(classificationProblemData, out errorMessage);
     144
     145      throw new ArgumentException("The problem data is not a regression nor a classification problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
     146    }
    129147
    130148    #region IClassificationModel Members
     
    153171    }
    154172    #endregion
     173
    155174    private IEnumerable<double> GetEstimatedValuesHelper(IDataset dataset, IEnumerable<int> rows) {
    156175      // calculate predictions for the currently requested rows
  • trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/kMeans/KMeansClusteringModel.cs

    r15583 r16243  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Drawing;
     
    3435  [StorableClass]
    3536  [Item("KMeansClusteringModel", "Represents a k-Means clustering model.")]
    36   public sealed class KMeansClusteringModel : NamedItem, IClusteringModel {
     37  public sealed class KMeansClusteringModel : DataAnalysisModel, IClusteringModel {
    3738    public static new Image StaticItemImage {
    3839      get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
    3940    }
    4041
    41     public IEnumerable<string> VariablesUsedForPrediction {
     42    public override IEnumerable<string> VariablesUsedForPrediction {
    4243      get { return allowedInputVariables; }
    4344    }
     
    8485    }
    8586
     87    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
     88      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
     89      return IsDatasetCompatible(problemData.Dataset, out errorMessage);
     90    }
     91
    8692
    8793    public IEnumerable<int> GetClusterValues(IDataset dataset, IEnumerable<int> rows) {
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicClassificationModel.cs

    r15583 r16243  
    7474    }
    7575
     76    public virtual bool IsProblemDataCompatible(IClassificationProblemData problemData, out string errorMessage) {
     77      return ClassificationModel.IsProblemDataCompatible(this, problemData, out errorMessage);
     78    }
     79
     80    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
     81      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
     82      var classificationProblemData = problemData as IClassificationProblemData;
     83      if (classificationProblemData == null)
     84        throw new ArgumentException("The problem data is not a regression problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
     85      return IsProblemDataCompatible(classificationProblemData, out errorMessage);
     86    }
     87
    7688    #region events
    7789    public event EventHandler TargetVariableChanged;
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionModel.cs

    r15583 r16243  
    8383    }
    8484
     85    public virtual bool IsProblemDataCompatible(IRegressionProblemData problemData, out string errorMessage) {
     86      return RegressionModel.IsProblemDataCompatible(this, problemData, out errorMessage);
     87    }
     88
     89    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
     90      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
     91      var regressionProblemData = problemData as IRegressionProblemData;
     92      if (regressionProblemData == null)
     93        throw new ArgumentException("The problem data is not a regression problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
     94      return IsProblemDataCompatible(regressionProblemData, out errorMessage);
     95    }
     96
    8597    #region events
    8698    public event EventHandler TargetVariableChanged;
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisModel.cs

    r15583 r16243  
    3434  /// </summary>
    3535  [StorableClass]
    36   public abstract class SymbolicDataAnalysisModel : NamedItem, ISymbolicDataAnalysisModel {
     36  public abstract class SymbolicDataAnalysisModel : DataAnalysisModel, ISymbolicDataAnalysisModel {
    3737    public static new Image StaticItemImage {
    3838      get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
     
    5959    }
    6060
    61     public IEnumerable<string> VariablesUsedForPrediction {
     61    public override IEnumerable<string> VariablesUsedForPrediction {
    6262      get {
    6363        var variables =
  • trunk/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Symbolic/Model.cs

    r15583 r16243  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    5354    }
    5455
     56    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
     57      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
     58      return IsDatasetCompatible(problemData.Dataset, out errorMessage);
     59    }
     60
     61
    5562    // Transforms an enumerable of real values to an enumerable of trading signals (buy(1) / hold(0) / sell(-1))
    5663    public static IEnumerable<double> GetSignals(IEnumerable<double> xs) {
  • trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationModel.cs

    r15583 r16243  
    6666    public abstract IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData);
    6767
     68    public virtual bool IsProblemDataCompatible(IClassificationProblemData problemData, out string errorMessage) {
     69      return IsProblemDataCompatible(this, problemData, out errorMessage);
     70    }
     71
     72    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
     73      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
     74      var classificationProblemData = problemData as IClassificationProblemData;
     75      if (classificationProblemData == null)
     76        throw new ArgumentException("The problem data is not a regression problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
     77      return IsProblemDataCompatible(classificationProblemData, out errorMessage);
     78    }
     79
     80    public static bool IsProblemDataCompatible(IClassificationModel model, IClassificationProblemData problemData, out string errorMessage) {
     81      if (model == null) throw new ArgumentNullException("model", "The provided model is null.");
     82      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
     83      errorMessage = string.Empty;
     84
     85      if (model.TargetVariable != problemData.TargetVariable)
     86        errorMessage = string.Format("The target variable of the model {0} does not match the target variable of the problemData {1}.", model.TargetVariable, problemData.TargetVariable);
     87
     88      var evaluationErrorMessage = string.Empty;
     89      var datasetCompatible = model.IsDatasetCompatible(problemData.Dataset, out evaluationErrorMessage);
     90      if (!datasetCompatible)
     91        errorMessage += evaluationErrorMessage;
     92
     93      return string.IsNullOrEmpty(errorMessage);
     94    }
     95
    6896    #region events
    6997    public event EventHandler TargetVariableChanged;
  • trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/ConstantModel.cs

    r15583 r16243  
    8383    }
    8484
     85    public virtual bool IsProblemDataCompatible(IClassificationProblemData problemData, out string errorMessage) {
     86      return ClassificationModel.IsProblemDataCompatible(this, problemData, out errorMessage);
     87    }
     88
     89    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
     90      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
     91
     92      var regressionProblemData = problemData as IRegressionProblemData;
     93      if (regressionProblemData != null)
     94        return IsProblemDataCompatible(regressionProblemData, out errorMessage);
     95
     96      var classificationProblemData = problemData as IClassificationProblemData;
     97      if (classificationProblemData != null)
     98        return IsProblemDataCompatible(classificationProblemData, out errorMessage);
     99
     100      throw new ArgumentException("The problem data is not a regression nor a classification problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
     101    }
     102
    85103    #region IStringConvertibleValue
    86104    public bool ReadOnly { get; private set; }
  • trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisModel.cs

    r15583 r16243  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using HeuristicLab.Common;
     
    3839
    3940    public abstract IEnumerable<string> VariablesUsedForPrediction { get; }
     41
     42    public virtual bool IsDatasetCompatible(IDataset dataset, out string errorMessage) {
     43      if (dataset == null) throw new ArgumentNullException("dataset", "The provided dataset is null.");
     44      return IsDatasetCompatible(this, dataset, out errorMessage);
     45    }
     46
     47    public abstract bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage);
     48
     49    public static bool IsDatasetCompatible(IDataAnalysisModel model, IDataset dataset, out string errorMessage) {
     50      if(model == null) throw new ArgumentNullException("model", "The provided model is null.");
     51      if (dataset == null) throw new ArgumentNullException("dataset", "The provided dataset is null.");
     52      errorMessage = string.Empty;
     53
     54      foreach (var variable in model.VariablesUsedForPrediction) {
     55        if (!dataset.ContainsVariable(variable)) {
     56          if (string.IsNullOrEmpty(errorMessage)) {
     57            errorMessage = "The following variables must be present in the dataset for model evaluation:";
     58          }
     59          errorMessage += System.Environment.NewLine + " " + variable;
     60        }
     61      }
     62
     63      return string.IsNullOrEmpty(errorMessage);
     64    }
    4065  }
    4166}
  • trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionModel.cs

    r15583 r16243  
    6767    public abstract IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData);
    6868
     69    public virtual bool IsProblemDataCompatible(IRegressionProblemData problemData, out string errorMessage) {
     70      return IsProblemDataCompatible(this, problemData, out errorMessage);
     71    }
     72
     73    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
     74      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
     75      var regressionProblemData = problemData as IRegressionProblemData;
     76      if (regressionProblemData == null)
     77        throw new ArgumentException("The problem data is not a regression problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
     78      return IsProblemDataCompatible(regressionProblemData, out errorMessage);
     79    }
     80
     81    public static bool IsProblemDataCompatible(IRegressionModel model, IRegressionProblemData problemData, out string errorMessage) {
     82      if (model == null) throw new ArgumentNullException("model", "The provided model is null.");
     83      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
     84      errorMessage = string.Empty;
     85
     86      if (model.TargetVariable != problemData.TargetVariable)
     87        errorMessage = string.Format("The target variable of the model {0} does not match the target variable of the problemData {1}.", model.TargetVariable, problemData.TargetVariable);
     88
     89      var evaluationErrorMessage = string.Empty;
     90      var datasetCompatible = model.IsDatasetCompatible(problemData.Dataset, out evaluationErrorMessage);
     91      if (!datasetCompatible)
     92        errorMessage += evaluationErrorMessage;
     93
     94      return string.IsNullOrEmpty(errorMessage);
     95    }
     96
    6997    #region events
    7098    public event EventHandler TargetVariableChanged;
  • trunk/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Classification/IClassificationModel.cs

    r15583 r16243  
    3131    IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows);
    3232    IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData);
     33    bool IsProblemDataCompatible(IClassificationProblemData problemData, out string errorMessage);
    3334    string TargetVariable { get; set; }
    3435    event EventHandler TargetVariableChanged;
  • trunk/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDataAnalysisModel.cs

    r15583 r16243  
    3030  public interface IDataAnalysisModel : INamedItem {
    3131    IEnumerable<string> VariablesUsedForPrediction { get; }
     32    bool IsDatasetCompatible(IDataset dataset, out string errorMessage);
     33    bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage);
    3234  }
    3335}
  • trunk/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Regression/IRegressionModel.cs

    r15583 r16243  
    3131    IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows);
    3232    IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData);
     33    bool IsProblemDataCompatible(IRegressionProblemData problemData, out string errorMessage);
    3334    string TargetVariable { get; set; }
    3435    event EventHandler TargetVariableChanged;
Note: See TracChangeset for help on using the changeset viewer.