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

import numpy as np
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

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

sns.set(font_scale=2.0)<jupyter_output><empty_output><jupyter_text>Git machinery<jupyter_code>run_old_version = False
if run_old_version:
    hash_when_file_was_written = '9ab30d88a8c3320bedaecd5be39cb15cdb8ba677'
    hash_at_the_moment = subprocess.check_output(["git", 'rev-parse', 'HEAD']).strip()
    print('Actual hash', hash_at_the_moment)
    
    print('Hash of the commit used to run the simulation', hash_when_file_was_written)
    subprocess.call(['git', 'checkout', hash_when_file_was_written])
from network import Protocol, BCPNNFast, NetworkManager
from analysis_functions import calculate_recall_success_sequences, calculate_excitation_inhibition_ratio
from plotting_functions import plot_weight_matrix<jupyter_output><empty_output><jupyter_text>Initial exploration Run the example<jupyter_code># Patterns parameters
hypercolumns = 4
minicolumns = 20
n_patterns = 20

dt = 0.001

values_to_save = ['o']

# Recall
n = 1
T_cue = 0.100
T_recall = 10.0

# Protocol
training_time = 0.1
inter_sequence_interval = 1.0
inter_pulse_interval = 0.0
epochs = 3

# Build the network
tau_z_pre = 0.150
nn = BCPNNFast(hypercolumns, minicolumns, tau_z_pre=tau_z_pre)

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

protocol = Protocol()
sequences = [i for i in range(n_patterns)]
protocol.simple_protocol(patterns_indexes=sequences, training_time=training_time,
                         inter_sequence_interval=inter_sequence_interval, epochs=epochs)


manager.run_network_protocol(protocol=protocol, verbose=False)<jupyter_output><empty_output><jupyter_text>Analysis<jupyter_code># Data analysis
w_ampa = nn.w_ampa
w_nmda = nn.w

w = np.copy(w_nmda)

remove_inter = False
if remove_inter:
    for i in range(hypercolumns):
        for j in range(hypercolumns):
            index1 = minicolumns * j
            index2 = minicolumns * i
            indexes1  = [index1 + k for k in range(n_patterns)]
            indexes2 = [index2 + k for k in range(n_patterns)]
            w[indexes1, indexes2] = 0

sequence_element = sequences[0]
total_connectivity_weights = w[sequence_element, :]
indexes = total_connectivity_weights > 0
excitation = total_connectivity_weights[indexes]
inhibition = total_connectivity_weights[np.logical_not(indexes)]

print('total excitaiton', np.sum(excitation))
print('total inhibition', np.sum(inhibition))
fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)
ax.hist(excitation, label='excitation')
ax.hist(inhibition, label='inhibition');
def remove_auto_influence(hypercolumns, minicolumns,w , n_patterns):
    for i in range(hypercolumns):
        for j in range(hypercolumns):
            index1 = minicolumns * j
            index2 = minicolumns * i
            indexes1  = [index1 + k for k in range(n_patterns)]
            indexes2 = [index2 + k for k in range(n_patterns)]
            w[indexes1, indexes2] = 0
            
            
# You see that
def get_excitation(index, w):
    sequence_element = sequences[index]
    total_connectivity_weights = w[sequence_element, :]
    exc_indexes = total_connectivity_weights > 0
    excitation = total_connectivity_weights[exc_indexes]
    
    return excitation

def get_inhibition(index, w):
    sequence_element = sequences[index]
    total_connectivity_weights = w[sequence_element, :]
    inh_indexes = total_connectivity_weights < 0
    inhibition = total_connectivity_weights[inh_indexes]
    
    return inhibition
w_ampa = nn.w_ampa
w_nmda = nn.w

w = np.copy(w_nmda)

total_exc = []
total_inh = []

indexes = np.arange(1, n_patterns - 1, dtype='int')
for index in indexes:
    excitation = get_excitation(index, w)
    inhibition = get_inhibition(index, w)
    
    total_exc.append(np.sum(excitation))
    total_inh.append(np.sum(inhibition))

ratio = np.sum(total_exc) / -np.sum(total_inh)
print('ratio', ratio)
fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)
ax.hist(total_exc, label='excitation')
ax.hist(total_inh, label='inhibition')

ax.legend();
def calculate_excitation_inhibition_ratio2(nn, hypercolumns, minicolumns, indexes, ampa=False):
    if ampa:
        w_use = nn.w_ampa
    else:
        w_use = nn.w

    w = np.copy(w_use)

    total_exc = []
    total_inh = []

   
    for index in indexes:
        excitation = get_excitation(index, w)
        inhibition = get_inhibition(index, w)

        total_exc.append(np.sum(excitation))
        total_inh.append(np.sum(inhibition))

    ratio = np.sum(total_exc) / -np.sum(total_inh)
    
    return ratio
indexes = np.arange(1, n_patterns, dtype='int')
ratio_nmda = calculate_excitation_inhibition_ratio2(nn, hypercolumns, minicolumns, indexes, ampa=False)
ratio_ampa = calculate_excitation_inhibition_ratio2(nn, hypercolumns, minicolumns, indexes, ampa=True)

print('nmda ratio', ratio_nmda)
print('ampa ratio', ratio_ampa)<jupyter_output>('nmda ratio', 0.23635685772947923)
('ampa ratio', 0.089949255567180836)<jupyter_text>Now we use the function that we build specially for it and store in analysis functions<jupyter_code>ratio_nmda = calculate_excitation_inhibition_ratio(nn, sequences, ampa=False)
ratio_ampa = calculate_excitation_inhibition_ratio(nn, sequences, ampa=True)

print('nmda ratio', ratio_nmda[0])
print('ampa ratio', ratio_ampa[0])<jupyter_output>('nmda ratio', 0.26379600684800408)
('ampa ratio', 0.091061920508488853)<jupyter_text>Paramater study  Structure parameters n_patterns<jupyter_code># Patterns parameters
hypercolumns = 4
minicolumns = 40
n_patterns = 10

dt = 0.001

values_to_save = ['o']

# Recall
n = 1
T_cue = 0.100
T_recall = 10.0

# Protocol
training_time = 0.1
inter_sequence_interval = 1.0
inter_pulse_interval = 0.0
epochs = 3

# Build the network
tau_z_pre = 0.150
tau_a = 2.70
n_patterns_range = np.arange(5, 25, 5)
nmda_ratio_n_patterns = []
ampa_ratio_n_patterns = []
success_n_patterns = []

for n_patterns in n_patterns_range:
    nn = BCPNNFast(hypercolumns, minicolumns, tau_z_pre=tau_z_pre)

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

    protocol = Protocol()
    sequences = [i for i in range(n_patterns)]
    protocol.simple_protocol(patterns_indexes=sequences, training_time=training_time,
                             inter_sequence_interval=inter_sequence_interval, epochs=epochs)


    manager.run_network_protocol(protocol=protocol, verbose=False)
    
    ratio_nmda = calculate_excitation_inhibition_ratio(nn, sequences, ampa=False)[0]
    ratio_ampa = calculate_excitation_inhibition_ratio(nn, sequences, ampa=True)[0]
    
    successes = calculate_recall_success_sequences(manager, T_recall=T_recall, 
                                                   T_cue=T_cue, n=n, sequences=[sequences])
    
    # Store everything
    nmda_ratio_n_patterns.append(ratio_nmda)
    ampa_ratio_n_patterns.append(ratio_ampa)
    success_n_patterns.append(successes[0])
fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)
ax.plot(n_patterns_range, nmda_ratio_n_patterns, '*-', markersize=13, label='nmda')
ax.plot(n_patterns_range, ampa_ratio_n_patterns, '*-', markersize=13, label='ampa')

ax2 = ax.twinx()
ax2.plot(n_patterns_range, success_n_patterns, '--', label='success')
ax2.set_ylim([-5, 105])

ax.set_xlabel('Number of patterns in the sequence')
ax.set_ylabel('Ratio of excitatory to inhibitory')

ax.set_ylim([0, 1])
ax.legend();<jupyter_output><empty_output><jupyter_text>Minicolumns<jupyter_code># Patterns parameters
hypercolumns = 4
minicolumns = 40
n_patterns = 10

dt = 0.001

values_to_save = ['o']

# Recall
n = 1
T_cue = 0.100
T_recall = 10.0

# Protocol
training_time = 0.1
inter_sequence_interval = 1.0
inter_pulse_interval = 0.0
epochs = 3

# Build the network
tau_z_pre = 0.150
tau_a = 2.70
minicolumns_range = np.arange(15, 45, 5)
nmda_ratio_minicolumns = []
ampa_ratio_minicolumns = []
success_minicolumns = []

for minicolumns in minicolumns_range:
    nn = BCPNNFast(hypercolumns, minicolumns, tau_z_pre=tau_z_pre)

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

    protocol = Protocol()
    sequences = [i for i in range(n_patterns)]
    protocol.simple_protocol(patterns_indexes=sequences, training_time=training_time,
                             inter_sequence_interval=inter_sequence_interval, epochs=epochs)


    manager.run_network_protocol(protocol=protocol, verbose=False)
    
    ratio_nmda = calculate_excitation_inhibition_ratio(nn, sequences, ampa=False)[0]
    ratio_ampa = calculate_excitation_inhibition_ratio(nn, sequences, ampa=True)[0]
    
    successes = calculate_recall_success_sequences(manager, T_recall=T_recall, 
                                                   T_cue=T_cue, n=n, sequences=[sequences])
    
    # Store everything
    nmda_ratio_minicolumns.append(ratio_nmda)
    ampa_ratio_minicolumns.append(ratio_ampa)
    success_minicolumns.append(successes[0])
fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)
ax.plot(minicolumns_range, nmda_ratio_minicolumns, '*-', markersize=13, label='nmda')
ax.plot(minicolumns_range, ampa_ratio_minicolumns, '*-', markersize=13, label='ampa')

ax2 = ax.twinx()
ax2.plot(minicolumns_range, success_minicolumns, '--', label='success')
ax2.set_ylim([-5, 105])

ax.set_xlabel('Minicolumns')
ax.set_ylabel('Ratio of excitatory to inhibitory')

ax.set_ylim([0, 1])
ax.legend();<jupyter_output><empty_output><jupyter_text>Hypercolumns<jupyter_code># Patterns parameters
hypercolumns = 4
minicolumns = 40
n_patterns = 10

dt = 0.001

values_to_save = ['o']

# Recall
n = 1
T_cue = 0.100
T_recall = 10.0

# Protocol
training_time = 0.1
inter_sequence_interval = 1.0
inter_pulse_interval = 0.0
epochs = 3

# Build the network
tau_z_pre = 0.150
tau_a = 2.70
hypercolumns_range = np.arange(3, 12, 2)
nmda_ratio_hypercolumns = []
ampa_ratio_hypercolumns = []
success_hypercolumns = []

for hypercolumns in hypercolumns_range:
    nn = BCPNNFast(hypercolumns, minicolumns, tau_z_pre=tau_z_pre)

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

    protocol = Protocol()
    sequences = [i for i in range(n_patterns)]
    protocol.simple_protocol(patterns_indexes=sequences, training_time=training_time,
                             inter_sequence_interval=inter_sequence_interval, epochs=epochs)


    manager.run_network_protocol(protocol=protocol, verbose=False)
    
    ratio_nmda = calculate_excitation_inhibition_ratio(nn, sequences, ampa=False)[0]
    ratio_ampa = calculate_excitation_inhibition_ratio(nn, sequences, ampa=True)[0]
   
    successes = calculate_recall_success_sequences(manager, T_recall=T_recall, 
                                                   T_cue=T_cue, n=n, sequences=[sequences])
    
    # Store everything
    nmda_ratio_hypercolumns.append(ratio_nmda)
    ampa_ratio_hypercolumns.append(ratio_ampa)
    success_hypercolumns.append(successes[0])
fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)
ax.plot(hypercolumns_range, nmda_ratio_hypercolumns, '*-', markersize=13, label='nmda')
ax.plot(hypercolumns_range, ampa_ratio_hypercolumns, '*-', markersize=13, label='ampa')

ax2 = ax.twinx()
ax2.plot(hypercolumns_range, success_hypercolumns, '--', label='success')
ax2.set_ylim([-5, 105])

ax.set_xlabel('Hypercolumns')
ax.set_ylabel('Ratio of excitatory to inhibitory')

ax.set_ylim([0, 1])
ax.legend();<jupyter_output><empty_output><jupyter_text>Training parameters Epochs<jupyter_code># Patterns parameters
hypercolumns = 4
minicolumns = 40
n_patterns = 10

dt = 0.001

values_to_save = ['o']

# Recall
n = 1
T_cue = 0.100
T_recall = 10.0

# Protocol
training_time = 0.1
inter_sequence_interval = 1.0
inter_pulse_interval = 0.0
epochs = 3

# Build the network
tau_z_pre = 0.150
tau_a = 2.70
epochs_range = np.arange(1, 11, 2)
nmda_ratio_epochs = []
ampa_ratio_epochs  = []
success_epochs  = []

for epochs  in epochs_range:
    nn = BCPNNFast(hypercolumns, minicolumns, tau_z_pre=tau_z_pre)

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

    protocol = Protocol()
    sequences = [i for i in range(n_patterns)]
    protocol.simple_protocol(patterns_indexes=sequences, training_time=training_time,
                             inter_sequence_interval=inter_sequence_interval, epochs=epochs)


    manager.run_network_protocol(protocol=protocol, verbose=False)
    
    ratio_nmda = calculate_excitation_inhibition_ratio(nn, sequences, ampa=False)[0]
    ratio_ampa = calculate_excitation_inhibition_ratio(nn, sequences, ampa=True)[0]
    
    successes = calculate_recall_success_sequences(manager, T_recall=T_recall, 
                                                   T_cue=T_cue, n=n, sequences=[sequences])
    
    # Store everything
    nmda_ratio_epochs.append(ratio_nmda)
    ampa_ratio_epochs.append(ratio_ampa)
    success_epochs.append(successes[0])
fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)
ax.plot(epochs_range, nmda_ratio_epochs , '*-', markersize=13, label='nmda')
ax.plot(epochs_range, ampa_ratio_epochs , '*-', markersize=13, label='ampa')

ax2 = ax.twinx()
ax2.plot(epochs_range, success_epochs , '--', label='success')
ax2.set_ylim([-5, 105])

ax.set_xlabel('Epochs')
ax.set_ylabel('Ratio of excitatory to inhibitory')

ax.set_ylim([0, 1])
ax.legend();<jupyter_output><empty_output><jupyter_text>Training time<jupyter_code># Patterns parameters
hypercolumns = 4
minicolumns = 40
n_patterns = 10

dt = 0.001

values_to_save = ['o']

# Recall
n = 1
T_cue = 0.100
T_recall = 10.0

# Protocol
training_time = 0.1
inter_sequence_interval = 1.0
inter_pulse_interval = 0.0
epochs = 3

# Build the network
tau_z_pre = 0.150
tau_a = 2.70
training_time_range = np.arange(0.050,0.450, 0.050)
nmda_ratio_training_time = []
ampa_ratio_training_time  = []
success_training_time  = []

for training_time  in training_time_range:
    nn = BCPNNFast(hypercolumns, minicolumns, tau_z_pre=tau_z_pre)

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

    protocol = Protocol()
    sequences = [i for i in range(n_patterns)]
    protocol.simple_protocol(patterns_indexes=sequences, training_time=training_time,
                             inter_sequence_interval=inter_sequence_interval, epochs=epochs)


    manager.run_network_protocol(protocol=protocol, verbose=False)
    
    ratio_nmda = calculate_excitation_inhibition_ratio(nn, sequences, ampa=False)[0]
    ratio_ampa = calculate_excitation_inhibition_ratio(nn, sequences, ampa=True)[0]
    
    successes = calculate_recall_success_sequences(manager, T_recall=T_recall, 
                                                   T_cue=T_cue, n=n, sequences=[sequences])
    
    # Store everything
    nmda_ratio_training_time.append(ratio_nmda)
    ampa_ratio_training_time.append(ratio_ampa)
    success_training_time.append(successes[0])
fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)
ax.plot(training_time_range, nmda_ratio_training_time , '*-', markersize=13, label='nmda')
ax.plot(training_time_range, ampa_ratio_training_time , '*-', markersize=13, label='ampa')

ax2 = ax.twinx()
ax2.plot(training_time_range, success_training_time , '--', label='success')
ax2.set_ylim([-5, 105])

ax.set_xlabel('Training time')
ax.set_ylabel('Ratio of excitatory to inhibitory')

ax.set_ylim([0, 1])
ax.legend();<jupyter_output><empty_output><jupyter_text>System parameters tau_z_nmda<jupyter_code># Patterns parameters
hypercolumns = 4
minicolumns = 40
n_patterns = 10

dt = 0.001

values_to_save = ['o']

# Recall
n = 1
T_cue = 0.100
T_recall = 10.0

# Protocol
training_time = 0.1
inter_sequence_interval = 1.0
inter_pulse_interval = 0.0
epochs = 3

# Build the network
tau_z_pre = 0.150
tau_a = 2.70
tau_z_pre_range = np.arange(0.050,0.450, 0.050)
nmda_ratio_tau_z_pre = []
ampa_ratio_tau_z_pre  = []
success_tau_z_pre  = []

for tau_z_pre in tau_z_pre_range:
    nn = BCPNNFast(hypercolumns, minicolumns, tau_z_pre=tau_z_pre)

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

    protocol = Protocol()
    sequences = [i for i in range(n_patterns)]
    protocol.simple_protocol(patterns_indexes=sequences, training_time=training_time,
                             inter_sequence_interval=inter_sequence_interval, epochs=epochs)


    manager.run_network_protocol(protocol=protocol, verbose=False)
    
    ratio_nmda = calculate_excitation_inhibition_ratio(nn, sequences, ampa=False)[0]
    ratio_ampa = calculate_excitation_inhibition_ratio(nn, sequences, ampa=True)[0]
    
    successes = calculate_recall_success_sequences(manager, T_recall=T_recall, 
                                                   T_cue=T_cue, n=n, sequences=[sequences])
    
    # Store everything
    nmda_ratio_tau_z_pre.append(ratio_nmda)
    ampa_ratio_tau_z_pre.append(ratio_ampa)
    success_tau_z_pre.append(successes[0])
fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)
ax.plot(tau_z_pre_range, nmda_ratio_tau_z_pre , '*-', markersize=13, label='nmda')
ax.plot(tau_z_pre_range, ampa_ratio_tau_z_pre , '*-', markersize=13, label='ampa')

ax2 = ax.twinx()
ax2.plot(tau_z_pre_range, success_tau_z_pre , '--', label='success')
ax2.set_ylim([-5, 105])

ax.set_xlabel('tau_z')
ax.set_ylabel('Ratio of excitatory to inhibitory')

ax.set_ylim([0, 1])
ax.legend();<jupyter_output><empty_output><jupyter_text>tau_a<jupyter_code># Patterns parameters
hypercolumns = 4
minicolumns = 40
n_patterns = 10

dt = 0.001

values_to_save = ['o']

# Recall
n = 1
T_cue = 0.100
T_recall = 10.0

# Protocol
training_time = 0.1
inter_sequence_interval = 1.0
inter_pulse_interval = 0.0
epochs = 3

# Build the network
tau_z_pre = 0.150
tau_a = 2.70
tau_a_range = np.arange(0.100,0.600, 0.100)
nmda_ratio_tau_a = []
ampa_ratio_tau_a = []
success_tau_a = []

for tau_a in tau_a_range:
    nn = BCPNNFast(hypercolumns, minicolumns, tau_z_pre=tau_z_pre)

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

    protocol = Protocol()
    sequences = [i for i in range(n_patterns)]
    protocol.simple_protocol(patterns_indexes=sequences, training_time=training_time,
                             inter_sequence_interval=inter_sequence_interval, epochs=epochs)


    manager.run_network_protocol(protocol=protocol, verbose=False)
    
    ratio_nmda = calculate_excitation_inhibition_ratio(nn, sequences, ampa=False)[0]
    ratio_ampa = calculate_excitation_inhibition_ratio(nn, sequences, ampa=True)[0]
    
    successes = calculate_recall_success_sequences(manager, T_recall=T_recall, 
                                                   T_cue=T_cue, n=n, sequences=[sequences])
    
    # Store everything
    nmda_ratio_tau_a.append(ratio_nmda)
    ampa_ratio_tau_a.append(ratio_ampa)
    success_tau_a.append(successes[0])
fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)
ax.plot(tau_a_range, nmda_ratio_tau_a , '*-', markersize=13, label='nmda')
ax.plot(tau_a_range, ampa_ratio_tau_a , '*-', markersize=13, label='ampa')

ax2 = ax.twinx()
ax2.plot(tau_a_range, success_tau_a , '--', label='success')
ax2.set_ylim([-5, 105])

ax.set_xlabel('tau a')
ax.set_ylabel('Ratio of excitatory to inhibitory')

ax.set_ylim([0, 1])
ax.legend();<jupyter_output><empty_output><jupyter_text>Git machinery<jupyter_code>if run_old_version:
    subprocess.call(['git', 'checkout', 'master'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10779920447107397, 'fraction_numerical': 0.030209959216555057, 'mean_word_length': 4.655466970387244, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '68897', 'license': 'MIT', 'n_tokens_mistral': 7666, 'n_tokens_neox': 7059, 'n_words': 1259, 'path': 'notebooks/2017-08-14(Excitation-Inhibition Balance I).ipynb', 'repo_name': 'h-mayorquin/attractor_sequences', 'size': '19861'}