Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2559

  • Enabled type selection for creating/importing/exporting/applying.
  • Deleted unnecessary interfaces.
  • Reorganized source files of DataPreprocessing.
File size: 5.8 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    [Storable]
38    public IFilteredPreprocessingData Data { get; private set; }
39
40    [Storable]
41    private IItem Source { get; set; }
42
43    public event EventHandler Reset;
44
45    #region Constructors
46    public PreprocessingContext()
47      : this(new RegressionProblemData()) {
48    }
49    public PreprocessingContext(IDataAnalysisProblemData problemData, IItem source = null)
50      : base("Data Preprocessing") {
51      if (problemData == null) throw new ArgumentNullException("problemData");
52      Import(problemData, source);
53    }
54
55    [StorableConstructor]
56    protected PreprocessingContext(bool deserializing)
57      : base(deserializing) { }
58    protected PreprocessingContext(PreprocessingContext original, Cloner cloner)
59      : base(original, cloner) {
60      Source = cloner.Clone(original.Source);
61      Data = cloner.Clone(original.Data);
62    }
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new PreprocessingContext(this, cloner);
65    }
66    #endregion
67
68    #region Import
69    public void Import(IDataAnalysisProblemData problemData, IItem source = null) {
70      if (problemData == null) throw new ArgumentNullException("problemData");
71      if (source != null && ExtractProblemData(source) != problemData)
72        throw new ArgumentException("The ProblemData extracted from the Source is different than the given ProblemData.");
73      Source = source ?? problemData;
74      var namedSource = Source as INamedItem;
75      if (namedSource != null)
76        Name = "Preprocessing " + namedSource.Name;
77      Data = new FilteredPreprocessingData(new TransactionalPreprocessingData(problemData));
78      OnReset();
79      // Reset GUI:
80      // - OnContentChanged for PreprocessingView!
81      // event? task(async import)?
82    }
83    private IDataAnalysisProblemData ExtractProblemData(IItem source) {
84      var algorithm = source as Algorithm;
85      var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : source as IDataAnalysisProblem;
86      var problemData = problem != null ? problem.ProblemData : source as IDataAnalysisProblemData;
87      return problemData;
88    }
89    #endregion
90
91    #region Export
92    public bool CanExport {
93      get { return Source is IAlgorithm || Source is IDataAnalysisProblem || Source is IDataAnalysisProblemData; }
94    }
95    public IEnumerable<KeyValuePair<string, Func<IItem>>> GetSourceExportOptions() {
96      var algorithm = Source as IAlgorithm;
97      if (algorithm != null)
98        yield return new KeyValuePair<string, Func<IItem>>(
99          algorithm.GetType().GetPrettyName(),
100          () => ExportAlgorithm(algorithm));
101
102      var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : Source as IDataAnalysisProblem;
103      if (problem != null)
104        yield return new KeyValuePair<string, Func<IItem>>(
105          problem.GetType().GetPrettyName(),
106          () => ExportProblem(problem));
107
108      var problemData = problem != null ? problem.ProblemData : Source as IDataAnalysisProblemData;
109      if (problemData != null)
110        yield return new KeyValuePair<string, Func<IItem>>(
111          problemData.GetType().GetPrettyName(),
112          () => ExportProblemData(problemData));
113    }
114
115    private IAlgorithm ExportAlgorithm(IAlgorithm source) {
116      var preprocessedAlgorithm = (IAlgorithm)source.Clone();
117      preprocessedAlgorithm.Name = preprocessedAlgorithm.Name + "(Preprocessed)";
118      preprocessedAlgorithm.Runs.Clear();
119      var problem = (IDataAnalysisProblem)preprocessedAlgorithm.Problem;
120      problem.ProblemDataParameter.ActualValue = CreateNewProblemData();
121      return preprocessedAlgorithm;
122    }
123    private IDataAnalysisProblem ExportProblem(IDataAnalysisProblem source) {
124      var preprocessedProblem = (IDataAnalysisProblem)source.Clone();
125      preprocessedProblem.ProblemDataParameter.ActualValue = CreateNewProblemData();
126      return preprocessedProblem;
127    }
128    private IDataAnalysisProblemData ExportProblemData(IDataAnalysisProblemData source) {
129      return CreateNewProblemData();
130    }
131
132    public IDataAnalysisProblemData CreateNewProblemData() {
133      var creator = new ProblemDataCreator(this);
134      var oldProblemData = ExtractProblemData(Source);
135      var newProblemData = creator.CreateProblemData(oldProblemData);
136      newProblemData.Name = "Preprocessed " + oldProblemData.Name;
137      return newProblemData;
138    }
139    #endregion
140
141    protected virtual void OnReset() {
142      if (Reset != null)
143        Reset(this, EventArgs.Empty);
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.