<jupyter_start><jupyter_text>Mushroom Body DemoIn this notebook, we go through the following examples.1. Simulate Larva Mushroom Body2. Change PN to KC connectivity by reducing the number of PN connected to a given KC to 23. Change strength of APL inhibition<jupyter_code>%load_ext autoreload
%autoreload 2
import typing as tp
import numpy as np
from itertools import product
from eoscircuits import mb
import olftrans as olf
import olftrans.data
import olftrans.fbl
from olftrans.plot import plot_mat
from neurokernel.LPU.InputProcessors.StepInputProcessor import StepInputProcessor
import matplotlib.pyplot as plt
import typing as tp
import pandas as pd
import seaborn as sns
from eoscircuits.plot import plot_data, plot_spikes<jupyter_output>/mnt/server-home/tingkai/Project/FBL/olftrans/olftrans/olftrans.py:210: RuntimeWarning: invalid value encountered in power
  res = np.atleast_1d(np.power(c * (y - b) / (a - (y - b)), 1.0 / n))
/mnt/server-home/tingkai/Project/FBL/olftrans/olftrans/olftrans.py:210: RuntimeWarning: invalid value encountered in power
  res = np.atleast_1d(np.power(c * (y - b) / (a - (y - b)), 1.0 / n))<jupyter_text>Setup<jupyter_code>receptor_names = olf.fbl.LARVA.affinities.loc['ethyl acetate'].index.values.astype(str)
affinities = olf.fbl.LARVA.affinities.loc['ethyl acetate'].values

dt = 1e-5
dur = 4
steps = int((dur+dt/2)//dt)
t = np.arange(steps)*dt<jupyter_output><empty_output><jupyter_text>1. Simulate Larva Mushroom Body<jupyter_code>cfg = mb.MBConfig(
    affs=affinities,
    receptors=receptor_names,
    NP=1, NO=1, NPreLN=1, NPosteLN=1, NPostiLN=2, 
    NK=200, NAPL=1,
    node_params=dict(
        osn_bsgs=dict(sigma=0.),
        osn_axts=dict(gamma=1.),
        postelns=dict(a2=0.012),
        apls=dict(N=200.),
        kcs=dict(threshold=0.6)
    )
)
mb_circ = mb.MBCircuit.create_from_config(cfg)
fi = StepInputProcessor('conc', mb_circ.inputs['conc'], 100., start=1, stop=3)
fi, fo, lpu = mb_circ.simulate(
    t, fi, 
    record_var_list=[
        (
            'I', 
            sum(mb_circ.config.osn_otps,[]) + \
            mb_circ.config.kcdends
        ),
        (
            'r', 
            sum(mb_circ.config.pns,[]) + \
            mb_circ.config.kcs
        ),
        (
            'g', 
            sum(mb_circ.config.osn_alphas,[]) + \
            mb_circ.config.apls
        ),
        (
            'spike_state',
            sum(mb_circ.config.osn_bsgs, [])
        )
    ])
otp_I = fo.get_output(var='I', uids=sum(mb_circ.config.osn_otps,[]))
bsg_spikes = fo.get_output(var='spike_state', uids=sum(mb_circ.config.osn_bsgs,[]))
pn_r = fo.get_output(var='r', uids=sum(mb_circ.config.pns, []))
kc_r = fo.get_output(var='r', uids=mb_circ.config.kcs)
kcdend_I = fo.get_output(var='I', uids=mb_circ.config.kcdends)
apl_g = fo.get_output(var='g', uids=mb_circ.config.apls)
%matplotlib inline
fig,axes = plt.subplots(1,6, figsize=(25,5), gridspec_kw={'width_ratios': [1]+[10]*5, 'wspace':.5})
axes[0].imshow(np.log10(mb_circ.config.affs)[:,None], cmap=plt.cm.gnuplot, origin='lower', aspect='auto')
_ = plot_data(otp_I, t=t, ax=axes[1], cmap=plt.cm.gnuplot, cax=True, cbar_kw={'label': 'I [$\mu A$]'})
_ = plot_spikes(bsg_spikes, ax=axes[2], markersize=10, color='k')
_ = plot_data(pn_r, t=t, ax=axes[3], cmap=plt.cm.gnuplot, cax=True, cbar_kw={'label': 'Spike Rate A.U.'})
_ = plot_data(kcdend_I, t=t, ax=axes[4], cmap=plt.cm.gnuplot, cax=True, cbar_kw={'label': 'Spike Rate A.U.'})
_ = plot_data(kc_r, t=t, ax=axes[5], cmap=plt.cm.gnuplot, cax=True, cbar_kw={'label': 'Spike Rate A.U.'})

for ax in axes[1:]:
    ax.set_xlabel('Time [sec]')
axes[0].set_xticks([])
axes[0].set_title('$\log_{10}\mathbf{b}_o$')
axes[0].set_yticks(np.arange(mb_circ.config.NR))
axes[0].set_yticklabels(mb_circ.config.receptors)
axes[2].set_xlim([t.min(), t.max()])
axes[1].set_title('OSN-OTP')
axes[2].set_title('OSN-BSG')
axes[3].set_title('PN')
axes[4].set_title('KC Dendriate')
axes[5].set_title('KC')
axes[2].grid()
fig.suptitle('Larva Mushroom Body, Ethyl Acetate at 100ppm', fontsize=15)
plt.show()<jupyter_output><empty_output><jupyter_text>2. Change PN to KC Routing - Change to connect 2 PNs to each KC<jupyter_code>mb_circ.change_pn_to_kc(fanin=2)
fi = StepInputProcessor('conc', mb_circ.inputs['conc'], 100., start=1, stop=3)
fi, fo, lpu = mb_circ.simulate(
    t, fi, 
    record_var_list=[
        (
            'I', 
            sum(mb_circ.config.osn_otps,[]) + \
            mb_circ.config.kcdends
        ),
        (
            'r', 
            sum(mb_circ.config.pns,[]) + \
            mb_circ.config.kcs
        ),
        (
            'g', 
            sum(mb_circ.config.osn_alphas,[]) + \
            mb_circ.config.apls
        ),
        (
            'spike_state',
            sum(mb_circ.config.osn_bsgs, [])
        )
    ])
otp_I = fo.get_output(var='I', uids=sum(mb_circ.config.osn_otps,[]))
bsg_spikes = fo.get_output(var='spike_state', uids=sum(mb_circ.config.osn_bsgs,[]))
pn_r = fo.get_output(var='r', uids=sum(mb_circ.config.pns, []))
kc_r = fo.get_output(var='r', uids=mb_circ.config.kcs)
kcdend_I = fo.get_output(var='I', uids=mb_circ.config.kcdends)
apl_g = fo.get_output(var='g', uids=mb_circ.config.apls)
%matplotlib inline
fig,axes = plt.subplots(1,6, figsize=(25,5), gridspec_kw={'width_ratios': [1]+[10]*5, 'wspace':.5})
axes[0].imshow(np.log10(mb_circ.config.affs)[:,None], cmap=plt.cm.gnuplot, origin='lower', aspect='auto')
_ = plot_data(otp_I, t=t, ax=axes[1], cmap=plt.cm.gnuplot, cax=True, cbar_kw={'label': 'I [$\mu A$]'})
_ = plot_spikes(bsg_spikes, ax=axes[2], markersize=2, color='k')
_ = plot_data(pn_r, t=t, ax=axes[3], cmap=plt.cm.gnuplot, cax=True, cbar_kw={'label': 'Spike Rate A.U.'})
_ = plot_data(kcdend_I, t=t, ax=axes[4], cmap=plt.cm.gnuplot, cax=True, cbar_kw={'label': 'Spike Rate A.U.'})
_ = plot_data(kc_r, t=t, ax=axes[5], cmap=plt.cm.gnuplot, cax=True, cbar_kw={'label': 'Spike Rate A.U.'})

for ax in axes[1:]:
    ax.set_xlabel('Time [sec]')
axes[0].set_xticks([])
axes[0].set_title('$\log_{10}\mathbf{b}_o$')
axes[0].set_yticks(np.arange(mb_circ.config.NR))
axes[0].set_yticklabels(mb_circ.config.receptors)
axes[2].set_xlim([t.min(), t.max()])
axes[1].set_title('OSN-OTP')
axes[2].set_title('OSN-BSG')
axes[3].set_title('PN')
axes[4].set_title('KC Dendriate')
axes[5].set_title('KC')
axes[2].grid()
fig.suptitle('Larva Mushroom Body, Ethyl Acetate at 100ppm - PN to KC Fan-in reduced', fontsize=15)
plt.show()<jupyter_output><empty_output><jupyter_text>3. Change APL to KC Strength - Decrease Inhibition Strength by Twice<jupyter_code>mb_circ.change_pn_to_kc(fanin=6)
mb_circ.change_apl_strength(N=1000.)
fi = StepInputProcessor('conc', mb_circ.inputs['conc'], 100., start=1, stop=3)
fi, fo, lpu = mb_circ.simulate(
    t, fi, 
    record_var_list=[
        (
            'I', 
            sum(mb_circ.config.osn_otps,[]) + \
            mb_circ.config.kcdends
        ),
        (
            'r', 
            sum(mb_circ.config.pns,[]) + \
            mb_circ.config.kcs
        ),
        (
            'g', 
            sum(mb_circ.config.osn_alphas,[]) + \
            mb_circ.config.apls
        ),
        (
            'spike_state',
            sum(mb_circ.config.osn_bsgs, [])
        )
    ])
otp_I = fo.get_output(var='I', uids=sum(mb_circ.config.osn_otps,[]))
bsg_spikes = fo.get_output(var='spike_state', uids=sum(mb_circ.config.osn_bsgs,[]))
pn_r = fo.get_output(var='r', uids=sum(mb_circ.config.pns, []))
kc_r = fo.get_output(var='r', uids=mb_circ.config.kcs)
kcdend_I = fo.get_output(var='I', uids=mb_circ.config.kcdends)
apl_g = fo.get_output(var='g', uids=mb_circ.config.apls)
%matplotlib inline
fig,axes = plt.subplots(1,6, figsize=(25,5), gridspec_kw={'width_ratios': [1]+[10]*5, 'wspace':.5})
axes[0].imshow(np.log10(mb_circ.config.affs)[:,None], cmap=plt.cm.gnuplot, origin='lower', aspect='auto')
_ = plot_data(otp_I, t=t, ax=axes[1], cmap=plt.cm.gnuplot, cax=True, cbar_kw={'label': 'I [$\mu A$]'})
_ = plot_spikes(bsg_spikes, ax=axes[2], markersize=2, color='k')
_ = plot_data(pn_r, t=t, ax=axes[3], cmap=plt.cm.gnuplot, cax=True, cbar_kw={'label': 'Spike Rate A.U.'})
_ = plot_data(kcdend_I, t=t, ax=axes[4], cmap=plt.cm.gnuplot, cax=True, cbar_kw={'label': 'Spike Rate A.U.'})
_ = plot_data(kc_r, t=t, ax=axes[5], cmap=plt.cm.gnuplot, cax=True, cbar_kw={'label': 'Spike Rate A.U.'})

for ax in axes[1:]:
    ax.set_xlabel('Time [sec]')
axes[0].set_xticks([])
axes[0].set_title('$\log_{10}\mathbf{b}_o$')
axes[0].set_yticks(np.arange(mb_circ.config.NR))
axes[0].set_yticklabels(mb_circ.config.receptors)
axes[2].set_xlim([t.min(), t.max()])
axes[1].set_title('OSN-OTP')
axes[2].set_title('OSN-BSG')
axes[3].set_title('PN')
axes[4].set_title('KC Dendriate')
axes[5].set_title('KC')
axes[2].grid()
fig.suptitle('Larva Mushroom Body, Ethyl Acetate at 100ppm - Reduce APL Inhibition', fontsize=15)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18443544759334232, 'fraction_numerical': 0.02046783625730994, 'mean_word_length': 4.058589306029579, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '603938', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3952, 'n_tokens_neox': 3755, 'n_words': 558, 'path': 'notebooks/Mushroom Body.ipynb', 'repo_name': 'FlyBrainLab/EOScircuits', 'size': '8892'}