Beispiel #1
0
    def test_create_overlapping_patches(self):
        self.reset()

        r_loc = 0.3
        p_loc = 0.5
        p_r = r_loc / 2.
        distance = .7
        num_patches = 2
        p_p = 0.3

        nc.create_overlapping_patches(
            self.torus_layer,
            r_loc=r_loc,
            p_loc=p_loc,
            distance=distance,
            num_patches=num_patches,
            p_p=p_p
        )

        anchors = [tu.to_coordinates(n * 360. / float(num_patches), distance) for n in range(1, num_patches + 1)]

        for s in self.torus_nodes:
            pos_s = tp.GetPosition([s])[0]
            patch_centers = (np.asarray(anchors) + np.asarray(pos_s)).tolist()
            conn = nest.GetConnections(source=[s])
            targets = [t for t in nest.GetStatus(conn, "target") if t in self.torus_nodes]
            for t in targets:
                d = tp.Distance([s], [t])[0]
                self.assertGreaterEqual(d, distance - p_r, "Established connection is too short")
                self.assertLessEqual(d, distance + p_r, "Established connection is too long")
                d_to_centers = list(tp.Distance(patch_centers, [t]))
                self.assertTrue(
                    np.any(np.asarray(d_to_centers) <= p_r),
                    "Node is too far from any patch center"
                )
Beispiel #2
0
    def _positions(self):
        '''Return positions of all nodes.'''

        return [
            tuple(pos)
            for pos in topo.GetPosition(nest.GetLeaves(self._lt)[0])
        ]
Beispiel #3
0
    def test_a_create_perlin_stimulus_map(self):
        self.reset()

        num_stimulus_discr = 4
        resolution = (15, 15)
        spacing = 0.1
        plot = False
        save_plot = False

        tuning_to_neuron, neuron_to_tuning, color_map = nc.create_perlin_stimulus_map(
            self.torus_layer,
            self.inh_nodes,
            num_stimulus_discr=num_stimulus_discr,
            resolution=resolution,
            spacing=spacing,
            plot=plot,
            save_plot=save_plot
        )

        for n in self.torus_nodes:
            if n not in self.torus_nodes:
                self.assertIn(n, tuning_to_neuron[neuron_to_tuning[n]], "Neuron is not assigned to the correct tuning list")
                p = tp.GetPosition([n])[0]
                x_grid, y_grid = tu.coordinates_to_cmap_index(self.size_layer, p, spacing)
                self.assertEqual(
                    color_map[x_grid, y_grid],
                    neuron_to_tuning[n],
                    "Color map and tuning preference doesn't match"
                )
Beispiel #4
0
    def test_create_torus_layer_with_jitter(self):
        self.reset()

        # Must be number that has a square root in N
        num_neurons = 144
        jitter = 0.01
        neuron_type = "iaf_psc_delta"
        layer_size = 3.

        layer = nc.create_torus_layer_with_jitter(
            num_neurons=num_neurons,
            jitter=jitter,
            neuron_type=neuron_type,
            layer_size=layer_size
        )

        nodes = nest.GetNodes(layer)[0]
        self.assertEqual(len(nodes), num_neurons, "Not the right number of nodes")

        model = nest.GetStatus(nodes, "model")
        for m in model:
            self.assertEqual(m, neuron_type, "Wrong neuron type set")

        mod_size = layer_size - jitter * 2
        step_size = mod_size / float(np.sqrt(num_neurons))
        coordinate_scale = np.arange(-mod_size / 2., mod_size / 2., step_size)
        grid = [[x, y] for y in coordinate_scale for x in coordinate_scale]

        for n in nodes:
            pos = np.asarray(tp.GetPosition([n])[0])
            sorted(grid, key=lambda l: np.linalg.norm(np.asarray(l) - pos))
            self.assertLessEqual(pos[0], np.abs(grid[0][0] + jitter), "Distance in x to original position too high")
            self.assertLessEqual(pos[1], np.abs(grid[0][1] + jitter), "Distance in y to original position too high")
def wd_fig(fig,
           loc,
           ldict,
           cdict,
           what,
           rpos=None,
           xlim=[-1, 51],
           ylim=[0, 1],
           xticks=range(0, 51, 5),
           yticks=np.arange(0., 1.1, 0.2),
           clr='blue',
           label=''):
    nest.ResetKernel()
    l = tp.CreateLayer(ldict)
    tp.ConnectLayers(l, l, cdict)

    ax = fig.add_subplot(loc)

    if rpos is None:
        rn = nest.GetLeaves(l)[0][:1]  # first node
    else:
        rn = tp.FindNearestElement(l, rpos)

    conns = nest.GetConnections(rn)
    cstat = nest.GetStatus(conns)
    vals = np.array([sd[what] for sd in cstat])
    tgts = [sd['target'] for sd in cstat]
    locs = np.array(tp.GetPosition(tgts))
    ax.plot(locs[:, 0], vals, 'o', mec='none', mfc=clr, label=label)
    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
    ax.set_xticks(xticks)
    ax.set_yticks(yticks)
Beispiel #6
0
def geometryFunction(topologyLayer):

    positions = topo.GetPosition(nest.GetLeaves(topologyLayer)[0])

    def geometry_function(idx):
        return positions[idx]

    return geometry_function
Beispiel #7
0
 def test_GetPosition(self):
     """Check if GetPosition returns proper positions."""
     pos = [[1.0, 0.0], [0.0, 1.0], [3.5, 1.5]]
     ldict = {
         'elements': 'iaf_neuron',
         'extent': [20., 20.],
         'positions': pos
     }
     nlayers = 2
     nest.ResetKernel()
     l = topo.CreateLayer((ldict, ) * nlayers)
     nodepos = [topo.GetPosition(node) for node in nest.GetLeaves(l)]
     self.assertEqual([pos] * nlayers, nodepos)
Beispiel #8
0
def check_distance():
    layer = tp.CreateLayer(l3d_specs)
    tp.ConnectLayers(layer, layer, conn_dict1)

    nodes = nest.GetChildren(layer)[0]

    positions = tp.GetPosition(nodes)
    target_positions = tp.GetTargetPositions(nodes, layer)

    deltas = filter(
        lambda x: x > 0.35,
        [[get_delta(positions[i], pos2) for pos2 in target_positions[i]]
         for i in range(len(positions))])
    print len(deltas)
Beispiel #9
0
    def setUp(self):
        self.num_neurons = 1500
        self.neuron_type = "iaf_psc_delta"
        self.rest_pot = -1.
        self.threshold_pot = 1e2
        self.time_const = 22.
        self.capacitance = 1e4

        self.num_stimulus_discr = 4
        self.size_layer = 2.

        self.input_stimulus = cs.image_with_spatial_correlation(size_img=(20, 20), radius=3, num_circles=80)
        self.organise_on_grid = True

        self.torus_layer, self.spike_det, self.multi = nc.create_torus_layer_uniform(
            num_neurons=self.num_neurons,
            neuron_type=self.neuron_type,
            rest_pot=self.rest_pot,
            threshold_pot=self.threshold_pot,
            time_const=self.time_const,
            capacitance=self.capacitance,
            size_layer=self.size_layer
        )
        self.torus_nodes = nest.GetNodes(self.torus_layer)[0]
        self.inh_nodes = np.random.choice(np.asarray(self.torus_nodes), self.num_neurons // 5, replace=False)
        self.min_id_torus = min(self.torus_nodes)
        self.torus_positions = tp.GetPosition(self.torus_nodes)
        self.torus_tree = KDTree(self.torus_positions)

        self.perlin_resolution = (15, 15)
        self.perlin_spacing = 0.1

        (self.tuning_to_neuron_map,
         self.neuron_to_tuning_map,
         self.tuning_weight_vector) = nc.create_perlin_stimulus_map(
            self.torus_layer,
            self.inh_nodes,
            num_stimulus_discr=self.num_stimulus_discr,
            resolution=self.perlin_resolution,
            spacing=self.perlin_spacing,
            plot=False,
            save_plot=False
        )

        self.retina = nc.create_input_current_generator(
            self.input_stimulus,
            organise_on_grid=self.organise_on_grid
        )

        self.receptors = nest.GetNodes(self.retina)[0]
Beispiel #10
0
    def test_GetPosition(self):
        """Check if GetPosition returns proper positions."""
        pos = ((1.0, 0.0), (0.0, 1.0), (3.5, 1.5))
        ldict = {'elements': 'iaf_psc_alpha',
                 'extent': (20., 20.),
                 'positions': pos}
        nlayers = 2
        nest.ResetKernel()
        l = topo.CreateLayer((ldict,) * nlayers)

        nodepos_ref = (pos,) * nlayers
        nodepos_exp = (topo.GetPosition(node) for node in nest.hl_api.GetLeaves(l))

        for npe, npr in zip(nodepos_exp, nodepos_ref):
            self.assertEqual(npe, npr)
def sort_nodes_space(nodes, axis=0):
    """
    Sorting the nodes with respect to space and in either x or y direction
    :param nodes: The nodes that need to be sorted
    :param axis: Integer for the axis. 0 is x axis, 1 is y axis
    :return: The sorted list of nodes, the sorted list of positions
    """
    pos = tp.GetPosition(nodes)
    nodes_pos = list(zip(nodes, pos))
    if axis == 0:
        nodes_pos.sort(key=lambda p: (p[1][0], p[1][1]))
    elif axis == 1:
        nodes_pos.sort(key=lambda p: (p[1][1], p[1][0]))
    nodes, pos = zip(*nodes_pos)
    return nodes, pos
Beispiel #12
0
    def test_create_connections_rf(self):
        self.reset()

        rf_size = (10, 10)
        synaptic_strength = 1.
        calc_error = True
        use_dc = False
        p_rf = 1.

        positions = tp.GetPosition(self.torus_nodes)
        rf_centers = [
            (
                (x / (self.size_layer / 2.)) * self.input_stimulus.shape[1] / 2.,
                (y / (self.size_layer / 2.)) * self.input_stimulus.shape[0] / 2.
            )
            for (x, y) in positions
        ]

        adj_mat, recon = nc.create_connections_rf(
            self.input_stimulus,
            self.torus_nodes,
            rf_centers,
            self.neuron_to_tuning_map,
            self.inh_nodes,
            rf_size=rf_size,
            p_rf=p_rf,
            synaptic_strength=synaptic_strength,
            total_num_target=self.num_neurons,
            target_layer_size=self.size_layer,
            calc_error=calc_error,
            use_dc=use_dc,
        )

        # Check for lower equal since there can be less neurons at the edges of the retina
        self.assertLessEqual(
            np.max((adj_mat > 0).sum(axis=0)),  # (adj_mat > 0) since it is technically no adj mat but a tranform matrix
            rf_size[0] * rf_size[1],
            "There are receptors to which no connections are established although they are in the receptive field"
        )

        img_vector = np.ones(self.input_stimulus.size + 1)
        img_vector[:-1] = self.input_stimulus.reshape(-1)
        img_vector[img_vector == 0] = np.finfo("float64").eps
        recon_transform = sr.direct_stimulus_reconstruction(img_vector.dot(adj_mat)[:-1], adj_mat)

        for res, exp in zip(recon.reshape(-1), recon_transform.reshape(-1)):
            self.assertAlmostEqual(res, exp, msg="The transformation through the feedforward matrix does not lead to a "
                                                 "similar reconstruction")
Beispiel #13
0
def geometryFunction(topologyLayer):
    """
    This factory returns a CSA-style geometry function for the given layer.

    The function returned will return for each CSA-index the position in
    space of the given neuron as a 2- or 3-element list.

    Note: This function stores a copy of the neuron positions internally,
          entailing memory overhead.
    """

    positions = topo.GetPosition(nest.GetLeaves(topologyLayer)[0])

    def geometry_function(idx):
        "Return position of neuron with given CSA-index."
        return positions[idx]

    return geometry_function
Beispiel #14
0
def recordElectrodeEnviromentLazy(network, folder):
    neurons_x_position_distance = 0.5/(network.parameters['Columns']/2.)
    neurons_x_position = [(i, 0.) for i in np.arange(0., 0.5, neurons_x_position_distance*2.)]
    #Add y-Positions
    neurons_x_position += ([(0., i) for i in np.arange(0., 0.5, neurons_x_position_distance*2.)])
    #Add diagonal
    neurons_x_position += ([(i, i) for i in np.arange(0., 0.5, neurons_x_position_distance*2.)])
    neurons_x_position = tp.FindNearestElement(network.l, neurons_x_position)
    neurons_position = tp.GetPosition(neurons_x_position)
    for position in neurons_position:
        plt.close()
        magic.recordElectrodeEnviroment(network.df_ex, position[0], position[1], 0.05, 0.05)
        plt.savefig(folder + '/excitatory_neurons/electrode_enviroment' + str(round(position[0], 4)) + ', ' + str(round(position[1], 4)) + '.png',
                    dpi=300)
        plt.close()
        magic.recordElectrodeEnviroment(network.df_in, position[0], position[1], 0.05, 0.05)
        plt.savefig(folder + '/inhibitory_neurons/electrode_enviroment' + str(round(position[0], 4)) + ', ' + str(round(position[1], 4)) + '.png',
                    dpi=300)
Beispiel #15
0
def plot_connections(ax, src, tgt, pops_list, red_conn_dens):

    # z-positions
    z0 = pops_list.index(src)
    z1 = z0 + (pops_list.index(tgt) - z0)

    # x,y-positions
    if src == 'STIM':
        xyloc = [0., 0.]
    elif src == tgt:
        xyloc = [0.8, 0.8]
    elif src == 'EX':
        xyloc = [0.8, -0.8]
    elif src == 'IN':
        xyloc = [-0.8, -0.8]

    srcid = tp.FindNearestElement(pops[src]['layer'], xyloc, False)
    srcloc = tp.GetPosition(srcid)[0]
    tgtsloc = np.array(tp.GetTargetPositions(srcid, pops[tgt]['layer'])[0])
    # targets do not get picked in the same order;
    # they are sorted here for reproducibility
    tgtsloc = tgtsloc[np.argsort(tgtsloc[:, 0])]
    tgtsloc_show = tgtsloc[0:len(tgtsloc):red_conn_dens]
    for tgtloc in tgtsloc_show:
        ax.plot([srcloc[0], tgtloc[0]], [srcloc[1], tgtloc[1]], [z0, z1],
                c=pops[src]['conn_color_light'],
                linewidth=1.)
        # highlight target
        ax.plot(xs=[tgtloc[0]],
                ys=[tgtloc[1]],
                zs=[z1],
                marker='o',
                markeredgecolor='none',
                markersize=2,
                color=pops[src]['conn_color_dark'],
                alpha=1.)

    # to be printed on top
    dots = [tgtsloc_show, z1, pops[src]['conn_color_dark'], 'none', 2]
    srcdot = [srcloc, z0, 'white', 'black', 3]
    return dots, srcdot
Beispiel #16
0
def makePandas(senderEvents, ctr):
    """
    Return a pandas DataFrame (Sender, Time, Position, Distance from ctr) for the recordings of a spike detector
    :param senderEvents: Recordings from spike detector
    :param ctr: Center Element(GID)
    :return: Pandas dataframe with Sender, Time, Position and Distance
    """
    senders = [int(i) for i in senderEvents['senders']]  #int necessary, because GID's for tp.GetPosition. Otherwise error
    times = [i for i in senderEvents['times']]
    positions = [i for i in tp.GetPosition(senders)]
    if len(senders) >= 2:
        distances = [i for i in tp.Distance(senders, [ctr])]
        return pd.DataFrame({'Sender': senders,
                             'Time': times, 'Distance from Center': distances,
                             'Position': positions
                             })
    else:
        return pd.DataFrame({'Sender': senders,
                             'Time': times,
                             'Position': positions
                             })
def plot_spikes_over_time(spikes_s,
                          time_s,
                          network,
                          t_start=0.,
                          t_end=1000.,
                          t_stim_start=[],
                          t_stim_end=[],
                          title="",
                          font_size=16,
                          save_plot=True,
                          save_prefix=""):
    """
    Plot neural activity over time
    :param spikes_s: The IDs of the neurons that spiked
    :param time_s: The respective times
    :param network: The netwokr
    :param t_start: Start of the simulation
    :param t_end: End of the simulation
    :param t_stim_start: Start times of input stimulation (excitation)
    :param t_stim_end: End times of input stimulation (excitation)
    :param title: Plot title
    :param font_size: Font size
    :param save_plot: If true, the plot is saved
    :param save_prefix: Prefix that is added to the saved file
    :return: None
    """
    plt.rcParams.update({"font.size": font_size})
    plt.figure(figsize=(10, 5))
    positions = np.asarray(tp.GetPosition(spikes_s.tolist()))
    plot_colorbar(plt.gcf(),
                  plt.gca(),
                  num_stim_classes=network.num_stim_discr)

    inh_mask = np.zeros(len(spikes_s)).astype('bool')
    for inh_n in network.torus_inh_nodes:
        inh_mask[spikes_s == inh_n] = True

    x_grid, y_grid = coordinates_to_cmap_index(network.layer_size,
                                               positions[~inh_mask],
                                               network.spacing_perlin)
    stim_classes = network.color_map[x_grid, y_grid]
    cl = np.full(len(spikes_s), -1)
    cl[~inh_mask] = stim_classes
    c = np.full(len(spikes_s), '#000000')
    c[~inh_mask] = np.asarray(list(
        mcolors.TABLEAU_COLORS.items()))[stim_classes, 1]
    sorted_zip = sorted(zip(time_s, spikes_s, c, cl), key=lambda l: l[3])
    sorted_time, sorted_spikes, sorted_c, _ = zip(*sorted_zip)
    new_idx_spikes = []
    new_idx_neurons = {}
    for s in sorted_spikes:
        new_idx_spikes.append(
            firing_rate_sorting(new_idx_spikes, sorted_spikes, new_idx_neurons,
                                s))
    plt.scatter(sorted_time, new_idx_spikes, s=1, c=list(sorted_c))

    plt.xlim(t_start, t_end)
    for st in t_stim_start:
        plt.axvline(x=st, c="red")
    for st in t_stim_end:
        plt.axvline(x=st, c="blue")

    plt.xlabel("Time", fontsize=font_size)
    plt.ylabel("Neuron", fontsize=font_size)
    plt.title(title)
    if save_plot:
        curr_dir = os.getcwd()
        Path(curr_dir + "/figures/firing_rate").mkdir(parents=True,
                                                      exist_ok=True)
        plt.savefig(curr_dir +
                    "/figures/firing_rate/%s_firing_time.png" % save_prefix)
        plt.close()
    else:
        plt.show()
Beispiel #18
0
    random.uniform(-0.5, 0.5)
] for j in range(1000)]

l1 = topo.CreateLayer({
    'extent': [1.5, 1.5, 1.5],  # must specify 3d extent AND center
    'center': [0., 0., 0.],
    'positions': pos,
    'elements': 'iaf_neuron'
})

# visualize
#xext, yext = nest.GetStatus(l1, 'topology')[0]['extent']
#xctr, yctr = nest.GetStatus(l1, 'topology')[0]['center']

# extract position information, transpose to list of x, y and z positions
xpos, ypos, zpos = zip(*topo.GetPosition(nest.GetChildren(l1)[0]))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xpos, ypos, zpos, s=15, facecolor='b', edgecolor='none')

# Gaussian connections in full volume [-0.75,0.75]**3
topo.ConnectLayers(
    l1, l1, {
        'connection_type': 'divergent',
        'allow_autapses': False,
        'mask': {
            'volume': {
                'lower_left': [-0.75, -0.75, -0.75],
                'upper_right': [0.75, 0.75, 0.75]
            }
        },
Beispiel #19
0
        },
        'delays': 1.0
    })

pylab.clf()

# plot sources of neurons in different grid locations
for tgt_pos in [[15, 15], [0, 0]]:
    # obtain node id for center
    tgt = topo.GetElement(b, tgt_pos)

    # obtain list of outgoing connections for ctr
    # int() required to cast numpy.int64
    spos = tuple(
        zip(*[
            topo.GetPosition([int(conn[0])])[0]
            for conn in nest.GetConnections(target=tgt)
        ]))

    # scatter-plot
    pylab.scatter(spos[0], spos[1], 20, zorder=10)

    # mark sender position with transparent red circle
    ctrpos = pylab.array(topo.GetPosition(tgt)[0])
    pylab.gca().add_patch(
        pylab.Circle(ctrpos,
                     radius=0.1,
                     zorder=99,
                     fc='r',
                     alpha=0.4,
                     ec='none'))
#! Retina
#! ------
layerProps.update({'elements': 'RetinaNode'})
retina = topo.CreateLayer(layerProps)

# Original: Gabor retina input

if Params['lambda_dg'] >= 0:
    #! Now set phases of retinal oscillators; we use a list comprehension instead
    #! of a loop.
    [
        nest.SetStatus(
            [n], {
                "phase":
                phaseInit(
                    topo.GetPosition([n])[0], Params["lambda_dg"],
                    Params["phi_dg"])
            }) for n in nest.GetLeaves(retina)[0]
    ]
else:
    # Leonardo: Random retina input

    [
        nest.SetStatus(
            [n], {
                "phase":
                phaseInit(
                    topo.GetPosition([n])[0], np.pi * np.random.rand(),
                    np.pi * np.random.rand())
            }) for n in nest.GetLeaves(retina)[0]
    ]
def simulation(Params):

    root_folder = Params['root_folder']
    if not os.path.isdir(root_folder):
        os.makedirs(root_folder)

    net_folder = root_folder + Params['net_folder']
    if not os.path.isdir(net_folder):
        os.makedirs(net_folder)

    data_folder = net_folder + Params['data_folder']
    if not os.path.isdir(data_folder):
        os.makedirs(data_folder)

    #! =================
    #! Import network
    #! =================

    # NEST Kernel and Network settings
    nest.ResetKernel()
    nest.ResetNetwork()
    nest.SetKernelStatus({"local_num_threads": Params['threads'],'resolution': Params['resolution']})
    nest.SetStatus([0],{'print_time': True})

    # initialize random seed
    import time
    # msd = int(round(time.time() * 1000))
    msd = 42
    nest.SetKernelStatus({'grng_seed' : msd})
    # Tom debug
    # nest.SetKernelStatus({'rng_seeds' : range(msd+Params['threads']+1, msd+2*Params['threads']+1)})
    nest.SetKernelStatus({'rng_seeds': range(msd + Params['threads'] + 1, msd + 2 * Params['threads'] + 1)})

    import importlib
    network = importlib.import_module(Params['network'])
    reload(network)
    models, layers, conns  = network.get_Network(Params)
    #import network_full_keiko
    #reload(network_full_keiko)
    # models, layers, conns  = network_full_keiko.get_Network(Params)

    # Create models
    print('Creating models...', end="")
    for m in models:
        print('.', end="")
#        print(m), print('\n')
        nest.CopyModel(m[0], m[1], m[2])
    print(' done.')

    # Create layers, store layer info in Python variable
    print('Creating layers...', end="")
    for l in layers:
        print('.', end="")
        exec '%s = tp.CreateLayer(l[1])' % l[0] in globals(), locals()
    print(' done.')

    # Create connections, need to insert variable names
    print('Creating connectiions...', end="")
    for c in conns:
        print('.', end="")
        eval('tp.ConnectLayers(%s,%s,c[2])' % (c[0], c[1]))
    print(' done.')

    # Check connections
    if Params.has_key('show_center_connectivity_figure') and Params['show_center_connectivity_figure']:

        # this code only gives one element, and you have no control over the
        # model it comes from...
        # tp.PlotTargets(tp.FindCenterElement(Vp_horizontal), Vp_horizontal, 'L56_exc', 'AMPA_syn')
        # tp.PlotTargets(tp.FindCenterElement(Vp_horizontal), Vp_vertical, 'L56_exc', 'AMPA_syn')
        # tp.PlotTargets(tp.FindCenterElement(Vp_vertical), Vp_horizontal, 'L56_exc', 'AMPA_syn')
        # tp.PlotTargets(tp.FindCenterElement(Vp_vertical), Vp_vertical, 'L56_exc', 'AMPA_syn')

        # this way I can get all nodes in the positions, and later filter per Model using GetStatus
        # IMPORTANT: when layer has even number of neurons per line/column
        #            using center (0,0) makes the function return 4 neurons
        #            per postiion since they all have the same distance.
        #            Using (-0,1, 0,1) solves this problem.
        #            When using odd number perhaps this has to change...

        for src_label in ['Tp_layer', 'Vp_vertical', 'Vp_horizontal']:
            for tgt_label in ['Tp_layer', 'Vp_vertical', 'Vp_horizontal']:

                if src_label == 'Tp_layer' and src_label == tgt_label:
                    continue

                print('source population %s' % src_label)
                print('target population %s' % tgt_label)
                n_plot = 0
                f = plt.figure()
                f.canvas.set_window_title('AMPA Connections: %s -> %s' % (src_label, tgt_label))
                all_center = eval('tp.FindNearestElement(%s, (-0.1, 0.1), True)[0]' % src_label)
                for n in all_center:
                    s = nest.GetStatus([n])
                    p = tp.GetPosition([n])
                    # print('%s : (%2.2f, %2.2f)' % (s[0]['model'], p[0][0], p[0][1]))
                    m1 = s[0]['model'].name
                    print('Source neuron model %s' % m1)
                    if m1.find('_exc') > -1:
                        if src_label.find('Tp_') > -1:
                            print( 'found Tp_')
                            # target has to be one of Vp_ or Vs_ models
                            target_models = ['L23_exc', 'L4_exc', 'L56_exc']
                        elif src_label.find('Vp_') > -1 or src_label.find('Vs') > -1:
                            # if target is Vp_ of Vs_ too, then one of those models
                            if tgt_label.find('Vp_') > -1:
                                target_models = ['L23_exc', 'L4_exc', 'L56_exc']
                            elif tgt_label.find('Tp_') > -1:
                                # otherwise it has to be a Tp_target
                                target_models = ['Tp_exc']
                            else:
                                raise ValueError('Invalide target %s for source model: %s' % (tgt_label, src_label))
                        else:
                            raise ValueError('Invalide source model: %s' % (src_label))

                        for m2 in target_models:
                            print('Target neuron model %s' % m2)
                            try:
                                get_targets_command = 'tp.GetTargetNodes([n], %s, m2, "AMPA_syn")[0]' % tgt_label
                                print(get_targets_command)
                                targets = eval(get_targets_command)
                                if len(targets) > 0:
                                    n_plot += 1
                                    f.add_subplot(3,5,n_plot)
                                    eval('tp.PlotTargets([n], %s, m2, "AMPA_syn", f)' % tgt_label)
                                    plt.title('%s -> %s' % (m1, m2), fontsize=9)
                            except:
                                print('didnt work')

                f.savefig(data_folder + '/connectivity_%s_%s.png' % (src_label, tgt_label), dpi=100)


    if Params.has_key('dry_run') and Params['dry_run']:
        print('Only generation, loading and ploting network. No actual simulation done.')
        return

    '''
    # pablo
    # Create vertical grating
    for n in nest.GetLeaves(Retina_layer)[0]:
            retina_0 = (nest.GetLeaves(Retina_layer)[0])[0]
            col = (n-retina_0)/Params['Np']

            cells_per_degree = Params['Np']/Params['visSize']
            cells_per_cycle = cells_per_degree/Params['spatial_frequency']

            nest.SetStatus([n], { "phase": col * 360/(cells_per_cycle-1) })
    '''
    ### keiko
    [nest.SetStatus([n], {"phase": phaseInit(tp.GetPosition([n])[0],
                                             Params["lambda_dg"],
                                             Params["phi_dg"])})
    for n in nest.GetLeaves(Retina_layer)[0]]

    # --------------------------------------------------------------------#
    # ---------- SET IB NEURONS ----------------------------------------- #
    # --------------------------------------------------------------------#

    # 30% of Cortex L56 excitatory neurons are intrinsically bursting(IB) neuron.
    # That is achieved by setting pacemaker current I_h.
    # So select 30% of L56_exc neuron, and change h_g_peak from 0.0 to 1.0.
    # (Other cortical neuron do not have I_h, thus h_g_peak=0.0)

    L56_vertical_idx = [nd for nd in nest.GetLeaves(Vp_vertical)[0] if nest.GetStatus([nd], 'model')[0]=='L56_exc']
    L56_horizontal_idx = [nd for nd in nest.GetLeaves(Vp_horizontal)[0] if nest.GetStatus([nd], 'model')[0]=='L56_exc']

    num_neuron = len(L56_vertical_idx)
    num_ib = int(num_neuron*0.3)

    ridx_vertical = np.random.randint(num_neuron, size=(1,num_ib))[0]
    ridx_horizontal = np.random.randint(num_neuron, size=(1,num_ib))[0]

    for i in range(1,num_ib,1):
        nest.SetStatus([L56_vertical_idx[ridx_vertical[i]]], {'g_peak_h': 1.0})
        nest.SetStatus([L56_horizontal_idx[ridx_horizontal[i]]], {'g_peak_h': 1.0})



    # initiate network activity
    #nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'rate': Params['ret_rate']})
    nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'rate': Params['ret_rate']})
    nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'amplitude': 0.0})
    nest.Simulate(500.0)


    #! =================
    #! Recording devices
    #! =================
    print('Connecting recorders...', end="")
    nest.CopyModel('multimeter', 'RecordingNode',
            params = {'interval'   : Params['resolution'],
            'record_from': ['V_m'],
                            # Put back when plotting synaptic currents, otherwise makes everything slower for no reason
                            # 'I_syn_AMPA',
                            # 'I_syn_NMDA',
                            # 'I_syn_GABA_A',
                            # 'I_syn_GABA_B',
                            # 'g_AMPA',
                            # 'g_NMDA',
                            # 'g_GABA_A',
                            # 'g_GABA_B'],
                            #'I_NaP',
                            #'I_KNa',
                            #'I_T',
                            #'I_h'
                            #]
            'record_to'  : ['memory'],
            'withgid'    : True,
            'withtime'   : True})

    recorders = []
    '''
    for population, model in [(Retina_layer, 'Retina'),
                              (Tp_layer  , 'Tp_exc'),
                              (Tp_layer  , 'Tp_inh'),
                              (Rp_layer  , 'Rp'),
                              (Vp_vertical, 'L23_exc'),
                              (Vp_horizontal, 'L23_exc'),
                              (Vp_vertical, 'L23_inh'),
                              (Vp_horizontal, 'L23_inh'),
                              (Vp_vertical, 'L4_exc'),
                              (Vp_horizontal, 'L4_exc'),
                              (Vp_vertical, 'L4_inh'),
                              (Vp_horizontal, 'L4_inh'),
                              (Vp_vertical, 'L56_exc'),
                              (Vp_horizontal, 'L56_exc'),
                              (Vp_vertical, 'L56_inh'),
                              (Vp_horizontal, 'L56_inh'),
                              (Vs_vertical, 'L23_exc'),
                              (Vs_horizontal, 'L23_exc'),
                              (Vs_vertical, 'L23_inh'),
                              (Vs_horizontal, 'L23_inh'),
                              (Vs_vertical, 'L4_exc'),
                              (Vs_horizontal, 'L4_exc'),
                              (Vs_vertical, 'L4_inh'),
                              (Vs_horizontal, 'L4_inh'),
                              (Vs_vertical, 'L56_exc'),
                              (Vs_horizontal, 'L56_exc'),
                              (Vs_vertical, 'L56_inh'),
                              (Vs_horizontal, 'L56_inh'),
                              (Vs_cross, 'L23_exc'),
                              (Vs_cross, 'L4_exc'),
                              (Vs_cross, 'L4_exc'),


                              ]:
    '''


    for population, model in [(Retina_layer, 'Retina'),
                              (Tp_layer  , 'Tp_exc'),
                              (Rp_layer  , 'Rp'),
                              (Vp_vertical, 'L23_exc'),
                              (Vp_horizontal, 'L23_exc'),
                              (Vp_vertical, 'L23_inh'),
                              (Vp_vertical, 'L4_exc'),
                              (Vp_horizontal, 'L4_exc'),
                              (Vp_vertical, 'L4_inh'),
                              (Vp_vertical, 'L56_exc'),
                              (Vp_horizontal, 'L56_exc'),
                              (Vs_vertical, 'L23_exc'),
                              (Vs_horizontal, 'L23_exc'),
                              (Vs_vertical, 'L4_exc'),
                              (Vs_horizontal, 'L4_exc'),
                              (Vs_vertical, 'L56_exc'),
                              (Vs_horizontal, 'L56_exc'),
                              (Vs_cross, 'L23_exc'),
                              (Vs_cross, 'L4_exc'),
                              (Vs_cross, 'L56_exc'),


                              ]:


        print('.', end="")
        rec = nest.Create('RecordingNode')
        recorders.append([rec,population,model])
        if (model=='Retina'):
            nest.SetStatus(rec,{'record_from': ['rate']})
        tgts = [nd for nd in nest.GetLeaves(population)[0] if nest.GetStatus([nd], 'model')[0]==model]
        #nest.Connect(rec, tgts, None, {'delay': recorders_delay})
        nest.Connect(rec, tgts)
    print('done.')

    #! =================
    #! Spike detector
    #! =================
    print('Connecting detectors...', end="")
    detectors = []
    '''
    for population, model in [(Retina_layer, 'Retina'),
                              (Tp_layer  , 'Tp_exc'),
                              (Tp_layer  , 'Tp_inh'),
                              (Rp_layer  , 'Rp'),
                              (Vp_vertical, 'L23_exc'),
                              (Vp_horizontal, 'L23_exc'),
                              (Vp_vertical, 'L23_inh'),
                              (Vp_horizontal, 'L23_inh'),
                              (Vp_vertical, 'L4_exc'),
                              (Vp_horizontal, 'L4_exc'),
                              (Vp_vertical, 'L4_inh'),
                              (Vp_horizontal, 'L4_inh'),
                              (Vp_vertical, 'L56_exc'),
                              (Vp_horizontal, 'L56_exc'),
                              (Vp_vertical, 'L56_inh'),
                              (Vp_horizontal, 'L56_inh'),
                              (Vs_vertical, 'L23_exc'),
                              (Vs_horizontal, 'L23_exc'),
                              (Vs_vertical, 'L23_inh'),
                              (Vs_horizontal, 'L23_inh'),
                              (Vs_vertical, 'L4_exc'),
                              (Vs_horizontal, 'L4_exc'),
                              (Vs_vertical, 'L4_inh'),
                              (Vs_horizontal, 'L4_inh'),
                              (Vs_vertical, 'L56_exc'),
                              (Vs_horizontal, 'L56_exc'),
                              (Vs_vertical, 'L56_inh'),
                              (Vs_horizontal, 'L56_inh')]:
        '''


    #Tom
    for population, model in [(Retina_layer, 'Retina'),
                              (Tp_layer, 'Tp_exc'),
                              (Tp_layer, 'Tp_inh'),
                              (Vp_vertical, 'L23_exc'),
                              (Vp_vertical, 'L4_exc'),
                              (Vp_vertical, 'L56_exc'),
                              (Vp_vertical, 'L23_inh'),
                              (Vp_vertical, 'L4_inh'),
                              (Vp_vertical, 'L56_inh'),
                              (Vs_vertical, 'L23_exc'),
                              (Vs_vertical, 'L23_inh'),
                              (Vs_vertical, 'L4_exc'),
                              (Vs_vertical, 'L4_inh'),
                              (Vs_vertical, 'L56_exc'),
                              (Vs_vertical, 'L56_inh')]:



        print('.', end="")
        rec = nest.Create('spike_detector', params={"withgid": True, "withtime": True})
        #rec = nest.Create('spike_detector')
        detectors.append([rec,population,model])
        tgts = [nd for nd in nest.GetLeaves(population)[0] if nest.GetStatus([nd], 'model')[0]==model]
        if model == 'Retina':
            for t in tgts:
                try:
                    # nest.Connect([t], rec, None, {'delay':  recorders_delay})
                    nest.Connect([t], rec)
                    print('connected %d' % t)
                except:
                    print('%d did not work' % t)
        else:
            # nest.Connect(tgts, rec, None, {'delay': recorders_delay})
            nest.Connect(tgts, rec)
    print('done.')


    #! ====================
    #! Simulation
    #! ====================
    '''
    # change gKL to 0.8 in all populations (necessary to get a little stronger evoked response)
    for l in layers:
            sim_elements = l[1]['elements']
            for m in np.arange(0,np.size(sim_elements),1):

                    if(np.size(sim_elements)==1):
                        sim_model = sim_elements
                    else:
                        sim_model = sim_elements[m]

                    exec("la = %s" % l[0])
                    pop = [nd for nd in nest.GetLeaves(la)[0] if nest.GetStatus([nd], 'model')[0]==sim_model]
                    if (l[0]!='Retina_layer'):
                            for cell in pop:
                                    nest.SetStatus([cell], {'g_KL':0.8})
    '''
    # Simulate
    for t in Params['intervals']:

        #if (t == 250.0):  # Stimulus ON
        #    # if (t == 1500.0):  # Stimulus ON
        #    nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'amplitude': 45.0})
        #else:  # Stimulus OFF
        #    nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'amplitude': 0.0})

        if Params['input_flag']==True:
            nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'amplitude': Params['ret_rate']})
        else:
            nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'amplitude': 0.0})

        nest.Simulate(t)



    #! ====================
    #! Plot Results
    #! ====================

    print("Creating figure 3...")

    rows = 9
    cols = 2

    fig = plt.figure(num=None, figsize=(13, 24), dpi=100)
    fig.subplots_adjust(hspace=0.4)

    # Plot A: membrane potential rasters

    recorded_models = [(Retina_layer,'Retina'),
                        (Vp_vertical,'L23_exc'),
                        (Vp_vertical,'L4_exc'),
                        (Vp_vertical,'L56_exc'),
                        (Rp_layer,'Rp'),
                        (Tp_layer,'Tp_exc')]

    #plotting.potential_raster(fig,recorders,recorded_models,0,Params['Np'],np.sum(Params['intervals']),Params['resolution'],rows,cols,0)
    plotting.potential_raster(fig,recorders,recorded_models,0,Params['Np'],
                              np.sum(Params['intervals']),
                              Params['resolution'],
                              rows,cols,0)
    #starting_neuron = 800+1
    #plotting.potential_raster(fig,recorders,recorded_models,starting_neuron,Params['Np'],np.sum(Params['intervals']),Params['resolution'],rows,cols,0)

    plt.title('Evoked')

    # Plot B: individual intracellular traces

    recorded_models =[(Vp_vertical,'L4_exc'),
                      (Vp_vertical,'L4_inh')]

    #plotting.intracellular_potentials(fig, recorders, recorded_models, 21, rows, cols, 6) #original
    # keiko
    total_time = 0.0
    for t in Params['intervals']:
        total_time += t

    #draw_neuron = (Params['Np']*Params['Np']/2)
    #plotting.intracellular_potentials(fig, recorders, recorded_models, draw_neuron, rows, cols, 6, total_time)
    plotting.intracellular_potentials(fig, recorders, recorded_models, 21, rows, cols, 6, total_time)
    #plotting.intracellular_potentials(fig, recorders, recorded_models, 820, rows, cols, 6, total_time)

    # Plot C: topographical activity of the vertical and horizontal layers


    if Params.has_key('start_membrane_potential') and  Params.has_key('end_membrane_potential'):
        start = Params['start_membrane_potential']
        stop = Params['end_membrane_potential']
    else:
        start = 130.0
        stop = 140.0

    recorded_models = [(Vp_vertical,'L23_exc')]
    labels = ["Vertical"]
    plotting.topographic_representation(fig,
                                        recorders,
                                        recorded_models,
                                        labels,
                                        Params['Np'],
                                        np.sum(Params['intervals']),
                                        Params['resolution'],
                                        rows,
                                        cols,
                                        start,
                                        stop,
                                        8,
                                        0)

    recorded_models = [(Vp_horizontal,'L23_exc')]

    labels = ["Horizontal"]

    plotting.topographic_representation(fig,recorders,recorded_models,labels,Params['Np'],np.sum(Params['intervals']),Params['resolution'],rows,cols,start,stop,8,1)

    if Params.has_key('figure_title'):
        fig.suptitle(Params['figure_title'], size = 20)

    fig.savefig(data_folder + '/figure3.png', dpi=100)

    if Params.has_key('show_main_figure') and Params['show_main_figure']:
        plt.show()


    # Plot D: movie

    #labels = ["Evoked_Vp_L23_Vertical","Evoked_Vp_L23_Horizontal"]
    #recorded_models = [(Vp_vertical,'L23_exc'),(Vp_horizontal,'L23_exc')]
    #plotting.makeMovie(fig,recorders,recorded_models,labels,Params['Np'],np.sum(Params['intervals']),Params['resolution'])



    # Plot F: All areas


    if Params.has_key('plot_all_regions') and Params['plot_all_regions']:

        print("Creating plot of all cortical areas")

        #First column
        vertical_models = [(Retina_layer,'Retina'),
                        (Vp_vertical,'L23_exc'),
                        (Vp_vertical,'L4_exc'),
                        (Vp_vertical,'L56_exc'),
                        (Vs_vertical,'L23_exc'),
                        (Vs_vertical, 'L4_exc'),
                        (Vs_vertical, 'L56_exc')]
        #Second column
        horizontal_models = [(Retina_layer,'Retina'),
                        (Vp_horizontal,'L23_exc'),
                        (Vp_horizontal,'L4_exc'),
                        (Vp_horizontal,'L56_exc'),
                        (Vs_horizontal,'L23_exc'),
                        (Vs_horizontal, 'L4_exc'),
                        (Vs_horizontal, 'L56_exc')]
        #Third column
        cross_models = [(Retina_layer,'Retina'),
                        (),
                        (),
                        (),
                        (Vs_cross,'L23_exc'),
                        (Vs_cross, 'L4_exc'),
                        (Vs_cross, 'L56_exc')]

        #Column labels
        labels = ['Vertical', 'Horizontal', 'Cross']

        #Row labels
        areas = ['Retina', 'Primary\nL2/3', 'Primary\nL4', 'Primary\nL5/6', 'Secondary\nL2/3', 'Secondary\nL4', 'Secondary\nL4/6']

        plotcols = [vertical_models, horizontal_models, cross_models]


        fig = plotting.potential_raster_multiple_models(fig, recorders, plotcols, labels, areas, 0, Params['Np'],
                                                  np.sum(Params['intervals']),
                                                  Params['resolution'],
                                                  0)

        fig.savefig(data_folder + '/figure_all_areas.png', dpi=100)







    #! ====================
    #! Save Results
    #! ====================

    print('save recorders data')
    # Set folder
    #rootdir = '/home/kfujii2/newNEST2/ht_model_pablo_based/data/'
    #expdir = 'random/'
    # expdir = 'random_full/'
    # expdir = 'structured_full/'
    # data_folder = rootdir + expdir

    # To save spike data, set pairs of population id and its name
    population_name = [ {'population': Retina_layer, 'name': 'Retina'},
                        {'population': Vp_vertical, 'name': 'Vp_v'},
                        {'population': Vp_horizontal, 'name': 'Vp_h'},
                        {'population': Rp_layer, 'name': 'Rp'},
                        {'population': Tp_layer, 'name': 'Tp'},
                        {'population': Vs_vertical, 'name': 'Vs_v'},
                        {'population': Vs_horizontal, 'name': 'Vs_h'}]

    if Params.has_key('save_recorders') and Params['save_recorders']:
        for rec, population, model in recorders:

            # Get name of population
            for p in range(0, len(population_name), 1):
                if population_name[p]['population'] == population:
                    p_name = population_name[p]['name']

            data = nest.GetStatus(rec)[0]['events']

            if model == 'Retina':
                scipy.io.savemat(data_folder + '/recorder_' + p_name + '_' + model + '.mat',
                                 mdict={'senders': data['senders'],
                                        'rate': data['rate']})
            else:
                scipy.io.savemat(data_folder + '/recorder_' + p_name + '_' + model + '.mat',
                                 mdict={'senders': data['senders'],
                                        'times': data['times'],
                                        'V_m': data['V_m'] #,
                                        #'I_syn_AMPA': data['I_syn_AMPA'],
                                        #'I_syn_NMDA': data['I_syn_NMDA'],
                                        #'I_syn_GABA_A': data['I_syn_GABA_A'],
                                        #'I_syn_GABA_B': data['I_syn_GABA_B'],
                                        # 'g_AMPA': data['g_AMPA'],
                                        # 'g_NMDA': data['g_NMDA'],
                                        # 'g_GABA_A': data['g_GABA_A'],
                                        # 'g_GABA_B': data['g_GABA_B']
                                        } )



    print('save raster images')
    plt.close()
    for rec, population, model in detectors:
        spikes = nest.GetStatus(rec, 'events')[0]

        # Get name of population
        for p in range(0, len(population_name), 1):
            if population_name[p]['population'] == population:
                p_name = population_name[p]['name']

        if len(nest.GetStatus(rec)[0]['events']['senders']) > 3:
            raster = raster_plot.from_device(rec, hist=True)
            pylab.title( p_name + '_' + model )
            f = raster[0].figure
            f.set_size_inches(15, 9)
            f.savefig(data_folder + '/spikes_' + p_name + '_' + model + '.png', dpi=100)
            plt.close()

            # Set filename and save spike data
            filename = data_folder + '/spike_' + p_name + '_' + model + '.pickle'
            pickle.dump(spikes, open(filename, 'w'))
            scipy.io.savemat(data_folder + '/spike_' + p_name + '_' + model + '.mat', mdict={'senders': spikes['senders'], 'times': spikes['times']})

            '''
            filename_AMPA = data_folder + 'connection_' + p_name + '_AMPA_syn' + '.dat'
            filename_NMDA = data_folder + 'connection_' + p_name + '_NMDA_syn' + '.dat'
            filename_GABAA = data_folder + 'connection_' + p_name + '_GABA_A_syn' + '.dat'
            filename_GABAB = data_folder + 'connection_' + p_name + '_GABA_B_syn' + '.dat'
            tp.DumpLayerConnections(population, 'AMPA_syn', filename_AMPA)
            tp.DumpLayerConnections(population, 'NMDA_syn', filename_NMDA)
            tp.DumpLayerConnections(population, 'GABA_A_syn', filename_GABAA)
            tp.DumpLayerConnections(population, 'GABA_B_syn', filename_GABAB)
            '''
    '''
    for p in range(0, len(population_name), 1):

        population = population_name[p]['population']
        p_name = population_name[p]['name']
        filename_nodes = data_folder + '/gid_' + p_name + '.dat'

        tp.DumpLayerNodes(population, filename_nodes)
    '''

    network_script = Params['network'] + '.py'
    shutil.copy2(network_script, data_folder + '/' + network_script)

    print('end')
              'columns'  : Params['N'],
              'extent'   : [Params['visSize'], Params['visSize']],
              'edge_wrap': True}
#! This dictionary does not yet specify the elements to put into the
#! layer, since they will differ from layer to layer. We will add them
#! below by updating the ``'elements'`` dictionary entry for each
#! population.

#! Retina
#! ------
layerProps.update({'elements': 'RetinaNode'})
retina = topo.CreateLayer(layerProps)

#! Now set phases of retinal oscillators; we use a list comprehension instead
#! of a loop.
[nest.SetStatus([n], {"phase": phaseInit(topo.GetPosition([n])[0], 
                                         Params["lambda_dg"],
                                         Params["phi_dg"])})
 for n in nest.GetLeaves(retina)[0]]

#! Thalamus
#! --------

#! We first introduce specific neuron models for the thalamic relay
#! cells and interneurons. These have identical properties, but by
#! treating them as different models, we can address them specifically
#! when building connections.
#!
#! We use a list comprehension to do the model copies.
[nest.CopyModel('ThalamicNeuron', SpecificModel) for SpecificModel in ('TpRelay', 'TpInter')]
def simulation(Params):

    root_folder = Params['root_folder']
    if not os.path.isdir(root_folder):
        os.makedirs(root_folder)

    net_folder = root_folder + Params['net_folder']
    if not os.path.isdir(net_folder):
        os.makedirs(net_folder)

    conn_folder = net_folder + '/connections'
    if not os.path.isdir(conn_folder):
        os.makedirs(conn_folder)

    data_folder = net_folder + Params['data_folder']
    if not os.path.isdir(data_folder):
        os.makedirs(data_folder)

    #! =================
    #! Import network
    #! =================

    # NEST Kernel and Network settings
    nest.ResetKernel()
    nest.ResetNetwork()
    nest.SetKernelStatus({"local_num_threads": Params['threads'],'resolution': Params['resolution']})
    nest.SetStatus([0],{'print_time': True})

    # initialize random seed
    import time
    msd = int(round(time.time() * 1000))
    nest.SetKernelStatus({'grng_seed' : msd})
    # Tom debug
    # nest.SetKernelStatus({'rng_seeds' : range(msd+Params['threads']+1, msd+2*Params['threads']+1)})
    nest.SetKernelStatus({'rng_seeds': range(msd + Params['threads'] + 1, msd + 2 * Params['threads'] + 1)})

    import importlib
    network = importlib.import_module(Params['network'])
    reload(network)
    models, layers, conns  = network.get_Network(Params)
    #import network_full_keiko
    #reload(network_full_keiko)
    # models, layers, conns  = network_full_keiko.get_Network(Params)

    # TODO this should come from network file
    synapse_models = ['AMPA_syn', 'NMDA_syn', 'GABA_A_syn', 'GABA_B_syn', 'reticular_projection']
    # synapse_models = ['AMPA_syn', 'NMDA_syn', 'GABA_A_syn', 'GABA_B_syn']

    # Create models
    print('Creating models...', end="")
    for m in models:
        print('.', end="")
#        print(m), print('\n')
        nest.CopyModel(m[0], m[1], m[2])
    print(' done.')

    # Create layers, store layer info in Python variable
    print('Creating layers...', end="")
    for l in layers:
        print('.', end="")
        exec '%s = tp.CreateLayer(l[1])' % l[0] in globals(), locals()
    print(' done.')

    # now either create brand new connections, or load connectivity files from
    # connections_file
    connections_file =  conn_folder + '/connections.pickle'

    # assume alread runned before, and it is going to load connections and layers
    if Params.has_key('load_connections') and Params['load_connections']:

        if not os.path.isfile(connections_file):
            raise IOError('File with connections was never created: %s' % connections_file)

        print('Loading connections...', end="")
        with open(connections_file) as f:
            (pre, post_intact, post_scrambled, w, d, syn) = pickle.load(f)
        print(' done.')

        if Params['scrambled']:
            post = post_scrambled
        else:
            post = post_intact

        print('Connecting neurons...', end="")
        con_dict = {'rule': 'one_to_one'}
        for (pi, ti, wi, di, syni) in zip(pre, post, w, d, syn):
            print('.', end="")
            # print(syni)

            syn_dict = {"model": syni,
                        'weight': wi,
                        'delay': di
                        }
            nest.Connect(pi, ti, con_dict, syn_dict)
        print(' done.')

        # print('Connection retina...', end="")
        # # Create connections, need to insert variable names
        # for c in conns:
        #     if c[0]=='Retina_layer':
        #         eval('tp.ConnectLayers(%s,%s,c[2])' % (c[0], c[1]))
        # print('done.')

    else:
        # Create connections, need to insert variable names
        print('Creating connections...', end="")
        for c in conns:
            if Params['Np'] < 40:
                print('%s -> %s' % (c[0], c[1]))
                c[2]['allow_oversized_mask'] = True
            else:
                print('.', end="")
            eval('tp.ConnectLayers(%s,%s,c[2])' % (c[0], c[1]))
        print(' done.')

    if Params.has_key('dump_connections') and Params['dump_connections']:

        import glob

        # First dump all nodes and connections to files
        for l in layers:
            print('dumping connections for %s' % l[0])
            eval("tp.DumpLayerNodes(%s, conn_folder + '/nodes_%s.dump')" % (l[0], l[0]) )
            for synapse in synapse_models:
                 eval("tp.DumpLayerConnections(%s, '%s', conn_folder + '/connections_%s_%s.dump')" % (l[0], synapse, l[0], synapse) )

        # Now load all nodes, and mark which physical layer they belong to
        all_nodes_files = glob.glob(conn_folder + "/nodes_*.dump")
        if len(all_nodes_files) < 1:
            raise ValueError('No files save for this configuration. First '
                             'run with load_connection = False')

        pop_nodes = [None] * len(all_nodes_files)
        pop_names = [None] * len(all_nodes_files)
        perm_pop_nodes = [None] * len(all_nodes_files)
        for (idx, pop_file) in enumerate(all_nodes_files):

            population = pop_file[pop_file.find('nodes_')+6:-5]
            pop_names[idx] = population
            print('Loading nodes for population %s' % population)
            pop_nodes[idx] = np.loadtxt(pop_file)

            # identify physical layers
            n_layer = 0
            n_nodes = pop_nodes[idx].shape[0]
            layers = np.zeros((n_nodes,1))
            for i_line in range(n_nodes):
                if pop_nodes[idx][i_line, 1] == -3.9 and pop_nodes[idx][i_line, 2] == 3.9:
                    n_layer += 1
                layers[i_line] = n_layer
            pop_nodes[idx] = np.append(pop_nodes[idx], layers, 1)

        # Finally, load connections and save a big vector for each one of the 5
        # parameters needed by nest.Connect: pre, post, w, d, and synapse_model
        # Save unmodified and scrambled versions in network.pickle

        all_conn_files = glob.glob(conn_folder + "/connections_*.dump")

        pre = []
        post_intact = []
        post_scrambled = []
        w = []
        d = []
        syn = []
        for (idx, pop_conn_file) in enumerate(all_conn_files):

            population = pop_conn_file[pop_conn_file.find('connections_')+12:-5]
            print('Scrambling connections for %s (%d out of %d files)' % (population, idx+1, len(all_conn_files)))

            synapse_model = [s for i,s in enumerate(synapse_models) if s in population][0]

            print('Loading file')
            pop_conns = np.loadtxt(pop_conn_file)
            tn_lines = pop_conns.shape[0]

            if tn_lines > 0:

                syn.append(synapse_model)

                pre.append(pop_conns[:,0].astype(int))
                post_intact.append(pop_conns[:,1].astype(int))
                w.append(pop_conns[:,2])
                d.append(pop_conns[:,3].astype(int))

                # Do the actual scrfambling
                # TODO this should be optimized, it is way too slow
                if any(w == population[0:2] for w in ['Vs', 'Vp']):
                    print('Scrambling...', end="")
                    for i_line in range(tn_lines):
                        if not i_line % round(tn_lines/10.):
                            print('%d%%, ' % (round(100.*i_line/tn_lines) if i_line > 0 else 0), end="")

                        for (i_pop, this_pop) in enumerate(pop_nodes):
                            t_idx = np.where(this_pop[:,0] == pop_conns[i_line, 1])[0]
                            if t_idx.size:
                                break
                        if not t_idx.size:
                            raise ValueError('Could not find tartget %s for node %s' % (pop_conns[i_line, 1], pop_conns[i_line, 0]))
                        if len(t_idx) > 1:
                            raise ValueError('Target %s apears in more than one population' % (pop_conns[i_line, 1], pop_conns[i_line, 0]))
                        new_t_idx = t_idx[0]

                        # For the moment, only scramble connections within the visual cortex
                        if any(w == pop_names[i_pop][0:2] for w in ['Vs', 'Vp']):
                            possible_targets = pop_nodes[i_pop][pop_nodes[i_pop][:,3] == pop_nodes[i_pop][new_t_idx,3], 0]
                            pop_conns[i_line, 1] = np.random.permutation(possible_targets)[0]

                print('done.')

                post_scrambled.append(pop_conns[:,1].astype(int))

        # save results
        print('Saving results...', end="")
        with open(connections_file, 'w') as f:
            pickle.dump((pre, post_intact, post_scrambled, w, d, syn), f)
        print(' done.')

    if Params.has_key('show_V4_num_conn_figure') and Params['show_V4_num_conn_figure']:

        horizontal_nodes = nest.GetLeaves(Vp_horizontal, properties={'model': 'L4_exc'}, local_only=True)[0]
        vertical_nodes = nest.GetLeaves(Vp_vertical, properties={'model': 'L4_exc'}, local_only=True)[0]

        n_conns_hor = []
        for (idx, horizontal_node) in enumerate(horizontal_nodes):
            tgt_map = []
            this_conns = nest.GetConnections([horizontal_node], horizontal_nodes, synapse_model='AMPA_syn')
            tgt_map.extend([conn[1] for conn in this_conns])
            n_conns_hor.append(len(tgt_map))

        plt.figure()
        plt.hist(n_conns_hor, range(0, max(n_conns_hor + [30])))
        plt.title('# of connections Vp(h) L4pyr -> Vp(h) L4Pyr')

        n_conns_hor = []
        for (idx, horizontal_node) in enumerate(horizontal_nodes):
            tgt_map = []
            this_conns = nest.GetConnections([horizontal_node], vertical_nodes, synapse_model='AMPA_syn')
            tgt_map.extend([conn[1] for conn in this_conns])
            n_conns_hor.append(len(tgt_map))

            # nest.DisconnectOneToOne(tp_node, tgt_map[0], {"synapse_model": "AMPA_syn"})
            #nest.Disconnect([tp_node], tgt_map, 'one_to_one', {"synapse_model": "AMPA_syn"})

        plt.figure()
        plt.hist(n_conns_hor, range(0, max(n_conns_hor + [30])))
        plt.title('# of connections Vp(h) L4pyr -> Vp(v) L4Pyr')

        n_conns_ver = []
        for (idx, vertical_node) in enumerate(vertical_nodes):
            tgt_map = []
            this_conns = nest.GetConnections([vertical_node], vertical_nodes, synapse_model='AMPA_syn')
            tgt_map.extend([conn[1] for conn in this_conns])
            n_conns_ver.append(len(tgt_map))

        plt.figure()
        plt.hist(n_conns_ver, range(0, max(n_conns_ver + [30])))
        plt.title('# of connections Vp(v) L4pyr -> Vp(v) L4Pyr')

        n_conns_ver = []
        for (idx, vertical_node) in enumerate(vertical_nodes):
            tgt_map = []
            this_conns = nest.GetConnections([vertical_node], horizontal_nodes, synapse_model='AMPA_syn')
            tgt_map.extend([conn[1] for conn in this_conns])
            n_conns_ver.append(len(tgt_map))

        plt.figure()
        plt.hist(n_conns_ver, range(0, max(n_conns_ver + [30])))
        plt.title('# of connections Vp(v) L4pyr -> Vp(h) L4Pyr')

        plt.show()

    # Check connections


    if Params.has_key('show_center_connectivity_figure') and Params['show_center_connectivity_figure']:

        # this code only gives one element, and you have no control over the
        # model it comes from...
        # tp.PlotTargets(tp.FindCenterElement(Vp_horizontal), Vp_horizontal, 'L56_exc', 'AMPA_syn')
        # tp.PlotTargets(tp.FindCenterElement(Vp_horizontal), Vp_vertical, 'L56_exc', 'AMPA_syn')
        # tp.PlotTargets(tp.FindCenterElement(Vp_vertical), Vp_horizontal, 'L56_exc', 'AMPA_syn')
        # tp.PlotTargets(tp.FindCenterElement(Vp_vertical), Vp_vertical, 'L56_exc', 'AMPA_syn')

        # this way I can get all nodes in the positions, and later filter per Model using GetStatus
        # IMPORTANT: when layer has even number of neurons per line/column
        #            using center (0,0) makes the function return 4 neurons
        #            per postiion since they all have the same distance.
        #            Using (-0,1, 0,1) solves this problem.
        #            When using odd number perhaps this has to change...

        for src_label in ['Tp_layer', 'Vp_vertical', 'Vp_horizontal']:
            for tgt_label in ['Tp_layer', 'Vp_vertical', 'Vp_horizontal']:

                if src_label == 'Tp_layer' and src_label == tgt_label:
                    continue

                print('source population %s' % src_label)
                print('target population %s' % tgt_label)
                n_plot = 0
                f = plt.figure()
                f.canvas.set_window_title('AMPA Connections: %s -> %s' % (src_label, tgt_label))
                all_center = eval('tp.FindNearestElement(%s, (-0.1, 0.1), True)[0]' % src_label)
                for n in all_center:
                    s = nest.GetStatus([n])
                    p = tp.GetPosition([n])
                    # print('%s : (%2.2f, %2.2f)' % (s[0]['model'], p[0][0], p[0][1]))
                    m1 = s[0]['model'].name
                    print('Source neuron model %s' % m1)
                    if m1.find('_exc') > -1:
                        if src_label.find('Tp_') > -1:
                            print( 'found Tp_')
                            # target has to be one of Vp_ or Vs_ models
                            target_models = ['L23_exc', 'L4_exc', 'L56_exc']
                        elif src_label.find('Vp_') > -1 or src_label.find('Vs') > -1:
                            # if target is Vp_ of Vs_ too, then one of those models
                            if tgt_label.find('Vp_') > -1:
                                target_models = ['L23_exc', 'L4_exc', 'L56_exc']
                            elif tgt_label.find('Tp_') > -1:
                                # otherwise it has to be a Tp_target
                                target_models = ['Tp_exc']
                            else:
                                raise ValueError('Invalide target %s for source model: %s' % (tgt_label, src_label))
                        else:
                            raise ValueError('Invalide source model: %s' % (src_label))

                        for m2 in target_models:
                            print('Target neuron model %s' % m2)
                            try:
                                get_targets_command = 'tp.GetTargetNodes([n], %s, m2, "AMPA_syn")[0]' % tgt_label
                                print(get_targets_command)
                                targets = eval(get_targets_command)
                                if len(targets) > 0:
                                    n_plot += 1
                                    f.add_subplot(3,5,n_plot)
                                    eval('tp.PlotTargets([n], %s, m2, "AMPA_syn", f)' % tgt_label)
                                    plt.title('%s -> %s' % (m1, m2), fontsize=9)
                            except:
                                print('didnt work')

                f.savefig(data_folder + '/connectivity_%s_%s.png' % (src_label, tgt_label), dpi=100)

    if Params.has_key('show_V4_connectivity_figure') and Params['show_V4_connectivity_figure']:

        Vp_hor_gids = tp.GetElement(Vp_horizontal, [0,0])
        n_Vp_hor = len(Vp_hor_gids)

        f = []
        for idx in range(n_Vp_hor):
            f.append(plt.figure())

        positions = range(0,41,10)
        positions[-1] -= 1
        for xi in range(len(positions)):
            for yi in range(len(positions)):
                print("Position [%d,%d] : %d" %(xi,yi,yi*(len(positions))+xi+1))
                x = positions[xi]
                y = positions[yi]
                Vp_hor_gids = tp.GetElement(Vp_horizontal, [x,y])
                Vp_hor_status = nest.GetStatus(Vp_hor_gids)
                for (idx, n) in enumerate(Vp_hor_status):
                    if n['Tau_theta'] == 2.0:
                        print(idx)
                        try:
                            f[idx].add_subplot(len(positions), len(positions), yi*(len(positions))+xi+1)
                            tp.PlotTargets([Vp_hor_gids[idx]], Vp_horizontal, 'L4_exc', 'AMPA_syn', f[idx])
                        except:
                            print('%i bad' % Vp_hor_gids[idx])
        plt.show()

    if Params.has_key('dry_run') and Params['dry_run']:
        print('Only generation, loading and ploting network. No actual simulation done.')
        return

    '''
    # pablo
    # Create vertical grating
    for n in nest.GetLeaves(Retina_layer)[0]:
            retina_0 = (nest.GetLeaves(Retina_layer)[0])[0]
            col = (n-retina_0)/Params['Np']

            cells_per_degree = Params['Np']/Params['visSize']
            cells_per_cycle = cells_per_degree/Params['spatial_frequency']

            nest.SetStatus([n], { "phase": col * 360/(cells_per_cycle-1) })
    '''
    ### keiko
    if Params['lambda_dg'] >= 0:
        [nest.SetStatus([n], {"phase": phaseInit(tp.GetPosition([n])[0],
                                                 Params["lambda_dg"],
                                                 Params["phi_dg"])})
        for n in nest.GetLeaves(Retina_layer)[0]]
    else:
        # Leonardo: Random retina input
        [nest.SetStatus([n], {"phase": phaseInit(tp.GetPosition([n])[0],
                                                 np.pi * np.random.rand(),
                                                 np.pi * np.random.rand())})
         for n in nest.GetLeaves(Retina_layer)[0]]


    # --------------------------------------------------------------------#
    # ---------- SET IB NEURONS ----------------------------------------- #
    # --------------------------------------------------------------------#

    # 30% of Cortex L56 excitatory neurons are intrinsically bursting(IB) neuron.
    # That is achieved by setting pacemaker current I_h.
    # So select 30% of L56_exc neuron, and change h_g_peak from 0.0 to 1.0.
    # (Other cortical neuron do not have I_h, thus h_g_peak=0.0)

    L56_vertical_idx = [nd for nd in nest.GetLeaves(Vp_vertical)[0] if nest.GetStatus([nd], 'model')[0]=='L56_exc']
    L56_horizontal_idx = [nd for nd in nest.GetLeaves(Vp_horizontal)[0] if nest.GetStatus([nd], 'model')[0]=='L56_exc']

    num_neuron = len(L56_vertical_idx)
    num_ib = int(num_neuron*0.3)

    ridx_vertical = np.random.randint(num_neuron, size=(1,num_ib))[0]
    ridx_horizontal = np.random.randint(num_neuron, size=(1,num_ib))[0]

    for i in range(1,num_ib,1):
        nest.SetStatus([L56_vertical_idx[ridx_vertical[i]]], {'g_peak_h': 1.0})
        nest.SetStatus([L56_horizontal_idx[ridx_horizontal[i]]], {'g_peak_h': 1.0})



    # initiate network activity
    #nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'rate': Params['ret_rate']})
    nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'rate': Params['ret_rate']})
    nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'amplitude': 0.0})
    nest.Simulate(500.0)


    #! =================
    #! Recording devices
    #! =================
    print('Connecting recorders...', end="")
    nest.CopyModel('multimeter', 'RecordingNode',
            params = {'interval'   : Params['resolution'],
            'record_from': ['V_m'],#,
                            # Put back when plotting synaptic currents, otherwise makes everything slower for no reason
                            # 'I_syn_AMPA',
                            # 'I_syn_NMDA',
                            # 'I_syn_GABA_A',
                            # 'I_syn_GABA_B',
                            # 'g_AMPA',
                            # 'g_NMDA',
                            # 'g_GABA_A',
                            # 'g_GABA_B'],
                            #'I_NaP',
                            #'I_KNa',
                            #'I_T',
                            #'I_h'
                            #]
            'record_to'  : ['memory'],
            'withgid'    : True,
            'withtime'   : True})

    recorders = []
    '''
    for population, model in [(Retina_layer, 'Retina'),
                              (Tp_layer  , 'Tp_exc'),
                              (Tp_layer  , 'Tp_inh'),
                              (Rp_layer  , 'Rp'),
                              (Vp_vertical, 'L23_exc'),
                              (Vp_horizontal, 'L23_exc'),
                              (Vp_vertical, 'L23_inh'),
                              (Vp_horizontal, 'L23_inh'),
                              (Vp_vertical, 'L4_exc'),
                              (Vp_horizontal, 'L4_exc'),
                              (Vp_vertical, 'L4_inh'),
                              (Vp_horizontal, 'L4_inh'),
                              (Vp_vertical, 'L56_exc'),
                              (Vp_horizontal, 'L56_exc'),
                              (Vp_vertical, 'L56_inh'),
                              (Vp_horizontal, 'L56_inh'),
                              (Vs_vertical, 'L23_exc'),
                              (Vs_horizontal, 'L23_exc'),
                              (Vs_vertical, 'L23_inh'),
                              (Vs_horizontal, 'L23_inh'),
                              (Vs_vertical, 'L4_exc'),
                              (Vs_horizontal, 'L4_exc'),
                              (Vs_vertical, 'L4_inh'),
                              (Vs_horizontal, 'L4_inh'),
                              (Vs_vertical, 'L56_exc'),
                              (Vs_horizontal, 'L56_exc'),
                              (Vs_vertical, 'L56_inh'),
                              (Vs_horizontal, 'L56_inh')]:
    '''
    # for population, model in [(Retina_layer, 'Retina'),
    #                           (Tp_layer  , 'Tp_exc'),
    #                           (Tp_layer  , 'Tp_inh'),
    #                           (Vp_vertical, 'L4_exc'),
    #                           (Vp_vertical, 'L4_inh'),
    #                           (Vp_horizontal, 'L4_exc'),
    #                           (Vp_vertical, 'L23_exc'),
    #                           (Vp_horizontal, 'L23_exc'),
    #                           (Vp_vertical, 'L56_exc'),
    #                           (Rp_layer, 'Rp')]:
    for population, model in [(Retina_layer, 'Retina'),
                              # (Tp_layer  , 'Tp_exc'),
                              # (Tp_layer  , 'Tp_inh'),
                              (Vp_vertical, 'L4_inh'),
                              (Vp_horizontal, 'L4_exc'),
                              (Vp_horizontal, 'L23_exc'),
                              # (Rp_layer, 'Rp'),
                              (Vp_vertical, 'L23_exc'),
                              (Vp_vertical, 'L4_exc'),
                              (Vp_vertical, 'L56_exc'),
                              (Vs_vertical, 'L23_exc'),
                              (Vs_vertical, 'L4_exc'),
                              (Vs_vertical, 'L56_exc'),

                              ]:


        print('.', end="")
        rec = nest.Create('RecordingNode')
        recorders.append([rec,population,model])
        if (model=='Retina'):
            nest.SetStatus(rec,{'record_from': ['rate']})
        tgts = [nd for nd in nest.GetLeaves(population)[0] if nest.GetStatus([nd], 'model')[0]==model]
        #nest.Connect(rec, tgts, None, {'delay': recorders_delay})
        nest.Connect(rec, tgts)
    print('done.')

    #! =================
    #! Spike detector
    #! =================
    print('Connecting detectors...', end="")
    detectors = []
    '''
    for population, model in [(Retina_layer, 'Retina'),
                              (Tp_layer  , 'Tp_exc'),
                              (Tp_layer  , 'Tp_inh'),
                              (Rp_layer  , 'Rp'),
                              (Vp_vertical, 'L23_exc'),
                              (Vp_horizontal, 'L23_exc'),
                              (Vp_vertical, 'L23_inh'),
                              (Vp_horizontal, 'L23_inh'),
                              (Vp_vertical, 'L4_exc'),
                              (Vp_horizontal, 'L4_exc'),
                              (Vp_vertical, 'L4_inh'),
                              (Vp_horizontal, 'L4_inh'),
                              (Vp_vertical, 'L56_exc'),
                              (Vp_horizontal, 'L56_exc'),
                              (Vp_vertical, 'L56_inh'),
                              (Vp_horizontal, 'L56_inh'),
                              (Vs_vertical, 'L23_exc'),
                              (Vs_horizontal, 'L23_exc'),
                              (Vs_vertical, 'L23_inh'),
                              (Vs_horizontal, 'L23_inh'),
                              (Vs_vertical, 'L4_exc'),
                              (Vs_horizontal, 'L4_exc'),
                              (Vs_vertical, 'L4_inh'),
                              (Vs_horizontal, 'L4_inh'),
                              (Vs_vertical, 'L56_exc'),
                              (Vs_horizontal, 'L56_exc'),
                              (Vs_vertical, 'L56_inh'),
                              (Vs_horizontal, 'L56_inh')]:
        '''
    '''
    for population, model in [(Retina_layer, 'Retina'),
                              (Tp_layer  , 'Tp_exc'),
                              (Tp_layer  , 'Tp_inh'),
                              (Vp_vertical, 'L23_exc'),
                              (Vp_horizontal, 'L23_exc'),
                              (Vp_vertical, 'L4_exc'),
                              (Vp_horizontal, 'L4_exc')]:
        '''

    #Tom
    for population, model in [(Retina_layer, 'Retina'),
                              # (Tp_layer, 'Tp_exc'),
                              # (Tp_layer, 'Tp_inh'),
                              (Vp_vertical, 'L23_exc'),
                              (Vp_vertical, 'L4_exc'),
                              (Vp_vertical, 'L56_exc'),
                              (Vp_vertical, 'L23_inh'),
                              (Vp_vertical, 'L4_inh'),
                              (Vp_vertical, 'L56_inh'),
                              (Vs_vertical, 'L23_exc'),
                              (Vs_vertical, 'L23_inh'),
                              (Vs_vertical, 'L4_exc'),
                              (Vs_vertical, 'L4_inh'),
                              (Vs_vertical, 'L56_exc'),
                              (Vs_vertical, 'L56_inh')]:



        print('.', end="")
        rec = nest.Create('spike_detector', params={"withgid": True, "withtime": True})
        #rec = nest.Create('spike_detector')
        detectors.append([rec,population,model])
        tgts = [nd for nd in nest.GetLeaves(population)[0] if nest.GetStatus([nd], 'model')[0]==model]
        if model == 'Retina':
            for t in tgts:
                try:
                    # nest.Connect([t], rec, None, {'delay':  recorders_delay})
                    nest.Connect([t], rec)
                    print('connected %d' % t)
                except:
                    print('%d did not work' % t)
        else:
            # nest.Connect(tgts, rec, None, {'delay': recorders_delay})
            nest.Connect(tgts, rec)
    print('done.')


    #! ====================
    #! Simulation
    #! ====================
    '''
    # change gKL to 0.8 in all populations (necessary to get a little stronger evoked response)
    for l in layers:
            sim_elements = l[1]['elements']
            for m in np.arange(0,np.size(sim_elements),1):

                    if(np.size(sim_elements)==1):
                        sim_model = sim_elements
                    else:
                        sim_model = sim_elements[m]

                    exec("la = %s" % l[0])
                    pop = [nd for nd in nest.GetLeaves(la)[0] if nest.GetStatus([nd], 'model')[0]==sim_model]
                    if (l[0]!='Retina_layer'):
                            for cell in pop:
                                    nest.SetStatus([cell], {'g_KL':0.8})
    '''
    # Simulate
    for t in Params['intervals']:

        #if (t == 250.0):  # Stimulus ON
        #    # if (t == 1500.0):  # Stimulus ON
        #    nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'amplitude': 45.0})
        #else:  # Stimulus OFF
        #    nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'amplitude': 0.0})

        if Params['input_flag']==True:
            nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'amplitude': Params['ret_rate']})
        else:
            nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'amplitude': 0.0})

        nest.Simulate(t)



    #! ====================
    #! Plot Results
    #! ====================

    print("Creating figure 3...")

    rows = 9
    cols = 2

    fig = plt.figure(num=None, figsize=(13, 24), dpi=100)
    fig.subplots_adjust(hspace=0.4)

    # Plot A: membrane potential rasters

    recorded_models = [(Retina_layer,'Retina'),
                        (Vp_vertical,'L23_exc'),
                        (Vp_vertical,'L4_exc'),
                        (Vp_vertical,'L56_exc'),
                        (Vs_vertical,'L23_exc'),
                        (Vs_vertical,'L4_exc')]

    #plotting.potential_raster(fig,recorders,recorded_models,0,Params['Np'],np.sum(Params['intervals']),Params['resolution'],rows,cols,0)
    plotting.potential_raster(fig,recorders,recorded_models,0,Params['Np'],
                              np.sum(Params['intervals']),
                              Params['resolution'],
                              rows,cols,0)
    #starting_neuron = 800+1
    #plotting.potential_raster(fig,recorders,recorded_models,starting_neuron,Params['Np'],np.sum(Params['intervals']),Params['resolution'],rows,cols,0)

    plt.title('Evoked')

    # Plot B: individual intracellular traces

    recorded_models =[(Vp_vertical,'L4_exc'),
                      (Vp_vertical,'L4_inh')]

    #plotting.intracellular_potentials(fig, recorders, recorded_models, 21, rows, cols, 6) #original
    # keiko
    total_time = 0.0
    for t in Params['intervals']:
        total_time += t

    #draw_neuron = (Params['Np']*Params['Np']/2)
    #plotting.intracellular_potentials(fig, recorders, recorded_models, draw_neuron, rows, cols, 6, total_time)
    plotting.intracellular_potentials(fig, recorders, recorded_models, 21, rows, cols, 6, total_time)
    #plotting.intracellular_potentials(fig, recorders, recorded_models, 820, rows, cols, 6, total_time)

    # Plot C: topographical activity of the vertical and horizontal layers


    if Params.has_key('start_membrane_potential') and  Params.has_key('end_membrane_potential'):
        start = Params['start_membrane_potential']
        stop = Params['end_membrane_potential']
    else:
        start = 130.0
        stop = 140.0

    recorded_models = [(Vp_vertical,'L23_exc')]
    labels = ["Vertical"]
    plotting.topographic_representation(fig,
                                        recorders,
                                        recorded_models,
                                        labels,
                                        Params['Np'],
                                        np.sum(Params['intervals']),
                                        Params['resolution'],
                                        rows,
                                        cols,
                                        start,
                                        stop,
                                        8,
                                        0)

    recorded_models = [(Vp_horizontal,'L23_exc')]

    labels = ["Horizontal"]

    plotting.topographic_representation(fig,recorders,recorded_models,labels,Params['Np'],np.sum(Params['intervals']),Params['resolution'],rows,cols,start,stop,8,1)

    fig.savefig(data_folder + '/figure3.png', dpi=100)

    if Params.has_key('show_main_figure') and Params['show_main_figure']:
        plt.show()


    # Plot D: movie

    #labels = ["Evoked_Vp_L23_Vertical","Evoked_Vp_L23_Horizontal"]
    #recorded_models = [(Vp_vertical,'L23_exc'),(Vp_horizontal,'L23_exc')]
    #plotting.makeMovie(fig,recorders,recorded_models,labels,Params['Np'],np.sum(Params['intervals']),Params['resolution'])


    #! ====================
    #! Save Results
    #! ====================

    print('save recorders data')
    # Set folder
    #rootdir = '/home/kfujii2/newNEST2/ht_model_pablo_based/data/'
    #expdir = 'random/'
    # expdir = 'random_full/'
    # expdir = 'structured_full/'
    # data_folder = rootdir + expdir

    # To save spike data, set pairs of population id and its name
    population_name = [ {'population': Retina_layer, 'name': 'Retina'},
                        {'population': Vp_vertical, 'name': 'Vp_v'},
                        {'population': Vp_horizontal, 'name': 'Vp_h'},
                        # {'population': Rp_layer, 'name': 'Rp'},
                        # {'population': Tp_layer, 'name': 'Tp'},
                        {'population': Vs_vertical, 'name': 'Vs_v'},
                        {'population': Vs_horizontal, 'name': 'Vs_h'}]

    if Params.has_key('save_recorders') and Params['save_recorders']:
        for rec, population, model in recorders:

            # Get name of population
            for p in range(0, len(population_name), 1):
                if population_name[p]['population'] == population:
                    p_name = population_name[p]['name']

            data = nest.GetStatus(rec)[0]['events']

            if model == 'Retina':
                scipy.io.savemat(data_folder + '/recorder_' + p_name + '_' + model + '.mat',
                                 mdict={'senders': data['senders'],
                                        'rate': data['rate']})
            else:
                scipy.io.savemat(data_folder + '/recorder_' + p_name + '_' + model + '.mat',
                                 mdict={'senders': data['senders'],
                                        'times': data['times'],
                                        'V_m': data['V_m'],
                                        #'I_syn_AMPA': data['I_syn_AMPA'],
                                        #'I_syn_NMDA': data['I_syn_NMDA'],
                                        #'I_syn_GABA_A': data['I_syn_GABA_A'],
                                        #'I_syn_GABA_B': data['I_syn_GABA_B'],
                                        # 'g_AMPA': data['g_AMPA'],
                                        # 'g_NMDA': data['g_NMDA'],
                                        # 'g_GABA_A': data['g_GABA_A'],
                                        # 'g_GABA_B': data['g_GABA_B']
                                        } )



    print('save raster images')
    plt.close()
    for rec, population, model in detectors:
        spikes = nest.GetStatus(rec, 'events')[0]

        # Get name of population
        for p in range(0, len(population_name), 1):
            if population_name[p]['population'] == population:
                p_name = population_name[p]['name']

        if len(nest.GetStatus(rec)[0]['events']['senders']) > 3:
            raster = raster_plot.from_device(rec, hist=True)
            pylab.title( p_name + '_' + model )
            f = raster[0].figure
            f.set_size_inches(15, 9)
            f.savefig(data_folder + '/spikes_' + p_name + '_' + model + '.png', dpi=100)
            plt.close()

            # Set filename and save spike data
            filename = data_folder + '/spike_' + p_name + '_' + model + '.pickle'
            pickle.dump(spikes, open(filename, 'w'))
            scipy.io.savemat(data_folder + '/spike_' + p_name + '_' + model + '.mat', mdict={'senders': spikes['senders'], 'times': spikes['times']})

            '''
            filename_AMPA = data_folder + 'connection_' + p_name + '_AMPA_syn' + '.dat'
            filename_NMDA = data_folder + 'connection_' + p_name + '_NMDA_syn' + '.dat'
            filename_GABAA = data_folder + 'connection_' + p_name + '_GABA_A_syn' + '.dat'
            filename_GABAB = data_folder + 'connection_' + p_name + '_GABA_B_syn' + '.dat'
            tp.DumpLayerConnections(population, 'AMPA_syn', filename_AMPA)
            tp.DumpLayerConnections(population, 'NMDA_syn', filename_NMDA)
            tp.DumpLayerConnections(population, 'GABA_A_syn', filename_GABAA)
            tp.DumpLayerConnections(population, 'GABA_B_syn', filename_GABAB)
            '''
    '''
    for p in range(0, len(population_name), 1):

        population = population_name[p]['population']
        p_name = population_name[p]['name']
        filename_nodes = data_folder + '/gid_' + p_name + '.dat'

        tp.DumpLayerNodes(population, filename_nodes)
    '''

    network_script = Params['network'] + '.py'
    shutil.copy2(network_script, data_folder + '/' + network_script)
    shutil.copy2(network_script, conn_folder + '/' + network_script)

    print('end')
Beispiel #24
0
def simulation(Params):

    #! =================
    #! Import network
    #! =================

    import importlib
    import time

    #network_names = ['network_full_keiko',  'network_full_leonardo']
    network_names = ['network_full_keiko', 'network_full_keiko']

    colors = [[0, 0, 1, .5], [0, 1, 0, .5]]
    fig, axs = plt.subplots(2, 2)
    for (inet, netname) in enumerate(network_names):

        # NEST Kernel and Network settings
        nest.ResetKernel()
        nest.ResetNetwork()
        nest.SetKernelStatus({
            "local_num_threads": Params['threads'],
            'resolution': Params['resolution']
        })
        nest.SetStatus([0], {'print_time': True})

        # initialize random seed
        msd = int(round(time.time() * 1000))
        nest.SetKernelStatus({'grng_seed': msd})
        nest.SetKernelStatus({
            'rng_seeds':
            range(msd + Params['threads'] + 1, msd + 2 * Params['threads'] + 1)
        })

        network = importlib.import_module(netname)
        reload(network)
        models, layers, conns = network.get_Network(Params)
        # import network_full_keiko
        # reload(network_full_keiko)
        # models, layers, conns  = network_full_keiko.get_Network(Params)

        # Create models
        for m in models:
            nest.CopyModel(m[0], m[1], m[2])

        # Create layers, store layer info in Python variable
        for l in layers:
            exec '%s = tp.CreateLayer(l[1])' % l[0]

        # Create connections, need to insert variable names
        for c in conns:
            eval('tp.ConnectLayers(%s,%s,c[2])' % (c[0], c[1]))

        if Params.has_key('show_V4_num_conn_figure'
                          ) and Params['show_V4_num_conn_figure']:

            horizontal_nodes = nest.GetLeaves(Vp_horizontal,
                                              properties={'model': 'L4_exc'},
                                              local_only=True)[0]
            vertical_nodes = nest.GetLeaves(Vp_vertical,
                                            properties={'model': 'L4_exc'},
                                            local_only=True)[0]

            print('Ploting #1')
            n_conns_hor = []
            for (idx, horizontal_node) in enumerate(horizontal_nodes):
                tgt_map = []
                this_conns = nest.GetConnections([horizontal_node],
                                                 horizontal_nodes,
                                                 synapse_model='AMPA_syn')
                tgt_map.extend([conn[1] for conn in this_conns])
                n_conns_hor.append(len(tgt_map))

            plt.axes(axs[0, 0])
            plt.hist(n_conns_hor,
                     range(0, max(n_conns_hor + [30])),
                     fc=colors[inet])
            plt.title('# of connections Vp(h) L4pyr -> Vp(h) L4Pyr')

            print('Ploting #2')
            n_conns_hor = []
            for (idx, horizontal_node) in enumerate(horizontal_nodes):
                tgt_map = []
                this_conns = nest.GetConnections([horizontal_node],
                                                 vertical_nodes,
                                                 synapse_model='AMPA_syn')
                tgt_map.extend([conn[1] for conn in this_conns])
                n_conns_hor.append(len(tgt_map))

                # nest.DisconnectOneToOne(tp_node, tgt_map[0], {"synapse_model": "AMPA_syn"})
                #nest.Disconnect([tp_node], tgt_map, 'one_to_one', {"synapse_model": "AMPA_syn"})

            plt.axes(axs[0, 1])
            plt.hist(n_conns_hor,
                     range(0, max(n_conns_hor + [30])),
                     fc=colors[inet])
            plt.title('# of connections Vp(h) L4pyr -> Vp(v) L4Pyr')

            print('Ploting #3')
            n_conns_ver = []
            for (idx, vertical_node) in enumerate(vertical_nodes):
                tgt_map = []
                this_conns = nest.GetConnections([vertical_node],
                                                 vertical_nodes,
                                                 synapse_model='AMPA_syn')
                tgt_map.extend([conn[1] for conn in this_conns])
                n_conns_ver.append(len(tgt_map))

            plt.axes(axs[1, 1])
            plt.hist(n_conns_ver,
                     range(0, max(n_conns_ver + [30])),
                     fc=colors[inet])
            plt.title('# of connections Vp(v) L4pyr -> Vp(v) L4Pyr')

            print('Ploting #4')
            n_conns_ver = []
            for (idx, vertical_node) in enumerate(vertical_nodes):
                tgt_map = []
                this_conns = nest.GetConnections([vertical_node],
                                                 horizontal_nodes,
                                                 synapse_model='AMPA_syn')
                tgt_map.extend([conn[1] for conn in this_conns])
                n_conns_ver.append(len(tgt_map))

            plt.axes(axs[1, 0])
            plt.hist(n_conns_ver,
                     range(0, max(n_conns_ver + [30])),
                     fc=colors[inet])
            plt.title('# of connections Vp(v) L4pyr -> Vp(h) L4Pyr')

    plt.show()

    # Check connections

    # Connections from Retina to TpRelay
    # tp.PlotTargets(tp.FindCenterElement(Retina_layer), Tp_layer)

    if Params.has_key('show_V4_connectivity_figure'
                      ) and Params['show_V4_connectivity_figure']:

        Vp_hor_gids = tp.GetElement(Vp_horizontal, [0, 0])
        n_Vp_hor = len(Vp_hor_gids)

        f = []
        for idx in range(n_Vp_hor):
            f.append(plt.figure())

        positions = range(0, 41, 10)
        positions[-1] -= 1
        for xi in range(len(positions)):
            for yi in range(len(positions)):
                print("Position [%d,%d] : %d" % (xi, yi, yi *
                                                 (len(positions)) + xi + 1))
                x = positions[xi]
                y = positions[yi]
                Vp_hor_gids = tp.GetElement(Vp_horizontal, [x, y])
                Vp_hor_status = nest.GetStatus(Vp_hor_gids)
                for (idx, n) in enumerate(Vp_hor_status):
                    if n['Tau_theta'] == 2.0:
                        print idx
                        try:
                            f[idx].add_subplot(len(positions), len(positions),
                                               yi * (len(positions)) + xi + 1)
                            tp.PlotTargets([Vp_hor_gids[idx]], Vp_horizontal,
                                           'L4_exc', 'AMPA_syn', f[idx])
                        except:
                            print('%i bad' % Vp_hor_gids[idx])
        plt.show()

    # Connections from TpRelay to L4pyr in Vp (horizontally tuned)
    #topo.PlotTargets(topo.FindCenterElement(Tp), Vp_h, 'L4pyr', 'AMPA')
    #pylab.title('Connections TpRelay -> Vp(h) L4pyr')
    #pylab.show()

    # Connections from TpRelay to L4pyr in Vp (vertically tuned)
    #topo.PlotTargets(topo.FindCenterElement(Tp), Vp_v, 'L4pyr', 'AMPA')
    #pylab.title('Connections TpRelay -> Vp(v) L4pyr')
    #pylab.show()
    '''
    # pablo
    # Create vertical grating
    for n in nest.GetLeaves(Retina_layer)[0]:
            retina_0 = (nest.GetLeaves(Retina_layer)[0])[0]
            col = (n-retina_0)/Params['Np']

            cells_per_degree = Params['Np']/Params['visSize']
            cells_per_cycle = cells_per_degree/Params['spatial_frequency']

            nest.SetStatus([n], { "phase": col * 360/(cells_per_cycle-1) })
    '''
    ### keiko
    if Params['lambda_dg'] >= 0:
        [
            nest.SetStatus(
                [n], {
                    "phase":
                    phaseInit(
                        tp.GetPosition([n])[0], Params["lambda_dg"],
                        Params["phi_dg"])
                }) for n in nest.GetLeaves(Retina_layer)[0]
        ]
    else:
        # Leonardo: Random retina input
        [
            nest.SetStatus(
                [n], {
                    "phase":
                    phaseInit(
                        tp.GetPosition([n])[0], np.pi * np.random.rand(),
                        np.pi * np.random.rand())
                }) for n in nest.GetLeaves(Retina_layer)[0]
        ]

    # --------------------------------------------------------------------#
    # ---------- SET IB NEURONS ----------------------------------------- #
    # --------------------------------------------------------------------#

    # 30% of Cortex L56 excitatory neurons are intrinsically bursting(IB) neuron.
    # That is achieved by setting pacemaker current I_h.
    # So select 30% of L56_exc neuron, and change h_g_peak from 0.0 to 1.0.
    # (Other cortical neuron do not have I_h, thus h_g_peak=0.0)

    L56_vertical_idx = [
        nd for nd in nest.GetLeaves(Vp_vertical)[0]
        if nest.GetStatus([nd], 'model')[0] == 'L56_exc'
    ]
    L56_horizontal_idx = [
        nd for nd in nest.GetLeaves(Vp_horizontal)[0]
        if nest.GetStatus([nd], 'model')[0] == 'L56_exc'
    ]

    num_neuron = len(L56_vertical_idx)
    num_ib = int(num_neuron * 0.3)

    ridx_vertical = np.random.randint(num_neuron, size=(1, num_ib))[0]
    ridx_horizontal = np.random.randint(num_neuron, size=(1, num_ib))[0]

    for i in range(1, num_ib, 1):
        nest.SetStatus([L56_vertical_idx[ridx_vertical[i]]], {'h_g_peak': 1.0})
        nest.SetStatus([L56_horizontal_idx[ridx_horizontal[i]]],
                       {'h_g_peak': 1.0})

    # initiate network activity
    #nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'rate': Params['ret_rate']})
    nest.SetStatus(
        nest.GetLeaves(Retina_layer)[0], {'rate': Params['ret_rate']})
    nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'amplitude': 0.0})
    nest.Simulate(500.0)

    #! =================
    #! Recording devices
    #! =================

    nest.CopyModel(
        'multimeter',
        'RecordingNode',
        params={
            'interval':
            Params['resolution'],
            #'record_from': ['V_m'],
            'record_from': [
                'V_m', 'I_syn_AMPA', 'I_syn_NMDA', 'I_syn_GABA_A',
                'I_syn_GABA_B', 'g_AMPA', 'g_NMDA', 'g_GABAA', 'g_GABAB',
                'I_NaP', 'I_KNa', 'I_T', 'I_h'
            ],
            'record_to': ['memory'],
            'withgid':
            True,
            'withtime':
            True
        })

    recorders = []
    '''
    for population, model in [(Retina_layer, 'Retina'),
                              (Tp_layer  , 'Tp_exc'),
                              (Tp_layer  , 'Tp_inh'),
                              (Rp_layer  , 'Rp'),
                              (Vp_vertical, 'L23_exc'),
                              (Vp_horizontal, 'L23_exc'),
                              (Vp_vertical, 'L23_inh'),
                              (Vp_horizontal, 'L23_inh'),
                              (Vp_vertical, 'L4_exc'),
                              (Vp_horizontal, 'L4_exc'),
                              (Vp_vertical, 'L4_inh'),
                              (Vp_horizontal, 'L4_inh'),
                              (Vp_vertical, 'L56_exc'),
                              (Vp_horizontal, 'L56_exc'),
                              (Vp_vertical, 'L56_inh'),
                              (Vp_horizontal, 'L56_inh'),
                              (Vs_vertical, 'L23_exc'),
                              (Vs_horizontal, 'L23_exc'),
                              (Vs_vertical, 'L23_inh'),
                              (Vs_horizontal, 'L23_inh'),
                              (Vs_vertical, 'L4_exc'),
                              (Vs_horizontal, 'L4_exc'),
                              (Vs_vertical, 'L4_inh'),
                              (Vs_horizontal, 'L4_inh'),
                              (Vs_vertical, 'L56_exc'),
                              (Vs_horizontal, 'L56_exc'),
                              (Vs_vertical, 'L56_inh'),
                              (Vs_horizontal, 'L56_inh')]:
    '''
    for population, model in [(Retina_layer, 'Retina'), (Tp_layer, 'Tp_exc'),
                              (Tp_layer, 'Tp_inh'), (Vp_vertical, 'L4_exc'),
                              (Vp_vertical, 'L4_inh'),
                              (Vp_horizontal, 'L4_exc'),
                              (Vp_vertical, 'L23_exc'),
                              (Vp_horizontal, 'L23_exc'),
                              (Vp_vertical, 'L56_exc'), (Rp_layer, 'Rp')]:
        rec = nest.Create('RecordingNode')
        recorders.append([rec, population, model])
        if (model == 'Retina'):
            nest.SetStatus(rec, {'record_from': ['rate']})
        tgts = [
            nd for nd in nest.GetLeaves(population)[0]
            if nest.GetStatus([nd], 'model')[0] == model
        ]
        nest.Connect(rec, tgts)

    #! =================
    #! Spike detector
    #! =================
    detectors = []
    '''
    for population, model in [(Retina_layer, 'Retina'),
                              (Tp_layer  , 'Tp_exc'),
                              (Tp_layer  , 'Tp_inh'),
                              (Rp_layer  , 'Rp'),
                              (Vp_vertical, 'L23_exc'),
                              (Vp_horizontal, 'L23_exc'),
                              (Vp_vertical, 'L23_inh'),
                              (Vp_horizontal, 'L23_inh'),
                              (Vp_vertical, 'L4_exc'),
                              (Vp_horizontal, 'L4_exc'),
                              (Vp_vertical, 'L4_inh'),
                              (Vp_horizontal, 'L4_inh'),
                              (Vp_vertical, 'L56_exc'),
                              (Vp_horizontal, 'L56_exc'),
                              (Vp_vertical, 'L56_inh'),
                              (Vp_horizontal, 'L56_inh'),
                              (Vs_vertical, 'L23_exc'),
                              (Vs_horizontal, 'L23_exc'),
                              (Vs_vertical, 'L23_inh'),
                              (Vs_horizontal, 'L23_inh'),
                              (Vs_vertical, 'L4_exc'),
                              (Vs_horizontal, 'L4_exc'),
                              (Vs_vertical, 'L4_inh'),
                              (Vs_horizontal, 'L4_inh'),
                              (Vs_vertical, 'L56_exc'),
                              (Vs_horizontal, 'L56_exc'),
                              (Vs_vertical, 'L56_inh'),
                              (Vs_horizontal, 'L56_inh')]:
        '''

    for population, model in [(Retina_layer, 'Retina'), (Tp_layer, 'Tp_exc'),
                              (Tp_layer, 'Tp_inh'), (Vp_vertical, 'L4_exc'),
                              (Vp_horizontal, 'L4_exc')]:
        rec = nest.Create('spike_detector',
                          params={
                              "withgid": True,
                              "withtime": True
                          })
        #rec = nest.Create('spike_detector')
        detectors.append([rec, population, model])
        tgts = [
            nd for nd in nest.GetLeaves(population)[0]
            if nest.GetStatus([nd], 'model')[0] == model
        ]
        if model == 'Retina':
            for t in tgts:
                try:
                    nest.Connect([t], rec)
                    print('connected %d' % t)
                except:
                    print('%d did not work' % t)
        else:
            nest.Connect(tgts, rec)

    #! ====================
    #! Simulation
    #! ====================
    '''
    # change gKL to 0.8 in all populations (necessary to get a little stronger evoked response)
    for l in layers:
            sim_elements = l[1]['elements']
            for m in np.arange(0,np.size(sim_elements),1):

                    if(np.size(sim_elements)==1):
                        sim_model = sim_elements
                    else:
                        sim_model = sim_elements[m]

                    exec("la = %s" % l[0])
                    pop = [nd for nd in nest.GetLeaves(la)[0] if nest.GetStatus([nd], 'model')[0]==sim_model]
                    if (l[0]!='Retina_layer'):
                            for cell in pop:
                                    nest.SetStatus([cell], {'g_KL':0.8})
    '''
    # Simulate
    for t in Params['intervals']:

        #if (t == 250.0):  # Stimulus ON
        #    # if (t == 1500.0):  # Stimulus ON
        #    nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'amplitude': 45.0})
        #else:  # Stimulus OFF
        #    nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'amplitude': 0.0})

        if Params['input_flag'] == True:
            nest.SetStatus(
                nest.GetLeaves(Retina_layer)[0],
                {'amplitude': Params['ret_rate']})
        else:
            nest.SetStatus(nest.GetLeaves(Retina_layer)[0], {'amplitude': 0.0})

        nest.Simulate(t)

    #! ====================
    #! Plot Results
    #! ====================

    if Params.has_key('show_main_figure') and Params['show_main_figure']:
        print "plotting..."

        rows = 9
        cols = 2

        fig = plt.figure()
        fig.subplots_adjust(hspace=0.4)

        # Plot A: membrane potential rasters

        recorded_models = [(Retina_layer, 'Retina'), (Vp_vertical, 'L23_exc'),
                           (Vp_vertical, 'L4_exc'), (Vp_vertical, 'L56_exc'),
                           (Rp_layer, 'Rp'), (Tp_layer, 'Tp_exc')]

        #plotting.potential_raster(fig,recorders,recorded_models,0,Params['Np'],np.sum(Params['intervals']),Params['resolution'],rows,cols,0)
        plotting.potential_raster(fig, recorders, recorded_models, 0,
                                  Params['Np'], np.sum(Params['intervals']),
                                  Params['resolution'], rows, cols, 0)
        #starting_neuron = 800+1
        #plotting.potential_raster(fig,recorders,recorded_models,starting_neuron,Params['Np'],np.sum(Params['intervals']),Params['resolution'],rows,cols,0)

        plt.title('Evoked')

        # Plot B: individual intracellular traces

        recorded_models = [(Vp_vertical, 'L4_exc'), (Vp_vertical, 'L4_inh')]

        #plotting.intracellular_potentials(fig, recorders, recorded_models, 21, rows, cols, 6) #original
        # keiko
        total_time = 0.0
        for t in Params['intervals']:
            total_time += t

        #draw_neuron = (Params['Np']*Params['Np']/2)
        #plotting.intracellular_potentials(fig, recorders, recorded_models, draw_neuron, rows, cols, 6, total_time)
        plotting.intracellular_potentials(fig, recorders, recorded_models, 21,
                                          rows, cols, 6, total_time)
        #plotting.intracellular_potentials(fig, recorders, recorded_models, 820, rows, cols, 6, total_time)

        # Plot C: topographical activity of the vertical and horizontal layers

        recorded_models = [(Vp_vertical, 'L23_exc')]

        labels = ["Vertical"]
        start = 130.0
        stop = 140.0
        #start = 650.0
        #stop = 660.0
        plotting.topographic_representation(fig, recorders, recorded_models,
                                            labels, Params['Np'],
                                            np.sum(Params['intervals']),
                                            Params['resolution'], rows, cols,
                                            start, stop, 8, 0)

        recorded_models = [(Vp_horizontal, 'L23_exc')]

        labels = ["Horizontal"]
        start = 130.0
        stop = 140.0
        #start = 650.0
        #stop = 660.0

        plotting.topographic_representation(fig, recorders, recorded_models,
                                            labels, Params['Np'],
                                            np.sum(Params['intervals']),
                                            Params['resolution'], rows, cols,
                                            start, stop, 8, 1)

        plt.show()

    # Plot D: movie

    #labels = ["Evoked_Vp_L23_Vertical","Evoked_Vp_L23_Horizontal"]
    #recorded_models = [(Vp_vertical,'L23_exc'),(Vp_horizontal,'L23_exc')]
    #plotting.makeMovie(fig,recorders,recorded_models,labels,Params['Np'],np.sum(Params['intervals']),Params['resolution'])

    #! ====================
    #! Save Results
    #! ====================

    print('save recorders data')
    # Set folder
    #rootdir = '/home/kfujii2/newNEST2/ht_model_pablo_based/data/'
    #expdir = 'random/'
    # expdir = 'random_full/'
    # expdir = 'structured_full/'
    # data_folder = rootdir + expdir
    data_folder = Params['data_folder']

    if not os.path.isdir(data_folder):
        os.makedirs(data_folder)

    # To save spike data, set pairs of population id and its name
    population_name = [{
        'population': Retina_layer,
        'name': 'Retina'
    }, {
        'population': Vp_vertical,
        'name': 'Vp_v'
    }, {
        'population': Vp_horizontal,
        'name': 'Vp_h'
    }, {
        'population': Rp_layer,
        'name': 'Rp'
    }, {
        'population': Tp_layer,
        'name': 'Tp'
    }, {
        'population': Vs_vertical,
        'name': 'Vs_v'
    }, {
        'population': Vs_horizontal,
        'name': 'Vs_h'
    }]

    for rec, population, model in recorders:

        # Get name of population
        for p in range(0, len(population_name), 1):
            if population_name[p]['population'] == population:
                p_name = population_name[p]['name']

        data = nest.GetStatus(rec)[0]['events']

        if model == 'Retina':
            scipy.io.savemat(data_folder + '/recorder_' + p_name + '_' +
                             model + '.mat',
                             mdict={
                                 'senders': data['senders'],
                                 'rate': data['rate']
                             })
        else:
            scipy.io.savemat(data_folder + '/recorder_' + p_name + '_' +
                             model + '.mat',
                             mdict={
                                 'senders': data['senders'],
                                 'V_m': data['V_m'],
                                 'I_syn_AMPA': data['I_syn_AMPA'],
                                 'I_syn_NMDA': data['I_syn_NMDA'],
                                 'I_syn_GABA_A': data['I_syn_GABA_A'],
                                 'I_syn_GABA_B': data['I_syn_GABA_B'],
                                 'g_AMPA': data['g_AMPA'],
                                 'g_NMDA': data['g_NMDA'],
                                 'g_GABAA': data['g_GABAA'],
                                 'g_GABAB': data['g_GABAB']
                             })

    print('save raster images')
    plt.close()
    for rec, population, model in detectors:
        spikes = nest.GetStatus(rec, 'events')[0]

        # Get name of population
        for p in range(0, len(population_name), 1):
            if population_name[p]['population'] == population:
                p_name = population_name[p]['name']

        if len(nest.GetStatus(rec)[0]['events']['senders']) > 3:
            raster = raster_plot.from_device(rec, hist=True)
            pylab.title(p_name + '_' + model)
            f = raster[0].figure
            f.set_size_inches(15, 9)
            f.savefig(data_folder + 'spikes_' + p_name + '_' + model + '.png',
                      dpi=100)
            plt.close()

            # Set filename and save spike data
            filename = data_folder + 'spike_' + p_name + '_' + model + '.pickle'
            pickle.dump(spikes, open(filename, 'w'))
            scipy.io.savemat(data_folder + '/spike_' + p_name + '_' + model +
                             '.mat',
                             mdict={
                                 'senders': spikes['senders'],
                                 'times': spikes['times']
                             })
            '''
            filename_AMPA = data_folder + 'connection_' + p_name + '_AMPA_syn' + '.dat'
            filename_NMDA = data_folder + 'connection_' + p_name + '_NMDA_syn' + '.dat'
            filename_GABAA = data_folder + 'connection_' + p_name + '_GABA_A_syn' + '.dat'
            filename_GABAB = data_folder + 'connection_' + p_name + '_GABA_B_syn' + '.dat'
            tp.DumpLayerConnections(population, 'AMPA_syn', filename_AMPA)
            tp.DumpLayerConnections(population, 'NMDA_syn', filename_NMDA)
            tp.DumpLayerConnections(population, 'GABA_A_syn', filename_GABAA)
            tp.DumpLayerConnections(population, 'GABA_B_syn', filename_GABAB)
            '''
    '''
    for p in range(0, len(population_name), 1):

        population = population_name[p]['population']
        p_name = population_name[p]['name']
        filename_nodes = data_folder + '/gid_' + p_name + '.dat'

        tp.DumpLayerNodes(population, filename_nodes)
    '''

    network_script = Params['network'] + '.py'
    shutil.copy2(network_script, Params['data_folder'] + network_script)

    print('end')
def plot_connections(src_nodes,
                     target_nodes,
                     layer_size,
                     save_plot=False,
                     plot_name=None,
                     font_size=16,
                     save_prefix="",
                     color_mask=None):
    """
    Plotting function for connections between nodes for visualisation and debugging purposes
    :param src_nodes: Source nodes
    :param target_nodes: Target nodes
    :param layer_size: Size of the layer
    :param save_plot: Flag for saving the plot
    :param plot_name: Name of the saved plot file. Is not taken into account if save_plot is False
    :param font_size: Font size
    :param save_prefix: Naming prefix that is used if the plot save_plot is set to true
    :param color_mask: Color mask for the color/orientation map of neurons. If none it is not taken into account
    :return None
    """
    plt.rcParams.update({"font.size": font_size})
    plt.axis(
        (-layer_size / 2., layer_size / 2., -layer_size / 2., layer_size / 2.))
    source_positions = tp.GetPosition(src_nodes)
    x_source, y_source = zip(*source_positions)
    plt.plot(x_source, y_source, 'o')

    if len(target_nodes) > 0:
        target_positions = tp.GetPosition(target_nodes)
        x_target, y_target = zip(*target_positions)
        plt.plot(x_target, y_target, 'o')

        for s in source_positions:
            for t in target_positions:
                plt.plot([s[0], t[0]], [s[1], t[1]], color="k")

    if color_mask is not None:
        plot_colorbar(plt.gcf(),
                      plt.gca(),
                      num_stim_classes=color_mask.max() + 1)
        if not type(color_mask) == np.ndarray:
            for mask in color_mask:
                area_rect = patches.Rectangle(mask["lower_left"],
                                              width=mask["width"],
                                              height=mask["height"],
                                              color=mask["color"],
                                              alpha=0.4)
                plt.gca().add_patch(area_rect)
        else:
            plt.imshow(color_mask,
                       origin=(color_mask.shape[0] // 2,
                               color_mask.shape[1] // 2),
                       extent=(-layer_size / 2., layer_size / 2.,
                               -layer_size / 2., layer_size / 2.),
                       cmap=custom_cmap(color_mask.max() + 1),
                       alpha=0.4)

    plt.xlabel("Tissue in X")
    plt.ylabel("Tissue in Y")
    if plot_name is None:
        plot_name = "connections.png"
    if save_plot:
        curr_dir = os.getcwd()
        Path(curr_dir + "/figures/connections/").mkdir(exist_ok=True,
                                                       parents=True)
        plt.savefig(curr_dir + "/figures/connections/%s_%s" %
                    (save_prefix, plot_name))
        plt.close()
    else:
        plt.show()
Beispiel #26
0
def run(data):
    # print(data)
    logs = []

    logs.append(log('Get request'))
    simtime = data.get('time', 1000.0)
    kernel = data.get('kernel', {})
    models = data['models']
    collections = data['collections']
    connectomes = data['connectomes']
    records = []
    collections_obj = []

    logs.append(log('Reset kernel'))
    nest.ResetKernel()

    logs.append(log('Set seed in numpy random'))
    np.random.seed(int(data.get('random_seed', 0)))

    logs.append(log('Set kernel status'))
    local_num_threads = int(kernel.get('local_num_threads', 1))
    rng_seeds = np.random.randint(0, 1000, local_num_threads).tolist()
    resolution = float(kernel.get('resolution', 1.0))
    kernel_dict = {
        'local_num_threads': local_num_threads,
        'resolution': resolution,
        'rng_seeds': rng_seeds,
    }
    nest.SetKernelStatus(kernel_dict)
    data['kernel'] = kernel_dict

    logs.append(log('Collect all recordables for multimeter'))
    for idx, collection in enumerate(collections):
        model = models[collection['model']]
        if model['existing'] != 'multimeter':
            continue

        if 'record_from' in model['params']:
            continue

        recs = list(filter(lambda conn: conn['source'] == idx, connectomes))
        if len(recs) == 0:
            continue

        recordable_models = []
        for conn in recs:
            recordable_model = models[collections[conn['target']]['model']]
            recordable_models.append(recordable_model['existing'])
        recordable_models_set = list(set(recordable_models))
        assert len(recordable_models_set) == 1

        recordables = nest.GetDefaults(recordable_models_set[0], 'recordables')
        model['params']['record_from'] = list(map(str, recordables))

    logs.append(log('Copy models'))
    for new, model in models.items():
        params_serialized = serialize.model_params(model['existing'],
                                                   model['params'])
        nest.CopyModel(model['existing'], new, params_serialized)

    logs.append(log('Create collections'))
    for idx, collection in enumerate(collections):
        collections[idx]['idx'] = idx
        if 'spatial' in collection:
            specs = collection['spatial']
            specs['elements'] = collection['model']
            obj = tp.CreateLayer(serialize.layer(specs))
            if 'positions' in specs:
                positions = specs['positions']
            else:
                positions = tp.GetPosition(nest.GetNodes(obj)[0])
            positions = np.round(positions, decimals=2).astype(float)
            collections[idx]['spatial']['positions'] = positions.tolist()
            collections[idx]['n'] = positions.shape[0]
            collections[idx]['ndim'] = positions.ndim
            collections[idx]['global_ids'] = nest.GetNodes(obj)[0]
        else:
            n = int(collection.get('n', 1))
            obj = nest.Create(collection['model'], n)
            collections[idx]['global_ids'] = list(obj)
            if collection['element_type'] == 'recorder':
                model = models[collection['model']]
                record = {
                    'recorder': {
                        'global_ids': list(obj),
                        'idx': idx,
                        'model': model['existing']
                    }
                }
                if 'record_from' in model['params']:
                    record['record_from'] = model['params']['record_from']
                records.append(record)
        collections_obj.append(obj)

    logs.append(log('Connect collections'))
    for connectome in connectomes:
        source = collections[connectome['source']]
        target = collections[connectome['target']]
        source_obj = collections_obj[connectome['source']]
        target_obj = collections_obj[connectome['target']]
        if ('spatial' in source) and ('spatial' in target):
            projections = connectome['projections']
            tp.ConnectLayers(source_obj, target_obj,
                             serialize.projections(projections))
        else:
            conn_spec = connectome.get('conn_spec', 'all_to_all')
            syn_spec = connectome.get('syn_spec', 'static_synapse')
            # NEST 2.18
            source_nodes = getNodes(source_obj, source)
            target_nodes = getNodes(target_obj, target)
            if 'tgt_idx' in connectome:
                tgt_idx = connectome['tgt_idx']
                if len(tgt_idx) > 0:
                    if isinstance(tgt_idx[0], int):
                        source = source_nodes
                        target = np.array(target_nodes)[tgt_idx].tolist()
                        nest.Connect(source_nodes, target,
                                     serialize.conn(conn_spec),
                                     serialize.syn(syn_spec))
                    else:
                        for idx in range(len(tgt_idx)):
                            target = np.array(target_nodes)[
                                tgt_idx[idx]].tolist()
                            if 'src_idx' in connectome:
                                src_idx = connectome['src_idx']
                                source = np.array(source_nodes)[
                                    src_idx[idx]].tolist()
                            else:
                                source = [source_nodes[idx]]
                            nest.Connect(source, target,
                                         serialize.conn(conn_spec),
                                         serialize.syn(syn_spec))
            else:
                nest.Connect(source_nodes, target_nodes,
                             serialize.conn(conn_spec),
                             serialize.syn(syn_spec))
            # NEST 3
            # nest.Connect(source_obj, target_obj, serialize.conn(conn_spec), serialize.syn(syn_spec))

    logs.append(log('Start simulation'))
    nest.Simulate(float(simtime))

    logs.append(log('End simulation'))
    data['kernel']['time'] = nest.GetKernelStatus('time')

    logs.append(log('Serialize recording data'))
    ndigits = int(-1 * np.log10(resolution))
    for idx, record in enumerate(records):
        records[idx]['idx'] = idx
        if record['recorder']['model'] == 'spike_detector':
            neuron, rec = 'source', 'target'
        else:
            rec, neuron = 'source', 'target'
        global_ids = []
        for connectome in connectomes:
            if connectome[rec] == record['recorder']['idx']:
                collection = collections[connectome[neuron]]
                global_ids.extend(collection['global_ids'])
            records[idx]['global_ids'] = global_ids
            recorder_obj = collections_obj[record['recorder']['idx']]
            events = serialize.events(recorder_obj, ndigits)
            records[idx]['events'] = events
            records[idx]['senders'] = list(set(events['senders']))
    data['records'] = records

    return {'data': data, 'logs': logs}
Beispiel #27
0
B_top = nest.GetStatus(RG, 'topology')[0]
ctr_id = topo.GetElement(
    RG,
    [int(B_top['rows'] / 2), int(B_top['columns'] / 2)])

# find excitatory element in B
E_id = [gid for gid in ctr_id if nest.GetStatus([gid], 'model')[0] == 'E']

# get all targets, split into excitatory and inhibitory
alltgts = nest.GetStatus(
    nest.GetConnections(E_id, synapse_model='static_synapse'), 'target')
Etgts = [t for t in alltgts if nest.GetStatus([t], 'model')[0] == 'E']
Itgts = [t for t in alltgts if nest.GetStatus([t], 'model')[0] == 'I']

# obtain positions of targets
Etpos = tuple(zip(*topo.GetPosition(Etgts)))
Itpos = tuple(zip(*topo.GetPosition(Itgts)))

# plot excitatory
pylab.clf()
pylab.subplot(121)
pylab.scatter(Etpos[0], Etpos[1])
ctrpos = pylab.array(topo.GetPosition(E_id)[0])
ax = pylab.gca()
ax.add_patch(
    pylab.Circle(ctrpos, radius=0.02, zorder=99, fc='r', alpha=0.4, ec='none'))
ax.add_patch(
    pylab.Rectangle(ctrpos + pylab.array((-0.4, -0.2)),
                    0.8,
                    0.4,
                    zorder=1,
Beispiel #28
0
l1 = topo.CreateLayer(
    {'extent': [1.5, 1.5, 1.5],  # must specify 3d extent AND center
     'center': [0., 0., 0.],
     'positions': pos,
     'elements': 'iaf_psc_alpha'})

# visualize
# xext, yext = nest.GetStatus(l1, 'topology')[0]['extent']
# xctr, yctr = nest.GetStatus(l1, 'topology')[0]['center']

# l1_children is a work-around until NEST 3.0 is released
l1_children = nest.hl_api.GetChildren(l1)[0]

# extract position information, transpose to list of x, y and z positions
xpos, ypos, zpos = zip(*topo.GetPosition(l1_children))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xpos, ypos, zpos, s=15, facecolor='b', edgecolor='none')

# Gaussian connections in full volume [-0.75,0.75]**3
topo.ConnectLayers(l1, l1,
                   {'connection_type': 'divergent', 'allow_autapses': False,
                    'mask': {'volume': {'lower_left': [-0.75, -0.75, -0.75],
                                        'upper_right': [0.75, 0.75, 0.75]}},
                    'kernel': {'gaussian': {'p_center': 1., 'sigma': 0.25}}})

# show connections from center element
# sender shown in red, targets in green
ctr = topo.FindCenterElement(l1)
xtgt, ytgt, ztgt = zip(*topo.GetTargetPositions(ctr, l1)[0])
Beispiel #29
0
#! below by updating the ``'elements'`` dictionary entry for each
#! population.

#! Retina
#! ------
layerProps.update({'elements': 'RetinaNode'})
retina = topo.CreateLayer(layerProps)

#! Now set phases of retinal oscillators; we use a list comprehension instead
#! of a loop.
[
    nest.SetStatus(
        [n], {
            "phi":
            phiInit(
                topo.GetPosition([n])[0], Params["lambda_dg"],
                Params["phi_dg"])
        }) for n in nest.GetLeaves(retina)[0]
]

#! Thalamus
#! --------

#! We first introduce specific neuron models for the thalamic relay
#! cells and interneurons. These have identical properties, but by
#! treating them as different models, we can address them specifically
#! when building connections.
#!
#! We use a list comprehension to do the model copies.
[
    nest.CopyModel('ThalamicNeuron', SpecificModel)
Beispiel #30
0
pylab.clf()

# plot targets of neurons in different grid locations
for ctr in [[15, 15]]:
    # obtain node id for center: pick first node of composite
    ctr_id = topo.GetElement(a, ctr)

    # get all projection targets of center neuron
    tgts = [ci[1] for ci in nest.GetConnections(ctr_id)]

    # get positions of targets
    tpyr = pylab.array(
        tuple(
            zip(*[
                topo.GetPosition([n])[0] for n in tgts
                if nest.GetStatus([n], 'model')[0] == 'pyr'
            ])))
    tin = pylab.array(
        tuple(
            zip(*[
                topo.GetPosition([n])[0] for n in tgts
                if nest.GetStatus([n], 'model')[0] == 'in'
            ])))

    # scatter-plot
    pylab.scatter(tpyr[0] - 0.02, tpyr[1] - 0.02, 20, 'b', zorder=10)
    pylab.scatter(tin[0] + 0.02, tin[1] + 0.02, 20, 'r', zorder=10)

    # mark locations with background grey circle
    pylab.plot(tpyr[0],