Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing.Views/3.4/DataCompletenessView.cs @ 14820

Last change on this file since 14820 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 5.5 KB
RevLine 
[14075]1#region License Information
2/* HeuristicLab
[14186]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[14075]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
22using System;
[10913]23using System.Collections.Generic;
24using System.Drawing;
[14076]25using System.Linq;
[14075]26using System.Windows.Forms;
[10979]27using System.Windows.Forms.DataVisualization.Charting;
[14075]28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
[10658]30
31namespace HeuristicLab.DataPreprocessing.Views {
32
33  [View("Histogram View")]
[10877]34  [Content(typeof(DataCompletenessChartContent), true)]
[14075]35  public partial class DataCompletenessView : ItemView {
[10979]36    //series colors
[14076]37    private static readonly Color colorNonMissingValue = Color.CornflowerBlue;
38    private static readonly Color colorMissingValue = Color.Orange;
[10913]39
[14075]40    public new DataCompletenessChartContent Content {
[10877]41      get { return (DataCompletenessChartContent)base.Content; }
[10658]42      set { base.Content = value; }
43    }
44
[14075]45    public DataCompletenessView() {
[10877]46      InitializeComponent();
[10736]47    }
48
[14075]49    protected override void OnContentChanged() {
[10818]50      base.OnContentChanged();
[14076]51      if (Content == null) return;
52      InitData();
[10818]53    }
54
[14075]55    private void InitData() {
[10913]56      IDictionary<int, IList<int>> missingValueIndices = Content.SearchLogic.GetMissingValueIndices();
[14076]57
58      bool[,] valueMissing = new bool[Content.SearchLogic.Rows, Content.SearchLogic.Columns];
59      foreach (var columnMissingValues in missingValueIndices) {
60        var column = columnMissingValues.Key;
61        foreach (var missingValueIndex in columnMissingValues.Value)
62          valueMissing[missingValueIndex, column] = true;
[10913]63      }
[14076]64
65      var yValuesPerColumn = ProcessMatrixForCharting(valueMissing);
[10979]66      PrepareChart();
67      CreateSeries(yValuesPerColumn);
[10913]68    }
69
[14075]70    private void PrepareChart() {
[10979]71      chart.Titles.Add("DataCompletenessChart");
[10981]72      chart.EnableDoubleClickResetsZoom = true;
[10979]73      chart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
74      chart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
75      chart.ChartAreas[0].AxisX.IsMarginVisible = false;
76      chart.ChartAreas[0].AxisY.IsMarginVisible = false;
[10981]77      chart.ChartAreas[0].CursorX.IsUserEnabled = true;
78      chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
79      chart.ChartAreas[0].CursorY.IsUserEnabled = true;
80      chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
[10979]81      //custom x axis label
82      double from = 0.5;
[14075]83      foreach (String columnName in Content.SearchLogic.VariableNames) {
[10979]84        double to = from + 1;
85        chart.ChartAreas[0].AxisX.CustomLabels.Add(from, to, columnName);
86        from = to;
87      }
88      //custom y axis label
89      chart.ChartAreas[0].AxisY.IsReversed = true;
90    }
91
[14075]92    private void CreateSeries(List<List<int>> yValuesPerColumn) {
[14076]93      chart.Series.SuspendUpdates();
[10913]94      //prepare series
[10979]95      int seriesCount = DetermineSeriesCount(yValuesPerColumn);
[14075]96      for (int i = 0; i < seriesCount; i++) {
[14076]97        Series series = new Series(CreateSeriesName(i));
[10979]98        series.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;
99        series.IsVisibleInLegend = false;
100        series["PointWidth"] = "1.0";
[14076]101        series.IsVisibleInLegend = i < 2; //only first two series are visible, non-missing and missing values
102        series.Color = i % 2 == 0 ? colorNonMissingValue : colorMissingValue;
103
104        var values = yValuesPerColumn.Select(y => i < y.Count ? y[i] : 0).ToArray();
105        series.Points.DataBindY(values);
106        chart.Series.Add(series);
[10913]107      }
[14076]108      chart.Series.ResumeUpdates();
[10913]109    }
110
[14075]111    private String CreateSeriesName(int index) {
[10979]112      if (index == 0)
113        return "non-missing value";
114      else if (index == 1)
115        return "missing value";
116      return "series" + index;
117    }
118
119    #region data_preparation_for_chartseries
[14075]120    private int DetermineSeriesCount(List<List<int>> yValuesPerColumn) {
[14076]121      return yValuesPerColumn.Max(values => values.Count);
[10913]122    }
123
[14076]124    private List<List<int>> ProcessMatrixForCharting(bool[,] matrix) {
125      var columnsYValues = new List<List<int>>();
126      for (int column = 0; column < matrix.GetLength(1); column++) {
127        var yValues = new List<int>();
[10913]128        bool missingState = false;
129        int valueCount = 0;
[14076]130
131        for (int row = 0; row < matrix.GetLength(0); row++) {
132          if (missingState == matrix[row, column]) {
[10913]133            valueCount++;
[14075]134          } else {
[10913]135            yValues.Add(valueCount);
136            valueCount = 1;
137            missingState = !missingState;
138          }
139        }
140        yValues.Add(valueCount);
[10979]141        if (missingState) //handle last missing
142        {
143          yValues.Add(0);
144        }
[10913]145        columnsYValues.Add(yValues);
146      }
147      return columnsYValues;
148    }
[10979]149    #endregion
[10658]150  }
151}
Note: See TracBrowser for help on using the repository browser.