def testSerializationWithPyRegion(self): """Test (de)serialization of network containing a python region""" engine.Network.registerPyRegion(__name__, SerializationTestPyRegion.__name__) try: srcNet = engine.Network() srcNet.addRegion( SerializationTestPyRegion.__name__, "py." + SerializationTestPyRegion.__name__, json.dumps({ "dataWidth": 128, "randomSeed": 99, })) # Serialize srcNet.saveToFile("SerializationTest.stream") # Deserialize destNet = engine.Network() destNet.loadFromFile("SerializationTest.stream") destRegion = destNet.getRegions().getByName( SerializationTestPyRegion.__name__) self.assertEqual(destRegion.getParameterUInt32("dataWidth"), 128) self.assertEqual(destRegion.getParameterUInt32("randomSeed"), 99) finally: engine.Network.unregisterPyRegion( SerializationTestPyRegion.__name__)
def testSimpleTwoRegionNetworkIntrospection(self): # Create Network instance network = engine.Network() # Add two TestNode regions to network network.addRegion("region1", "TestNode", "") network.addRegion("region2", "TestNode", "") # Set dimensions on first region region1 = network.getRegions().getByName("region1") region1.setDimensions(engine.Dimensions([1, 1])) # Link region1 and region2 network.link("region1", "region2", "UniformLink", "") # Initialize network network.initialize() for linkName, link in network.getLinks(): # Compare Link API to what we know about the network self.assertEqual(link.toString(), linkName) self.assertEqual(link.getDestRegionName(), "region2") self.assertEqual(link.getSrcRegionName(), "region1") self.assertEqual(link.getLinkType(), "UniformLink") self.assertEqual(link.getDestInputName(), "bottomUpIn") self.assertEqual(link.getSrcOutputName(), "bottomUpOut") break else: self.fail("Unable to iterate network links.")
def testCapnpSerializationWithPyRegion(self): """Test capnp (de)serialization of network containing a python region""" engine.Network.registerPyRegion(__name__, SerializationTestPyRegion.__name__) try: srcNet = engine.Network() srcNet.addRegion( SerializationTestPyRegion.__name__, "py." + SerializationTestPyRegion.__name__, json.dumps({ "dataWidth": 128, "randomSeed": 99, })) # Serialize builderProto = NetworkProto.new_message() srcNet.write(builderProto) # Construct NetworkProto reader from populated builder readerProto = NetworkProto.from_bytes(builderProto.to_bytes()) # Deserialize destNet = engine.Network.read(readerProto) destRegion = destNet.getRegions().getByName( SerializationTestPyRegion.__name__) self.assertEqual(destRegion.getParameterUInt32("dataWidth"), 128) self.assertEqual(destRegion.getParameterUInt32("randomSeed"), 99) finally: engine.Network.unregisterPyRegion( SerializationTestPyRegion.__name__)
def createNetwork(fromRegion, toRegion): """Create test network""" network = engine.Network() config = str({"maxActive": MAX_ACTIVE, "outputWidth": OUTPUT_WIDTH}) network.addRegion("from", fromRegion, config) network.addRegion("to", toRegion, config) network.link("from", "to", "UniformLink", "") return network
def createSimpleNetwork(region1, region2): """Create test network""" network = engine.Network() config = str({"maxActive": MAX_ACTIVE, "outputWidth": OUTPUT_WIDTH}) network.addRegion("region1", region1, config) network.addRegion("region2", region2, config) network.link("region1", "region2", "UniformLink", "") return network
def _runTest(): net = engine.Network() net.addRegion(SerializationTestPyRegion.__name__, "py." + SerializationTestPyRegion.__name__, json.dumps({ "dataWidth": 128, "randomSeed": 99, })) # Measure serialization startSerializationTime = time.time() for i in xrange(_SERIALIZATION_LOOPS): # NOTE pycapnp's builder.from_dict (used in nupic.bindings) leaks # memory if called on the same builder more than once, so we construct a # fresh builder here builderProto = NetworkProto.new_message() net.write(builderProto) elapsedSerializationTime = time.time() - startSerializationTime builderBytes = builderProto.to_bytes() # Measure deserialization startDeserializationTime = time.time() deserializationCount = 0 while deserializationCount < _DESERIALIZATION_LOOPS: # NOTE: periodicaly create a new reader to avoid "Exceeded message traversal # limit" error readerProto = NetworkProto.from_bytes( builderBytes, traversal_limit_in_words=_TRAVERSAL_LIMIT_IN_WORDS, nesting_limit=_NESTING_LIMIT) numReads = min(_DESERIALIZATION_LOOPS - deserializationCount, _MAX_DESERIALIZATION_LOOPS_PER_READER) for _ in xrange(numReads): engine.Network.read(readerProto) deserializationCount += numReads elapsedDeserializationTime = time.time() - startDeserializationTime # Print report print _SERIALIZATION_LOOPS, "Serialization loops in", \ elapsedSerializationTime, "seconds." print "\t", elapsedSerializationTime / _SERIALIZATION_LOOPS, "seconds per loop." print deserializationCount, "Deserialization loops in", \ elapsedDeserializationTime, "seconds." print "\t", elapsedDeserializationTime / deserializationCount, "seconds per loop."
def _runTest(): net = engine.Network() net.addRegion(SerializationTestPyRegion.__name__, "py." + SerializationTestPyRegion.__name__, json.dumps({ "dataWidth": 128, "randomSeed": 99, })) # Measure serialization startSerializationTime = time.time() # serialize 100000 times to a file. for i in range(_SERIALIZATION_LOOPS): net.saveToFile("SerializationTest.stream") elapsedSerializationTime = time.time() - startSerializationTime # Measure deserialization startDeserializationTime = time.time() deserializationCount = 0 for i in range(_DESERIALIZATION_LOOPS): net = engine.Network() net.loadFromFile("SerializationTest.stream") elapsedDeserializationTime = time.time() - startDeserializationTime # Print report print(_SERIALIZATION_LOOPS, "Serialization loops in", \ elapsedSerializationTime, "seconds.") print("\t", elapsedSerializationTime / _SERIALIZATION_LOOPS, "seconds per loop.") print(deserializationCount, "Deserialization loops in", \ elapsedDeserializationTime, "seconds.") print("\t", elapsedDeserializationTime / deserializationCount, "seconds per loop.")
def testNetworkLinkTypeValidation(self): """ This tests whether the links source and destination dtypes match """ network = engine.Network() r_from = network.addRegion("from", "py.LinkRegion", "") r_to = network.addRegion("to", "py.LinkRegion", "") cnt = r_from.getOutputElementCount("UInt32") self.assertEqual(1, cnt) # Check for valid links network.link("from", "to", "UniformLink", "", "UInt32", "UInt32") network.link("from", "to", "UniformLink", "", "Real32", "Real32") network.link("from", "to", "UniformLink", "", "Real32", "UInt32") network.link("from", "to", "UniformLink", "", "UInt32", "Real32")
def createDelayedNetwork(region1, region2, region3, propagationDelay=1): """Create test network with propagation delay""" network = engine.Network() config = str({"maxActive": MAX_ACTIVE, "outputWidth": OUTPUT_WIDTH}) network.addRegion("region1", region1, config) network.addRegion("region2", region2, config) network.addRegion("region3", region3, config) network.link("region1", "region2", "UniformLink", "") network.link( "region2", "region3", "UniformLink", "", propagationDelay=propagationDelay) return network
def testNetworkLinkTypeValidation(self): """ This tests whether the links source and destination dtypes match """ network = engine.Network() network.addRegion("from", "py.TestLinks", "") network.addRegion("to", "py.TestLinks", "") # Check for valid links network.link("from", "to", "UniformLink", "", "UInt32", "UInt32") network.link("from", "to", "UniformLink", "", "Real32", "Real32") # Check for invalid links with pytest.raises(RuntimeError): network.link("from", "to", "UniformLink", "", "Real32", "UInt32") with pytest.raises(RuntimeError): network.link("from", "to", "UniformLink", "", "UInt32", "Real32")
def testParameters(self): n = engine.Network() l1 = n.addRegion("l1", "TestNode", "") scalars = [("int32Param", l1.getParameterInt32, l1.setParameterInt32, 32, int, 35), ("uint32Param", l1.getParameterUInt32, l1.setParameterUInt32, 33, int, 36), ("int64Param", l1.getParameterInt64, l1.setParameterInt64, 64, int, 74), ("uint64Param", l1.getParameterUInt64, l1.setParameterUInt64, 65, int, 75), ("real32Param", l1.getParameterReal32, l1.setParameterReal32, 32.1, float, 33.1), ("real64Param", l1.getParameterReal64, l1.setParameterReal64, 64.1, float, 65.1), ("stringParam", l1.getParameterString, l1.setParameterString, "nodespec value", str, "new value")] for paramName, paramGetFunc, paramSetFunc, initval, paramtype, newval in scalars: # Check the initial value for each parameter. x = paramGetFunc(paramName) self.assertEqual(type(x), paramtype, paramName) if initval is None: continue if type(x) == float: self.assertTrue(abs(x - initval) < 0.00001, paramName) else: self.assertEqual(x, initval, paramName) # Now set the value, and check to make sure the value is updated paramSetFunc(paramName, newval) x = paramGetFunc(paramName) self.assertEqual(type(x), paramtype) if type(x) == float: self.assertTrue(abs(x - newval) < 0.00001) else: self.assertEqual(x, newval)