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

Last change on this file since 13507 was 13507, checked in by pfleck, 6 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
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.DataPreprocessing {
31  [Item("PreprocessingContext", "PreprocessingContext")]
32  [StorableClass]
33  public class PreprocessingContext : NamedItem, IStorableContent {
34
35    public string Filename { get; set; }
36
37    public IEnumerable<KeyValuePair<string, Func<IItem>>> ExportPossibilities {
38      get {
39        var algorithm = Source as IAlgorithm;
40        if (algorithm != null)
41          yield return new KeyValuePair<string, Func<IItem>>(algorithm.GetType().GetPrettyName(), () => ExportAlgorithm(algorithm));
42
43        var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : Source as IDataAnalysisProblem;
44        if (problem != null)
45          yield return new KeyValuePair<string, Func<IItem>>(problem.GetType().GetPrettyName(), () => ExportProblem(problem));
46
47        var problemData = problem != null ? problem.ProblemData : Source as IDataAnalysisProblemData;
48        if (problemData != null)
49          yield return new KeyValuePair<string, Func<IItem>>(problemData.GetType().GetPrettyName(), () => ExportProblemData(problemData));
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]
59    public IFilteredPreprocessingData Data { get; private set; }
60
61    [Storable]
62    private IItem Source { get; set; }
63
64
65    public PreprocessingContext() : this(new RegressionProblemData()) {
66      Name = "Data Preprocessing";
67    }
68    public PreprocessingContext(IItem source)
69      : base("Data Preprocessing") {
70      Import(source);
71    }
72
73    [StorableConstructor]
74    protected PreprocessingContext(bool deserializing)
75      : base(deserializing) { }
76    protected PreprocessingContext(PreprocessingContext original, Cloner cloner)
77      : base(original, cloner) {
78      Source = cloner.Clone(original.Source);
79      Data = cloner.Clone(original.Data);
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new PreprocessingContext(this, cloner);
84    }
85
86    #region Import
87    public void Import(IItem source) {
88      Source = source;
89      var namedSource = source as INamedItem;
90      if (namedSource != null) Name = "Preprocessing: " + namedSource.Name;
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
105    public IItem Export() {
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;
113    }
114    private IAlgorithm ExportAlgorithm(IAlgorithm source) {
115      var preprocessedAlgorithm = (IAlgorithm)source.Clone();
116      preprocessedAlgorithm.Name = preprocessedAlgorithm.Name + "(Preprocessed)";
117      preprocessedAlgorithm.Runs.Clear();
118      var problem = (IDataAnalysisProblem)preprocessedAlgorithm.Problem;
119      SetNewProblemData(problem);
120      return preprocessedAlgorithm;
121    }
122    private IDataAnalysisProblem ExportProblem(IDataAnalysisProblem source) {
123      var preprocessedProblem = (IDataAnalysisProblem)source.Clone();
124      SetNewProblemData(preprocessedProblem);
125      return preprocessedProblem;
126    }
127    private IDataAnalysisProblemData ExportProblemData(IDataAnalysisProblemData source) {
128      var creator = new ProblemDataCreator(this);
129      var preprocessedProblemData = creator.CreateProblemData(source);
130      preprocessedProblemData.Name = "Preprocessed " + source.Name;
131      return preprocessedProblemData;
132    }
133    private void SetNewProblemData(IDataAnalysisProblem problem) {
134      var data = ExtractProblemData(problem.ProblemData);
135      problem.ProblemDataParameter.ActualValue = data;
136      problem.Name = "Preprocessed " + problem.Name;
137    }
138    #endregion
139  }
140}
Note: See TracBrowser for help on using the repository browser.