Changeset 10553 for branches/ClassificationModelComparison/HeuristicLab.Problems.DataAnalysis.Views/3.4/MenuItems
- Timestamp:
- 03/05/14 17:30:38 (11 years ago)
- 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 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 2Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Windows.Forms; 26 using HeuristicLab.Core; 25 using HeuristicLab.Common; 27 26 using HeuristicLab.MainForm; 27 using HeuristicLab.MainForm.WindowsForms; 28 28 using HeuristicLab.Optimization; 29 using HeuristicLab.Optimization.Views; 29 30 using HeuristicLab.Optimizer; 30 31 31 namespace HeuristicLab.Problems.DataAnalysis. MenuItems {32 namespace HeuristicLab.Problems.DataAnalysis.Views { 32 33 internal class CreateEnsembleMenuItem : HeuristicLab.MainForm.WindowsForms.MenuItem, IOptimizerUserInterfaceItemProvider { 33 34 public override string Name { … … 35 36 } 36 37 public override IEnumerable<string> Structure { 37 get { return new string[] { "&Edit" }; }38 get { return new string[] { "&Edit", "&Data Analysis" }; } 38 39 } 39 40 public override int Position { 40 get { return 2500; }41 get { return 5100; } 41 42 } 42 43 public override string ToolTipText { … … 49 50 protected override void OnActiveViewChanged(object sender, EventArgs e) { 50 51 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(); 57 58 } 58 59 59 60 public override void Execute() { 60 61 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); 94 89 } 95 90 } 96 91 } 97 92 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 100 115 select r.Results; 101 116 return from r in allResults 102 117 from result in r 103 let s = result.Valueas IDataAnalysisSolution118 let s = cloner.Clone(result.Value) as IDataAnalysisSolution 104 119 where s != null 105 select result;120 select new KeyValuePair<string, IDataAnalysisSolution>(result.Key, s); 106 121 } 107 122 }
Note: See TracChangeset
for help on using the changeset viewer.