Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.Problems.DataAnalysis.Transformations/3.3/LogarithmicTransformation.cs @ 10920

Last change on this file since 10920 was 10909, checked in by tsteinre, 11 years ago
  • modified Transformations Names / Parameter Names
File size: 2.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Parameters;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9
10namespace HeuristicLab.Problems.DataAnalysis.Transformations {
11  [Item("Logarithmic Transformation", "f(x) = log(x, b) | Represents a logarithmic transformation.")]
12  public class LogarithmicTransformation : Transformation<double> {
13    protected const string BaseParameterName = "Base";
14
15    #region Parameters
16    public IValueParameter<DoubleValue> BaseParameter {
17      get { return (IValueParameter<DoubleValue>)Parameters[BaseParameterName]; }
18    }
19    #endregion
20
21    #region properties
22    public double Base {
23      get { return BaseParameter.Value.Value; }
24    }
25    #endregion
26
27    [StorableConstructor]
28    protected LogarithmicTransformation(bool deserializing) : base(deserializing) { }
29    protected LogarithmicTransformation(Transformation<double> original, Cloner cloner)
30      : base(original, cloner) {
31    }
32    public LogarithmicTransformation(IEnumerable<string> allowedColumns)
33      : base(allowedColumns) {
34      Parameters.Add(new ValueParameter<DoubleValue>(BaseParameterName, "b | Base of log-function", new DoubleValue(Math.E)));
35    }
36
37    public override IDeepCloneable Clone(Cloner cloner) {
38      return new LogarithmicTransformation(this, cloner);
39    }
40
41    public override IEnumerable<double> Apply(IEnumerable<double> data) {
42      foreach (double i in data) {
43        if (i > 0.0)
44          yield return Math.Log(i, Base);
45        else
46          yield return i;
47      }
48    }
49
50    public override bool Check(IEnumerable<double> data, out string errorMsg) {
51      errorMsg = null;
52      int errorCounter = data.Count(i => i <= 0.0);
53      if (errorCounter > 0) {
54        errorMsg = String.Format("{0} values are zero or below zero. Logarithm can not be applied onto these values", errorCounter);
55        return false;
56      }
57      return true;
58    }
59
60  }
61}
Note: See TracBrowser for help on using the repository browser.