<jupyter_start><jupyter_text>Disambiguation<jupyter_code>import pprint
import subprocess
import sys 
sys.path.append('../')

import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1 import make_axes_locatable
import seaborn as sns

%matplotlib inline
plt.rcParams['figure.figsize'] = (12.9, 12)

np.set_printoptions(suppress=True, precision=5)

sns.set(font_scale=3.5)

from network import Protocol, NetworkManager, BCPNNPerfect, TimedInput
from connectivity_functions import create_orthogonal_canonical_representation, build_network_representation
from connectivity_functions import get_weights_from_probabilities, get_probabilities_from_network_representation
from analysis_functions import calculate_recall_time_quantities, get_weights
from analysis_functions import get_weights_collections
from plotting_functions import plot_network_activity_angle, plot_weight_matrix
from analysis_functions import calculate_angle_from_history, calculate_winning_pattern_from_distances
from analysis_functions import calculate_patterns_timings
epsilon = 10e-20
def produce_overlaped_sequences(minicolumns, hypercolumns, n_patterns, s, r, mixed_start=False, contiguous=True):
    
    n_r = int(r * n_patterns/2)
    n_s = int(s * hypercolumns)
    n_size = int(n_patterns / 2)

    matrix = create_orthogonal_canonical_representation(minicolumns, hypercolumns)[:n_patterns]
    sequence1 = matrix[:n_size]
    sequence2 = matrix[n_size:]
 
    if mixed_start:
        start_index = 0
        end_index = n_r 
    else:
        start_index = max(int(0.5 * (n_size - n_r)), 0)
        end_index = min(start_index + n_r, n_size)
    
    for index in range(start_index, end_index):
        if contiguous:
            sequence2[index, :n_s] = sequence1[index, :n_s]
        else:
            sequence2[index, ...] = sequence1[index, ...]
            sequence2[index, n_s:] = n_patterns + index 
        
    if False:
        print(n_r)
        print(n_size)
        print(start_index)
        print(end_index)
           
    return sequence1, sequence2

def create_weights_from_two_sequences(nn, dt, n_patterns, s, r, mixed_start, contiguous, 
                                     training_time, inter_pulse_interval, inter_sequence_interval, 
                                      epochs, resting_time):
    filtered = True
    minicolumns = nn.minicolumns
    hypercolumns = nn.hypercolumns
       
    tau_z_pre_ampa = nn.tau_z_pre_ampa
    tau_z_post_ampa = nn.tau_z_post_ampa
    
    seq1, seq2 = produce_overlaped_sequences(minicolumns, hypercolumns, n_patterns, s, r, 
                                             mixed_start=mixed_start, contiguous=contiguous)

    nr1 = build_network_representation(seq1, minicolumns, hypercolumns)
    nr2 = build_network_representation(seq2, minicolumns, hypercolumns)

    # Get the first
    timed_input = TimedInput(nr1, dt, training_time, inter_pulse_interval=inter_pulse_interval,
                             inter_sequence_interval=inter_pulse_interval, epochs=epochs, 
                             resting_time=resting_time)
    
    S = timed_input.build_timed_input()
    z_pre = timed_input.build_filtered_input_pre(tau_z_pre_ampa)
    z_post = timed_input.build_filtered_input_post(tau_z_post_ampa)


    pi1, pj1, P1 = timed_input.calculate_probabilities_from_time_signal(filtered=filtered)
    w_timed1 = get_weights_from_probabilities(pi1, pj1, P1, minicolumns, hypercolumns)
    t1 = timed_input.T_total

    # Get the second
    timed_input = TimedInput(nr2, dt, training_time, inter_pulse_interval=inter_pulse_interval,
                             inter_sequence_interval=inter_pulse_interval, epochs=epochs,
                             resting_time=resting_time)
    
    S = timed_input.build_timed_input()
    z_pre = timed_input.build_filtered_input_pre(tau_z_pre_ampa)
    z_post = timed_input.build_filtered_input_post(tau_z_post_ampa)
    t2 = timed_input.T_total

    pi2, pj2, P2 = timed_input.calculate_probabilities_from_time_signal(filtered=filtered)
    w_timed2 = get_weights_from_probabilities(pi2, pj2, P2, minicolumns, hypercolumns)
    t_total = t1 + t2

    # Mix
    pi_total = (t1 / t_total) * pi1 + ((t_total - t1)/ t_total) * pi2
    pj_total = (t1 / t_total) * pj1 + ((t_total - t1)/ t_total) * pj2
    P_total = (t1 / t_total) * P1 + ((t_total - t1)/ t_total) * P2
    w_total, beta = get_weights_from_probabilities(pi_total, pj_total, P_total, minicolumns, hypercolumns)
    
    return seq1, seq2, nr1, nr2, w_total, beta

def calculate_recall_success_nr(manager, nr, T_recall, T_cue, debug=False, remove=0.020):
    n_seq = nr.shape[0] 
    I_cue = nr[0]
  
    # Do the recall
    manager.run_network_recall(T_recall=T_recall, I_cue=I_cue, T_cue=T_cue,
                           reset=True, empty_history=True)
    distances = calculate_angle_from_history(manager)
    winning = calculate_winning_pattern_from_distances(distances)
    timings = calculate_patterns_timings(winning, manager.dt, remove=remove)
    pattern_sequence = [x[0] for x in timings]
    # Calculate whether it was succesfull 
    success = 1.0
    for index, pattern_index in enumerate(pattern_sequence[:n_seq]):
        pattern = manager.patterns_dic[pattern_index]
        goal_pattern = nr[index]

        if not np.array_equal(pattern, goal_pattern):
 
            success = 0.0
            break
    if debug:
        return success, timings, pattern_sequence
    else:
        return success<jupyter_output><empty_output><jupyter_text>An example<jupyter_code>always_learning = False
strict_maximum = True
perfect = False
z_transfer = False
k_perfect = True
diagonal_zero = False
normalized_currents = True

g_w_ampa = 2.0
g_w = 0.0
g_a = 10.0
tau_a = 0.250
G = 1.0
sigma = 0.0
tau_m = 0.020
tau_z_pre_ampa = 0.025
tau_z_post_ampa = 0.025
tau_p = 10.0

hypercolumns = 1
minicolumns = 20
n_patterns = 20

# Manager properties
dt = 0.001
values_to_save = ['o', 'i_ampa', 'a']

# Protocol
training_time = 0.100
inter_sequence_interval = 0.0
inter_pulse_interval = 0.0
resting_time = 2.0
epochs = 1

# Recall
T_recall = 1.0
T_cue = 0.020

# Patterns parameters
nn = BCPNNPerfect(hypercolumns, minicolumns, g_w_ampa=g_w_ampa, g_w=g_w, g_a=g_a, tau_a=tau_a, tau_m=tau_m,
                  sigma=sigma, G=G, tau_z_pre_ampa=tau_z_pre_ampa, tau_z_post_ampa=tau_z_post_ampa, tau_p=tau_p,
                  z_transfer=z_transfer, diagonal_zero=diagonal_zero, strict_maximum=strict_maximum, 
                  perfect=perfect, k_perfect=k_perfect, always_learning=always_learning, 
                  normalized_currents=normalized_currents)


# Build the manager
manager = NetworkManager(nn=nn, dt=dt, values_to_save=values_to_save)

# Build the protocol for training
mixed_start = False
contiguous = True
s = 1.0
r = 0.3
matrix = create_orthogonal_canonical_representation(minicolumns, hypercolumns)

aux = create_weights_from_two_sequences(nn, dt, n_patterns, s, r, mixed_start, contiguous, 
                                        training_time, inter_pulse_interval=inter_pulse_interval,
                                        inter_sequence_interval=inter_sequence_interval, epochs=epochs,
                                        resting_time=resting_time) 

seq1, seq2, nr1, nr2, w_total, beta = aux

nr = np.concatenate((nr1, nr2))
aux, indexes = np.unique(nr, axis=0, return_index=True)
patterns_dic = {index:pattern for (index, pattern) in zip(indexes, aux)}


nn.w_ampa = w_total
manager.patterns_dic = patterns_dic


s = calculate_recall_success_nr(manager, nr1, T_recall, T_cue)
print('s1=', s)
plot_network_activity_angle(manager)
s = calculate_recall_success_nr(manager, nr2, T_recall, T_cue)
print('s2=', s)
plot_network_activity_angle(manager)
plot_weight_matrix(nn, ampa=True)<jupyter_output><empty_output><jupyter_text>More systematic<jupyter_code>%%time

always_learning = False
strict_maximum = True
perfect = False
z_transfer = False
k_perfect = True
diagonal_zero = False
normalized_currents = True

g_w_ampa = 2.0
g_w = 0.0
g_a = 10.0
tau_a = 0.250
g_beta = 1.0
G = 1.0
sigma = 0.0
tau_m = 0.010
tau_z_pre_ampa = 0.050
tau_z_post_ampa = 0.005
tau_p = 10.0

hypercolumns = 1
minicolumns = 20
n_patterns = 20

# Manager properties
dt = 0.001
values_to_save = ['o', 'i_ampa', 'a']

# Protocol
training_time = 0.100
inter_sequence_interval = 0.0
inter_pulse_interval = 0.0
epochs = 1
mixed_start = False
contiguous = True
s = 1.0
r = 0.25

# Recall
T_recall = 1.0
T_cue = 0.020

num = 10
r_space = np.linspace(0, 0.9, num=num)
success_vector = np.zeros(num)
factor = 0.2

g_w_ampa * (w_total[0, 0] - w_total[2, 0])
for r_index, r in enumerate(r_space):
    print('r_index', r_index)
    # The network
    nn = BCPNNPerfect(hypercolumns, minicolumns, g_w_ampa=g_w_ampa, g_w=g_w, g_a=g_a, tau_a=tau_a, tau_m=tau_m,
                      sigma=sigma, G=G, tau_z_pre_ampa=tau_z_pre_ampa, tau_z_post_ampa=tau_z_post_ampa, tau_p=tau_p,
                      z_transfer=z_transfer, diagonal_zero=diagonal_zero, strict_maximum=strict_maximum, 
                      perfect=perfect, k_perfect=k_perfect, always_learning=always_learning, 
                      normalized_currents=normalized_currents, g_beta=g_beta)


    # Build the manager
    manager = NetworkManager(nn=nn, dt=dt, values_to_save=values_to_save)

    # The sequences
    matrix = create_orthogonal_canonical_representation(minicolumns, hypercolumns)
    aux = create_weights_from_two_sequences(nn, dt, n_patterns, s, r, mixed_start, contiguous, 
                                        training_time, inter_pulse_interval=inter_pulse_interval,
                                        inter_sequence_interval=inter_sequence_interval, epochs=epochs,
                                        resting_time=resting_time) 
    seq1, seq2, nr1, nr2, w_total, beta = aux
    nr = np.concatenate((nr1, nr2))
    aux, indexes = np.unique(nr, axis=0, return_index=True)
    patterns_dic = {index:pattern for (index, pattern) in zip(indexes, aux)}
    nn.w_ampa = w_total
    nn.beta = beta
    manager.patterns_dic = patterns_dic
    
    current = g_w_ampa * (w_total[0, 0] - w_total[2, 0])
    noise = factor * current
    nn.sigma = noise

    # Recall
    aux = calculate_recall_success_nr(manager, nr1, T_recall, T_cue, debug=True, remove=0.020)
    s1, timings, pattern_sequence = aux
    print('1', s1, pattern_sequence, seq1)
    aux = calculate_recall_success_nr(manager, nr2, T_recall, T_cue, debug=True, remove=0.020)
    s2, timings, pattern_sequence = aux
    print('2', s2, pattern_sequence, seq2)

    success_vector[r_index] = 0.5 * (s1 + s2)
markersize = 15
linewdith = 8

fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)

ax.plot(r_space, success_vector, 'o-', lw=linewdith, ms=markersize)

ax.axhline(0, ls='--', color='gray')
ax.axvline(0, ls='--', color='gray')

ax.set_xlabel('Overlap')
ax.set_ylabel('Recall')<jupyter_output><empty_output><jupyter_text>tau_z<jupyter_code>%%time

always_learning = False
strict_maximum = True
perfect = False
z_transfer = False
k_perfect = True
diagonal_zero = False
normalized_currents = True

g_w_ampa = 2.0
g_w = 0.0
g_a = 10.0
tau_a = 0.250
G = 1.0
sigma = 0.0
tau_m = 0.010
tau_z_pre_ampa = 0.025
tau_z_post_ampa = 0.025
tau_p = 10.0

hypercolumns = 1
minicolumns = 20
n_patterns = 20

# Manager properties
dt = 0.001
values_to_save = ['o']

# Protocol
training_time = 0.100
inter_sequence_interval = 0.0
inter_pulse_interval = 0.0
epochs = 1
mixed_start = False
contiguous = True
s = 1.0
r = 0.25

# Recall
T_recall = 1.0
T_cue = 0.020

num = 10
r_space = np.linspace(0, 0.9, num=num)
success_vector = np.zeros(num)

tau_z_list = [0.025, 0.035, 0.050, 0.075]
#tau_z_list = [0.025, 0.100, 0.250]
#tau_z_list = [0.025, 0.050]


success_list = []


for tau_z_pre_ampa in tau_z_list:
    success_vector = np.zeros(num)
    print(tau_z_pre_ampa)
    for r_index, r in enumerate(r_space):
        # The network
        nn = BCPNNPerfect(hypercolumns, minicolumns, g_w_ampa=g_w_ampa, g_w=g_w, g_a=g_a, tau_a=tau_a, tau_m=tau_m,
                          sigma=sigma, G=G, tau_z_pre_ampa=tau_z_pre_ampa, tau_z_post_ampa=tau_z_post_ampa, tau_p=tau_p,
                          z_transfer=z_transfer, diagonal_zero=diagonal_zero, strict_maximum=strict_maximum, 
                          perfect=perfect, k_perfect=k_perfect, always_learning=always_learning, 
                          normalized_currents=normalized_currents)


        # Build the manager
        manager = NetworkManager(nn=nn, dt=dt, values_to_save=values_to_save)

        # The sequences
        matrix = create_orthogonal_canonical_representation(minicolumns, hypercolumns)
        aux = create_weights_from_two_sequences(nn, dt, n_patterns, s, r, mixed_start, contiguous, 
                                        training_time, inter_pulse_interval=inter_pulse_interval,
                                        inter_sequence_interval=inter_sequence_interval, epochs=epochs,
                                        resting_time=resting_time) 
        seq1, seq2, nr1, nr2, w_total, beta = aux
        nr = np.concatenate((nr1, nr2))
        aux, indexes = np.unique(nr, axis=0, return_index=True)
        patterns_dic = {index:pattern for (index, pattern) in zip(indexes, aux)}
        nn.w_ampa = w_total
        manager.patterns_dic = patterns_dic

        # Recall
        s1 = calculate_recall_success_nr(manager, nr1, T_recall, T_cue)
        s2 = calculate_recall_success_nr(manager, nr2, T_recall, T_cue)
        success_vector[r_index] = 0.5 * (s1 + s2)
        
    success_list.append(np.copy(success_vector))
markersize = 15
linewdith = 8

fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)

for tau_z, success_vector in zip(tau_z_list, success_list):
    ax.plot(r_space, success_vector, 'o-', lw=linewdith, ms=markersize, label=str(tau_z))

ax.axhline(0, ls='--', color='gray')
ax.axvline(0, ls='--', color='gray')

ax.set_xlabel('Overlap')
ax.set_ylabel('Recall')

ax.legend();<jupyter_output><empty_output><jupyter_text>Scale<jupyter_code>%%time
always_learning = False
strict_maximum = True
perfect = False
z_transfer = False
k_perfect = True
diagonal_zero = False
normalized_currents = True

g_w_ampa = 2.0
g_w = 0.0
g_a = 10.0
tau_a = 0.250
G = 1.0
sigma = 0.0
tau_m = 0.010
tau_z_pre_ampa = 0.025
tau_z_post_ampa = 0.025
tau_p = 10.0

hypercolumns = 1
minicolumns = 20
n_patterns = 20

# Manager properties
dt = 0.001
values_to_save = ['o']

# Protocol
training_time = 0.100
inter_sequence_interval = 0.0
inter_pulse_interval = 0.0
epochs = 1
mixed_start = False
contiguous = True
s = 1.0
r = 0.25

# Recall
T_recall = 1.0
T_cue = 0.020

num = 10
r_space = np.linspace(0, 0.9, num=num)
success_vector = np.zeros(num)

hypercolumns_list = [1, 3, 7, 10]
#tau_z_list = [0.025, 0.100, 0.250]
#tau_z_list = [0.025, 0.050]


success_list = []


for hypercolumns in hypercolumns_list:
    success_vector = np.zeros(num)
    print(hypercolumns)
    for r_index, r in enumerate(r_space):
        # The network
        nn = BCPNNPerfect(hypercolumns, minicolumns, g_w_ampa=g_w_ampa, g_w=g_w, g_a=g_a, tau_a=tau_a, tau_m=tau_m,
                          sigma=sigma, G=G, tau_z_pre_ampa=tau_z_pre_ampa, tau_z_post_ampa=tau_z_post_ampa, tau_p=tau_p,
                          z_transfer=z_transfer, diagonal_zero=diagonal_zero, strict_maximum=strict_maximum, 
                          perfect=perfect, k_perfect=k_perfect, always_learning=always_learning, 
                          normalized_currents=normalized_currents)


        # Build the manager
        manager = NetworkManager(nn=nn, dt=dt, values_to_save=values_to_save)

        # The sequences
        matrix = create_orthogonal_canonical_representation(minicolumns, hypercolumns)
        aux = create_weights_from_two_sequences(nn, dt, n_patterns, s, r, mixed_start, contiguous, 
                                        training_time, inter_pulse_interval=inter_pulse_interval,
                                        inter_sequence_interval=inter_sequence_interval, epochs=epochs,
                                        resting_time=resting_time) 
        seq1, seq2, nr1, nr2, w_total, beta = aux
        nr = np.concatenate((nr1, nr2))
        aux, indexes = np.unique(nr, axis=0, return_index=True)
        patterns_dic = {index:pattern for (index, pattern) in zip(indexes, aux)}
        nn.w_ampa = w_total
        manager.patterns_dic = patterns_dic

        # Recall
        s1 = calculate_recall_success_nr(manager, nr1, T_recall, T_cue)
        s2 = calculate_recall_success_nr(manager, nr2, T_recall, T_cue)
        success_vector[r_index] = 0.5 * (s1 + s2)
        
    success_list.append(np.copy(success_vector))
markersize = 15
linewdith = 8

fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)

for hypercolumns, success_vector in zip(hypercolumns_list, success_list):
    ax.plot(r_space, success_vector, 'o-', lw=linewdith, ms=markersize, label=str(hypercolumns))

ax.axhline(0, ls='--', color='gray')
ax.axvline(0, ls='--', color='gray')

ax.set_xlabel('Overlap')
ax.set_ylabel('Recall')

ax.legend();<jupyter_output><empty_output><jupyter_text>tau_m<jupyter_code>%%time
always_learning = False
strict_maximum = True
perfect = False
z_transfer = False
k_perfect = True
diagonal_zero = False
normalized_currents = True

g_w_ampa = 2.0
g_w = 0.0
g_a = 10.0
tau_a = 0.250
G = 1.0
sigma = 0.0
tau_m = 0.010
tau_z_pre_ampa = 0.025
tau_z_post_ampa = 0.025
tau_p = 10.0

hypercolumns = 1
minicolumns = 20
n_patterns = 20

# Manager properties
dt = 0.001
values_to_save = ['o']

# Protocol
training_time = 0.100
inter_sequence_interval = 0.0
inter_pulse_interval = 0.0
epochs = 1
mixed_start = False
contiguous = True
s = 1.0
r = 0.25

# Recall
T_recall = 1.0
T_cue = 0.020

num = 10
r_space = np.linspace(0, 0.9, num=num)
success_vector = np.zeros(num)

tau_m_list = [0.001, 0.008, 0.020]
 
success_list = []


for tau_m in tau_m_list:
    success_vector = np.zeros(num)
    print(tau_m)
    for r_index, r in enumerate(r_space):
        # The network
        nn = BCPNNPerfect(hypercolumns, minicolumns, g_w_ampa=g_w_ampa, g_w=g_w, g_a=g_a, tau_a=tau_a, tau_m=tau_m,
                          sigma=sigma, G=G, tau_z_pre_ampa=tau_z_pre_ampa, tau_z_post_ampa=tau_z_post_ampa, tau_p=tau_p,
                          z_transfer=z_transfer, diagonal_zero=diagonal_zero, strict_maximum=strict_maximum, 
                          perfect=perfect, k_perfect=k_perfect, always_learning=always_learning, 
                          normalized_currents=normalized_currents)


        # Build the manager
        manager = NetworkManager(nn=nn, dt=dt, values_to_save=values_to_save)

        # The sequences
        matrix = create_orthogonal_canonical_representation(minicolumns, hypercolumns)
        aux = create_weights_from_two_sequences(nn, dt, n_patterns, s, r, mixed_start, contiguous, 
                                        training_time, inter_pulse_interval=inter_pulse_interval,
                                        inter_sequence_interval=inter_sequence_interval, epochs=epochs,
                                        resting_time=resting_time) 
        seq1, seq2, nr1, nr2, w_total, beta = aux
        nr = np.concatenate((nr1, nr2))
        aux, indexes = np.unique(nr, axis=0, return_index=True)
        patterns_dic = {index:pattern for (index, pattern) in zip(indexes, aux)}
        nn.w_ampa = w_total
        manager.patterns_dic = patterns_dic

        # Recall
        s1 = calculate_recall_success_nr(manager, nr1, T_recall, T_cue)
        s2 = calculate_recall_success_nr(manager, nr2, T_recall, T_cue)
        success_vector[r_index] = 0.5 * (s1 + s2)
        
    success_list.append(np.copy(success_vector))
markersize = 15
linewdith = 8

fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)

for tau_m, success_vector in zip(tau_m_list, success_list):
    ax.plot(r_space, success_vector, 'o-', lw=linewdith, ms=markersize, label=str(tau_m))

ax.axhline(0, ls='--', color='gray')
ax.axvline(0, ls='--', color='gray')

ax.set_xlabel('Overlap')
ax.set_ylabel('Recall')

ax.legend();<jupyter_output><empty_output><jupyter_text>training time<jupyter_code>%%time
always_learning = False
strict_maximum = True
perfect = False
z_transfer = False
k_perfect = True
diagonal_zero = False
normalized_currents = True

g_w_ampa = 2.0
g_w = 0.0
g_a = 10.0
tau_a = 0.250
G = 1.0
sigma = 0.0
tau_m = 0.010
tau_z_pre_ampa = 0.025
tau_z_post_ampa = 0.025
tau_p = 10.0

hypercolumns = 1
minicolumns = 20
n_patterns = 20

# Manager properties
dt = 0.001
values_to_save = ['o']

# Protocol
training_time = 0.100
inter_sequence_interval = 0.0
inter_pulse_interval = 0.0
epochs = 1
mixed_start = False
contiguous = True
s = 1.0
r = 0.25

# Recall
T_recall = 1.0
T_cue = 0.020

num = 10
r_space = np.linspace(0, 0.9, num=num)
success_vector = np.zeros(num)

training_time_list = [0.050, 0.100, 0.250, 0.500]

success_list = []


for training_time in training_time_list:
    success_vector = np.zeros(num)
    print(training_time)
    for r_index, r in enumerate(r_space):
        # The network
        nn = BCPNNPerfect(hypercolumns, minicolumns, g_w_ampa=g_w_ampa, g_w=g_w, g_a=g_a, tau_a=tau_a, tau_m=tau_m,
                          sigma=sigma, G=G, tau_z_pre_ampa=tau_z_pre_ampa, tau_z_post_ampa=tau_z_post_ampa, tau_p=tau_p,
                          z_transfer=z_transfer, diagonal_zero=diagonal_zero, strict_maximum=strict_maximum, 
                          perfect=perfect, k_perfect=k_perfect, always_learning=always_learning, 
                          normalized_currents=normalized_currents)


        # Build the manager
        manager = NetworkManager(nn=nn, dt=dt, values_to_save=values_to_save)

        # The sequences
        matrix = create_orthogonal_canonical_representation(minicolumns, hypercolumns)
        aux = create_weights_from_two_sequences(nn, dt, n_patterns, s, r, mixed_start, contiguous, 
                                        training_time, inter_pulse_interval=inter_pulse_interval,
                                        inter_sequence_interval=inter_sequence_interval, epochs=epochs,
                                        resting_time=resting_time) 
        seq1, seq2, nr1, nr2, w_total, beta = aux
        nr = np.concatenate((nr1, nr2))
        aux, indexes = np.unique(nr, axis=0, return_index=True)
        patterns_dic = {index:pattern for (index, pattern) in zip(indexes, aux)}
        nn.w_ampa = w_total
        manager.patterns_dic = patterns_dic

        # Recall
        s1 = calculate_recall_success_nr(manager, nr1, T_recall, T_cue)
        s2 = calculate_recall_success_nr(manager, nr2, T_recall, T_cue)
        success_vector[r_index] = 0.5 * (s1 + s2)
        
    success_list.append(np.copy(success_vector))
markersize = 15
linewdith = 8

fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)

for training_time, success_vector in zip(training_time_list, success_list):
    ax.plot(r_space, success_vector, 'o-', lw=linewdith, ms=markersize, label=str(training_time))

ax.axhline(0, ls='--', color='gray')
ax.axvline(0, ls='--', color='gray')

ax.set_xlabel('Overlap')
ax.set_ylabel('Recall')

ax.legend();<jupyter_output><empty_output><jupyter_text>Systematic with noise<jupyter_code>%%time

always_learning = False
strict_maximum = True
perfect = False
z_transfer = False
k_perfect = True
diagonal_zero = False
normalized_currents = True

g_w_ampa = 2.0
g_w = 0.0
g_a = 10.0
tau_a = 0.250
g_beta = 0.0
G = 1.0
sigma = 0.0
tau_m = 0.010
tau_z_pre_ampa = 0.050
tau_z_post_ampa = 0.005
tau_p = 10.0

hypercolumns = 1
minicolumns = 20
n_patterns = 20

# Manager properties
dt = 0.001
values_to_save = ['o', 'i_ampa', 'a']

# Protocol
training_time = 0.100
inter_sequence_interval = 0.0
inter_pulse_interval = 0.0
epochs = 1
mixed_start = False
contiguous = True
s = 1.0
r = 0.25

# Recall
T_recall = 1.0
T_cue = 0.020

num = 15
trials = 25
r_space = np.linspace(0, 0.6, num=num)
success_vector = np.zeros((num, trials))
factor = 0.1

for r_index, r in enumerate(r_space):
    print(r_index)
    # The network
    nn = BCPNNPerfect(hypercolumns, minicolumns, g_w_ampa=g_w_ampa, g_w=g_w, g_a=g_a, tau_a=tau_a, tau_m=tau_m,
                      sigma=sigma, G=G, tau_z_pre_ampa=tau_z_pre_ampa, tau_z_post_ampa=tau_z_post_ampa, tau_p=tau_p,
                      z_transfer=z_transfer, diagonal_zero=diagonal_zero, strict_maximum=strict_maximum, 
                      perfect=perfect, k_perfect=k_perfect, always_learning=always_learning, 
                      normalized_currents=normalized_currents, g_beta=g_beta)


    # Build the manager
    manager = NetworkManager(nn=nn, dt=dt, values_to_save=values_to_save)

    # The sequences
    matrix = create_orthogonal_canonical_representation(minicolumns, hypercolumns)
    aux = create_weights_from_two_sequences(nn, dt, n_patterns, s, r, mixed_start, contiguous, 
                                        training_time, inter_pulse_interval=inter_pulse_interval,
                                        inter_sequence_interval=inter_sequence_interval, epochs=epochs,
                                        resting_time=resting_time) 
    seq1, seq2, nr1, nr2, w_total, beta = aux
    nr = np.concatenate((nr1, nr2))
    aux, indexes = np.unique(nr, axis=0, return_index=True)
    patterns_dic = {index:pattern for (index, pattern) in zip(indexes, aux)}
    manager.patterns_dic = patterns_dic
    nn.w_ampa = w_total
    nn.beta = beta
    current = g_w_ampa * (w_total[0, 0] - w_total[2, 0])
    noise = factor * current
    nn.sigma = noise
    print(nn.sigma)
    # Recall
    for trial in range(trials):
        s1 = calculate_recall_success_nr(manager, nr1, T_recall, T_cue)
        s2 = calculate_recall_success_nr(manager, nr2, T_recall, T_cue)
        success_vector[r_index, trial] = 0.5 * (s1 + s2)
markersize = 15
linewdith = 8
current_palette = sns.color_palette()
index = 0
alpha = 0.5

fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)

mean_success = success_vector.mean(axis=1)
std = success_vector.std(axis=1)

ax.plot(r_space, mean_success, 'o-', lw=linewdith, ms=markersize)
ax.fill_between(r_space, mean_success - std, mean_success + std,
                    color=current_palette[index], alpha=alpha)
ax.axhline(0, ls='--', color='gray')
ax.axvline(0, ls='--', color='gray')

ax.set_xlabel('Overlap')
ax.set_ylabel('Recall')
%%time

always_learning = False
strict_maximum = True
perfect = False
z_transfer = False
k_perfect = True
diagonal_zero = False
normalized_currents = True

g_w_ampa = 2.0
g_w = 0.0
g_a = 10.0
tau_a = 0.250
g_beta = 0.0
G = 1.0
sigma = 0.0
tau_m = 0.010
tau_z_pre_ampa = 0.050
tau_z_post_ampa = 0.005
tau_p = 10.0

hypercolumns = 1
minicolumns = 20
n_patterns = 20

# Manager properties
dt = 0.001
values_to_save = ['o', 'i_ampa', 'a']

# Protocol
training_time = 0.100
inter_sequence_interval = 0.0
inter_pulse_interval = 0.0
epochs = 1
mixed_start = False
contiguous = True
s = 1.0
r = 0.25

# Recall
T_recall = 1.0
T_cue = 0.020

num = 15
trials = 25
r_space = np.linspace(0, 0.6, num=num)
success_vector = np.zeros((num, trials))
successes = []
factors = [0.0, 0.1, 0.2, 0.3]

for factor in factors:
    print(factor)
    for r_index, r in enumerate(r_space):
        print(r_index)
        # The network
        nn = BCPNNPerfect(hypercolumns, minicolumns, g_w_ampa=g_w_ampa, g_w=g_w, g_a=g_a, tau_a=tau_a, tau_m=tau_m,
                          sigma=sigma, G=G, tau_z_pre_ampa=tau_z_pre_ampa, tau_z_post_ampa=tau_z_post_ampa, tau_p=tau_p,
                          z_transfer=z_transfer, diagonal_zero=diagonal_zero, strict_maximum=strict_maximum, 
                          perfect=perfect, k_perfect=k_perfect, always_learning=always_learning, 
                          normalized_currents=normalized_currents, g_beta=g_beta)


        # Build the manager
        manager = NetworkManager(nn=nn, dt=dt, values_to_save=values_to_save)

        # The sequences
        matrix = create_orthogonal_canonical_representation(minicolumns, hypercolumns)
        aux = create_weights_from_two_sequences(nn, dt, n_patterns, s, r, mixed_start, contiguous, 
                                            training_time, inter_pulse_interval=inter_pulse_interval,
                                            inter_sequence_interval=inter_sequence_interval, epochs=epochs,
                                            resting_time=resting_time) 
        seq1, seq2, nr1, nr2, w_total, beta = aux
        nr = np.concatenate((nr1, nr2))
        aux, indexes = np.unique(nr, axis=0, return_index=True)
        patterns_dic = {index:pattern for (index, pattern) in zip(indexes, aux)}
        manager.patterns_dic = patterns_dic
        nn.w_ampa = w_total
        nn.beta = beta
        current = g_w_ampa * (w_total[0, 0] - w_total[2, 0])
        noise = factor * current
        nn.sigma = noise
        # Recall
        for trial in range(trials):
            s1 = calculate_recall_success_nr(manager, nr1, T_recall, T_cue)
            s2 = calculate_recall_success_nr(manager, nr2, T_recall, T_cue)
            success_vector[r_index, trial] = 0.5 * (s1 + s2)
            
    successes.append(np.copy(success_vector))
markersize = 15
linewdith = 8
current_palette = sns.color_palette()
index = 0
alpha = 0.5

fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)

for index, success_vector in enumerate(successes):

    mean_success = success_vector.mean(axis=1)
    std = success_vector.std(axis=1)

    ax.plot(r_space, mean_success, 'o-', lw=linewdith, ms=markersize, label=str(factors[index]))
    ax.fill_between(r_space, mean_success - std, mean_success + std,
                        color=current_palette[index], alpha=alpha)

ax.axhline(0, ls='--', color='gray')
ax.axvline(0, ls='--', color='gray')

ax.set_xlabel('Overlap')
ax.set_ylabel('Recall')
ax.legend();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09924530822146531, 'fraction_numerical': 0.031389835036398854, 'mean_word_length': 3.274479017984585, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 31, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '61788', 'license': 'MIT', 'n_tokens_mistral': 11628, 'n_tokens_neox': 10639, 'n_words': 2129, 'path': 'jupyter/2018-05-23(Disambiguation).ipynb', 'repo_name': 'h-mayorquin/BCPNN_sequences', 'size': '29946'}