Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/ReciprocalTransformation.cs @ 12012

Last change on this file since 12012 was 11114, checked in by mkommend, 10 years ago

#2206: Merged data preprocessing into the trunk.

File size: 1.6 KB
Line 
1
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8namespace HeuristicLab.Problems.DataAnalysis {
9  [Item("Reciprocal Transformation", "f(x) = 1 / x | Represents a reciprocal transformation.")]
10  public class ReciprocalTransformation : Transformation<double> {
11
12    #region properties
13    public override string ShortName {
14      get { return "Inv"; }
15    }
16    #endregion
17
18    //TODO: is a special case of Linear
19    [StorableConstructor]
20    protected ReciprocalTransformation(bool deserializing) : base(deserializing) { }
21    protected ReciprocalTransformation(ReciprocalTransformation original, Cloner cloner)
22      : base(original, cloner) {
23    }
24    public ReciprocalTransformation(IEnumerable<string> allowedColumns)
25      : base(allowedColumns) {
26    }
27
28    public override IDeepCloneable Clone(Cloner cloner) {
29      return new ReciprocalTransformation(this, cloner);
30    }
31
32    public override IEnumerable<double> Apply(IEnumerable<double> data) {
33      foreach (double i in data) {
34        if (i > 0.0)
35          yield return 1.0 / i;
36        else
37          yield return i;
38      }
39    }
40
41    public override bool Check(IEnumerable<double> data, out string errorMsg) {
42      errorMsg = null;
43      int errorCounter = data.Count(i => i <= 0.0);
44      if (errorCounter > 0) {
45        errorMsg = String.Format("{0} values are zero or below zero. 1/x can not be applied onto these values", errorCounter);
46        return false;
47      }
48      return true;
49    }
50  }
51}
Note: See TracBrowser for help on using the repository browser.