Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10809 was 10808, checked in by tsteinre, 11 years ago
  • rewritten Item descriptions for Transformations.
File size: 1.7 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", "f(x) = 1 / x | Represents a reciprocal transformation.")]
10  public class ReciprocalTransformation : Transformation<double> {
11
12    //TODO: is a special case of Linear
13    [StorableConstructor]
14    protected ReciprocalTransformation(bool deserializing) : base(deserializing) { }
15    protected ReciprocalTransformation(Transformation<double> original, Cloner cloner)
16      : base(original, cloner) {
17    }
18    public ReciprocalTransformation(IEnumerable<string> allowedColumns)
19      : base(allowedColumns) {
20    }
21
22    public override IDeepCloneable Clone(Cloner cloner) {
23      return new ReciprocalTransformation(this, cloner);
24    }
25
26    public override IEnumerable<double> Apply(IEnumerable<double> data) {
27      foreach (double i in data) {
28        if (i > 0.0)
29          yield return 1.0 / i;
30        else
31          yield return i;
32      }
33    }
34
35    public override bool Check(IEnumerable<double> data, out string errorMsg) {
36      StringBuilder sb = new StringBuilder();
37      errorMsg = null;
38      int errorCounter = 0;
39
40      foreach (double i in data) {
41        if (i <= 0.0) {
42          ++errorCounter;
43        }
44      }
45
46      if (errorCounter > 0) {
47        sb.Append(String.Format("{0} values are zero or below zero. 1/x can not be applied onto these values", errorCounter));
48        errorMsg = sb.ToString();
49        return false;
50      }
51      return true;
52    }
53  }
54}
Note: See TracBrowser for help on using the repository browser.