Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3044_variableScaling/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/Transformation.cs @ 17391

Last change on this file since 17391 was 17391, checked in by djoedick, 4 years ago

#3044: Added transformation of input variables to problem data and created scaled dataset.

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30
31namespace HeuristicLab.Problems.DataAnalysis {
32
33  [Item("Transformation", "Represents the base class for a transformation.")]
34  [StorableType("46B380CC-6CDE-40FC-9BB4-E3FF82BA35EA")]
35  public abstract class Transformation : ParameterizedNamedItem, ITransformation {
36    protected const string ColumnParameterName = "Column";
37    #region parameter properties
38    public IConstrainedValueParameter<StringValue> ColumnParameter {
39      get { return (IConstrainedValueParameter<StringValue>)Parameters[ColumnParameterName]; }
40    }
41    #endregion
42
43    #region properties
44    public abstract string ShortName { get; }
45
46    public string Column {
47      get { return ColumnParameter.Value.Value; }
48      set {
49        if (value == null) throw new ArgumentNullException("column", "The provided value for the column is null.");
50        if (value == Column) return;
51
52        var matchingParameterValue = ColumnParameter.ValidValues.FirstOrDefault(v => v.Value == value);
53        ColumnParameter.Value = matchingParameterValue ?? throw new ArgumentException("The provided value is not valid as the column.", "column");
54      }
55    }
56    #endregion
57
58    [StorableConstructor]
59    protected Transformation(StorableConstructorFlag _) : base(_) { }
60    protected Transformation(Transformation original, Cloner cloner) : base(original, cloner) { }
61    protected Transformation(IEnumerable<string> allowedColumns) {
62      var allowed = new ItemSet<StringValue>(allowedColumns.Select(e => new StringValue(e)));
63      Parameters.Add(new ConstrainedValueParameter<StringValue>(ColumnParameterName, "Column used for the Transformation", allowed, allowed.First()));
64    }
65
66
67    //TODO: Fill XML comments
68    /// <summary>
69    /// Creates transformation for the provided input variables.
70    /// </summary>
71    /// <param name="transformation"></param>
72    /// <param name="dataset"></param>
73    /// <param name="rows"></param>
74    /// <param name="allowedInputVariables"></param>
75    /// <returns></returns>
76    public static ITransformation<double>[] CreateTransformations(ITransformation<double> transformation, IDataset dataset, IEnumerable<int> rows, IEnumerable<string> allowedInputVariables) {
77      var trans = new ITransformation<double>[allowedInputVariables.Count()];
78      int i = 0;
79      foreach (var variable in allowedInputVariables) {
80        var trans_clone = (ITransformation<double>)transformation.Clone();
81        trans_clone.Column = variable;
82        trans_clone.ConfigureParameters(dataset.GetDoubleValues(variable, rows));
83        trans[i] = trans_clone;
84        i++;
85      }
86      return trans;
87    }
88
89  }
90
91  [Item("Transformation", "Represents the base class for a transformation.")]
92  [StorableType("F244DB89-72EA-4B41-89FA-C2E14F30AD29")]
93  public abstract class Transformation<T> : Transformation, ITransformation<T> {
94
95    [StorableConstructor]
96    protected Transformation(StorableConstructorFlag _) : base(_) { }
97    protected Transformation(Transformation<T> original, Cloner cloner) : base(original, cloner) { }
98    protected Transformation(IEnumerable<string> allowedColumns) : base(allowedColumns) { }
99
100    public virtual void ConfigureParameters(IEnumerable<T> data) {
101      // override in transformations with parameters
102    }
103
104    public abstract IEnumerable<T> Apply(IEnumerable<T> data);
105    public IEnumerable<T> ConfigureAndApply(IEnumerable<T> data) {
106      ConfigureParameters(data);
107      return Apply(data);
108    }
109
110    public abstract bool Check(IEnumerable<T> data, out string errorMsg);
111
112
113  }
114}
Note: See TracBrowser for help on using the repository browser.