Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/26/14 16:13:11 (10 years ago)
Author:
bburlacu
Message:

#2276: Unsealed Dataset class, refactored GetDoubleValues, GetStringValues, GetDateTimeValues methods to internally use the same generic method, added ModifiableDataset class which adds the functionality of replacing, adding or removing rows in the dataset.

Location:
branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis/3.4
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs

    r11571 r11589  
    3333  [Item("Dataset", "Represents a dataset containing data that should be analyzed.")]
    3434  [StorableClass]
    35   public sealed class Dataset : NamedItem, IDataset {
     35  public class Dataset : NamedItem, IDataset {
    3636    [StorableConstructor]
    37     private Dataset(bool deserializing) : base(deserializing) { }
    38     private Dataset(Dataset original, Cloner cloner)
     37    protected Dataset(bool deserializing) : base(deserializing) { }
     38    protected Dataset(Dataset original, Cloner cloner)
    3939      : base(original, cloner) {
    4040      variableValues = new Dictionary<string, IList>(original.variableValues);
     
    143143
    144144    [Storable(Name = "VariableValues")]
    145     private Dictionary<string, IList> variableValues;
    146 
    147     private List<string> variableNames;
     145    protected Dictionary<string, IList> variableValues;
     146
     147    protected List<string> variableNames;
    148148    [Storable]
    149149    public IEnumerable<string> VariableNames {
    150150      get { return variableNames; }
    151       private set {
     151      protected set {
    152152        if (variableNames != null) throw new InvalidOperationException();
    153153        variableNames = new List<string>(value);
    154154      }
    155155    }
    156 
    157156    public IEnumerable<string> DoubleVariables {
    158157      get { return variableValues.Where(p => p.Value is List<double>).Select(p => p.Key); }
    159158    }
    160 
    161159    public IEnumerable<double> GetDoubleValues(string variableName) {
     160      return GetValues<double>(variableName);
     161    }
     162    public IEnumerable<string> GetStringValues(string variableName) {
     163      return GetValues<string>(variableName);
     164    }
     165    public IEnumerable<DateTime> GetDateTimeValues(string variableName) {
     166      return GetValues<DateTime>(variableName);
     167    }
     168    public ReadOnlyCollection<double> GetReadOnlyDoubleValues(string variableName) {
     169      var values = GetValues<double>(variableName).ToList();
     170      return values.AsReadOnly();
     171    }
     172    public double GetDoubleValue(string variableName, int row) {
     173      var values = GetValues<double>(variableName) as List<double>;
     174      return values[row];
     175    }
     176    public IEnumerable<double> GetDoubleValues(string variableName, IEnumerable<int> rows) {
     177      return GetValues<double>(variableName, rows);
     178    }
     179    private IEnumerable<T> GetValues<T>(string variableName, IEnumerable<int> rows) {
     180      var values = GetValues<T>(variableName) as List<T>;
     181      return rows.Select(x => values[x]);
     182    }
     183    private IEnumerable<T> GetValues<T>(string variableName) {
    162184      IList list;
    163185      if (!variableValues.TryGetValue(variableName, out list))
    164186        throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
    165       List<double> values = list as List<double>;
    166       if (values == null) throw new ArgumentException("The variable " + variableName + " is not a double variable.");
    167 
    168       //mkommend yield return used to enable lazy evaluation
    169       foreach (double value in values)
    170         yield return value;
    171     }
    172 
    173     public IEnumerable<string> GetStringValues(string variableName) {
    174       IList list;
    175       if (!variableValues.TryGetValue(variableName, out list))
    176         throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
    177       List<string> values = list as List<string>;
    178       if (values == null) throw new ArgumentException("The variable " + variableName + " is not a string variable.");
    179 
    180       //mkommend yield return used to enable lazy evaluation
    181       foreach (string value in values)
    182         yield return value;
    183     }
    184 
    185     public IEnumerable<DateTime> GetDateTimeValues(string variableName) {
    186       IList list;
    187       if (!variableValues.TryGetValue(variableName, out list))
    188         throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
    189       List<DateTime> values = list as List<DateTime>;
    190       if (values == null) throw new ArgumentException("The variable " + variableName + " is not a datetime variable.");
    191 
    192       //mkommend yield return used to enable lazy evaluation
    193       foreach (DateTime value in values)
    194         yield return value;
    195     }
    196 
    197     public ReadOnlyCollection<double> GetReadOnlyDoubleValues(string variableName) {
    198       IList list;
    199       if (!variableValues.TryGetValue(variableName, out list))
    200         throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
    201       List<double> values = list as List<double>;
    202       if (values == null) throw new ArgumentException("The variable " + variableName + " is not a double variable.");
    203       return values.AsReadOnly();
    204     }
    205     public double GetDoubleValue(string variableName, int row) {
    206       IList list;
    207       if (!variableValues.TryGetValue(variableName, out list))
    208         throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
    209       List<double> values = list as List<double>;
    210       if (values == null) throw new ArgumentException("The variable " + variableName + " is not a double variable.");
    211       return values[row];
    212     }
    213     public IEnumerable<double> GetDoubleValues(string variableName, IEnumerable<int> rows) {
    214       IList list;
    215       if (!variableValues.TryGetValue(variableName, out list))
    216         throw new ArgumentException("The variable " + variableName + " does not exist in the dataset.");
    217       List<double> values = list as List<double>;
    218       if (values == null) throw new ArgumentException("The variable " + variableName + " is not a double variable.");
    219 
    220       return rows.Select(index => values[index]);
    221     }
    222 
     187      List<T> values = list as List<T>;
     188      if (values == null) throw new ArgumentException("The variable " + variableName + " is not a " + typeof(T) + " variable.");
     189      return values;
     190    }
    223191    public bool VariableHasType<T>(string variableName) {
    224192      return variableValues[variableName] is IList<T>;
     
    227195    #region IStringConvertibleMatrix Members
    228196    [Storable]
    229     private int rows;
     197    protected int rows;
    230198    public int Rows {
    231199      get { return rows; }
     
    236204      set { throw new NotSupportedException(); }
    237205    }
    238 
    239206    public bool SortableView {
    240207      get { return false; }
     
    244211      get { return true; }
    245212    }
    246 
    247213    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
    248214      get { return this.VariableNames; }
     
    253219      set { throw new NotSupportedException(); }
    254220    }
    255 
    256221    public string GetValue(int rowIndex, int columnIndex) {
    257222      return variableValues[variableNames[columnIndex]][rowIndex].ToString();
  • branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis/3.4/HeuristicLab.Problems.DataAnalysis-3.4.csproj

    r11571 r11589  
    172172    <Compile Include="Interfaces\TimeSeriesPrognosis\ITimeSeriesPrognosisProblemData.cs" />
    173173    <Compile Include="Interfaces\TimeSeriesPrognosis\ITimeSeriesPrognosisSolution.cs" />
     174    <Compile Include="ModifiableDataset.cs" />
    174175    <Compile Include="OnlineCalculators\AutoCorrelationCalculator.cs" />
    175176    <Compile Include="OnlineCalculators\DependencyCalculator\HoeffdingsDependenceCalculator.cs" />
  • branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDataset.cs

    r11571 r11589  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Collections.ObjectModel;
     
    3132    ReadOnlyCollection<double> GetReadOnlyDoubleValues(string variableName);
    3233    IEnumerable<string> GetStringValues(string variableName);
    33 
     34    IEnumerable<DateTime> GetDateTimeValues(string variableName);
    3435    IEnumerable<string> VariableNames { get; }
    3536    IEnumerable<string> DoubleVariables { get; }
  • branches/HeuristicLab.DatasetRefactor/sources/HeuristicLab.Problems.DataAnalysis/3.4/SimpleDataset.cs

    r11571 r11589  
    9696    }
    9797
     98    public IEnumerable<T> GetValues<T>(string variableName, IEnumerable<int> rows) {
     99      throw new NotImplementedException();
     100    }
     101
     102    public IEnumerable<T> GetValues<T>(string variableName) {
     103      throw new NotImplementedException();
     104    }
     105
    98106    public IEnumerable<double> GetDoubleValues(string variableName, IEnumerable<int> rows) {
    99107      yield return GetDoubleValue(variableName, 0);
     
    110118    public IEnumerable<string> GetStringValues(string variableName) {
    111119      yield return GetDoubleValue(variableName, 0).ToString(CultureInfo.InvariantCulture);
     120    }
     121
     122    public IEnumerable<DateTime> GetDateTimeValues(string variableName) {
     123      throw new NotImplementedException();
    112124    }
    113125
Note: See TracChangeset for help on using the changeset viewer.