Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/25/15 13:46:24 (9 years ago)
Author:
mkommend
Message:

#2276: Reintegrated branch for dataset refactoring.

File:
1 edited

Legend:

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

    r12012 r12509  
    3333  [Item("Dataset", "Represents a dataset containing data that should be analyzed.")]
    3434  [StorableClass]
    35   public sealed class Dataset : NamedItem, IStringConvertibleMatrix {
     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);
     
    118118    }
    119119
     120    protected Dataset(Dataset dataset) : this(dataset.variableNames, dataset.variableValues.Values) { }
     121
    120122    #region Backwards compatible code, remove with 3.5
    121123    private double[,] storableData;
     
    143145
    144146    [Storable(Name = "VariableValues")]
    145     private Dictionary<string, IList> variableValues;
    146 
    147     private List<string> variableNames;
     147    protected Dictionary<string, IList> variableValues;
     148
     149    protected List<string> variableNames;
    148150    [Storable]
    149151    public IEnumerable<string> VariableNames {
    150152      get { return variableNames; }
    151       private set {
     153      protected set {
    152154        if (variableNames != null) throw new InvalidOperationException();
    153155        variableNames = new List<string>(value);
    154156      }
    155157    }
    156 
    157158    public IEnumerable<string> DoubleVariables {
    158159      get { return variableValues.Where(p => p.Value is List<double>).Select(p => p.Key); }
    159160    }
    160 
    161161    public IEnumerable<double> GetDoubleValues(string variableName) {
     162      return GetValues<double>(variableName);
     163    }
     164    public IEnumerable<string> GetStringValues(string variableName) {
     165      return GetValues<string>(variableName);
     166    }
     167    public IEnumerable<DateTime> GetDateTimeValues(string variableName) {
     168      return GetValues<DateTime>(variableName);
     169    }
     170
     171    public ReadOnlyCollection<double> GetReadOnlyDoubleValues(string variableName) {
     172      var values = GetValues<double>(variableName);
     173      return values.AsReadOnly();
     174    }
     175    public double GetDoubleValue(string variableName, int row) {
     176      var values = GetValues<double>(variableName);
     177      return values[row];
     178    }
     179    public IEnumerable<double> GetDoubleValues(string variableName, IEnumerable<int> rows) {
     180      return GetValues<double>(variableName, rows);
     181    }
     182    private IEnumerable<T> GetValues<T>(string variableName, IEnumerable<int> rows) {
     183      var values = GetValues<T>(variableName);
     184      return rows.Select(x => values[x]);
     185    }
     186    private List<T> GetValues<T>(string variableName) {
    162187      IList list;
    163188      if (!variableValues.TryGetValue(variableName, out list))
    164189        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 
     190      List<T> values = list as List<T>;
     191      if (values == null) throw new ArgumentException("The variable " + variableName + " is not a " + typeof(T) + " variable.");
     192      return values;
     193    }
    223194    public bool VariableHasType<T>(string variableName) {
    224195      return variableValues[variableName] is IList<T>;
     
    227198    #region IStringConvertibleMatrix Members
    228199    [Storable]
    229     private int rows;
     200    protected int rows;
    230201    public int Rows {
    231202      get { return rows; }
     
    236207      set { throw new NotSupportedException(); }
    237208    }
    238 
    239209    public bool SortableView {
    240210      get { return false; }
     
    244214      get { return true; }
    245215    }
    246 
    247216    IEnumerable<string> IStringConvertibleMatrix.ColumnNames {
    248217      get { return this.VariableNames; }
     
    253222      set { throw new NotSupportedException(); }
    254223    }
    255 
    256224    public string GetValue(int rowIndex, int columnIndex) {
    257225      return variableValues[variableNames[columnIndex]][rowIndex].ToString();
    258226    }
    259     public bool SetValue(string value, int rowIndex, int columnIndex) {
     227    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
    260228      throw new NotSupportedException();
    261229    }
    262     public bool Validate(string value, out string errorMessage) {
     230    bool IStringConvertibleMatrix.Validate(string value, out string errorMessage) {
    263231      throw new NotSupportedException();
    264232    }
    265233
    266     public event EventHandler ColumnsChanged { add { } remove { } }
    267     public event EventHandler RowsChanged { add { } remove { } }
    268     public event EventHandler ColumnNamesChanged { add { } remove { } }
    269     public event EventHandler RowNamesChanged { add { } remove { } }
    270     public event EventHandler SortableViewChanged { add { } remove { } }
    271     public event EventHandler<EventArgs<int, int>> ItemChanged { add { } remove { } }
    272     public event EventHandler Reset { add { } remove { } }
     234    public virtual event EventHandler ColumnsChanged { add { } remove { } }
     235    public virtual event EventHandler RowsChanged { add { } remove { } }
     236    public virtual event EventHandler ColumnNamesChanged { add { } remove { } }
     237    public virtual event EventHandler RowNamesChanged { add { } remove { } }
     238    public virtual event EventHandler SortableViewChanged { add { } remove { } }
     239    public virtual event EventHandler<EventArgs<int, int>> ItemChanged { add { } remove { } }
     240    public virtual event EventHandler Reset { add { } remove { } }
    273241    #endregion
    274242  }
Note: See TracChangeset for help on using the changeset viewer.