Ejemplo n.º 1
0
        public void TestHoliday()
        {
            //use of forced is not recommended, used here for readability, see ScalarEncoder
            DateEncoder e = (DateEncoder)((DateEncoder.Builder)DateEncoder.GetBuilder()).Holiday(5).Forced(true).Build();

            int[] holiday    = new int[] { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 };
            int[] notholiday = new int[] { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
            int[] holiday2   = new int[] { 0, 0, 0, 1, 1, 1, 1, 1, 0, 0 };

            DateTime d = new DateTime(2010, 12, 25, 4, 55, 00);

            //Console.WriteLine(String.format("1:%s", Arrays.toString(e.encode(d))));
            Assert.IsTrue(holiday.SequenceEqual(e.Encode(d)));

            d = new DateTime(2008, 12, 27, 4, 55, 0);
            //Console.WriteLine(String.format("2:%s", Arrays.toString(e.encode(d))));
            Assert.IsTrue(notholiday.SequenceEqual(e.Encode(d)));

            d = new DateTime(1999, 12, 26, 8, 0, 0);
            //Console.WriteLine(String.Format("{0}", Arrays.ToString(e.Encode(d))));
            Assert.IsTrue(holiday2.SequenceEqual(e.Encode(d)));

            d = new DateTime(2011, 12, 24, 16, 0, 0);
            //Console.WriteLine(String.format("4:%s", Arrays.toString(e.encode(d))));
            Assert.IsTrue(holiday2.SequenceEqual(e.Encode(d)));
        }
Ejemplo n.º 2
0
        public void TestWeekend()
        {
            //use of forced is not recommended, used here for readability, see ScalarEncoder
            DateEncoder e = (DateEncoder)((DateEncoder.Builder)DateEncoder.GetBuilder()).CustomDays(21, new List <string>
            {
                "sat",
                "sun",
                "fri"
            }).Forced(true).Build();
            DateEncoder mon = (DateEncoder)((DateEncoder.Builder)DateEncoder.GetBuilder()).CustomDays(21, new List <string> {
                "Monday"
            })
                              .Forced(true).Build();
            DateEncoder e2 = (DateEncoder)((DateEncoder.Builder)DateEncoder.GetBuilder()).Weekend(21, 1).Forced(true).Build();

            //DateTime d = new DateTime(1988,5,29,20,0);
            DateTime d = new DateTime(1988, 5, 29, 20, 0, 0);

            Console.WriteLine("DateEncoderTest.testWeekend(): e.encode(d)  = " + Arrays.ToString(e.Encode(d)));
            Console.WriteLine("DateEncoderTest.testWeekend(): e2.encode(d) = " + Arrays.ToString(e2.Encode(d)));
            Assert.IsTrue(e.Encode(d).SequenceEqual(e2.Encode(d)));

            for (int i = 0; i < 300; i++)
            {
                DateTime curDate = d.AddDays(i + 1);
                Assert.IsTrue(e.Encode(curDate).SequenceEqual(e2.Encode(curDate)));

                //Make sure
                Tuple decoded = mon.Decode(mon.Encode(curDate), null);

                Map <String, RangeList> fieldsMap   = (Map <String, RangeList>)decoded.Get(0);
                List <String>           fieldsOrder = (List <String>)decoded.Get(1);

                Assert.IsNotNull(fieldsMap);
                Assert.IsNotNull(fieldsOrder);
                Assert.AreEqual(1, fieldsMap.Count);

                RangeList range = fieldsMap["Monday"];
                Assert.AreEqual(1, range.Count);
                Assert.AreEqual(1, ((List <MinMax>)range.Get(0)).Count);
                MinMax minmax = range.GetRange(0);
                Console.WriteLine("DateEncoderTest.testWeekend(): minmax.min() = {0} -> {1}", minmax.Min(), curDate.DayOfWeek);

                if (minmax.Min() == 1.0)
                {
                    Assert.AreEqual(1, (int)curDate.DayOfWeek);
                }
                else
                {
                    Assert.AreNotEqual(1, (int)curDate.DayOfWeek);
                }
            }
        }
Ejemplo n.º 3
0
        //[TestMethod]
        public void TestScalarPeriodicDates()
        {
            builder = (DateEncoder.Builder)DateEncoder.GetBuilder();
            de      = (DateEncoder)builder
                      .DayOfWeek(11, 24)
                      .Weekend(0, 1)
                      .TimeOfDay(11, 9.5)
                      .Build();

            dt = new DateTime(2010, 7, 3, 6, 00, 00);
            int[] tempArray = new int[de.GetWidth()];
            de.EncodeIntoArray(dt, tempArray);
        }
Ejemplo n.º 4
0
        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));
        }
Ejemplo n.º 5
0
        //[TestMethod]
        public void TestDateEncoderRanges()
        {
            // 3 bits for season, 1 bit for day of week, 3 for weekend, 5 for time of day
            // use of forced is not recommended, used here for readability.
            builder = (DateEncoder.Builder)DateEncoder.GetBuilder();

            de = (DateEncoder)builder
                 .DayOfWeek(11, 1.0)
                 .TimeOfDay(11, 2.0)
                 .Build();

            List <string> bitlist = new List <string>();

            Calendar c    = new GregorianCalendar();
            int      year = 2010;
            //for (int year = 1999; year < DateTime.Now.Year + 1; year++)
            {
                dt = new DateTime(year, 01, 01, 0, 0, 0);
                for (int day = 0; day < 7 /*c.GetDaysInYear(year)*/; day++)
                {
                    for (int hour = 0; hour < 24; hour++)
                    {
                        for (int minute = 0; minute < 60; minute += 15)
                        {
                            DateTime dt2 = dt.AddDays(day).AddHours(hour).AddMinutes(minute);
                            try
                            {
                                bits = de.Encode(dt2);
                            }
                            catch (Exception)
                            {
                                Debug.WriteLine("F****d up on date: " + dt2);
                                Assert.Fail();
                            }
                            Assert.IsNotNull(bits);
                            //Assert.AreEqual(63, bits.Length);
                            bitlist.Add(Arrays.ToString(bits));
                        }
                    }
                }
            }

            //bitlist.ForEach(Console.WriteLine);

            // All bits must be different!
            int count         = bitlist.Count;
            int distinctCount = bitlist.AsParallel().Distinct().Count();

            Assert.AreEqual(count, distinctCount);
        }
Ejemplo n.º 6
0
        private void SetUp()
        {
            // 3 bits for season, 1 bit for day of week, 3 for weekend, 5 for time of day
            // use of forced is not recommended, used here for readability.
            builder = (DateEncoder.Builder)DateEncoder.GetBuilder();

            de = (DateEncoder)builder.Season(3)
                 .DayOfWeek(1)
                 .Weekend(3)
                 .TimeOfDay(5)
                 .Build();

            //in the middle of fall, Thursday, not a weekend, afternoon - 4th Nov, 2010, 14:55
            dt = new DateTime(2010, 11, 4, 14, 55, 00);
            DateTime comparison = new DateTime(2010, 11, 4, 13, 55, 00);

            bits = de.Encode(dt);
            int[] comparisonBits = de.Encode(comparison);

            Console.WriteLine(Arrays.ToString(bits));
            Console.WriteLine(Arrays.ToString(comparisonBits));

            //
            //dt.GetMillis();

            // season is aaabbbcccddd (1 bit/month) # TODO should be <<3?
            // should be 000000000111 (centered on month 11 - Nov)
            int[] seasonExpected = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 };

            // week is MTWTFSS
            // contrary to local time documentation, Monday = 0 (for python
            //  datetime.datetime.timetuple()
            int[] dayOfWeekExpected = { 0, 0, 0, 1, 0, 0, 0 };

            // not a weekend, so it should be "False"
            int[] weekendExpected = { 1, 1, 1, 0, 0, 0 };

            // time of day has radius of 4 hours and w of 5 so each bit = 240/5 min = 48min
            // 14:55 is minute 14*60 + 55 = 895; 895/48 = bit 18.6
            // should be 30 bits total (30 * 48 minutes = 24 hours)
            int[] timeOfDayExpected = { 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 };

            expected = ArrayUtils.ConcatAll(seasonExpected, dayOfWeekExpected, weekendExpected, timeOfDayExpected);
        }
Ejemplo n.º 7
0
        public void TestScalarDayOfWeek()
        {
            builder = (DateEncoder.Builder)DateEncoder.GetBuilder();
            de      = (DateEncoder)builder
                      .DayOfWeek(11, 24)
                      .Weekend(0, 1)
                      .TimeOfDay(11, 9.5)
                      .Build();

            dt = new DateTime(2016, 2, 15, 00, 00, 00); // monday, midnight
            for (int i = 0; i < 7; i++)
            {
                var scalars   = de.GetScalars(dt.AddDays(i));
                var dayOfWeek = scalars.First(); // first one is day of week
                Assert.AreEqual(i, dayOfWeek);
            }
            for (int i = 0; i < 24; i++)
            {
                var timeScalar = de.GetScalars(dt.AddHours(i)).Last();
                Assert.AreEqual(i, timeScalar);
            }
        }
Ejemplo n.º 8
0
        /**
         * Returns the {@link Encoder} matching this field type.
         * @return
         */
        public IEncoder NewEncoder(FieldMetaType type)
        {
            switch (type)
            {
            case FieldMetaType.List:
            case FieldMetaType.String: return(SDRCategoryEncoder.GetBuilder().Build());

            case FieldMetaType.DateTime: return(DateEncoder.GetBuilder().Build());

            case FieldMetaType.Boolean: return(ScalarEncoder.GetBuilder().Build());

            case FieldMetaType.Coord: return(CoordinateEncoder.GetBuilder().Build());

            case FieldMetaType.Geo: return(GeospatialCoordinateEncoder.GetGeobuilder().Build());

            case FieldMetaType.Integer:
            case FieldMetaType.Float: return(RandomDistributedScalarEncoder.GetBuilder().Build());

            case FieldMetaType.DenseArray:
            case FieldMetaType.SparseArray: return(SDRPassThroughEncoder.GetSptBuilder().Build());

            default: return(null);
            }
        }
Ejemplo n.º 9
0
        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));
        }
Ejemplo n.º 10
0
 private void InitDe()
 {
     de = (DateEncoder)builder.Build();
 }