Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.Problems.DataAnalysis.Transformations/3.3/ReciprocalTransformation.cs @ 10781

Last change on this file since 10781 was 10781, checked in by tsteinre, 10 years ago

Added ReciprocalTransformation

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