Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataAnalysis/DatasetView.cs @ 232

Last change on this file since 232 was 232, checked in by gkronber, 16 years ago

implemented #144

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
22using System;
23using System.Windows.Forms;
24using HeuristicLab.Core;
25
26namespace HeuristicLab.DataAnalysis {
27  public partial class DatasetView : EditorBase {
28    public Dataset Dataset {
29      get { return (Dataset)Item; }
30      set {
31        Item = value;
32        Refresh();
33      }
34    }
35
36    private double[] scalingFactor;
37    private double[] scalingOffset;
38    public DatasetView()
39      : base() {
40      InitializeComponent();
41    }
42
43    public DatasetView(Dataset dataset)
44      : this() {
45      this.Dataset = dataset;
46    }
47
48    protected override void UpdateControls() {
49      base.UpdateControls();
50      if(this.scalingFactor == null) {
51        this.scalingFactor = new double[Dataset.Columns];
52        this.scalingOffset = new double[Dataset.Columns];
53        for(int i = 0; i < scalingFactor.Length; i++) {
54          scalingFactor[i] = 1.0;
55          scalingOffset[i] = 0.0;
56        }
57      }
58      if (Dataset != null) {
59        int rows = Dataset.Rows;
60        int columns = Dataset.Columns;
61        nameTextBox.Text = Dataset.Name;
62        rowsTextBox.Text = rows + "";
63        columnsTextBox.Text = columns + "";
64        dataGridView.ColumnCount = columns;
65        dataGridView.RowCount = rows;
66        for (int i = 0; i < rows; i++) {
67          for (int j = 0; j < columns; j++) {
68            dataGridView.Rows[i].Cells[j].Value = Dataset.GetValue(i, j);
69          }
70        }
71        for (int i = 0; i < columns; i++) {
72          dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
73          dataGridView.Columns[i].Name = GetColumnName(i);
74          dataGridView.Columns[i].ContextMenuStrip = contextMenuStrip;
75        }
76        dataGridView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
77      } else {
78        rowsTextBox.Text = "1";
79        columnsTextBox.Text = "1";
80        dataGridView.ColumnCount = 1;
81        dataGridView.RowCount = 1;
82      }
83    }
84
85    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
86      if (ValidateData((string)e.FormattedValue)) {
87        SetArrayElement(e.RowIndex, e.ColumnIndex, (string)e.FormattedValue);
88        e.Cancel = false;
89      } else {
90        e.Cancel = true;
91      }
92    }
93
94
95    private void SetArrayElement(int row, int column, string element) {
96      double result;
97      double.TryParse(element, out result);
98      if (result != Dataset.GetValue(row, column)) {
99        Dataset.SetValue(row, column, result);
100        Dataset.FireChanged();
101      }
102    }
103
104    private bool ValidateData(string element) {
105      double result;
106      return element != null && double.TryParse(element, out result);
107    }
108
109    private void exportButton_Click(object sender, EventArgs e) {
110      throw new NotImplementedException();
111    }
112
113    private void scaleValuesToolStripMenuItem_Click(object sender, EventArgs e) {
114      foreach(DataGridViewColumn column in dataGridView.SelectedColumns) {
115        if(scalingFactor[column.Index] == 1.0) {
116          double min = Dataset.GetMinimum(column.Index);
117          double max = Dataset.GetMaximum(column.Index);
118          double range = max - min;
119          scalingFactor[column.Index] = range;
120          scalingOffset[column.Index] = min;
121          column.Name = GetColumnName(column.Index) + " [scaled]";
122          for(int i = 0; i < Dataset.Rows; i++) {
123            Dataset.SetValue(i, column.Index, (Dataset.GetValue(i, column.Index)-min) / range);
124          }
125        }
126      }
127      Refresh();
128    }
129
130    private void originalValuesToolStripMenuItem_Click(object sender, EventArgs e) {
131      foreach(DataGridViewColumn column in dataGridView.SelectedColumns) {
132        if(scalingFactor[column.Index] != 1.0) {
133          column.Name = GetColumnName(column.Index);
134          for(int i = 0; i < Dataset.Rows; i++) {
135            Dataset.SetValue(i, column.Index, Dataset.GetValue(i, column.Index) * scalingFactor[column.Index] + scalingOffset[column.Index]);
136          }
137          scalingFactor[column.Index] = 1.0;
138          scalingOffset[column.Index] = 0.0;
139        }
140      }
141      Refresh();     
142    }
143
144    private string GetColumnName(int index) {
145      if(Dataset.VariableNames.Length == dataGridView.Columns.Count) {
146        return Dataset.VariableNames[index];
147      } else {
148        return "Var " + index;
149      }
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.