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

Last change on this file since 17389 was 17389, checked in by mkommend, 22 months ago

#3044: Allowed modification of column within transformations.

File size: 3.6 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));
64    }
65  }
66
67  [Item("Transformation", "Represents the base class for a transformation.")]
68  [StorableType("F244DB89-72EA-4B41-89FA-C2E14F30AD29")]
69  public abstract class Transformation<T> : Transformation, ITransformation<T> {
70
71    [StorableConstructor]
72    protected Transformation(StorableConstructorFlag _) : base(_) { }
73    protected Transformation(Transformation<T> original, Cloner cloner) : base(original, cloner) { }
74    protected Transformation(IEnumerable<string> allowedColumns) : base(allowedColumns) { }
75
76    public virtual void ConfigureParameters(IEnumerable<T> data) {
77      // override in transformations with parameters
78    }
79
80    public abstract IEnumerable<T> Apply(IEnumerable<T> data);
81    public IEnumerable<T> ConfigureAndApply(IEnumerable<T> data) {
82      ConfigureParameters(data);
83      return Apply(data);
84    }
85
86    public abstract bool Check(IEnumerable<T> data, out string errorMsg);
87  }
88}
Note: See TracBrowser for help on using the repository browser.