Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataPreprocessing/3.4/PreprocessingContext.cs @ 13507

Last change on this file since 13507 was 13507, checked in by pfleck, 8 years ago

#2559

  • Enabled different export types for preprocessing.
  • Hid unused buttons in ViewShortcutListView.
  • Made data preprocessing a NamedItem to allow naming.
File size: 5.5 KB
RevLine 
[10219]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10219]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
[13502]22using System;
23using System.Collections.Generic;
[10219]24using HeuristicLab.Common;
25using HeuristicLab.Core;
[10240]26using HeuristicLab.Optimization;
[13502]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[10221]28using HeuristicLab.Problems.DataAnalysis;
[10219]29
30namespace HeuristicLab.DataPreprocessing {
31  [Item("PreprocessingContext", "PreprocessingContext")]
[13504]32  [StorableClass]
[13502]33  public class PreprocessingContext : NamedItem, IStorableContent {
[10383]34
[13502]35    public string Filename { get; set; }
[10990]36
[13502]37    public IEnumerable<KeyValuePair<string, Func<IItem>>> ExportPossibilities {
38      get {
39        var algorithm = Source as IAlgorithm;
40        if (algorithm != null)
[13507]41          yield return new KeyValuePair<string, Func<IItem>>(algorithm.GetType().GetPrettyName(), () => ExportAlgorithm(algorithm));
[13502]42
43        var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : Source as IDataAnalysisProblem;
44        if (problem != null)
[13507]45          yield return new KeyValuePair<string, Func<IItem>>(problem.GetType().GetPrettyName(), () => ExportProblem(problem));
[13502]46
47        var problemData = problem != null ? problem.ProblemData : Source as IDataAnalysisProblemData;
48        if (problemData != null)
[13507]49          yield return new KeyValuePair<string, Func<IItem>>(problemData.GetType().GetPrettyName(), () => ExportProblemData(problemData));
[13502]50
51        // ToDo: Export CSV
52      }
53    }
54    public bool CanExport {
55      get { return Source is IAlgorithm || Source is IDataAnalysisProblem || Source is IDataAnalysisProblemData; }
56    }
57
58    [Storable]
[10783]59    public IFilteredPreprocessingData Data { get; private set; }
[10990]60
[13502]61    [Storable]
62    private IItem Source { get; set; }
[10614]63
[10990]64
[13507]65    public PreprocessingContext() : this(new RegressionProblemData()) {
66      Name = "Data Preprocessing";
67    }
[13504]68    public PreprocessingContext(IItem source)
[13507]69      : base("Data Preprocessing") {
[13502]70      Import(source);
[10219]71    }
72
[13502]73    [StorableConstructor]
[13504]74    protected PreprocessingContext(bool deserializing)
[13502]75      : base(deserializing) { }
[11114]76    protected PreprocessingContext(PreprocessingContext original, Cloner cloner)
[10219]77      : base(original, cloner) {
[13502]78      Source = cloner.Clone(original.Source);
[10219]79      Data = cloner.Clone(original.Data);
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new PreprocessingContext(this, cloner);
84    }
[10383]85
[13502]86    #region Import
87    public void Import(IItem source) {
88      Source = source;
[13507]89      var namedSource = source as INamedItem;
90      if (namedSource != null) Name = "Preprocessing: " + namedSource.Name;
[13502]91
92      var dataSource = ExtractProblemData(source);
93      Data = new FilteredPreprocessingData(new TransactionalPreprocessingData(dataSource));
94    }
95
96    private IDataAnalysisProblemData ExtractProblemData(IItem source) {
97      var algorithm = source as Algorithm;
98      var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : source as IDataAnalysisProblem;
99      var problemData = problem != null ? problem.ProblemData : source as IDataAnalysisProblemData;
100      return problemData;
101    }
102    #endregion
103
104    #region Export
[10990]105    public IItem Export() {
[13502]106      if (Source is IAlgorithm)
107        return ExportAlgorithm((IAlgorithm)Source);
108      if (Source is IDataAnalysisProblem)
109        return ExportProblem((IDataAnalysisProblem)Source);
110      if (Source is IDataAnalysisProblemData)
111        return ExportProblemData((IDataAnalysisProblemData)Source);
112      return null;
[10383]113    }
[13502]114    private IAlgorithm ExportAlgorithm(IAlgorithm source) {
115      var preprocessedAlgorithm = (IAlgorithm)source.Clone();
[10990]116      preprocessedAlgorithm.Name = preprocessedAlgorithm.Name + "(Preprocessed)";
[13502]117      preprocessedAlgorithm.Runs.Clear();
[10990]118      var problem = (IDataAnalysisProblem)preprocessedAlgorithm.Problem;
119      SetNewProblemData(problem);
120      return preprocessedAlgorithm;
[10614]121    }
[13502]122    private IDataAnalysisProblem ExportProblem(IDataAnalysisProblem source) {
123      var preprocessedProblem = (IDataAnalysisProblem)source.Clone();
[10990]124      SetNewProblemData(preprocessedProblem);
125      return preprocessedProblem;
126    }
[13502]127    private IDataAnalysisProblemData ExportProblemData(IDataAnalysisProblemData source) {
128      var creator = new ProblemDataCreator(this);
[13507]129      var preprocessedProblemData = creator.CreateProblemData(source);
130      preprocessedProblemData.Name = "Preprocessed " + source.Name;
131      return preprocessedProblemData;
[10383]132    }
[10990]133    private void SetNewProblemData(IDataAnalysisProblem problem) {
[13502]134      var data = ExtractProblemData(problem.ProblemData);
[10383]135      problem.ProblemDataParameter.ActualValue = data;
[10536]136      problem.Name = "Preprocessed " + problem.Name;
[10383]137    }
[13502]138    #endregion
[10219]139  }
140}
Note: See TracBrowser for help on using the repository browser.