本文整理汇总了Python中nupic.encoders.date.DateEncoder.write方法的典型用法代码示例。如果您正苦于以下问题:Python DateEncoder.write方法的具体用法?Python DateEncoder.write怎么用?Python DateEncoder.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nupic.encoders.date.DateEncoder
的用法示例。
在下文中一共展示了DateEncoder.write方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DateEncoderTest
# 需要导入模块: from nupic.encoders.date import DateEncoder [as 别名]
# 或者: from nupic.encoders.date.DateEncoder import write [as 别名]
#.........这里部分代码省略.........
def testHoliday(self):
"""look at holiday more carefully because of the smooth transition"""
# use of forced is not recommended, used here for readability, see
# scalar.py
e = DateEncoder(holiday=5, forced=True)
holiday = numpy.array([0,0,0,0,0,1,1,1,1,1], dtype="uint8")
notholiday = numpy.array([1,1,1,1,1,0,0,0,0,0], dtype="uint8")
holiday2 = numpy.array([0,0,0,1,1,1,1,1,0,0], dtype="uint8")
d = datetime.datetime(2010, 12, 25, 4, 55)
self.assertTrue(numpy.array_equal(e.encode(d), holiday))
d = datetime.datetime(2008, 12, 27, 4, 55)
self.assertTrue(numpy.array_equal(e.encode(d), notholiday))
d = datetime.datetime(1999, 12, 26, 8, 00)
self.assertTrue(numpy.array_equal(e.encode(d), holiday2))
d = datetime.datetime(2011, 12, 24, 16, 00)
self.assertTrue(numpy.array_equal(e.encode(d), holiday2))
def testHolidayMultiple(self):
"""look at holiday more carefully because of the smooth transition"""
# use of forced is not recommended, used here for readability, see
# scalar.py
e = DateEncoder(holiday=5, forced=True, holidays=[(12, 25), (2018, 4, 1), (2017, 4, 16)])
holiday = numpy.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1], dtype="uint8")
notholiday = numpy.array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0], dtype="uint8")
d = datetime.datetime(2011, 12, 25, 4, 55)
self.assertTrue(numpy.array_equal(e.encode(d), holiday))
d = datetime.datetime(2007, 12, 2, 4, 55)
self.assertTrue(numpy.array_equal(e.encode(d), notholiday))
d = datetime.datetime(2018, 4, 1, 16, 10)
self.assertTrue(numpy.array_equal(e.encode(d), holiday))
d = datetime.datetime(2017, 4, 16, 16, 10)
self.assertTrue(numpy.array_equal(e.encode(d), holiday))
def testWeekend(self):
"""Test weekend encoder"""
# use of forced is not recommended, used here for readability, see scalar.py
e = DateEncoder(customDays=(21, ["sat", "sun", "fri"]), forced=True)
mon = DateEncoder(customDays=(21, "Monday"), forced=True)
e2 = DateEncoder(weekend=(21, 1), forced=True)
d = datetime.datetime(1988, 5, 29, 20, 00)
self.assertTrue(numpy.array_equal(e.encode(d), e2.encode(d)))
for _ in range(300):
d = d+datetime.timedelta(days=1)
self.assertTrue(numpy.array_equal(e.encode(d), e2.encode(d)))
#Make sure
if mon.decode(mon.encode(d))[0]["Monday"][0][0][0] == 1.0:
self.assertEqual(d.weekday(), 0)
else:
self.assertNotEqual(d.weekday(), 0)
@unittest.skipUnless(
capnp, "pycapnp is not installed, skipping serialization test.")
def testReadWrite(self):
originalTS = datetime.datetime(1997, 8, 29, 2, 14)
originalValue = self._e.encode(originalTS)
proto1 = DateEncoderProto.new_message()
self._e.write(proto1)
# Write the proto to a temp file and read it back into a new proto
with tempfile.TemporaryFile() as f:
proto1.write(f)
f.seek(0)
proto2 = DateEncoderProto.read(f)
encoder = DateEncoder.read(proto2)
self.assertIsInstance(encoder, DateEncoder)
self.assertEqual(encoder.width, self._e.width)
self.assertEqual(encoder.weekendOffset, self._e.weekendOffset)
self.assertEqual(encoder.timeOfDayOffset, self._e.timeOfDayOffset)
self.assertEqual(encoder.seasonOffset, self._e.seasonOffset)
self.assertEqual(encoder.dayOfWeekOffset, self._e.dayOfWeekOffset)
self.assertIsInstance(encoder.customDaysEncoder,
self._e.customDaysEncoder.__class__)
self.assertIsInstance(encoder.dayOfWeekEncoder,
self._e.dayOfWeekEncoder.__class__)
self.assertIsInstance(encoder.seasonEncoder,
self._e.seasonEncoder.__class__)
self.assertIsInstance(encoder.timeOfDayEncoder,
self._e.timeOfDayEncoder.__class__)
self.assertIsInstance(encoder.weekendEncoder,
self._e.weekendEncoder.__class__)
self.assertTrue(numpy.array_equal(self._bits, encoder.encode(self._d)))
self.assertTrue(numpy.array_equal(encoder.encode(originalTS),
originalValue))
self.assertEqual(self._e.decode(encoder.encode(self._d)),
encoder.decode(self._e.encode(self._d)))