def test_EllipticalMask2DwithAnchorAndCenteredLayer(self): """Anchored elliptical mask contains correct GIDs when layer is not centered around origo""" cntr = [5.0, 5.0] layer = topo.CreateLayer({ 'rows': 5, 'columns': 5, 'extent': [5., 5.], 'center': cntr, 'elements': 'iaf_psc_alpha' }) maskdict = {'major_axis': 3.0, 'minor_axis': 1.0} mask = topo.CreateMask('elliptical', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, ( 9, 14, 19, )) maskdict = {'major_axis': 3.0, 'minor_axis': 1.0, 'anchor': [1., 1.]} mask = topo.CreateMask('elliptical', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, ( 13, 18, 23, ))
def test_EllipticalMask2D(self): """Simple elliptical mask contains the correct GIDs""" layer = topo.CreateLayer({ 'rows': 11, 'columns': 11, 'extent': [11., 11.], 'elements': 'iaf_psc_alpha' }) maskdict = {'major_axis': 2.0, 'minor_axis': 1.0} mask = topo.CreateMask('elliptical', maskdict) cntr = [0.0, 0.0] gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, ( 51, 62, 73, )) maskdict = {'major_axis': 6.0, 'minor_axis': 3.0} mask = topo.CreateMask('elliptical', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) gid_list_sort = sorted(gid_list) self.assertEqual(gid_list_sort, [ 29, 39, 40, 41, 50, 51, 52, 61, 62, 63, 72, 73, 74, 83, 84, 85, 95 ])
def test_TiltedEllipticalMask2DWithAnchor(self): """Tilted and anchored elliptical mask contains the correct GIDs""" layer = topo.CreateLayer({ 'rows': 11, 'columns': 11, 'extent': [11., 11.], 'elements': 'iaf_psc_alpha' }) maskdict = { 'major_axis': 3.0, 'minor_axis': 1.0, 'anchor': [3., 3.], 'azimuth_angle': 45. } mask = topo.CreateMask('elliptical', maskdict) cntr = [0.0, 0.0] gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, ( 82, 92, 102, )) maskdict = { 'major_axis': 6.0, 'minor_axis': 3.0, 'anchor': [-1.5, 1.], 'azimuth_angle': 135. } mask = topo.CreateMask('elliptical', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) gid_list_sort = sorted(gid_list) self.assertEqual( gid_list_sort, [26, 27, 28, 37, 38, 39, 40, 49, 50, 51, 52, 61, 62, 63]) maskdict = { 'major_axis': 8.0, 'minor_axis': 3.0, 'anchor': [0., 1.], 'azimuth_angle': 90. } mask = topo.CreateMask('elliptical', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) gid_list_sort = sorted(gid_list) self.assertEqual(gid_list_sort, [ 48, 49, 50, 51, 52, 57, 58, 59, 60, 61, 62, 63, 64, 65, 70, 71, 72, 73, 74 ])
def test_RotatedBoxMaskByAzimuthAngle(self): """Test rotated box mask with azimuth angle.""" # Test a 3D layer. pos = [[x * 1., y * 1., z * 1.] for x in range(-2, 3) for y in range(-2, 3) for z in range(-2, 3)] layer = topo.CreateLayer({'positions': pos, 'extent': [5., 5., 5.], 'elements': 'iaf_psc_alpha'}) # First test that we get correct GIDs with box mask that is not # rotated. maskdict = {'lower_left': [-1., -0.5, -0.5], 'upper_right': [1., 0.5, 0.5]} mask = topo.CreateMask('box', maskdict) cntr = [0., 0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, (39, 64, 89,)) # Test with a larger box mask. maskdict = {'lower_left': [-1., -0.5, -1.], 'upper_right': [1., 0.5, 1.]} mask = topo.CreateMask('box', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) sorted_gid_list = sorted(gid_list) self.assertEqual(sorted_gid_list, [38, 39, 40, 63, 64, 65, 88, 89, 90]) # Test the smaller box mask with a rotation of 90 degrees. Only test # the azimuth angle, not the polar angle. maskdict = {'lower_left': [-1., -0.5, -0.5], 'upper_right': [1., 0.5, 0.5], 'azimuth_angle': 90.} mask = topo.CreateMask('box', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, (59, 64, 69,)) # Test rotation of the larger box with an azimuth angle of 90 degrees. maskdict = {'lower_left': [-1., -0.5, -1.], 'upper_right': [1., 0.5, 1.], 'azimuth_angle': 90.} mask = topo.CreateMask('box', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) sorted_gid_list = sorted(gid_list) self.assertEqual(sorted_gid_list, [58, 59, 60, 63, 64, 65, 68, 69, 70])
def test_EllipticalMask2DWithAnchor(self): """Anchored elliptical mask contains the correct GIDs""" layer = topo.CreateLayer({ 'rows': 11, 'columns': 11, 'extent': [11., 11.], 'elements': 'iaf_psc_alpha' }) maskdict = {'major_axis': 6.0, 'minor_axis': 3.0, 'anchor': [-2., -2.]} mask = topo.CreateMask('elliptical', maskdict) cntr = [0.0, 0.0] gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, ( 9, 19, 20, 21, 30, 31, 32, 41, 42, 43, 52, 53, 54, 63, 64, 65, 75, ))
def mask(masktype, specs): if 'rectangular' in masktype: newSpecs = { 'lower_left': np.array(specs['lower_left'], dtype=float).tolist(), 'upper_right': np.array(specs['upper_right'], dtype=float).tolist(), 'azimuth_angle': float(specs['azimuth_angle']), } elif 'circular' in masktype: newSpecs = {'radius': float(specs['radius'])} elif 'doughnut' in masktype: newSpecs = { 'inner_radius': float(specs['inner_radius']), 'outer_radius': float(specs['outer_radius']), } elif 'elliptical' in masktype: newSpecs = { 'major_axis': float(specs['major_axis']), 'minor_axis': float(specs['minor_axis']), 'azimuth_angle': float(specs['azimuth_angle']), # 'anchor': np.array(specs['anchor']).tolist(), } else: newSpecs = {} return tp.CreateMask(masktype=masktype, specs=newSpecs)
def test_SelectNodesByMaskIn3D(self): """Test SelectNodesByMask for rectangular mask in 3D layer""" pos = [[x * 1., y * 1., z * 1.] for x in range(-5, 6) for y in range(-5, 6) for z in range(-5, 6)] layer = topo.CreateLayer({ 'positions': pos, 'extent': [11., 11., 11.], 'elements': 'iaf_psc_alpha' }) maskdict = { 'lower_left': [-6., -6., -6.], 'upper_right': [-4., -4., -4.] } mask = topo.CreateMask('box', maskdict) cntr = [0., 0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, ( 2, 3, 13, 14, 123, 124, 134, 135, ))
def test_TiltedEllipsoidalMask3DWithAnchor(self): """Tilted and anchored ellipsoidal mask contains the correct GIDs""" pos = [[x * 1., y * 1., z * 1.] for x in range(-5, 6) for y in range(-5, 6) for z in range(-5, 6)] layer = topo.CreateLayer({ 'positions': pos, 'extent': [11., 11., 11.], 'elements': 'iaf_psc_alpha' }) maskdict = { 'major_axis': 4.0, 'minor_axis': 1.0, 'polar_axis': 1.0, 'anchor': [-5., -5., -4.] } mask = topo.CreateMask('ellipsoidal', maskdict) cntr = [0., 0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, ( 3, 124, 245, )) maskdict = { 'major_axis': 4., 'minor_axis': 1., 'polar_axis': 1., 'anchor': [-4., -4., -4.], 'azimuth_angle': 45. } mask = topo.CreateMask('ellipsoidal', maskdict) cntr = [0., 0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, ( 3, 135, 267, ))
def test_SelectNodesByMaskIn2D(self): """Test SelectNodesByMask for rectangular mask in 2D layer""" layer = topo.CreateLayer({ 'rows': 11, 'columns': 11, 'extent': [11., 11.], 'elements': 'iaf_psc_alpha' }) maskdict = {'lower_left': [-2., -1.], 'upper_right': [2., 1.]} mask = topo.CreateMask('rectangular', maskdict) cntr = [0.0, 0.0] gid_list = topo.SelectNodesByMask(layer, cntr, mask) gid_list_sort = sorted(gid_list) self.assertEqual( gid_list_sort, [39, 40, 41, 50, 51, 52, 61, 62, 63, 72, 73, 74, 83, 84, 85]) nest.ResetKernel() cntr = [3., 3.] layer = topo.CreateLayer({ 'rows': 5, 'columns': 5, 'extent': [11., 11.], 'center': cntr, 'elements': 'iaf_psc_alpha' }) maskdict = {'lower_left': [1., 1.], 'upper_right': [5., 5.]} mask = topo.CreateMask('rectangular', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, ( 17, 18, 22, 23, ))
def test_EllipsoidalMask3D(self): """Simple ellipsoidal mask contains the correct GIDs""" pos = [[x * 1., y * 1., z * 1.] for x in range(-5, 6) for y in range(-5, 6) for z in range(-5, 6)] layer = topo.CreateLayer({ 'positions': pos, 'extent': [11., 11., 11.], 'elements': 'iaf_psc_alpha' }) maskdict = {'major_axis': 3.0, 'minor_axis': 1.0, 'polar_axis': 1.0} mask = topo.CreateMask('ellipsoidal', maskdict) cntr = [0., 0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, ( 546, 667, 788, )) maskdict = { 'major_axis': 3.0, 'minor_axis': 1.0, 'polar_axis': 1.0, 'azimuth_angle': 90. } mask = topo.CreateMask('ellipsoidal', maskdict) cntr = [0., 0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, ( 656, 667, 678, ))
def create_random_patches( layer, r_loc=0.5, p_loc=0.7, num_patches=3, ): """ Create random long range patchy connections. To every neuron a single link is established instead of taking axonal morphology into account. :param layer: Layer in which the connections should be established :param r_loc: Radius for local connections :param p_loc: Probability for local connections :param num_patches: Number of patches that should be created :return Nodes of the layer for debugging purposes (plotting) """ # Calculate the parameters for the patches r_p = r_loc / 2. min_distance = r_loc + r_p max_distance = R_MAX / 2. - r_loc p_p = get_lr_connection_probability_patches(r_loc, p_loc, r_p, num_patches=num_patches) # Iterate through all neurons, as all neurons have random patches nodes = nest.GetNodes(layer)[0] for neuron in nodes: # Calculate radial distance and the respective coordinates for patches radial_distance = np.random.uniform(min_distance, max_distance, size=num_patches).tolist() radial_angle = np.random.uniform(0., 359., size=num_patches).tolist() # Calculate patch region mask_specs = {"radius": r_p} anchors = [ to_coordinates(distance, angle) for angle, distance in zip(radial_angle, radial_distance) ] patches = tuple() for anchor in anchors: patches += tp.SelectNodesByMask(layer, anchor, mask_obj=tp.CreateMask( "circular", specs=mask_specs)) # Define connection connect_dict = {"rule": "pairwise_bernoulli", "p": p_p} nest.Connect([neuron], patches, connect_dict) # Return nodes of layer for debugging return nest.GetNodes(layer)[0]
def test_RotatedBoxOutsideOrigin(self): """Test rotated box where the mask does not contain the origin.""" pos = [[x * 1., y * 1., z * 1.] for x in range(-2, 3) for y in range(-2, 3) for z in range(-2, 3)] layer = topo.CreateLayer({'positions': pos, 'extent': [5., 5., 5.], 'elements': 'iaf_psc_alpha'}) # First test that we get the correct GIDs when our mask does not # contain the origin. maskdict = {'lower_left': [-2.0, -1.0, 0.5], 'upper_right': [-0.5, -0.5, 2.0]} mask = topo.CreateMask('box', maskdict) cntr = [0., 0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, (10, 11, 35, 36,)) # Test that we get the correct GIDs with a azimuth rotation angle of 45 # degrees when the mask does not contain the origin. maskdict = {'lower_left': [-2.5, -1.0, 0.5], 'upper_right': [-0.5, -0.5, 2.5], 'azimuth_angle': 45.0} mask = topo.CreateMask('box', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, (10, 11, 40, 41,)) # Test that we get the correct GIDs with a polar rotation angle of 45 # degrees when the mask does not contain the origin. maskdict = {'lower_left': [-1.5, -2.5, 0.5], 'upper_right': [-1.0, -0.5, 2.5], 'polar_angle': 45.0} mask = topo.CreateMask('box', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, (5, 10, 31, 36,))
def test_RotatedRectangleOutsideOrigin(self): """ Test rotated rectangle where the mask does not contain the origin. """ layer = topo.CreateLayer({'rows': 11, 'columns': 11, 'extent': [11., 11.], 'elements': 'iaf_psc_alpha'}) # First test that we get the correct GIDs when our mask does not # contain the origin. maskdict = {'lower_left': [1., 1.], 'upper_right': [4., 2.]} mask = topo.CreateMask('rectangular', maskdict) cntr = [0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, (71, 72, 82, 83, 93, 94, 104, 105,)) # Then test that we get the correct GIDs with a azimuth rotation angle # of 45 degrees when the mask does not contain the origin. maskdict = {'lower_left': [0.5, 0.5], 'upper_right': [4.5, 2.5], 'azimuth_angle': 45.0} mask = topo.CreateMask('rectangular', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, (72, 82, 83, 84, 92, 93, 94, 104,)) # Test that we get the correct GIDs with a azimuth rotation angle # of 90 degrees when the mask does not contain the origin. maskdict = {'lower_left': [1.0, 1.0], 'upper_right': [4.0, 2.0], 'azimuth_angle': 90.0} mask = topo.CreateMask('rectangular', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, (81, 82, 83, 84, 92, 93, 94, 95,))
def test_RotatedBoxMaskByAzimuthAndPolarAngle(self): """Test rotated box mask with azimuth and polar angle.""" pos = [[x * 1., y * 1., z * 1.] for x in range(-2, 3) for y in range(-2, 3) for z in range(-2, 3)] layer = topo.CreateLayer({'positions': pos, 'extent': [5., 5., 5.], 'elements': 'iaf_psc_alpha'}) # Test with a azimuth angle and polar angle of 45 degrees. maskdict = {'lower_left': [-0.5, -1.5, -1.5], 'upper_right': [0.5, 1.5, 1.5], 'azimuth_angle': 45., 'polar_angle': 45.} mask = topo.CreateMask('box', maskdict) cntr = [0., 0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) sorted_gid_list = sorted(gid_list) self.assertEqual(sorted_gid_list, [38, 39, 44, 58, 59, 64, 69, 70, 84, 89, 90])
def test_RotatedBoxMaskByPolarAngle(self): """Test rotated box mask with polar angle.""" pos = [[x * 1., y * 1., z * 1.] for x in range(-2, 3) for y in range(-2, 3) for z in range(-2, 3)] layer = topo.CreateLayer({'positions': pos, 'extent': [5., 5., 5.], 'elements': 'iaf_psc_alpha'}) # First test without rotation maskdict = {'lower_left': [-0.5, -1.0, -1.0], 'upper_right': [0.5, 1.0, 1.0]} mask = topo.CreateMask('box', maskdict) cntr = [0., 0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) sorted_gid_list = sorted(gid_list) self.assertEqual(sorted_gid_list, [58, 59, 60, 63, 64, 65, 68, 69, 70]) # Test with a polar angle of 90 degrees. maskdict = {'lower_left': [-0.5, -1.0, -1.0], 'upper_right': [0.5, 1.0, 1.0], 'polar_angle': 90.} mask = topo.CreateMask('box', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) sorted_gid_list = sorted(gid_list) self.assertEqual(sorted_gid_list, [34, 39, 44, 59, 64, 69, 84, 89, 94]) # Test with a polar angle of 180 degrees, should be the same as the # one without a polar angle. maskdict = {'lower_left': [-0.5, -1.0, -1.0], 'upper_right': [0.5, 1.0, 1.0], 'polar_angle': 180.} mask = topo.CreateMask('box', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) sorted_gid_list = sorted(gid_list) self.assertEqual(sorted_gid_list, [58, 59, 60, 63, 64, 65, 68, 69, 70]) # Test with a polar angle of 45 degrees. maskdict = {'lower_left': [-0.5, -1.5, -1.5], 'upper_right': [0.5, 1.5, 1.5], 'polar_angle': 45.} mask = topo.CreateMask('box', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) sorted_gid_list = sorted(gid_list) self.assertEqual(sorted_gid_list, [33, 38, 43, 59, 64, 69, 85, 90, 95]) # Test with a polar angle of 135 degrees. The GIDs should be # perpendicular to the ones obtained by a polar angle of 45 degrees. maskdict = {'lower_left': [-0.5, -1.5, -1.5], 'upper_right': [0.5, 1.5, 1.5], 'polar_angle': 135.} mask = topo.CreateMask('box', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) sorted_gid_list = sorted(gid_list) self.assertEqual(sorted_gid_list, [35, 40, 45, 59, 64, 69, 83, 88, 93]) # Test two symmetric masks in x and z direction. One with no polar # angle and one with a polar angle of 90 degrees. As the masks are # symmetrical in x and z, a polar angle of 90 degrees should give the # same GIDs as the one without a polar angle. maskdict = {'lower_left': [-1., -0.5, -1.], 'upper_right': [1., 0.5, 1.]} mask = topo.CreateMask('box', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) sorted_gid_list_1 = sorted(gid_list) self.assertEqual(sorted_gid_list_1, [38, 39, 40, 63, 64, 65, 88, 89, 90]) maskdict = {'lower_left': [-1., -0.5, -1.], 'upper_right': [1., 0.5, 1.], 'polar_angle': 90.} mask = topo.CreateMask('box', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) sorted_gid_list = sorted(gid_list) self.assertEqual(sorted_gid_list, [38, 39, 40, 63, 64, 65, 88, 89, 90]) self.assertEqual(sorted_gid_list_1, sorted_gid_list)
def test_TiltedEllipsoidalMask(self): """"Ellipsoidal mask contains correct GIDs when tilted with respect to x-axis and z-axis""" pos = [[x * 1., y * 1., z * 1.] for x in range(-2, 3) for y in range(-2, 3) for z in range(-2, 3)] layer = topo.CreateLayer({ 'positions': pos, 'extent': [5., 5., 5.], 'elements': 'iaf_psc_alpha' }) maskdict = { 'major_axis': 3.0, 'minor_axis': 1.0, 'polar_axis': 1.0, 'polar_angle': 90. } mask = topo.CreateMask('ellipsoidal', maskdict) cntr = [0., 0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, ( 63, 64, 65, )) nest.ResetKernel() pos = [[x * 1., y * 1., z * 1.] for x in range(-2, 3) for y in range(-2, 3) for z in range(-2, 3)] layer = topo.CreateLayer({ 'positions': pos, 'extent': [5., 5., 5.], 'elements': 'iaf_psc_alpha' }) maskdict = { 'major_axis': 4.0, 'minor_axis': 1., 'polar_axis': 1.5, 'azimuth_angle': 45., 'polar_angle': 45. } mask = topo.CreateMask('ellipsoidal', maskdict) cntr = [0., 0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) sorted_gid_list = sorted(gid_list) self.assertEqual(sorted_gid_list, [35, 64, 93]) nest.ResetKernel() layer = topo.CreateLayer({ 'positions': pos, 'extent': [5., 5., 5.], 'elements': 'iaf_psc_alpha' }) maskdict = { 'major_axis': 3.0, 'minor_axis': 2., 'polar_axis': 1.0, 'polar_angle': 45. } mask = topo.CreateMask('ellipsoidal', maskdict) cntr = [0., 0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) sorted_gid_list = sorted(gid_list) self.assertEqual(sorted_gid_list, [40, 59, 64, 69, 88]) nest.ResetKernel() layer = topo.CreateLayer({ 'positions': pos, 'extent': [5., 5., 5.], 'elements': 'iaf_psc_alpha' }) maskdict = { 'major_axis': 4.0, 'minor_axis': 1., 'polar_axis': 1.5, 'polar_angle': 30. } mask = topo.CreateMask('ellipsoidal', maskdict) cntr = [0., 0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) sorted_gid_list = sorted(gid_list) self.assertEqual(sorted_gid_list, [39, 40, 64, 88, 89]) nest.ResetKernel() layer = topo.CreateLayer({ 'positions': pos, 'extent': [5., 5., 5.], 'elements': 'iaf_psc_alpha' }) maskdict = { 'major_axis': 4.0, 'minor_axis': 2.5, 'polar_axis': 1.0, 'azimuth_angle': 45., 'polar_angle': 30. } mask = topo.CreateMask('ellipsoidal', maskdict) cntr = [0., 0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) sorted_gid_list = sorted(gid_list) self.assertEqual(sorted_gid_list, [35, 39, 59, 64, 69, 89, 93])
def create_localtion_based_patches(layer, r_loc=0.5, p_loc=0.7, size_boxes=0.5, num_patches=3, num_shared_patches=6, num_patches_replaced=3, is_partially_overlapping=False): """ Function to establish patchy connections for neurons that have a location based relationship, such that they are in the same sublayer / box :param layer: Layer in which the connections should be established :param r_loc: Radius of local connections :param p_loc: Probaility of local connections :param size_boxes: Size of a sublayer in which neurons share patches. The R_MAX / size_boxes should be an integer value :param num_patches: Number of patches per neuron :param num_shared_patches: Number of patches per box that are shared between the neurons :param num_patches_replaced: Number of patches that are replaced in x-direction (for partially overlapping patches) :param is_partially_overlapping: Flag for partially overlapping patches :return: Sublayer at size_boxes/2 for debugging purposes (plotting) """ # Calculate parameters for the patches r_p = r_loc / 2. min_distance = r_loc + r_p max_distance = R_MAX / 2. - r_loc p_p = get_lr_connection_probability_patches(r_loc, p_loc, r_p, num_patches=num_patches) # Create sublayer boxes that share same patches sublayer_anchors = [[ x * size_boxes + size_boxes / 2., y * size_boxes + size_boxes / 2. ] for y in range(-int(R_MAX / (2. * float(size_boxes))), int(R_MAX / (2. * float(size_boxes)))) for x in range(-int(R_MAX / (2. * float(size_boxes))), int(R_MAX / (2. * float(size_boxes))))] box_mask_dict = { "lower_left": [-size_boxes / 2., -size_boxes / 2.], "upper_right": [size_boxes / 2., size_boxes / 2.] } last_y_anchor = -R_MAX - 1 debug_sub_layer = None for anchor in sublayer_anchors: sub_layer = tp.SelectNodesByMask(layer, anchor, mask_obj=tp.CreateMask( "rectangular", specs=box_mask_dict)) if np.all(np.asarray(anchor) == size_boxes / 2.): debug_sub_layer = sub_layer # Create pool of patches # Calculate radial distance and the respective coordinates for patches if not is_partially_overlapping or anchor[1] > last_y_anchor: radial_distance = np.random.uniform( min_distance, max_distance, size=num_shared_patches).tolist() radial_angle = np.random.uniform(0., 359., size=num_shared_patches).tolist() else: replaced_indices = np.random.choice(num_shared_patches, size=num_patches_replaced, replace=False) np.asarray(radial_distance)[replaced_indices] = np.random.uniform( min_distance, max_distance, size=num_patches_replaced).tolist() np.asarray(radial_angle)[replaced_indices] = np.random.uniform( 0., 359., size=num_patches_replaced).tolist() last_y_anchor = anchor[1] # Calculate anchors of patches mask_specs = {"radius": r_p} patches_anchors = [ to_coordinates(distance, angle) for angle, distance in zip(radial_angle, radial_distance) ] # Iterate through all neurons, as patches are chosen for each neuron independently for neuron in sub_layer: neuron_patch_anchors = np.asarray(patches_anchors)[ np.random.choice(len(patches_anchors), size=num_patches, replace=False)] patches = tuple() for neuron_anchor in neuron_patch_anchors.tolist(): patches += tp.SelectNodesByMask(layer, neuron_anchor, mask_obj=tp.CreateMask( "circular", specs=mask_specs)) # Define connection connect_dict = {"rule": "pairwise_bernoulli", "p": p_p} nest.Connect([neuron], patches, connect_dict) # Return last sublayer for debugging return debug_sub_layer
def test_CreateEllipticalMask2D(self): """Creates simple elliptical mask""" mask_dict = {'major_axis': 6.0, 'minor_axis': 3.0} mask = topo.CreateMask('elliptical', mask_dict) self.assertTrue(mask.Inside([0.0, 0.0]))
def test_RotatedRectangularMask(self): """Test rotated rectangular mask. We have: lower_left: [-1., -0.5] upper_right: [ 1., 0.5] So, if we have: layer: 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25 6 11 16 21 26 and have azimuth_angle = 0, we should get gids 9, 14, 19 if we select GIDs by mask. If we have azimuth_angle = 90, we should get gids 13, 14, 15. """ # Test 2D layer layer = topo.CreateLayer({'rows': 5, 'columns': 5, 'extent': [5., 5.], 'elements': 'iaf_psc_alpha'}) # First test without rotation. maskdict = {'lower_left': [-1., -0.5], 'upper_right': [1., 0.5]} mask = topo.CreateMask('rectangular', maskdict) cntr = [0., 0.] gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, (9, 14, 19,)) # Test if we get correct GIDs when rotating 90 degrees. maskdict = {'lower_left': [-1., -0.5], 'upper_right': [1., 0.5], 'azimuth_angle': 90.0} mask = topo.CreateMask('rectangular', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, (13, 14, 15,)) # Test rotation with an azimuth angle of 45 degrees. maskdict = {'lower_left': [-1.5, -0.5], 'upper_right': [1.5, 0.5], 'azimuth_angle': 45.0} mask = topo.CreateMask('rectangular', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, (10, 14, 18,)) # Test rotation with an azimuth angle of 135 degrees. maskdict = {'lower_left': [-1.5, -0.5], 'upper_right': [1.5, 0.5], 'azimuth_angle': 135.0} mask = topo.CreateMask('rectangular', maskdict) gid_list = topo.SelectNodesByMask(layer, cntr, mask) self.assertEqual(gid_list, (8, 14, 20,)) # Test that an error is raised if we send in a polar angle to a 2D # mask. maskdict = {'lower_left': [-1.5, -0.5], 'upper_right': [1.5, 0.5], 'polar_angle': 45.0} with self.assertRaises(nest.NESTError): mask = topo.CreateMask('rectangular', maskdict)
def test_create_partially_overlapping_patches(self): self.reset() r_loc = 0.1 p_loc = 0.7 d_p = r_loc size_boxes = 0.2 num_patches = 2 num_shared_patches = 3 num_patches_replaced = 3 p_p = 0.3 nc.create_partially_overlapping_patches( self.torus_layer, r_loc=r_loc, p_loc=p_loc, size_boxes=size_boxes, num_patches=num_patches, num_shared_patches=num_shared_patches, num_patches_replaced=num_patches_replaced, p_p=p_p ) sublayer_anchors, box_mask = nc.create_distinct_sublayer_boxes(size_boxes, size_layer=self.size_layer) for anchor in sublayer_anchors: box_nodes = tp.SelectNodesByMask( self.torus_layer, anchor, mask_obj=tp.CreateMask("rectangular", specs=box_mask) ) box_patches = [] for s in box_nodes: conn = nest.GetConnections(source=[s]) targets = [t for t in nest.GetStatus(conn, "target") if t in self.torus_nodes] patch_idx = set() for t in targets: distance_anchor_t = tp.Distance([anchor], [t])[0] self.assertGreaterEqual(distance_anchor_t, r_loc, "Target node is too close to anchor") patch_not_existent = True for idx, patch in enumerate(box_patches): patch_not_existent = False ds = tp.Distance([t], list(patch)) if np.any(np.asarray(ds) > d_p): patch_not_existent = True continue if not patch_not_existent: patch_idx.add(idx) box_patches[idx].add(t) patch_not_existent = False break self.assertLessEqual(len(patch_idx), num_patches, "Created too many patches per neuron") if patch_not_existent: box_patches.append({t}) # Chose num shared patches + 1, as it can happen that patches are very close to each other # -x-x-x- ==> If all patches (x) are right next to each other the algorithm can accidentally # see the spaces in between (-) as patch as well. Then the maximum is one more than the # num of shared patches self.assertLessEqual(len(box_patches), num_shared_patches+1, "Created too many patches per box")