From 7e65b970618f5e6c4304b99849d762f53e5031ec Mon Sep 17 00:00:00 2001 From: Guido Tapia Date: Wed, 11 Sep 2013 10:11:26 +1000 Subject: [PATCH] Added some needed tests to core classes --- PicNetML.Tests/HelpersTests.cs | 174 ++++++++++++++++++- PicNetML.Tests/IEnumerableExtensionsTests.cs | 81 +++++++++ PicNetML.Tests/PicNetML.Tests.csproj | 1 + PicNetML/CombinationRuntimeBuilder.cs | 9 +- PicNetML/ExtendableObj.cs | 24 ++- PicNetML/Helpers.cs | 9 +- PicNetML/IEnumerableExtensions.cs | 28 ++- PicNetML/PicNetML.csproj | 3 +- PicNetML/RuntimeHelpers/RuntimeFileLoader.cs | 29 ++-- PicNetML/Runtime_Construction.cs | 67 ------- PicNetML/Runtime_Construction_Typed.cs | 84 +++++++++ PicNetML/Runtime_Construction_Untyped.cs | 26 +++ 12 files changed, 427 insertions(+), 108 deletions(-) create mode 100644 PicNetML.Tests/IEnumerableExtensionsTests.cs delete mode 100644 PicNetML/Runtime_Construction.cs create mode 100644 PicNetML/Runtime_Construction_Typed.cs create mode 100644 PicNetML/Runtime_Construction_Untyped.cs diff --git a/PicNetML.Tests/HelpersTests.cs b/PicNetML.Tests/HelpersTests.cs index 2843c45..7eddc59 100644 --- a/PicNetML.Tests/HelpersTests.cs +++ b/PicNetML.Tests/HelpersTests.cs @@ -1,8 +1,17 @@ using System; +using System.IO; +using System.Linq; using NUnit.Framework; +using PicNetML.Arff; namespace PicNetML.Tests { - [TestFixture] public class HelpersTests { + [TestFixture] + public class HelpersTests + { + [SetUp, TearDown] public void RemoveWorkingFiles() { + Directory.GetFiles(".", "*.obj").ForEach2(File.Delete); + } + [Test] public void test_range_with_positive_step_functions() { var actual = Helpers.Range(0.1m, 0.3m, 0.1m); CollectionAssert.AreEqual(new [] {0.1m, 0.2m, 0.3m}, actual, "Actual: " + String.Join(", ", actual)); @@ -19,5 +28,166 @@ [Test] public void test_range_arg_valudation() { Assert.Throws(typeof(ArgumentOutOfRangeException), () => Helpers.Range(0.1m, 0.3m, -0.1m)); Assert.Throws(typeof(ArgumentOutOfRangeException), () => Helpers.Range(0.3m, 0.3m, -0.1m)); } - } + + [Test] public void test_serialisation_and_deserialisation() + { + var file = "test.obj"; + var o = new SerialisationObj(); + var serialised = Helpers.Serialise(o, file); + var deserialised = Helpers.Deserialise(file); + + Assert.AreEqual(o, serialised); + + Assert.AreNotEqual(o, deserialised); + Assert.AreEqual(o.p, deserialised.p); + } + + [Test] public void test_get_or_serialise_functionality() + { + var file = "test.obj"; + var o = new SerialisationObj(); + var count = 0; + Func creater = () => { + count++; + return o; + }; + var created = Helpers.GetOrSetSerialised(file, creater); + var deserialised = Helpers.GetOrSetSerialised(file, creater); + Assert.AreEqual(1, count); + Assert.AreEqual(o, created); + + Assert.AreNotEqual(o, deserialised); + Assert.AreEqual(o.p, deserialised.p); + } + + [Test] public void test_get_value_on_simple_objects() + { + var s = new SimpleObj { p = "p1" }; + Assert.AreEqual("p1", Helpers.GetValue(s, "p")); + Assert.AreEqual("p1", Helpers.GetValue(s, "p")); + } + + [Test] public void test_get_value_on_igetvalue_objects() + { + var s = new IGetValueObj(); + Assert.AreEqual("p1", Helpers.GetValue(s, "p")); + Assert.AreEqual("p1", Helpers.GetValue(s, "p")); + } + + [Test] public void test_get_value_on_extendable_objects() + { + var ex = ExtendableObj.Create(new SimpleObj { p = "p1" }); + ex.AddString("p2", "v2"); + + Assert.AreEqual("p1", Helpers.GetValue(ex, "p")); + Assert.AreEqual("p1", Helpers.GetValue(ex, "p")); + Assert.AreEqual("v2", Helpers.GetValue(ex, "p2")); + Assert.AreEqual("v2", Helpers.GetValue(ex, "p2")); + } + + [Test] public void test_set_value_on_simple_objects() + { + var s = new SimpleObj { p = "p1" }; + Helpers.SetValue(s, "p", "p2"); + + Assert.AreEqual("p2", s.p); + Assert.AreEqual("p2", Helpers.GetValue(s, "p")); + Assert.AreEqual("p2", Helpers.GetValue(s, "p")); + } + + [Test] public void test_set_value_on_extendable_objects() + { + var ex = ExtendableObj.Create(new SimpleObj { p = "p1" }); + ex.AddString("p2", "v2"); + + Helpers.SetValue(ex, "p", "p2"); + Helpers.SetValue(ex, "p2", "v3"); + + Assert.AreEqual("p2", ex.BaseObject.p); + Assert.AreEqual("p2", Helpers.GetValue(ex, "p")); + Assert.AreEqual("p2", Helpers.GetValue(ex, "p")); + + Assert.AreEqual("v3", Helpers.GetValue(ex, "p2")); + Assert.AreEqual("v3", Helpers.GetValue(ex, "p2")); + } + + [Test] public void test_rows_where_prop_is_value_functionality() + { + var rows = new [] { + new SimpleObj { p = "1" }, + new SimpleObj { p = "2" }, + new SimpleObj { p = "2" }, + new SimpleObj { p = "3" }, + }; + var result = Helpers.RowsWherePropIsValue(rows, "p", "2"); + CollectionAssert.AreEqual(new [] {rows[1], rows[2]}, result); + } + + [Test] public void test_get_props_on_simple_objects() + { + var names = Helpers.GetProps(typeof(SimpleObj)).Select(p => p.Name).ToArray(); + Assert.AreEqual(new [] {"p"}, names); + } + + [Test] public void test_get_props_on_extendable_objects_gets_from_base_type() + { + var names = Helpers.GetProps(typeof(ExtendableObj)).Select(p => p.Name).ToArray(); + Assert.AreEqual(new [] {"p"}, names); + } + + [Test] public void test_cloning_on_simple_objs() + { + var s = new SimpleObj {p ="v"}; + var cloned = Helpers.Clone(s); + Assert.AreNotEqual(s, cloned); + Assert.AreEqual(s.p, cloned.p); + } + + [Test] public void test_cloning_on_icloneables() + { + var c = new CloneableObj {p =100}; + var cloned = Helpers.Clone(c); + Assert.AreEqual(100, c.p); + Assert.AreEqual(101, cloned.p); + } + + [Test] public void test_cloning_on_extendables_with_simple_base() + { + var s = new SimpleObj {p ="v"}; + var ex = ExtendableObj.Create(s); + ex.AddString("p2", "v2"); + var cloned = Helpers.Clone(ex); + + Assert.AreNotEqual(ex, cloned); + Assert.AreEqual(ex.GetValue("p"), cloned.GetValue("p")); + Assert.AreEqual(ex.GetValue("p2"), cloned.GetValue("p2")); + } + + [Test] public void test_cloning_on_extendables_with_clonable_base() + { + var c = new CloneableObj {p =100}; + var ex = ExtendableObj.Create(c); + ex.AddString("p2", "v2"); + var cloned = Helpers.Clone(ex); + + Assert.AreNotEqual(ex, cloned); + Assert.AreEqual(100, ex.GetValue("p")); + Assert.AreEqual(101, cloned.GetValue("p")); + Assert.AreEqual(ex.GetValue("p2"), cloned.GetValue("p2")); + } + + [Serializable] class SerialisationObj { public string p = "property value"; } + class SimpleObj { public string p {get;set;} } } + class IGetValueObj : IGetValue { + public object GetValue(string property) { + if (property == "p") return "p1"; + throw new ArgumentException("property"); + } + } + class CloneableObj : ICloneable { + public int p {get;set;} + public object Clone() { + return new CloneableObj { p = p + 1 }; + } + } } \ No newline at end of file diff --git a/PicNetML.Tests/IEnumerableExtensionsTests.cs b/PicNetML.Tests/IEnumerableExtensionsTests.cs new file mode 100644 index 0000000..a2792d3 --- /dev/null +++ b/PicNetML.Tests/IEnumerableExtensionsTests.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using java.util; +using PicNetML; +using NUnit.Framework; +namespace PicNetML.Tests +{ + [TestFixture] public class IEnumerableExtensionsTests + { + [Test] public void test_GetMajority() + { + var possibilities = new [] {"a", "b", "c", "b", "c", "b"}; + Assert.AreEqual("b", possibilities.GetMajority()); + } + + [Test] public void test_Randomize() + { + var l = new [] {1, 2, 3, 4, 5}; + var rnd = l.Randomize().ToArray(); + CollectionAssert.AreNotEqual(l, rnd); + CollectionAssert.AreEqual(l, rnd.OrderBy(v => v)); + } + + [Test] public void test_RandomSample() + { + var l = new [] {1, 2, 3, 4, 5}; + var rnd = l.RandomSample(2); + var rnd2 = l.RandomSample(5).ToArray(); + + Assert.AreEqual(2, rnd.Count()); + CollectionAssert.AreNotEqual(l, rnd2); + CollectionAssert.AreEqual(l, rnd2.OrderBy(v => v)); + } + + [Test] public void test_RandomSampleWithReplacement() + { + var l = new [] {1, 2, 3, 4, 5}; + var rnd = l.RandomSampleWithReplacement(2); + var rnd2 = l.RandomSampleWithReplacement(5).ToArray(); + + Assert.AreEqual(2, rnd.Count()); + CollectionAssert.AreNotEqual(l, rnd2); + CollectionAssert.AreNotEqual(l, rnd2.OrderBy(v => v)); + } + + [Test] public void test_ToArrayList() + { + var l = new [] {1, 2, 3, 4, 5}; + var al = l.ToArrayList(); + Assert.AreEqual(typeof(ArrayList), al.GetType()); + CollectionAssert.AreEqual(l, al); + } + + [Test] public void test_ForEach2() + { + var l = new [] {1, 2, 3, 4, 5}; + var l2 = new List(); + l.ForEach2(v => l2.Add(v * 2)); + CollectionAssert.AreEqual(new [] {2, 4, 6, 8, 10}, l2); + } + + [Test] public void test_ForEach2_witj_index() + { + var l = new [] {1, 2, 3, 4, 5}; + var l2 = new List(); + l.ForEach2((v,i) => l2.Add(v + i)); + CollectionAssert.AreEqual(new [] {1, 3, 5, 7, 9}, l2); + } + + [Test] public void test_ToEnumerable() + { + var al = new ArrayList(); + al.add(1); al.add(2); al.add(3); + var l = al.ToEnumerable(); + CollectionAssert.AreEqual(new [] {1, 2, 3}, l); + } + } +} diff --git a/PicNetML.Tests/PicNetML.Tests.csproj b/PicNetML.Tests/PicNetML.Tests.csproj index a5eef67..ba7d2ed 100644 --- a/PicNetML.Tests/PicNetML.Tests.csproj +++ b/PicNetML.Tests/PicNetML.Tests.csproj @@ -99,6 +99,7 @@ + diff --git a/PicNetML/CombinationRuntimeBuilder.cs b/PicNetML/CombinationRuntimeBuilder.cs index 55874a3..8a06812 100644 --- a/PicNetML/CombinationRuntimeBuilder.cs +++ b/PicNetML/CombinationRuntimeBuilder.cs @@ -5,16 +5,17 @@ namespace PicNetML { + // TODO: Implement fully or remove. internal class CombinationRuntimeBuilder where T : new() { - private readonly int classifier; + private readonly int classidx; private readonly T[] data; private readonly string[][] props; private readonly int degrees; - public CombinationRuntimeBuilder(int classifier, T[] data, int[] indexes, int degrees) { + public CombinationRuntimeBuilder(int classidx, T[] data, int[] indexes, int degrees) { if (degrees != 3) throw new ArgumentException("Only 3 degrees currently supported.", "degrees"); - this.classifier = classifier; + this.classidx = classidx; this.data = data; this.degrees = degrees; @@ -45,7 +46,7 @@ public Runtime CreateRuntime() { return ext; }).ToArray(); Console.WriteLine("Created Extended array"); - return Runtime.LoadFromRows(classifier, extended); + return Runtime.LoadFromRows(classidx, extended); } private ICollection GetAdditionalProperties(string[] startprops) { diff --git a/PicNetML/ExtendableObj.cs b/PicNetML/ExtendableObj.cs index 3564cf7..4fec85b 100644 --- a/PicNetML/ExtendableObj.cs +++ b/PicNetML/ExtendableObj.cs @@ -32,19 +32,28 @@ internal ICollection GetExtendedPropertiesValues() { return Properties.Select(p => p.Value).ToArray(); } + public bool HasExtendedProperty(string name) { + return namevalmap.ContainsKey(name); + } + public object GetValue(string name) { return base_obj_keys.ContainsKey(name) ? Helpers.GetValue(BaseObjectObj, name) : namevalmap[name].Item2; } + public void SetValue(string name, object value) { + if (base_obj_keys.ContainsKey(name)) Helpers.SetValue(BaseObjectObj, name, value); + else namevalmap[name] = Tuple.Create(namevalmap[name].Item1, value); + } + public void AddProperty(string name, EAttributeType type, object value) { namevalmap.Add(name, Tuple.Create(type, value)); Properties.Add(new ExtendedProperty { Type = type, Name = name, Value = value }); } } - [Serializable] public class ExtendableObj : ExtendableObjBase, IExtendableObj where T : new() { + [Serializable] public class ExtendableObj : ExtendableObjBase, ICloneable, IExtendableObj where T : new() { public ExtendableObj() { throw new ApplicationException("Create ExtendableObj using ExtendableObj.Create."); } internal ExtendableObj(T t) { @@ -79,12 +88,13 @@ public ExtendableObj AddDate(string name, DateTime value) { return this; } - public object Clone() { - var cloned = new ExtendableObj { - Properties = Properties.ToArray(), - BaseObject = (T) (BaseObject is ICloneable ? - ((ICloneable) BaseObject).Clone() : BaseObject) - }; + public object Clone() { + Console.WriteLine("HERE"); + var props = Properties.ToArray(); + var baseo = BaseObject is ICloneable ? + ((ICloneable) BaseObject).Clone() : BaseObject; + var cloned = new ExtendableObj((T) baseo); + props.ForEach2(p => cloned.AddProperty(p.Name, p.Type, p.Value)); return cloned; } diff --git a/PicNetML/Helpers.cs b/PicNetML/Helpers.cs index 78907ee..58b150d 100644 --- a/PicNetML/Helpers.cs +++ b/PicNetML/Helpers.cs @@ -45,15 +45,18 @@ public static T GetOrSetSerialised(string file, Func loader) { } public static object GetValue(object o, string prop) { - if (o is IExtendableObj) o = ((IExtendableObj)o).BaseObject; return o is IGetValue ? ((IGetValue) o).GetValue(prop) : o.GetType().GetProperty(prop).GetValue(o); } public static void SetValue(object target, string prop, object value) { - if (target is IExtendableObj) target = ((IExtendableObj)target).BaseObject; - target.GetType().GetProperty(prop).SetValue(target, value); + if (target is ExtendableObjBase) { + var ex = (ExtendableObjBase) target; + ex.SetValue(prop, value); + } else { + target.GetType().GetProperty(prop).SetValue(target, value); + } } public static IEnumerable RowsWherePropIsValue(IEnumerable data, string prop, object value) { diff --git a/PicNetML/IEnumerableExtensions.cs b/PicNetML/IEnumerableExtensions.cs index 35167b1..d8cb414 100644 --- a/PicNetML/IEnumerableExtensions.cs +++ b/PicNetML/IEnumerableExtensions.cs @@ -17,12 +17,23 @@ public static IEnumerable Randomize(this IEnumerable data) return data.OrderBy(d => Runtime.Random).ToList(); } - public static IEnumerable RandomSample(this IEnumerable data, int size, int seed = 0) + public static IEnumerable RandomSample(this IEnumerable data, int size) { + return RandomSampleImpl(data, size, false); + } + + public static IEnumerable RandomSampleWithReplacement(this IEnumerable data, int size) { + return RandomSampleImpl(data, size, true); + } + + private static IEnumerable RandomSampleImpl(this IEnumerable data, int size, bool wreplacement) { var lst = data.ToList(); - if (size <= 0 || size > lst.Count) throw new ArgumentException("size"); - if (size == lst.Count) return lst; - var odds = size / (double) lst.Count; + var lcount = lst.Count; + if (size <= 0) throw new ArgumentException("size"); + if (!wreplacement && size > lcount) throw new ArgumentException("size"); + if (!wreplacement && size == lcount) return lst.Randomize(); + + var odds = Math.Min(0.5, size / (double) lcount); var sample = new List(); var source = lst.ToList(); var idx = 0; @@ -33,7 +44,7 @@ public static IEnumerable RandomSample(this IEnumerable data, int size, var modded = idx % source.Count; var val = source[modded]; sample.Add(val); - source.RemoveAt(modded); + if (!wreplacement) source.RemoveAt(modded); if (sample.Count == size) return sample; } idx++; @@ -58,11 +69,12 @@ public static void ForEach2(this IEnumerable source, Action action foreach (var e in source) { action(e, idx++); } } - public static IEnumerable ToEnumerable(this Enumeration source) + public static IEnumerable ToEnumerable(this Collection source) { var lst = new List(); - while (source.hasMoreElements()) { - lst.Add((T) source.nextElement()); + var iter = source.iterator(); + while (iter.hasNext()) { + lst.Add((T) iter.next()); } return lst; } diff --git a/PicNetML/PicNetML.csproj b/PicNetML/PicNetML.csproj index f7ffc1d..5e4f332 100644 --- a/PicNetML/PicNetML.csproj +++ b/PicNetML/PicNetML.csproj @@ -135,6 +135,7 @@ + @@ -142,7 +143,7 @@ - + diff --git a/PicNetML/RuntimeHelpers/RuntimeFileLoader.cs b/PicNetML/RuntimeHelpers/RuntimeFileLoader.cs index 55c2be4..7b636f0 100644 --- a/PicNetML/RuntimeHelpers/RuntimeFileLoader.cs +++ b/PicNetML/RuntimeHelpers/RuntimeFileLoader.cs @@ -9,45 +9,42 @@ namespace PicNetML.RuntimeHelpers { internal class RuntimeFileLoader { - private readonly int classifier; private readonly string file; - private readonly int trainingsize; private readonly Func preprocessor; - public RuntimeFileLoader(int classifier, string file, int trainingsize, Func preprocessor) { + public RuntimeFileLoader(string file, Func preprocessor) { if (System.String.IsNullOrWhiteSpace(file)) { throw new ArgumentNullException("file"); } - this.classifier = classifier; this.file = file; - this.trainingsize = trainingsize; this.preprocessor = preprocessor; } - internal Instances Load() where T : new () { + internal Instances Load(int classidx) where T : new () { var instances = file.EndsWith(".arff") ? - LoadRuntimFromArffFile() : + LoadRuntimFromArffFile(classidx) : file.EndsWith(".xrff") || file.EndsWith(".xrff.gz") ? - LoadRuntimFromXrffFile() : - LoadRuntimeFromNonArffFile(); + LoadRuntimFromXrffFile(classidx) : + LoadRuntimeFromNonArffFile(classidx); if (instances.numInstances() <= 0) { throw new IllegalStateException("Could not load any instances from the file provided"); } return instances; } - private Instances LoadRuntimFromXrffFile() { return LoadRuntimFromInternalFile(new XRFFLoader()); } - private Instances LoadRuntimFromArffFile() { return LoadRuntimFromInternalFile(new ArffLoader()); } - private Instances LoadRuntimFromInternalFile(AbstractFileLoader loader) { + private Instances LoadRuntimFromXrffFile(int classidx) { return LoadRuntimFromInternalFile(new XRFFLoader(), classidx); } + private Instances LoadRuntimFromArffFile(int classidx) { return LoadRuntimFromInternalFile(new ArffLoader(), classidx); } + private Instances LoadRuntimFromInternalFile(AbstractFileLoader loader, int classidx) { + if (preprocessor != null) throw new ApplicationException ("RuntimeFileLoader.preprocessor can only be used when loading non arff/xrff files."); loader.setFile(new java.io.File(file)); var instances = loader.getDataSet(); - instances.setClassIndex(classifier); + instances.setClassIndex(classidx); return instances; } - private Instances LoadRuntimeFromNonArffFile() where T : new () { + private Instances LoadRuntimeFromNonArffFile(int classidx) where T : new () { var rows = LoadRows(); - var instances = new InstancesBuilder(rows, classifier, trainingsize).Build(); - instances.setClassIndex(classifier); + var instances = new InstancesBuilder(rows, classidx).Build(); + instances.setClassIndex(classidx); return instances; } diff --git a/PicNetML/Runtime_Construction.cs b/PicNetML/Runtime_Construction.cs deleted file mode 100644 index 8a75d81..0000000 --- a/PicNetML/Runtime_Construction.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using PicNetML.Arff; -using PicNetML.Arff.Builder; -using PicNetML.RuntimeHelpers; -using weka.core; - -namespace PicNetML -{ - public partial class Runtime - { - public static Runtime LoadFromFile(int classifier, string file, int trainingsize = 0, Func preprocessor = null) where T : new() { - var loader = new RuntimeFileLoader(classifier, file, trainingsize, preprocessor); - return new Runtime(loader.Load()); - } - - public static Runtime LoadFromRows(int classifier, IEnumerable lines, int trainingsize = 0) where T : new() { - var instances = new InstancesBuilder(lines, classifier, trainingsize).Build(); - instances.setClassIndex(classifier); - return new Runtime(instances); - } - - public static Runtime FromInstances(IEnumerable instances) { - var all = instances.ToArray(); - if (!all.Any()) throw new ArgumentNullException("instances"); - var template = all.First(); - var impl = new Instances("frominstances", template.EnumerateAttributes.ToArrayList(), all.Length); - Array.ForEach(all, i => impl.add(i.Impl)); - return new Runtime(impl); - } - - public static IEnumerable LoadRowsFromFile(string file, int trainingsize = 0, Func preprocessor = null) where T : new() - { - if (String.IsNullOrWhiteSpace(file)) throw new ArgumentNullException("file"); - return new RuntimeFileLoader(0, file, trainingsize, preprocessor).LoadRows(); - } - - public static IEnumerable LoadRowsFromLines(ICollection lines) where T : new() - { - if (!lines.Any()) throw new ArgumentNullException("lines"); - return new CsvLoader().LoadLines(lines); - } - - public static Runtime CreateWithArgCombinations(int classifier, T[] data, int[] indexes, int degrees) where T : new() { - return new CombinationRuntimeBuilder(classifier, data, indexes, degrees).CreateRuntime(); - } - - public Runtime Clone() { - return new Runtime(new Instances(Impl)); - } - - public static PmlInstance BuildInstance(int classifier, T o) where T : new() { - if (o == null) throw new ArgumentNullException("o"); - var instances = new InstancesBuilder(new [] {o}, classifier).Build(); - return new PmlInstance(instances.instance(0)); - - } - - internal PmlInstance BuildInstance(T o) where T : new() { - if (o == null) throw new ArgumentNullException("o"); - var instances = new InstancesBuilder(new [] {o}, ClassIndex).Build(); - return new PmlInstance(instances.instance(0)); - - } - } -} \ No newline at end of file diff --git a/PicNetML/Runtime_Construction_Typed.cs b/PicNetML/Runtime_Construction_Typed.cs new file mode 100644 index 0000000..56c686a --- /dev/null +++ b/PicNetML/Runtime_Construction_Typed.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using PicNetML.Arff; +using PicNetML.Arff.Builder; +using PicNetML.RuntimeHelpers; + +namespace PicNetML +{ + public partial class Runtime + { + public static Runtime LoadFromFile(string classifier, string file, Func preprocessor = null) where T : new() { + return LoadFromFile(GetClassIdx(classifier), file, preprocessor); + } + + public static Runtime LoadFromRows(string classifier, IEnumerable lines, int trainingsize = 0) where T : new() { + return LoadFromRows(GetClassIdx(classifier), lines, trainingsize); + } + + public static Runtime CreateWithArgCombinations(string classifier, T[] data, int[] indexes, int degrees) where T : new() { + return CreateWithArgCombinations(GetClassIdx(classifier), data, indexes, degrees); + } + + public static PmlInstance BuildInstance(string classifier, T o) where T : new() { + return BuildInstance(GetClassIdx(classifier), o); + } + + private static int GetClassIdx(string classifier) { + var unignored = Helpers.GetProps(typeof(T)). + Where(p => !InternalHelpers.IsAtt(p)). + Select(p => p.Name). + ToArray(); + + var idx = Array.IndexOf(unignored, classifier); + if (idx < 0) throw new ArgumentException("classifier: " + classifier + " could not be found"); + return idx; + } + + public static Runtime LoadFromFile(int classidx, string file, Func preprocessor = null) where T : new() { + if (classidx < 0) throw new ArgumentOutOfRangeException("classidx: " + classidx + " is not valid"); + var loader = new RuntimeFileLoader(file, preprocessor); + return new Runtime(loader.Load(classidx)); + } + + public static Runtime LoadFromRows(int classidx, IEnumerable lines, int trainingsize = 0) where T : new() { + if (classidx < 0) throw new ArgumentOutOfRangeException("classidx: " + classidx + " is not valid"); + var instances = new InstancesBuilder(lines, classidx, trainingsize).Build(); + instances.setClassIndex(classidx); + return new Runtime(instances); + } + + public static Runtime CreateWithArgCombinations(int classidx, T[] data, int[] indexes, int degrees) where T : new() { + if (classidx < 0) throw new ArgumentOutOfRangeException("classidx: " + classidx + " is not valid"); + return new CombinationRuntimeBuilder(classidx, data, indexes, degrees).CreateRuntime(); + } + + public static PmlInstance BuildInstance(int classidx, T o) where T : new() { + if (classidx < 0) throw new ArgumentOutOfRangeException("classidx: " + classidx + " is not valid"); + if (o == null) throw new ArgumentNullException("o"); + var instances = new InstancesBuilder(new [] {o}, classidx).Build(); + return new PmlInstance(instances.instance(0)); + + } + + public static IEnumerable LoadRowsFromFile(string file, Func preprocessor = null) where T : new() + { + if (String.IsNullOrWhiteSpace(file)) throw new ArgumentNullException("file"); + return new RuntimeFileLoader(file, preprocessor).LoadRows(); + } + + public static IEnumerable LoadRowsFromLines(ICollection lines) where T : new() + { + if (!lines.Any()) throw new ArgumentNullException("lines"); + return new CsvLoader().LoadLines(lines); + } + + internal PmlInstance BuildInstance(T o) where T : new() { + if (o == null) throw new ArgumentNullException("o"); + var instances = new InstancesBuilder(new [] {o}, ClassIndex).Build(); + return new PmlInstance(instances.instance(0)); + + } + } +} \ No newline at end of file diff --git a/PicNetML/Runtime_Construction_Untyped.cs b/PicNetML/Runtime_Construction_Untyped.cs new file mode 100644 index 0000000..339f6de --- /dev/null +++ b/PicNetML/Runtime_Construction_Untyped.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using PicNetML.Arff; +using PicNetML.Arff.Builder; +using PicNetML.RuntimeHelpers; +using weka.core; + +namespace PicNetML +{ + public partial class Runtime + { + public static Runtime FromInstances(IEnumerable instances) { + var all = instances.ToArray(); + if (!all.Any()) throw new ArgumentNullException("instances"); + var template = all.First(); + var impl = new Instances("frominstances", template.EnumerateAttributes.ToArrayList(), all.Length); + Array.ForEach(all, i => impl.add(i.Impl)); + return new Runtime(impl); + } + + public Runtime Clone() { + return new Runtime(new Instances(Impl)); + } + } +} \ No newline at end of file