[10779] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[10821] | 3 | using System.Linq;
|
---|
[10779] | 4 | using HeuristicLab.Common;
|
---|
| 5 | using HeuristicLab.Core;
|
---|
| 6 | using HeuristicLab.Data;
|
---|
| 7 | using HeuristicLab.Parameters;
|
---|
| 8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[16628] | 9 | using HEAL.Attic;
|
---|
[10779] | 10 |
|
---|
[11068] | 11 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[16628] | 12 | [StorableType("AAA306DF-F6F8-4AF2-B98F-5F6CBF1E131D")]
|
---|
[10909] | 13 | [Item("Logarithmic Transformation", "f(x) = log(x, b) | Represents a logarithmic transformation.")]
|
---|
[10779] | 14 | public class LogarithmicTransformation : Transformation<double> {
|
---|
| 15 | protected const string BaseParameterName = "Base";
|
---|
| 16 |
|
---|
| 17 | #region Parameters
|
---|
| 18 | public IValueParameter<DoubleValue> BaseParameter {
|
---|
| 19 | get { return (IValueParameter<DoubleValue>)Parameters[BaseParameterName]; }
|
---|
| 20 | }
|
---|
| 21 | #endregion
|
---|
| 22 |
|
---|
| 23 | #region properties
|
---|
[10932] | 24 | public override string ShortName {
|
---|
| 25 | get { return "Log"; }
|
---|
| 26 | }
|
---|
[10779] | 27 | public double Base {
|
---|
| 28 | get { return BaseParameter.Value.Value; }
|
---|
| 29 | }
|
---|
| 30 | #endregion
|
---|
| 31 |
|
---|
| 32 | [StorableConstructor]
|
---|
[16628] | 33 | protected LogarithmicTransformation(StorableConstructorFlag _) : base(_) { }
|
---|
[11114] | 34 | protected LogarithmicTransformation(LogarithmicTransformation original, Cloner cloner)
|
---|
[10779] | 35 | : base(original, cloner) {
|
---|
| 36 | }
|
---|
| 37 | public LogarithmicTransformation(IEnumerable<string> allowedColumns)
|
---|
| 38 | : base(allowedColumns) {
|
---|
[10808] | 39 | Parameters.Add(new ValueParameter<DoubleValue>(BaseParameterName, "b | Base of log-function", new DoubleValue(Math.E)));
|
---|
[10779] | 40 | }
|
---|
| 41 |
|
---|
| 42 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 43 | return new LogarithmicTransformation(this, cloner);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public override IEnumerable<double> Apply(IEnumerable<double> data) {
|
---|
[12612] | 47 | var b = Base;
|
---|
| 48 | return data.Select(d => d > 0.0 ? Math.Log(d, b) : d);
|
---|
[10779] | 49 | }
|
---|
| 50 |
|
---|
| 51 | public override bool Check(IEnumerable<double> data, out string errorMsg) {
|
---|
| 52 | errorMsg = null;
|
---|
[10821] | 53 | int errorCounter = data.Count(i => i <= 0.0);
|
---|
[10779] | 54 | if (errorCounter > 0) {
|
---|
[10821] | 55 | errorMsg = String.Format("{0} values are zero or below zero. Logarithm can not be applied onto these values", errorCounter);
|
---|
[10779] | 56 | return false;
|
---|
| 57 | }
|
---|
| 58 | return true;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | }
|
---|
| 62 | }
|
---|