Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/LogarithmicTransformation.cs @ 12009

Last change on this file since 12009 was 11114, checked in by mkommend, 10 years ago

#2206: Merged data preprocessing into the trunk.

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 {
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 override string ShortName {
23      get { return "Log"; }
24    }
25    public double Base {
26      get { return BaseParameter.Value.Value; }
27    }
28    #endregion
29
30    [StorableConstructor]
31    protected LogarithmicTransformation(bool deserializing) : base(deserializing) { }
32    protected LogarithmicTransformation(LogarithmicTransformation original, Cloner cloner)
33      : base(original, cloner) {
34    }
35    public LogarithmicTransformation(IEnumerable<string> allowedColumns)
36      : base(allowedColumns) {
37      Parameters.Add(new ValueParameter<DoubleValue>(BaseParameterName, "b | Base of log-function", new DoubleValue(Math.E)));
38    }
39
40    public override IDeepCloneable Clone(Cloner cloner) {
41      return new LogarithmicTransformation(this, cloner);
42    }
43
44    public override IEnumerable<double> Apply(IEnumerable<double> data) {
45      foreach (double i in data) {
46        if (i > 0.0)
47          yield return Math.Log(i, Base);
48        else
49          yield return i;
50      }
51    }
52
53    public override bool Check(IEnumerable<double> data, out string errorMsg) {
54      errorMsg = null;
55      int errorCounter = data.Count(i => i <= 0.0);
56      if (errorCounter > 0) {
57        errorMsg = String.Format("{0} values are zero or below zero. Logarithm can not be applied onto these values", errorCounter);
58        return false;
59      }
60      return true;
61    }
62
63  }
64}
Note: See TracBrowser for help on using the repository browser.