public void TestCategoryEncoderCreation() { Sensor <FileInfo> sensor = Sensor <FileInfo> .Create( FileSensor.Create, SensorParams.Create( SensorParams.Keys.Path, "", ResourceLocator.Path(typeof(Resources), "rec-center-hourly-4period-cat.Csv"))); // Cast the ValueList to the more complex type (Header) HTMSensor <FileInfo> htmSensor = (HTMSensor <FileInfo>)sensor; Header meta = (Header)htmSensor.GetMetaInfo(); Assert.IsTrue(meta.GetFieldTypes().TrueForAll( l => l.Equals(FieldMetaType.DateTime) || l.Equals(FieldMetaType.Float) || l.Equals(FieldMetaType.List))); Assert.IsTrue(meta.GetFieldNames().TrueForAll( l => l.Equals("timestamp") || l.Equals("consumption") || l.Equals("type"))); Assert.IsTrue(meta.GetFlags().TrueForAll( l => l.Equals(SensorFlags.T) || l.Equals(SensorFlags.B) || l.Equals(SensorFlags.C))); // Set the parameters on the sensor. // This enables it to auto-configure itself; a step which will // be done at the Region level. Encoder <object> multiEncoder = htmSensor.GetEncoder(); Assert.IsNotNull(multiEncoder); Assert.IsTrue(multiEncoder is MultiEncoder); // Set the Local parameters on the Sensor htmSensor.InitEncoder(GetCategoryEncoderParams()); List <EncoderTuple> encoders = multiEncoder.GetEncoders(multiEncoder); Assert.AreEqual(3, encoders.Count); DateEncoder dateEnc = (DateEncoder)encoders[1].GetEncoder(); SDRCategoryEncoder catEnc = (SDRCategoryEncoder)encoders[2].GetEncoder(); Assert.AreEqual("[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0]", Arrays.ToString(catEnc.Encode("ES"))); // Now test the encoding of an input row Map <string, object> d = new Map <string, object>(); d.Add("timestamp", dateEnc.Parse("7/12/10 13:10")); d.Add("consumption", 35.3); d.Add("type", "ES"); int[] output = multiEncoder.Encode(d); int[] expected = { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0 }; Debug.WriteLine(Arrays.ToString(expected)); Debug.WriteLine(Arrays.ToString(output)); Assert.IsTrue(Arrays.AreEqual(expected, output)); }
public void TestInternalEncoderCreation() { Sensor <FileInfo> sensor = Sensor <FileInfo> .Create( FileSensor.Create, SensorParams.Create( SensorParams.Keys.Path, "", ResourceLocator.Path(typeof(Resources), "rec-center-hourly.Csv"))); // Cast the ValueList to the more complex type (Header) HTMSensor <FileInfo> htmSensor = (HTMSensor <FileInfo>)sensor; Header meta = (Header)htmSensor.GetMetaInfo(); Assert.IsTrue(meta.GetFieldTypes().TrueForAll( l => l.Equals(FieldMetaType.DateTime) || l.Equals(FieldMetaType.Float))); Assert.IsTrue(meta.GetFieldNames().TrueForAll( l => l.Equals("timestamp") || l.Equals("consumption"))); Assert.IsTrue(meta.GetFlags().TrueForAll( l => l.Equals(SensorFlags.T) || l.Equals(SensorFlags.B))); // Set the parameters on the sensor. // This enables it to auto-configure itself; a step which will // be done at the Region level. Encoder <object> multiEncoder = htmSensor.GetEncoder(); Assert.IsNotNull(multiEncoder); Assert.IsTrue(multiEncoder is MultiEncoder); // Set the Local parameters on the Sensor htmSensor.InitEncoder(GetTestEncoderParams()); List <EncoderTuple> encoders = multiEncoder.GetEncoders(multiEncoder); Assert.AreEqual(2, encoders.Count); // Test date specific encoder configuration // // All encoders in the MultiEncoder are accessed in a particular // order (the alphabetical order their corresponding fields are in), // so alphabetically "consumption" proceeds "timestamp" // so we need to ensure that the proper order is preserved (i.E. exists at index 1) DateEncoder dateEnc = (DateEncoder)encoders[1].GetEncoder(); try { dateEnc.ParseEncode("7/12/10 13:10"); dateEnc.ParseEncode("7/12/2010 13:10"); // Should fail here due to conflict with configured format dateEnc.ParseEncode("--13:10 7-12-10"); Assert.Fail(); } catch (Exception e) { Assert.IsInstanceOfType(e, typeof(FormatException)); //Assert.AreEqual("Invalid format: \"13:10 7/12/10\" is malformed at \":10 7/12/10\"", e.Message); } RandomDistributedScalarEncoder rdse = (RandomDistributedScalarEncoder)encoders[0].GetEncoder(); int[] encoding = rdse.Encode(35.3); Console.WriteLine(Arrays.ToString(encoding)); // Now test the encoding of an input row Map <string, object> d = new Map <string, object>(); d.Add("timestamp", dateEnc.Parse("7/12/10 13:10")); d.Add("consumption", 35.3); int[] output = multiEncoder.Encode(d); int[] expected = { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; Console.WriteLine(Arrays.ToString(expected)); Console.WriteLine(Arrays.ToString(output)); Assert.IsTrue(Arrays.AreEqual(expected, output)); }