Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2559
Fixed Plugin dependencies.
Fixed Storable and NamedItem stuff.

File size: 5.2 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.Name, () => 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.Name, () => 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.Name, () => 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    public PreprocessingContext(IItem source)
67      : base("PreprocessingContext") {
68      Import(source);
69    }
70
71    [StorableConstructor]
72    protected PreprocessingContext(bool deserializing)
73      : base(deserializing) { }
74    protected PreprocessingContext(PreprocessingContext original, Cloner cloner)
75      : base(original, cloner) {
76      Source = cloner.Clone(original.Source);
77      Data = cloner.Clone(original.Data);
78    }
79
80    public override IDeepCloneable Clone(Cloner cloner) {
81      return new PreprocessingContext(this, cloner);
82    }
83
84    #region Import
85    public void Import(IItem source) {
86      Source = source;
87
88      var dataSource = ExtractProblemData(source);
89      Data = new FilteredPreprocessingData(new TransactionalPreprocessingData(dataSource));
90    }
91
92    private IDataAnalysisProblemData ExtractProblemData(IItem source) {
93      var algorithm = source as Algorithm;
94      var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : source as IDataAnalysisProblem;
95      var problemData = problem != null ? problem.ProblemData : source as IDataAnalysisProblemData;
96      return problemData;
97    }
98    #endregion
99
100    #region Export
101    public IItem Export() {
102      var creator = new ProblemDataCreator(this);
103      if (Source is IAlgorithm)
104        return ExportAlgorithm((IAlgorithm)Source);
105      if (Source is IDataAnalysisProblem)
106        return ExportProblem((IDataAnalysisProblem)Source);
107      if (Source is IDataAnalysisProblemData)
108        return ExportProblemData((IDataAnalysisProblemData)Source);
109      return null;
110    }
111    private IAlgorithm ExportAlgorithm(IAlgorithm source) {
112      var preprocessedAlgorithm = (IAlgorithm)source.Clone();
113      preprocessedAlgorithm.Name = preprocessedAlgorithm.Name + "(Preprocessed)";
114      preprocessedAlgorithm.Runs.Clear();
115      var problem = (IDataAnalysisProblem)preprocessedAlgorithm.Problem;
116      SetNewProblemData(problem);
117      return preprocessedAlgorithm;
118    }
119    private IDataAnalysisProblem ExportProblem(IDataAnalysisProblem source) {
120      var preprocessedProblem = (IDataAnalysisProblem)source.Clone();
121      SetNewProblemData(preprocessedProblem);
122      return preprocessedProblem;
123    }
124    private IDataAnalysisProblemData ExportProblemData(IDataAnalysisProblemData source) {
125      var creator = new ProblemDataCreator(this);
126      return creator.CreateProblemData(source);
127    }
128    private void SetNewProblemData(IDataAnalysisProblem problem) {
129      var data = ExtractProblemData(problem.ProblemData);
130      problem.ProblemDataParameter.ActualValue = data;
131      problem.Name = "Preprocessed " + problem.Name;
132    }
133    #endregion
134  }
135}
Note: See TracBrowser for help on using the repository browser.