Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing Enhancements/HeuristicLab.DataPreprocessing.Views/3.4/DataPreprocessingView.cs @ 14462

Last change on this file since 14462 was 14462, checked in by pfleck, 7 years ago

#2709

  • Added a separate MultiScatterPlot entry and removed the ViewHost views-icon instead.
  • Moved legend of DataCompletenessChart to the top and removed the title instead.
File size: 11.6 KB
RevLine 
[10539]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10539]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;
[13508]23using System.IO;
24using System.Threading.Tasks;
[10239]25using System.Windows.Forms;
26using HeuristicLab.Common;
[10625]27using HeuristicLab.Core;
[10254]28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
[13508]30using HeuristicLab.Problems.DataAnalysis;
31using HeuristicLab.Problems.Instances.DataAnalysis;
32using HeuristicLab.Problems.Instances.DataAnalysis.Views;
[10239]33
[10558]34namespace HeuristicLab.DataPreprocessing.Views {
[10239]35  [View("DataPreprocessing View")]
[10614]36  [Content(typeof(PreprocessingContext), true)]
[13507]37  public partial class DataPreprocessingView : NamedItemView {
[10998]38
[10239]39    public DataPreprocessingView() {
40      InitializeComponent();
[10258]41    }
[10239]42
[13502]43    public new PreprocessingContext Content {
44      get { return (PreprocessingContext)base.Content; }
[10617]45      set { base.Content = value; }
[10239]46    }
47
[10614]48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      if (Content != null) {
[10617]51        var data = Content.Data;
[12676]52        var filterLogic = new FilterLogic(data);
53        var searchLogic = new SearchLogic(data, filterLogic);
[10617]54        var statisticsLogic = new StatisticsLogic(data, searchLogic);
[11002]55        var manipulationLogic = new ManipulationLogic(data, searchLogic, statisticsLogic);
[10970]56
[10904]57        var viewShortcuts = new ItemList<IViewShortcut> {
[11002]58          new DataGridContent(data, manipulationLogic, filterLogic),
[10617]59          new StatisticsContent(statisticsLogic),
[10921]60
[10992]61          new LineChartContent(data),
62          new HistogramContent(data),
63          new ScatterPlotContent(data),
[14462]64          new MultiScatterPlotContent(data),
[10934]65          new CorrelationMatrixContent(Content),
[11002]66          new DataCompletenessChartContent(searchLogic),
[13502]67
[10921]68          new FilterContent(filterLogic),
[11002]69          new ManipulationContent(manipulationLogic, searchLogic, filterLogic),
[10977]70          new TransformationContent(data, filterLogic)
[10617]71        };
[10625]72
[10904]73        viewShortcutListView.Content = viewShortcuts.AsReadOnly();
74        viewShortcutListView.ItemsListView.Items[0].Selected = true;
75        viewShortcutListView.Select();
[10735]76
[13508]77        applyTypeContextMenuStrip.Items.Clear();
78        exportTypeContextMenuStrip.Items.Clear();
79        foreach (var exportOption in Content.GetSourceExportOptions()) {
80          var applyMenuItem = new ToolStripMenuItem(exportOption.Key) { Tag = exportOption.Value };
81          applyMenuItem.Click += applyToolStripMenuItem_Click;
82          applyTypeContextMenuStrip.Items.Add(applyMenuItem);
83
84          var exportMenuItem = new ToolStripMenuItem(exportOption.Key) { Tag = exportOption.Value };
85          exportMenuItem.Click += exportToolStripMenuItem_Click;
86          exportTypeContextMenuStrip.Items.Add(exportMenuItem);
87        }
88        var exportCsvMenuItem = new ToolStripMenuItem(".csv");
89        exportCsvMenuItem.Click += exportCsvMenuItem_Click;
90        exportTypeContextMenuStrip.Items.Add(exportCsvMenuItem);
[10614]91      } else {
[10904]92        viewShortcutListView.Content = null;
[10254]93      }
[10239]94    }
[10969]95    protected override void RegisterContentEvents() {
96      base.RegisterContentEvents();
[13508]97      Content.Reset += Content_Reset;
[10969]98      Content.Data.FilterChanged += Data_FilterChanged;
99    }
100
101    protected override void DeregisterContentEvents() {
102      base.DeregisterContentEvents();
[13508]103      Content.Reset -= Content_Reset;
[10969]104      Content.Data.FilterChanged -= Data_FilterChanged;
105    }
106
[13508]107    void Content_Reset(object sender, EventArgs e) {
108      OnContentChanged(); // Reset by setting new content
109    }
110
[10969]111    void Data_FilterChanged(object sender, EventArgs e) {
112      lblFilterActive.Visible = Content.Data.IsFiltered;
113    }
114
[10614]115    protected override void SetEnabledStateOfControls() {
116      base.SetEnabledStateOfControls();
[10904]117      viewShortcutListView.Enabled = Content != null;
[10614]118      applyInNewTabButton.Enabled = Content != null;
[13502]119      exportProblemButton.Enabled = Content != null && Content.CanExport;
[10614]120      undoButton.Enabled = Content != null;
[10239]121    }
122
[13508]123    #region New
124    private void newButton_Click(object sender, EventArgs e) {
125      newProblemDataTypeContextMenuStrip.Show(Cursor.Position);
126    }
127    private void newRegressionToolStripMenuItem_Click(object sender, EventArgs e) {
128      Content.Import(new RegressionProblemData());
129    }
130    private void newClassificationToolStripMenuItem_Click(object sender, EventArgs e) {
131      Content.Import(new ClassificationProblemData());
132    }
133    private void newTimeSeriesToolStripMenuItem_Click(object sender, EventArgs e) {
134      Content.Import(new TimeSeriesPrognosisProblemData());
135    }
136    #endregion
137
138    #region Import
139    private void importButton_Click(object sender, EventArgs e) {
140      importProblemDataTypeContextMenuStrip.Show(Cursor.Position);
141    }
142    private void importRegressionToolStripMenuItem_Click(object sender, EventArgs e) {
143      Import(new RegressionCSVInstanceProvider(), new RegressionImportTypeDialog(),
144        (dialog => ((RegressionImportTypeDialog)dialog).ImportType));
145    }
146    private void importClassificationToolStripMenuItem_Click(object sender, EventArgs e) {
147      Import(new ClassificationCSVInstanceProvider(), new ClassificationImportTypeDialog(),
148        (dialog => ((ClassificationImportTypeDialog)dialog).ImportType));
149    }
150    private void importTimeSeriesToolStripMenuItem_Click(object sender, EventArgs e) {
151      Import(new TimeSeriesPrognosisCSVInstanceProvider(), new TimeSeriesPrognosisImportTypeDialog(),
152        (dialog => ((TimeSeriesPrognosisImportTypeDialog)dialog).ImportType));
153    }
154    private void Import<TProblemData, TImportType>(DataAnalysisInstanceProvider<TProblemData, TImportType> instanceProvider, DataAnalysisImportTypeDialog importTypeDialog,
155      Func<DataAnalysisImportTypeDialog, TImportType> getImportType)
156      where TProblemData : class, IDataAnalysisProblemData
157      where TImportType : DataAnalysisImportType {
158      if (importTypeDialog.ShowDialog() == DialogResult.OK) {
159        Task.Run(() => {
160          TProblemData instance;
161          var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
162          // lock active view and show progress bar
163          var activeView = (IContentView)MainFormManager.MainForm.ActiveView;
164
165          try {
166            var progress = mainForm.AddOperationProgressToContent(activeView.Content, "Loading problem instance.");
167
168            instanceProvider.ProgressChanged += (o, args) => { progress.ProgressValue = args.ProgressPercentage / 100.0; };
169
170            instance = instanceProvider.ImportData(importTypeDialog.Path, getImportType(importTypeDialog), importTypeDialog.CSVFormat);
171          } catch (IOException ex) {
172            MessageBox.Show(string.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
173            mainForm.RemoveOperationProgressFromContent(activeView.Content);
174            return;
175          }
176          try {
177            Content.Import(instance);
178          } catch (IOException ex) {
179            MessageBox.Show(string.Format("This problem does not support loading the instance {0}: {1}", Path.GetFileName(importTypeDialog.Path), Environment.NewLine + ex.Message), "Cannot load instance");
180          } finally {
181            mainForm.RemoveOperationProgressFromContent(activeView.Content);
182          }
183        });
184      }
185    }
186    #endregion
187
188    #region Apply
189    private void applyInNewTabButton_Click(object sender, EventArgs e) {
190      applyTypeContextMenuStrip.Show(Cursor.Position);
191    }
192    private void applyToolStripMenuItem_Click(object sender, EventArgs e) {
193      var menuItem = (ToolStripMenuItem)sender;
194      var itemCreator = (Func<IItem>)menuItem.Tag;
195      MainFormManager.MainForm.ShowContent(itemCreator());
196    }
197    #endregion
198
199    #region Export
[10304]200    private void exportProblemButton_Click(object sender, EventArgs e) {
[13508]201      exportTypeContextMenuStrip.Show(Cursor.Position);
202    }
203    private void exportToolStripMenuItem_Click(object sender, EventArgs e) {
204      var menuItem = (ToolStripMenuItem)sender;
205      var itemCreator = (Func<IItem>)menuItem.Tag;
[10307]206      var saveFileDialog = new SaveFileDialog {
207        Title = "Save Item",
208        DefaultExt = "hl",
209        Filter = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*",
210        FilterIndex = 2
211      };
212
213      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
[13508]214        Task.Run(() => {
215          bool compressed = saveFileDialog.FilterIndex != 1;
216          var storable = itemCreator() as IStorableContent;
217          if (storable != null) {
218            var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
219            var activeView = (IContentView)MainFormManager.MainForm.ActiveView;
220            try {
221              mainForm.AddOperationProgressToContent(activeView.Content, "Exporting data.");
222              ContentManager.Save(storable, saveFileDialog.FileName, compressed);
223            } finally {
224              mainForm.RemoveOperationProgressFromContent(activeView.Content);
225            }
226          }
227        });
[10307]228      }
[10304]229    }
[13508]230    private void exportCsvMenuItem_Click(object sender, EventArgs e) {
231      var saveFileDialog = new SaveFileDialog {
232        Title = "Save Data",
233        DefaultExt = "csv",
234        Filter = "CSV files|*.csv|All files|*.*",
235        FilterIndex = 1
236      };
[10304]237
[13508]238      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
239        Task.Run(() => {
240          var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
241          var activeView = (IContentView)MainFormManager.MainForm.ActiveView;
242          try {
243            var problemData = Content.CreateNewProblemData();
244            mainForm.AddOperationProgressToContent(activeView.Content, "Exporting data.");
245            if (problemData is TimeSeriesPrognosisProblemData)
246              Export(new TimeSeriesPrognosisCSVInstanceProvider(), problemData, saveFileDialog.FileName);
247            else if (problemData is RegressionProblemData)
248              Export(new RegressionCSVInstanceProvider(), problemData, saveFileDialog.FileName);
249            else if (problemData is ClassificationProblemData)
250              Export(new ClassificationCSVInstanceProvider(), problemData, saveFileDialog.FileName);
251          } finally {
252            mainForm.RemoveOperationProgressFromContent(activeView.Content);
253          }
254        });
255      }
[10254]256    }
[13508]257    private void Export<TProblemData, TImportType>(DataAnalysisInstanceProvider<TProblemData, TImportType> instanceProvider,
258      IDataAnalysisProblemData problemData, string path)
259      where TProblemData : class, IDataAnalysisProblemData where TImportType : DataAnalysisImportType {
260      instanceProvider.ExportData((TProblemData)problemData, path);
261    }
262    #endregion
[10550]263
[13508]264    #region Undo / Redo
[10550]265    private void undoButton_Click(object sender, EventArgs e) {
266      Content.Data.Undo();
267    }
[13508]268    #endregion
[10239]269  }
[10310]270}
Note: See TracBrowser for help on using the repository browser.