def testWriteRead(self): c1 = Connections(1024) # Add data before serializing s1 = c1.createSegment(0) c1.createSynapse(s1, 254, 0.1173) s2 = c1.createSegment(100) c1.createSynapse(s2, 20, 0.3) c1.createSynapse(s1, 40, 0.3) s3 = c1.createSegment(0) c1.createSynapse(s3, 0, 0.5) c1.createSynapse(s3, 1, 0.5) s4 = c1.createSegment(10) c1.createSynapse(s4, 0, 0.5) c1.createSynapse(s4, 1, 0.5) c1.destroySegment(s4) proto1 = ConnectionsProto_capnp.ConnectionsProto.new_message() c1.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 = ConnectionsProto_capnp.ConnectionsProto.read(f) # Load the deserialized proto c2 = Connections.read(proto2) # Check that the two connections objects are functionally equal self.assertEqual(c1, c2)
def read(cls, proto): """Deserialize from proto instance. :param proto: (TemporalMemoryShimProto) the proto instance to read from """ tm = super(TemporalMemoryShim, cls).read(proto.baseTM) tm.predictiveCells = set(proto.predictedState) tm.connections = Connections.read(proto.conncetions)
def read(cls, proto): """ Reads deserialized data from proto object. :param proto: (DynamicStructBuilder) Proto object :returns: (:class:TemporalMemory) TemporalMemory instance """ tm = object.__new__(cls) # capnp fails to save a tuple, so proto.columnDimensions was forced to # serialize as a list. We prefer a tuple, however, because columnDimensions # should be regarded as immutable. tm.columnDimensions = tuple(proto.columnDimensions) tm.cellsPerColumn = int(proto.cellsPerColumn) tm.activationThreshold = int(proto.activationThreshold) tm.initialPermanence = proto.initialPermanence tm.connectedPermanence = proto.connectedPermanence tm.minThreshold = int(proto.minThreshold) tm.maxNewSynapseCount = int(proto.maxNewSynapseCount) tm.permanenceIncrement = proto.permanenceIncrement tm.permanenceDecrement = proto.permanenceDecrement tm.predictedSegmentDecrement = proto.predictedSegmentDecrement tm.maxSegmentsPerCell = int(proto.maxSegmentsPerCell) tm.maxSynapsesPerSegment = int(proto.maxSynapsesPerSegment) tm.connections = Connections.read(proto.connections) #pylint: disable=W0212 tm._random = Random() tm._random.read(proto.random) #pylint: enable=W0212 tm.activeCells = [int(x) for x in proto.activeCells] tm.winnerCells = [int(x) for x in proto.winnerCells] flatListLength = tm.connections.segmentFlatListLength() tm.numActiveConnectedSynapsesForSegment = [0] * flatListLength tm.numActivePotentialSynapsesForSegment = [0] * flatListLength tm.lastUsedIterationForSegment = [0] * flatListLength tm.activeSegments = [] tm.matchingSegments = [] for protoSegment in proto.activeSegments: tm.activeSegments.append( tm.connections.getSegment(protoSegment.cell, protoSegment.idxOnCell)) for protoSegment in proto.matchingSegments: tm.matchingSegments.append( tm.connections.getSegment(protoSegment.cell, protoSegment.idxOnCell)) for protoSegment in proto.numActivePotentialSynapsesForSegment: segment = tm.connections.getSegment(protoSegment.cell, protoSegment.idxOnCell) tm.numActivePotentialSynapsesForSegment[segment.flatIdx] = (int( protoSegment.number)) tm.iteration = long(proto.iteration) for protoSegment in proto.lastUsedIterationForSegment: segment = tm.connections.getSegment(protoSegment.cell, protoSegment.idxOnCell) tm.lastUsedIterationForSegment[segment.flatIdx] = (long( protoSegment.number)) return tm
def read(cls, proto): """ Reads deserialized data from proto object. :param proto: (DynamicStructBuilder) Proto object :returns: (:class:TemporalMemory) TemporalMemory instance """ tm = object.__new__(cls) # capnp fails to save a tuple, so proto.columnDimensions was forced to # serialize as a list. We prefer a tuple, however, because columnDimensions # should be regarded as immutable. tm.columnDimensions = tuple(proto.columnDimensions) tm.cellsPerColumn = int(proto.cellsPerColumn) tm.activationThreshold = int(proto.activationThreshold) tm.initialPermanence = round(proto.initialPermanence, EPSILON_ROUND) tm.connectedPermanence = round(proto.connectedPermanence, EPSILON_ROUND) tm.minThreshold = int(proto.minThreshold) tm.maxNewSynapseCount = int(proto.maxNewSynapseCount) tm.permanenceIncrement = round(proto.permanenceIncrement, EPSILON_ROUND) tm.permanenceDecrement = round(proto.permanenceDecrement, EPSILON_ROUND) tm.predictedSegmentDecrement = round(proto.predictedSegmentDecrement, EPSILON_ROUND) tm.maxSegmentsPerCell = int(proto.maxSegmentsPerCell) tm.maxSynapsesPerSegment = int(proto.maxSynapsesPerSegment) tm.connections = Connections.read(proto.connections) #pylint: disable=W0212 tm._random = Random() tm._random.read(proto.random) #pylint: enable=W0212 tm.activeCells = [int(x) for x in proto.activeCells] tm.winnerCells = [int(x) for x in proto.winnerCells] flatListLength = tm.connections.segmentFlatListLength() tm.numActiveConnectedSynapsesForSegment = [0] * flatListLength tm.numActivePotentialSynapsesForSegment = [0] * flatListLength tm.lastUsedIterationForSegment = [0] * flatListLength tm.activeSegments = [] tm.matchingSegments = [] for protoSegment in proto.activeSegments: tm.activeSegments.append( tm.connections.getSegment(protoSegment.cell, protoSegment.idxOnCell)) for protoSegment in proto.matchingSegments: tm.matchingSegments.append( tm.connections.getSegment(protoSegment.cell, protoSegment.idxOnCell)) for protoSegment in proto.numActivePotentialSynapsesForSegment: segment = tm.connections.getSegment(protoSegment.cell, protoSegment.idxOnCell) tm.numActivePotentialSynapsesForSegment[segment.flatIdx] = ( int(protoSegment.number)) tm.iteration = long(proto.iteration) for protoSegment in proto.lastUsedIterationForSegment: segment = tm.connections.getSegment(protoSegment.cell, protoSegment.idxOnCell) tm.lastUsedIterationForSegment[segment.flatIdx] = ( long(protoSegment.number)) return tm
def read(cls, proto): """ Reads deserialized data from proto object. @param proto (DynamicStructBuilder) Proto object @return (TemporalMemory) TemporalMemory instance """ tm = object.__new__(cls) # capnp fails to save a tuple, so proto.columnDimensions was forced to # serialize as a list. We prefer a tuple, however, because columnDimensions # should be regarded as immutable. tm.columnDimensions = tuple(proto.columnDimensions) tm.cellsPerColumn = int(proto.cellsPerColumn) tm.activationThreshold = int(proto.activationThreshold) tm.initialPermanence = proto.initialPermanence tm.connectedPermanence = proto.connectedPermanence tm.minThreshold = int(proto.minThreshold) tm.maxNewSynapseCount = int(proto.maxNewSynapseCount) tm.permanenceIncrement = proto.permanenceIncrement tm.permanenceDecrement = proto.permanenceDecrement tm.predictedSegmentDecrement = proto.predictedSegmentDecrement tm.connections = Connections.read(proto.connections) #pylint: disable=W0212 tm._random = Random() tm._random.read(proto.random) #pylint: enable=W0212 tm.activeCells = [int(x) for x in proto.activeCells] tm.winnerCells = [int(x) for x in proto.winnerCells] flatListLength = tm.connections.segmentFlatListLength() tm.numActiveConnectedSynapsesForSegment = [0] * flatListLength tm.numActivePotentialSynapsesForSegment = [0] * flatListLength tm.activeSegments = [] tm.matchingSegments = [] for i in xrange(len(proto.activeSegmentOverlaps)): protoSegmentOverlap = proto.activeSegmentOverlaps[i] segment = tm.connections.getSegment(protoSegmentOverlap.cell, protoSegmentOverlap.segment) tm.activeSegments.append(segment) overlap = protoSegmentOverlap.overlap tm.numActiveConnectedSynapsesForSegment[segment.flatIdx] = overlap for i in xrange(len(proto.matchingSegmentOverlaps)): protoSegmentOverlap = proto.matchingSegmentOverlaps[i] segment = tm.connections.getSegment(protoSegmentOverlap.cell, protoSegmentOverlap.segment) tm.matchingSegments.append(segment) overlap = protoSegmentOverlap.overlap tm.numActivePotentialSynapsesForSegment[segment.flatIdx] = overlap return tm