Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10702


Ignore:
Timestamp:
04/02/14 13:02:17 (11 years ago)
Author:
tsteinre
Message:

refactored LinearTransformation / Transformation to fit HL coding style

Location:
branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/Transformations
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/Transformations/LinearTransformation.cs

    r10694 r10702  
    2121
    2222
     23using System;
    2324using System.Collections.Generic;
    2425using System.Linq;
     26using System.Text;
    2527using HeuristicLab.Common;
    2628using HeuristicLab.Core;
     
    2830using HeuristicLab.DataPreprocessing.Transformations;
    2931using HeuristicLab.Parameters;
     32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3033namespace HeuristicLab.DataPreprocessing.Implementations.Transformations {
    3134  [Item("LinearTransformation", "Represents a linear transformation with multiplication and addition.")]
    3235  public class LinearTransformation : Transformation<double> {
     36    protected const string MultiplicandParameterName = "Multiplicand";
     37    protected const string SummandParameterName = "Summand";
    3338
    3439    #region Parameters
    3540    public IValueParameter<DoubleValue> MultiplicandParameter {
    36       get { return (IValueParameter<DoubleValue>)Parameters["Multiplicand"]; }
     41      get { return (IValueParameter<DoubleValue>)Parameters[MultiplicandParameterName]; }
    3742    }
    3843    public IValueParameter<DoubleValue> SummandParameter {
    39       get { return (IValueParameter<DoubleValue>)Parameters["Summand"]; }
     44      get { return (IValueParameter<DoubleValue>)Parameters[SummandParameterName]; }
    4045    }
    4146    #endregion
    4247
    43     public LinearTransformation()
    44       : base() {
    45       Parameters.Add(new ValueParameter<DoubleValue>("Multiplicand", "Multiplicand for linear transformation", new DoubleValue(1.0)));
    46       Parameters.Add(new ValueParameter<DoubleValue>("Summand", "Summand for linear transformation", new DoubleValue(0.0)));
     48    #region properties
     49    public double Multiplicand {
     50      get { return MultiplicandParameter.Value.Value; }
     51    }
     52
     53    public double Summand {
     54      get { return SummandParameter.Value.Value; }
     55    }
     56    #endregion
     57
     58    [StorableConstructor]
     59    protected LinearTransformation(bool deserializing) : base(deserializing) { }
     60    protected LinearTransformation(Transformation<double> original, Cloner cloner)
     61      : base(original, cloner) {
     62    }
     63    protected LinearTransformation() {
     64    }
     65    public LinearTransformation(IEnumerable<string> allowedColumns)
     66      : base(allowedColumns) {
     67      Parameters.Add(new ValueParameter<DoubleValue>(MultiplicandParameterName, "Multiplicand for linear transformation", new DoubleValue(1.0)));
     68      Parameters.Add(new ValueParameter<DoubleValue>(SummandParameterName, "Summand for linear transformation", new DoubleValue(0.0)));
     69    }
     70
     71    public override IDeepCloneable Clone(Cloner cloner) {
     72      return new LinearTransformation(this, cloner);
    4773    }
    4874
    4975    public override IEnumerable<double> Apply(IEnumerable<double> data) {
    50       return data.Select(e => e * MultiplicandParameter.Value.Value + SummandParameter.Value.Value).ToList();
     76      return data.Select(e => e * Multiplicand + Summand);
    5177    }
    5278
    5379    public override IEnumerable<double> InverseApply(IEnumerable<double> data) {
    54       throw new System.NotImplementedException();
     80      return data.Select(e => (e - Summand) / Multiplicand);
    5581    }
    5682
    57     public override IDeepCloneable Clone(Cloner cloner) {
    58       throw new System.NotImplementedException();
    59     }
     83    public override bool Check(IEnumerable<double> data, out string errorMsg) {
     84      StringBuilder sb = new StringBuilder();
     85      bool success = true;
     86      errorMsg = null;
    6087
    61     public override void Check(IEnumerable<double> data) {
    62       throw new System.NotImplementedException();
     88      if (Multiplicand == 0.0) {
     89        sb.Append(String.Format("Multiplicand is 0, all {0} entries will be set to {1}. Reversal will not be possble", data.Count(), Summand));
     90        errorMsg = sb.ToString();
     91        success = false;
     92      }
     93      return success;
    6394    }
    6495  }
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/Transformations/Transformation.cs

    r10694 r10702  
    2121
    2222using System.Collections.Generic;
     23using System.Linq;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     26using HeuristicLab.Data;
     27using HeuristicLab.Parameters;
    2528using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2629using HeuristicLab.Problems.DataAnalysis;
     
    3033  [StorableClass]
    3134  public abstract class Transformation<T> : ParameterizedNamedItem, ITransformation<T> {
    32     public string Column { get; private set; }
     35    protected const string ColumnParameterName = "Column";
     36    #region parameter properties
     37    public IConstrainedValueParameter<StringValue> ColumnParameter {
     38      get { return (IConstrainedValueParameter<StringValue>)Parameters[ColumnParameterName]; }
     39    }
     40    #endregion
     41
     42    #region properties
     43    public string Column {
     44      get { return ColumnParameter.Value.Value; }
     45    }
     46    #endregion
    3347
    3448    [StorableConstructor]
    3549    protected Transformation(bool deserializing) : base(deserializing) { }
    36     protected Transformation(Transformation<T> original, Cloner cloner)
    37       : base(original, cloner) {
    38       Column = original.Column;
    39     }
     50    protected Transformation(Transformation<T> original, Cloner cloner): base(original, cloner) {}
    4051    protected Transformation() { }
    41     protected Transformation(string column) {
    42       this.Column = column;
     52    protected Transformation(IEnumerable<string> allowedColumns) {
     53      var allowed = new ItemSet<StringValue>(allowedColumns.Select(e => new StringValue(e)));
     54      Parameters.Add(new ConstrainedValueParameter<StringValue>(ColumnParameterName, "Column used for the Transformation", allowed));
    4355    }
    4456
     
    4759    public abstract IEnumerable<T> InverseApply(IEnumerable<T> data);
    4860
    49     public abstract void Check(IEnumerable<T> data);
     61    public abstract bool Check(IEnumerable<T> data, out string errorMsg);
    5062  }
    5163}
Note: See TracChangeset for help on using the changeset viewer.