1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HEAL.Attic;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
12 | [StorableType("AAA306DF-F6F8-4AF2-B98F-5F6CBF1E131D")]
|
---|
13 | [Item("Logarithmic Transformation", "f(x) = log(x, b) | Represents a logarithmic transformation.")]
|
---|
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
|
---|
24 | public override string ShortName {
|
---|
25 | get { return "Log"; }
|
---|
26 | }
|
---|
27 | public double Base {
|
---|
28 | get { return BaseParameter.Value.Value; }
|
---|
29 | }
|
---|
30 | #endregion
|
---|
31 |
|
---|
32 | [StorableConstructor]
|
---|
33 | protected LogarithmicTransformation(StorableConstructorFlag _) : base(_) { }
|
---|
34 | protected LogarithmicTransformation(LogarithmicTransformation original, Cloner cloner)
|
---|
35 | : base(original, cloner) {
|
---|
36 | }
|
---|
37 | public LogarithmicTransformation(IEnumerable<string> allowedColumns)
|
---|
38 | : base(allowedColumns) {
|
---|
39 | Parameters.Add(new ValueParameter<DoubleValue>(BaseParameterName, "b | Base of log-function", new DoubleValue(Math.E)));
|
---|
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) {
|
---|
47 | var b = Base;
|
---|
48 | return data.Select(d => d > 0.0 ? Math.Log(d, b) : d);
|
---|
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 | }
|
---|