Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2845_EnhancedProgress/HeuristicLab.DataPreprocessing.Views/3.4/DataPreprocessingView.cs @ 15870

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

#2845

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