Changeset 10720 for branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4
- Timestamp:
- 04/03/14 11:06:14 (10 years ago)
- Location:
- branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SlidingWindowBestSolutionsCollectionHeatMapView.cs
r10686 r10720 21 21 22 22 using System; 23 using System.Co llections.Generic;23 using System.ComponentModel; 24 24 using System.Linq; 25 25 using System.Windows.Forms; 26 26 using HeuristicLab.Analysis; 27 27 using HeuristicLab.Core.Views; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;29 28 using HeuristicLab.MainForm; 30 29 using HeuristicLab.Optimization; … … 39 38 set { base.Content = value; } 40 39 } 40 41 protected override void OnContentChanged() { 42 base.OnContentChanged(); 43 if (Content != null) { 44 if (Content.SlidingWindowQualities != null) { 45 SetHeatmapContent(Content.SlidingWindowQualities); 46 } else if (!Content.QualitiesCalculationInProgress) { 47 progress.Start("Calculating best solution qualities..."); 48 Content.CalculateQualities(); 49 } 50 } 51 } 52 53 private readonly IProgress progress; // use this to show some progress when the sliding window qualities are calculated for each model 54 41 55 public SlidingWindowBestSolutionsCollectionHeatMapView() { 42 56 InitializeComponent(); 43 44 qualityMeasure = (int)QualityMeasures.PEARSON;57 progress = new Progress(); 58 MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, progress); 45 59 } 46 47 enum QualityMeasures { PEARSON, MSE };48 49 private int qualityMeasure;50 60 51 61 protected override void DeregisterContentEvents() { 52 62 // TODO: Deregister your event handlers here 63 Content.QualitiesCalculationCompleted -= bestSolutionsCollectionQualitiesCalculated; 64 Content.QualitiesCalculationProgress -= bestSolutionCollectionQualitiesCalculationProgress; 65 Content.QualitiesUpdated -= bestSolutionCollectionQualitiesUpdated; 53 66 base.DeregisterContentEvents(); 54 67 } … … 57 70 base.RegisterContentEvents(); 58 71 // TODO: Register your event handlers here 72 Content.QualitiesCalculationCompleted += bestSolutionsCollectionQualitiesCalculated; 73 Content.QualitiesCalculationProgress += bestSolutionCollectionQualitiesCalculationProgress; 74 Content.QualitiesUpdated += bestSolutionCollectionQualitiesUpdated; 59 75 } 60 76 77 private void bestSolutionsCollectionQualitiesCalculated(object sender, RunWorkerCompletedEventArgs e) { 78 if (e.Cancelled || e.Error != null) { 79 progress.Cancel(); 80 return; 81 } 82 SetHeatmapContent(Content.SlidingWindowQualities); 83 progress.Finish(); 84 } 85 86 private void bestSolutionCollectionQualitiesUpdated(object sender, EventArgs e) { 87 SetHeatmapContent(Content.SlidingWindowQualities); 88 } 89 90 private void SetHeatmapContent(double[,] values) { 91 double min = 0, max = 0; 92 foreach (var q in values) { 93 if (min > q) min = q; 94 if (max < q) max = q; 95 } 96 int rows = values.GetLength(0); 97 int cols = values.GetLength(1); 98 var flipped = new double[rows, cols]; 99 for (int i = 0; i < rows; ++i) { 100 for (int j = 0; j < cols; ++j) { 101 flipped[i, j] = values[rows - i - 1, j]; 102 } 103 } 104 heatMapView.Content = new HeatMap(flipped, "Best Sliding Window Solutions", min, max); 105 } 106 107 private void bestSolutionCollectionQualitiesCalculationProgress(object sender, ProgressChangedEventArgs e) { 108 progress.ProgressValue = e.ProgressPercentage / 100.0; 109 } 110 111 protected override void SetEnabledStateOfControls() { 112 base.SetEnabledStateOfControls(); 113 // TODO: Enable or disable controls based on whether the content is null or the view is set readonly 114 } 115 116 #region Event Handlers (child controls) 61 117 public void heatMapView_cellDoubleClick(object sender, DataGridViewCellEventArgs e) { 62 118 var dataGridView = (DataGridView)sender; 63 119 var cell = dataGridView.SelectedCells[0]; 64 var bestSolutions = Content. BestSolutions.Values.ToList();120 var bestSolutions = Content.SlidingWindowBestSolutions.Values.ToList(); 65 121 var tree = bestSolutions[cell.RowIndex]; 66 122 var model = Content.CreateModel(tree, Content.Interpreter); … … 74 130 } 75 131 76 #region Event Handlers (Content)77 // TODO: Put event handlers of the content here78 #endregion79 80 protected override void OnContentChanged() {81 base.OnContentChanged();82 if (Content == null) {83 // TODO: Add code when content has been changed and is null84 } else {85 // TODO: Add code when content has been changed and is not null86 UpdateQualitiesMap();87 }88 }89 90 protected override void SetEnabledStateOfControls() {91 base.SetEnabledStateOfControls();92 // TODO: Enable or disable controls based on whether the content is null or the view is set readonly93 }94 95 #region Event Handlers (child controls)96 // TODO: Put event handlers of child controls here.97 #endregion98 99 132 private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) { 100 133 var combobox = (ComboBox)sender; 101 134 switch ((string)combobox.SelectedItem) { 102 135 case "Pearson R2": { 103 qualityMeasure = (int)QualityMeasures.PEARSON;136 Content.QualityMeasure = SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON; 104 137 break; 105 138 } 106 139 case "Mean Squared Error": { 107 qualityMeasure = (int)QualityMeasures.MSE;140 Content.QualityMeasure = SlidingWindowBestSolutionsCollection.QualityMeasures.MSE; 108 141 break; 109 142 } 110 143 } 111 UpdateQualitiesMap();112 144 } 113 114 private void UpdateQualitiesMap() { 115 var bestSolutions = Content.BestSolutions.Values.ToList(); 116 var swPositions = Content.BestSolutions.Keys.ToList(); 117 118 var nRows = bestSolutions.Count; 119 var nCols = swPositions.Count + 1; 120 var qualitiesMap = new double[nRows, nCols]; 121 double min = 0, max = 0; 122 123 for (int i = 0; i < nCols - 1; ++i) { 124 var pos = swPositions[i]; 125 var rows = Enumerable.Range(pos.Item1, pos.Item2 - pos.Item1).ToList(); 126 127 for (int j = nRows - 1; j >= 0; --j) { 128 var tree = bestSolutions[j]; 129 var q = CalculateQuality(tree, rows); 130 if (min > q) min = q; 131 if (max < q) max = q; 132 qualitiesMap[j, i] = q; 133 } 134 } 135 // deal separately with the last column which represents the whole training partition interval 136 for (int j = nRows - 1; j >= 0; --j) { 137 var tree = bestSolutions[j]; 138 var q = CalculateQuality(tree, Content.ProblemData.TrainingIndices); 139 if (min > q) min = q; 140 if (max < q) max = q; 141 qualitiesMap[j, nCols - 1] = q; 142 } 143 144 heatMapView.Content = new HeatMap(qualitiesMap); 145 heatMapView.Content.Minimum = min; 146 heatMapView.Content.Maximum = max; 147 } 148 149 private string GetTargetVariable(IDataAnalysisProblemData problemData) { 150 var regressionProblemData = problemData as IRegressionProblemData; 151 var classificationProblemData = problemData as IClassificationProblemData; 152 if (regressionProblemData != null) return regressionProblemData.TargetVariable; 153 if (classificationProblemData != null) return classificationProblemData.TargetVariable; 154 throw new NotSupportedException(); 155 } 156 157 private double CalculateQuality(ISymbolicExpressionTree tree, IEnumerable<int> rows) { 158 var estimatedValues = Content.Interpreter.GetSymbolicExpressionTreeValues(tree, Content.ProblemData.Dataset, rows); 159 var originalValues = Content.ProblemData.Dataset.GetDoubleValues(GetTargetVariable(Content.ProblemData), rows); 160 double quality = 0; 161 var errorState = new OnlineCalculatorError(); 162 switch ((QualityMeasures)qualityMeasure) { 163 case QualityMeasures.PEARSON: 164 quality = OnlinePearsonsRSquaredCalculator.Calculate(estimatedValues, originalValues, out errorState); 165 break; 166 case QualityMeasures.MSE: 167 quality = OnlineMeanSquaredErrorCalculator.Calculate(estimatedValues, originalValues, out errorState); 168 break; 169 } 170 return errorState == OnlineCalculatorError.None ? quality : double.NaN; 171 } 145 #endregion 172 146 } 173 147 } -
branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SlidingWindowBestSolutionsCollectionView.cs
r10413 r10720 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.ComponentModel; 24 25 using System.Linq; 25 26 using System.Windows.Forms; 26 27 using HeuristicLab.Core.Views; 27 28 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;29 29 using HeuristicLab.MainForm; 30 30 using HeuristicLab.Optimization; … … 39 39 set { base.Content = value; } 40 40 } 41 public SlidingWindowBestSolutionsCollectionView() {42 InitializeComponent();43 41 44 qualityMeasure = (int)QualityMeasures.PEARSON; 42 protected override void OnContentChanged() { 43 base.OnContentChanged(); 44 if (Content != null) { 45 if (Content.SlidingWindowQualities != null) { 46 SetMatrixContent(Content.SlidingWindowQualities); 47 } else if (!Content.QualitiesCalculationInProgress) { 48 progress.Start("Calculating best solution qualities..."); 49 Content.CalculateQualities(); 50 } 51 } 45 52 } 46 53 47 enum QualityMeasures { PEARSON, MSE }; 48 49 private int qualityMeasure; 54 private readonly Progress progress; 50 55 51 56 protected override void DeregisterContentEvents() { 52 57 // TODO: Deregister your event handlers here 53 58 enhancedStringConvertibleMatrixView.DataGridView.CellDoubleClick -= enhancedStringConvertibleMatrixView_cellDoubleClick; 59 Content.QualitiesCalculationCompleted -= bestSolutionsCollectionQualitiesCalculated; 60 Content.QualitiesCalculationProgress -= bestSolutionCollectionQualitiesCalculationProgress; 61 Content.QualitiesUpdated -= bestSolutionCollectionQualitiesUpdated; 54 62 base.DeregisterContentEvents(); 55 63 } … … 57 65 protected override void RegisterContentEvents() { 58 66 base.RegisterContentEvents(); 67 // TODO: Register your event handlers here 59 68 enhancedStringConvertibleMatrixView.DataGridView.CellDoubleClick += enhancedStringConvertibleMatrixView_cellDoubleClick; 60 // TODO: Register your event handlers here 69 Content.QualitiesCalculationCompleted += bestSolutionsCollectionQualitiesCalculated; 70 Content.QualitiesCalculationProgress += bestSolutionCollectionQualitiesCalculationProgress; 71 Content.QualitiesUpdated += bestSolutionCollectionQualitiesUpdated; 72 } 73 74 public SlidingWindowBestSolutionsCollectionView() { 75 InitializeComponent(); 76 progress = new Progress(); 77 MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, progress); 78 } 79 80 #region Event Handlers (Content) 81 private void bestSolutionsCollectionQualitiesCalculated(object sender, RunWorkerCompletedEventArgs e) { 82 if (e.Cancelled || e.Error != null) { 83 progress.Cancel(); 84 return; 85 } 86 87 SetMatrixContent(Content.SlidingWindowQualities); 88 progress.Finish(); 89 } 90 91 private void SetMatrixContent(double[,] values) { 92 var qualitiesMap = new DoubleMatrix(values); // add an extra columns for the training data 93 qualitiesMap.ColumnNames = Enumerable.Range(0, qualitiesMap.Columns - 1).Select(x => Content.SlidingWindowRanges[x].Start + " - " + Content.SlidingWindowRanges[x].End).Concat(new List<string> { "Training" }); 94 qualitiesMap.RowNames = Enumerable.Range(1, qualitiesMap.Rows).Select(x => "M" + x); 95 96 double min = 0, max = 0; 97 foreach (var q in qualitiesMap) { 98 if (min > q) min = q; 99 if (max < q) max = q; 100 } 101 102 enhancedStringConvertibleMatrixView.Minimum = min; 103 enhancedStringConvertibleMatrixView.Maximum = max; 104 enhancedStringConvertibleMatrixView.Content = qualitiesMap; 105 } 106 107 private void bestSolutionCollectionQualitiesCalculationProgress(object sender, ProgressChangedEventArgs e) { 108 progress.ProgressValue = e.ProgressPercentage / 100.0; 109 } 110 111 private void bestSolutionCollectionQualitiesUpdated(object sender, EventArgs e) { 112 SetMatrixContent(Content.SlidingWindowQualities); 113 } 114 #endregion 115 116 protected override void SetEnabledStateOfControls() { 117 base.SetEnabledStateOfControls(); 118 // TODO: Enable or disable controls based on whether the content is null or the view is set readonly 119 } 120 121 #region Event Handlers (child controls) 122 private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) { 123 var combobox = (ComboBox)sender; 124 switch ((string)combobox.SelectedItem) { 125 case "Pearson R2": { 126 Content.QualityMeasure = SlidingWindowBestSolutionsCollection.QualityMeasures.PEARSON; 127 break; 128 } 129 case "Mean Squared Error": { 130 Content.QualityMeasure = SlidingWindowBestSolutionsCollection.QualityMeasures.MSE; 131 break; 132 } 133 } 61 134 } 62 135 … … 64 137 var dataGridView = (DataGridView)sender; 65 138 var cell = dataGridView.SelectedCells[0]; 66 var bestSolutions = Content. BestSolutions.Values.ToList();139 var bestSolutions = Content.SlidingWindowBestSolutions.Values.ToList(); 67 140 var tree = bestSolutions[cell.RowIndex]; 68 141 var model = Content.CreateModel(tree, Content.Interpreter); … … 75 148 } 76 149 } 77 78 #region Event Handlers (Content)79 // TODO: Put event handlers of the content here80 150 #endregion 81 82 protected override void OnContentChanged() {83 base.OnContentChanged();84 if (Content == null) {85 // TODO: Add code when content has been changed and is null86 } else {87 // TODO: Add code when content has been changed and is not null88 UpdateQualitiesMap();89 }90 }91 92 protected override void SetEnabledStateOfControls() {93 base.SetEnabledStateOfControls();94 // TODO: Enable or disable controls based on whether the content is null or the view is set readonly95 }96 97 #region Event Handlers (child controls)98 // TODO: Put event handlers of child controls here.99 #endregion100 101 private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) {102 var combobox = (ComboBox)sender;103 switch ((string)combobox.SelectedItem) {104 case "Pearson R2": {105 qualityMeasure = (int)QualityMeasures.PEARSON;106 break;107 }108 case "Mean Squared Error": {109 qualityMeasure = (int)QualityMeasures.MSE;110 break;111 }112 }113 UpdateQualitiesMap();114 }115 116 private void UpdateQualitiesMap() {117 var bestSolutions = Content.BestSolutions.Values.ToList();118 var swPositions = Content.BestSolutions.Keys.ToList();119 var qualitiesMap = new DoubleMatrix(bestSolutions.Count, swPositions.Count + 1);120 qualitiesMap.ColumnNames = swPositions.Select(x => x.Item1 + "-" + x.Item2).Concat(new List<string> { "Training" });121 qualitiesMap.RowNames = Enumerable.Range(1, bestSolutions.Count).Select(x => "M" + x);122 double min = 0, max = 0;123 124 for (int i = 0; i < qualitiesMap.Columns - 1; ++i) {125 var pos = swPositions[i];126 var rows = Enumerable.Range(pos.Item1, pos.Item2 - pos.Item1).ToList();127 128 for (int j = 0; j < qualitiesMap.Rows; ++j) {129 var tree = bestSolutions[j];130 var q = CalculateQuality(tree, rows);131 if (min > q) min = q;132 if (max < q) max = q;133 qualitiesMap[j, i] = q;134 }135 }136 // deal separately with the last column which represents the whole training partition interval137 for (int j = 0; j < qualitiesMap.Rows; ++j) {138 var tree = bestSolutions[j];139 var q = CalculateQuality(tree, Content.ProblemData.TrainingIndices);140 if (min > q) min = q;141 if (max < q) max = q;142 qualitiesMap[j, qualitiesMap.Columns - 1] = q;143 }144 145 enhancedStringConvertibleMatrixView.Minimum = min;146 enhancedStringConvertibleMatrixView.Maximum = max;147 enhancedStringConvertibleMatrixView.Content = qualitiesMap;148 }149 150 private string GetTargetVariable(IDataAnalysisProblemData problemData) {151 var regressionProblemData = problemData as IRegressionProblemData;152 var classificationProblemData = problemData as IClassificationProblemData;153 if (regressionProblemData != null) return regressionProblemData.TargetVariable;154 if (classificationProblemData != null) return classificationProblemData.TargetVariable;155 throw new NotSupportedException();156 }157 158 private double CalculateQuality(ISymbolicExpressionTree tree, IEnumerable<int> rows) {159 var estimatedValues = Content.Interpreter.GetSymbolicExpressionTreeValues(tree, Content.ProblemData.Dataset, rows);160 var originalValues = Content.ProblemData.Dataset.GetDoubleValues(GetTargetVariable(Content.ProblemData), rows);161 double quality = 0;162 var errorState = new OnlineCalculatorError();163 switch ((QualityMeasures)qualityMeasure) {164 case QualityMeasures.PEARSON:165 quality = OnlinePearsonsRSquaredCalculator.Calculate(estimatedValues, originalValues, out errorState);166 break;167 case QualityMeasures.MSE:168 quality = OnlineMeanSquaredErrorCalculator.Calculate(estimatedValues, originalValues, out errorState);169 break;170 }171 return errorState == OnlineCalculatorError.None ? quality : double.NaN;172 }173 151 } 174 152 }
Note: See TracChangeset
for help on using the changeset viewer.