[2] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Windows.Forms;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
[234] | 25 | using HeuristicLab.PluginInfrastructure;
|
---|
[2] | 26 |
|
---|
| 27 | namespace HeuristicLab.DataAnalysis {
|
---|
[232] | 28 | public partial class DatasetView : EditorBase {
|
---|
[2] | 29 | public Dataset Dataset {
|
---|
| 30 | get { return (Dataset)Item; }
|
---|
| 31 | set {
|
---|
| 32 | Item = value;
|
---|
[168] | 33 | Refresh();
|
---|
[2] | 34 | }
|
---|
| 35 | }
|
---|
[168] | 36 | public DatasetView()
|
---|
| 37 | : base() {
|
---|
[2] | 38 | InitializeComponent();
|
---|
[234] | 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)
|
---|
[454] | 44 | {
|
---|
| 45 | manipulator.Execute(Dataset);
|
---|
[459] | 46 | Refresh();
|
---|
[454] | 47 | }));
|
---|
[234] | 48 | }
|
---|
[344] | 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";
|
---|
[2] | 53 | }
|
---|
| 54 |
|
---|
[168] | 55 | public DatasetView(Dataset dataset)
|
---|
| 56 | : this() {
|
---|
[2] | 57 | this.Dataset = dataset;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | protected override void UpdateControls() {
|
---|
| 61 | base.UpdateControls();
|
---|
[168] | 62 | if (Dataset != null) {
|
---|
[276] | 63 | // DataGridView is bitching around. When it's columnCount (maybe also rowCount) is changed it creates
|
---|
[275] | 64 | // new column objects and they have SortMode set to 'automatic'. However this is not allowed if the
|
---|
[276] | 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
|
---|
[275] | 67 | // have been updated (and their SortMode set to 'NotSortable') we switch back to SelectionMode=ColumnHeaderSelect.
|
---|
| 68 | dataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
|
---|
[473] | 69 | dataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
|
---|
[2] | 70 | int rows = Dataset.Rows;
|
---|
| 71 | int columns = Dataset.Columns;
|
---|
| 72 | nameTextBox.Text = Dataset.Name;
|
---|
| 73 | rowsTextBox.Text = rows + "";
|
---|
| 74 | columnsTextBox.Text = columns + "";
|
---|
| 75 | dataGridView.ColumnCount = columns;
|
---|
| 76 | dataGridView.RowCount = rows;
|
---|
[168] | 77 | for (int i = 0; i < rows; i++) {
|
---|
| 78 | for (int j = 0; j < columns; j++) {
|
---|
[2] | 79 | dataGridView.Rows[i].Cells[j].Value = Dataset.GetValue(i, j);
|
---|
[458] | 80 | dataGridView.Rows[i].HeaderCell.Value = i.ToString();
|
---|
[2] | 81 | }
|
---|
| 82 | }
|
---|
[232] | 83 | for (int i = 0; i < columns; i++) {
|
---|
[275] | 84 | dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; // SortMode has to be NotSortable to allow ColumnHeaderSelect
|
---|
[232] | 85 | dataGridView.Columns[i].Name = GetColumnName(i);
|
---|
[2] | 86 | }
|
---|
[275] | 87 | dataGridView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect; // switch back to column selection
|
---|
[473] | 88 | dataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
|
---|
[2] | 89 | } else {
|
---|
| 90 | rowsTextBox.Text = "1";
|
---|
| 91 | columnsTextBox.Text = "1";
|
---|
| 92 | dataGridView.ColumnCount = 1;
|
---|
| 93 | dataGridView.RowCount = 1;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
|
---|
[168] | 98 | if (ValidateData((string)e.FormattedValue)) {
|
---|
[2] | 99 | SetArrayElement(e.RowIndex, e.ColumnIndex, (string)e.FormattedValue);
|
---|
| 100 | e.Cancel = false;
|
---|
| 101 | } else {
|
---|
| 102 | e.Cancel = true;
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 | private void SetArrayElement(int row, int column, string element) {
|
---|
| 108 | double result;
|
---|
| 109 | double.TryParse(element, out result);
|
---|
[168] | 110 | if (result != Dataset.GetValue(row, column)) {
|
---|
[2] | 111 | Dataset.SetValue(row, column, result);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | private bool ValidateData(string element) {
|
---|
| 116 | double result;
|
---|
| 117 | return element != null && double.TryParse(element, out result);
|
---|
| 118 | }
|
---|
[232] | 119 |
|
---|
| 120 | private void scaleValuesToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 121 | foreach(DataGridViewColumn column in dataGridView.SelectedColumns) {
|
---|
[237] | 122 | Dataset.ScaleVariable(column.Index);
|
---|
| 123 | column.Name = GetColumnName(column.Index) + " [scaled]";
|
---|
[232] | 124 | }
|
---|
| 125 | Refresh();
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | private void originalValuesToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 129 | foreach(DataGridViewColumn column in dataGridView.SelectedColumns) {
|
---|
[237] | 130 | Dataset.UnscaleVariable(column.Index);
|
---|
| 131 | column.Name = GetColumnName(column.Index);
|
---|
[232] | 132 | }
|
---|
| 133 | Refresh();
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | private string GetColumnName(int index) {
|
---|
| 137 | if(Dataset.VariableNames.Length == dataGridView.Columns.Count) {
|
---|
| 138 | return Dataset.VariableNames[index];
|
---|
| 139 | } else {
|
---|
| 140 | return "Var " + index;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
[312] | 143 |
|
---|
| 144 | private void showScalingToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 145 | ManualScalingControl scalingControl = new ManualScalingControl(false);
|
---|
| 146 | double[,] scalingParameters = new double[2, Dataset.Columns];
|
---|
| 147 | for(int i = 0; i < Dataset.Columns; i++) {
|
---|
| 148 | scalingParameters[0, i] = Dataset.ScalingFactor[i];
|
---|
| 149 | scalingParameters[1, i] = Dataset.ScalingOffset[i];
|
---|
| 150 | }
|
---|
| 151 | scalingControl.Data = scalingParameters;
|
---|
| 152 | scalingControl.ShowDialog();
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | private void scaleValuesmanuallyToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 156 | ManualScalingControl scalingControl = new ManualScalingControl(true);
|
---|
| 157 | double[,] scalingParameters = new double[2, Dataset.Columns];
|
---|
| 158 | for(int i = 0; i < Dataset.Columns; i++) {
|
---|
| 159 | scalingParameters[0, i] = Dataset.ScalingFactor[i];
|
---|
| 160 | scalingParameters[1, i] = Dataset.ScalingOffset[i];
|
---|
| 161 | }
|
---|
| 162 | scalingControl.Data = scalingParameters;
|
---|
| 163 | if(scalingControl.ShowDialog() == DialogResult.OK) {
|
---|
| 164 | for(int i = 0; i < Dataset.Columns; i++) {
|
---|
| 165 | Dataset.ScaleVariable(i, scalingControl.Data[0, i], scalingControl.Data[1, i]);
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | Refresh();
|
---|
| 169 | }
|
---|
[2] | 170 | }
|
---|
[232] | 171 | } |
---|