Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
GH-663: Adding examples for the CsvWriter Class.
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarsouza committed Jun 29, 2017
1 parent bce541a commit 573317b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Sources/Accord.IO/Csv/CsvWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ namespace Accord.IO
/// <code source="Unit Tests\Accord.Tests.IO\CsvWriterTest.cs" region="doc_matrix" />
///
/// <para>
/// The following example shows how to use <see cref="CsvWriter"/> to write a jagged array in .csv format.</para>
/// <code source="Unit Tests\Accord.Tests.IO\CsvWriterTest.cs" region="doc_jagged" />
///
/// <para>
/// The following example shows how to use <see cref="CsvWriter"/> to write a DataTable in .csv format.</para>
/// <code source="Unit Tests\Accord.Tests.IO\CsvWriterTest.cs" region="doc_table" />
/// </example>
Expand Down
73 changes: 63 additions & 10 deletions Unit Tests/Accord.Tests.IO/CsvWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,73 @@ public void write_matrix()
// https://github.com/accord-net/framework/issues/663

#region doc_matrix
// Tell where to store the matrix (adjust to your own liking)
string filename = Path.Combine(Path.GetTempPath(), "matrix.csv");

// Let's say we want to store a multidimensional array
double[,] values =
{
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
};

// Create a new writer and write the values to disk
using (CsvWriter writer = new CsvWriter(filename))
{
writer.WriteHeaders("a", "b", "c", "d"); // this is optional
writer.Write(values);
}

// Afterwards, we could read them back using
var reader = new CsvReader(filename, hasHeaders: true);
double[,] sameMatrix = reader.ToMatrix();
#endregion

Assert.IsTrue(values.IsEqual(sameMatrix));
}

[Test]
public void write_jagged()
{
// GH-663: Please add an example CsvWriter Class
// https://github.com/accord-net/framework/issues/663

#region doc_jagged
// Tell where to store the matrix (adjust to your own liking)
string filename = Path.Combine(Path.GetTempPath(), "jagged.csv");

// Let's say we want to store a jagged array
double[][] values =
{
new double[] { 1, 2, 3, 4 },
new double[] { 5, 6, 7, 8 },
new double[] { 9, 10, 11, 12 },
};

writer.WriteHeaders("a", "b", "c", "d");
// Create a new writer and write the values to disk
using (CsvWriter writer = new CsvWriter(filename))
{
writer.WriteHeaders("a", "b", "c", "d"); // this is optional
writer.Write(values);
}
#endregion doc_matrix

CsvReader reader = new CsvReader(filename, hasHeaders: true);
double[,] actual = reader.ToMatrix();
Assert.IsTrue(values.IsEqual(actual));
// Afterwards, we could read them back using
var reader = new CsvReader(filename, hasHeaders: true);
double[][] sameMatrix = reader.ToJagged();
#endregion

Assert.IsTrue(values.IsEqual(sameMatrix));

double[] lastColumn = sameMatrix.GetColumn(-1);
double[][] firstColumns = sameMatrix.Get(null, 0, -1);
Assert.AreEqual(lastColumn, new[] { 4.0, 8.0, 12 });
Assert.IsTrue(firstColumns.IsEqual(new []
{
new[] { 1, 2, 3.0 },
new[] { 5, 6, 7.0 },
new[] { 9, 10, 11.0 },
}));
}

[Test]
Expand All @@ -68,23 +115,31 @@ public void write_table()
// GH-663: Please add an example CsvWriter Class
// https://github.com/accord-net/framework/issues/663

#region doc_matrix
#region doc_table
// Tell where to store the data table (adjust to your own liking)
string filename = Path.Combine(Path.GetTempPath(), "table.csv");

// Let's say we have the following data table
DataTable table = new DataTable("My table");
table.Columns.Add("Name");
table.Columns.Add("Age");
table.Columns.Add("City");

// Let's add some rows for these columns
table.Rows.Add("John", 42, "New York");
table.Rows.Add("Josephine", 25, "Grenoble");
table.Rows.Add("João", 22, "Valinhos");

// Create a new writer and write the values to disk
using (CsvWriter writer = new CsvWriter(filename))
{
writer.Write(table);
}
#endregion doc_table

// Later, we could read it back from the disk using
var reader = new CsvReader(filename, hasHeaders: true);
DataTable sameTable = reader.ToTable();
#endregion

string text = new StreamReader(filename).ReadToEnd();
Assert.AreEqual(text,
Expand All @@ -93,9 +148,7 @@ public void write_table()
"\"Josephine\",\"25\",\"Grenoble\"\r\n" +
"\"João\",\"22\",\"Valinhos\"\r\n");

CsvReader reader = new CsvReader(filename, hasHeaders: true);
DataTable actual = reader.ToTable();
Assert.IsTrue(table.ToMatrix<string>().IsEqual(actual.ToMatrix<string>()));
Assert.IsTrue(table.ToMatrix<string>().IsEqual(sameTable.ToMatrix<string>()));
}
}

Expand Down

0 comments on commit 573317b

Please sign in to comment.