Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2559

  • Adapted import and export for preprocessing.
  • Added MenuItem to be able to open Preprocessing without creating a DataAnalysisProblem before.
  • Added coloring in ScatterPlot.
  • Removed IPreprocessingContext interface.
  • Reformatted code:
    • Added missing copyright headers.
    • Corrected namespaces.
    • Deleted unnecessary usings.
    • Applied correct formatting.
File size: 5.1 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  public class PreprocessingContext : NamedItem, IStorableContent {
33
34    public string Filename { get; set; }
35
36    public IEnumerable<KeyValuePair<string, Func<IItem>>> ExportPossibilities {
37      get {
38        var algorithm = Source as IAlgorithm;
39        if (algorithm != null)
40          yield return new KeyValuePair<string, Func<IItem>>(algorithm.Name, () => ExportAlgorithm(algorithm));
41
42        var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : Source as IDataAnalysisProblem;
43        if (problem != null)
44          yield return new KeyValuePair<string, Func<IItem>>(problem.Name, () => ExportProblem(problem));
45
46        var problemData = problem != null ? problem.ProblemData : Source as IDataAnalysisProblemData;
47        if (problemData != null)
48          yield return new KeyValuePair<string, Func<IItem>>(problemData.Name, () => ExportProblemData(problemData));
49
50        // ToDo: Export CSV
51      }
52    }
53    public bool CanExport {
54      get { return Source is IAlgorithm || Source is IDataAnalysisProblem || Source is IDataAnalysisProblemData; }
55    }
56
57    [Storable]
58    public IFilteredPreprocessingData Data { get; private set; }
59
60    [Storable]
61    private IItem Source { get; set; }
62
63
64    public PreprocessingContext() : this(new RegressionProblemData()) { }
65    public PreprocessingContext(IItem source) {
66      Import(source);
67
68    }
69
70    [StorableConstructor]
71    private PreprocessingContext(bool deserializing)
72      : base(deserializing) { }
73    protected PreprocessingContext(PreprocessingContext original, Cloner cloner)
74      : base(original, cloner) {
75      Source = cloner.Clone(original.Source);
76      Data = cloner.Clone(original.Data);
77    }
78
79    public override IDeepCloneable Clone(Cloner cloner) {
80      return new PreprocessingContext(this, cloner);
81    }
82
83    #region Import
84    public void Import(IItem source) {
85      Source = source;
86
87      var dataSource = ExtractProblemData(source);
88      Data = new FilteredPreprocessingData(new TransactionalPreprocessingData(dataSource));
89    }
90
91    private IDataAnalysisProblemData ExtractProblemData(IItem source) {
92      var algorithm = source as Algorithm;
93      var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : source as IDataAnalysisProblem;
94      var problemData = problem != null ? problem.ProblemData : source as IDataAnalysisProblemData;
95      return problemData;
96    }
97    #endregion
98
99    #region Export
100    public IItem Export() {
101      var creator = new ProblemDataCreator(this);
102      if (Source is IAlgorithm)
103        return ExportAlgorithm((IAlgorithm)Source);
104      if (Source is IDataAnalysisProblem)
105        return ExportProblem((IDataAnalysisProblem)Source);
106      if (Source is IDataAnalysisProblemData)
107        return ExportProblemData((IDataAnalysisProblemData)Source);
108      return null;
109    }
110    private IAlgorithm ExportAlgorithm(IAlgorithm source) {
111      var preprocessedAlgorithm = (IAlgorithm)source.Clone();
112      preprocessedAlgorithm.Name = preprocessedAlgorithm.Name + "(Preprocessed)";
113      preprocessedAlgorithm.Runs.Clear();
114      var problem = (IDataAnalysisProblem)preprocessedAlgorithm.Problem;
115      SetNewProblemData(problem);
116      return preprocessedAlgorithm;
117    }
118    private IDataAnalysisProblem ExportProblem(IDataAnalysisProblem source) {
119      var preprocessedProblem = (IDataAnalysisProblem)source.Clone();
120      SetNewProblemData(preprocessedProblem);
121      return preprocessedProblem;
122    }
123    private IDataAnalysisProblemData ExportProblemData(IDataAnalysisProblemData source) {
124      var creator = new ProblemDataCreator(this);
125      return creator.CreateProblemData(source);
126    }
127    private void SetNewProblemData(IDataAnalysisProblem problem) {
128      var data = ExtractProblemData(problem.ProblemData);
129      problem.ProblemDataParameter.ActualValue = data;
130      problem.Name = "Preprocessed " + problem.Name;
131    }
132    #endregion
133  }
134}
Note: See TracBrowser for help on using the repository browser.