Esempio n. 1
0
def main_CRCNS (data_path,recording_length,window_length,overlap):
    '''
    This is the main function that applies the cox method on the pickle data saved by CRCNS_to_COX. This function plots the \
    adjacency matrix of connection weights in a window-based manner.

    :param data_path: The path to the pickle data file.
    :param recording_length: Total duration of recording of the dataset. This can be found in the metadata-tables of the \
    experiments too, e.g. in case of ec012ec.187 experiment.
    :param window_length: The length of the window on which the cox method is going to apply on.
    :param overlap: the overlap of the window with its previous window.

    Main internal variables:

    * nn: Total number of neurons in the network.
    * p: Number of reference neurons in the network.
    * win_num: Number of windows on data based on the length of data, length of window and overlap size.
    * final_adjacency: The result of the function in form of adjaceency matrix.
    * l_band: Lower band of the window.
    * u_band: Higher band of the window.
    * betahats: Matrix of betahat values in the network.
    * betacis: Matrix of confidence interval of betahat values in the network.
    * Xs: Reference neuron indices (x-axis) for drawing the final connectivity matrix
    * Ys: Target neuron indices (y-axis) for drawing the final connectivity matrix
    * Ss: Strength of the connection between each target neuron and corresponding reference neurons
    '''
    with open (data_path + '/crcns_data.pickle','rb') as handle:
        spikes = pickle.load(handle)
    nn = len(spikes) # Total number of neurons in the network
    p = nn-1 # Number of reference neurons in the network
    radius_to_add =2
    win_num = int(float(recording_length)/(window_length-overlap)) # Number of windows on data based on the length of data, length of window and overlap size.
    print "number of windows:%d"%win_num
    final_adjacency = zeros ([nn,nn]) # The result of the function in form of adjaceency matrix
    for win_n in range (win_num):
        l_band = win_n * window_length if win_n == 0 else (win_n * window_length)-overlap # Lower band of the window
        u_band = l_band + window_length # Higher band of the window.
        temp_spikes = {}
        for qp in spikes:
            temp_spikes[qp] = array([tt for tt in spikes[qp] if (tt <u_band and tt>l_band)])
        temp_lengths = array([len(temp_spikes[q]) for q in temp_spikes])
        non_small_indices = array(where(temp_lengths>256)[0])
        selected_spikes = {}
        for non_small in non_small_indices :
            selected_spikes[non_small] = temp_spikes[non_small]
        nn_for_cox = len(non_small_indices)
        betahats = zeros((nn_for_cox,nn_for_cox)) # Matrix of betahat values in the network.
        betacis = zeros ((nn_for_cox*3-1 , nn_for_cox)) # Matrix of confidence interval of betahat values in the network.
        for neuron in range ( 0, nn_for_cox) :
            target_b = selected_spikes[non_small_indices[neuron]]
            if (neuron == 0):
                print ("Windows No. %d Length of each train in average: " %win_n)
                print(temp_lengths[non_small_indices].astype(int))
                print(average(non_small_indices))
            maxi_b = 0
            for i,q in list (enumerate (non_small_indices)):
                if i!= neuron and temp_lengths[q] > maxi_b :
                    maxi_b = temp_lengths[q]
            ref_b = zeros((maxi_b,nn_for_cox-1))-1
            idx = 0
            for q in range ( 0, nn_for_cox):
                if non_small_indices[q] != non_small_indices[neuron] :
                    ref_b[0:temp_lengths[non_small_indices[q]],idx] = selected_spikes[non_small_indices[q]]
                    idx+=1
            tsp_b = ref_b
            delta = zeros ([nn_for_cox-1])
            cox = Cox_Method(nn_for_cox,maxi_b , target_b, tsp_b.astype(int), delta)
            betahat, betaci = cox.alg1()
            if (neuron == 0):
                    betahats[0,1:] = betahat
                    betacis[0:2,1:] = betaci.T
            elif (neuron == p):
                betahats [neuron,0:neuron] = betahat
                betacis [nn*3-3:,0:neuron] = betaci.T
            else:
                betahats [neuron,0:neuron] = betahat[0:neuron]
                betahats [neuron,neuron+1:] = betahat [neuron:]
                ind_temp = 3*(neuron+1) - 3
                betacis [ind_temp:ind_temp+2 , 0:neuron] = betaci.T [:,0:neuron]
                betacis [ind_temp:ind_temp+2 , neuron+1:] = betaci.T [:, neuron:]
        fro = array ([])
        to = array ([])
        p_temp = 0
        for i in range (0,nn_for_cox):
            for j in range (0,nn_for_cox):
                if (betahats[i,j]>0):
                    if ((betacis[p_temp+1,j]> 0 and betacis[p_temp,j]>0) or (betacis[p_temp+1,j]< 0 and betacis[p_temp,j]< 0)):
                        final_adjacency[non_small_indices[j], non_small_indices[i]] += radius_to_add
                        fro = append(fro,non_small_indices[j])
                        to = append(to,non_small_indices[i])
            p_temp = p_temp + 3
        Xs = arange(1,nn+1) # Reference neuron indices (x-axis) for drawing the final connectivity matrix
        Ys = arange(1,nn+1) # Target neuron indices (y-axis) for drawing the final connectivity matrix
        Ss = ones([nn])*10 #  Strength of the connection between each target neuron and corresponding reference neurons
        for y in range(nn) :
            for x in range(nn) :
                if final_adjacency[x,y] != 0 :
                    Xs = append(Xs,x)
                    Ys= append(Ys,y)
                    Ss = append(Ss,final_adjacency[x,y] )
    with open ('PATH/Xs','wb') as h1:
        pickle.dump(Xs, h1)
    with open ('PATH/Ys','wb') as h2:
        pickle.dump(Ys, h2)
    with open ('PATH/Ss','wb') as h3:
        pickle.dump(Ss, h3)

    fig = figure ( )
    ax = fig.gca()
    ax.set_xticks(arange(-0.5,35.5,1))
    ax.set_yticks(arange(-0.5,35.5,1))
    scatter (Xs,Ys,s=Ss)
    axis('equal')
    ax.set_xlim([0,35])
    ax.set_ylim([0,35])
    grid()
    show()
Esempio n. 2
0
def main_rst(data_path,nn):
    '''
    This is the main function that applies the cox method on the rst data prodcued by ELIF simulator that is based on \
    ELIF model (the software can be found from the following web-site: http://www.tech.plymouth.ac.uk/infovis.) This \
    function plots the resulted connectivity map.

    :param data_path: The path to the .rst data file.
    :param nn: Total number of neurons in the network.

    Main internal variables:

    * p: Number of reference neurons in the network.
    * betahats: Matrix of betahat values in the network.
    * betacis: Matrix of confidence interval of betahat values in the network.
    * target:  Target spike train
    * maxi: The length of reference spike trains varies. This parameter defines the length of the longest reference spike train.
    * tsp: The matrix of reference spike trains. The size of this matrix is (maxi x nn-1) where each column corresponds\
    to a reference spike train and in case the length of the spike train is shorter than "maxi", it should be padded by zeros.
    * to_r: The output matrix of the main_rst function expressing the connections and corresponding reference and target neurons. 
    '''
    p = nn - 1 # Number of reference neurons in the network
    betahats = zeros((nn,nn)) # Matrix of betahat values in the network
    betacis = zeros ((nn*3-1 , nn)) # Matrix of confidence interval of betahat values in the network.
    for neuron in range (0,nn):
        with open (data_path ,'rb') as f:
            a = f.read().split()
            a = map(int,a)
            each = (len(a)+1)/nn
            target = a[neuron*each:neuron*each+each] # Target spike train
            target = nonzero(target)[0]
            target = target + 1
            lengths = zeros (nn)
            lengths_b = zeros (nn)
            for i in range(0,nn):
                lengths[i] = len(nonzero(a[(i*each):(i+1)*each])[0])
            if (neuron == 0):
                print ("Length of each train in average: ")
                print(lengths.astype(int))
                print(average(lengths))
            if (neuron == 0):
                maxi = max(lengths[1:])
            elif (neuron == p):
                maxi = max(lengths[0:nn])
            else:
                maxi = max([max(lengths[0:neuron]),max(lengths[neuron+1:])])
            tsp = zeros((maxi,nn-1))-1 #The matrix of reference spike trains. The size of this matrix is (maxi x nn-1) where each column corresponds to a reference spike train and in case the length of the spike train is shorter than "maxi", it should be padded by zeros.
            if (neuron==0):
                for i in range (0,p):
                    tsp[0:lengths[i+1],i] = nonzero(a[((i+1)*each):(i+2)*each])[0]+1
            elif (neuron == p):
                for i in range (0,p):
                    tsp[0:lengths[i],i] = nonzero(a[(i*each):(i+1)*each])[0]+1

            else:
                for i in range (0,neuron):
                    tsp[0:lengths[i],i] = nonzero(a[(i*each):(i+1)*each])[0]+1
                for i in range (neuron+1,nn):
                    tsp[0:lengths[i],i-1] = nonzero(a[(i*each):(i+1)*each])[0]+1
        delta = zeros ([p])
        cox = Cox_Method(nn,maxi,target,int_(tsp),delta)
        betahat,betaci = cox.alg2()
        if (neuron == 0):
                betahats[0,1:] = betahat
                betacis[0:2,1:] = betaci.T
        elif (neuron == p):
            betahats [neuron,0:neuron] = betahat
            betacis[nn*3-3:,0:neuron] = betaci.T
        else:
            betahats [neuron,0:neuron] = betahat[0:neuron]
            betahats [neuron,neuron+1:] = betahat [neuron:]
            ind_temp = 3*(neuron+1) - 3
            betacis [ind_temp:ind_temp+2 , 0:neuron] = betaci.T [:,0:neuron]
            betacis [ind_temp:ind_temp+2 , neuron+1:] = betaci.T [:, neuron:]
        print "Neuron " + str(neuron+1) + " out of " + str(nn) + " finished at %s."  %datetime.now()

    for row in betahats:
        row = list(row)

    print (list(betahats))
    print("\n\n\n\n\n")
    print (betacis)

    p = 0
    fro = []
    to = []
    thickness = []
    for i in range (0,nn):
        for j in range (0,nn):
            if (betahats[i,j]>0):
                if ((betacis[p+1,j]> 0.005 and betacis[p,j]>0.005) or (betacis[p+1,j]< -0.005 and betacis[p,j]< -0.005)):
                    fro =  append(fro,j+1)
                    to = append(to,i+1)
                    thickness = append(thickness, betahats[i,j])
        p = p + 3
    to_r = append (append([fro], [to],axis=0), [thickness], axis = 0)
    to_file = to_r.T
    print("\n\n\n\n\n")
    print (to_file)