[10664] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10664] | 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 |
|
---|
[17389] | 22 | using System;
|
---|
[10694] | 23 | using System.Collections.Generic;
|
---|
[10702] | 24 | using System.Linq;
|
---|
[17389] | 25 | using HEAL.Attic;
|
---|
[10669] | 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[10702] | 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
[10664] | 30 |
|
---|
[11068] | 31 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[10940] | 32 |
|
---|
[10669] | 33 | [Item("Transformation", "Represents the base class for a transformation.")]
|
---|
[16565] | 34 | [StorableType("46B380CC-6CDE-40FC-9BB4-E3FF82BA35EA")]
|
---|
[10940] | 35 | public abstract class Transformation : ParameterizedNamedItem, ITransformation {
|
---|
[10702] | 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
|
---|
[10664] | 42 |
|
---|
[10702] | 43 | #region properties
|
---|
[10932] | 44 | public abstract string ShortName { get; }
|
---|
| 45 |
|
---|
[10702] | 46 | public string Column {
|
---|
| 47 | get { return ColumnParameter.Value.Value; }
|
---|
[17389] | 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 | }
|
---|
[10702] | 55 | }
|
---|
| 56 | #endregion
|
---|
| 57 |
|
---|
[10694] | 58 | [StorableConstructor]
|
---|
[16565] | 59 | protected Transformation(StorableConstructorFlag _) : base(_) { }
|
---|
[10940] | 60 | protected Transformation(Transformation original, Cloner cloner) : base(original, cloner) { }
|
---|
[10702] | 61 | protected Transformation(IEnumerable<string> allowedColumns) {
|
---|
| 62 | var allowed = new ItemSet<StringValue>(allowedColumns.Select(e => new StringValue(e)));
|
---|
[17390] | 63 | Parameters.Add(new ConstrainedValueParameter<StringValue>(ColumnParameterName, "Column used for the Transformation", allowed, allowed.First()));
|
---|
[10664] | 64 | }
|
---|
[17390] | 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>
|
---|
[17391] | 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()];
|
---|
[17390] | 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 |
|
---|
[10940] | 89 | }
|
---|
[10664] | 90 |
|
---|
[10940] | 91 | [Item("Transformation", "Represents the base class for a transformation.")]
|
---|
[16565] | 92 | [StorableType("F244DB89-72EA-4B41-89FA-C2E14F30AD29")]
|
---|
[10940] | 93 | public abstract class Transformation<T> : Transformation, ITransformation<T> {
|
---|
| 94 |
|
---|
| 95 | [StorableConstructor]
|
---|
[16565] | 96 | protected Transformation(StorableConstructorFlag _) : base(_) { }
|
---|
[10940] | 97 | protected Transformation(Transformation<T> original, Cloner cloner) : base(original, cloner) { }
|
---|
[11068] | 98 | protected Transformation(IEnumerable<string> allowedColumns) : base(allowedColumns) { }
|
---|
[10940] | 99 |
|
---|
[14843] | 100 | public virtual void ConfigureParameters(IEnumerable<T> data) {
|
---|
| 101 | // override in transformations with parameters
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[10694] | 104 | public abstract IEnumerable<T> Apply(IEnumerable<T> data);
|
---|
[14843] | 105 | public IEnumerable<T> ConfigureAndApply(IEnumerable<T> data) {
|
---|
| 106 | ConfigureParameters(data);
|
---|
| 107 | return Apply(data);
|
---|
| 108 | }
|
---|
[10669] | 109 |
|
---|
[10702] | 110 | public abstract bool Check(IEnumerable<T> data, out string errorMsg);
|
---|
[17390] | 111 |
|
---|
| 112 |
|
---|
[10664] | 113 | }
|
---|
| 114 | }
|
---|