Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15016 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 11.6 KB
RevLine 
[10539]1#region License Information
2/* HeuristicLab
[14186]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;
[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 {
[10998]38
[10239]39    public DataPreprocessingView() {
40      InitializeComponent();
[10258]41    }
[10239]42
[14075]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;
[12718]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),
[10934]64          new CorrelationMatrixContent(Content),
[11002]65          new DataCompletenessChartContent(searchLogic),
[14075]66
[10921]67          new FilterContent(filterLogic),
[11002]68          new ManipulationContent(manipulationLogic, searchLogic, filterLogic),
[10977]69          new TransformationContent(data, filterLogic)
[10617]70        };
[10625]71
[10904]72        viewShortcutListView.Content = viewShortcuts.AsReadOnly();
73        viewShortcutListView.ItemsListView.Items[0].Selected = true;
74        viewShortcutListView.Select();
[10735]75
[14075]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);
[10614]90      } else {
[10904]91        viewShortcutListView.Content = null;
[10254]92      }
[10239]93    }
[10969]94    protected override void RegisterContentEvents() {
95      base.RegisterContentEvents();
[14075]96      Content.Reset += Content_Reset;
[10969]97      Content.Data.FilterChanged += Data_FilterChanged;
98    }
99
100    protected override void DeregisterContentEvents() {
101      base.DeregisterContentEvents();
[14075]102      Content.Reset -= Content_Reset;
[10969]103      Content.Data.FilterChanged -= Data_FilterChanged;
104    }
105
[14075]106    void Content_Reset(object sender, EventArgs e) {
107      OnContentChanged(); // Reset by setting new content
108    }
109
[10969]110    void Data_FilterChanged(object sender, EventArgs e) {
111      lblFilterActive.Visible = Content.Data.IsFiltered;
112    }
113
[10614]114    protected override void SetEnabledStateOfControls() {
115      base.SetEnabledStateOfControls();
[10904]116      viewShortcutListView.Enabled = Content != null;
[10614]117      applyInNewTabButton.Enabled = Content != null;
[14075]118      exportProblemButton.Enabled = Content != null && Content.CanExport;
[10614]119      undoButton.Enabled = Content != null;
[10239]120    }
121
[14075]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
[10304]199    private void exportProblemButton_Click(object sender, EventArgs e) {
[14075]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;
[10307]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) {
[14075]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        });
[10307]227      }
[10304]228    }
[14075]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      };
[10304]236
[14075]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      }
[10254]255    }
[14075]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
[10550]262
[14075]263    #region Undo / Redo
[10550]264    private void undoButton_Click(object sender, EventArgs e) {
265      Content.Data.Undo();
266    }
[14075]267    #endregion
[10239]268  }
[10310]269}
Note: See TracBrowser for help on using the repository browser.