Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/18/18 16:16:31 (6 years ago)
Author:
gkronber
Message:

#2915: merged changes in the trunk up to current HEAD (r15951:16232) into the branch

Location:
branches/2915-AbsoluteSymbol
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2915-AbsoluteSymbol

  • branches/2915-AbsoluteSymbol/HeuristicLab.Problems.DataAnalysis

  • branches/2915-AbsoluteSymbol/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs

    r15829 r16240  
    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      return variableValues.Select(x => x.Value[row]);
     59    }
     60
     61    public void AddRow(IEnumerable<object> values) {
     62      var list = values.ToList();
     63      if (list.Count != variableNames.Count)
     64        throw new ArgumentException("The number of values must be equal to the number of variable names.");
     65      // check if all the values are of the correct type
     66      for (int i = 0; i < list.Count; ++i) {
     67        if (list[i].GetType() != GetVariableType(variableNames[i])) {
     68          throw new ArgumentException("The type of the provided value does not match the variable type.");
     69        }
     70      }
     71      // add values
     72      for (int i = 0; i < list.Count; ++i) {
     73        variableValues[variableNames[i]].Add(list[i]);
     74      }
     75      Rows++;
     76      OnRowsChanged();
     77      OnReset();
     78    }
    5179
    5280    public void ReplaceRow(int row, IEnumerable<object> values) {
     
    6492        variableValues[variableNames[i]][row] = list[i];
    6593      }
     94      OnReset();
     95    }
     96
     97    // slow, avoid using this
     98    public void RemoveRow(int row) {
     99      foreach (var list in variableValues.Values)
     100        list.RemoveAt(row);
     101      Rows--;
     102      OnRowsChanged();
     103      OnReset();
     104    }
     105
     106    // adds a new variable to the dataset
     107    public void AddVariable(string variableName, IList values) {
     108      InsertVariable(variableName, Columns, values);
     109    }
     110
     111    public void InsertVariable(string variableName, int position, IList values) {
     112      if (variableValues.ContainsKey(variableName))
     113        throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
     114
     115      if (position < 0 || position > Columns)
     116        throw new ArgumentException(string.Format("Incorrect position {0} specified. The position must be between 0 and {1}.", position, Columns));
     117
     118      if (values == null)
     119        throw new ArgumentNullException("values", "Values must not be null. At least an empty list of values has to be provided.");
     120
     121      if (values.Count != Rows)
     122        throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows));
     123
     124      if (!IsAllowedType(values))
     125        throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
     126
     127      variableNames.Insert(position, variableName);
     128      variableValues[variableName] = values;
     129
     130      OnColumnsChanged();
     131      OnColumnNamesChanged();
    66132      OnReset();
    67133    }
     
    77143    }
    78144
    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     }
    116145
    117146    public void RemoveVariable(string variableName) {
     
    125154    }
    126155
    127     // slow, avoid using this
    128     public void RemoveRow(int row) {
    129       foreach (var list in variableValues.Values)
    130         list.RemoveAt(row);
    131       rows--;
     156    public void ClearValues() {
     157      foreach (var list in variableValues.Values) {
     158        list.Clear();
     159      }
     160      Rows = 0;
    132161      OnRowsChanged();
    133162      OnReset();
    134163    }
     164
    135165
    136166    public void SetVariableValue(object value, string variableName, int row) {
Note: See TracChangeset for help on using the changeset viewer.