Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis.Views/3.4/Solution Views/DataAnalysisSolutionView.cs @ 15870

Last change on this file since 15870 was 15870, checked in by pfleck, 6 years ago

#2906

  • Implemented for classification, clustering, etc.
  • Simplified Transformation interfaces (read-only, ...).
  • Started moving transformation logic out of ProblemData.
File size: 13.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.Core;
29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31using HeuristicLab.Optimization;
32using HeuristicLab.Persistence.Default.Xml;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Problems.DataAnalysis.Views {
36
37  [View("DataAnalysisSolution View")]
38  [Content(typeof(DataAnalysisSolution), false)]
39  public partial class DataAnalysisSolutionView : NamedItemCollectionView<IResult> {
40
41    private const string BacktransformButtonText = "Integrate Transformations";
42    private const string ReapplyTransformationsButtonText = "Restore Original Model";
43
44    public DataAnalysisSolutionView() {
45      InitializeComponent();
46      viewHost.ViewsLabelVisible = false;
47      transformButton.Image = VSImageLibrary.Event;
48    }
49
50    public new DataAnalysisSolution Content {
51      get { return (DataAnalysisSolution)base.Content; }
52      set { base.Content = value; }
53    }
54
55    protected override void SetEnabledStateOfControls() {
56      base.SetEnabledStateOfControls();
57      addButton.Enabled = false;
58      removeButton.Enabled = false;
59      loadProblemDataButton.Enabled = Content != null && !Locked;
60      transformButton.Enabled = Content != null && !Locked;
61      transformButton.Visible = Content != null && Content.ProblemData.Transformations.Any();
62    }
63
64    protected override void RegisterContentEvents() {
65      base.RegisterContentEvents();
66      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
67    }
68    protected override void DeregisterContentEvents() {
69      base.DeregisterContentEvents();
70      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
71    }
72    private void Content_ProblemDataChanged(object sender, EventArgs e) {
73      OnContentChanged();
74    }
75
76    protected override void OnContentChanged() {
77      string selectedName = null;
78      if ((itemsListView.SelectedItems.Count == 1) && (itemsListView.SelectedItems[0].Tag != null && itemsListView.SelectedItems[0].Tag is Type))
79        selectedName = itemsListView.SelectedItems[0].Text;
80
81      base.OnContentChanged();
82      AddEvaluationViewTypes();
83
84      //recover selection
85      if (selectedName != null) {
86        foreach (ListViewItem item in itemsListView.Items) {
87          if (item.Tag != null && item.Tag is Type && item.Text == selectedName)
88            item.Selected = true;
89        }
90      }
91
92      if (Content == null) return;
93
94      transformButton.Text = Content.Model is IDataAnalysisTransformationModel
95        ? ReapplyTransformationsButtonText : BacktransformButtonText;
96    }
97
98    protected override IResult CreateItem() {
99      return null;
100    }
101
102    protected virtual void AddEvaluationViewTypes() {
103      if (Content != null && !Content.ProblemData.IsEmpty) {
104        var viewTypes = MainFormManager.GetViewTypes(Content.GetType(), true)
105          .Where(t => typeof(IDataAnalysisSolutionEvaluationView).IsAssignableFrom(t));
106        foreach (var viewType in viewTypes)
107          AddViewListViewItem(viewType, ((IDataAnalysisSolutionEvaluationView)Activator.CreateInstance(viewType)).ViewImage);
108      }
109    }
110
111    protected sealed override void itemsListView_DoubleClick(object sender, EventArgs e) {
112      if (itemsListView.SelectedItems.Count != 1) return;
113
114      IResult result = itemsListView.SelectedItems[0].Tag as IResult;
115      Type viewType = itemsListView.SelectedItems[0].Tag as Type;
116      if (result != null) {
117        IContentView view = MainFormManager.MainForm.ShowContent(result.Value);
118        if (view != null) {
119          view.Caption = result.Name;
120          view.ReadOnly = ReadOnly;
121          view.Locked = Locked;
122        }
123      } else if (viewType != null) {
124        MainFormManager.MainForm.ShowContent(Content, viewType);
125      }
126    }
127
128    protected override void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
129      if (itemsListView.SelectedItems.Count == 1 && itemsListView.SelectedItems[0].Tag is Type) {
130        detailsGroupBox.Enabled = true;
131        Type viewType = (Type)itemsListView.SelectedItems[0].Tag;
132        viewHost.ViewType = viewType;
133        viewHost.Content = Content;
134      } else
135        base.itemsListView_SelectedIndexChanged(sender, e);
136    }
137
138    protected virtual void loadProblemDataButton_Click(object sender, EventArgs e) {
139      if (loadProblemDataFileDialog.ShowDialog(this) != DialogResult.OK) return;
140      try {
141        object hlFile = XmlParser.Deserialize(loadProblemDataFileDialog.FileName);
142
143        IDataAnalysisProblemData problemData = null;
144        if (hlFile is IDataAnalysisProblemData) {
145          problemData = (IDataAnalysisProblemData)hlFile;
146        } else if (hlFile is IDataAnalysisProblem) {
147          problemData = ((IDataAnalysisProblem)hlFile).ProblemData;
148        } else if (hlFile is IDataAnalysisSolution) {
149          problemData = ((IDataAnalysisSolution)hlFile).ProblemData;
150        }
151
152        if (problemData == null)
153          throw new InvalidOperationException("The chosen HeuristicLab file does not contain a ProblemData, Problem, or DataAnalysisSolution.");
154
155        var solution = (IDataAnalysisSolution)Content.Clone();
156        problemData.AdjustProblemDataProperties(solution.ProblemData);
157
158        solution.ProblemData = problemData;
159        if (!solution.Name.EndsWith(" with loaded problemData"))
160          solution.Name += " with loaded problemData";
161        MainFormManager.MainForm.ShowContent(solution);
162      } catch (InvalidOperationException invalidOperationException) {
163        ErrorHandling.ShowErrorDialog(this, invalidOperationException);
164      } catch (ArgumentException argumentException) {
165        ErrorHandling.ShowErrorDialog(this, argumentException);
166      }
167    }
168
169    protected void AddViewListViewItem(Type viewType, Image image) {
170      ListViewItem listViewItem = new ListViewItem();
171      listViewItem.Text = ViewAttribute.GetViewName(viewType);
172      itemsListView.SmallImageList.Images.Add(image);
173      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
174      listViewItem.Tag = viewType;
175      itemsListView.Items.Add(listViewItem);
176
177      AdjustListViewColumnSizes();
178    }
179
180    protected void RemoveViewListViewItem(Type viewType) {
181      List<ListViewItem> itemsToRemove = itemsListView.Items.Cast<ListViewItem>().Where(item => item.Tag as Type == viewType).ToList();
182
183      foreach (ListViewItem item in itemsToRemove)
184        itemsListView.Items.Remove(item);
185    }
186
187    protected override void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
188      if (showDetailsCheckBox.Checked && itemsListView.SelectedItems.Count == 1 && itemsListView.SelectedItems[0].Tag is Type) {
189        Type viewType = (Type)itemsListView.SelectedItems[0].Tag;
190        viewHost.ViewType = viewType;
191        viewHost.Content = Content;
192        splitContainer.Panel2Collapsed = false;
193        detailsGroupBox.Enabled = true;
194      } else base.showDetailsCheckBox_CheckedChanged(sender, e);
195    }
196
197    protected override void RebuildImageList() {
198      itemsListView.SmallImageList.Images.Clear();
199      foreach (ListViewItem listViewItem in itemsListView.Items) {
200        IResult result = listViewItem.Tag as IResult;
201        Type viewType = listViewItem.Tag as Type;
202        if (result != null) itemsListView.SmallImageList.Images.Add(result.ItemImage);
203        else if (viewType != null && typeof(IDataAnalysisSolutionEvaluationView).IsAssignableFrom(viewType))
204          itemsListView.SmallImageList.Images.Add(((IDataAnalysisSolutionEvaluationView)Activator.CreateInstance(viewType)).ViewImage);
205        else itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
206
207        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
208      }
209    }
210
211    #region drag and drop
212    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
213      validDragOperation = false;
214      if (ReadOnly) return;
215      if (e.Effect != DragDropEffects.Copy) return;
216
217      var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
218      if (dropData is IDataAnalysisProblemData) validDragOperation = true;
219      else if (dropData is IDataAnalysisProblem) validDragOperation = true;
220      else if (dropData is IValueParameter) {
221        var param = (IValueParameter)dropData;
222        if (param.Value is IDataAnalysisProblemData) validDragOperation = true;
223      }
224    }
225
226    protected override void itemsListView_DragDrop(object sender, DragEventArgs e) {
227      if (e.Effect != DragDropEffects.Copy) return;
228
229      IDataAnalysisProblemData problemData = null;
230      var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
231      if (dropData is IDataAnalysisProblemData)
232        problemData = (IDataAnalysisProblemData)dropData;
233      else if (dropData is IDataAnalysisProblem)
234        problemData = ((IDataAnalysisProblem)dropData).ProblemData;
235      else if (dropData is IValueParameter) {
236        var param = (IValueParameter)dropData;
237        problemData = param.Value as DataAnalysisProblemData;
238      }
239      if (problemData == null) return;
240
241      problemData = (IDataAnalysisProblemData)problemData.Clone();
242
243      try {
244        problemData.AdjustProblemDataProperties(Content.ProblemData);
245        Content.ProblemData = problemData;
246
247        if (!Content.Name.EndsWith(" with changed problemData"))
248          Content.Name += " with changed problemData";
249        Content.Filename = string.Empty;
250        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
251      } catch (InvalidOperationException invalidOperationException) {
252        ErrorHandling.ShowErrorDialog(this, invalidOperationException);
253      } catch (ArgumentException argumentException) {
254        ErrorHandling.ShowErrorDialog(this, argumentException);
255      }
256    }
257    #endregion
258
259    private void transformButton_Click(object sender, EventArgs e) {
260      var transformedSolution = CreateTransformedSolution(Content);
261      MainFormManager.MainForm.ShowContent(transformedSolution);
262    }
263
264    private static IDataAnalysisSolution CreateTransformedSolution(IDataAnalysisSolution solution) {
265      if (solution.Model is IRegressionTransformationModel regressionTransformationModel && !(solution.Model is ITimeSeriesPrognosisTransformationModel)) {
266        var originalData = (IRegressionProblemData)((IRegressionSolution)solution).ProblemData.Transform();
267        return regressionTransformationModel.OriginalModel.CreateRegressionSolution(originalData);
268      } else if (solution.Model is IClassificationTransformationModel classificationTransformationModel) {
269        var originalData = (IClassificationProblemData)((IClassificationSolution)solution).ProblemData.Transform();
270        return classificationTransformationModel.OriginalModel.CreateClassificationSolution(originalData);
271      } else if (solution.Model is IRegressionModel regressionModel && !(solution.Model is ITimeSeriesPrognosisModel)) {
272        var transformationModel = new RegressionTransformationModel(regressionModel, solution.ProblemData.Transformations);
273        var transformedData = (IRegressionProblemData)((IRegressionSolution)solution).ProblemData.InverseTransform();
274        return transformationModel.CreateRegressionSolution(transformedData);
275      } else if (solution.Model is IClassificationModel classificationModel) {
276        var transformationModel = new ClassificationTransformationModel(classificationModel, solution.ProblemData.Transformations);
277        var transformedData = (IClassificationProblemData)((IClassificationSolution)solution).ProblemData.InverseTransform();
278        return transformationModel.CreateClassificationSolution(transformedData);
279      } else throw new NotSupportedException();
280    }
281
282    /*if (Content.Model is IDataAnalysisTransformationModel transformationModel) {
283      var originalModel = transformationModel.InverseTransform();
284      originalModel.CreateSolution();
285    } else {
286      var originalModel = Content.Model;
287      var transformationModel = originalModel.Transform();
288      transformationModel.CreateSolution();
289    }*/
290
291    ////Content.Model.Transform()
292
293    //var transformedModel = new DataAnalysisTransformationModel(Content.Model, Content.ProblemData.Transformations);
294    //var originalProblemData = (IRegressionProblemData)Content.ProblemData.InverseTransform();
295    //var transformedSolution = new TransformedRegressionSolution(transformedModel, originalProblemData);
296    //MainFormManager.MainForm.ShowContent(transformedSolution);
297  }
298}
Note: See TracBrowser for help on using the repository browser.