Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataAnalysis/SvmExporter.cs @ 370

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

added missing GPL headers in HeuristicLab.Analysis

File size: 5.7 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        }
53      }
54      for(int i = 0; i < dataset.Columns; i++) {
55        dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
56        dataGridView.Columns[i].Name = dataset.VariableNames[i];
57      }
58      dataGridView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
59
60      ShowDialog();
61    }
62
63    #endregion
64
65    private void InitializeComponent() {
66      this.dataGridView = new System.Windows.Forms.DataGridView();
67      this.helpLabel = new System.Windows.Forms.Label();
68      this.nextButton = new System.Windows.Forms.Button();
69      ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
70      this.SuspendLayout();
71      //
72      // dataGridView
73      //
74      this.dataGridView.AllowUserToAddRows = false;
75      this.dataGridView.AllowUserToDeleteRows = false;
76      this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
77                  | System.Windows.Forms.AnchorStyles.Right)));
78      this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
79      this.dataGridView.Location = new System.Drawing.Point(12, 25);
80      this.dataGridView.Name = "dataGridView";
81      this.dataGridView.ReadOnly = true;
82      this.dataGridView.Size = new System.Drawing.Size(568, 200);
83      this.dataGridView.TabIndex = 0;
84      this.dataGridView.SelectionChanged += new System.EventHandler(this.dataGridView_SelectionChanged);
85      //
86      // helpLabel
87      //
88      this.helpLabel.AutoSize = true;
89      this.helpLabel.Location = new System.Drawing.Point(12, 9);
90      this.helpLabel.Name = "helpLabel";
91      this.helpLabel.Size = new System.Drawing.Size(231, 13);
92      this.helpLabel.TabIndex = 1;
93      this.helpLabel.Text = "Please select the column of  the target variable.";
94      //
95      // nextButton
96      //
97      this.nextButton.Location = new System.Drawing.Point(505, 231);
98      this.nextButton.Name = "nextButton";
99      this.nextButton.Size = new System.Drawing.Size(75, 23);
100      this.nextButton.TabIndex = 2;
101      this.nextButton.Text = "Next...";
102      this.nextButton.UseVisualStyleBackColor = true;
103      this.nextButton.Click += new System.EventHandler(this.nextButton_Click);
104      //
105      // SvmExporter
106      //
107      this.ClientSize = new System.Drawing.Size(592, 266);
108      this.Controls.Add(this.nextButton);
109      this.Controls.Add(this.helpLabel);
110      this.Controls.Add(this.dataGridView);
111      this.Name = "SvmExporter";
112      this.Text = "Execute to libSVM";
113      ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
114      this.ResumeLayout(false);
115      this.PerformLayout();
116
117    }
118
119    private void dataGridView_SelectionChanged(object sender, EventArgs e) {
120      if(dataGridView.SelectedColumns.Count == 1) nextButton.Enabled = true;
121      else nextButton.Enabled = false;
122    }
123
124    private void nextButton_Click(object sender, EventArgs e) {
125      int targetColumn = dataGridView.SelectedColumns[0].Index;
126
127      SaveFileDialog saveDialog = new SaveFileDialog();
128      if(saveDialog.ShowDialog() == DialogResult.OK) {
129        string filename = saveDialog.FileName;
130        StreamWriter writer = new StreamWriter(filename);
131        for(int i = 0; i < dataset.Rows; i++) {
132          writer.Write(dataset.GetValue(i, targetColumn).ToString("r", CultureInfo.InvariantCulture) + "\t");
133          for(int j = 0; j < dataset.Columns; j++) {
134            if(j != targetColumn) {
135              double val = dataset.GetValue(i, j);
136              if(!double.IsInfinity(val) && !double.IsNaN(val))
137                writer.Write((j + 1) + ":" + val.ToString("r", CultureInfo.InvariantCulture) + "\t");
138            }
139          }
140          writer.WriteLine();
141        }
142        writer.Close();
143      }
144      Close();
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.