def testDayOfWeek(self): """ Creating date encoder instance. """ # 1 bit for days in a week (x7 days -> 7 bits), no other fields encoded p = DateEncoderParameters() p.dayOfWeek_width = 1 p.verbose = False enc = DateEncoder(p) # In the middle of fall, Thursday, not a weekend, afternoon - 4th Nov, # 2010, 14:55 d = datetime.datetime(2010, 11, 4, 14, 55) bits = enc.encode(d) # Week is MTWTFSS, # Monday = 0 (for python datetime.datetime.timetuple()) dayOfWeekExpected = [0, 0, 0, 1, 0, 0, 0] #Thu expected = dayOfWeekExpected self.assertEqual(bits.size, 7) self.assertEqual(expected, bits.dense.tolist()) # check a day is encoded consistently during most of its hours. p = DateEncoderParameters() p.dayOfWeek_width = 40 p.verbose = False enc = DateEncoder(p) dMorn = datetime.datetime(2010, 11, 4, 8, 00) # 8 AM to dEve = datetime.datetime(2010, 11, 4, 8 + 12, 00) # 8 PM bits1 = enc.encode(dMorn) bits2 = enc.encode(dEve) assert (bits1.getOverlap(bits2) > 40 * .25) # Check the long term statistics of the encoder. p = DateEncoderParameters() p.dayOfWeek_width = 300 p.verbose = False enc = DateEncoder(p) sdr = SDR(enc.dimensions) test_period = 1000 metrics = Metrics(sdr, test_period) now = datetime.datetime.now() inc = datetime.timedelta(hours=1) for i in range(test_period): enc.encode(now, sdr) now += inc #print( metrics ) assert (metrics.sparsity.min() >= .05) assert (metrics.sparsity.max() <= .20) assert (metrics.activationFrequency.min() >= .05) assert (metrics.activationFrequency.max() <= .20)
def testDateEncoder(self): """ Creating date encoder instance. """ # 3 bits for season(4seasons), 1 bit for day of week, 1 for weekend, 5 for time of day p = DateEncoderParameters() p.season_width = 3 p.dayOfWeek_width = 1 p.weekend_width = 1 p.timeOfDay_width = 5 p.verbose = False enc = DateEncoder(p) # In the middle of fall, Thursday, not a weekend, afternoon - 4th Nov, # 2010, 14:55 d = datetime.datetime(2010, 11, 4, 14, 55) # d = datetime.datetime(2010, 11, 1, 8, 55) # DEBUG bits = enc.encode(d) # Season is aaabbbcccddd (1 bit/month) seasonExpected = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1] # Week is MTWTFSS contrary to localtime documentation, Monday = 0 (for # python datetime.datetime.timetuple() dayOfWeekExpected = [0, 0, 0, 1, 0, 0, 0] # Not a weekend, so it should be "False" weekendExpected = [1, 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) timeOfDayExpected = ([ 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 ]) expected = seasonExpected + dayOfWeekExpected + weekendExpected + timeOfDayExpected self.assertEqual(bits.size, 51) self.assertEqual(expected, bits.dense.tolist())