Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing.Views/3.4/DataPreprocessingView.cs @ 16749

Last change on this file since 16749 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

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