def testAdaptShouldIncrementSynapses(self): """ Test that connections are generated on predefined segments. """ random = Random(1981) active_cells = np.array(random.sample( np.arange(0, NUM_CELLS, 1, dtype="uint32"), 40), dtype="uint32") active_cells.sort() presynaptic_input = list(range(0, 10)) presynaptic_input_set = set(presynaptic_input) inputSDR = SDR(1024) inputSDR.sparse = presynaptic_input connections = Connections(NUM_CELLS, 0.51) for i in range(NUM_CELLS): seg = connections.createSegment(i, 1) for cell in active_cells: segments = connections.segmentsForCell(cell) segment = segments[0] for c in presynaptic_input: connections.createSynapse(segment, c, 0.1) connections.adaptSegment(segment, inputSDR, 0.1, 0.001, True) presynamptic_cells = self._getPresynapticCells( connections, segment, 0.2) self.assertEqual(presynamptic_cells, presynaptic_input_set, "Missing synapses") presynamptic_cells = self._getPresynapticCells( connections, segment, 0.3) self.assertEqual(presynamptic_cells, set(), "Too many synapses")
def testNumSynapses(self): """ Test that connections are generated on predefined segments. """ random = Random(1981) active_cells = np.array(random.sample( np.arange(0, NUM_CELLS, 1, dtype="uint32"), 40), dtype="uint32") active_cells.sort() presynaptic_input = list(range(0, 10)) presynaptic_input_set = set(presynaptic_input) inputSDR = SDR(1024) inputSDR.sparse = presynaptic_input connections = Connections(NUM_CELLS, 0.3) for i in range(NUM_CELLS): seg = connections.createSegment(i, 1) for cell in active_cells: segments = connections.segmentsForCell(cell) segment = segments[0] for c in presynaptic_input: connections.createSynapse(segment, c, 0.1) connections.adaptSegment(segment, inputSDR, 0.1, 0.0, False) num_synapses = connections.numSynapses(segment) self.assertEqual(num_synapses, len(presynaptic_input), "Missing synapses") self.assertEqual(connections.numSynapses(), len(presynaptic_input) * 40, "Missing synapses")
def testComputeActivity(self): """ Test that connections are generated on predefined segments. """ random = Random(1981) active_cells = np.array(random.sample( np.arange(0, NUM_CELLS, 1, dtype="uint32"), 40), dtype="uint32") active_cells.sort() presynaptic_input = list(range(0, 10)) presynaptic_input_set = set(presynaptic_input) inputSDR = SDR(1024) inputSDR.sparse = presynaptic_input l = len(presynaptic_input) connections = Connections(NUM_CELLS, 0.51, False) for i in range(NUM_CELLS): seg = connections.createSegment(i, 1) numActiveConnectedSynapsesForSegment = connections.computeActivity( inputSDR, False) for count in numActiveConnectedSynapsesForSegment: self.assertEqual(count, 0, "Segment should not be active") for cell in active_cells: segments = connections.segmentsForCell(cell) segment = segments[0] for c in presynaptic_input: connections.createSynapse(segment, c, 0.1) numActiveConnectedSynapsesForSegment = connections.computeActivity( inputSDR, False) for count in numActiveConnectedSynapsesForSegment: self.assertEqual(count, 0, "Segment should not be active") for cell in active_cells: segments = connections.segmentsForCell(cell) segment = segments[0] connections.adaptSegment(segment, inputSDR, 0.5, 0.0, False) active_cells_set = set(active_cells) numActiveConnectedSynapsesForSegment = connections.computeActivity( inputSDR, False) for cell, count in enumerate(numActiveConnectedSynapsesForSegment): if cell in active_cells_set: self.assertEqual(count, l, "Segment should be active") else: self.assertEqual(count, 0, "Segment should not be active")
def testCreateSynapse(self): # empty connections, create segment and a synapse co = Connections(NUM_CELLS, 0.51) self.assertEqual(co.numSynapses(), 0) self.assertEqual(co.numSegments(), 0) # 1st, create a segment seg = co.createSegment(NUM_CELLS - 1, 1) self.assertEqual(co.numSegments(), 1) #1. create a synapse on that segment syn1 = co.createSynapse(seg, NUM_CELLS - 1, 0.52) self.assertEqual(pytest.approx(co.permanenceForSynapse(syn1)), 0.52) self.assertEqual(co.numSynapses(), 1) #2. creating a duplicit synapse should not crash! syn2 = co.createSynapse(seg, NUM_CELLS - 1, 0.52) self.assertEqual(syn1, syn2, "creating duplicate synapses should return the same") self.assertEqual(co.numSynapses(), 1, "Duplicate synapse, number should not increase") #3. create a different synapse syn3 = co.createSynapse(seg, 1, 0.52) self.assertNotEqual( syn1, syn3, "creating a different synapse must create a new one") self.assertEqual(co.numSynapses(), 2, "New synapse should increase the number") #4. create existing synapse with a new value -> should update permanence #4.a lower permanence -> keep max() syn4 = co.createSynapse( seg, NUM_CELLS - 1, 0.11) #all the same just permanence is a lower val self.assertEqual(syn1, syn4, "just updating existing syn") self.assertEqual(co.numSynapses(), 2, "Duplicate synapse, number should not increase") self.assertEqual(pytest.approx(co.permanenceForSynapse(syn1)), 0.52, "update keeps the larger value") #4.b higher permanence -> update syn5 = co.createSynapse( seg, NUM_CELLS - 1, 0.99) #all the same just permanence is a higher val self.assertEqual(syn1, syn5, "just updating existing syn") self.assertEqual(co.numSynapses(), 2, "Duplicate synapse, number should not increase") self.assertEqual(pytest.approx(co.permanenceForSynapse(syn1)), 0.99, "updated to the larger permanence value")
def testDestroySynapse(self): # empty connections, create segment seg and a synapse syn co = Connections(NUM_CELLS, 0.51) seg = co.createSegment(NUM_CELLS-1, 1) syn1 = co.createSynapse(seg, NUM_CELLS-1, 0.52) # destroy the synapse co.destroySynapse(syn1) self.assertEqual(co.numSynapses(), 0)
def testDestroySynapse(self): # empty connections, create segment seg and a synapse syn co = Connections(NUM_CELLS, 0.51) seg = co.createSegment(NUM_CELLS - 1, 1) syn1 = co.createSynapse(seg, NUM_CELLS - 1, 0.52) # destroy the synapse co.destroySynapse(syn1) self.assertEqual(co.numSynapses(), 0) with pytest.raises( RuntimeError ): # NTA_CHECK, data for removed synapse must not be accessible! permRemoved = co.permanenceForSynapse(syn1) assert permRemoved == perm1 # double remove should be just ignored co.destroySynapse(syn1)