#region License Information /* HeuristicLab * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Collections.Generic; using System.Drawing; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.DataPreprocessing { [Item("Transformation", "Represents the transformation grid.")] [StorableClass] public class TransformationContent : PreprocessingContent, IViewShortcut { public static new Image StaticItemImage { get { return HeuristicLab.Common.Resources.VSImageLibrary.Method; } } [Storable] public IItemList TransformationList { get; private set; } #region Constructor, Cloning & Persistence public TransformationContent(IFilteredPreprocessingData preprocessingData) : base(preprocessingData) { TransformationList = new ItemList(); } public TransformationContent(TransformationContent original, Cloner cloner) : base(original, cloner) { TransformationList = cloner.Clone(original.TransformationList); } public override IDeepCloneable Clone(Cloner cloner) { return new TransformationContent(this, cloner); } [StorableConstructor] protected TransformationContent(bool deserializing) : base(deserializing) { } #endregion public bool ApplyTransformations(out IEnumerable errorMessages) { bool success = true; var errors = new List(); errorMessages = errors; foreach (var transformation in TransformationList.Where(x => !x.IsApplied)) { var sourceVariable = transformation.OriginalVariable; var targetVariable = transformation.TransformedVariable ?? sourceVariable + " Transformed"; var sourceIdx = PreprocessingData.GetColumnIndex(sourceVariable); if (transformation.Transformation is ITransformation trans && PreprocessingData.VariableHasType(sourceIdx)) { if (!PreprocessingData.VariableNames.Contains(targetVariable)) PreprocessingData.InsertColumn(targetVariable, PreprocessingData.Columns); var targetIdx = PreprocessingData.GetColumnIndex(targetVariable); if (!PreprocessingData.VariableHasType(targetIdx)) { success = false; errors.Add("Target column is not double."); continue; } var sourceData = PreprocessingData.GetValues(sourceIdx); if (!trans.Check(sourceData, out string msg)) { success = false; errors.Add(msg); continue; } trans.Configure(sourceData); var transformedData = trans.Apply(sourceData).ToList(); PreprocessingData.SetValues(targetIdx, transformedData); PreprocessingData.Transformations.Add(transformation); transformation.IsApplied = true; // TODO: remove unused valid values in constrainedParameters } else { success = false; errors.Add("Transformation datatype is not supported."); } } return success; } } }