Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/05/14 17:30:38 (11 years ago)
Author:
mkommend
Message:

#1998: Updated classification model comparision branch with trunk changes.

Location:
branches/ClassificationModelComparison/HeuristicLab.Problems.DataAnalysis.Views/3.4/MenuItems
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/ClassificationModelComparison/HeuristicLab.Problems.DataAnalysis.Views/3.4/MenuItems/CreateEnsembleMenuItem.cs

    r7738 r10553  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2323using System.Collections.Generic;
    2424using System.Linq;
    25 using System.Windows.Forms;
    26 using HeuristicLab.Core;
     25using HeuristicLab.Common;
    2726using HeuristicLab.MainForm;
     27using HeuristicLab.MainForm.WindowsForms;
    2828using HeuristicLab.Optimization;
     29using HeuristicLab.Optimization.Views;
    2930using HeuristicLab.Optimizer;
    3031
    31 namespace HeuristicLab.Problems.DataAnalysis.MenuItems {
     32namespace HeuristicLab.Problems.DataAnalysis.Views {
    3233  internal class CreateEnsembleMenuItem : HeuristicLab.MainForm.WindowsForms.MenuItem, IOptimizerUserInterfaceItemProvider {
    3334    public override string Name {
     
    3536    }
    3637    public override IEnumerable<string> Structure {
    37       get { return new string[] { "&Edit" }; }
     38      get { return new string[] { "&Edit", "&Data Analysis" }; }
    3839    }
    3940    public override int Position {
    40       get { return 2500; }
     41      get { return 5100; }
    4142    }
    4243    public override string ToolTipText {
     
    4950    protected override void OnActiveViewChanged(object sender, EventArgs e) {
    5051      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
    51       if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IOptimizer) && !activeView.Locked) {
    52         var optimizer = activeView.Content as IOptimizer;
    53         ToolStripItem.Enabled = GetDataAnalysisResults(optimizer).Any();
    54       } else {
    55         ToolStripItem.Enabled = false;
    56       }
     52      ToolStripItem.Enabled = GetDataAnalysisResults(activeView).Any();
     53    }
     54    protected override void OnViewChanged(object sender, EventArgs e) {
     55      base.OnViewChanged(sender, e);
     56      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
     57      ToolStripItem.Enabled = GetDataAnalysisResults(activeView).Any();
    5758    }
    5859
    5960    public override void Execute() {
    6061      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
    61       if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IOptimizer) && !activeView.Locked) {
    62         var optimizer = activeView.Content as IOptimizer;
    63         var solutionGroups = from pair in GetDataAnalysisResults(optimizer)
    64                              group pair.Value by pair.Key into g
    65                              select g;
    66         foreach (var group in solutionGroups) {
    67           // check if all solutions in the group are either only regression or only classification solutions
    68           if (group.All(s => s is IRegressionSolution)) {
    69             // show all regression ensembles
    70             // clone problemdata (N.B. this assumes all solutions are based on the same problem data!)
    71             var problemData = (RegressionProblemData)group
    72               .OfType<IRegressionSolution>()
    73               .First()
    74               .ProblemData.Clone();
    75             var ensemble = new RegressionEnsembleSolution(problemData);
    76             ensemble.Name = group.Key + " ensemble";
    77             var nestedSolutions = group.OfType<RegressionEnsembleSolution>().SelectMany(e => e.RegressionSolutions);
    78             var solutions = group.Where(s => !(s is RegressionEnsembleSolution)).OfType<IRegressionSolution>();
    79             ensemble.AddRegressionSolutions(nestedSolutions.Concat(solutions));
    80             MainFormManager.MainForm.ShowContent(ensemble);
    81           } else if (group.All(s => s is IClassificationSolution)) {
    82             // show all classification ensembles
    83             var problemData = (ClassificationProblemData)group
    84               .OfType<IClassificationSolution>()
    85               .First()
    86               .ProblemData.Clone();
    87             var ensemble = new ClassificationEnsembleSolution(Enumerable.Empty<IClassificationModel>(), problemData);
    88             ensemble.Name = group.Key + " ensemble";
    89             var nestedSolutions = group.OfType<ClassificationEnsembleSolution>().SelectMany(e => e.ClassificationSolutions);
    90             var solutions = group.Where(s => !(s is ClassificationEnsembleSolution)).OfType<IClassificationSolution>();
    91             ensemble.AddClassificationSolutions(nestedSolutions.Concat(solutions));
    92             MainFormManager.MainForm.ShowContent(ensemble);
    93           }
     62      var solutionGroups = from pair in GetDataAnalysisResults(activeView)
     63                           group pair.Value by pair.Key into g
     64                           select g;
     65      foreach (var group in solutionGroups) {
     66        // check if all solutions in the group are either only regression or only classification solutions
     67        if (group.All(s => s is IRegressionSolution)) {
     68          // show all regression ensembles
     69          // N.B. this assumes all solutions are based on the same problem data!
     70          // the problem data is not cloned because the individual solutions were already cloned
     71          var problemData = group.OfType<IRegressionSolution>().First().ProblemData;
     72          var ensemble = new RegressionEnsembleSolution(problemData);
     73          ensemble.Name = group.Key + " ensemble";
     74          var nestedSolutions = group.OfType<RegressionEnsembleSolution>().SelectMany(e => e.RegressionSolutions);
     75          var solutions = group.Where(s => !(s is RegressionEnsembleSolution)).OfType<IRegressionSolution>();
     76          ensemble.AddRegressionSolutions(nestedSolutions.Concat(solutions));
     77          MainFormManager.MainForm.ShowContent(ensemble);
     78        } else if (group.All(s => s is IClassificationSolution)) {
     79          // show all classification ensembles
     80          // N.B. this assumes all solutions are based on the same problem data!
     81          // the problem data is not cloned because the individual solutions were already cloned
     82          var problemData = (ClassificationProblemData)group.OfType<IClassificationSolution>().First().ProblemData;
     83          var ensemble = new ClassificationEnsembleSolution(Enumerable.Empty<IClassificationModel>(), problemData);
     84          ensemble.Name = group.Key + " ensemble";
     85          var nestedSolutions = group.OfType<ClassificationEnsembleSolution>().SelectMany(e => e.ClassificationSolutions);
     86          var solutions = group.Where(s => !(s is ClassificationEnsembleSolution)).OfType<IClassificationSolution>();
     87          ensemble.AddClassificationSolutions(nestedSolutions.Concat(solutions));
     88          MainFormManager.MainForm.ShowContent(ensemble);
    9489        }
    9590      }
    9691    }
    9792
    98     private IEnumerable<KeyValuePair<string, IItem>> GetDataAnalysisResults(IOptimizer optimizer) {
    99       var allResults = from r in optimizer.Runs
     93    private IEnumerable<KeyValuePair<string, IDataAnalysisSolution>> GetDataAnalysisResults(IContentView view) {
     94      var empty = Enumerable.Empty<KeyValuePair<string, IDataAnalysisSolution>>();
     95      if (view == null) return empty;
     96      if (view.Content == null) return empty;
     97      if (view.Locked) return empty;
     98
     99      var optimizer = view.Content as IOptimizer;
     100      if (optimizer != null) return GetDataAnalysisResults(optimizer.Runs);
     101
     102      RunCollectionBubbleChartView bubbleChart;
     103      var viewHost = view as ViewHost;
     104      if (viewHost != null)
     105        bubbleChart = viewHost.ActiveView as RunCollectionBubbleChartView;
     106      else bubbleChart = view as RunCollectionBubbleChartView;
     107      if (bubbleChart != null && bubbleChart.SelectedRuns.Any()) return GetDataAnalysisResults(bubbleChart.SelectedRuns);
     108
     109      return empty;
     110    }
     111
     112    private IEnumerable<KeyValuePair<string, IDataAnalysisSolution>> GetDataAnalysisResults(IEnumerable<IRun> runs) {
     113      var cloner = new Cloner();
     114      var allResults = from r in runs
    100115                       select r.Results;
    101116      return from r in allResults
    102117             from result in r
    103              let s = result.Value as IDataAnalysisSolution
     118             let s = cloner.Clone(result.Value) as IDataAnalysisSolution
    104119             where s != null
    105              select result;
     120             select new KeyValuePair<string, IDataAnalysisSolution>(result.Key, s);
    106121    }
    107122  }
Note: See TracChangeset for help on using the changeset viewer.