Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5559


Ignore:
Timestamp:
02/24/11 16:44:24 (14 years ago)
Author:
mkommend
Message:

#1418: worked on different ProblemData classes.

Location:
branches/DataAnalysis Refactoring
Files:
1 added
13 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Evaluators/SymbolicDataAnalysisEvaluator.cs

    r5547 r5559  
    125125      if (count == 0) count = 1;
    126126      return RandomEnumerable.SampleRandomNumbers(seed, SamplesEnd.Value, SamplesStart.Value, count)
    127         .Where(i => i < ProblemDataParameter.ActualValue.TestSamplesStart || ProblemDataParameter.ActualValue.TestSamplesEnd <= i);
     127        .Where(i => i < ProblemDataParameter.ActualValue.TestPartitionStart || ProblemDataParameter.ActualValue.TestPartitionEnd <= i);
    128128    }
    129129  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/DataAnalysisProblemData.cs

    r5554 r5559  
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using HeuristicLab.Collections;
    2625using HeuristicLab.Common;
    2726using HeuristicLab.Core;
    28 using HeuristicLab.Data;
    29 using HeuristicLab.Parameters;
    3027using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3128
    3229namespace HeuristicLab.Problems.DataAnalysis {
    3330  [StorableClass]
    34   public abstract class DataAnalysisProblemData : ParameterizedNamedItem,
    35     IDataAnalysisProblemData {
    36     private const string DatasetParameterName = "Dataset";
    37     private const string InputVariablesParameterName = "Input variables";
    38     private const string TrainingSamplesStartParameterName = "Training partition start";
    39     private const string TrainingSamplesEndParameterName = "Training partition end";
    40     private const string TestSamplesStartParameterName = "Test partition start";
    41     private const string TestSamplesEndParameterName = "Test partition end";
    42 
    43     #region parameter properties
    44     public IValueParameter<Dataset> DatasetParameter {
    45       get { return (IValueParameter<Dataset>)Parameters[DatasetParameterName]; }
    46     }
    47     public IValueParameter<ICheckedItemList<StringValue>> InputVariablesParameter {
    48       get { return (IValueParameter<ICheckedItemList<StringValue>>)Parameters[InputVariablesParameterName]; }
    49     }
    50     public IValueParameter<IntValue> TrainingSamplesStartParameter {
    51       get { return (IValueParameter<IntValue>)Parameters[TrainingSamplesStartParameterName]; }
    52     }
    53     public IValueParameter<IntValue> TrainingSamplesEndParameter {
    54       get { return (IValueParameter<IntValue>)Parameters[TrainingSamplesEndParameterName]; }
    55     }
    56     public IValueParameter<IntValue> TestSamplesStartParameter {
    57       get { return (IValueParameter<IntValue>)Parameters[TestSamplesStartParameterName]; }
    58     }
    59     public IValueParameter<IntValue> TestSamplesEndParameter {
    60       get { return (IValueParameter<IntValue>)Parameters[TestSamplesEndParameterName]; }
    61     }
    62     #endregion
    63 
     31  public abstract class DataAnalysisProblemData : NamedItem, IDataAnalysisProblemData {
    6432    #region propeties
     33    [Storable]
     34    private Dataset dataset;
    6535    public Dataset Dataset {
    66       get { return DatasetParameter.Value; }
    67     }
    68     public IEnumerable<string> AllowedInputVariables {
    69       get { return InputVariablesParameter.Value.CheckedItems.Select(i => i.Value.Value); }
     36      get { return dataset; }
    7037    }
    7138
    72     public int TrainingSamplesStart {
    73       get { return TrainingSamplesStartParameter.Value.Value; }
    74       set { TrainingSamplesStartParameter.Value.Value = value; }
    75     }
    76     public int TrainingSamplesEnd {
    77       get { return TrainingSamplesEndParameter.Value.Value; }
    78       set { TrainingSamplesEndParameter.Value.Value = value; }
     39    [Storable]
     40    private HashSet<string> allowedInputVariables;
     41    public IEnumerable<string> AllowedInputVariables {
     42      get { return allowedInputVariables; }
    7943    }
    8044
    81     public int TestSamplesStart {
    82       get { return TestSamplesStartParameter.Value.Value; }
    83       set { TestSamplesStartParameter.Value.Value = value; }
     45    [Storable]
     46    private int trainingPartitionStart;
     47    public int TrainingPartitionStart {
     48      get { return trainingPartitionStart; }
     49      set {
     50        if (0 < value || value > dataset.Rows)
     51          throw new ArgumentException(string.Format("The training partition start must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
     52        if (trainingPartitionStart != value) {
     53          trainingPartitionStart = value;
     54          OnChanged();
     55        }
     56      }
    8457    }
    85     public int TestSamplesEnd {
    86       get { return TestSamplesEndParameter.Value.Value; }
    87       set { TestSamplesEndParameter.Value.Value = value; }
     58    [Storable]
     59    private int trainingPartitionEnd;
     60    public int TrainingPartitionEnd {
     61      get { return trainingPartitionEnd; }
     62      set {
     63        if (0 < value || value > dataset.Rows)
     64          throw new ArgumentException(string.Format("The training partition end must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
     65        if (trainingPartitionEnd != value) {
     66          trainingPartitionEnd = value;
     67          OnChanged();
     68        }
     69      }
     70    }
     71
     72    [Storable]
     73    private int testPartitionStart;
     74    public int TestPartitionStart {
     75      get { return testPartitionStart; }
     76      set {
     77        if (0 < value || value > dataset.Rows)
     78          throw new ArgumentException(string.Format("The test partition start must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
     79        if (testPartitionStart != value) {
     80          testPartitionStart = value;
     81          OnChanged();
     82        }
     83      }
     84    }
     85    [Storable]
     86    private int testPartitionEnd;
     87    public int TestPartitionEnd {
     88      get { return testPartitionEnd; }
     89      set {
     90        if (0 < value || value > dataset.Rows)
     91          throw new ArgumentException(string.Format("The test partition end must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
     92        if (testPartitionEnd != value) {
     93          testPartitionEnd = value;
     94          OnChanged();
     95        }
     96      }
    8897    }
    8998
    9099    public IEnumerable<int> TrainingIndizes {
    91100      get {
    92         return Enumerable.Range(TrainingSamplesStart, TrainingSamplesEnd - TrainingSamplesStart)
    93                          .Where(i => i >= 0 && i < Dataset.Rows && (i < TestSamplesStart || TestSamplesEnd <= i));
     101        return Enumerable.Range(TrainingPartitionStart, TrainingPartitionEnd - TrainingPartitionStart)
     102                         .Where(i => i >= 0 && i < Dataset.Rows && (i < TestPartitionStart || TestPartitionEnd <= i));
    94103      }
    95104    }
    96105    public IEnumerable<int> TestIndizes {
    97106      get {
    98         return Enumerable.Range(TestSamplesStart, TestSamplesEnd - TestSamplesStart)
     107        return Enumerable.Range(TestPartitionStart, TestPartitionEnd - TestPartitionStart)
    99108           .Where(i => i >= 0 && i < Dataset.Rows);
    100109      }
     
    102111    #endregion
    103112
    104     protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner)
    105       : base(original, cloner) {
    106       RegisterEventHandlers();
    107     }
     113    protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner) : base(original, cloner) { }
    108114    [StorableConstructor]
    109115    protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
     116
    110117    protected DataAnalysisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables) {
     118      if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
     119      if (allowedInputVariables == null) throw new ArgumentNullException("The allowedInputVariables must not be null.");
     120
    111121      if (allowedInputVariables.Except(dataset.VariableNames).Any())
    112122        throw new ArgumentException("All allowed input variables must be present in the dataset.");
    113123
    114       ICheckedItemList<StringValue> inputVariableList = new CheckedItemList<StringValue>(dataset.VariableNames.Select(x => new StringValue(x))).AsReadOnly();
    115       foreach (StringValue inputVariable in inputVariableList) {
    116         inputVariableList.SetItemCheckedState(inputVariable, allowedInputVariables.Contains(inputVariable.Value));
    117       }
    118 
    119       Parameters.Add(new ValueParameter<Dataset>(DatasetParameterName, dataset));
    120       Parameters.Add(new ValueParameter<ICheckedItemList<StringValue>>(InputVariablesParameterName, inputVariableList));
    121       Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesStartParameterName, new IntValue(0)));
    122       Parameters.Add(new ValueParameter<IntValue>(TrainingSamplesEndParameterName, new IntValue(dataset.Rows)));
    123       Parameters.Add(new ValueParameter<IntValue>(TestSamplesStartParameterName, new IntValue(dataset.Rows)));
    124       Parameters.Add(new ValueParameter<IntValue>(TestSamplesEndParameterName, new IntValue(dataset.Rows)));
    125 
    126       RegisterEventHandlers();
     124      this.dataset = dataset;
     125      allowedInputVariables = new HashSet<string>(allowedInputVariables);
     126      trainingPartitionStart = 0;
     127      trainingPartitionEnd = dataset.Rows / 2;
     128      testPartitionStart = dataset.Rows / 2;
     129      testPartitionEnd = dataset.Rows;
    127130    }
    128131
    129     [StorableHook(HookType.AfterDeserialization)]
    130     private void AfterDeserialization() {
    131       RegisterEventHandlers();
     132    public bool AddAllowedInputVariable(string inputVariable) {
     133      if (!Dataset.VariableNames.Contains(inputVariable))
     134        throw new ArgumentException("The allowed input variable must be present in the dataset.");
     135      if (allowedInputVariables.Contains(inputVariable)) return false;
     136
     137      allowedInputVariables.Add(inputVariable);
     138      return true;
    132139    }
     140    public bool RemoveAllowedInputVariable(string inputVariable) {
     141      if (!Dataset.VariableNames.Contains(inputVariable))
     142        throw new ArgumentException("The allowed input variable must be present in the dataset.");
     143      if (!allowedInputVariables.Contains(inputVariable)) return false;
    133144
    134     #region changed event propagation
    135     private void RegisterEventHandlers() {
    136       DatasetParameter.ValueChanged += new EventHandler(Dataset_Changed);
    137       TrainingSamplesStartParameter.ValueChanged += new EventHandler(PartitionParameter_ValueChanged);
    138       TrainingSamplesEndParameter.ValueChanged += new EventHandler(PartitionParameter_ValueChanged);
    139       TestSamplesStartParameter.ValueChanged += new EventHandler(PartitionParameter_ValueChanged);
    140       TestSamplesEndParameter.ValueChanged += new EventHandler(PartitionParameter_ValueChanged);
    141 
    142       InputVariablesParameter.ValueChanged += new EventHandler(InputVariablesParameter_ValueChanged);
    143       RegisterInputVariablesParameterValueEventHandlers();
    144 
    145       TrainingSamplesStartParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged);
    146       TrainingSamplesEndParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged);
    147       TestSamplesStartParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged);
    148       TestSamplesEndParameter.Value.ValueChanged += new EventHandler(Partitions_ValueChanged);
    149     }
    150     private void RegisterInputVariablesParameterValueEventHandlers() {
    151       InputVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_Changed);
    152       InputVariablesParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_Changed);
    153       InputVariablesParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_Changed);
    154       InputVariablesParameter.Value.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_Changed);
    155       InputVariablesParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_Changed);
    156       InputVariablesParameter.Value.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<StringValue>>(InputVariables_Changed);
    157     }
    158 
    159     private void PartitionParameter_ValueChanged(object sender, EventArgs e) {
    160       IntValue value = (IntValue)sender;
    161       value.ValueChanged += new EventHandler(Partitions_ValueChanged);
    162       OnChanged();
    163     }
    164     private void InputVariablesParameter_ValueChanged(object sender, EventArgs e) {
    165       RegisterInputVariablesParameterValueEventHandlers();
    166     }
    167 
    168     private void Partitions_ValueChanged(object sender, EventArgs e) {
    169       OnChanged();
    170     }
    171     private void InputVariables_Changed(object sender, CollectionItemsChangedEventArgs<IndexedItem<StringValue>> e) {
    172       OnChanged();
    173     }
    174     private void Dataset_Changed(object sender, EventArgs e) {
    175       OnChanged();
     145      allowedInputVariables.Remove(inputVariable);
     146      return true;
    176147    }
    177148
     
    181152      if (listeners != null) listeners(this, EventArgs.Empty);
    182153    }
    183     #endregion
    184154  }
    185155}
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs

    r5554 r5559  
    3636    private Dataset(Dataset original, Cloner cloner)
    3737      : base(original, cloner) {
    38       variableNames = original.variableNames;
     38      variableNameToVariableIndexMapping = original.variableNameToVariableIndexMapping;
    3939      data = original.data;
    4040    }
     
    5454
    5555
    56     private Dictionary<string, int> variableNames;
     56    private Dictionary<string, int> variableNameToVariableIndexMapping;
     57    private Dictionary<int, string> variableIndexToVariableNameMapping;
    5758    [Storable]
    5859    public IEnumerable<string> VariableNames {
    59       get { return variableNames.Keys; }
     60      get { return variableNameToVariableIndexMapping.Keys; }
    6061      private set {
    61         if (variableNames != null) throw new InvalidOperationException("VariableNames can only be set once.");
    62         this.variableNames = new Dictionary<string, int>();
     62        if (variableNameToVariableIndexMapping != null) throw new InvalidOperationException("VariableNames can only be set once.");
     63        this.variableNameToVariableIndexMapping = new Dictionary<string, int>();
    6364        int i = 0;
    6465        foreach (string variableName in value) {
    65           this.variableNames.Add(variableName, i);
     66          this.variableNameToVariableIndexMapping.Add(variableName, i);
    6667          i++;
    6768        }
     69        variableNameToVariableIndexMapping = variableIndexToVariableNameMapping.ToDictionary(x => x.Value, x => x.Key);
    6870      }
    6971    }
     
    127129
    128130    public string GetVariableName(int variableIndex) {
    129       return VariableNames.ElementAt(variableIndex);
     131      try {
     132        return variableIndexToVariableNameMapping[variableIndex];
     133      }
     134      catch (KeyNotFoundException ex) {
     135        throw new ArgumentException("The variable index " + variableIndex + " was not found.", ex);
     136      }
    130137    }
    131138    public int GetVariableIndex(string variableName) {
    132139      try {
    133         return variableNames[variableName];
     140        return variableNameToVariableIndexMapping[variableName];
    134141      }
    135142      catch (KeyNotFoundException ex) {
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/HeuristicLab.Problems.DataAnalysis-3.4.csproj

    r5540 r5559  
    108108  </ItemGroup>
    109109  <ItemGroup>
     110    <Compile Include="ClassificationProblemData.cs" />
    110111    <Compile Include="RegressionProblemData.cs" />
    111112    <Compile Include="DataAnalysisProblemData.cs" />
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Classification/IClassificationProblemData.cs

    r5501 r5559  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324namespace HeuristicLab.Problems.DataAnalysis {
    2425  public interface IClassificationProblemData : IDataAnalysisProblemData {
    25     string TargetVariable { get; }
    26     int NumberOfClasses { get; }
    27 
     26    string TargetVariable { get; set; }
    2827    IEnumerable<string> ClassNames { get; }
    2928    IEnumerable<double> ClassValues { get; }
     29    int Classes { get; }
    3030
    3131    string GetClassName(double classValue);
    3232    double GetClassValue(string className);
     33    void SetClassName(double classValue, string className);
    3334
    3435    double GetClassificationPenalty(string correctClass, string estimatedClass);
    3536    double GetClassificationPenalty(double correctClassValue, double estimatedClassValue);
     37    void SetClassificationPenalty(string correctClassName, string estimatedClassName, double penalty);
     38    void SetClassificationPenalty(double correctClassValue, double estimatedClassValue, double penalty);
     39
     40    event EventHandler ClassNamesChanged;
     41    event EventHandler ClassificationPenaltyChanged;
    3642  }
    3743}
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDataAnalysisProblemData.cs

    r5554 r5559  
    2929    IEnumerable<string> AllowedInputVariables { get; }
    3030
    31     int TrainingSamplesStart { get; set; }
    32     int TrainingSamplesEnd { get; set; }
    33     int TestSamplesStart { get; set; }
    34     int TestSamplesEnd { get; set; }
     31    bool AddAllowedInputVariable(string inputVariable);
     32    bool RemoveAllowedInputVariable(string inputVariable);
     33
     34    int TrainingPartitionStart { get; set; }
     35    int TrainingPartitionEnd { get; set; }
     36    int TestPartitionStart { get; set; }
     37    int TestPartitionEnd { get; set; }
    3538
    3639    IEnumerable<int> TrainingIndizes { get; }
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Regression/IRegressionProblemData.cs

    r5496 r5559  
    2222namespace HeuristicLab.Problems.DataAnalysis {
    2323  public interface IRegressionProblemData : IDataAnalysisProblemData {
    24     string TargetVariable { get; }
     24    string TargetVariable { get; set; }
    2525  }
    2626}
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/OnlineEvaluators/OnlineCovarianceEvaluator.cs

    r5507 r5559  
    7474      OnlineCovarianceEvaluator covarianceEvaluator = new OnlineCovarianceEvaluator();
    7575
    76       while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
     76      while (firstEnumerator.MoveNext() && secondEnumerator.MoveNext()) {
    7777        double estimated = secondEnumerator.Current;
    7878        double original = firstEnumerator.Current;
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/OnlineEvaluators/OnlineMeanAbsolutePercentageErrorEvaluator.cs

    r5507 r5559  
    7676      }
    7777
    78       if (secondEnumerator.MoveNext() || firstEnumerator.MoveNext()) {
     78      if (secondEnumerator.MoveNext() && firstEnumerator.MoveNext()) {
    7979        throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
    8080      } else {
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/OnlineEvaluators/OnlineMeanSquaredErrorEvaluator.cs

    r5500 r5559  
    6767      OnlineMeanSquaredErrorEvaluator mseEvaluator = new OnlineMeanSquaredErrorEvaluator();
    6868
    69       while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
     69      while (firstEnumerator.MoveNext() && secondEnumerator.MoveNext()) {
    7070        double estimated = secondEnumerator.Current;
    7171        double original = firstEnumerator.Current;
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/OnlineEvaluators/OnlineNormalizedMeanSquaredErrorEvaluator.cs

    r5507 r5559  
    6767      OnlineNormalizedMeanSquaredErrorEvaluator normalizedMSEEvaluator = new OnlineNormalizedMeanSquaredErrorEvaluator();
    6868
    69       while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
     69      while (firstEnumerator.MoveNext() && secondEnumerator.MoveNext()) {
    7070        double estimated = secondEnumerator.Current;
    7171        double original = firstEnumerator.Current;
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/OnlineEvaluators/OnlinePearsonsRSquaredEvaluator.cs

    r5500 r5559  
    7575      OnlinePearsonsRSquaredEvaluator rSquaredEvaluator = new OnlinePearsonsRSquaredEvaluator();
    7676
    77       while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
     77      while (firstEnumerator.MoveNext() && secondEnumerator.MoveNext()) {
    7878        double estimated = secondEnumerator.Current;
    7979        double original = firstEnumerator.Current;
  • branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis/3.4/RegressionProblemData.cs

    r5554 r5559  
    2222using System;
    2323using System.Collections.Generic;
     24using System.IO;
    2425using System.Linq;
    2526using HeuristicLab.Common;
    26 using HeuristicLab.Core;
    27 using HeuristicLab.Data;
    28 using HeuristicLab.Parameters;
    2927using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3028
    3129namespace HeuristicLab.Problems.DataAnalysis {
    3230  [StorableClass]
    33   public sealed class RegressionProblemData : DataAnalysisProblemData,
    34     IRegressionProblemData {
    35     private const string TargetVariableParameterName = "Target variable";
     31  public sealed class RegressionProblemData : DataAnalysisProblemData, IRegressionProblemData {
    3632
    3733    #region default data
     
    6965    static RegressionProblemData() {
    7066      defaultDataset = new Dataset(new string[] { "y", "x" }, kozaF1);
     67      defaultDataset.Name = "Fourth-order Polynomial Function Benchmark Dataset";
     68      defaultDataset.Description = "f(x) = x^4 + x^3 + x^2 + x^1";
    7169      defaultAllowedInputVariables = new List<string>() { "x" };
    7270      defaultTargetVariable = "y";
     
    7472    #endregion
    7573
    76     #region parameter properties
    77     public IValueParameter<StringValue> TargetVariableParameter {
    78       get { return (IValueParameter<StringValue>)Parameters[TargetVariableParameterName]; }
    79     }
    80     #endregion
    8174    #region propeties
     75    [Storable]
     76    private string targetVariable;
    8277    public string TargetVariable {
    83       get { return TargetVariableParameter.Value.Value; }
     78      get { return targetVariable; }
     79      set {
     80        if (!Dataset.VariableNames.Contains(value))
     81          throw new ArgumentException(string.Format("The target variable {0} is not present in the dataset", value));
     82        if (targetVariable != value) {
     83          targetVariable = value;
     84          OnChanged();
     85        }
     86      }
    8487    }
    8588    #endregion
     
    8790    [StorableConstructor]
    8891    private RegressionProblemData(bool deserializing) : base(deserializing) { }
    89     private RegressionProblemData(RegressionProblemData original, Cloner cloner)
    90       : base(original, cloner) {
    91       RegisterEventHandlers();
    92     }
     92    private RegressionProblemData(RegressionProblemData original, Cloner cloner) : base(original, cloner) { }
    9393    public override IDeepCloneable Clone(Cloner cloner) { return new RegressionProblemData(this, cloner); }
    9494
     
    9999    public RegressionProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable)
    100100      : base(dataset, allowedInputVariables) {
    101       if (!dataset.VariableNames.Contains(targetVariable))
    102         throw new ArgumentException("The target variable must be present in the dataset");
    103 
    104       ConstrainedValueParameter<StringValue> targetVariableParameter = new ConstrainedValueParameter<StringValue>(TargetVariableParameterName);
    105       foreach (var variableName in dataset.VariableNames) {
    106         StringValue targetVariableValue = new StringValue(variableName);
    107         targetVariableParameter.ValidValues.Add(targetVariableValue);
    108         if (variableName == targetVariable)
    109           targetVariableParameter.Value = targetVariableValue;
    110       }
    111 
    112       Parameters.Add(targetVariableParameter);
    113       RegisterEventHandlers();
     101      TargetVariable = targetVariable;
    114102    }
    115103
     104    public static RegressionProblemData ImportFromFile(string fileName) {
     105      TableFileParser csvFileParser = new TableFileParser();
     106      csvFileParser.Parse(fileName);
    116107
     108      Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
     109      dataset.Name = Path.GetFileName(fileName);
    117110
    118     [StorableHook(HookType.AfterDeserialization)]
    119     private void AfterDeserialization() {
    120       RegisterEventHandlers();
     111      RegressionProblemData problemData = new RegressionProblemData(dataset, dataset.VariableNames.Skip(1), dataset.VariableNames.First());
     112      problemData.Name = "Data imported from " + Path.GetFileName(fileName);
     113      return problemData;
    121114    }
    122 
    123     #region event propagation
    124     private void RegisterEventHandlers() {
    125       TargetVariableParameter.Value.ValueChanged += new EventHandler(TargetVariable_ValueChanged);
    126       TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
    127     }
    128 
    129     private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
    130       TargetVariableParameter.Value.ValueChanged += new EventHandler(TargetVariable_ValueChanged);
    131       OnChanged();
    132     }
    133 
    134     private void TargetVariable_ValueChanged(object sender, EventArgs e) {
    135       OnChanged();
    136     }
    137     #endregion
    138115  }
    139116}
Note: See TracChangeset for help on using the changeset viewer.