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 { [StorableClass] [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) { var b = Base; return data.Select(d => d > 0.0 ? Math.Log(d, b) : d); } 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; } } }