def getRFCenters(sp, params, type='connected'): numColumns = np.product(sp.getColumnDimensions()) dimensions = (params['nX'], params['nY']) meanCoordinates = np.zeros((numColumns, 2)) avgDistToCenter = np.zeros((numColumns, 2)) for columnIndex in range(numColumns): receptiveField = np.zeros((sp.getNumInputs(), )) if type == 'connected': sp.getConnectedSynapses(columnIndex, receptiveField) elif type == 'potential': sp.getPotential(columnIndex, receptiveField) else: raise RuntimeError('unknown RF type') connectedSynapseIndex = np.where(receptiveField)[0] if len(connectedSynapseIndex) == 0: continue coordinates = [] for synapseIndex in connectedSynapseIndex: coordinate = coordinatesFromIndex(synapseIndex, dimensions) coordinates.append(coordinate) coordinates = np.array(coordinates) coordinates = coordinates.astype('float32') angularCoordinates = np.array(coordinates) angularCoordinates[:, 0] = coordinates[:, 0] / params['nX'] * 2 * np.pi angularCoordinates[:, 1] = coordinates[:, 1] / params['nY'] * 2 * np.pi for i in range(2): meanCoordinate = np.arctan2( np.sum(np.sin(angularCoordinates[:, i])), np.sum(np.cos(angularCoordinates[:, i]))) if meanCoordinate < 0: meanCoordinate += 2 * np.pi dist2Mean = angularCoordinates[:, i] - meanCoordinate dist2Mean = np.arctan2(np.sin(dist2Mean), np.cos(dist2Mean)) dist2Mean = np.max(np.abs(dist2Mean)) meanCoordinate *= dimensions[i] / (2 * np.pi) dist2Mean *= dimensions[i] / (2 * np.pi) avgDistToCenter[columnIndex, i] = dist2Mean meanCoordinates[columnIndex, i] = meanCoordinate return meanCoordinates, avgDistToCenter
def testCoordinatesFromIndex(self): self.assertEqual([0], coordinatesFromIndex(0, [100])); self.assertEqual([50], coordinatesFromIndex(50, [100])); self.assertEqual([99], coordinatesFromIndex(99, [100])); self.assertEqual([0, 0], coordinatesFromIndex(0, [100, 80])); self.assertEqual([0, 10], coordinatesFromIndex(10, [100, 80])); self.assertEqual([1, 0], coordinatesFromIndex(80, [100, 80])); self.assertEqual([1, 10], coordinatesFromIndex(90, [100, 80])); self.assertEqual([0, 0, 0], coordinatesFromIndex(0, [100, 10, 8])); self.assertEqual([0, 0, 7], coordinatesFromIndex(7, [100, 10, 8])); self.assertEqual([0, 1, 0], coordinatesFromIndex(8, [100, 10, 8])); self.assertEqual([1, 0, 0], coordinatesFromIndex(80, [100, 10, 8])); self.assertEqual([1, 1, 0], coordinatesFromIndex(88, [100, 10, 8])); self.assertEqual([1, 1, 1], coordinatesFromIndex(89, [100, 10, 8]));
def testCoordinatesFromIndex(self): self.assertEquals([0], coordinatesFromIndex(0, [100])) self.assertEquals([50], coordinatesFromIndex(50, [100])) self.assertEquals([99], coordinatesFromIndex(99, [100])) self.assertEquals([0, 0], coordinatesFromIndex(0, [100, 80])) self.assertEquals([0, 10], coordinatesFromIndex(10, [100, 80])) self.assertEquals([1, 0], coordinatesFromIndex(80, [100, 80])) self.assertEquals([1, 10], coordinatesFromIndex(90, [100, 80])) self.assertEquals([0, 0, 0], coordinatesFromIndex(0, [100, 10, 8])) self.assertEquals([0, 0, 7], coordinatesFromIndex(7, [100, 10, 8])) self.assertEquals([0, 1, 0], coordinatesFromIndex(8, [100, 10, 8])) self.assertEquals([1, 0, 0], coordinatesFromIndex(80, [100, 10, 8])) self.assertEquals([1, 1, 0], coordinatesFromIndex(88, [100, 10, 8])) self.assertEquals([1, 1, 1], coordinatesFromIndex(89, [100, 10, 8]))