Пример #1
0
    def testReadWrite(self):
        categories = ["ES", "GB", "US"]

        # forced: is not recommended, but is used here for readability. see
        # scalar.py
        original = CategoryEncoder(w=3, categoryList=categories, forced=True)
        output = original.encode("US")
        target = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
                             dtype=defaultDtype)
        self.assertTrue(numpy.array_equal(output, target))

        decoded = original.decode(output)

        proto1 = CategoryEncoderProto.new_message()
        original.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 = CategoryEncoderProto.read(f)

        encoder = CategoryEncoder.read(proto2)

        self.assertIsInstance(encoder, CategoryEncoder)
        self.assertEqual(encoder.verbosity, original.verbosity)
        self.assertEqual(encoder.width, original.width)
        self.assertEqual(encoder.description, original.description)
        self.assertEqual(encoder.name, original.name)
        self.assertDictEqual(encoder.categoryToIndex, original.categoryToIndex)
        self.assertDictEqual(encoder.indexToCategory, original.indexToCategory)
        self.assertTrue(numpy.array_equal(encoder.encode("US"), output))
        self.assertEqual(original.decode(encoder.encode("US")),
                         encoder.decode(original.encode("US")))
        self.assertEqual(decoded, encoder.decode(output))
Пример #2
0
  def testReadWrite(self):
    categories = ["ES", "GB", "US"]

    # forced: is not recommended, but is used here for readability. see
    # scalar.py
    original = CategoryEncoder(w=3, categoryList=categories, forced=True)
    output = original.encode("US")
    target = numpy.array([0,0,0,0,0,0,0,0,0,1,1,1], dtype=defaultDtype)
    self.assertTrue(numpy.array_equal(output, target))

    decoded = original.decode(output)

    proto1 = CategoryEncoderProto.new_message()
    original.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 = CategoryEncoderProto.read(f)

    encoder = CategoryEncoder.read(proto2)

    self.assertIsInstance(encoder, CategoryEncoder)
    self.assertEqual(encoder.verbosity, original.verbosity)
    self.assertEqual(encoder.width, original.width)
    self.assertEqual(encoder.description, original.description)
    self.assertEqual(encoder.name, original.name)
    self.assertDictEqual(encoder.categoryToIndex, original.categoryToIndex)
    self.assertDictEqual(encoder.indexToCategory, original.indexToCategory)
    self.assertTrue(numpy.array_equal(encoder.encode("US"), output))
    self.assertEqual(original.decode(encoder.encode("US")),
                     encoder.decode(original.encode("US")))
    self.assertEqual(decoded, encoder.decode(output))