Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Cleanup/HeuristicLab.DataPreprocessing/3.4/Data/Columns/DoublePreprocessingDataColumn.cs @ 15309

Last change on this file since 15309 was 15309, checked in by pfleck, 7 years ago

#2809 Worked on type-save PreprocessingDataColumns.

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Linq;
24using System.Text;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.DataPreprocessing {
31  [Item("DoublePreprocessingDataColumn", "")]
32  [StorableClass]
33  public sealed class DoublePreprocessingDataColumn : PreprocessingDataColumn<double> {
34
35    #region Constructor, Cloning & Persistence
36
37    public DoublePreprocessingDataColumn()
38      : base() { }
39
40    public DoublePreprocessingDataColumn(string name, IEnumerable<double> values)
41      : base(name, values) { }
42
43    private DoublePreprocessingDataColumn(DoublePreprocessingDataColumn original, Cloner cloner)
44      : base(original, cloner) { }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new DoublePreprocessingDataColumn(this, cloner);
48    }
49
50    [StorableConstructor]
51    private DoublePreprocessingDataColumn(bool deserializing)
52      : base(deserializing) { }
53
54    #endregion
55
56    public override bool IsValidValue(double value) {
57      return !(double.IsNaN(value) || double.IsInfinity(value));
58    }
59
60    protected override double DefaultValue { get { return double.NaN; } }
61
62    #region Statistics
63    public double GetRange(IEnumerable<int> indices = null) { return GetMax(indices) - GetMin(indices); }
64    public double GetMean(IEnumerable<int> indices = null) { return GetValidValues(indices).DefaultIfEmpty(DefaultValue).Average(); }
65    public override double GetMedian(IEnumerable<int> indices = null) { return GetValidValues(indices).DefaultIfEmpty(DefaultValue).Median(); } // IEnumerable<doube> version is faster
66    public double GetStandardDeviation(IEnumerable<int> indices = null) { return GetValidValues(indices).DefaultIfEmpty(DefaultValue).StandardDeviation(); }
67    public double GetVariance(IEnumerable<int> indices = null) { return GetValidValues(indices).DefaultIfEmpty(DefaultValue).Variance(); }
68    public override double GetQuantile(double alpha, IEnumerable<int> indices = null) { return GetValidValues(indices).DefaultIfEmpty(double.NaN).Quantile(alpha); } // IEnumerable<doube> version is faster
69    #endregion
70
71    #region IStringConvertibleColumn
72    public override bool Validate(string value, out string errorMessage) {
73      double val;
74      bool valid = double.TryParse(value, out val) || string.IsNullOrEmpty(value);
75      errorMessage = string.Empty;
76      if (!valid) {
77        var sb = new StringBuilder();
78        sb.Append("Invalid Value (Valid Value Format: \"");
79        sb.Append(FormatPatterns.GetDoubleFormatPattern());
80        sb.Append("\")");
81        errorMessage = sb.ToString();
82      }
83      return valid;
84    }
85    public override string GetValue(int index) {
86      return Values[index].ToString("r");
87    }
88    public override bool SetValue(string value, int index) {
89      double val;
90      if (double.TryParse(value, out val)) {
91        Values[index] = val;
92        return true;
93      } else if (string.IsNullOrEmpty(value)) {
94        Values[index] = double.NaN;
95        return true;
96      } else {
97        return false;
98      }
99    }
100    #endregion
101  }
102}
Note: See TracBrowser for help on using the repository browser.