Free cookie consent management tool by TermsFeed Policy Generator

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

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

merged changes r338 r339 r340 r341 r342 r343 from the ticket-specific branch into the main trunk

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