1# -*- coding: utf-8 -*- 2# 3# layers.py 4# 5# This file is part of NEST. 6# 7# Copyright (C) 2004 The NEST Initiative 8# 9# NEST is free software: you can redistribute it and/or modify 10# it under the terms of the GNU General Public License as published by 11# the Free Software Foundation, either version 2 of the License, or 12# (at your option) any later version. 13# 14# NEST is distributed in the hope that it will be useful, 15# but WITHOUT ANY WARRANTY; without even the implied warranty of 16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17# GNU General Public License for more details. 18# 19# You should have received a copy of the GNU General Public License 20# along with NEST. If not, see <http://www.gnu.org/licenses/>. 21 22# Run as python3 layers.py > layers.log 23 24import matplotlib.pyplot as plt 25import nest 26import numpy as np 27 28# seed NumPy RNG to ensure identical results for runs with random placement 29np.random.seed(1234567) 30 31 32def beautify_layer(layer, fig=plt.gcf(), xlabel=None, ylabel=None, 33 xlim=None, ylim=None, xticks=None, yticks=None, dx=0, dy=0): 34 """Assume either x and ylims/ticks given or none""" 35 ctr = layer.spatial['center'] 36 ext = layer.spatial['extent'] 37 38 if xticks is None: 39 if 'shape' in layer.spatial: 40 dx = float(ext[0]) / layer.spatial['shape'][0] 41 dy = float(ext[1]) / layer.spatial['shape'][1] 42 xticks = ctr[0] - ext[0] / 2. + dx / 2. + dx * np.arange( 43 layer.spatial['shape'][0]) 44 yticks = ctr[1] - ext[1] / 2. + dy / 2. + dy * np.arange( 45 layer.spatial['shape'][1]) 46 47 if xlim is None: 48 xlim = [ctr[0] - ext[0] / 2. - dx / 2., ctr[0] + ext[ 49 0] / 2. + dx / 2.] # extra space so extent is visible 50 ylim = [ctr[1] - ext[1] / 2. - dy / 2., ctr[1] + ext[1] / 2. + dy / 2.] 51 else: 52 ext = [xlim[1] - xlim[0], ylim[1] - ylim[0]] 53 54 ax = fig.gca() 55 ax.set_xlim(xlim) 56 ax.set_ylim(ylim) 57 ax.set_aspect('equal', 'box') 58 ax.set_xticks(xticks) 59 ax.set_yticks(yticks) 60 ax.grid(True) 61 ax.set_axisbelow(True) 62 ax.set_xlabel(xlabel) 63 ax.set_ylabel(ylabel) 64 return 65 66 67# -------------------------------------------------- 68 69nest.ResetKernel() 70 71#{ layer1 #} 72layer = nest.Create('iaf_psc_alpha', 73 positions=nest.spatial.grid(shape=[5, 5])) 74#{ end #} 75 76fig = nest.PlotLayer(layer, nodesize=50) 77beautify_layer(layer, fig, xlabel='x-axis (columns)', ylabel='y-axis (rows)') 78ax = fig.gca() 79tx = [] 80for r in range(5): 81 tx.append(ax.text(0.65, 0.4 - r * 0.2, str(r), 82 horizontalalignment='center', 83 verticalalignment='center')) 84 tx.append(ax.text(-0.4 + r * 0.2, 0.65, str(r), 85 horizontalalignment='center', 86 verticalalignment='center')) 87 88# For bbox_extra_artists, see 89# https://github.com/matplotlib/matplotlib/issues/351 90# plt.savefig('../user_manual_figures/layer1.png', bbox_inches='tight', 91# bbox_extra_artists=tx) 92 93print("#{ layer1s.log #}") 94#{ layer1s #} 95print(layer.spatial) 96#{ end #} 97print("#{ end.log #}") 98 99print("#{ layer1p.log #}") 100#{ layer1p #} 101nest.PrintNodes() 102#{ end #} 103print("#{ end.log #}") 104 105# -------------------------------------------------- 106 107nest.ResetKernel() 108 109#{ layer2 #} 110layer = nest.Create('iaf_psc_alpha', 111 positions=nest.spatial.grid( 112 shape=[5, 5], 113 extent=[2.0, 0.5])) 114#{ end #} 115 116fig = nest.PlotLayer(layer, nodesize=50) 117beautify_layer(layer, fig, xlabel='x-axis (columns)', ylabel='y-axis (rows)') 118ax = fig.gca() 119tx = [] 120 121for r in range(5): 122 tx.append(fig.gca().text(1.25, 0.2 - r * 0.1, str(r), 123 horizontalalignment='center', 124 verticalalignment='center')) 125 tx.append(fig.gca().text(-0.8 + r * 0.4, 0.35, str(r), 126 horizontalalignment='center', 127 verticalalignment='center')) 128 129# See https://github.com/matplotlib/matplotlib/issues/351 130plt.savefig('../user_manual_figures/layer2.png', bbox_inches='tight', 131 bbox_extra_artists=tx) 132 133# -------------------------------------------------- 134 135nest.ResetKernel() 136 137#{ layer3 #} 138layer1 = nest.Create('iaf_psc_alpha', 139 positions=nest.spatial.grid(shape=[5, 5])) 140layer2 = nest.Create('iaf_psc_alpha', 141 positions=nest.spatial.grid( 142 shape=[5, 5], 143 center=[-1., 1.])) 144layer3 = nest.Create('iaf_psc_alpha', 145 positions=nest.spatial.grid( 146 shape=[5, 5], 147 center=[1.5, 0.5])) 148#{ end #} 149 150fig = nest.PlotLayer(layer1, nodesize=50) 151nest.PlotLayer(layer2, nodesize=50, nodecolor='g', fig=fig) 152nest.PlotLayer(layer3, nodesize=50, nodecolor='r', fig=fig) 153beautify_layer(layer1, fig, xlabel='x-axis (columns)', ylabel='y-axis (rows)', 154 xlim=[-1.6, 2.1], ylim=[-0.6, 1.6], 155 xticks=np.arange(-1.4, 2.05, 0.2), 156 yticks=np.arange(-0.4, 1.45, 0.2)) 157 158plt.savefig('../user_manual_figures/layer3.png', bbox_inches='tight') 159 160# -------------------------------------------------- 161 162nest.ResetKernel() 163 164#{ layer3a #} 165nx, ny = 5, 3 166d = 0.1 167layer = nest.Create('iaf_psc_alpha', 168 positions=nest.spatial.grid( 169 shape=[nx, ny], 170 extent=[nx * d, ny * d], 171 center=[nx * d / 2., 0.])) 172#{ end #} 173 174fig = nest.PlotLayer(layer, nodesize=100) 175plt.plot(0, 0, 'x', markersize=20, c='k', mew=3) 176plt.plot(nx * d / 2, 0, 'o', markersize=20, c='k', mew=3, mfc='none', 177 zorder=100) 178beautify_layer(layer, fig, xlabel='x-axis (columns)', ylabel='y-axis (rows)', 179 xticks=np.arange(0., 0.501, 0.05), 180 yticks=np.arange(-0.15, 0.151, 0.05), 181 xlim=[-0.05, 0.55], ylim=[-0.2, 0.2]) 182 183plt.savefig('../user_manual_figures/layer3a.png', bbox_inches='tight') 184 185# -------------------------------------------------- 186 187nest.ResetKernel() 188 189#{ layer4 #} 190pos = nest.spatial.free(pos=nest.random.uniform(min=-0.5, max=0.5), 191 num_dimensions=2) 192layer = nest.Create('iaf_psc_alpha', 50, 193 positions=pos) 194#{ end #} 195 196fig = nest.PlotLayer(layer, nodesize=50) 197beautify_layer(layer, fig, xlabel='x-axis (columns)', ylabel='y-axis (rows)', 198 xlim=[-0.55, 0.55], ylim=[-0.55, 0.55], 199 xticks=[-0.5, 0., 0.5], yticks=[-0.5, 0., 0.5]) 200 201plt.savefig('../user_manual_figures/layer4.png', bbox_inches='tight') 202 203# -------------------------------------------------- 204 205nest.ResetKernel() 206 207#{ layer4b #} 208pos = nest.spatial.free(pos=[[-0.5, -0.5], [-0.25, -0.25], [0.75, 0.75]]) 209layer = nest.Create('iaf_psc_alpha', positions=pos) 210#{ end #} 211 212fig = nest.PlotLayer(layer, nodesize=50) 213beautify_layer(layer, fig, xlabel='x-axis (columns)', ylabel='y-axis (rows)', 214 xlim=[-0.55, 0.80], ylim=[-0.55, 0.80], 215 xticks=[-0.75, -0.5, -0.25, 0., 0.25, 0.5, 0.75, 1.], 216 yticks=[-0.75, -0.5, -0.25, 0., 0.25, 0.5, 0.75, 1.]) 217 218plt.savefig('../user_manual_figures/layer4b.png', bbox_inches='tight') 219 220# -------------------------------------------------- 221 222nest.ResetKernel() 223 224#{ layer4_3d #} 225pos = nest.spatial.free(nest.random.uniform(min=-0.5, max=0.5), 226 num_dimensions=3) 227layer = nest.Create('iaf_psc_alpha', 200, positions=pos) 228#{ end #} 229 230fig = nest.PlotLayer(layer, nodesize=50) 231 232plt.savefig('../user_manual_figures/layer4_3d.png', bbox_inches='tight') 233 234# -------------------------------------------------- 235 236nest.ResetKernel() 237 238#{ layer4_3d_b #} 239pos = nest.spatial.grid(shape=[4, 5, 6]) 240layer = nest.Create('iaf_psc_alpha', positions=pos) 241#{ end #} 242 243fig = nest.PlotLayer(layer, nodesize=50) 244 245plt.savefig('../user_manual_figures/layer4_3d_b.png', bbox_inches='tight') 246 247# -------------------------------------------------- 248 249nest.ResetKernel() 250 251#{ player #} 252layer = nest.Create('iaf_psc_alpha', 253 positions=nest.spatial.grid( 254 shape=[5, 1], 255 extent=[5., 1.], 256 edge_wrap=True)) 257#{ end #} 258 259# fake plot with layer on line and circle 260clist = [(0, 0, 1), (0.35, 0, 1), (0.6, 0, 1), (0.8, 0, 1), (1.0, 0, 1)] 261fig = plt.figure() 262ax1 = fig.add_subplot(221) 263ax1.plot([0.5, 5.5], [0, 0], 'k-', lw=2) 264ax1.scatter(range(1, 6), [0] * 5, s=200, c=clist) 265ax1.set_xlim([0, 6]) 266ax1.set_ylim([-0.5, 1.25]) 267ax1.set_aspect('equal', 'box') 268ax1.set_xticks([]) 269ax1.set_yticks([]) 270for j in range(1, 6): 271 ax1.text(j, 0.5, str('(%d,0)' % (j - 3)), 272 horizontalalignment='center', verticalalignment='bottom') 273 274ax1a = fig.add_subplot(223) 275ax1a.plot([0.5, 5.5], [0, 0], 'k-', lw=2) 276ax1a.scatter(range(1, 6), [0] * 5, s=200, 277 c=[clist[0], clist[1], clist[2], clist[2], clist[1]]) 278ax1a.set_xlim([0, 6]) 279ax1a.set_ylim([-0.5, 1.25]) 280ax1a.set_aspect('equal', 'box') 281ax1a.set_xticks([]) 282ax1a.set_yticks([]) 283for j in range(1, 6): 284 ax1a.text(j, 0.5, str('(%d,0)' % (j - 3)), 285 horizontalalignment='center', verticalalignment='bottom') 286 287ax2 = fig.add_subplot(122) 288phic = np.arange(0., 2 * np.pi + 0.5, 0.1) 289r = 5. / (2 * np.pi) 290ax2.plot(r * np.cos(phic), r * np.sin(phic), 'k-', lw=2) 291phin = np.arange(0., 4.1, 1.) * 2 * np.pi / 5 292ax2.scatter(r * np.sin(phin), r * np.cos(phin), s=200, 293 c=[clist[0], clist[1], clist[2], clist[2], clist[1]]) 294ax2.set_xlim([-1.3, 1.3]) 295ax2.set_ylim([-1.2, 1.2]) 296ax2.set_aspect('equal', 'box') 297ax2.set_xticks([]) 298ax2.set_yticks([]) 299for j in range(5): 300 ax2.text(1.4 * r * np.sin(phin[j]), 1.4 * r * np.cos(phin[j]), 301 str('(%d,0)' % (j + 1 - 3)), 302 horizontalalignment='center', verticalalignment='center') 303 304plt.savefig('../user_manual_figures/player.png', bbox_inches='tight') 305 306# -------------------------------------------------- 307 308nest.ResetKernel() 309 310#{ layer6 #} 311layer1 = nest.Create('iaf_cond_alpha', 312 positions=nest.spatial.grid(shape=[2, 1])) 313layer2 = nest.Create('poisson_generator', 314 positions=nest.spatial.grid(shape=[2, 1])) 315#{ end #} 316 317print("#{ layer6 #}") 318nest.PrintNodes() 319print("#{ end #}") 320 321# -------------------------------------------------- 322 323nest.ResetKernel() 324 325#{ vislayer #} 326layer = nest.Create('iaf_psc_alpha', 327 positions=nest.spatial.grid(shape=[21, 21])) 328probability_param = nest.spatial_distributions.gaussian(nest.spatial.distance, std=0.15) 329conndict = {'rule': 'pairwise_bernoulli', 330 'p': probability_param, 331 'mask': {'circular': {'radius': 0.4}}} 332nest.Connect(layer, layer, conndict) 333fig = nest.PlotLayer(layer, nodesize=80) 334 335ctr = nest.FindCenterElement(layer) 336nest.PlotTargets(ctr, layer, fig=fig, 337 mask=conndict['mask'], probability_parameter=probability_param, 338 src_size=250, tgt_color='red', tgt_size=20, mask_color='red', 339 probability_cmap='Greens') 340#{ end #} 341plt.savefig('../user_manual_figures/vislayer.png', bbox_inches='tight') 342