Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2709

  • Removed some groupboxes in ViewShortcutListView.
  • Removed unnecessary IViewChartShortcut
  • Split ScatterPlot Multi and Single in to separate contents.
  • Renamed Color-combo box in Scatterplot to "Group".
File size: 11.6 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
39    public DataPreprocessingView() {
40      InitializeComponent();
41    }
42
43    public new PreprocessingContext Content {
44      get { return (PreprocessingContext)base.Content; }
45      set { base.Content = value; }
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      if (Content != null) {
51        var data = Content.Data;
52        var filterLogic = new FilterLogic(data);
53        var searchLogic = new SearchLogic(data, filterLogic);
54        var statisticsLogic = new StatisticsLogic(data, searchLogic);
55        var manipulationLogic = new ManipulationLogic(data, searchLogic, statisticsLogic);
56
57        var viewShortcuts = new ItemList<IViewShortcut> {
58          new DataGridContent(data, manipulationLogic, filterLogic),
59          new StatisticsContent(statisticsLogic),
60
61          new LineChartContent(data),
62          new HistogramContent(data),
63          new SingleScatterPlotContent(data),
64          new MultiScatterPlotContent(data),
65          new CorrelationMatrixContent(Content),
66          new DataCompletenessChartContent(searchLogic),
67
68          new FilterContent(filterLogic),
69          new ManipulationContent(manipulationLogic, searchLogic, filterLogic),
70          new TransformationContent(data, filterLogic)
71        };
72
73        viewShortcutListView.Content = viewShortcuts.AsReadOnly();
74        viewShortcutListView.ItemsListView.Items[0].Selected = true;
75        viewShortcutListView.Select();
76
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);
91      } else {
92        viewShortcutListView.Content = null;
93      }
94    }
95    protected override void RegisterContentEvents() {
96      base.RegisterContentEvents();
97      Content.Reset += Content_Reset;
98      Content.Data.FilterChanged += Data_FilterChanged;
99    }
100
101    protected override void DeregisterContentEvents() {
102      base.DeregisterContentEvents();
103      Content.Reset -= Content_Reset;
104      Content.Data.FilterChanged -= Data_FilterChanged;
105    }
106
107    void Content_Reset(object sender, EventArgs e) {
108      OnContentChanged(); // Reset by setting new content
109    }
110
111    void Data_FilterChanged(object sender, EventArgs e) {
112      lblFilterActive.Visible = Content.Data.IsFiltered;
113    }
114
115    protected override void SetEnabledStateOfControls() {
116      base.SetEnabledStateOfControls();
117      viewShortcutListView.Enabled = Content != null;
118      applyInNewTabButton.Enabled = Content != null;
119      exportProblemButton.Enabled = Content != null && Content.CanExport;
120      undoButton.Enabled = Content != null;
121    }
122
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
200    private void exportProblemButton_Click(object sender, EventArgs e) {
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;
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) {
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        });
228      }
229    }
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      };
237
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      }
256    }
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
263
264    #region Undo / Redo
265    private void undoButton_Click(object sender, EventArgs e) {
266      Content.Data.Undo();
267    }
268    #endregion
269  }
270}
Note: See TracBrowser for help on using the repository browser.