1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using HeuristicLab.Parameters;
|
---|
9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Problems.DataAnalysis.Transformations {
|
---|
12 | [Item("LogarithmicTransformation", "f(x) = log(x, b) | Represents a logarithmic transformation.")]
|
---|
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
|
---|
23 | public double Base {
|
---|
24 | get { return BaseParameter.Value.Value; }
|
---|
25 | }
|
---|
26 | #endregion
|
---|
27 |
|
---|
28 | [StorableConstructor]
|
---|
29 | protected LogarithmicTransformation(bool deserializing) : base(deserializing) { }
|
---|
30 | protected LogarithmicTransformation(Transformation<double> original, Cloner cloner)
|
---|
31 | : base(original, cloner) {
|
---|
32 | }
|
---|
33 | public LogarithmicTransformation(IEnumerable<string> allowedColumns)
|
---|
34 | : base(allowedColumns) {
|
---|
35 | Parameters.Add(new ValueParameter<DoubleValue>(BaseParameterName, "b | Base of log-function", new DoubleValue(Math.E)));
|
---|
36 | }
|
---|
37 |
|
---|
38 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
39 | return new LogarithmicTransformation(this, cloner);
|
---|
40 | }
|
---|
41 |
|
---|
42 | public override IEnumerable<double> Apply(IEnumerable<double> data) {
|
---|
43 | foreach (double i in data) {
|
---|
44 | if (i > 0.0)
|
---|
45 | yield return Math.Log(i, Base);
|
---|
46 | else
|
---|
47 | yield return i;
|
---|
48 | }
|
---|
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 | }
|
---|