Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.DataPreprocessing.Views/3.4/DataPreprocessingView.cs @ 14871

Last change on this file since 14871 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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 ScatterPlotContent(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      Content.Import(new RegressionProblemData());
128    }
129    private void newClassificationToolStripMenuItem_Click(object sender, EventArgs e) {
130      Content.Import(new ClassificationProblemData());
131    }
132    private void newTimeSeriesToolStripMenuItem_Click(object sender, EventArgs e) {
133      Content.Import(new TimeSeriesPrognosisProblemData());
134    }
135    #endregion
136
137    #region Import
138    private void importButton_Click(object sender, EventArgs e) {
139      importProblemDataTypeContextMenuStrip.Show(Cursor.Position);
140    }
141    private void importRegressionToolStripMenuItem_Click(object sender, EventArgs e) {
142      Import(new RegressionCSVInstanceProvider(), new RegressionImportTypeDialog(),
143        (dialog => ((RegressionImportTypeDialog)dialog).ImportType));
144    }
145    private void importClassificationToolStripMenuItem_Click(object sender, EventArgs e) {
146      Import(new ClassificationCSVInstanceProvider(), new ClassificationImportTypeDialog(),
147        (dialog => ((ClassificationImportTypeDialog)dialog).ImportType));
148    }
149    private void importTimeSeriesToolStripMenuItem_Click(object sender, EventArgs e) {
150      Import(new TimeSeriesPrognosisCSVInstanceProvider(), new TimeSeriesPrognosisImportTypeDialog(),
151        (dialog => ((TimeSeriesPrognosisImportTypeDialog)dialog).ImportType));
152    }
153    private void Import<TProblemData, TImportType>(DataAnalysisInstanceProvider<TProblemData, TImportType> instanceProvider, DataAnalysisImportTypeDialog importTypeDialog,
154      Func<DataAnalysisImportTypeDialog, TImportType> getImportType)
155      where TProblemData : class, IDataAnalysisProblemData
156      where TImportType : DataAnalysisImportType {
157      if (importTypeDialog.ShowDialog() == DialogResult.OK) {
158        Task.Run(() => {
159          TProblemData instance;
160          var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
161          // lock active view and show progress bar
162          var activeView = (IContentView)MainFormManager.MainForm.ActiveView;
163
164          try {
165            var progress = mainForm.AddOperationProgressToContent(activeView.Content, "Loading problem instance.");
166
167            instanceProvider.ProgressChanged += (o, args) => { progress.ProgressValue = args.ProgressPercentage / 100.0; };
168
169            instance = instanceProvider.ImportData(importTypeDialog.Path, getImportType(importTypeDialog), importTypeDialog.CSVFormat);
170          } catch (IOException ex) {
171            MessageBox.Show(string.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
172            mainForm.RemoveOperationProgressFromContent(activeView.Content);
173            return;
174          }
175          try {
176            Content.Import(instance);
177          } catch (IOException ex) {
178            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");
179          } finally {
180            mainForm.RemoveOperationProgressFromContent(activeView.Content);
181          }
182        });
183      }
184    }
185    #endregion
186
187    #region Apply
188    private void applyInNewTabButton_Click(object sender, EventArgs e) {
189      applyTypeContextMenuStrip.Show(Cursor.Position);
190    }
191    private void applyToolStripMenuItem_Click(object sender, EventArgs e) {
192      var menuItem = (ToolStripMenuItem)sender;
193      var itemCreator = (Func<IItem>)menuItem.Tag;
194      MainFormManager.MainForm.ShowContent(itemCreator());
195    }
196    #endregion
197
198    #region Export
199    private void exportProblemButton_Click(object sender, EventArgs e) {
200      exportTypeContextMenuStrip.Show(Cursor.Position);
201    }
202    private void exportToolStripMenuItem_Click(object sender, EventArgs e) {
203      var menuItem = (ToolStripMenuItem)sender;
204      var itemCreator = (Func<IItem>)menuItem.Tag;
205      var saveFileDialog = new SaveFileDialog {
206        Title = "Save Item",
207        DefaultExt = "hl",
208        Filter = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*",
209        FilterIndex = 2
210      };
211
212      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
213        Task.Run(() => {
214          bool compressed = saveFileDialog.FilterIndex != 1;
215          var storable = itemCreator() as IStorableContent;
216          if (storable != null) {
217            var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
218            var activeView = (IContentView)MainFormManager.MainForm.ActiveView;
219            try {
220              mainForm.AddOperationProgressToContent(activeView.Content, "Exporting data.");
221              ContentManager.Save(storable, saveFileDialog.FileName, compressed);
222            } finally {
223              mainForm.RemoveOperationProgressFromContent(activeView.Content);
224            }
225          }
226        });
227      }
228    }
229    private void exportCsvMenuItem_Click(object sender, EventArgs e) {
230      var saveFileDialog = new SaveFileDialog {
231        Title = "Save Data",
232        DefaultExt = "csv",
233        Filter = "CSV files|*.csv|All files|*.*",
234        FilterIndex = 1
235      };
236
237      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
238        Task.Run(() => {
239          var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
240          var activeView = (IContentView)MainFormManager.MainForm.ActiveView;
241          try {
242            var problemData = Content.CreateNewProblemData();
243            mainForm.AddOperationProgressToContent(activeView.Content, "Exporting data.");
244            if (problemData is TimeSeriesPrognosisProblemData)
245              Export(new TimeSeriesPrognosisCSVInstanceProvider(), problemData, saveFileDialog.FileName);
246            else if (problemData is RegressionProblemData)
247              Export(new RegressionCSVInstanceProvider(), problemData, saveFileDialog.FileName);
248            else if (problemData is ClassificationProblemData)
249              Export(new ClassificationCSVInstanceProvider(), problemData, saveFileDialog.FileName);
250          } finally {
251            mainForm.RemoveOperationProgressFromContent(activeView.Content);
252          }
253        });
254      }
255    }
256    private void Export<TProblemData, TImportType>(DataAnalysisInstanceProvider<TProblemData, TImportType> instanceProvider,
257      IDataAnalysisProblemData problemData, string path)
258      where TProblemData : class, IDataAnalysisProblemData where TImportType : DataAnalysisImportType {
259      instanceProvider.ExportData((TProblemData)problemData, path);
260    }
261    #endregion
262
263    #region Undo / Redo
264    private void undoButton_Click(object sender, EventArgs e) {
265      Content.Data.Undo();
266    }
267    #endregion
268  }
269}
Note: See TracBrowser for help on using the repository browser.