class_docstr: Unit tests for CategoryEncoder class
code: def testCategoryEncoder(self):
      categories = ["ES", "GB", "US"]
  
      # forced: is not recommended, but is used here for readability.
      # see scalar.py
      e = CategoryEncoder(w=3, categoryList=categories, forced=True)
      output = e.encode("US")
      expected = numpy.array(
          [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], dtype=defaultDtype
      )
      self.assertTrue(numpy.array_equal(output, expected))
  
      # Test reverse lookup
      decoded = e.decode(output)
      (fieldsDict, fieldNames) = decoded
      self.assertEqual(len(fieldNames), 1)
      self.assertEqual(len(fieldsDict), 1)
      self.assertEqual(fieldNames[0], fieldsDict.keys()[0])
      (ranges, desc) = fieldsDict.values()[0]
      self.assertEqual(desc, "US")
      self.assertEqual(len(ranges), 1)
      self.assertTrue(numpy.array_equal(ranges[0], [3, 3]))
  
      # Test topdown compute
      for v in categories:
          output = e.encode(v)
          topDown = e.topDownCompute(output)
          self.assertEqual(topDown.value, v)
          self.assertEqual(topDown.scalar, e.getScalars(v)[0])
  
          bucketIndices = e.getBucketIndices(v)
          topDown = e.getBucketInfo(bucketIndices)[0]
          self.assertEqual(topDown.value, v)
          self.assertEqual(topDown.scalar, e.getScalars(v)[0])
          self.assertTrue(numpy.array_equal(topDown.encoding, output))
          self.assertEqual(
              topDown.value, e.getBucketValues()[bucketIndices[0]]
          )
  
      # ---------------------
      # unknown category
      output = e.encode("NA")
      expected = numpy.array(
          [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=defaultDtype
      )
      self.assertTrue(numpy.array_equal(output, expected))
  
      # Test reverse lookup
      decoded = e.decode(output)
      (fieldsDict, fieldNames) = decoded
      self.assertEqual(len(fieldNames), 1)
      self.assertEqual(len(fieldsDict), 1)
      self.assertEqual(fieldNames[0], fieldsDict.keys()[0])
      (ranges, desc) = fieldsDict.values()[0]
      self.assertEqual(len(ranges), 1)
      self.assertTrue(numpy.array_equal(ranges[0], [0, 0]))
  
      # Test topdown compute
      topDown = e.topDownCompute(output)
      self.assertEqual(topDown.value, UNKNOWN)
      self.assertEqual(topDown.scalar, 0)
  
      # --------------------------------
      # ES
      output = e.encode("ES")
      expected = numpy.array(
          [0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0], dtype=defaultDtype
      )
      self.assertTrue(numpy.array_equal(output, expected))
  
      # MISSING VALUE
      outputForMissing = e.encode(SENTINEL_VALUE_FOR_MISSING_DATA)
      self.assertEqual(sum(outputForMissing), 0)
  
      # Test reverse lookup
      decoded = e.decode(output)
      (fieldsDict, fieldNames) = decoded
      self.assertEqual(len(fieldNames), 1)
      self.assertEqual(len(fieldsDict), 1)
      self.assertEqual(fieldNames[0], fieldsDict.keys()[0])
      (ranges, desc) = fieldsDict.values()[0]
      self.assertEqual(len(ranges), 1)
      self.assertTrue(numpy.array_equal(ranges[0], [1, 1]))
  
      # Test topdown compute
      topDown = e.topDownCompute(output)
      self.assertEqual(topDown.value, "ES")
      self.assertEqual(topDown.scalar, e.getScalars("ES")[0])
  
      # --------------------------------
      # Multiple categories
      output.fill(1)
  
      # Test reverse lookup
      decoded = e.decode(output)
      (fieldsDict, fieldNames) = decoded
      self.assertEqual(len(fieldNames), 1)
      self.assertEqual(len(fieldsDict), 1)
      self.assertEqual(fieldNames[0], fieldsDict.keys()[0])
      (ranges, desc) = fieldsDict.values()[0]
      self.assertEqual(len(ranges), 1)
      self.assertTrue(numpy.array_equal(ranges[0], [0, 3]))
  
      # -------------------------------------------------------------
      # Test with width = 1
      categories = ["cat1", "cat2", "cat3", "cat4", "cat5"]
      # forced: is not recommended, but is used here for readability.
      # see scalar.py
      e = CategoryEncoder(w=1, categoryList=categories, forced=True)
      for cat in categories:
          output = e.encode(cat)
          topDown = e.topDownCompute(output)
          self.assertEqual(topDown.value, cat)
          self.assertEqual(topDown.scalar, e.getScalars(cat)[0])
  
      # -------------------------------------------------------------
      # Test with width = 9, removing some bits end the encoded output
      categories = ["cat%d" % (x) for x in range(1, 10)]
      # forced: is not recommended, but is used here for readability.
      # see scalar.py
      e = CategoryEncoder(w=9, categoryList=categories, forced=True)
      for cat in categories:
          output = e.encode(cat)
          topDown = e.topDownCompute(output)
          self.assertEqual(topDown.value, cat)
          self.assertEqual(topDown.scalar, e.getScalars(cat)[0])
  
          # Get rid of 1 bit on the left
          outputNZs = output.nonzero()[0]
          output[outputNZs[0]] = 0
          topDown = e.topDownCompute(output)
          self.assertEqual(topDown.value, cat)
          self.assertEqual(topDown.scalar, e.getScalars(cat)[0])
  
          # Get rid of 1 bit on the right
          output[outputNZs[0]] = 1
          output[outputNZs[-1]] = 0
          topDown = e.topDownCompute(output)
          self.assertEqual(topDown.value, cat)
          self.assertEqual(topDown.scalar, e.getScalars(cat)[0])
  
          # Get rid of 4 bits on the left
          output.fill(0)
          output[outputNZs[-5:]] = 1
          topDown = e.topDownCompute(output)
          self.assertEqual(topDown.value, cat)
          self.assertEqual(topDown.scalar, e.getScalars(cat)[0])
  
          # Get rid of 4 bits on the right
          output.fill(0)
          output[outputNZs[0:5]] = 1
          topDown = e.topDownCompute(output)
          self.assertEqual(topDown.value, cat)
          self.assertEqual(topDown.scalar, e.getScalars(cat)[0])
  
      # OR together the output of 2 different categories, we should not get
      #  back the mean, but rather one or the other
      output1 = e.encode("cat1")
      output2 = e.encode("cat9")
      output = output1 + output2
      topDown = e.topDownCompute(output)
      self.assertTrue(
          topDown.scalar == e.getScalars("cat1")[0]
          or topDown.scalar == e.getScalars("cat9")[0]
      )
  
file_path: nupic-legacy/tests/unit/nupic/encoders/category_test.py
imports: import tempfile
  import unittest
  import numpy
  from nupic.data import SENTINEL_VALUE_FOR_MISSING_DATA
  from nupic.encoders.base import defaultDtype
  from nupic.encoders.category import CategoryEncoder, UNKNOWN
label_desc: Use image node_id 1 for calling the CategoryEncoderTest obj's underlying member method code with example usage: obj.testCategoryEncoder() without return types