Skip to content

Commit

Permalink
All tests done other than Runtime tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gatapia committed Sep 11, 2013
1 parent 7e65b97 commit 4759737
Show file tree
Hide file tree
Showing 11 changed files with 644 additions and 158 deletions.
25 changes: 25 additions & 0 deletions PicNetML.Tests/AttributeExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NUnit.Framework;
using PicNetML.Tests.TestUtils;

namespace PicNetML.Tests
{
[TestFixture]
public class AttributeExtensionsTests
{
[Test] public void test_attribute_to_enumerable_gives_all_distinct_nominal_vals()
{
var rt = TestingHelpers.LoadSmallRuntime<TitanicDataRow>("titanic_train.csv", 0, 10);
var pcclass = rt.Impl.attribute(1);
var values = pcclass.ToEnumerable();
Assert.AreEqual(new [] {"1", "2", "3"}, values);
}

[Test] public void test_attribute_to_enumerable_gives_nothing_on_numerics()
{
var rt = TestingHelpers.LoadSmallRuntime<TitanicDataRow2>("titanic_train.csv", 0, 10);
var age = rt.Impl.attribute(4);
var values = age.ToEnumerable();
Assert.AreEqual(new string [0], values);
}
}
}
26 changes: 26 additions & 0 deletions PicNetML.Tests/InstanceExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Linq;
using NUnit.Framework;
using PicNetML.Tests.TestUtils;

namespace PicNetML.Tests
{
[TestFixture]
public class InstanceExtensionsTests
{
[Test] public void test_ToStrings_gets_the_string_representation_of_the_instance()
{
var rt = TestingHelpers.LoadSmallRuntime<TitanicDataRow2>("titanic_train.csv", 0, 3);
var instance = rt[0].Impl;
var strs = instance.ToStrings();
Assert.AreEqual(new [] {"0", "3", "Braund, Mr. Owen Harris", "male", "22", "1", "0", "A/5 21171", "7.25", "?", "S"}, strs);
}

[Test] public void test_ToEnumerableAttributes()
{
var rt = TestingHelpers.LoadSmallRuntime<TitanicDataRow>("titanic_train.csv", 0, 3);
var atts = rt[0].Impl.ToEnumerableAttributes();
var names = atts.Select(a => a.Name).ToArray();
Assert.AreEqual(new [] {"survived", "pclass", "sex", "embarked"}, names);
}
}
}
38 changes: 38 additions & 0 deletions PicNetML.Tests/InstancesExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Linq;
using NUnit.Framework;
using PicNetML.Tests.TestUtils;

namespace PicNetML.Tests
{
[TestFixture] public class InstancesExtensionsTests
{
[Test] public void test_GetAttrStrings()
{
var rt = TestingHelpers.LoadSmallRuntime<TitanicDataRow>("titanic_train.csv", 0, 5);
var vals = rt.Impl.GetAttrStrings(1);
CollectionAssert.AreEqual(new [] {"3","1","3","1","3"}, vals);
}

[Test] public void test_GetAttrDoubles()
{
var rt = TestingHelpers.LoadSmallRuntime<TitanicDataRow>("titanic_train.csv", 0, 5);
var vals = rt.Impl.GetAttrDoubles(1);
CollectionAssert.AreEqual(new [] {2.0,0.0,2.0,0.0,2.0}, vals);
}

[Test] public void test_ToEnumerableAttributes()
{
var rt = TestingHelpers.LoadSmallRuntime<TitanicDataRow>("titanic_train.csv", 0, 5);
var atts = rt.Impl.ToEnumerableAttributes();
var names = atts.Select(a => a.Name).ToArray();
CollectionAssert.AreEqual(new [] {"survived", "pclass", "sex", "embarked"}, names);
}

[Test] public void test_ToEnumerable()
{
var rt = TestingHelpers.LoadSmallRuntime<TitanicDataRow>("titanic_train.csv", 0, 5);
var instances = rt.Impl.ToEnumerable();
Assert.AreEqual(5, instances.Count());
}
}
}
3 changes: 3 additions & 0 deletions PicNetML.Tests/PicNetML.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@
<Compile Include="Arff\Builder\InstanceBuilderTests.cs" />
<Compile Include="Arff\CsvLoaderTests.cs" />
<Compile Include="Asstn\BasicAssociationTests.cs" />
<Compile Include="AttributeExtensionsTests.cs" />
<Compile Include="AttrSel\BasicAttributeSelectionTests.cs" />
<Compile Include="Clss\BasicClassifierTests.cs" />
<Compile Include="Clstr\BasicClustererTests.cs" />
<Compile Include="Fltr\BasicFilteringTests.cs" />
<Compile Include="HelpersTests.cs" />
<Compile Include="IEnumerableExtensionsTests.cs" />
<Compile Include="InstanceExtensionsTests.cs" />
<Compile Include="InstancesExtensionsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RuntimeHelpers\AttributesRemoverTests.cs" />
<Compile Include="TestUtils\TestingHelpers.cs" />
Expand Down
8 changes: 4 additions & 4 deletions PicNetML.Tests/RuntimeHelpers/AttributesRemoverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ [TestFixture] public class AttributesRemoverTests
{
[Test] public void test_removing_attributes_by_index()
{
var rt = Runtime.LoadFromFile<TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv"));
var rt = TestingHelpers.LoadSmallRuntime<TitanicDataRow>("titanic_train.csv", 0, 3);
var ar = new AttributesRemover(rt);
var initial = rt.EnumerateAttributes.Select(a => a.Name).ToArray();
ar.RemoveAttributes(2, 3);
Expand All @@ -20,7 +20,7 @@ [Test] public void test_removing_attributes_by_index()

[Test] public void test_removing_attributes_by_names()
{
var rt = Runtime.LoadFromFile<TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv"));
var rt = TestingHelpers.LoadSmallRuntime<TitanicDataRow>("titanic_train.csv", 0, 3);
var ar = new AttributesRemover(rt);
var initial = rt.EnumerateAttributes.Select(a => a.Name).ToArray();
ar.RemoveAttributes("embarked", "sex");
Expand All @@ -31,7 +31,7 @@ [Test] public void test_removing_attributes_by_names()

[Test] public void test_keep_attributes_by_index()
{
var rt = Runtime.LoadFromFile<TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv"));
var rt = TestingHelpers.LoadSmallRuntime<TitanicDataRow>("titanic_train.csv", 0, 3);
var ar = new AttributesRemover(rt);
var initial = rt.EnumerateAttributes.Select(a => a.Name).ToArray();
ar.KeepAttributes(0, 2, 3);
Expand All @@ -42,7 +42,7 @@ [Test] public void test_keep_attributes_by_index()

[Test] public void test_keep_attributes_by_names()
{
var rt = Runtime.LoadFromFile<TitanicDataRow>(0, TestingHelpers.GetResourceFileName("titanic_train.csv"));
var rt = TestingHelpers.LoadSmallRuntime<TitanicDataRow>("titanic_train.csv", 0, 3);
var ar = new AttributesRemover(rt);
var initial = rt.EnumerateAttributes.Select(a => a.Name).ToArray();
ar.KeepAttributes("survived", "embarked", "sex");
Expand Down
18 changes: 14 additions & 4 deletions PicNetML.Tests/TestUtils/TestingHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
using System.IO;
using System.Linq;

namespace PicNetML.Tests {
namespace PicNetML.Tests.TestUtils {
public static class TestingHelpers {
public static string ReadResourceFile(string filename) {
return File.ReadAllText(GetResourceFileName(filename));
}

public static string GetResourceFileName(string filename) {
return @"..\..\resources\" + filename;
}

public static Runtime LoadSmallRuntime<T>(string filename, int classidx, int count) where T : new() {
var file = GetResourceFileName(filename);
using (var fs = File.OpenText(file)) {
string[] lines = Enumerable.Range(0, count + 1).
Select(i => fs.ReadLine()).
Skip(1).
ToArray();
var rows = Runtime.LoadRowsFromLines<T>(lines);
return Runtime.LoadFromRows(classidx, rows);
}
}
}
}
15 changes: 15 additions & 0 deletions PicNetML.Tests/TestUtils/TitanicDataRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,19 @@ public class TitanicDataRow {
[IgnoreFeature] public string cabin { get; set; }
[Nominal("C,Q,S")] public string embarked { get; set; }
}

public class TitanicDataRow2 {
[IgnoreFeature] public string passengerid { get; set; }
[Nominal("0,1")] public string survived { get; set; }
[Nominal("1,2,3")] public string pclass { get; set; }
public string name { get; set; }
[Nominal("male,female")] public string sex { get; set; }
public double age { get; set; }
public int sibsp { get; set; }
public int parch { get; set; }
public string ticket { get; set; }
public string fare { get; set; }
public string cabin { get; set; }
[Nominal("C,Q,S")] public string embarked { get; set; }
}
}
6 changes: 5 additions & 1 deletion PicNetML/InstancesExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using java.lang;
using weka.core;

namespace PicNetML
Expand All @@ -20,7 +21,10 @@ public static IEnumerable<string> ToStrings(this Instance source)
var num = source.numAttributes();
var arr = new string[num];
for (int i = 0; i < num; i++) {
arr[i] = source.stringValue(i);
// Nominal, String, Date
try { arr[i] = source.stringValue(i); }
// Numerics
catch (IllegalArgumentException) { arr[i] = source.value(i).ToString(); }
}
return arr;
}
Expand Down
2 changes: 1 addition & 1 deletion PicNetML/PicNetML.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>PicNetML</id>
<version>0.0.14</version>
<version>0.0.15</version>
<authors>guidot</authors>
<owners>guidot</owners>
<licenseUrl>http://www.gnu.org/licenses/gpl.html</licenseUrl>
Expand Down
Loading

0 comments on commit 4759737

Please sign in to comment.