using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.DataAnalysis { [Item("Logarithmic Transformation", "f(x) = log(x, b) | Represents a logarithmic transformation.")] public class LogarithmicTransformation : Transformation { protected const string BaseParameterName = "Base"; #region Parameters public IValueParameter BaseParameter { get { return (IValueParameter)Parameters[BaseParameterName]; } } #endregion #region properties public override string ShortName { get { return "Log"; } } public double Base { get { return BaseParameter.Value.Value; } } #endregion [StorableConstructor] protected LogarithmicTransformation(bool deserializing) : base(deserializing) { } protected LogarithmicTransformation(LogarithmicTransformation original, Cloner cloner) : base(original, cloner) { } public LogarithmicTransformation(IEnumerable allowedColumns) : base(allowedColumns) { Parameters.Add(new ValueParameter(BaseParameterName, "b | Base of log-function", new DoubleValue(Math.E))); } public override IDeepCloneable Clone(Cloner cloner) { return new LogarithmicTransformation(this, cloner); } public override IEnumerable Apply(IEnumerable data) { foreach (double i in data) { if (i > 0.0) yield return Math.Log(i, Base); else yield return i; } } public override bool Check(IEnumerable data, out string errorMsg) { errorMsg = null; int errorCounter = data.Count(i => i <= 0.0); if (errorCounter > 0) { errorMsg = String.Format("{0} values are zero or below zero. Logarithm can not be applied onto these values", errorCounter); return false; } return true; } } }