Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/22/18 09:17:44 (6 years ago)
Author:
jkarder
Message:

#2939: merged r16084, r16118, r16120 and r16242 into stable

Location:
stable
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.DataAnalysis

  • stable/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs

    r16125 r16247  
    116116
    117117    public ModifiableDataset ToModifiable() {
    118       return new ModifiableDataset(variableNames, variableNames.Select(v => variableValues[v]),true);
     118      return new ModifiableDataset(variableNames, variableNames.Select(v => variableValues[v]), true);
    119119    }
    120120
  • stable/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs

    r16125 r16247  
    5353      return new Dataset(variableNames, variableNames.Select(v => variableValues[v]));
    5454    }
     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    }
     82
    5583    public void ReplaceRow(int row, IEnumerable<object> values) {
    5684      var list = values.ToList();
     
    6795        variableValues[variableNames[i]][row] = list[i];
    6896      }
     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();
    69135      OnReset();
    70136    }
     
    80146    }
    81147
    82     public void AddRow(IEnumerable<object> values) {
    83       var list = values.ToList();
    84       if (list.Count != variableNames.Count)
    85         throw new ArgumentException("The number of values must be equal to the number of variable names.");
    86       // check if all the values are of the correct type
    87       for (int i = 0; i < list.Count; ++i) {
    88         if (list[i].GetType() != GetVariableType(variableNames[i])) {
    89           throw new ArgumentException("The type of the provided value does not match the variable type.");
    90         }
    91       }
    92       // add values
    93       for (int i = 0; i < list.Count; ++i) {
    94         variableValues[variableNames[i]].Add(list[i]);
    95       }
    96       Rows++;
    97       OnRowsChanged();
    98       OnReset();
    99     }
    100 
    101     // adds a new variable to the dataset
    102     public void AddVariable(string variableName, IList values) {
    103       if (variableValues.ContainsKey(variableName))
    104         throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
    105 
    106       if (values == null || values.Count == 0)
    107         throw new ArgumentException("Cannot add variable with no values.");
    108 
    109       if (values.Count != Rows)
    110         throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows));
    111 
    112       if (!IsAllowedType(values))
    113         throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
    114 
    115       variableValues[variableName] = values;
    116       variableNames.Add(variableName);
    117 
    118       OnColumnsChanged();
    119       OnColumnNamesChanged();
    120       OnReset();
    121     }
    122 
    123 
    124     public void InsertVariable(string variableName, int position, IList values) {
    125       if (variableValues.ContainsKey(variableName))
    126         throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
    127 
    128       if (position < 0 || position > Columns)
    129         throw new ArgumentException(string.Format("Incorrect position {0} specified. The position must be between 0 and {1}.", position, Columns));
    130 
    131       if (values == null || values.Count == 0)
    132         throw new ArgumentException("Cannot add variable with no values.");
    133 
    134       if (values.Count != Rows)
    135         throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows));
    136 
    137       if (!IsAllowedType(values))
    138         throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
    139 
    140       variableNames.Insert(position, variableName);
    141       variableValues[variableName] = values;
    142 
    143       OnColumnsChanged();
    144       OnColumnNamesChanged();
    145       OnReset();
    146     }
    147148
    148149    public void RemoveVariable(string variableName) {
     
    156157    }
    157158
    158     // slow, avoid using this
    159     public void RemoveRow(int row) {
    160       foreach (var list in variableValues.Values)
    161         list.RemoveAt(row);
    162       Rows--;
     159    public void ClearValues() {
     160      foreach (var list in variableValues.Values) {
     161        list.Clear();
     162      }
     163      Rows = 0;
    163164      OnRowsChanged();
    164165      OnReset();
    165166    }
     167
    166168
    167169    public void SetVariableValue(object value, string variableName, int row) {
Note: See TracChangeset for help on using the changeset viewer.