Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/23/12 10:07:48 (12 years ago)
Author:
sforsten
Message:

#1292:

  • removed combo box in TimeframeCorrelationView and added a textbox instead
  • caches are directly in (Timeframe-)FeatureCorrelationView
  • caches use Tuple<> instead of nested dictionaries
  • a control EnhancedStringConvertibleMatrix inherits from StringConvertibleMatrixView to reduce code duplication
  • add interface IDependencyCalculator to several calculators
  • fixed bug: a previous started calculation is cancelled, if a new calculation shall be started and the values are already in the cache
  • fixed bug: if the content is changed, the calculation is cancelled

HeatMap is still used for the dependency representation, because a class is needed which implements IStringConvertibleMatrix and it has a maximum and minimum value.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/FeatureCorrelation/AbstractFeatureCorrelationView.cs

    r8729 r8833  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    2422using System.ComponentModel;
    25 using System.Drawing;
    2623using System.Linq;
    27 using System.Text;
    2824using System.Windows.Forms;
    2925using HeuristicLab.Analysis;
    30 using HeuristicLab.Common;
    31 using HeuristicLab.Data.Views;
    32 using HeuristicLab.DataAnalysis.Views;
    3326using HeuristicLab.MainForm;
    3427using HeuristicLab.MainForm.WindowsForms;
     28using HeuristicLab.PluginInfrastructure;
    3529
    3630namespace HeuristicLab.Problems.DataAnalysis.Views {
     
    3832  [Content(typeof(DataAnalysisProblemData), false)]
    3933  public abstract partial class AbstractFeatureCorrelationView : AsynchronousContentView {
    40 
    41     private int[] virtualRowIndices;
    42     private VariableVisibilityDialog variableVisibility;
    43     private List<KeyValuePair<int, SortOrder>> sortedColumnIndices;
    44     private StringConvertibleMatrixView.RowComparer rowComparer;
    45 
    4634    protected FeatureCorrelationCalculator fcc;
    4735    protected HeatMap currentCorrelation;
     
    5442    protected AbstractFeatureCorrelationView() {
    5543      InitializeComponent();
    56       sortedColumnIndices = new List<KeyValuePair<int, SortOrder>>();
    57       rowComparer = new StringConvertibleMatrixView.RowComparer();
    5844      fcc = new FeatureCorrelationCalculator();
    59       var calculatorList = FeatureCorrelationEnums.EnumToList<FeatureCorrelationEnums.CorrelationCalculators>().Select(x => new KeyValuePair<FeatureCorrelationEnums.CorrelationCalculators, string>(x, FeatureCorrelationEnums.GetEnumDescription(x))).ToList();
    60       CorrelationCalcComboBox.ValueMember = "Key";
    61       CorrelationCalcComboBox.DisplayMember = "Value";
    62       CorrelationCalcComboBox.DataSource = new BindingList<KeyValuePair<FeatureCorrelationEnums.CorrelationCalculators, string>>(calculatorList);
    63       var partitionList = FeatureCorrelationEnums.EnumToList<FeatureCorrelationEnums.Partitions>().Select(x => new KeyValuePair<FeatureCorrelationEnums.Partitions, string>(x, FeatureCorrelationEnums.GetEnumDescription(x))).ToList();
    64       PartitionComboBox.ValueMember = "Key";
    65       PartitionComboBox.DisplayMember = "Value";
    66       PartitionComboBox.DataSource = new BindingList<KeyValuePair<FeatureCorrelationEnums.Partitions, string>>(partitionList);
     45      var calculators = ApplicationManager.Manager.GetInstances<IDependencyCalculator>();
     46      var calcList = calculators.OrderBy(c => c.Name).Select(c => new { Name = c.Name, Calculator = c }).ToList();
     47      CorrelationCalcComboBox.ValueMember = "Calculator";
     48      CorrelationCalcComboBox.DisplayMember = "Name";
     49      CorrelationCalcComboBox.DataSource = calcList;
     50      CorrelationCalcComboBox.SelectedItem = calcList.First(c => c.Calculator.GetType().Equals(typeof(PearsonsRDependenceCalculator)));
     51      PartitionComboBox.DataSource = FeatureCorrelationPartitions.Partitions;
     52      PartitionComboBox.SelectedItem = FeatureCorrelationPartitions.TRAININGSAMPLES;
    6753    }
    6854
     
    8167    protected override void OnContentChanged() {
    8268      base.OnContentChanged();
     69      fcc.TryCancelCalculation();
    8370      if (Content != null) {
    8471        fcc.ProblemData = Content;
    85         bool[] initialVisibility = SetInitialVisibilityOfColumns();
    86 
    87         variableVisibility = new VariableVisibilityDialog(Content.Dataset.DoubleVariables, initialVisibility);
    88         variableVisibility.VariableVisibilityChanged += new ItemCheckEventHandler(variableVisibility_VariableVisibilityChanged);
    8972        CalculateCorrelation();
    9073      } else {
    91         DataGridView.Columns.Clear();
    92         DataGridView.Rows.Clear();
     74        dataView.Content = null;
     75        dataView.ResetVisibility();
    9376      }
    9477    }
    9578
    96     protected virtual bool[] SetInitialVisibilityOfColumns() {
     79    protected virtual bool[] SetInitialVariableVisibility() {
    9780      bool[] initialVisibility = new bool[Content.Dataset.DoubleVariables.Count()];
    9881      int i = 0;
     
    10386      return initialVisibility;
    10487    }
    105 
    106     protected abstract void variableVisibility_VariableVisibilityChanged(object sender, ItemCheckEventArgs e);
    10788
    10889    protected void CorrelationMeasureComboBox_SelectedChangeCommitted(object sender, System.EventArgs e) {
     
    11697    protected abstract void Content_CorrelationCalculationFinished(object sender, FeatureCorrelationCalculator.CorrelationCalculationFinishedArgs e);
    11798
    118     protected void UpdateDataGrid() {
    119       virtualRowIndices = Enumerable.Range(0, currentCorrelation.Rows).ToArray();
    120       DataGridViewColumn[] columns = new DataGridViewColumn[currentCorrelation.Columns];
    121       for (int i = 0; i < columns.Length; ++i) {
    122         var column = new DataGridViewTextBoxColumn();
    123         column.FillWeight = 1;
    124         columns[i] = column;
    125       }
    126 
    127       DataGridView.Columns.Clear();
    128       DataGridView.Columns.AddRange(columns);
    129 
    130       DataGridView.RowCount = currentCorrelation.Rows;
    131 
    132       ClearSorting();
    133       UpdateColumnHeaders();
    134       UpdateRowHeaders();
    135 
     99    protected void UpdateDataView() {
    136100      maximumLabel.Text = currentCorrelation.Maximum.ToString();
    137101      minimumLabel.Text = currentCorrelation.Minimum.ToString();
    138102
    139       DataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
    140       DataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
    141       DataGridView.Enabled = true;
    142     }
    143 
    144     protected virtual void UpdateColumnHeaders() {
    145       for (int i = 0; i < DataGridView.ColumnCount; i++) {
    146         DataGridView.Columns[i].HeaderText = currentCorrelation.ColumnNames.ElementAt(i);
    147         DataGridView.Columns[i].Visible = variableVisibility.Visibility[i];
    148       }
    149     }
    150     protected virtual void UpdateRowHeaders() {
    151       for (int i = 0; i < DataGridView.RowCount; i++) {
    152         DataGridView.Rows[i].HeaderCell.Value = currentCorrelation.RowNames.ElementAt(virtualRowIndices[i]);
    153         DataGridView.Rows[i].Visible = variableVisibility.Visibility[virtualRowIndices[i]];
    154       }
     103      currentCorrelation.SortableView = true;
     104      dataView.Content = currentCorrelation;
     105      dataView.Enabled = true;
    155106    }
    156107
     
    163114      HeatMapProgressBar.Value = e.ProgressPercentage;
    164115    }
    165 
    166     protected void DataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) {
    167       if (Content == null) return;
    168       int rowIndex = virtualRowIndices[e.RowIndex];
    169       e.Value = currentCorrelation[rowIndex, e.ColumnIndex];
    170     }
    171 
    172     protected void DataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) {
    173       if (Content == null) return;
    174       if (e.RowIndex < 0) return;
    175       if (e.ColumnIndex < 0) return;
    176       if (e.State.HasFlag(DataGridViewElementStates.Selected)) return;
    177       if (!e.PaintParts.HasFlag(DataGridViewPaintParts.Background)) return;
    178 
    179       int rowIndex = virtualRowIndices[e.RowIndex];
    180       Color backColor = GetDataPointColor(currentCorrelation[rowIndex, e.ColumnIndex], currentCorrelation.Minimum, currentCorrelation.Maximum);
    181       using (Brush backColorBrush = new SolidBrush(backColor)) {
    182         e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
    183       }
    184       e.PaintContent(e.CellBounds);
    185       e.Handled = true;
    186     }
    187 
    188     protected virtual Color GetDataPointColor(double value, double min, double max) {
    189       if (double.IsNaN(value)) {
    190         return Color.DarkGray;
    191       }
    192       IList<Color> colors = ColorGradient.Colors;
    193       int index = (int)((colors.Count - 1) * (value - min) / (max - min));
    194       if (index >= colors.Count) index = colors.Count - 1;
    195       if (index < 0) index = 0;
    196       return colors[index];
    197     }
    198 
    199     #region sort
    200     protected void DataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) {
    201       if (Content != null) {
    202         if (e.Button == MouseButtons.Left) {
    203           bool addToSortedIndices = (Control.ModifierKeys & Keys.Control) == Keys.Control;
    204           SortOrder newSortOrder = SortOrder.Ascending;
    205           if (sortedColumnIndices.Any(x => x.Key == e.ColumnIndex)) {
    206             SortOrder oldSortOrder = sortedColumnIndices.Where(x => x.Key == e.ColumnIndex).First().Value;
    207             int enumLength = Enum.GetValues(typeof(SortOrder)).Length;
    208             newSortOrder = oldSortOrder = (SortOrder)Enum.Parse(typeof(SortOrder), ((((int)oldSortOrder) + 1) % enumLength).ToString());
    209           }
    210 
    211           if (!addToSortedIndices)
    212             sortedColumnIndices.Clear();
    213 
    214           if (sortedColumnIndices.Any(x => x.Key == e.ColumnIndex)) {
    215             int sortedIndex = sortedColumnIndices.FindIndex(x => x.Key == e.ColumnIndex);
    216             if (newSortOrder != SortOrder.None)
    217               sortedColumnIndices[sortedIndex] = new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder);
    218             else
    219               sortedColumnIndices.RemoveAt(sortedIndex);
    220           } else
    221             if (newSortOrder != SortOrder.None)
    222               sortedColumnIndices.Add(new KeyValuePair<int, SortOrder>(e.ColumnIndex, newSortOrder));
    223           Sort();
    224         }
    225       }
    226     }
    227 
    228     protected virtual void ClearSorting() {
    229       virtualRowIndices = Enumerable.Range(0, currentCorrelation.Rows).ToArray();
    230       sortedColumnIndices.Clear();
    231       UpdateSortGlyph();
    232     }
    233 
    234     private void Sort() {
    235       virtualRowIndices = Sort(sortedColumnIndices);
    236       UpdateSortGlyph();
    237       UpdateRowHeaders();
    238       DataGridView.Invalidate();
    239     }
    240 
    241     protected virtual int[] Sort(IEnumerable<KeyValuePair<int, SortOrder>> sortedColumns) {
    242       int[] newSortedIndex = Enumerable.Range(0, currentCorrelation.Rows).ToArray();
    243       if (sortedColumns.Count() != 0) {
    244         rowComparer.SortedIndices = sortedColumns;
    245         rowComparer.Matrix = currentCorrelation;
    246         Array.Sort(newSortedIndex, rowComparer);
    247       }
    248       return newSortedIndex;
    249     }
    250     private void UpdateSortGlyph() {
    251       foreach (DataGridViewColumn col in this.DataGridView.Columns)
    252         col.HeaderCell.SortGlyphDirection = SortOrder.None;
    253       foreach (KeyValuePair<int, SortOrder> p in sortedColumnIndices)
    254         this.DataGridView.Columns[p.Key].HeaderCell.SortGlyphDirection = p.Value;
    255     }
    256     #endregion
    257 
    258     #region copy
    259     protected void DataGridView_KeyDown(object sender, KeyEventArgs e) {
    260       if (e.Control && e.KeyCode == Keys.C)
    261         CopyValuesFromDataGridView();
    262     }
    263 
    264     private void CopyValuesFromDataGridView() {
    265       if (DataGridView.SelectedCells.Count == 0) return;
    266       StringBuilder s = new StringBuilder();
    267       int minRowIndex = DataGridView.SelectedCells[0].RowIndex;
    268       int maxRowIndex = DataGridView.SelectedCells[DataGridView.SelectedCells.Count - 1].RowIndex;
    269       int minColIndex = DataGridView.SelectedCells[0].ColumnIndex;
    270       int maxColIndex = DataGridView.SelectedCells[DataGridView.SelectedCells.Count - 1].ColumnIndex;
    271 
    272       if (minRowIndex > maxRowIndex) {
    273         int temp = minRowIndex;
    274         minRowIndex = maxRowIndex;
    275         maxRowIndex = temp;
    276       }
    277       if (minColIndex > maxColIndex) {
    278         int temp = minColIndex;
    279         minColIndex = maxColIndex;
    280         maxColIndex = temp;
    281       }
    282 
    283       bool addRowNames = DataGridView.AreAllCellsSelected(false) && currentCorrelation.RowNames.Count() > 0;
    284       bool addColumnNames = DataGridView.AreAllCellsSelected(false) && currentCorrelation.ColumnNames.Count() > 0;
    285 
    286       //add colum names
    287       if (addColumnNames) {
    288         if (addRowNames)
    289           s.Append('\t');
    290 
    291         DataGridViewColumn column = DataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
    292         while (column != null) {
    293           s.Append(column.HeaderText);
    294           s.Append('\t');
    295           column = DataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
    296         }
    297         s.Remove(s.Length - 1, 1); //remove last tab
    298         s.Append(Environment.NewLine);
    299       }
    300 
    301       for (int i = minRowIndex; i <= maxRowIndex; i++) {
    302         int rowIndex = this.virtualRowIndices[i];
    303         if (addRowNames) {
    304           s.Append(currentCorrelation.RowNames.ElementAt(rowIndex));
    305           s.Append('\t');
    306         }
    307 
    308         DataGridViewColumn column = DataGridView.Columns.GetFirstColumn(DataGridViewElementStates.Visible);
    309         while (column != null) {
    310           DataGridViewCell cell = DataGridView[column.Index, i];
    311           if (cell.Selected) {
    312             s.Append(currentCorrelation[rowIndex, column.Index]);
    313             s.Append('\t');
    314           }
    315 
    316           column = DataGridView.Columns.GetNextColumn(column, DataGridViewElementStates.Visible, DataGridViewElementStates.None);
    317         }
    318         s.Remove(s.Length - 1, 1); //remove last tab
    319         s.Append(Environment.NewLine);
    320       }
    321       Clipboard.SetText(s.ToString());
    322     }
    323     #endregion
    324 
    325     protected void ShowHideColumns_Click(object sender, EventArgs e) {
    326       variableVisibility.ShowDialog();
    327     }
    328 
    329     protected void DataGridView_MouseClick(object sender, MouseEventArgs e) {
    330       if (Content == null) return;
    331       if (e.Button == MouseButtons.Right && DataGridView.Columns.Count != 0)
    332         contextMenu.Show(MousePosition);
    333     }
    334 
    335     protected int GetRowIndexOfVirtualindex(int virtualIndex) {
    336       if (virtualIndex < 0 || virtualIndex >= virtualRowIndices.Length) {
    337         throw new ArgumentException("Virtual index is out of bounds");
    338       }
    339 
    340       for (int i = 0; i < virtualRowIndices.Length; i++) {
    341         if (virtualRowIndices[i] == virtualIndex) {
    342           return i;
    343         }
    344       }
    345       throw new ArgumentException("Virtual index was not found!");
    346     }
    347116  }
    348117}
Note: See TracChangeset for help on using the changeset viewer.