def simulate(seed, dt):
    """
    simulate 10 progressive recall of the original
    pattern at different dt according to the STDP 
    rule found experimentally


    """
    ncells = 3000 # number of cells
    c = 0.5       # connection probability 
    a = 0.1       # activity of a pattern
    m = 50        # number of patterns to store
    g1 = 0.433    # slope of inhibitory component
    
    W = topology(ncells, c, seed)
    Z = Pattern(ncells, a)
    Y = generate(Z, m)

    J = clipped_Hebbian(Y, W)
    J = J*delta_t(dt)


    overlap = np.empty(10)
    X = Y[0] # initial pattern

    for i in range(10):
        h = np.inner(J.T, X)/float(ncells)
        spk_avg = np.mean(X)
        X = ( h < g1*spk_avg ).choose(1,0)
        overlap[i] = get_overlap(Z, X)

    return(overlap)
def calculate_overlap(vector, dt_list):
    """ 
    perform 50 simulations with different seed for the connectivity 
    matrix (W)

    X   -- a vector of initial activities
    dt  -- time between pre and post synaptic timing for plasticity rule
    
    mynetwork = Simulation(3000, 0.5, 0.1, 50, 0.433, 0)

    # a vector of 50 simulations
    np.savetxt('./data/[dt=%2d].out')
    """
    ncells = 3000   # number of cells
    c = 0.5         # connection probability
    a = 0.1         # activity of a pattern
    m = 50          # number of patterns to store
    g1 = 0.433      # slope of inhibitory component

    nrecall = 10 # progressive recalls

    seedlist = np.arange(50, 2500, 50) # 49 seeds


    mynetwork = Simulation(ncells, c, a, m, g1, 0  ) # first seed is zero
    mynetwork.J = mynetwork.J*delta_t(dt)

    # create 
    for s in seedlist:
        mynetwork(seed=s) # create new topology
        # compute the firings after 10 recalls
        firings = mynetwork.recall(X=vector, nrecall=10)        
        for t in dt_list:
            mynetwor.J = mynetwork
        
        overlaps = [for n in 10 get_overlap(mynetwork.Z, firings[n])]
from plots import raster_plot
from STDP import delta_t

import numpy as np

n = 3000
c = 0.5
a = 0.1
g = 0.433

W = topology(n, c)
Z = Pattern(n, a)
Y = generate(Z, m = 50)

J = clipped_Hebbian(Y, W) # 8.93 s
J = J*delta_t(50)

# create a random distortion of Z
# 10% of the initial ones
b1 = 0.1
#bn = 1
X = np.zeros(n, dtype=int)

similarity =  int(n*a*b1)
X[:similarity] = 1

# recovery in 10 steps
spikes = np.empty((11, n))
spikes[0] = X

for i in range(1, 11):
        X = ( h < g1*spk_avg ).choose(1,0)
        overlap[i] = get_overlap(Z, X)

    return(overlap)
        

if __name__ == '__main__':
    # 50 seeds per simulation
    myseed = np.arange(0, 2500, 50)
    # 9 timmings
    mydt = np.array([-100, -50, -25, -10, 0, 10, 25, 50, 100])

    myseed_list = list()
    for s in myseed:
        W, Z, Y, J = create_synaptic_weights(seed=s)

        collecdt = np.empty([len(mydt),11])
        for j,t in enumerate(mydt):
            Jnew = J*delta_t(t)
            collecdt[j] = recall(Z, Jnew)
        myseed_list.append(collecdt)

    # reorder to save output in files
    for t, i in enumerate(mydt):
        dt_tmp = [myseed_list[x][t] for x in range(len(myseed_list))]   
        np.savetxt('./data/overlap[dt=%d].out'%(i), dt_tmp, fmt='%f')