def test_encdec_big(self): d = LinearDiscretizer(3, 0.0, 100.0) self.assertEqual(d.num_bytes, 3) self.assertEqual(d.num_buckets, 16777216) self.assertEqual(d.max_bucket, 16777215) # encoder, valid cases self.assertEqual(d.encode( -1.0), bytearray([ 0, 0, 0])) self.assertEqual(d.encode( 0.0), bytearray([ 0, 0, 0])) self.assertEqual(d.encode( 25.0), bytearray([ 64, 0, 0])) self.assertEqual(d.encode( 50.0), bytearray([128, 0, 0])) self.assertEqual(d.encode( 75.0), bytearray([191, 255, 255])) self.assertEqual(d.encode(100.0), bytearray([255, 255, 255])) self.assertEqual(d.encode(101.0), bytearray([255, 255, 255])) # encoder, byte transitions self.assertEqual(d.encode(0.00152), bytearray([ 0, 0, 255])) self.assertEqual(d.encode(0.39062), bytearray([ 0, 255, 255])) # decoder, valid cases self.assertAlmostEqual(d.decode(bytearray([ 0, 0, 0])), 0.00000, 5) self.assertAlmostEqual(d.decode(bytearray([ 0, 0, 255])), 0.00152, 5) self.assertAlmostEqual(d.decode(bytearray([ 0, 255, 255])), 0.39062, 5) self.assertAlmostEqual(d.decode(bytearray([ 64, 0, 0])), 25.00000, 5) self.assertAlmostEqual(d.decode(bytearray([128, 0, 0])), 50.00000, 5) self.assertAlmostEqual(d.decode(bytearray([191, 255, 255])), 75.00000, 5) self.assertAlmostEqual(d.decode(bytearray([255, 255, 255])), 100.00000, 5)
def test_bucket_num(self): d = LinearDiscretizer(1, -10.0, 20.0) # to bucket number, valid cases self.assertEqual(d.val_to_bucket_num(-11.0), 0) self.assertEqual(d.val_to_bucket_num(-10.0), 0) self.assertEqual(d.val_to_bucket_num( -2.5), 64) self.assertEqual(d.val_to_bucket_num( 4.9), 127) self.assertEqual(d.val_to_bucket_num( 5.0), 128) self.assertEqual(d.val_to_bucket_num( 12.5), 191) self.assertEqual(d.val_to_bucket_num( 20.0), 255) self.assertEqual(d.val_to_bucket_num( 21.0), 255) # to bucket number, invalid cases self.assertRaises(DiscretizerException, d.val_to_bucket_num, '') self.assertRaises(DiscretizerException, d.val_to_bucket_num, 1) # from bucket number, valid cases self.assertAlmostEqual(d.bucket_num_to_val( 0), -10.0000, 4) self.assertAlmostEqual(d.bucket_num_to_val( 64), -2.4706, 4) self.assertAlmostEqual(d.bucket_num_to_val(127), 4.9412, 4) self.assertAlmostEqual(d.bucket_num_to_val(128), 5.0588, 4) self.assertAlmostEqual(d.bucket_num_to_val(191), 12.4706, 4) self.assertAlmostEqual(d.bucket_num_to_val(255), 20.0000, 4) # from bucket number, invalid cases self.assertRaises(DiscretizerException, d.bucket_num_to_val, '') self.assertRaises(DiscretizerException, d.bucket_num_to_val, 1.1) self.assertRaises(DiscretizerException, d.bucket_num_to_val, -1) self.assertRaises(DiscretizerException, d.bucket_num_to_val, 256)
def test_encdec_small(self): d = LinearDiscretizer(1, -10.0, 20.0) # encoder, valid cases self.assertEqual(d.encode(-11.0), bytearray([ 0])) self.assertEqual(d.encode(-10.0), bytearray([ 0])) self.assertEqual(d.encode( -2.5), bytearray([ 64])) self.assertEqual(d.encode( 4.9), bytearray([127])) self.assertEqual(d.encode( 5.0), bytearray([128])) self.assertEqual(d.encode( 12.5), bytearray([191])) self.assertEqual(d.encode( 20.0), bytearray([255])) self.assertEqual(d.encode( 21.0), bytearray([255])) # encoder, invalid cases self.assertRaises(DiscretizerException, d.encode, '') self.assertRaises(DiscretizerException, d.encode, 1) # decoder, valid cases self.assertAlmostEqual(d.decode(bytearray([ 0])), -10.0000, 4) self.assertAlmostEqual(d.decode(bytearray([ 64])), -2.4706, 4) self.assertAlmostEqual(d.decode(bytearray([127])), 4.9412, 4) self.assertAlmostEqual(d.decode(bytearray([128])), 5.0588, 4) self.assertAlmostEqual(d.decode(bytearray([191])), 12.4706, 4) self.assertAlmostEqual(d.decode(bytearray([255])), 20.0000, 4) # decoder, invalid cases self.assertRaises(DiscretizerException, d.decode, '') self.assertRaises(DiscretizerException, d.decode, 1.1) self.assertRaises(DiscretizerException, d.decode, bytearray()) self.assertRaises(DiscretizerException, d.decode, bytearray([0, 0]))
def test_mapper(self): d = LinearDiscretizer(1, -10.0, 20.0) # base encoder self.assertAlmostEqual(d.map_encoder(0.00), 0.00) self.assertAlmostEqual(d.map_encoder(0.25), 0.25) self.assertAlmostEqual(d.map_encoder(0.50), 0.50) self.assertAlmostEqual(d.map_encoder(0.75), 0.75) self.assertAlmostEqual(d.map_encoder(1.00), 1.00) # base decoder self.assertAlmostEqual(d.map_decoder(0.00), 0.00) self.assertAlmostEqual(d.map_decoder(0.25), 0.25) self.assertAlmostEqual(d.map_decoder(0.50), 0.50) self.assertAlmostEqual(d.map_decoder(0.75), 0.75) self.assertAlmostEqual(d.map_decoder(1.00), 1.00)