Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/13/13 19:45:37 (11 years ago)
Author:
mkommend
Message:

#1730: Implemented new features for the excel export:

  • The menu item is enabled if any view containing a symreg solution is displayed.
  • Classifications are not longer supported.
  • Export is performed asynchronously.
  • Inputs are sorted in the same order if a dataset is passed.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/MenuItems/ExportSymbolicSolutionToExcelMenuItem.cs

    r9607 r9626  
    2222using System;
    2323using System.Collections.Generic;
     24using System.ComponentModel;
    2425using System.IO;
    2526using System.Linq;
     
    2728using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
    2829using HeuristicLab.MainForm;
     30using HeuristicLab.MainForm.WindowsForms;
    2931using HeuristicLab.Optimizer;
    3032using OfficeOpenXml;
     
    5254
    5355    protected override void OnToolStripItemSet(EventArgs e) {
     56      base.OnToolStripItemSet(e);
    5457      ToolStripItem.Enabled = false;
    55     }
    56     protected override void OnActiveViewChanged(object sender, EventArgs e) {
     58      var menuItem = ToolStripItem.OwnerItem as ToolStripMenuItem;
     59      if (menuItem != null)
     60        menuItem.DropDownOpening += menuItem_DropDownOpening;
     61    }
     62
     63    private void menuItem_DropDownOpening(object sender, EventArgs e) {
    5764      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
    58       ToolStripItem.Enabled = activeView != null && activeView.Content is ISymbolicDataAnalysisSolution;
     65      Control control = activeView as Control;
     66      activeView = control.GetNestedControls((c) => c.Visible)
     67        .OfType<IContentView>().FirstOrDefault(v => v.Content is ISymbolicDataAnalysisSolution && v.Content is IRegressionSolution);
     68      ToolStripItem.Enabled = activeView != null;
    5969    }
    6070
    6171    public override void Execute() {
    62       var activeView = (IContentView)MainFormManager.MainForm.ActiveView;
     72      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
     73      Control control = activeView as Control;
     74      activeView = control.GetNestedControls((c) => c.Visible)
     75        .OfType<IContentView>().First(v => v.Content is ISymbolicDataAnalysisSolution && v.Content is IRegressionSolution);
    6376      var solution = (ISymbolicDataAnalysisSolution)activeView.Content;
    6477      var formatter = new SymbolicDataAnalysisExpressionExcelFormatter();
    65       var formula = formatter.Format(solution.Model.SymbolicExpressionTree);
    66       var formulaParts = formula.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
     78      var formula = formatter.Format(solution.Model.SymbolicExpressionTree, solution.ProblemData.Dataset);
     79
    6780
    6881      SaveFileDialog saveFileDialog = new SaveFileDialog();
     
    7184      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
    7285        string fileName = saveFileDialog.FileName;
    73         FileInfo newFile = new FileInfo(fileName);
    74         if (newFile.Exists) {
    75           newFile.Delete();
    76           newFile = new FileInfo(fileName);
     86        using (BackgroundWorker bg = new BackgroundWorker()) {
     87          bg.DoWork += (b, e) => ExportChart(fileName, solution, formula);
     88          bg.RunWorkerAsync();
    7789        }
    78         using (ExcelPackage package = new ExcelPackage(newFile)) {
    79           ExcelWorksheet modelWorksheet = package.Workbook.Worksheets.Add("Model");
    80           FormatModelSheet(modelWorksheet, solution, formulaParts);
    81 
    82           ExcelWorksheet datasetWorksheet = package.Workbook.Worksheets.Add("Dataset");
    83           WriteDatasetToExcel(datasetWorksheet, solution.ProblemData);
    84 
    85           ExcelWorksheet inputsWorksheet = package.Workbook.Worksheets.Add("Inputs");
    86           WriteInputSheet(inputsWorksheet, datasetWorksheet, formulaParts.Skip(2), solution.ProblemData.Dataset);
    87 
    88           if (solution is IRegressionSolution) {
    89             ExcelWorksheet estimatedWorksheet = package.Workbook.Worksheets.Add("Estimated Values");
    90             WriteEstimatedWorksheet(estimatedWorksheet, datasetWorksheet, formulaParts, solution as IRegressionSolution);
    91 
    92             ExcelWorksheet chartsWorksheet = package.Workbook.Worksheets.Add("Charts");
    93             AddCharts(chartsWorksheet);
    94           }
    95           package.Workbook.Properties.Title = "Excel Export";
    96           package.Workbook.Properties.Author = "HEAL";
    97           package.Workbook.Properties.Comments = "Excel export of a symbolic data analysis solution from HeuristicLab";
    98 
    99           package.Save();
     90      }
     91    }
     92
     93    private void ExportChart(string fileName, ISymbolicDataAnalysisSolution solution, string formula) {
     94      FileInfo newFile = new FileInfo(fileName);
     95      if (newFile.Exists) {
     96        newFile.Delete();
     97        newFile = new FileInfo(fileName);
     98      }
     99      var formulaParts = formula.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
     100
     101      using (ExcelPackage package = new ExcelPackage(newFile)) {
     102        ExcelWorksheet modelWorksheet = package.Workbook.Worksheets.Add("Model");
     103        FormatModelSheet(modelWorksheet, solution, formulaParts);
     104
     105        ExcelWorksheet datasetWorksheet = package.Workbook.Worksheets.Add("Dataset");
     106        WriteDatasetToExcel(datasetWorksheet, solution.ProblemData);
     107
     108        ExcelWorksheet inputsWorksheet = package.Workbook.Worksheets.Add("Inputs");
     109        WriteInputSheet(inputsWorksheet, datasetWorksheet, formulaParts.Skip(2), solution.ProblemData.Dataset);
     110
     111        if (solution is IRegressionSolution) {
     112          ExcelWorksheet estimatedWorksheet = package.Workbook.Worksheets.Add("Estimated Values");
     113          WriteEstimatedWorksheet(estimatedWorksheet, datasetWorksheet, formulaParts, solution as IRegressionSolution);
     114
     115          ExcelWorksheet chartsWorksheet = package.Workbook.Worksheets.Add("Charts");
     116          AddCharts(chartsWorksheet);
    100117        }
     118        package.Workbook.Properties.Title = "Excel Export";
     119        package.Workbook.Properties.Author = "HEAL";
     120        package.Workbook.Properties.Comments = "Excel export of a symbolic data analysis solution from HeuristicLab";
     121
     122        package.Save();
    101123      }
    102124    }
Note: See TracChangeset for help on using the changeset viewer.