Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/15/18 12:07:16 (6 years ago)
Author:
gkronber
Message:

#2925 merged changes r15972:16382 from trunk to branch

Location:
branches/2925_AutoDiffForDynamicalModels
Files:
20 edited

Legend:

Unmodified
Added
Removed
  • branches/2925_AutoDiffForDynamicalModels

  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis

  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs

    r15829 r16386  
    116116
    117117    public ModifiableDataset ToModifiable() {
    118       var values = new List<IList>();
    119       foreach (var v in variableNames) {
    120         if (VariableHasType<double>(v)) {
    121           values.Add(new List<double>((IList<double>)variableValues[v]));
    122         } else if (VariableHasType<string>(v)) {
    123           values.Add(new List<string>((IList<string>)variableValues[v]));
    124         } else if (VariableHasType<DateTime>(v)) {
    125           values.Add(new List<DateTime>((IList<DateTime>)variableValues[v]));
    126         } else {
    127           throw new ArgumentException("Unknown variable type.");
    128         }
    129       }
    130       return new ModifiableDataset(variableNames, values);
     118      return new ModifiableDataset(variableNames, variableNames.Select(v => variableValues[v]), true);
    131119    }
    132120
     
    141129    }
    142130
    143     protected Dataset(Dataset dataset) : this(dataset.variableNames, dataset.variableValues.Values) { }
     131
    144132
    145133    #region Backwards compatible code, remove with 3.5
     
    179167      }
    180168    }
     169
     170    public bool ContainsVariable(string variableName) {
     171      return variableValues.ContainsKey(variableName);
     172    }
    181173    public IEnumerable<string> DoubleVariables {
    182174      get { return variableValues.Where(p => p.Value is IList<double>).Select(p => p.Key); }
     
    318310    #region IStringConvertibleMatrix Members
    319311    [Storable]
    320     protected int rows;
     312    private int rows;
    321313    public int Rows {
    322314      get { return rows; }
     315      protected set { rows = value; }
    323316    }
    324317    int IStringConvertibleMatrix.Rows {
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationModel.cs

    r15583 r16386  
    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;
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationProblemData.cs

    r15583 r16386  
    467467    }
    468468    #endregion
    469 
    470     protected override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
    471       if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
    472       IClassificationProblemData classificationProblemData = problemData as IClassificationProblemData;
    473       if (classificationProblemData == null)
    474         throw new ArgumentException("The problem data is no classification problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
    475 
    476       var returnValue = base.IsProblemDataCompatible(classificationProblemData, out errorMessage);
    477       //check targetVariable
    478       if (classificationProblemData.InputVariables.All(var => var.Value != TargetVariable)) {
    479         errorMessage = string.Format("The target variable {0} is not present in the new problem data.", TargetVariable)
    480                        + Environment.NewLine + errorMessage;
    481         return false;
    482       }
    483 
    484       var newClassValues = classificationProblemData.Dataset.GetDoubleValues(TargetVariable).Distinct().OrderBy(x => x);
    485       if (!newClassValues.SequenceEqual(ClassValues)) {
    486         errorMessage = errorMessage + string.Format("The class values differ in the provided classification problem data.");
    487         returnValue = false;
    488       }
    489 
    490       var newPositivieClassName = classificationProblemData.PositiveClass;
    491       if (newPositivieClassName != PositiveClass) {
    492         errorMessage = errorMessage + string.Format("The positive class differs in the provided classification problem data.");
    493         returnValue = false;
    494       }
    495 
    496       return returnValue;
    497     }
    498 
    499     public override void AdjustProblemDataProperties(IDataAnalysisProblemData problemData) {
    500       if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
    501       ClassificationProblemData classificationProblemData = problemData as ClassificationProblemData;
    502       if (classificationProblemData == null)
    503         throw new ArgumentException("The problem data is not a classification problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
    504 
    505       base.AdjustProblemDataProperties(problemData);
    506       TargetVariable = classificationProblemData.TargetVariable;
    507       for (int i = 0; i < classificationProblemData.ClassNames.Count(); i++)
    508         ClassNamesParameter.Value[i, 0] = classificationProblemData.ClassNames.ElementAt(i);
    509 
    510       PositiveClass = classificationProblemData.PositiveClass;
    511 
    512       for (int i = 0; i < Classes; i++) {
    513         for (int j = 0; j < Classes; j++) {
    514           ClassificationPenaltiesParameter.Value[i, j] = classificationProblemData.GetClassificationPenalty(ClassValuesCache[i], ClassValuesCache[j]);
    515         }
    516       }
    517     }
    518469  }
    519470}
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationSolutionBase.cs

    r15583 r16386  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    4445    public new IClassificationProblemData ProblemData {
    4546      get { return (IClassificationProblemData)base.ProblemData; }
    46       set { base.ProblemData = value; }
     47      set {
     48        if (value == null) throw new ArgumentNullException("The problemData must not be null.");
     49        string errorMessage = string.Empty;
     50        if (!Model.IsProblemDataCompatible(value, out errorMessage)) throw new ArgumentException(errorMessage);
     51
     52        base.ProblemData = value;
     53      }
    4754    }
    4855
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/ConstantModel.cs

    r15583 r16386  
    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; }
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisModel.cs

    r15583 r16386  
    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}
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs

    r15583 r16386  
    163163
    164164      var variables = dataset.VariableNames.Where(variable => dataset.VariableHasType<double>(variable) || dataset.VariableHasType<string>(variable));
    165       var inputVariables = new CheckedItemList<StringValue>(variables.Select(x => new StringValue(x)));
     165      var inputVariables = new CheckedItemList<StringValue>(variables.Select(x => new StringValue(x).AsReadOnly()));
    166166      foreach (StringValue x in inputVariables)
    167167        inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
     
    207207      if (listeners != null) listeners(this, EventArgs.Empty);
    208208    }
    209 
    210     protected virtual bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
    211       errorMessage = string.Empty;
    212       if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
    213 
    214       //check allowed input variables
    215       StringBuilder message = new StringBuilder();
    216       var variables = new HashSet<string>(problemData.InputVariables.Select(x => x.Value));
    217       foreach (var item in AllowedInputVariables) {
    218         if (!variables.Contains(item))
    219           message.AppendLine("Input variable '" + item + "' is not present in the new problem data.");
    220       }
    221 
    222       if (message.Length != 0) {
    223         errorMessage = message.ToString();
    224         return false;
    225       }
    226       return true;
    227 
    228     }
    229 
    230     public virtual void AdjustProblemDataProperties(IDataAnalysisProblemData problemData) {
    231       DataAnalysisProblemData data = problemData as DataAnalysisProblemData;
    232       if (data == null) throw new ArgumentException("The problem data is not a data analysis problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
    233 
    234       string errorMessage;
    235       if (!data.IsProblemDataCompatible(this, out errorMessage)) {
    236         throw new InvalidOperationException(errorMessage);
    237       }
    238 
    239       foreach (var inputVariable in InputVariables) {
    240         var variable = data.InputVariables.FirstOrDefault(i => i.Value == inputVariable.Value);
    241         InputVariables.SetItemCheckedState(inputVariable, variable != null && data.InputVariables.ItemChecked(variable));
    242       }
    243     }
    244209  }
    245210}
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisSolution.cs

    r15583 r16386  
    5858      get { return (IDataAnalysisProblemData)this[ProblemDataResultName].Value; }
    5959      set {
    60         if (this[ProblemDataResultName].Value != value) {
    61           if (value != null) {
    62             ProblemData.Changed -= new EventHandler(ProblemData_Changed);
    63             this[ProblemDataResultName].Value = value;
    64             ProblemData.Changed += new EventHandler(ProblemData_Changed);
    65             OnProblemDataChanged();
    66           }
    67         }
     60        if (value == null) throw new ArgumentNullException("The problemData must not be null.");
     61        if (this[ProblemDataResultName].Value == value) return;
     62        string errorMessage = string.Empty;
     63        if (!Model.IsProblemDataCompatible(value, out errorMessage)) throw new ArgumentException(errorMessage);
     64
     65        ProblemData.Changed -= new EventHandler(ProblemData_Changed);
     66        this[ProblemDataResultName].Value = value;
     67        ProblemData.Changed += new EventHandler(ProblemData_Changed);
     68        OnProblemDataChanged();
    6869      }
    6970    }
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionModel.cs

    r15583 r16386  
    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;
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs

    r15583 r16386  
    161161      OnChanged();
    162162    }
    163 
    164     protected override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
    165       if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
    166       IRegressionProblemData regressionProblemData = problemData as IRegressionProblemData;
    167       if (regressionProblemData == null)
    168         throw new ArgumentException("The problem data is not a regression problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
    169 
    170       var returnValue = base.IsProblemDataCompatible(problemData, out errorMessage);
    171       return returnValue;
    172     }
    173 
    174     public override void AdjustProblemDataProperties(IDataAnalysisProblemData problemData) {
    175       if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
    176       RegressionProblemData regressionProblemData = problemData as RegressionProblemData;
    177       if (regressionProblemData == null)
    178         throw new ArgumentException("The problem data is not a regression problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
    179 
    180       base.AdjustProblemDataProperties(problemData);
    181     }
    182163  }
    183164}
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolutionBase.cs

    r15583 r16386  
    7070    public new IRegressionProblemData ProblemData {
    7171      get { return (IRegressionProblemData)base.ProblemData; }
    72       set { base.ProblemData = value; }
     72      set {
     73        if (value == null) throw new ArgumentNullException("The problemData must not be null.");
     74        string errorMessage = string.Empty;
     75        if (!Model.IsProblemDataCompatible(value, out errorMessage)) throw new ArgumentException(errorMessage);
     76
     77        base.ProblemData = value;
     78      }
    7379    }
    7480
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisProblemData.cs

    r15583 r16386  
    16201620      OnChanged();
    16211621    }
    1622 
    1623     protected override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
    1624       if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
    1625       ITimeSeriesPrognosisProblemData timeseriesProblemData = problemData as ITimeSeriesPrognosisProblemData;
    1626       if (timeseriesProblemData == null)
    1627         throw new ArgumentException("The problem data is not a time-series problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
    1628 
    1629       var returnValue = base.IsProblemDataCompatible(problemData, out errorMessage);
    1630       //check targetVariable
    1631       if (problemData.InputVariables.All(var => var.Value != TargetVariable)) {
    1632         errorMessage = string.Format("The target variable {0} is not present in the new problem data.", TargetVariable)
    1633                        + Environment.NewLine + errorMessage;
    1634         return false;
    1635       }
    1636       return returnValue;
    1637     }
    1638 
    1639     public override void AdjustProblemDataProperties(IDataAnalysisProblemData problemData) {
    1640       TimeSeriesPrognosisProblemData timeSeriesProblemData = problemData as TimeSeriesPrognosisProblemData;
    1641       if (timeSeriesProblemData == null)
    1642         throw new ArgumentException("The problem data is not a timeseries problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
    1643 
    1644       var trainingDataStart = TrainingIndices.First();
    1645 
    1646       base.AdjustProblemDataProperties(problemData);
    1647 
    1648       TestPartition.Start = trainingDataStart;
    1649 
    1650       TrainingHorizon = timeSeriesProblemData.TrainingHorizon;
    1651       TestHorizon = timeSeriesProblemData.TestHorizon;
    1652     }
    1653 
    16541622  }
    16551623}
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Classification/IClassificationModel.cs

    r15583 r16386  
    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;
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDataAnalysisModel.cs

    r15583 r16386  
    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}
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDataAnalysisProblemData.cs

    r15583 r16386  
    4949
    5050    event EventHandler Changed;
    51 
    52     void AdjustProblemDataProperties(IDataAnalysisProblemData problemData);
    5351  }
    5452}
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDataset.cs

    r15583 r16386  
    3333    IEnumerable<string> DateTimeVariables { get; }
    3434
     35    bool ContainsVariable(string variablename);
    3536    bool VariableHasType<T>(string variableName);
    3637
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/Regression/IRegressionModel.cs

    r15583 r16386  
    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;
  • branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs

    r15829 r16386  
    4747    public ModifiableDataset() { }
    4848
    49     public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) :
    50       base(variableNames, variableValues, cloneValues: false) { }
     49    public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues, bool cloneValues = false) :
     50      base(variableNames, variableValues, cloneValues) { }
     51
     52    public Dataset ToDataset() {
     53      return new Dataset(variableNames, variableNames.Select(v => variableValues[v]));
     54    }
     55
     56
     57    public IEnumerable<object> GetRow(int row) {
     58      if (row < 0 || row >= Rows)
     59        throw new ArgumentException(string.Format("Invalid row {0} specified. The dataset contains {1} row(s).", row, Rows));
     60
     61      return variableValues.Select(x => x.Value[row]);
     62    }
     63
     64    public void AddRow(IEnumerable<object> values) {
     65      var list = values.ToList();
     66      if (list.Count != variableNames.Count)
     67        throw new ArgumentException("The number of values must be equal to the number of variable names.");
     68      // check if all the values are of the correct type
     69      for (int i = 0; i < list.Count; ++i) {
     70        if (list[i].GetType() != GetVariableType(variableNames[i])) {
     71          throw new ArgumentException("The type of the provided value does not match the variable type.");
     72        }
     73      }
     74      // add values
     75      for (int i = 0; i < list.Count; ++i) {
     76        variableValues[variableNames[i]].Add(list[i]);
     77      }
     78      Rows++;
     79      OnRowsChanged();
     80      OnReset();
     81    }
    5182
    5283    public void ReplaceRow(int row, IEnumerable<object> values) {
     
    6495        variableValues[variableNames[i]][row] = list[i];
    6596      }
     97      OnReset();
     98    }
     99
     100    // slow, avoid using this
     101    public void RemoveRow(int row) {
     102      foreach (var list in variableValues.Values)
     103        list.RemoveAt(row);
     104      Rows--;
     105      OnRowsChanged();
     106      OnReset();
     107    }
     108
     109    // adds a new variable to the dataset
     110    public void AddVariable(string variableName, IList values) {
     111      InsertVariable(variableName, Columns, values);
     112    }
     113
     114    public void InsertVariable(string variableName, int position, IList values) {
     115      if (variableValues.ContainsKey(variableName))
     116        throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
     117
     118      if (position < 0 || position > Columns)
     119        throw new ArgumentException(string.Format("Incorrect position {0} specified. The position must be between 0 and {1}.", position, Columns));
     120
     121      if (values == null)
     122        throw new ArgumentNullException("values", "Values must not be null. At least an empty list of values has to be provided.");
     123
     124      if (values.Count != Rows)
     125        throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows));
     126
     127      if (!IsAllowedType(values))
     128        throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
     129
     130      variableNames.Insert(position, variableName);
     131      variableValues[variableName] = values;
     132
     133      OnColumnsChanged();
     134      OnColumnNamesChanged();
    66135      OnReset();
    67136    }
     
    77146    }
    78147
    79     public void AddRow(IEnumerable<object> values) {
    80       var list = values.ToList();
    81       if (list.Count != variableNames.Count)
    82         throw new ArgumentException("The number of values must be equal to the number of variable names.");
    83       // check if all the values are of the correct type
    84       for (int i = 0; i < list.Count; ++i) {
    85         if (list[i].GetType() != GetVariableType(variableNames[i])) {
    86           throw new ArgumentException("The type of the provided value does not match the variable type.");
    87         }
    88       }
    89       // add values
    90       for (int i = 0; i < list.Count; ++i) {
    91         variableValues[variableNames[i]].Add(list[i]);
    92       }
    93       rows++;
    94       OnRowsChanged();
    95       OnReset();
    96     }
    97 
    98     // adds a new variable to the dataset
    99     public void AddVariable(string variableName, IList values) {
    100       if (variableValues.ContainsKey(variableName))
    101         throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
    102 
    103       if (values == null || values.Count == 0)
    104         throw new ArgumentException("Cannot add variable with no values.");
    105 
    106       if (!IsAllowedType(values))
    107         throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
    108 
    109       variableValues[variableName] = values;
    110       variableNames.Add(variableName);
    111 
    112       OnColumnsChanged();
    113       OnColumnNamesChanged();
    114       OnReset();
    115     }
    116148
    117149    public void RemoveVariable(string variableName) {
     
    125157    }
    126158
    127     // slow, avoid using this
    128     public void RemoveRow(int row) {
    129       foreach (var list in variableValues.Values)
    130         list.RemoveAt(row);
    131       rows--;
     159    public void ClearValues() {
     160      foreach (var list in variableValues.Values) {
     161        list.Clear();
     162      }
     163      Rows = 0;
    132164      OnRowsChanged();
    133165      OnReset();
    134166    }
     167
    135168
    136169    public void SetVariableValue(object value, string variableName, int row) {
Note: See TracChangeset for help on using the changeset viewer.