コード例 #1
0
    def testCapnpSerialization(self):
        """Test capnp serialization of NuPIC randomness."""

        # Simple test: make sure that dumping / loading works...
        r = Random(99)

        builderProto = RandomProto.new_message()
        r.write(builderProto)
        readerProto = RandomProto.from_bytes(builderProto.to_bytes())

        test1 = [r.getUInt32() for _ in range(10)]
        r = Random(1)
        r.read(readerProto)
        self.assertEqual(r.getSeed(), 99)
        test2 = [r.getUInt32() for _ in range(10)]

        self.assertEqual(
            test1, test2,
            "Simple NuPIC random capnp serialization check failed.")

        # A little tricker: dump / load _after_ some numbers have been generated
        # (in the first test).  Things should still work...
        # ...the idea of this test is to make sure that the pickle code isn't just
        # saving the initial seed...
        builderProto = RandomProto.new_message()
        r.write(builderProto)
        readerProto = RandomProto.from_bytes(builderProto.to_bytes())

        test3 = [r.getUInt32() for _ in range(10)]
        r = Random()
        r.read(readerProto)
        self.assertEqual(r.getSeed(), 99)
        test4 = [r.getUInt32() for _ in range(10)]

        self.assertEqual(
            test3, test4,
            "NuPIC random capnp serialization check didn't work for saving later "
            "state.")

        self.assertNotEqual(
            test1, test3,
            "NuPIC random serialization test gave the same result twice?!?")
コード例 #2
0
  def testCapnpSerialization(self):
    """Test capnp serialization of NuPIC randomness."""

    # Simple test: make sure that dumping / loading works...
    r = Random(99)

    builderProto = RandomProto.new_message()
    r.write(builderProto)
    readerProto = RandomProto.from_bytes(builderProto.to_bytes())

    test1 = [r.getUInt32() for _ in xrange(10)]
    r = Random(1);
    r.read(readerProto)
    self.assertEqual(r.getSeed(), 99)
    test2 = [r.getUInt32() for _ in xrange(10)]

    self.assertEqual(test1, test2,
                     "Simple NuPIC random capnp serialization check failed.")

    # A little tricker: dump / load _after_ some numbers have been generated
    # (in the first test).  Things should still work...
    # ...the idea of this test is to make sure that the pickle code isn't just
    # saving the initial seed...
    builderProto = RandomProto.new_message()
    r.write(builderProto)
    readerProto = RandomProto.from_bytes(builderProto.to_bytes())

    test3 = [r.getUInt32() for _ in xrange(10)]
    r = Random();
    r.read(readerProto)
    self.assertEqual(r.getSeed(), 99)
    test4 = [r.getUInt32() for _ in xrange(10)]

    self.assertEqual(
      test3, test4,
      "NuPIC random capnp serialization check didn't work for saving later "
      "state.")

    self.assertNotEqual(
      test1, test3,
      "NuPIC random serialization test gave the same result twice?!?")
コード例 #3
0
    def testSerialization(self):
        """Test serialization of NuPIC randomness."""

        path = "RandomSerialization.stream"

        # Simple test: make sure that dumping / loading works...
        r = Random(99)

        r.saveToFile(path)

        test1 = [r.getUInt32() for _ in range(10)]
        r = Random(1)
        r.loadFromFile(path)
        self.assertEqual(r.getSeed(), 99)
        test2 = [r.getUInt32() for _ in range(10)]

        self.assertEqual(test1, test2,
                         "Simple NuPIC random serialization check failed.")

        # A little tricker: dump / load _after_ some numbers have been generated
        # (in the first test).  Things should still work...
        # ...the idea of this test is to make sure that the pickle code isn't just
        # saving the initial seed...
        r.saveToFile(path)

        test3 = [r.getUInt32() for _ in range(10)]
        r = Random()
        r.loadFromFile(path)
        self.assertEqual(r.getSeed(), 99)
        test4 = [r.getUInt32() for _ in range(10)]

        self.assertEqual(
            test3, test4,
            "NuPIC random serialization check didn't work for saving later state."
        )

        self.assertNotEqual(
            test1, test3,
            "NuPIC random serialization test gave the same result twice?!?")
コード例 #4
0
  def testSerialization(self):
    """Test serialization of NuPIC randomness."""
	
    path = "RandomSerialization.stream"

    # Simple test: make sure that dumping / loading works...
    r = Random(99)

    r.saveToFile(path)

    test1 = [r.getUInt32() for _ in range(10)]
    r = Random(1);
    r.loadFromFile(path)
    self.assertEqual(r.getSeed(), 99)
    test2 = [r.getUInt32() for _ in range(10)]

    self.assertEqual(test1, test2,
				"Simple NuPIC random serialization check failed.")

    # A little tricker: dump / load _after_ some numbers have been generated
    # (in the first test).  Things should still work...
    # ...the idea of this test is to make sure that the pickle code isn't just
    # saving the initial seed...
    r.saveToFile(path)

    test3 = [r.getUInt32() for _ in range(10)]
    r = Random();
    r.loadFromFile(path)
    self.assertEqual(r.getSeed(), 99)
    test4 = [r.getUInt32() for _ in range(10)]

    self.assertEqual(
        test3, test4,
        "NuPIC random serialization check didn't work for saving later state.")

    self.assertNotEqual(
        test1, test3,
        "NuPIC random serialization test gave the same result twice?!?")
コード例 #5
0
class SerializationTestPyRegion(PyRegion):
    """Custom python region for testing serialization/deserialization of network
  containing a python region that contains a nupic.bindings-based Random
  instance.
  """
    def __init__(self, dataWidth, randomSeed):
        if dataWidth <= 0:
            raise ValueError("Parameter dataWidth must be > 0")

        # Arbitrary value that's compatible with UInt32 in the proto schema
        # for testing serialization of python-native property
        self._dataWidth = dataWidth

        # For testing serialization of object implemented in the extension
        self._rand = Random(randomSeed)

    @property
    def dataWidth(self):
        return self._dataWidth

    @property
    def randomSeed(self):
        return self._rand.getSeed()

    def initialize(self, dims=None, splitterMaps=None):
        pass

    def compute(self, inputs, outputs):
        """
    Run one iteration of SerializationTestPyRegion's compute
    """
        outputs["out"][:] = inputs["in"]

    @classmethod
    def getSpec(cls):
        """Return the Spec for SerializationTestPyRegion.
    """
        spec = {
            "description": SerializationTestPyRegion.__doc__,
            "singleNodeOnly": True,
            "inputs": {
                "in": {
                    "description": "The input vector.",
                    "dataType": "Real32",
                    "count": 0,
                    "required": True,
                    "regionLevel": False,
                    "isDefaultInput": True,
                    "requireSplitterMap": False
                },
            },
            "outputs": {
                "out": {
                    "description": "A copy of the input vector.",
                    "dataType": "Real32",
                    "count": 0,
                    "regionLevel": True,
                    "isDefaultOutput": True
                },
            },
            "parameters": {
                "dataWidth": {
                    "description": "Size of inputs",
                    "accessMode": "Read",
                    "dataType": "UInt32",
                    "count": 1,
                    "constraints": ""
                },
                "randomSeed": {
                    "description": "Seed for constructing the Random instance",
                    "accessMode": "Read",
                    "dataType": "UInt32",
                    "count": 1,
                    "constraints": ""
                },
            },
        }

        return spec

    def getOutputElementCount(self, name):
        if name == "out":
            return self._dataWidth
        else:
            raise Exception("Unrecognized output: " + name)
コード例 #6
0
 def testConstructor(self):
     r = Random(42)
     self.assertEqual(r.getSeed(), 42)
コード例 #7
0
class SerializationTestPyRegion(PyRegion):
  """Custom python region for testing serialization/deserialization of network
  containing a python region that contains a nupic.bindings-based Random
  instance.
  """


  def __init__(self, dataWidth, randomSeed):
    if dataWidth <= 0:
      raise ValueError("Parameter dataWidth must be > 0")

    # Arbitrary value that's compatible with UInt32 in the proto schema
    # for testing serialization of python-native property
    self._dataWidth = dataWidth

    # For testing serialization of object implemented in the extension
    self._rand = Random(randomSeed)


  @property
  def dataWidth(self):
    return self._dataWidth


  @property
  def randomSeed(self):
    return self._rand.getSeed()


  @staticmethod
  def getSchema():
    """Return the pycapnp proto type that the class uses for serialization.

    This is used to convert the proto into the proper type before passing it
    into the read or write method of the subclass.
    """
    return SerializationTestPyRegionProto


  def writeToProto(self, proto):
    """Write state to proto object.

    The type of proto is determined by getSchema().
    """
    proto.dataWidth = self._dataWidth
    self._rand.write(proto.random)


  @classmethod
  def readFromProto(cls, proto):
    """Read state from proto object.

    The type of proto is determined by getSchema().

    :returns: Instance of SerializationTestPyRegion initialized from proto
    """
    obj = object.__new__(cls)
    obj._dataWidth = proto.dataWidth
    obj._rand = Random()
    obj._rand.read(proto.random)

    return obj


  def initialize(self, dims=None, splitterMaps=None):
    pass


  def compute(self, inputs, outputs):
    """
    Run one iteration of SerializationTestPyRegion's compute
    """
    outputs["out"][:] = inputs["in"]


  @classmethod
  def getSpec(cls):
    """Return the Spec for SerializationTestPyRegion.
    """
    spec = {
        "description":SerializationTestPyRegion.__doc__,
        "singleNodeOnly":True,
        "inputs":{
          "in":{
            "description":"The input vector.",
            "dataType":"Real32",
            "count":0,
            "required":True,
            "regionLevel":False,
            "isDefaultInput":True,
            "requireSplitterMap":False},
        },
        "outputs":{
          "out":{
            "description":"A copy of the input vector.",
            "dataType":"Real32",
            "count":0,
            "regionLevel":True,
            "isDefaultOutput":True},
        },

        "parameters":{
          "dataWidth":{
            "description":"Size of inputs",
            "accessMode":"Read",
            "dataType":"UInt32",
            "count":1,
            "constraints":""},
          "randomSeed":{
            "description":"Seed for constructing the Random instance",
            "accessMode":"Read",
            "dataType":"UInt32",
            "count":1,
            "constraints":""},
        },

    }

    return spec


  def getOutputElementCount(self, name):
    if name == "out":
      return self._dataWidth
    else:
      raise Exception("Unrecognized output: " + name)
コード例 #8
0
class SerializationTestPyRegion(PyRegion):
  """Custom python region for testing serialization/deserialization of network
  containing a python region that contains a nupic.bindings-based Random
  instance.
  """


  def __init__(self, dataWidth, randomSeed):
    if dataWidth <= 0:
      raise ValueError("Parameter dataWidth must be > 0")

    # Arbitrary value that's compatible with UInt32 in the proto schema
    # for testing serialization of python-native property
    self._dataWidth = dataWidth

    # For testing serialization of object implemented in the extension
    self._rand = Random(randomSeed)


  @property
  def dataWidth(self):
    return self._dataWidth


  @property
  def randomSeed(self):
    return self._rand.getSeed()


  @staticmethod
  def getProtoType():
    """Return the pycapnp proto type that the class uses for serialization.

    This is used to convert the proto into the proper type before passing it
    into the read or write method of the subclass.
    """
    return SerializationTestPyRegionProto


  def writeToProto(self, proto):
    """Write state to proto object.

    The type of proto is determined by getProtoType().
    """
    proto.dataWidth = self._dataWidth
    self._rand.write(proto.random)


  @classmethod
  def readFromProto(cls, proto):
    """Read state from proto object.

    The type of proto is determined by getProtoType().

    :returns: Instance of SerializationTestPyRegion initialized from proto
    """
    obj = object.__new__(cls)
    obj._dataWidth = proto.dataWidth
    obj._rand = Random()
    obj._rand.read(proto.random)

    return obj


  def initialize(self, dims=None, splitterMaps=None):
    pass


  def compute(self, inputs, outputs):
    """
    Run one iteration of SerializationTestPyRegion's compute
    """
    outputs["out"][:] = inputs["in"]


  @classmethod
  def getSpec(cls):
    """Return the Spec for SerializationTestPyRegion.
    """
    spec = {
        "description":SerializationTestPyRegion.__doc__,
        "singleNodeOnly":True,
        "inputs":{
          "in":{
            "description":"The input vector.",
            "dataType":"Real32",
            "count":0,
            "required":True,
            "regionLevel":False,
            "isDefaultInput":True,
            "requireSplitterMap":False},
        },
        "outputs":{
          "out":{
            "description":"A copy of the input vector.",
            "dataType":"Real32",
            "count":0,
            "regionLevel":True,
            "isDefaultOutput":True},
        },

        "parameters":{
          "dataWidth":{
            "description":"Size of inputs",
            "accessMode":"Read",
            "dataType":"UInt32",
            "count":1,
            "constraints":""},
          "randomSeed":{
            "description":"Seed for constructing the Random instance",
            "accessMode":"Read",
            "dataType":"UInt32",
            "count":1,
            "constraints":""},
        },

    }

    return spec


  def getOutputElementCount(self, name):
    if name == "out":
      return self._dataWidth
    else:
      raise Exception("Unrecognized output: " + name)