Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.DataAnalysis/SvmExporter.cs @ 2857

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

fixed #234 GUI (layout of SvmExporter is botched).
Also added line numbers in the row headers

File size: 7.0 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.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Windows.Forms;
27using System.IO;
28using System.Globalization;
29
30namespace HeuristicLab.DataAnalysis {
31  class SvmExporter : Form, IDatasetManipulator {
32    private Label helpLabel;
33    private Button nextButton;
34    private DataGridView dataGridView;
35    private Dataset dataset;
36    public SvmExporter() : base() {
37      InitializeComponent();
38    }
39
40    #region IDatasetManipulator Members
41    public string Action {
42      get { return "Export to libsvm..."; }
43    }
44
45    public void Execute(Dataset dataset) {
46      this.dataset = dataset;
47      dataGridView.ColumnCount = dataset.Columns;
48      dataGridView.RowCount = dataset.Rows;
49      for(int i = 0; i < dataset.Rows; i++) {
50        for(int j = 0; j < dataset.Columns; j++) {
51          dataGridView.Rows[i].Cells[j].Value = dataset.GetValue(i, j);
52          dataGridView.Rows[i].HeaderCell.Value = i.ToString();
53        }
54      }
55      for(int i = 0; i < dataset.Columns; i++) {
56        dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
57        dataGridView.Columns[i].Name = dataset.VariableNames[i];
58      }
59      dataGridView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
60
61      ShowDialog();
62    }
63
64    #endregion
65
66    private void InitializeComponent() {
67      System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
68      this.dataGridView = new System.Windows.Forms.DataGridView();
69      this.helpLabel = new System.Windows.Forms.Label();
70      this.nextButton = new System.Windows.Forms.Button();
71      ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
72      this.SuspendLayout();
73      //
74      // dataGridView
75      //
76      this.dataGridView.AllowUserToAddRows = false;
77      this.dataGridView.AllowUserToDeleteRows = false;
78      this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
79                  | System.Windows.Forms.AnchorStyles.Left)
80                  | System.Windows.Forms.AnchorStyles.Right)));
81      this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
82      this.dataGridView.Location = new System.Drawing.Point(12, 25);
83      this.dataGridView.Name = "dataGridView";
84      this.dataGridView.ReadOnly = true;
85      dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight;
86      dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
87      dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
88      dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
89      dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
90      dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
91      dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
92      this.dataGridView.RowHeadersDefaultCellStyle = dataGridViewCellStyle1;
93      this.dataGridView.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
94      this.dataGridView.Size = new System.Drawing.Size(568, 200);
95      this.dataGridView.TabIndex = 0;
96      this.dataGridView.SelectionChanged += new System.EventHandler(this.dataGridView_SelectionChanged);
97      //
98      // helpLabel
99      //
100      this.helpLabel.AutoSize = true;
101      this.helpLabel.Location = new System.Drawing.Point(12, 9);
102      this.helpLabel.Name = "helpLabel";
103      this.helpLabel.Size = new System.Drawing.Size(231, 13);
104      this.helpLabel.TabIndex = 1;
105      this.helpLabel.Text = "Please select the column of  the target variable.";
106      //
107      // nextButton
108      //
109      this.nextButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
110      this.nextButton.Location = new System.Drawing.Point(505, 231);
111      this.nextButton.Name = "nextButton";
112      this.nextButton.Size = new System.Drawing.Size(75, 23);
113      this.nextButton.TabIndex = 2;
114      this.nextButton.Text = "Next...";
115      this.nextButton.UseVisualStyleBackColor = true;
116      this.nextButton.Click += new System.EventHandler(this.nextButton_Click);
117      //
118      // SvmExporter
119      //
120      this.ClientSize = new System.Drawing.Size(592, 266);
121      this.Controls.Add(this.nextButton);
122      this.Controls.Add(this.helpLabel);
123      this.Controls.Add(this.dataGridView);
124      this.Name = "SvmExporter";
125      this.Text = "Export to libSVM";
126      ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
127      this.ResumeLayout(false);
128      this.PerformLayout();
129
130    }
131
132    private void dataGridView_SelectionChanged(object sender, EventArgs e) {
133      if(dataGridView.SelectedColumns.Count == 1) nextButton.Enabled = true;
134      else nextButton.Enabled = false;
135    }
136
137    private void nextButton_Click(object sender, EventArgs e) {
138      int targetColumn = dataGridView.SelectedColumns[0].Index;
139
140      SaveFileDialog saveDialog = new SaveFileDialog();
141      if(saveDialog.ShowDialog() == DialogResult.OK) {
142        string filename = saveDialog.FileName;
143        StreamWriter writer = new StreamWriter(filename);
144        for(int i = 0; i < dataset.Rows; i++) {
145          writer.Write(dataset.GetValue(i, targetColumn).ToString("r", CultureInfo.InvariantCulture) + "\t");
146          for(int j = 0; j < dataset.Columns; j++) {
147            if(j != targetColumn) {
148              double val = dataset.GetValue(i, j);
149              if(!double.IsInfinity(val) && !double.IsNaN(val))
150                writer.Write((j + 1) + ":" + val.ToString("r", CultureInfo.InvariantCulture) + "\t");
151            }
152          }
153          writer.WriteLine();
154        }
155        writer.Close();
156      }
157      Close();
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.