AllenInstitute / bmtk

Brain Modeling Toolkit
https://alleninstitute.github.io/bmtk/
BSD 3-Clause "New" or "Revised" License
266 stars 86 forks source link

Fix gap junctions builder code #318

Closed tjbanks closed 1 year ago

tjbanks commented 1 year ago

A recent change broke gap junctions, this fixes.

Tyler

tjbanks commented 1 year ago

Can be tested with the following

from bmtk.builder.networks import NetworkBuilder
from bmtk.utils.sim_setup import build_env_bionet
import numpy as np
import os
import shutil

if os.path.isdir('network'):
    shutil.rmtree('network')

net = NetworkBuilder('test_net')

net.add_nodes(
    N=2,
    pop_name='PV',
    cell_name='Scnn1a_473845048',
    potential='exc',
    model_type='biophysical',
    model_template='ctdb:Biophys1.hoc',
    model_processing='aibs_perisomatic',
    dynamics_params='472363762_fit.json',
    morphology='Scnn1a_473845048_m.swc'
)

thalamus = NetworkBuilder('thalamus')
thalamus.add_nodes(
    N=1,
    pop_name='tON',
    potential='exc',
    model_type='virtual'
)    

def gap_rule(source, target):
    sid = source.node_id                        
    tid = target.node_id
    if sid != tid:
        return 1
    else:
        return 0 

def conn_rule(source,target):
    sid = source.node_id                        
    tid = target.node_id
    if sid == tid:
        return 1

net.add_gap_junctions(source={'pop_name': 'PV'}, target={'pop_name': 'PV'}, connection_rule=gap_rule,resistance=1500)

thalamus.add_edges(
    source={'pop_name': 'tON'}, target=net.nodes(),
    connection_rule=conn_rule,
    syn_weight=1,
    delay=2.0,
    weight_function=None,
    target_sections=['somatic'],
    distance_range=[0.0, 150.0],
    dynamics_params='AMPA_ExcToExc.json',
    model_template='exp2syn'
)

net.build()
net.save('network')
thalamus.build()
thalamus.save(output_dir='network')

build_env_bionet(
    base_dir='./',
    config_file='config.json',
    network_dir='network',
    tstop=3000.0, dt=0.1,
    report_vars=['v'],     
    include_examples=True,
    overwrite_config=True,
    compile_mechanisms=True,
    spikes_inputs=[('thalamus','thalamus_spikes.h5')],
)

from bmtk.utils.reports.spike_trains import PoissonSpikeGenerator

psg = PoissonSpikeGenerator(population='thalamus')
psg.add(
    node_ids=0,  
    firing_rate=10.0,    
    times=(0.0, 3.0)  
)
psg.to_sonata('thalamus_spikes.h5')
python run_bionet.py config.json

Plot should show both cells reacting similarly.

from bmtk.utils.reports.compartment import CompartmentReport
import matplotlib.pyplot as plt

report = CompartmentReport('output/v_report.h5')

cell1 = report.data(node_id=0)
cell2 = report.data(node_id=1)

plt.figure(0)
plt.plot(cell1,label='cell 0')
plt.legend()
plt.figure(1)
plt.plot(cell2,label='cell 1')
plt.legend()
plt.show()
chenziao commented 1 year ago

To use gap junctions, you will also need the mechanism in the file gap.mod as follows.

NEURON {
  POINT_PROCESS Gap
  ELECTRODE_CURRENT i
  RANGE g, i, vgap
}

PARAMETER {
  g = 0.001 (uS)
}

ASSIGNED {
  v (millivolt)
  vgap (millivolt)
  i (nanoamp)
}

BREAKPOINT {
  i = g * (vgap - v)
}