예제 #1
0
def _update_num_aff_connections(net_id=0, verbose=False):
    """
    We need to set for all NormProjections the number of afferent
    connections for each projection (across multiple projections,
    otherwise pre_rank[i].size() would be sufficient.

    Attention:

    This function should be used only internally!!! The function is
    called during Compiler.compile() call.
    """
    # Do we need to execute this procedure?
    need_to_execute = False
    for proj in Global.projections():
        if isinstance(proj, NormProjection):
            need_to_execute = True
            break

    if need_to_execute == False:
        return

    # iterate over all populations
    for pop in Global.populations(net_id):
        nb_synapses = np.zeros((pop.size))

        # get afferent projections of this layer
        aff_proj = Global.projections(net_id, pop.name)

        # we accumulate the number of synapses per dendrite
        # across all afferent projections of this layer.
        for i, proj in enumerate(aff_proj):
            # nb synapses per dendrite is oriented at the post_ranks list. If a neuron
            # does not receive any connection in THIS projection there is no entry
            nb_synapses_per_dend = np.array(proj.nb_synapses_per_dendrite())
            if len(nb_synapses_per_dend) == 0:
                continue

            # Update global count
            for idx, rank in enumerate(proj.post_ranks):
                nb_synapses[rank] += nb_synapses_per_dend[idx]

        # set number of afferent connections for correct normalization
        for i, proj in enumerate(aff_proj):
            if not isinstance(proj, NormProjection):
                continue

            if verbose:
                print('Update:', proj.pre.name, '->', proj.post.name, '(',
                      proj.target, ') -- (', i + 1, 'of', len(aff_proj), ')')
                if len(proj.post_ranks) != proj.post.size:
                    print('ranks:', proj.post_ranks)

            nb_synapses_per_dend = np.array(proj.nb_synapses_per_dendrite())

            if verbose:
                print('before:', nb_synapses_per_dend)
            for idx, rank in enumerate(proj.post_ranks):
                nb_synapses_per_dend[idx] = nb_synapses[rank]

            if verbose:
                print('after:', nb_synapses_per_dend)
            proj.cyInstance.set_semiglobal_attribute_all(
                "nb_aff_synapse", nb_synapses_per_dend, "double")