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
RevLine 
[3884]1#region License Information
2/* HeuristicLab
[15583]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3884]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
[5829]21
[3884]22using System;
[5829]23using System.Collections.Generic;
[6612]24using System.Drawing;
25using System.Linq;
[3884]26using System.Windows.Forms;
[15870]27using HeuristicLab.Common.Resources;
[6692]28using HeuristicLab.Core;
[6652]29using HeuristicLab.Core.Views;
[3884]30using HeuristicLab.MainForm;
[6652]31using HeuristicLab.Optimization;
[10175]32using HeuristicLab.Persistence.Default.Xml;
[9973]33using HeuristicLab.PluginInfrastructure;
[3884]34
35namespace HeuristicLab.Problems.DataAnalysis.Views {
[9973]36
[5834]37  [View("DataAnalysisSolution View")]
[6652]38  [Content(typeof(DataAnalysisSolution), false)]
[9974]39  public partial class DataAnalysisSolutionView : NamedItemCollectionView<IResult> {
[15870]40
41    private const string BacktransformButtonText = "Integrate Transformations";
42    private const string ReapplyTransformationsButtonText = "Restore Original Model";
43
[3884]44    public DataAnalysisSolutionView() {
45      InitializeComponent();
[6652]46      viewHost.ViewsLabelVisible = false;
[15870]47      transformButton.Image = VSImageLibrary.Event;
[3884]48    }
49
[5829]50    public new DataAnalysisSolution Content {
51      get { return (DataAnalysisSolution)base.Content; }
[3884]52      set { base.Content = value; }
53    }
54
[6666]55    protected override void SetEnabledStateOfControls() {
56      base.SetEnabledStateOfControls();
57      addButton.Enabled = false;
58      removeButton.Enabled = false;
[10540]59      loadProblemDataButton.Enabled = Content != null && !Locked;
[15870]60      transformButton.Enabled = Content != null && !Locked;
61      transformButton.Visible = Content != null && Content.ProblemData.Transformations.Any();
[6666]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
[3884]76    protected override void OnContentChanged() {
[5829]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
[3884]81      base.OnContentChanged();
[6642]82      AddEvaluationViewTypes();
[5829]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        }
[3884]90      }
[15870]91
92      if (Content == null) return;
93
94      transformButton.Text = Content.Model is IDataAnalysisTransformationModel
95        ? ReapplyTransformationsButtonText : BacktransformButtonText;
[3884]96    }
97
[6652]98    protected override IResult CreateItem() {
99      return null;
100    }
101
[6642]102    protected virtual void AddEvaluationViewTypes() {
[6666]103      if (Content != null && !Content.ProblemData.IsEmpty) {
[6642]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
[14770]111    protected sealed override void itemsListView_DoubleClick(object sender, EventArgs e) {
[6652]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) {
[14770]117        IContentView view = MainFormManager.MainForm.ShowContent(result.Value);
[6652]118        if (view != null) {
[14770]119          view.Caption = result.Name;
[6652]120          view.ReadOnly = ReadOnly;
121          view.Locked = Locked;
122        }
123      } else if (viewType != null) {
[5829]124        MainFormManager.MainForm.ShowContent(Content, viewType);
[6652]125      }
[3884]126    }
[3915]127
[5829]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);
[3884]136    }
[3915]137
[10175]138    protected virtual void loadProblemDataButton_Click(object sender, EventArgs e) {
139      if (loadProblemDataFileDialog.ShowDialog(this) != DialogResult.OK) return;
[10540]140      try {
141        object hlFile = XmlParser.Deserialize(loadProblemDataFileDialog.FileName);
[9973]142
[10540]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        }
[10175]151
[10540]152        if (problemData == null)
153          throw new InvalidOperationException("The chosen HeuristicLab file does not contain a ProblemData, Problem, or DataAnalysisSolution.");
[10175]154
155        var solution = (IDataAnalysisSolution)Content.Clone();
[10540]156        problemData.AdjustProblemDataProperties(solution.ProblemData);
[15395]157
[10175]158        solution.ProblemData = problemData;
[10540]159        if (!solution.Name.EndsWith(" with loaded problemData"))
160          solution.Name += " with loaded problemData";
[10175]161        MainFormManager.MainForm.ShowContent(solution);
[15870]162      } catch (InvalidOperationException invalidOperationException) {
[10540]163        ErrorHandling.ShowErrorDialog(this, invalidOperationException);
[15870]164      } catch (ArgumentException argumentException) {
[10540]165        ErrorHandling.ShowErrorDialog(this, argumentException);
166      }
[9973]167    }
168
[6612]169    protected void AddViewListViewItem(Type viewType, Image image) {
[5829]170      ListViewItem listViewItem = new ListViewItem();
171      listViewItem.Text = ViewAttribute.GetViewName(viewType);
[6612]172      itemsListView.SmallImageList.Images.Add(image);
[5829]173      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
174      listViewItem.Tag = viewType;
175      itemsListView.Items.Add(listViewItem);
176
177      AdjustListViewColumnSizes();
[3915]178    }
[5829]179
180    protected void RemoveViewListViewItem(Type viewType) {
[6612]181      List<ListViewItem> itemsToRemove = itemsListView.Items.Cast<ListViewItem>().Where(item => item.Tag as Type == viewType).ToList();
[5829]182
183      foreach (ListViewItem item in itemsToRemove)
184        itemsListView.Items.Remove(item);
[3884]185    }
[6653]186
[8125]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
[8798]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
[6653]211    #region drag and drop
212    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
213      validDragOperation = false;
[6692]214      if (ReadOnly) return;
[10540]215      if (e.Effect != DragDropEffects.Copy) return;
[6692]216
217      var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
[10540]218      if (dropData is IDataAnalysisProblemData) validDragOperation = true;
[10173]219      else if (dropData is IDataAnalysisProblem) validDragOperation = true;
[6692]220      else if (dropData is IValueParameter) {
221        var param = (IValueParameter)dropData;
[10173]222        if (param.Value is IDataAnalysisProblemData) validDragOperation = true;
[6653]223      }
224    }
225
226    protected override void itemsListView_DragDrop(object sender, DragEventArgs e) {
[15395]227      if (e.Effect != DragDropEffects.Copy) return;
[10174]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;
[6653]238      }
[10174]239      if (problemData == null) return;
240
[15395]241      problemData = (IDataAnalysisProblemData)problemData.Clone();
242
[10540]243      try {
244        problemData.AdjustProblemDataProperties(Content.ProblemData);
245        Content.ProblemData = problemData;
[10174]246
[10540]247        if (!Content.Name.EndsWith(" with changed problemData"))
248          Content.Name += " with changed problemData";
[15486]249        Content.Filename = string.Empty;
250        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
[15870]251      } catch (InvalidOperationException invalidOperationException) {
[10540]252        ErrorHandling.ShowErrorDialog(this, invalidOperationException);
[15870]253      } catch (ArgumentException argumentException) {
[10540]254        ErrorHandling.ShowErrorDialog(this, argumentException);
255      }
[10174]256    }
257    #endregion
[10540]258
[15870]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);
[3884]297  }
298}
Note: See TracBrowser for help on using the repository browser.