Free cookie consent management tool by TermsFeed Policy Generator

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

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

implemented #233 (Shuffle instances in Dataset)

File size: 6.5 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;
25using HeuristicLab.PluginInfrastructure;
26
27namespace HeuristicLab.DataAnalysis {
28  public partial class DatasetView : EditorBase {
29    public Dataset Dataset {
30      get { return (Dataset)Item; }
31      set {
32        Item = value;
33        Refresh();
34      }
35    }
36    public DatasetView()
37      : base() {
38      InitializeComponent();
39      DiscoveryService discovery = new DiscoveryService();
40      IDatasetManipulator[] manipuators = discovery.GetInstances<IDatasetManipulator>();
41      contextMenuStrip.Items.Add(new ToolStripSeparator());
42      foreach(IDatasetManipulator manipulator in manipuators) {
43        contextMenuStrip.Items.Add(new ToolStripButton(manipulator.Action,null , delegate(object source, EventArgs args)
44          {
45            manipulator.Execute(Dataset);
46            UpdateControls();
47          }));
48      }
49     
50      // format all cells with the round-trip formatter to make sure that values that are exported and imported to
51      // another C# app (HL2) have the same numeric value
52      dataGridView.DefaultCellStyle.Format = "r";
53    }
54
55    public DatasetView(Dataset dataset)
56      : this() {
57      this.Dataset = dataset;
58    }
59
60    protected override void UpdateControls() {
61      base.UpdateControls();
62      if (Dataset != null) {
63        // DataGridView is bitching around. When it's columnCount (maybe also rowCount) is changed it creates
64        // new column objects and they have SortMode set to 'automatic'. However this is not allowed if the
65        // selectionmode is set to 'ColumnHeaderSelect' at the same time, resulting in an exception.
66        // A solution is to set the SelectionMode to CellSelect before any changes. After the columns
67        // have been updated (and their SortMode set to 'NotSortable') we switch back to SelectionMode=ColumnHeaderSelect.
68        dataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
69        int rows = Dataset.Rows;
70        int columns = Dataset.Columns;
71        nameTextBox.Text = Dataset.Name;
72        rowsTextBox.Text = rows + "";
73        columnsTextBox.Text = columns + "";
74        dataGridView.ColumnCount = columns;
75        dataGridView.RowCount = rows;
76        for (int i = 0; i < rows; i++) {
77          for (int j = 0; j < columns; j++) {
78            dataGridView.Rows[i].Cells[j].Value = Dataset.GetValue(i, j);
79          }
80        }
81        for (int i = 0; i < columns; i++) {
82          dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; // SortMode has to be NotSortable to allow ColumnHeaderSelect
83          dataGridView.Columns[i].Name = GetColumnName(i);
84        }
85        dataGridView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect; // switch back to column selection
86      } else {
87        rowsTextBox.Text = "1";
88        columnsTextBox.Text = "1";
89        dataGridView.ColumnCount = 1;
90        dataGridView.RowCount = 1;
91      }
92    }
93
94    private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
95      if (ValidateData((string)e.FormattedValue)) {
96        SetArrayElement(e.RowIndex, e.ColumnIndex, (string)e.FormattedValue);
97        e.Cancel = false;
98      } else {
99        e.Cancel = true;
100      }
101    }
102
103
104    private void SetArrayElement(int row, int column, string element) {
105      double result;
106      double.TryParse(element, out result);
107      if (result != Dataset.GetValue(row, column)) {
108        Dataset.SetValue(row, column, result);
109      }
110    }
111
112    private bool ValidateData(string element) {
113      double result;
114      return element != null && double.TryParse(element, out result);
115    }
116
117    private void scaleValuesToolStripMenuItem_Click(object sender, EventArgs e) {
118      foreach(DataGridViewColumn column in dataGridView.SelectedColumns) {
119        Dataset.ScaleVariable(column.Index);
120        column.Name = GetColumnName(column.Index) + " [scaled]";
121      }
122      Refresh();
123    }
124
125    private void originalValuesToolStripMenuItem_Click(object sender, EventArgs e) {
126      foreach(DataGridViewColumn column in dataGridView.SelectedColumns) {
127        Dataset.UnscaleVariable(column.Index);
128        column.Name = GetColumnName(column.Index);
129      }
130      Refresh();     
131    }
132
133    private string GetColumnName(int index) {
134      if(Dataset.VariableNames.Length == dataGridView.Columns.Count) {
135        return Dataset.VariableNames[index];
136      } else {
137        return "Var " + index;
138      }
139    }
140
141    private void showScalingToolStripMenuItem_Click(object sender, EventArgs e) {
142      ManualScalingControl scalingControl = new ManualScalingControl(false);
143      double[,] scalingParameters = new double[2, Dataset.Columns];
144      for(int i = 0; i < Dataset.Columns; i++) {
145        scalingParameters[0, i] = Dataset.ScalingFactor[i];
146        scalingParameters[1, i] = Dataset.ScalingOffset[i];
147      }
148      scalingControl.Data = scalingParameters;
149      scalingControl.ShowDialog();
150    }
151
152    private void scaleValuesmanuallyToolStripMenuItem_Click(object sender, EventArgs e) {
153      ManualScalingControl scalingControl = new ManualScalingControl(true);
154      double[,] scalingParameters = new double[2, Dataset.Columns];
155      for(int i = 0; i < Dataset.Columns; i++) {
156        scalingParameters[0, i] = Dataset.ScalingFactor[i];
157        scalingParameters[1, i] = Dataset.ScalingOffset[i];
158      }
159      scalingControl.Data = scalingParameters;
160      if(scalingControl.ShowDialog() == DialogResult.OK) {
161        for(int i = 0; i < Dataset.Columns; i++) {
162          Dataset.ScaleVariable(i, scalingControl.Data[0, i], scalingControl.Data[1, i]);
163        }
164      }
165      Refresh();
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.