Free cookie consent management tool by TermsFeed Policy Generator

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

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