Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/LogarithmicTransformation.cs @ 17912

Last change on this file since 17912 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 2.1 KB
RevLine 
[10779]1using System;
2using System.Collections.Generic;
[10821]3using System.Linq;
[10779]4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Parameters;
[16565]8using HEAL.Attic;
[10779]9
[11068]10namespace HeuristicLab.Problems.DataAnalysis {
[16565]11  [StorableType("D3330C77-060A-4F75-A0C5-47AC81C5F2DF")]
[10909]12  [Item("Logarithmic Transformation", "f(x) = log(x, b) | Represents a logarithmic transformation.")]
[10779]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
[10932]23    public override string ShortName {
24      get { return "Log"; }
25    }
[10779]26    public double Base {
27      get { return BaseParameter.Value.Value; }
28    }
29    #endregion
30
31    [StorableConstructor]
[16565]32    protected LogarithmicTransformation(StorableConstructorFlag _) : base(_) { }
[11114]33    protected LogarithmicTransformation(LogarithmicTransformation original, Cloner cloner)
[10779]34      : base(original, cloner) {
35    }
36    public LogarithmicTransformation(IEnumerable<string> allowedColumns)
37      : base(allowedColumns) {
[10808]38      Parameters.Add(new ValueParameter<DoubleValue>(BaseParameterName, "b | Base of log-function", new DoubleValue(Math.E)));
[10779]39    }
40
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new LogarithmicTransformation(this, cloner);
43    }
44
45    public override IEnumerable<double> Apply(IEnumerable<double> data) {
[12612]46      var b = Base;
47      return data.Select(d => d > 0.0 ? Math.Log(d, b) : d);
[10779]48    }
49
50    public override bool Check(IEnumerable<double> data, out string errorMsg) {
51      errorMsg = null;
[10821]52      int errorCounter = data.Count(i => i <= 0.0);
[10779]53      if (errorCounter > 0) {
[10821]54        errorMsg = String.Format("{0} values are zero or below zero. Logarithm can not be applied onto these values", errorCounter);
[10779]55        return false;
56      }
57      return true;
58    }
59
60  }
61}
Note: See TracBrowser for help on using the repository browser.