[234] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Windows.Forms;
|
---|
| 6 | using System.IO;
|
---|
| 7 | using System.Globalization;
|
---|
| 8 |
|
---|
| 9 | namespace HeuristicLab.DataAnalysis {
|
---|
| 10 | class SvmExporter : Form, IDatasetManipulator {
|
---|
| 11 | private Label helpLabel;
|
---|
| 12 | private Button nextButton;
|
---|
| 13 | private DataGridView dataGridView;
|
---|
| 14 | private Dataset dataset;
|
---|
| 15 | public SvmExporter() : base() {
|
---|
| 16 | InitializeComponent();
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | #region IDatasetManipulator Members
|
---|
| 20 | public string Action {
|
---|
| 21 | get { return "Export to libsvm..."; }
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | public void Execute(Dataset dataset) {
|
---|
| 25 | this.dataset = dataset;
|
---|
| 26 | dataGridView.ColumnCount = dataset.Columns;
|
---|
| 27 | dataGridView.RowCount = dataset.Rows;
|
---|
| 28 | for(int i = 0; i < dataset.Rows; i++) {
|
---|
| 29 | for(int j = 0; j < dataset.Columns; j++) {
|
---|
| 30 | dataGridView.Rows[i].Cells[j].Value = dataset.GetValue(i, j);
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 | for(int i = 0; i < dataset.Columns; i++) {
|
---|
| 34 | dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
|
---|
| 35 | dataGridView.Columns[i].Name = dataset.VariableNames[i];
|
---|
| 36 | }
|
---|
| 37 | dataGridView.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
|
---|
| 38 |
|
---|
| 39 | ShowDialog();
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | #endregion
|
---|
| 43 |
|
---|
| 44 | private void InitializeComponent() {
|
---|
| 45 | this.dataGridView = new System.Windows.Forms.DataGridView();
|
---|
| 46 | this.helpLabel = new System.Windows.Forms.Label();
|
---|
| 47 | this.nextButton = new System.Windows.Forms.Button();
|
---|
| 48 | ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
|
---|
| 49 | this.SuspendLayout();
|
---|
| 50 | //
|
---|
| 51 | // dataGridView
|
---|
| 52 | //
|
---|
| 53 | this.dataGridView.AllowUserToAddRows = false;
|
---|
| 54 | this.dataGridView.AllowUserToDeleteRows = false;
|
---|
| 55 | this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|
---|
| 56 | | System.Windows.Forms.AnchorStyles.Right)));
|
---|
| 57 | this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
|
---|
| 58 | this.dataGridView.Location = new System.Drawing.Point(12, 25);
|
---|
| 59 | this.dataGridView.Name = "dataGridView";
|
---|
| 60 | this.dataGridView.ReadOnly = true;
|
---|
| 61 | this.dataGridView.Size = new System.Drawing.Size(568, 200);
|
---|
| 62 | this.dataGridView.TabIndex = 0;
|
---|
| 63 | this.dataGridView.SelectionChanged += new System.EventHandler(this.dataGridView_SelectionChanged);
|
---|
| 64 | //
|
---|
| 65 | // helpLabel
|
---|
| 66 | //
|
---|
| 67 | this.helpLabel.AutoSize = true;
|
---|
| 68 | this.helpLabel.Location = new System.Drawing.Point(12, 9);
|
---|
| 69 | this.helpLabel.Name = "helpLabel";
|
---|
| 70 | this.helpLabel.Size = new System.Drawing.Size(231, 13);
|
---|
| 71 | this.helpLabel.TabIndex = 1;
|
---|
| 72 | this.helpLabel.Text = "Please select the column of the target variable.";
|
---|
| 73 | //
|
---|
| 74 | // nextButton
|
---|
| 75 | //
|
---|
| 76 | this.nextButton.Location = new System.Drawing.Point(505, 231);
|
---|
| 77 | this.nextButton.Name = "nextButton";
|
---|
| 78 | this.nextButton.Size = new System.Drawing.Size(75, 23);
|
---|
| 79 | this.nextButton.TabIndex = 2;
|
---|
| 80 | this.nextButton.Text = "Next...";
|
---|
| 81 | this.nextButton.UseVisualStyleBackColor = true;
|
---|
| 82 | this.nextButton.Click += new System.EventHandler(this.nextButton_Click);
|
---|
| 83 | //
|
---|
| 84 | // SvmExporter
|
---|
| 85 | //
|
---|
| 86 | this.ClientSize = new System.Drawing.Size(592, 266);
|
---|
| 87 | this.Controls.Add(this.nextButton);
|
---|
| 88 | this.Controls.Add(this.helpLabel);
|
---|
| 89 | this.Controls.Add(this.dataGridView);
|
---|
| 90 | this.Name = "SvmExporter";
|
---|
| 91 | this.Text = "Execute to libSVM";
|
---|
| 92 | ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
|
---|
| 93 | this.ResumeLayout(false);
|
---|
| 94 | this.PerformLayout();
|
---|
| 95 |
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | private void dataGridView_SelectionChanged(object sender, EventArgs e) {
|
---|
| 99 | if(dataGridView.SelectedColumns.Count == 1) nextButton.Enabled = true;
|
---|
| 100 | else nextButton.Enabled = false;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | private void nextButton_Click(object sender, EventArgs e) {
|
---|
| 104 | int targetColumn = dataGridView.SelectedColumns[0].Index;
|
---|
| 105 |
|
---|
| 106 | SaveFileDialog saveDialog = new SaveFileDialog();
|
---|
| 107 | if(saveDialog.ShowDialog() == DialogResult.OK) {
|
---|
| 108 | string filename = saveDialog.FileName;
|
---|
| 109 | StreamWriter writer = new StreamWriter(filename);
|
---|
| 110 | for(int i = 0; i < dataset.Rows; i++) {
|
---|
| 111 | writer.Write(dataset.GetValue(i, targetColumn).ToString(CultureInfo.InvariantCulture) + "\t");
|
---|
| 112 | for(int j = 0; j < dataset.Columns; j++) {
|
---|
| 113 | if(j != targetColumn) {
|
---|
| 114 | double val = dataset.GetValue(i, j);
|
---|
| 115 | if(!double.IsInfinity(val) && !double.IsNaN(val))
|
---|
| 116 | writer.Write((j + 1) + ":" + val.ToString(CultureInfo.InvariantCulture) + "\t");
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | writer.WriteLine();
|
---|
| 120 | }
|
---|
| 121 | writer.Close();
|
---|
| 122 | }
|
---|
| 123 | Close();
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|