Changeset 15769


Ignore:
Timestamp:
02/13/18 16:26:29 (18 months ago)
Author:
bburlacu
Message:

#2897: Add type checks in Dataset constructor, remove cloning of values in the ModifiableDataset.AddVariable method. Provide small fix in GradientBoostingRegressionAlgorithm for AddVariable.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/GBM/GradientBoostingRegressionAlgorithm.cs

    r15583 r15769  
    258258
    259259          modifiableDataset.RemoveVariable(targetVarName);
    260           modifiableDataset.AddVariable(targetVarName, curY.Concat(curYTest));
     260          modifiableDataset.AddVariable(targetVarName, curY.Concat(curYTest).ToList());
    261261
    262262          SampleTrainingData(rand, modifiableDataset, rRows, problemData.Dataset, curY, problemData.TargetVariable, problemData.TrainingIndices); // all training indices from the original problem data are allowed
  • trunk/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs

    r15583 r15769  
    7878      this.variableValues = new Dictionary<string, IList>(this.variableNames.Count);
    7979      for (int i = 0; i < this.variableNames.Count; i++) {
     80        var variableName = this.variableNames[i];
    8081        var values = variableValues.ElementAt(i);
    81         this.variableValues.Add(this.variableNames[i], values);
     82
     83        if (!IsAllowedType(values)) {
     84          throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
     85        }
     86
     87        this.variableValues.Add(variableName, values);
    8288      }
    8389    }
     
    125131      return new ModifiableDataset(variableNames, values);
    126132    }
     133
    127134    /// <summary>
    128135    /// Shuffle a dataset's rows
     
    249256    }
    250257
     258    protected Type GetVariableType(string variableName) {
     259      IList list;
     260      variableValues.TryGetValue(variableName, out list);
     261      if (list == null)
     262        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
     263      return GetElementType(list);
     264    }
     265
     266    protected Type GetElementType(IList list) {
     267      var type = list.GetType();
     268      return type.IsGenericType ? type.GetGenericArguments()[0] : type.GetElementType();
     269    }
     270
     271    protected bool IsAllowedType(IList list) {
     272      var type = GetElementType(list);
     273      return IsAllowedType(type);
     274    }
     275
     276    protected bool IsAllowedType(Type type) {
     277      return type == typeof(double) || type == typeof(string) || type == typeof(DateTime);
     278    }
     279
    251280    #region IStringConvertibleMatrix Members
    252281    [Storable]
  • trunk/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs

    r15583 r15769  
    105105
    106106    // adds a new variable to the dataset
    107     public void AddVariable<T>(string variableName, IEnumerable<T> values) {
     107    public void AddVariable(string variableName, IList values) {
    108108      if (variableValues.ContainsKey(variableName))
    109         throw new ArgumentException("Variable " + variableName + " is already present in the dataset.");
    110       int count = values.Count();
    111       if (count != rows)
    112         throw new ArgumentException("The number of values must exactly match the number of rows in the dataset.");
    113       variableValues[variableName] = new List<T>(values);
     109        throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
     110
     111      if (values == null || values.Count == 0)
     112        throw new ArgumentException("Cannot add variable with no values.");
     113
     114      if (!IsAllowedType(values))
     115        throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
     116
     117      variableValues[variableName] = values;
    114118      variableNames.Add(variableName);
     119
    115120      OnColumnsChanged();
    116121      OnColumnNamesChanged();
     
    120125    public void RemoveVariable(string variableName) {
    121126      if (!variableValues.ContainsKey(variableName))
    122         throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
     127        throw new ArgumentException(string.Format("The variable {0} does not exist in the dataset.", variableName));
    123128      variableValues.Remove(variableName);
    124129      variableNames.Remove(variableName);
     
    128133    }
    129134
    130     // slow, avoid to use this
     135    // slow, avoid using this
    131136    public void RemoveRow(int row) {
    132137      foreach (var list in variableValues.Values)
     
    151156    }
    152157
    153     private Type GetVariableType(string variableName) {
    154       IList list;
    155       variableValues.TryGetValue(variableName, out list);
    156       if (list == null)
    157         throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
    158       return list.GetType().GetGenericArguments()[0];
    159     }
    160 
    161158    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
    162159      var variableName = variableNames[columnIndex];
Note: See TracChangeset for help on using the changeset viewer.