[8483] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.ComponentModel;
|
---|
| 25 | using System.Drawing;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using System.Text;
|
---|
| 28 | using System.Windows.Forms;
|
---|
| 29 | using HeuristicLab.Common;
|
---|
| 30 | using HeuristicLab.Core.Views;
|
---|
| 31 | using HeuristicLab.Data.Views;
|
---|
| 32 | using HeuristicLab.MainForm;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
| 35 | [View("Feature Correlation View")]
|
---|
| 36 | [Content(typeof(FeatureCorrelation), true)]
|
---|
| 37 | public partial class FeatureCorrelationView : ItemView {
|
---|
| 38 |
|
---|
| 39 | private int[] virtualRowIndices;
|
---|
| 40 | private List<KeyValuePair<int, SortOrder>> sortedColumnIndices;
|
---|
| 41 | private StringConvertibleMatrixView.RowComparer rowComparer;
|
---|
| 42 |
|
---|
| 43 | public new FeatureCorrelation Content {
|
---|
| 44 | get { return (FeatureCorrelation)base.Content; }
|
---|
| 45 | set { base.Content = value; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public FeatureCorrelationView() {
|
---|
| 49 | InitializeComponent();
|
---|
| 50 | sortedColumnIndices = new List<KeyValuePair<int, SortOrder>>();
|
---|
| 51 | rowComparer = new StringConvertibleMatrixView.RowComparer();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | protected override void RegisterContentEvents() {
|
---|
| 55 | base.RegisterContentEvents();
|
---|
| 56 | Content.ProgressCalculation += new DataAnalysis.FeatureCorrelation.ProgressCalculationHandler(Content_ProgressCalculation);
|
---|
[8492] | 57 | Content.CorrelationCalculationFinished += new System.EventHandler(Content_CorrelationCalculationFinished);
|
---|
[8483] | 58 | }
|
---|
| 59 |
|
---|
| 60 | protected override void DeregisterContentEvents() {
|
---|
[8492] | 61 | Content.CorrelationCalculationFinished -= new System.EventHandler(Content_CorrelationCalculationFinished);
|
---|
[8483] | 62 | Content.ProgressCalculation -= new DataAnalysis.FeatureCorrelation.ProgressCalculationHandler(Content_ProgressCalculation);
|
---|
| 63 | base.DeregisterContentEvents();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | protected void Content_ProgressCalculation(object sender, ProgressChangedEventArgs e) {
|
---|
[8529] | 67 | if (!CalculatingPanel.Visible && e.ProgressPercentage != HeatMapProgressBar.Maximum) {
|
---|
| 68 | CalculatingPanel.Show();
|
---|
[8483] | 69 | } else if (e.ProgressPercentage == HeatMapProgressBar.Maximum) {
|
---|
[8529] | 70 | CalculatingPanel.Hide();
|
---|
[8483] | 71 | }
|
---|
| 72 | HeatMapProgressBar.Value = e.ProgressPercentage;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | protected override void OnContentChanged() {
|
---|
| 76 | base.OnContentChanged();
|
---|
| 77 | if (Content != null) {
|
---|
| 78 | CorrelationCalcComboBox.DataSource = Content.CorrelationCalculators;
|
---|
| 79 | PartitionComboBox.DataSource = Content.Partitions;
|
---|
[8492] | 80 | CalculateCorrelation();
|
---|
[8483] | 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | protected void CorrelationMeasureComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
|
---|
[8492] | 85 | CalculateCorrelation();
|
---|
[8483] | 86 | }
|
---|
| 87 | protected void PartitionComboBox_SelectedIndexChanged(object sender, System.EventArgs e) {
|
---|
[8492] | 88 | CalculateCorrelation();
|
---|
[8483] | 89 | }
|
---|
| 90 |
|
---|
[8492] | 91 | protected virtual void CalculateCorrelation() {
|
---|
[8483] | 92 | string calc = (string)CorrelationCalcComboBox.SelectedItem;
|
---|
| 93 | string partition = (string)PartitionComboBox.SelectedItem;
|
---|
| 94 | if (calc != null && partition != null) {
|
---|
[8529] | 95 | DataGridView.Columns.Clear();
|
---|
[8483] | 96 | DataGridView.Enabled = false;
|
---|
| 97 | Content.Recalculate(calc, partition);
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | protected void UpdateDataGrid() {
|
---|
| 102 | virtualRowIndices = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
| 103 | DataGridViewColumn[] columns = new DataGridViewColumn[Content.Columns];
|
---|
| 104 | for (int i = 0; i < columns.Length; ++i) {
|
---|
| 105 | var column = new DataGridViewTextBoxColumn();
|
---|
| 106 | column.FillWeight = 1;
|
---|
| 107 | columns[i] = column;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | DataGridView.Columns.Clear();
|
---|
| 111 | DataGridView.Columns.AddRange(columns);
|
---|
| 112 |
|
---|
| 113 | DataGridView.RowCount = Content.Rows;
|
---|
| 114 |
|
---|
[8492] | 115 | ClearSorting();
|
---|
[8483] | 116 | UpdateColumnHeaders();
|
---|
| 117 | UpdateRowHeaders();
|
---|
| 118 |
|
---|
| 119 | maximumLabel.Text = Content.Maximum.ToString();
|
---|
| 120 | minimumLabel.Text = Content.Minimum.ToString();
|
---|
| 121 |
|
---|
| 122 | DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
|
---|
| 123 | DataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
|
---|
| 124 | DataGridView.Enabled = true;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | protected virtual void UpdateColumnHeaders() {
|
---|
| 128 | for (int i = 0; i < DataGridView.ColumnCount; i++) {
|
---|
| 129 | DataGridView.Columns[i].HeaderText = Content.ColumnNames.ElementAt(i);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | protected void UpdateRowHeaders() {
|
---|
| 133 | for (int i = 0; i < DataGridView.RowCount; i++) {
|
---|
| 134 | DataGridView.Rows[i].HeaderCell.Value = Content.RowNames.ElementAt(virtualRowIndices[i]);
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[8492] | 138 | protected void Content_CorrelationCalculationFinished(object sender, System.EventArgs e) {
|
---|
| 139 | if (InvokeRequired) {
|
---|
| 140 | Invoke(new EventHandler(Content_CorrelationCalculationFinished), sender, e);
|
---|
| 141 | } else {
|
---|
[8483] | 142 | UpdateDataGrid();
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[8529] | 146 | protected void DataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {
|
---|
[8483] | 147 | if (Content != null && DataGridView.Enabled && e.RowIndex < Content.Rows && e.ColumnIndex < Content.Columns) {
|
---|
| 148 | int rowIndex = virtualRowIndices[e.RowIndex];
|
---|
| 149 | e.Value = Content[rowIndex, e.ColumnIndex];
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[8529] | 153 | protected void DataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
|
---|
| 154 | if (Content != null && DataGridView.Enabled && e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.PaintParts.HasFlag(DataGridViewPaintParts.Background)) {
|
---|
| 155 | int rowIndex = virtualRowIndices[e.RowIndex];
|
---|
| 156 | e.CellStyle.BackColor = GetDataPointColor(Content[rowIndex, e.ColumnIndex], Content.Minimum, Content.Maximum);
|
---|
| 157 | }
|
---|
| 158 | e.Paint(e.CellBounds, e.PaintParts);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[8483] | 161 | protected virtual Color GetDataPointColor(double value, double min, double max) {
|
---|
| 162 | IList<Color> colors = ColorGradient.Colors;
|
---|
| 163 | int index = (int)((colors.Count - 1) * (value - min) / (max - min));
|
---|
| 164 | if (index >= colors.Count) index = colors.Count - 1;
|
---|
| 165 | if (index < 0) index = 0;
|
---|
| 166 | return colors[index];
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | #region sort
|
---|
| 170 | protected void DataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
|
---|
| 171 | if (Content != null) {
|
---|
| 172 | if (e.Button == MouseButtons.Left && Content.SortableView) {
|
---|
| 173 | bool addToSortedIndices = (Control.ModifierKeys & Keys.Control) == Keys.Control;
|
---|
| 174 | SortOrder newSortOrder = SortOrder.Ascending;
|
---|
| 175 | if (sortedColumnIndices.Any(x => x.Key == e.ColumnIndex)) {
|
---|
| 176 | SortOrder oldSortOrder = sortedColumnIndices.Where(x => x.Key == e.ColumnIndex).First().Value;
|
---|
| 177 | int enumLength = Enum.GetValues(typeof(SortOrder)).Length;
|
---|
| 178 | newSortOrder = oldSortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), ((((int)oldSortOrder) + 1) % enumLength).ToString());
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | if (!addToSortedIndices)
|
---|
| 182 | sortedColumnIndices.Clear();
|
---|
| 183 |
|
---|
| 184 | if (sortedColumnIndices.Any(x => x.Key == e.ColumnIndex)) {
|
---|
| 185 | int sortedIndex = sortedColumnIndices.FindIndex(x => x.Key == e.ColumnIndex);
|
---|
| 186 | if (newSortOrder != SortOrder.None)
|
---|
| 187 | sortedColumnIndices[sortedIndex] = new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder);
|
---|
| 188 | else
|
---|
| 189 | sortedColumnIndices.RemoveAt(sortedIndex);
|
---|
| 190 | } else
|
---|
| 191 | if (newSortOrder != SortOrder.None)
|
---|
| 192 | sortedColumnIndices.Add(new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder));
|
---|
| 193 | Sort();
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[8492] | 198 | protected virtual void ClearSorting() {
|
---|
| 199 | virtualRowIndices = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
| 200 | sortedColumnIndices.Clear();
|
---|
| 201 | UpdateSortGlyph();
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[8483] | 204 | private void Sort() {
|
---|
| 205 | virtualRowIndices = Sort(sortedColumnIndices);
|
---|
| 206 | UpdateSortGlyph();
|
---|
| 207 | UpdateRowHeaders();
|
---|
[8529] | 208 | DataGridView.Invalidate();
|
---|
[8483] | 209 | }
|
---|
| 210 |
|
---|
| 211 | protected virtual int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
|
---|
| 212 | int[] newSortedIndex = Enumerable.Range(0, Content.Rows).ToArray();
|
---|
| 213 | if (sortedColumns.Count() != 0) {
|
---|
| 214 | rowComparer.SortedIndices = sortedColumns;
|
---|
| 215 | rowComparer.Matrix = Content;
|
---|
| 216 | Array.Sort(newSortedIndex, rowComparer);
|
---|
| 217 | }
|
---|
| 218 | return newSortedIndex;
|
---|
| 219 | }
|
---|
| 220 | private void UpdateSortGlyph() {
|
---|
| 221 | foreach (DataGridViewColumn col in this.DataGridView.Columns)
|
---|
| 222 | col.HeaderCell.SortGlyphDirection = SortOrder.None;
|
---|
| 223 | foreach (KeyValuePair<int, SortOrder> p in sortedColumnIndices)
|
---|
| 224 | this.DataGridView.Columns[p.Key].HeaderCell.SortGlyphDirection = p.Value;
|
---|
| 225 | }
|
---|
| 226 | #endregion
|
---|
| 227 |
|
---|
| 228 | #region copy
|
---|
| 229 | private void DataGridView_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 230 | if (e.Control && e.KeyCode == Keys.C)
|
---|
| 231 | CopyValuesFromDataGridView();
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | private void CopyValuesFromDataGridView() {
|
---|
| 235 | if (DataGridView.SelectedCells.Count == 0) return;
|
---|
| 236 | StringBuilder s = new StringBuilder();
|
---|
| 237 | int minRowIndex = DataGridView.SelectedCells[0].RowIndex;
|
---|
| 238 | int maxRowIndex = DataGridView.SelectedCells[DataGridView.SelectedCells.Count - 1].RowIndex;
|
---|
| 239 | int minColIndex = DataGridView.SelectedCells[0].ColumnIndex;
|
---|
| 240 | int maxColIndex = DataGridView.SelectedCells[DataGridView.SelectedCells.Count - 1].ColumnIndex;
|
---|
| 241 |
|
---|
| 242 | if (minRowIndex > maxRowIndex) {
|
---|
| 243 | int temp = minRowIndex;
|
---|
| 244 | minRowIndex = maxRowIndex;
|
---|
| 245 | maxRowIndex = temp;
|
---|
| 246 | }
|
---|
| 247 | if (minColIndex > maxColIndex) {
|
---|
| 248 | int temp = minColIndex;
|
---|
| 249 | minColIndex = maxColIndex;
|
---|
| 250 | maxColIndex = temp;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | bool addRowNames = DataGridView.AreAllCellsSelected(false) && Content.RowNames.Count() > 0;
|
---|
| 254 | bool addColumnNames = DataGridView.AreAllCellsSelected(false) && Content.ColumnNames.Count() > 0;
|
---|
| 255 |
|
---|
| 256 | //add colum names
|
---|
| 257 | if (addColumnNames) {
|
---|
| 258 | if (addRowNames)
|
---|
| 259 | s.Append('\t');
|
---|
| 260 |
|
---|
| 261 | DataGridViewColumn column = DataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
|
---|
| 262 | while (column != null) {
|
---|
| 263 | s.Append(column.HeaderText);
|
---|
| 264 | s.Append('\t');
|
---|
| 265 | column = DataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
|
---|
| 266 | }
|
---|
| 267 | s.Remove(s.Length - 1, 1); //remove last tab
|
---|
| 268 | s.Append(Environment.NewLine);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | for (int i = minRowIndex; i <= maxRowIndex; i++) {
|
---|
| 272 | int rowIndex = this.virtualRowIndices[i];
|
---|
| 273 | if (addRowNames) {
|
---|
| 274 | s.Append(Content.RowNames.ElementAt(rowIndex));
|
---|
| 275 | s.Append('\t');
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | DataGridViewColumn column = DataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
|
---|
| 279 | while (column != null) {
|
---|
| 280 | DataGridViewCell cell = DataGridView[column.Index, i];
|
---|
| 281 | if (cell.Selected) {
|
---|
| 282 | s.Append(Content[rowIndex, column.Index]);
|
---|
| 283 | s.Append('\t');
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | column = DataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
|
---|
| 287 | }
|
---|
| 288 | s.Remove(s.Length - 1, 1); //remove last tab
|
---|
| 289 | s.Append(Environment.NewLine);
|
---|
| 290 | }
|
---|
| 291 | Clipboard.SetText(s.ToString());
|
---|
| 292 | }
|
---|
| 293 | #endregion
|
---|
| 294 | }
|
---|
| 295 | } |
---|