示例#1
0
def generate(reference = "SimpleNet",
             scale=1,
             format='xml'):

    population_size = scale_pop_size(3,scale)

    nml_doc, network = oc.generate_network(reference)

    oc.include_opencortex_cell(nml_doc, 'izhikevich/RS.cell.nml')

    pop = oc.add_population_in_rectangular_region(network,
                                                  'RS_pop',
                                                  'RS',
                                                  population_size,
                                                  0,0,0,
                                                  100,100,100,
                                                  color='0 .8 0')
    import neuroml
    pop.properties.append(neuroml.Property('radius',10))

    syn = oc.add_exp_two_syn(nml_doc, 
                             id="syn0", 
                             gbase="2nS",
                             erev="0mV",
                             tau_rise="0.5ms",
                             tau_decay="10ms")

    pfs = oc.add_poisson_firing_synapse(nml_doc,
                                       id="poissonFiringSyn",
                                       average_rate="50 Hz",
                                       synapse_id=syn.id)

    oc.add_inputs_to_population(network,
                                "Stim0",
                                pop,
                                pfs.id,
                                all_cells=True)

    nml_file_name = '%s.net.nml'%network.id
    oc.save_network(nml_doc, 
                    nml_file_name,
                    validate=(format=='xml'),
                    format = format)

    if format=='xml':
        oc.generate_lems_simulation(nml_doc, 
                                    network, 
                                    nml_file_name, 
                                    duration =      500, 
                                    dt =            0.025,
                                    report_file_name='report.simple.txt')
def generate(cell_id, duration, reference, iRange):

    cell_file = '%s.cell.nml'%cell_id

    #cell_id = 'Cell0'
    #cell_file = 'L23_morph.cell.nml'

    nml_doc, network = oc.generate_network(reference, temperature='35degC')

    oc.include_neuroml2_cell_and_channels(nml_doc,cell_file,cell_id)



    population_size = len(iRange)

    pop = oc.add_population_in_rectangular_region(network,
                                                  'L23_pop',
                                                  cell_id,
                                                  population_size,
                                                  0,0,0,
                                                  1000,100,1000)
    for i in range(iRange.size):
        stim_id = ("Stim_%i"%i)
        pg = oc.add_pulse_generator(nml_doc,
                               id=stim_id,
                               delay="50ms",
                               duration="250ms",
                               amplitude="%fnA"%iRange[i])

        oc.add_inputs_to_population(network,
                                    stim_id,
                                    pop,
                                    pg.id,
                                    all_cells=False,
                                    only_cells=[i])

    nml_file_name = '%s.net.nml'%network.id
    oc.save_network(nml_doc, nml_file_name, validate=True)


    oc.generate_lems_simulation(nml_doc, 
                                network, 
                                nml_file_name, 
                                duration, 
                                dt = 0.025)
示例#3
0
def generate(cell_id, duration, reference, 
             Bee=1,
             Ensyn = 10, 
             Erates = [50,100],
             st_onset = 0,
             st_duration = 1e9,
             format='hdf5',
             simulator=None,
             num_processors=1,
             target_group='soma_group',
             temperature='35degC'):

    #Insyn = int(Ensyn * 0.2)
    #bInsyn = int(bEnsyn * 0.2)
    
    cell_file = '../%s.cell.nml'%cell_id
    if '/' in cell_id:
        cell_id=cell_id.split('/')[-1]

    nml_doc, network = oc.generate_network(reference, temperature=temperature)

    oc.include_neuroml2_cell_and_channels(nml_doc,cell_file,cell_id)
    
    
    synAmpaEE = oc.add_exp_one_syn(nml_doc, id="synAmpaEE", gbase="%snS"%Bee,
                             erev="0mV", tau_decay="1ms")


    pop = oc.add_population_in_rectangular_region(network,
                                        'L23_pop',
                                        cell_id,
                                        len(Erates),
                                        0,0,0,
                                        100,100,100)
    
          
    
    to_plot = {'Some_voltages':[]}
    to_save = {'%s_voltages.dat'%cell_id:[]}
    
    interesting_seg_ids = [0,200,1000,2000,2500,2949] # [soma, .. some dends .. , axon]
    interesting_seg_ids = [0] # [soma, .. some dends .. , axon]

    
    for i,r in enumerate(Erates):
        
        syn0 = oc.add_transient_poisson_firing_synapse(nml_doc,
                                           id="%s_stim_%s"%(synAmpaEE.id,r),
                                           average_rate="%s Hz"%r,
                                           synapse_id=synAmpaEE.id,
                                           delay='%s ms'%st_onset,
                                           duration='%s ms'%st_duration)
                                           
        oc.add_targeted_inputs_to_population(network, 
                                             "Esyn_%s"%r,
                                             pop, 
                                             syn0.id, 
                                             segment_group=target_group,
                                             number_per_cell = Ensyn,
                                             all_cells=False,
                                             only_cells=[i])
                                             
        for seg_id in interesting_seg_ids:
            to_plot.values()[0].append('%s/%s/%s/%s/v'%(pop.id, i, pop.component,seg_id))
            to_save.values()[0].append('%s/%s/%s/%s/v'%(pop.id, i, pop.component,seg_id))
            
                                

    nml_file_name = '%s.net.nml'%network.id + ('.h5' if format=='hdf5' else '')
    
    target_dir='./'
    
    oc.save_network(nml_doc, 
                    nml_file_name, 
                    validate=False, 
                    use_subfolder=True,
                    target_dir=target_dir,
                    format=format)



    lems_file_name, lems_sim = oc.generate_lems_simulation(nml_doc,
                                network, 
                                nml_file_name, 
                                duration, 
                                dt = 0.025,
                                target_dir=target_dir,
                                gen_plots_for_all_v = False,
                                plot_all_segments = False,
                                gen_plots_for_quantities = to_plot,   #  Dict with displays vs lists of quantity paths
                                gen_saves_for_all_v = False,
                                save_all_segments = False,
                                gen_saves_for_quantities = to_save,   #  Dict with file names vs lists of quantity paths
                                verbose = True)
                                
    if simulator:
                
        print ("Running %s for %sms in %s"%(lems_file_name, duration, simulator))

        traces, events = oc.simulate_network(lems_file_name,
                 simulator,
                 max_memory='4000M',
                 nogui=True,
                 load_saved_data=True,
                 reload_events=True,
                 plot=False,
                 verbose=True,
                 num_processors=num_processors)
                 
        rates = {}
        tt = [t*1000 for t in traces['t']]
        for tk in traces.keys():
            if tk!='t':
                rate = get_rate_from_trace(tt,[v*1000 for v in traces[tk]])
                print("Cell %s has rate %s Hz"%(tk,rate))
                i = int(tk.split('/')[1])
                rates[Erates[i]]=rate

        print Erates
        print rates

        ax = pynml.generate_plot([Erates],             
                                 [ [rates[r] for r in Erates] ],                
                                 "FF plots",               
                                 xaxis = 'Input frequency (Hz)',        
                                 yaxis = 'Firing frequency (Hz)',  
                                 markers=['o'],
                                 show_plot_already=True)     # Save figure
                                
        file_name = '%s.%s.%ssyns.%s.rates'%(cell_id,target_group,Ensyn,temperature)     
        f = open(file_name,'w')
        for r in Erates:
            f.write('%s\t%s\n'%(r,rates[r]))
        f.close()
        
        print("Finished! Saved rates data to %s"%file_name)
示例#4
0
pfs200 = oc.add_poisson_firing_synapse(nml_doc,
                                   id="poissonFiringSyn200",
                                   average_rate="200 Hz",
                                   synapse_id=synAmpa1.id)
                             
pfsStrong = oc.add_poisson_firing_synapse(nml_doc,
                                   id="poissonFiringSynStrong",
                                   average_rate="200 Hz",
                                   synapse_id=synAmpa2.id)
                                   
#####   Populations

popIaf = oc.add_population_in_rectangular_region(network,
                                              'popIaf',
                                              'iaf',
                                              scale_pop_size(20),
                                              0,offset,0,
                                              xDim,yDim,zDim,
                                              color='.8 0 0')
import neuroml
popIaf.properties.append(neuroml.Property('radius',5))
offset+=yDim

popIafRef = oc.add_population_in_rectangular_region(network,
                                              'popIafRef',
                                              'iafRef',
                                              scale_pop_size(20),
                                              0,offset,0,
                                              xDim,yDim,zDim,
                                              color='0 0 .8')
popIafRef.properties.append(neuroml.Property('radius',5))
示例#5
0
import opencortex.core as oc

population_size0 = 10
population_size1 = 10

nml_doc, network = oc.generate_network("SpikingNet")

oc.include_opencortex_cell(nml_doc, 'izhikevich/RS.cell.nml')

pop_pre = oc.add_population_in_rectangular_region(network,
                                              'pop_pre',
                                              'RS',
                                              population_size0,
                                              0,0,0,
                                              100,100,100,
                                              color='.8 0 0')
import neuroml
pop_pre.properties.append(neuroml.Property('radius',10))

pop_post = oc.add_population_in_rectangular_region(network,
                                              'pop_post',
                                              'RS',
                                              population_size1,
                                              0,100,0,
                                              100,200,100,
                                              color='0 0 .8')
                                              
pop_post.properties.append(neuroml.Property('radius',10))
                                              
syn0 = oc.add_exp_two_syn(nml_doc, 
示例#6
0
文件: ISN.py 项目: pgleeson/multi
def generate(scale_populations=1,
             percentage_exc_detailed=0,
             exc2_cell='SmithEtAl2013/L23_Retuned_477127614',
             percentage_inh_detailed=0,
             scalex=1,
             scaley=1,
             scalez=1,
             Bee=.1,
             Bei=.1,
             Bie=-.2,
             Bii=-.2,
             Be_bkg=.1,
             Be_stim=.1,
             r_bkg=0,
             r_stim=0,
             fraction_inh_pert=0.75,
             connections=True,
             exc_target_dendrites=False,
             inh_target_dendrites=False,
             duration=1000,
             dt=0.025,
             global_delay=.1,
             max_in_pop_to_plot_and_save=10,
             format='xml',
             suffix='',
             run_in_simulator=None,
             num_processors=1,
             target_dir='./temp/',
             exc_clamp=None):  # exc_clamp is work in progress...

    reference = ("ISN_net%s" % (suffix)).replace('.', '_')

    print('-------------------------------------------------')
    print('  Generating ISN network: %s' % reference)
    print('    Duration: %s; dt: %s; scale: %s; simulator: %s (num proc. %s)' %
          (duration, dt, scale_populations, run_in_simulator, num_processors))
    print('    Bee: %s; Bei: %s; Bie: %s; Bii: %s' % (Bee, Bei, Bie, Bii))
    print('    Be_bkg: %s at %sHz' % (Be_bkg, r_bkg))
    print('    Be_stim: %s at %sHz (i.e. %sHz for %s perturbed I cells)' %
          (Be_stim, r_stim, r_bkg + r_stim, fraction_inh_pert))
    print('    Exc detailed: %s%% - Inh detailed %s%%' %
          (percentage_exc_detailed, percentage_inh_detailed))
    print('-------------------------------------------------')

    num_exc = scale_pop_size(80, scale_populations)
    num_exc2 = int(math.ceil(num_exc * percentage_exc_detailed / 100.0))
    num_exc -= num_exc2

    num_inh = scale_pop_size(20, scale_populations)
    num_inh2 = int(math.ceil(num_inh * percentage_inh_detailed / 100.0))
    num_inh -= num_inh2

    nml_doc, network = oc.generate_network(reference, network_seed=1234)

    #exc_cell_id = 'AllenHH_480351780'
    exc_cell_id = 'AllenHH_477127614'
    #exc_cell_id = 'HH_477127614'
    exc_cell_id = 'HH2_477127614'
    exc_type = exc_cell_id.split('_')[0]
    oc.include_neuroml2_cell_and_channels(
        nml_doc, 'cells/%s/%s.cell.nml' % (exc_type, exc_cell_id), exc_cell_id)

    #inh_cell_id = 'AllenHH_485058595'
    inh_cell_id = 'AllenHH_476686112'
    #inh_cell_id = 'AllenHH_477127614'
    #inh_cell_id = 'HH_476686112'
    inh_cell_id = 'HH2_476686112'
    inh_type = exc_cell_id.split('_')[0]
    oc.include_neuroml2_cell_and_channels(
        nml_doc, 'cells/%s/%s.cell.nml' % (inh_type, inh_cell_id), inh_cell_id)

    if percentage_exc_detailed > 0:
        exc2_cell_id = exc2_cell.split('/')[1]
        exc2_cell_dir = exc2_cell.split('/')[0]
        oc.include_neuroml2_cell_and_channels(
            nml_doc, 'cells/%s/%s.cell.nml' % (exc2_cell_dir, exc2_cell_id),
            exc2_cell_id)

    if percentage_inh_detailed > 0:
        inh2_cell_id = 'cNAC187_L23_NBC_9d37c4b1f8_0_0'
        oc.include_neuroml2_cell_and_channels(
            nml_doc, 'cells/BBP/%s.cell.nml' % inh2_cell_id, inh2_cell_id)

    xDim = 700 * scalex
    yDim = 200 * scaley
    zDim = 700 * scalez

    xs = -1 * xDim / 2
    ys = -1 * yDim / 2
    zs = -1 * zDim / 2

    #####   Synapses

    synAmpaEE = oc.add_exp_one_syn(nml_doc,
                                   id="synAmpaEE",
                                   gbase="%snS" % Bee,
                                   erev="0mV",
                                   tau_decay="1ms")
    synAmpaEI = oc.add_exp_one_syn(nml_doc,
                                   id="synAmpaEI",
                                   gbase="%snS" % Bei,
                                   erev="0mV",
                                   tau_decay="1ms")

    synGabaIE = oc.add_exp_one_syn(nml_doc,
                                   id="synGabaIE",
                                   gbase="%snS" % abs(Bie),
                                   erev="-80mV",
                                   tau_decay="1ms")
    synGabaII = oc.add_exp_one_syn(nml_doc,
                                   id="synGabaII",
                                   gbase="%snS" % abs(Bii),
                                   erev="-80mV",
                                   tau_decay="1ms")

    synAmpaBkg = oc.add_exp_one_syn(nml_doc,
                                    id="synAmpaBkg",
                                    gbase="%snS" % Be_bkg,
                                    erev="0mV",
                                    tau_decay="1ms")
    synAmpaStim = oc.add_exp_one_syn(nml_doc,
                                     id="synAmpaStim",
                                     gbase="%snS" % Be_stim,
                                     erev="0mV",
                                     tau_decay="1ms")

    #####   Input types

    tpfsA = oc.add_transient_poisson_firing_synapse(
        nml_doc,
        id="tpsfA",
        average_rate="%s Hz" % r_bkg,
        delay='0ms',
        duration='%sms' % (Ttrans + Tblank),
        synapse_id=synAmpaBkg.id)

    tpfsB = oc.add_transient_poisson_firing_synapse(
        nml_doc,
        id="tpsfB",
        average_rate="%s Hz" % r_bkg,
        delay='%sms' % (Ttrans + Tblank),
        duration='%sms' % (Tstim),
        synapse_id=synAmpaBkg.id)

    tpfsC = oc.add_transient_poisson_firing_synapse(
        nml_doc,
        id="tpsfC",
        average_rate="%s Hz" % (r_bkg + r_stim),
        delay='%sms' % (Ttrans + Tblank),
        duration='%sms' % (Tstim),
        synapse_id=synAmpaStim.id)

    #####   Populations

    popExc = oc.add_population_in_rectangular_region(network,
                                                     'popExc',
                                                     exc_cell_id,
                                                     num_exc,
                                                     xs,
                                                     ys,
                                                     zs,
                                                     xDim,
                                                     yDim,
                                                     zDim,
                                                     color=exc_color)
    allExc = [popExc]

    if num_exc2 > 0:
        popExc2 = oc.add_population_in_rectangular_region(network,
                                                          'popExc2',
                                                          exc2_cell_id,
                                                          num_exc2,
                                                          xs,
                                                          ys,
                                                          zs,
                                                          xDim,
                                                          yDim,
                                                          zDim,
                                                          color=exc2_color)

        allExc.append(popExc2)

    popInh = oc.add_population_in_rectangular_region(network,
                                                     'popInh',
                                                     inh_cell_id,
                                                     num_inh,
                                                     xs,
                                                     ys,
                                                     zs,
                                                     xDim,
                                                     yDim,
                                                     zDim,
                                                     color=inh_color)
    allInh = [popInh]

    if num_inh2 > 0:
        popInh2 = oc.add_population_in_rectangular_region(network,
                                                          'popInh2',
                                                          inh2_cell_id,
                                                          num_inh2,
                                                          xs,
                                                          ys,
                                                          zs,
                                                          xDim,
                                                          yDim,
                                                          zDim,
                                                          color=inh2_color)

        allInh.append(popInh2)

    #####   Projections

    if connections:

        exc_exc_conn_prob = 0.15
        exc_inh_conn_prob = 0.15
        inh_exc_conn_prob = 1
        inh_inh_conn_prob = 1

        weight_expr = 'abs(normal(1,0.5))'

        for popEpr in allExc:

            for popEpo in allExc:
                proj = add_projection(network, "projEE", popEpr, popEpo,
                                      synAmpaEE.id, exc_exc_conn_prob,
                                      global_delay, exc_target_dendrites,
                                      weight_expr)

            for popIpo in allInh:
                proj = add_projection(network, "projEI", popEpr, popIpo,
                                      synAmpaEI.id, exc_inh_conn_prob,
                                      global_delay, exc_target_dendrites,
                                      weight_expr)

        for popIpr in allInh:

            for popEpo in allExc:
                proj = add_projection(network, "projIE", popIpr, popEpo,
                                      synGabaIE.id, inh_exc_conn_prob,
                                      global_delay, inh_target_dendrites,
                                      weight_expr)

            for popIpo in allInh:
                proj = add_projection(network, "projII", popIpr, popIpo,
                                      synGabaII.id, inh_inh_conn_prob,
                                      global_delay, inh_target_dendrites,
                                      weight_expr)

    #####   Inputs

    for pop in allExc:
        oc.add_inputs_to_population(network,
                                    "Stim_pre_%s" % pop.id,
                                    pop,
                                    tpfsA.id,
                                    all_cells=True)
    for pop in allInh:
        oc.add_inputs_to_population(network,
                                    "Stim_pre_%s" % pop.id,
                                    pop,
                                    tpfsA.id,
                                    all_cells=True)

    for pop in allExc:
        oc.add_inputs_to_population(network,
                                    "Stim_E_%s" % pop.id,
                                    pop,
                                    tpfsB.id,
                                    all_cells=True)

    for pop in allInh:

        num_inh_pert = int(pop.get_size() * fraction_inh_pert)

        oc.add_inputs_to_population(network,
                                    "Stim_I_pert_%s" % pop.id,
                                    pop,
                                    tpfsC.id,
                                    all_cells=False,
                                    only_cells=range(0, num_inh_pert))

        oc.add_inputs_to_population(network,
                                    "Stim_I_nonpert_%s" % pop.id,
                                    pop,
                                    tpfsB.id,
                                    all_cells=False,
                                    only_cells=range(num_inh_pert,
                                                     pop.get_size()))

    # Work in progress...
    # General idea: clamp one (or more) exc cell at rev pot of inh syn and see only exc inputs
    #
    if exc_clamp:

        vc = oc.add_voltage_clamp_triple(nml_doc,
                                         id="exc_clamp",
                                         delay='0ms',
                                         duration='%sms' % duration,
                                         conditioning_voltage=synGaba1.erev,
                                         testing_voltage=synGaba1.erev,
                                         return_voltage=synGaba1.erev,
                                         simple_series_resistance="1e5ohm",
                                         active="1")

        for pop in exc_clamp:
            oc.add_inputs_to_population(network,
                                        "exc_clamp_%s" % pop,
                                        network.get_by_id(pop),
                                        vc.id,
                                        all_cells=False,
                                        only_cells=exc_clamp[pop])

    #####   Save NeuroML and LEMS Simulation files

    nml_file_name = '%s.net.%s' % (network.id,
                                   'nml.h5' if format == 'hdf5' else 'nml')
    oc.save_network(nml_doc,
                    nml_file_name,
                    validate=(format == 'xml'),
                    format=format,
                    target_dir=target_dir)

    print("Saved to: %s" % nml_file_name)

    save_v = {}
    plot_v = {}

    if num_exc > 0:
        exc_traces = '%s_%s_v.dat' % (network.id, popExc.id)
        save_v[exc_traces] = []
        plot_v[popExc.id] = []

    if num_inh > 0:
        inh_traces = '%s_%s_v.dat' % (network.id, popInh.id)
        save_v[inh_traces] = []
        plot_v[popInh.id] = []

    if num_exc2 > 0:
        exc2_traces = '%s_%s_v.dat' % (network.id, popExc2.id)
        save_v[exc2_traces] = []
        plot_v[popExc2.id] = []

    if num_inh2 > 0:
        inh2_traces = '%s_%s_v.dat' % (network.id, popInh2.id)
        save_v[inh2_traces] = []
        plot_v[popInh2.id] = []

    for i in range(min(max_in_pop_to_plot_and_save, num_exc)):
        plot_v[popExc.id].append("%s/%i/%s/v" %
                                 (popExc.id, i, popExc.component))
        save_v[exc_traces].append("%s/%i/%s/v" %
                                  (popExc.id, i, popExc.component))

    for i in range(min(max_in_pop_to_plot_and_save, num_exc2)):
        plot_v[popExc2.id].append("%s/%i/%s/v" %
                                  (popExc2.id, i, popExc2.component))
        save_v[exc2_traces].append("%s/%i/%s/v" %
                                   (popExc2.id, i, popExc2.component))

    for i in range(min(max_in_pop_to_plot_and_save, num_inh)):
        plot_v[popInh.id].append("%s/%i/%s/v" %
                                 (popInh.id, i, popInh.component))
        save_v[inh_traces].append("%s/%i/%s/v" %
                                  (popInh.id, i, popInh.component))

    for i in range(min(max_in_pop_to_plot_and_save, num_inh2)):
        plot_v[popInh2.id].append("%s/%i/%s/v" %
                                  (popInh2.id, i, popInh2.component))
        save_v[inh2_traces].append("%s/%i/%s/v" %
                                   (popInh2.id, i, popInh2.component))

    gen_spike_saves_for_all_somas = True

    lems_file_name, lems_sim = oc.generate_lems_simulation(
        nml_doc,
        network,
        target_dir + nml_file_name,
        duration=duration,
        dt=dt,
        gen_plots_for_all_v=False,
        gen_plots_for_quantities=plot_v,
        gen_saves_for_all_v=False,
        gen_saves_for_quantities=save_v,
        gen_spike_saves_for_all_somas=gen_spike_saves_for_all_somas,
        target_dir=target_dir,
        report_file_name='report.txt')

    if run_in_simulator:

        print("Running %s for %sms in %s" %
              (lems_file_name, duration, run_in_simulator))

        traces, events = oc.simulate_network(lems_file_name,
                                             run_in_simulator,
                                             max_memory='4000M',
                                             nogui=True,
                                             load_saved_data=True,
                                             reload_events=True,
                                             plot=False,
                                             verbose=True,
                                             num_processors=num_processors)

        print("Reloaded traces: %s" % traces.keys())
        #print("Reloaded events: %s"%events.keys())

        use_events_for_rates = False

        exc_rate = 0
        inh_rate = 0

        if use_events_for_rates:
            if (run_in_simulator == 'jNeuroML_NetPyNE'):
                raise (
                    'Saving of spikes (and so calculation of rates) not yet supported in jNeuroML_NetPyNE'
                )
            for ek in events.keys():
                rate = 1000 * len(events[ek]) / float(duration)
                print("Cell %s has a rate %s Hz" % (ek, rate))
                if 'popExc' in ek:
                    exc_rate += rate / num_exc
                if 'popInh' in ek:
                    inh_rate += rate / num_inh

        else:
            tot_exc_rate = 0
            exc_cells = 0
            tot_inh_rate = 0
            inh_cells = 0
            tt = [t * 1000 for t in traces['t']]
            for tk in traces.keys():
                if tk != 't':
                    rate = get_rate_from_trace(tt,
                                               [v * 1000 for v in traces[tk]])
                    print("Cell %s has rate %s Hz" % (tk, rate))
                    if 'popExc' in tk:
                        tot_exc_rate += rate
                        exc_cells += 1
                    if 'popInh' in tk:
                        tot_inh_rate += rate
                        inh_cells += 1

            exc_rate = tot_exc_rate / exc_cells
            inh_rate = tot_inh_rate / inh_cells

        print("Run %s: Exc rate: %s Hz; Inh rate %s Hz" %
              (reference, exc_rate, inh_rate))

        return exc_rate, inh_rate, traces

    return nml_doc, nml_file_name, lems_file_name
def generate(reference = "Balanced",
             scalePops = 1,
             scalex=1,
             scaley=1,
             scalez=1,
             connections=True,
             connections_scaling=1,
             duration = 1000,
             global_delay = 2,
             max_in_pop_to_plot_and_save = 5,
             gen_spike_saves_for_all_somas = True,
             deterministic = True,
             format='xml'):

    num_exc = scale_pop_size(80,scalePops)
    num_inh = scale_pop_size(40,scalePops)
    
    if scalePops!=1:
        reference += '_%s'%scalePops
    
    nml_doc, network = oc.generate_network(reference)
    
    oc.include_opencortex_cell(nml_doc, 'AllenInstituteCellTypesDB_HH/HH_477127614.cell.nml')
    oc.include_opencortex_cell(nml_doc, 'AllenInstituteCellTypesDB_HH/HH_476686112.cell.nml')
    
    xDim = 400*scalex
    yDim = 500*scaley
    zDim = 300*scalez

    xs = -200
    ys = -150
    zs = 100

    #####   Synapses

    synAmpa1 = oc.add_exp_two_syn(nml_doc, id="synAmpa1", gbase="1nS",
                             erev="0mV", tau_rise="0.5ms", tau_decay="5ms")

    synGaba1 = oc.add_exp_two_syn(nml_doc, id="synGaba1", gbase="2nS",
                             erev="-80mV", tau_rise="1ms", tau_decay="20ms")

    #####   Input types

    if not deterministic:
        pfs1 = oc.add_poisson_firing_synapse(nml_doc,
                                           id="psf1",
                                           average_rate="150 Hz",
                                           synapse_id=synAmpa1.id)



    #####   Populations

    popExc = oc.add_population_in_rectangular_region(network,
                                                  'popExc',
                                                  'HH_477127614',
                                                  num_exc,
                                                  xs,ys,zs,
                                                  xDim,yDim,zDim)

    popInh = oc.add_population_in_rectangular_region(network,
                                                  'popInh',
                                                  'HH_476686112',
                                                  num_inh,
                                                  xs,ys,zs,
                                                  xDim,yDim,zDim)


    #####   Projections

    total_conns = 0
    if connections:
        proj = oc.add_probabilistic_projection(network, "proj0",
                                        popExc, popExc,
                                        synAmpa1.id, connections_scaling*0.3, delay = global_delay)
        total_conns += len(proj.connection_wds)

        proj = oc.add_probabilistic_projection(network, "proj1",
                                        popExc, popInh,
                                        synAmpa1.id, connections_scaling*0.5, delay = global_delay)
        total_conns += len(proj.connection_wds)

        proj = oc.add_probabilistic_projection(network, "proj3",
                                        popInh, popExc,
                                        synGaba1.id, connections_scaling*0.7, delay = global_delay)
        total_conns += len(proj.connection_wds)

        proj = oc.add_probabilistic_projection(network, "proj4",
                                        popInh, popInh,
                                        synGaba1.id, connections_scaling*0.5, delay = global_delay)
        total_conns += len(proj.connection_wds)


    #####   Inputs

    if not deterministic:
        oc.add_inputs_to_population(network, "Stim0",
                                popExc, pfs1.id,
                                all_cells=True)
                                
    else:

        for i in range(num_exc):

            pg = oc.add_pulse_generator(nml_doc,
                                   id="pg_%i"%i,
                                   delay="0ms",
                                   duration="10000ms",
                                   amplitude="%snA"%(random()*0.5))

            oc.add_inputs_to_population(network, "Stim_%i"%i,
                                    popExc, pg.id,
                                    all_cells=False,
                                    only_cells=[i])
                               



    #####   Save NeuroML and LEMS Simulation files      
    

    nml_file_name = '%s%s.net.%s'%('XH_' if format == 'xml_hdf5' else '', network.id,'nml.h5' if format == 'hdf5' else 'nml')
    
    oc.save_network(nml_doc, 
                    nml_file_name, 
                    validate=(format=='xml'),
                    format = format)


    plot_v = {popExc.id:[],popInh.id:[]}
    save_v = {'%s_v.dat'%popExc.id:[],'%s_v.dat'%popInh.id:[]}

    for i in range(min(max_in_pop_to_plot_and_save,num_exc)):
        plot_v[popExc.id].append("%s/%i/%s/v"%(popExc.id,i,popExc.component))
        save_v['%s_v.dat'%popExc.id].append("%s/%i/%s/v"%(popExc.id,i,popExc.component))

    for i in range(min(max_in_pop_to_plot_and_save,num_inh)):
        plot_v[popInh.id].append("%s/%i/%s/v"%(popInh.id,i,popInh.component))
        save_v['%s_v.dat'%popInh.id].append("%s/%i/%s/v"%(popInh.id,i,popInh.component))
        
    lems_file_name = "LEMS_%s.xml"%network.id
    if format != 'xml':
        lems_file_name = "LEMS_%s_%s.xml"%(network.id,format)
        

    lems_file_name = oc.generate_lems_simulation(nml_doc, network, 
                            nml_file_name, 
                            duration =      duration, 
                            dt =            0.025,
                            gen_plots_for_all_v = False,
                            gen_plots_for_quantities = plot_v,
                            gen_saves_for_all_v = False,
                            gen_saves_for_quantities = save_v,
                            gen_spike_saves_for_all_somas = gen_spike_saves_for_all_somas,
                            lems_file_name = lems_file_name)
                                
    return nml_doc, nml_file_name, lems_file_name
示例#8
0
def generate(
        scale_populations=1,
        percentage_exc_detailed=0,
        #exc2_cell = 'SmithEtAl2013/L23_Retuned_477127614',
        exc2_cell='SmithEtAl2013/L23_NoHotSpot',
        #exc2_cell = 'BBP/cADpyr229_L23_PC_5ecbf9b163_0_0',
        #exc2_cell = 'BBP/cNAC187_L23_NBC_9d37c4b1f8_0_0',
        #exc2_cell = 'Thalamocortical/L23PyrRS',
        percentage_inh_detailed=0,
        scalex=1,
        scaley=1,
        scalez=1,
        exc_exc_conn_prob=0.25,
        exc_inh_conn_prob=0.25,
        inh_exc_conn_prob=0.75,
        inh_inh_conn_prob=0.75,
        ee2_conn_prob=0,
        ie2_conn_prob=0,
        Bee=.1,
        Bei=.1,
        Bie=-.2,
        Bii=-.2,
        Bee2=1,
        Bie2=-2,
        Be_bkg=.1,
        Be_stim=.1,
        r_bkg=0,
        r_bkg_ExtExc=0,
        r_bkg_ExtInh=0,
        r_bkg_ExtExc2=0,
        r_stim=0,
        fraction_inh_pert=0.75,
        Ttrans=500,  # transitent time to discard the data (ms)
        Tblank=1500,  # simulation time before perturbation (ms)
        Tstim=1500,  # simulation time of perturbation (ms)
        Tpost=500,  # simulation time after perturbation (ms)
        connections=True,
        connections2=False,
        exc_target_dendrites=False,
        inh_target_dendrites=False,
        duration=1000,
        dt=0.025,
        global_delay=.1,
        max_in_pop_to_plot_and_save=10,
        format='xml',
        suffix='',
        run_in_simulator=None,
        num_processors=1,
        target_dir='./temp/',
        v_clamp=False,
        simulation_seed=11111):

    reference = ("ISN_net%s" % (suffix)).replace('.', '_')

    ks = open('kernelseed', 'w')
    ks.write('%i' % simulation_seed)
    ks.close()

    info = ('  Generating ISN network: %s\n' % reference)
    info += (
        '    Duration: %s; dt: %s; scale: %s; simulator: %s (num proc. %s)\n' %
        (duration, dt, scale_populations, run_in_simulator, num_processors))
    info += ('    Bee: %s; Bei: %s; Bie: %s; Bii: %s\n' % (Bee, Bei, Bie, Bii))
    info += ('    Bkg exc at %sHz\n' % (r_bkg_ExtExc))
    info += ('    Bkg inh at %sHz\n' % (r_bkg_ExtInh))
    info += ('    Be_stim: %s at %sHz (i.e. %sHz for %s perturbed I cells)\n' %
             (Be_stim, r_stim, r_bkg_ExtInh + r_stim, fraction_inh_pert))
    info += ('    Exc detailed: %s%% - Inh detailed %s%%\n' %
             (percentage_exc_detailed, percentage_inh_detailed))
    info += ('    Seed: %s' % (simulation_seed))

    print('-------------------------------------------------')
    print(info)
    print('-------------------------------------------------')

    num_exc = scale_pop_size(np.round(100 * exc_inh_fraction),
                             scale_populations)
    num_exc2 = int(math.ceil(num_exc * percentage_exc_detailed / 100.0))
    num_exc -= num_exc2

    num_inh = scale_pop_size(np.round(100 * (1 - exc_inh_fraction)),
                             scale_populations)
    num_inh2 = int(math.ceil(num_inh * percentage_inh_detailed / 100.0))
    num_inh -= num_inh2

    nml_doc, network = oc.generate_network(reference,
                                           network_seed=simulation_seed)
    nml_doc.notes = info
    network.notes = info

    #exc_cell_id = 'AllenHH_480351780'
    #exc_cell_id = 'AllenHH_477127614'
    #exc_cell_id = 'HH_477127614'
    exc_cell_id = 'HH2_477127614'
    exc_type = exc_cell_id.split('_')[0]
    oc.include_neuroml2_cell_and_channels(
        nml_doc, 'cells/%s/%s.cell.nml' % (exc_type, exc_cell_id), exc_cell_id)

    #inh_cell_id = 'AllenHH_485058595'
    #inh_cell_id = 'AllenHH_476686112'
    #inh_cell_id = 'AllenHH_477127614'
    #inh_cell_id = 'HH_476686112'
    inh_cell_id = 'HH2_476686112'
    inh_type = exc_cell_id.split('_')[0]
    oc.include_neuroml2_cell_and_channels(
        nml_doc, 'cells/%s/%s.cell.nml' % (inh_type, inh_cell_id), inh_cell_id)

    if percentage_exc_detailed > 0:
        exc2_cell_id = exc2_cell.split('/')[1]
        exc2_cell_dir = exc2_cell.split('/')[0]
        oc.include_neuroml2_cell_and_channels(
            nml_doc, 'cells/%s/%s.cell.nml' % (exc2_cell_dir, exc2_cell_id),
            exc2_cell_id)

    if percentage_inh_detailed > 0:
        inh2_cell_id = 'cNAC187_L23_NBC_9d37c4b1f8_0_0'
        oc.include_neuroml2_cell_and_channels(
            nml_doc, 'cells/BBP/%s.cell.nml' % inh2_cell_id, inh2_cell_id)

    xDim = 700 * scalex
    yDim = 100 * scaley
    yDimExc2 = 50 * scaley
    zDim = 700 * scalez

    xs = -1 * xDim / 2
    ys = -1 * yDim / 2
    zs = -1 * zDim / 2

    #####   Synapses

    synAmpaEE = oc.add_exp_one_syn(nml_doc,
                                   id="ampaEE",
                                   gbase="%snS" % Bee,
                                   erev="0mV",
                                   tau_decay="1ms")
    synAmpaEI = oc.add_exp_one_syn(nml_doc,
                                   id="ampaEI",
                                   gbase="%snS" % Bei,
                                   erev="0mV",
                                   tau_decay="1ms")

    synGabaIE = oc.add_exp_one_syn(nml_doc,
                                   id="gabaIE",
                                   gbase="%snS" % abs(Bie),
                                   erev="-80mV",
                                   tau_decay="2ms")
    synGabaII = oc.add_exp_one_syn(nml_doc,
                                   id="gabaII",
                                   gbase="%snS" % abs(Bii),
                                   erev="-80mV",
                                   tau_decay="2ms")

    synAmpaBkg = oc.add_exp_one_syn(nml_doc,
                                    id="ampaBkg",
                                    gbase="%snS" % Be_bkg,
                                    erev="0mV",
                                    tau_decay="1ms")
    #synAmpaStim = oc.add_exp_one_syn(nml_doc, id="ampaStim", gbase="%snS"%Be_stim,
    #                         erev="0mV", tau_decay="1ms")

    synAmpaEE2 = oc.add_exp_one_syn(nml_doc,
                                    id="ampaEE2",
                                    gbase="%snS" % Bee2,
                                    erev="0mV",
                                    tau_decay="10ms")
    synGabaIE2 = oc.add_exp_one_syn(nml_doc,
                                    id="gabaIE2",
                                    gbase="%snS" % abs(Bie2),
                                    erev="-80mV",
                                    tau_decay="10ms")

    #####   Input types
    '''tpfsA = oc.add_transient_poisson_firing_synapse(nml_doc,
                                       id="tpsfA",
                                       average_rate="%s Hz"%r_bkg,
                                       delay = '0ms', 
                                       duration = '%sms'%(Ttrans+Tblank),
                                       synapse_id=synAmpaBkg.id)

    tpfsB = oc.add_transient_poisson_firing_synapse(nml_doc,
                                       id="tpsfB",
                                       average_rate="%s Hz"%r_bkg,
                                       delay = '%sms'%(Ttrans+Tblank),
                                       duration = '%sms'%(Tstim),
                                       synapse_id=synAmpaBkg.id)

    tpfsC = oc.add_transient_poisson_firing_synapse(nml_doc,
                                       id="tpsfC",
                                       average_rate="%s Hz"%(r_bkg+r_stim),
                                       delay = '%sms'%(Ttrans+Tblank),
                                       duration = '%sms'%(Tstim),
                                       synapse_id=synAmpaStim.id)'''

    tpfsExtExc = oc.add_transient_poisson_firing_synapse(
        nml_doc,
        id="tpfsExtExc",
        average_rate="%s Hz" % r_bkg_ExtExc,
        delay='0ms',
        duration='%sms' % (Ttrans + Tblank + Tstim + Tpost),
        synapse_id=synAmpaBkg.id)

    tpfsExtExc2 = oc.add_transient_poisson_firing_synapse(
        nml_doc,
        id="tpfsExtExc2",
        average_rate="%s Hz" % r_bkg_ExtExc2,
        delay='0ms',
        duration='%sms' % (Ttrans + Tblank + Tstim + Tpost),
        synapse_id=synAmpaBkg.id)

    tpfsExtInh = oc.add_transient_poisson_firing_synapse(
        nml_doc,
        id="tpfsExtInh",
        average_rate="%s Hz" % r_bkg_ExtInh,
        delay='0ms',
        duration='%sms' % (Ttrans + Tblank + Tstim + Tpost),
        synapse_id=synAmpaBkg.id)

    tpfsPertInh_before = oc.add_transient_poisson_firing_synapse(
        nml_doc,
        id="tpfsPertInh_before",
        average_rate="%s Hz" % r_bkg_ExtInh,
        delay='0ms',
        duration='%sms' % (Ttrans + Tblank),
        synapse_id=synAmpaBkg.id)
    tpfsPertInh_during = oc.add_transient_poisson_firing_synapse(
        nml_doc,
        id="tpfsPertInh_during",
        average_rate="%s Hz" % (r_bkg_ExtInh + r_stim),
        delay='%sms' % (Ttrans + Tblank),
        duration='%sms' % (Tstim),
        synapse_id=synAmpaBkg.id)
    tpfsPertInh_after = oc.add_transient_poisson_firing_synapse(
        nml_doc,
        id="tpfsPertInh_after",
        average_rate="%s Hz" % r_bkg_ExtInh,
        delay='%sms' % (Ttrans + Tblank + Tstim),
        duration='%sms' % (Tpost),
        synapse_id=synAmpaBkg.id)

    #####   Populations

    popExc = oc.add_population_in_rectangular_region(network,
                                                     'popExc',
                                                     exc_cell_id,
                                                     num_exc,
                                                     xs,
                                                     ys,
                                                     zs,
                                                     xDim,
                                                     yDim,
                                                     zDim,
                                                     color=exc_color)
    from neuroml import Property
    popExc.properties.append(Property('type', 'E'))
    allExc = [popExc]

    if num_exc2 > 0:
        popExc2 = oc.add_population_in_rectangular_region(network,
                                                          'popExc2',
                                                          exc2_cell_id,
                                                          num_exc2,
                                                          xs,
                                                          yDim / 2,
                                                          zs,
                                                          xDim,
                                                          yDimExc2,
                                                          zDim,
                                                          color=exc2_color)
        popExc2.properties.append(Property('type', 'E'))

        allExc.append(popExc2)

    popInh = oc.add_population_in_rectangular_region(network,
                                                     'popInh',
                                                     inh_cell_id,
                                                     num_inh,
                                                     xs,
                                                     ys,
                                                     zs,
                                                     xDim,
                                                     yDim,
                                                     zDim,
                                                     color=inh_color)
    popInh.properties.append(Property('type', 'I'))
    allInh = [popInh]

    if num_inh2 > 0:
        popInh2 = oc.add_population_in_rectangular_region(network,
                                                          'popInh2',
                                                          inh2_cell_id,
                                                          num_inh2,
                                                          xs,
                                                          ys,
                                                          zs,
                                                          xDim,
                                                          yDim,
                                                          zDim,
                                                          color=inh2_color)

        allInh.append(popInh2)

    #####   Projections

    if connections:

        weight_expr = 'abs(normal(1,0.5))'

        for popEpr in allExc:

            for popEpo in allExc:
                proj = add_projection(network, "projEE", popEpr, popEpo,
                                      synAmpaEE.id, exc_exc_conn_prob,
                                      global_delay, exc_target_dendrites,
                                      weight_expr)

            for popIpo in allInh:
                proj = add_projection(network, "projEI", popEpr, popIpo,
                                      synAmpaEI.id, exc_inh_conn_prob,
                                      global_delay, exc_target_dendrites,
                                      weight_expr)

        for popIpr in allInh:

            for popEpo in allExc:
                proj = add_projection(network, "projIE", popIpr, popEpo,
                                      synGabaIE.id, inh_exc_conn_prob,
                                      global_delay, inh_target_dendrites,
                                      weight_expr)

            for popIpo in allInh:
                proj = add_projection(network, "projII", popIpr, popIpo,
                                      synGabaII.id, inh_inh_conn_prob,
                                      global_delay, inh_target_dendrites,
                                      weight_expr)

    elif connections2:

        weight_expr = 'abs(normal(1,0.5))'

        proj = add_projection(network, "projEE", popExc, popExc, synAmpaEE.id,
                              exc_exc_conn_prob, global_delay,
                              exc_target_dendrites, weight_expr)
        proj = add_projection(network, "projEI", popExc, popInh, synAmpaEI.id,
                              exc_inh_conn_prob, global_delay,
                              exc_target_dendrites, weight_expr)
        proj = add_projection(network, "projIE", popInh, popExc, synGabaIE.id,
                              inh_exc_conn_prob, global_delay,
                              inh_target_dendrites, weight_expr)
        proj = add_projection(network, "projII", popInh, popInh, synGabaII.id,
                              inh_inh_conn_prob, global_delay,
                              inh_target_dendrites, weight_expr)

        proj = add_projection(network, "projEE2", popExc, popExc2,
                              synAmpaEE2.id, ee2_conn_prob, global_delay,
                              exc_target_dendrites, weight_expr)
        proj = add_projection(network, "projIE2", popInh, popExc2,
                              synGabaIE2.id, ie2_conn_prob, global_delay,
                              inh_target_dendrites, weight_expr)

    #####   Inputs

    oc.add_inputs_to_population(network,
                                "Stim_E",
                                popExc,
                                tpfsExtExc.id,
                                all_cells=True)

    if num_exc2 > 0:
        oc.add_inputs_to_population(network,
                                    "Stim_E2",
                                    popExc2,
                                    tpfsExtExc2.id,
                                    all_cells=True)

    num_inh_pert = int(popInh.get_size() * fraction_inh_pert)

    oc.add_inputs_to_population(network,
                                "Stim_I_nonpert",
                                popInh,
                                tpfsExtInh.id,
                                all_cells=False,
                                only_cells=range(num_inh_pert,
                                                 popInh.get_size()))

    oc.add_inputs_to_population(network,
                                "Stim_I_pert_before",
                                popInh,
                                tpfsPertInh_before.id,
                                all_cells=False,
                                only_cells=range(0, num_inh_pert))
    oc.add_inputs_to_population(network,
                                "Stim_I_pert_during",
                                popInh,
                                tpfsPertInh_during.id,
                                all_cells=False,
                                only_cells=range(0, num_inh_pert))
    oc.add_inputs_to_population(network,
                                "Stim_I_pert_after",
                                popInh,
                                tpfsPertInh_after.id,
                                all_cells=False,
                                only_cells=range(0, num_inh_pert))

    # injecting noise in the soma of detailed neurons to insert some variability
    '''oc.add_targeted_inputs_to_population(network, 
                                         "PG_noise",
                                         popExc2, 
                                         'noisyCurrentSource1',             # from ../../../NoisyCurrentSource.xml
                                         segment_group='soma_group',
                                         number_per_cell = 1,
                                         all_cells=True)
    
    
    oc.add_inputs_to_population(network, "Stim_pre_ExtExc_%s"%popExc.id,
                                    popExc, tpfsExtExc.id,
                                    all_cells=True)

    for pop in allExc:
        #oc.add_inputs_to_population(network, "Stim_pre_ExtExc_%s"%pop.id,
        #                            pop, tpfsExtExc.id,
        #                            all_cells=True)

        oc.add_inputs_to_population(network, "Stim_pre_%s"%pop.id,
                                    pop, tpfsA.id,
                                    all_cells=True)
        
        oc.add_inputs_to_population(network, "Stim_E_%s"%pop.id,
                                    pop, tpfsB.id,
                                    all_cells=True) 

    for pop in allInh:
        num_inh_pert = int(pop.get_size()*fraction_inh_pert)

        oc.add_inputs_to_population(network, "Stim_pre_ExtInh_%s"%pop.id,
                                    pop, tpfsExtInh.id,
                                    all_cells=True)

        oc.add_inputs_to_population(network, "Stim_pre_%s"%pop.id,
                                    pop, tpfsA.id,
                                    all_cells=True)
        
        oc.add_inputs_to_population(network, "Stim_I_pert_%s"%pop.id,
                                    pop, tpfsC.id,
                                    all_cells=False,
                                    only_cells=range(0,num_inh_pert))   
                                    
        oc.add_inputs_to_population(network, "Stim_I_nonpert_%s"%pop.id,
                                    pop, tpfsB.id,
                                    all_cells=False,
                                    only_cells=range(num_inh_pert,pop.get_size()))  '''

    save_v = {}
    plot_v = {}

    # Work in progress...
    # General idea: clamp one (or more) exc cell at rev pot of inh syn and see only exc inputs
    #
    if v_clamp:

        levels = {'IPSC': synAmpaEE.erev, 'EPSC': synGabaIE.erev}

        for l in levels:
            cell_index = levels.keys().index(l)

            pop = 'popExc2'
            plot = 'IClamp_i_%s' % (l)

            for seg_id in [0, 2953,
                           1406]:  # 2953: end of axon; 1406 on dendrite

                clamp_id = "vclamp_cell%s_seg%s_%s" % (cell_index, seg_id, l)
                v_clamped = levels[l]

                vc = oc.add_voltage_clamp_triple(
                    nml_doc,
                    id=clamp_id,
                    delay='0ms',
                    duration='%sms' % duration,
                    conditioning_voltage=v_clamped,
                    testing_voltage=v_clamped,
                    return_voltage=v_clamped,
                    simple_series_resistance="1e2ohm",
                    active="1")

                vc_dat_file = 'v_clamps_i_seg%s_%s.%s.dat' % (seg_id, l,
                                                              simulation_seed)

                seg_file = '%s_seg%s_%s_v.dat' % (pop, seg_id, l)

                save_v[vc_dat_file] = []
                plot_v[plot] = []

                oc.add_inputs_to_population(network,
                                            "vclamp_seg%s_%s" % (seg_id, l),
                                            network.get_by_id(pop),
                                            vc.id,
                                            all_cells=False,
                                            only_cells=[cell_index],
                                            segment_ids=[seg_id])

                # record at seg
                q = '%s/%s/%s/%s/%s/i' % (pop, cell_index,
                                          network.get_by_id(pop).component,
                                          seg_id, clamp_id)

                save_v[vc_dat_file].append(q)
                plot_v[plot].append(q)

                if seg_id != 0:
                    save_v[seg_file] = []
                    q = '%s/%s/%s/%s/v' % (pop, cell_index,
                                           network.get_by_id(pop).component,
                                           seg_id)
                    save_v[seg_file].append(q)

    #####   Save NeuroML and LEMS Simulation files

    nml_file_name = '%s.net.%s' % (network.id,
                                   'nml.h5' if format == 'hdf5' else 'nml')
    oc.save_network(nml_doc,
                    nml_file_name,
                    validate=(format == 'xml'),
                    format=format,
                    target_dir=target_dir)

    print("Saved to: %s" % nml_file_name)

    if num_exc > 0:
        exc_traces = '%s_%s_v.dat' % (network.id, popExc.id)
        save_v[exc_traces] = []
        plot_v[popExc.id] = []

    if num_inh > 0:
        inh_traces = '%s_%s_v.dat' % (network.id, popInh.id)
        save_v[inh_traces] = []
        plot_v[popInh.id] = []

    if num_exc2 > 0:
        exc2_traces = '%s_%s_v.dat' % (network.id, popExc2.id)
        save_v[exc2_traces] = []
        plot_v[popExc2.id] = []

    if num_inh2 > 0:
        inh2_traces = '%s_%s_v.dat' % (network.id, popInh2.id)
        save_v[inh2_traces] = []
        plot_v[popInh2.id] = []

    for i in range(min(max_in_pop_to_plot_and_save, num_exc)):
        plot_v[popExc.id].append("%s/%i/%s/v" %
                                 (popExc.id, i, popExc.component))
        save_v[exc_traces].append("%s/%i/%s/v" %
                                  (popExc.id, i, popExc.component))

    for i in range(min(max_in_pop_to_plot_and_save, num_exc2)):
        plot_v[popExc2.id].append("%s/%i/%s/v" %
                                  (popExc2.id, i, popExc2.component))
        save_v[exc2_traces].append("%s/%i/%s/v" %
                                   (popExc2.id, i, popExc2.component))

    for i in range(min(max_in_pop_to_plot_and_save, num_inh)):
        plot_v[popInh.id].append("%s/%i/%s/v" %
                                 (popInh.id, i, popInh.component))
        save_v[inh_traces].append("%s/%i/%s/v" %
                                  (popInh.id, i, popInh.component))

    for i in range(min(max_in_pop_to_plot_and_save, num_inh2)):
        plot_v[popInh2.id].append("%s/%i/%s/v" %
                                  (popInh2.id, i, popInh2.component))
        save_v[inh2_traces].append("%s/%i/%s/v" %
                                   (popInh2.id, i, popInh2.component))

    gen_spike_saves_for_all_somas = True

    lems_file_name, lems_sim = oc.generate_lems_simulation(
        nml_doc,
        network,
        target_dir + nml_file_name,
        duration=duration,
        dt=dt,
        gen_plots_for_all_v=False,
        gen_plots_for_quantities=plot_v,
        gen_saves_for_all_v=False,
        gen_saves_for_quantities=save_v,
        gen_spike_saves_for_all_somas=gen_spike_saves_for_all_somas,
        target_dir=target_dir,
        include_extra_lems_files=['./NoisyCurrentSource.xml'],
        report_file_name='report.txt',
        simulation_seed=simulation_seed)

    if run_in_simulator:

        print("Running %s for %sms in %s" %
              (lems_file_name, duration, run_in_simulator))

        traces, events = oc.simulate_network(lems_file_name,
                                             run_in_simulator,
                                             max_memory='4000M',
                                             nogui=True,
                                             load_saved_data=True,
                                             reload_events=True,
                                             plot=False,
                                             verbose=True,
                                             num_processors=num_processors)

        print("Reloaded traces: %s" % traces.keys())
        #print("Reloaded events: %s"%events.keys())

        use_events_for_rates = False

        exc_rate = 0
        inh_rate = 0

        if use_events_for_rates:
            if (run_in_simulator == 'jNeuroML_NetPyNE'):
                raise (
                    'Saving of spikes (and so calculation of rates) not yet supported in jNeuroML_NetPyNE'
                )
            for ek in events.keys():
                rate = 1000 * len(events[ek]) / float(duration)
                print("Cell %s has a rate %s Hz" % (ek, rate))
                if 'popExc' in ek:
                    exc_rate += rate / num_exc
                if 'popInh' in ek:
                    inh_rate += rate / num_inh

        else:
            tot_exc_rate = 0
            exc_cells = 0
            tot_inh_rate = 0
            inh_cells = 0
            tt = [t * 1000 for t in traces['t']]
            for tk in traces.keys():
                if tk != 't':
                    rate = get_rate_from_trace(tt,
                                               [v * 1000 for v in traces[tk]])
                    print("Cell %s has rate %s Hz" % (tk, rate))
                    if 'popExc' in tk:
                        tot_exc_rate += rate
                        exc_cells += 1
                    if 'popInh' in tk:
                        tot_inh_rate += rate
                        inh_cells += 1

            exc_rate = tot_exc_rate / exc_cells
            inh_rate = tot_inh_rate / inh_cells

        print("Run %s: Exc rate: %s Hz; Inh rate %s Hz" %
              (reference, exc_rate, inh_rate))

        return exc_rate, inh_rate, traces

    return nml_doc, nml_file_name, lems_file_name
示例#9
0
def generate(reference="VClamp",
             poisson_inputs=True,
             use_vclamp=False,
             duration=500,
             format='xml'):

    nml_doc, network = oc.generate_network(reference)

    oc.include_opencortex_cell(nml_doc, 'Thalamocortical/L23PyrRS.cell.nml')

    num_cells = 4

    pop_rs = oc.add_population_in_rectangular_region(network,
                                                     'popRS',
                                                     'L23PyrRS',
                                                     num_cells,
                                                     0,
                                                     0,
                                                     0,
                                                     1000,
                                                     20,
                                                     20,
                                                     color='.8 0 0')

    syn0 = oc.add_exp_two_syn(nml_doc,
                              id="syn0",
                              gbase="1nS",
                              erev="0mV",
                              tau_rise="0.5ms",
                              tau_decay="10ms")

    if poisson_inputs:

        pfs = oc.add_transient_poisson_firing_synapse(nml_doc,
                                                      id="poissonFiringSyn",
                                                      average_rate="20 Hz",
                                                      delay="50 ms",
                                                      duration="400 ms",
                                                      synapse_id=syn0.id)

        oc.add_targeted_inputs_to_population(network,
                                             "pfs_noise",
                                             pop_rs,
                                             pfs.id,
                                             segment_group='dendrite_group',
                                             number_per_cell=100,
                                             all_cells=True)

    all_vclamp_segs = [0, 142, 87]
    vclamp_segs = {0: [], 1: [0], 2: [0, 142], 3: all_vclamp_segs}

    gen_plots_for_quantities = {
    }  #  Dict with displays vs lists of quantity paths
    gen_saves_for_quantities = {
    }  #  Dict with file names vs lists of quantity paths

    if use_vclamp:

        v_clamped = '-70mV'

        for cell_id in vclamp_segs:

            for seg_id in vclamp_segs[cell_id]:

                vc = oc.add_voltage_clamp_triple(
                    nml_doc,
                    id='vclamp_cell%i_seg%i' % (cell_id, seg_id),
                    delay='0ms',
                    duration='%sms' % duration,
                    conditioning_voltage=v_clamped,
                    testing_voltage=v_clamped,
                    return_voltage=v_clamped,
                    simple_series_resistance="1e1ohm",
                    active="1")

                vc_dat_file = 'v_clamps_i_cell%s_seg%s.dat' % (cell_id, seg_id)

                gen_saves_for_quantities[vc_dat_file] = []

                oc.add_inputs_to_population(network,
                                            "input_vClamp_cell%i_seg%i" %
                                            (cell_id, seg_id),
                                            pop_rs,
                                            vc.id,
                                            all_cells=False,
                                            only_cells=[cell_id],
                                            segment_ids=[seg_id])

                q = '%s/%s/%s/%s/%s/i' % (pop_rs.id, cell_id, pop_rs.component,
                                          seg_id, vc.id)

                gen_saves_for_quantities[vc_dat_file].append(q)

    nml_file_name = '%s.net.%s' % (network.id,
                                   'nml.h5' if format == 'hdf5' else 'nml')
    oc.save_network(nml_doc,
                    nml_file_name,
                    validate=(format == 'xml'),
                    format=format)

    segments_to_plot_record = {pop_rs.id: all_vclamp_segs + [20, 50, 99, 139]}

    if format == 'xml':

        for pop in segments_to_plot_record.keys():
            pop_nml = network.get_by_id(pop)
            if pop_nml is not None and pop_nml.size > 0:
                for i in range(int(pop_nml.size)):
                    gen_plots_for_quantities['Display_%s_%i_v' % (pop, i)] = []
                    gen_saves_for_quantities['Sim_%s.%s.%i.v.dat' %
                                             (nml_doc.id, pop, i)] = []

                    for seg in segments_to_plot_record[pop]:
                        quantity = '%s/%i/%s/%i/v' % (pop, i,
                                                      pop_nml.component, seg)
                        gen_plots_for_quantities['Display_%s_%i_v' %
                                                 (pop, i)].append(quantity)
                        gen_saves_for_quantities['Sim_%s.%s.%i.v.dat' %
                                                 (nml_doc.id, pop,
                                                  i)].append(quantity)

        lems_file_name = oc.generate_lems_simulation(
            nml_doc,
            network,
            nml_file_name,
            duration=duration,
            dt=0.025,
            gen_plots_for_all_v=False,
            gen_plots_for_quantities=gen_plots_for_quantities,
            gen_saves_for_all_v=False,
            gen_saves_for_quantities=gen_saves_for_quantities)
    else:
        lems_file_name = None

    return nml_doc, nml_file_name, lems_file_name
示例#10
0
def generate(scalePops = 1,
             percentage_exc_detailed=0,
             exc2_cell = 'SmithEtAl2013/L23_Retuned_477127614',
             percentage_inh_detailed=0,
             scalex=1,
             scaley=1,
             scalez=1,
             ratio_inh_exc=2,
             connections=True,
             exc_target_dendrites=False,
             inh_target_dendrites=False,
             duration = 1000,
             dt = 0.025,
             input_rate = 150,
             global_delay = 2,
             max_in_pop_to_plot_and_save = 5,
             format='xml',
             suffix='',
             run_in_simulator = None,
             num_processors = 1,
             target_dir='./temp/',
             exc_clamp=None):       # exc_clamp is work in progress...
                 
    reference = ("Multiscale__g%s__i%s%s"%(ratio_inh_exc,input_rate,suffix)).replace('.','_')
                    

    num_exc = scale_pop_size(80,scalePops)
    num_exc2  = int(math.ceil(num_exc*percentage_exc_detailed/100.0))
    num_exc -= num_exc2
    
    num_inh = scale_pop_size(40,scalePops)
    num_inh2  = int(math.ceil(num_inh*percentage_inh_detailed/100.0))
    num_inh -= num_inh2
    
    nml_doc, network = oc.generate_network(reference, network_seed=1234)
    
    #exc_cell_id = 'AllenHH_480351780'
    exc_cell_id = 'AllenHH_477127614'
    #exc_cell_id = 'HH_477127614'
    exc_type = exc_cell_id.split('_')[0]
    oc.include_neuroml2_cell_and_channels(nml_doc, 'cells/%s/%s.cell.nml'%(exc_type,exc_cell_id), exc_cell_id)
    
    
    #inh_cell_id = 'AllenHH_485058595'
    inh_cell_id = 'AllenHH_476686112'
    #inh_cell_id = 'HH_476686112'
    inh_type = exc_cell_id.split('_')[0]
    oc.include_neuroml2_cell_and_channels(nml_doc, 'cells/%s/%s.cell.nml'%(inh_type,inh_cell_id), inh_cell_id)

    if percentage_exc_detailed>0:
        exc2_cell_id = exc2_cell.split('/')[1]
        exc2_cell_dir = exc2_cell.split('/')[0]
        oc.include_neuroml2_cell_and_channels(nml_doc, 'cells/%s/%s.cell.nml'%(exc2_cell_dir,exc2_cell_id), exc2_cell_id)

    if percentage_inh_detailed>0:
        inh2_cell_id = 'cNAC187_L23_NBC_9d37c4b1f8_0_0'
        oc.include_neuroml2_cell_and_channels(nml_doc, 'cells/BBP/%s.cell.nml'%inh2_cell_id, inh2_cell_id)
    

    xDim = 700*scalex
    yDim = 200*scaley
    zDim = 700*scalez

    xs = -1*xDim/2
    ys = -1*yDim/2
    zs = -1*zDim/2

    #####   Synapses
    
    exc_syn_nS = 1.

    synAmpa1 = oc.add_exp_two_syn(nml_doc, id="synAmpa1", gbase="%snS"%exc_syn_nS,
                             erev="0mV", tau_rise="0.5ms", tau_decay="5ms")

    synGaba1 = oc.add_exp_two_syn(nml_doc, id="synGaba1", gbase="%snS"%(exc_syn_nS*ratio_inh_exc),
                             erev="-80mV", tau_rise="1ms", tau_decay="20ms")

    #####   Input types


    pfs1 = oc.add_poisson_firing_synapse(nml_doc,
                                       id="psf1",
                                       average_rate="%s Hz"%input_rate,
                                       synapse_id=synAmpa1.id)


    #####   Populations

    popExc = oc.add_population_in_rectangular_region(network,
                                                  'popExc',
                                                  exc_cell_id,
                                                  num_exc,
                                                  xs,ys,zs,
                                                  xDim,yDim,zDim,
                                                  color=exc_color)                 
    allExc = [popExc]

    if num_exc2>0:
        popExc2 = oc.add_population_in_rectangular_region(network,
                                                  'popExc2',
                                                  exc2_cell_id,
                                                  num_exc2,
                                                  xs,ys,zs,
                                                  xDim,yDim,zDim,
                                                  color=exc2_color)
                                                  
        allExc.append(popExc2)

    popInh = oc.add_population_in_rectangular_region(network,
                                                  'popInh',
                                                  inh_cell_id,
                                                  num_inh,
                                                  xs,ys,zs,
                                                  xDim,yDim,zDim,
                                                  color=inh_color)           
    allInh = [popInh]
    
    if num_inh2>0:
        popInh2 = oc.add_population_in_rectangular_region(network,
                                                  'popInh2',
                                                  inh2_cell_id,
                                                  num_inh2,
                                                  xs,ys,zs,
                                                  xDim,yDim,zDim,
                                                  color=inh2_color)
                                                  
        allInh.append(popInh2)


    #####   Projections

    if connections:

        exc_exc_conn_prob = 0.5
        exc_inh_conn_prob = 0.7
        inh_exc_conn_prob = 0.7
        inh_inh_conn_prob = 0.5
        
        for popEpr in allExc:
            
            for popEpo in allExc:
                proj = add_projection(network, "projEE",
                                      popEpr, popEpo,
                                      synAmpa1.id, exc_exc_conn_prob, 
                                      global_delay,
                                      exc_target_dendrites)
                                                
            for popIpo in allInh:
                proj = add_projection(network, "projEI",
                                      popEpr, popIpo,
                                      synAmpa1.id, exc_inh_conn_prob, 
                                      global_delay,
                                      exc_target_dendrites)

            
        for popIpr in allInh:
            
            for popEpo in allExc:
                proj = add_projection(network, "projIE",
                                      popIpr, popEpo,
                                      synGaba1.id, inh_exc_conn_prob, 
                                      global_delay,
                                      inh_target_dendrites)
        
            for popIpo in allInh:
                proj = add_projection(network, "projII",
                                      popIpr, popIpo,
                                      synGaba1.id, inh_inh_conn_prob, 
                                      global_delay,
                                      inh_target_dendrites)

                                        

    #####   Inputs

    for pop in allExc:
        oc.add_inputs_to_population(network, "Stim_%s"%pop.id,
                                    pop, pfs1.id,
                                    all_cells=True)


    # Work in progress...
    # General idea: clamp one (or more) exc cell at rev pot of inh syn and see only exc inputs
    #
    if exc_clamp:
        
        vc = oc.add_voltage_clamp_triple(nml_doc, id="exc_clamp", 
                             delay='0ms', 
                             duration='%sms'%duration, 
                             conditioning_voltage=synGaba1.erev,
                             testing_voltage=synGaba1.erev,
                             return_voltage=synGaba1.erev, 
                             simple_series_resistance="1e5ohm",
                             active = "1")
                             
        for pop in exc_clamp:
            oc.add_inputs_to_population(network, "exc_clamp_%s"%pop,
                                        network.get_by_id(pop), vc.id,
                                        all_cells=False,
                                        only_cells=exc_clamp[pop])
                


    #####   Save NeuroML and LEMS Simulation files      
    

    nml_file_name = '%s.net.%s'%(network.id,'nml.h5' if format == 'hdf5' else 'nml')
    oc.save_network(nml_doc, 
                    nml_file_name, 
                    validate=(format=='xml'),
                    format = format,
                    target_dir=target_dir)
        

    if format=='xml':
        
        
        save_v = {}
        plot_v = {}
        
        if num_exc>0:
            exc_traces = '%s_%s_v.dat'%(network.id,popExc.id)
            save_v[exc_traces] = []
            plot_v[popExc.id] = []
            
        if num_inh>0:
            inh_traces = '%s_%s_v.dat'%(network.id,popInh.id)
            save_v[inh_traces] = []
            plot_v[popInh.id] = []
            
        if num_exc2>0:
            exc2_traces = '%s_%s_v.dat'%(network.id,popExc2.id)
            save_v[exc2_traces] = []
            plot_v[popExc2.id] = []
            
        if num_inh2>0:
            inh2_traces = '%s_%s_v.dat'%(network.id,popInh2.id)
            save_v[inh2_traces] = []
            plot_v[popInh2.id] = []
        
        
        for i in range(min(max_in_pop_to_plot_and_save,num_exc)):
            plot_v[popExc.id].append("%s/%i/%s/v"%(popExc.id,i,popExc.component))
            save_v[exc_traces].append("%s/%i/%s/v"%(popExc.id,i,popExc.component))
            
        for i in range(min(max_in_pop_to_plot_and_save,num_exc2)):
            plot_v[popExc2.id].append("%s/%i/%s/v"%(popExc2.id,i,popExc2.component))
            save_v[exc2_traces].append("%s/%i/%s/v"%(popExc2.id,i,popExc2.component))
            
        for i in range(min(max_in_pop_to_plot_and_save,num_inh)):
            plot_v[popInh.id].append("%s/%i/%s/v"%(popInh.id,i,popInh.component))
            save_v[inh_traces].append("%s/%i/%s/v"%(popInh.id,i,popInh.component))
            
        for i in range(min(max_in_pop_to_plot_and_save,num_inh2)):
            plot_v[popInh2.id].append("%s/%i/%s/v"%(popInh2.id,i,popInh2.component))
            save_v[inh2_traces].append("%s/%i/%s/v"%(popInh2.id,i,popInh2.component))
            
        gen_spike_saves_for_all_somas = run_in_simulator!='jNeuroML_NetPyNE'
            
        lems_file_name = oc.generate_lems_simulation(nml_doc, network, 
                                target_dir+nml_file_name, 
                                duration =      duration, 
                                dt =            dt,
                                gen_plots_for_all_v = False,
                                gen_plots_for_quantities = plot_v,
                                gen_saves_for_all_v = False,
                                gen_saves_for_quantities = save_v,
                                gen_spike_saves_for_all_somas = gen_spike_saves_for_all_somas,
                                target_dir=target_dir)
                                
        
        if run_in_simulator:
            
            print ("Running %s for %sms in %s"%(lems_file_name, duration, run_in_simulator))
            
            traces, events = oc.simulate_network(lems_file_name,
                     run_in_simulator,
                     max_memory='4000M',
                     nogui=True,
                     load_saved_data=True,
                     reload_events=True,
                     plot=False,
                     verbose=False,
                     num_processors=num_processors)
                     
                     
            print("Reloaded traces: %s"%traces.keys())
            #print("Reloaded events: %s"%events.keys())
            
            use_events_for_rates = False
            
            exc_rate = 0
            inh_rate = 0
            
            if use_events_for_rates:
                if (run_in_simulator=='jNeuroML_NetPyNE'):
                    raise('Saving of spikes (and so calculation of rates) not yet supported in jNeuroML_NetPyNE')
                for ek in events.keys():
                    rate = 1000 * len(events[ek])/float(duration)
                    print("Cell %s has rate %s Hz"%(ek,rate))
                    if 'popExc' in ek:
                        exc_rate += rate/num_exc
                    if 'popInh' in ek:
                        inh_rate += rate/num_inh
            
            else:
                tot_exc_rate = 0 
                exc_cells = 0
                tot_inh_rate = 0 
                inh_cells = 0
                tt = [t*1000 for t in traces['t']]
                for tk in traces.keys():
                    if tk!='t':
                        rate = get_rate_from_trace(tt,[v*1000 for v in traces[tk]])
                        print("Cell %s has rate %s Hz"%(tk,rate))
                        if 'popExc' in tk:
                            tot_exc_rate += rate
                            exc_cells+=1
                        if 'popInh' in tk:
                            tot_inh_rate += rate
                            inh_cells+=1
                            
                exc_rate = tot_exc_rate/exc_cells
                inh_rate = tot_inh_rate/inh_cells
                    
                    
                    
            print("Run %s: Exc rate: %s Hz; Inh rate %s Hz"%(reference,exc_rate, inh_rate))
                     
            return exc_rate, inh_rate, traces
        
    else:
        lems_file_name = None
                                
    return nml_doc, nml_file_name, lems_file_name
示例#11
0
import opencortex.core as oc

population_size0 = 10
population_size1 = 10

nml_doc, network = oc.generate_network("SpikingNet")

oc.include_opencortex_cell(nml_doc, 'izhikevich/RS.cell.nml')

pop_pre = oc.add_population_in_rectangular_region(network,
                                              'pop_pre',
                                              'RS',
                                              population_size0,
                                              0,0,0,
                                              100,100,100)

pop_post = oc.add_population_in_rectangular_region(network,
                                              'pop_post',
                                              'RS',
                                              population_size1,
                                              0,100,0,
                                              100,200,100)
                                              
syn0 = oc.add_exp_two_syn(nml_doc, 
                         id="syn0", 
                         gbase="1nS",
                         erev="0mV",
                         tau_rise="0.5ms",
                         tau_decay="10ms")
             
示例#12
0
                             duration="400ms",
                             amplitude="0.35nA")

pfs = oc.add_poisson_firing_synapse(nml_doc,
                                    id="poissonFiringSyn",
                                    average_rate="150 Hz",
                                    synapse_id=synAmpa2.id)

#####   Populations

pop0 = oc.add_population_in_rectangular_region(network,
                                               'pop0',
                                               'pyr_4_sym',
                                               5,
                                               0,
                                               offset,
                                               0,
                                               xDim,
                                               yDim,
                                               zDim,
                                               color='0 .8 0')

offset += yDim

#####   Projections

oc.add_probabilistic_projection(network, "proj0", pop0, pop0, synAmpa1.id, 0.5)

#####   Inputs

oc.add_inputs_to_population(network, "Stim0", pop0, pg0.id, only_cells=[0])
示例#13
0
def generate(reference="Balanced",
             num_bbp=1,
             scalePops=1,
             scalex=1,
             scaley=1,
             scalez=1,
             connections=True,
             duration=1000,
             input_rate=150,
             global_delay=0,
             max_in_pop_to_plot_and_save=5,
             gen_spike_saves_for_all_somas=True,
             format='xml'):

    num_exc = scale_pop_size(80, scalePops)
    num_inh = scale_pop_size(40, scalePops)

    nml_doc, network = oc.generate_network(reference)

    oc.include_opencortex_cell(
        nml_doc, 'AllenInstituteCellTypesDB_HH/HH_477127614.cell.nml')
    oc.include_opencortex_cell(
        nml_doc, 'AllenInstituteCellTypesDB_HH/HH_476686112.cell.nml')

    if num_bbp > 0:
        oc.include_opencortex_cell(
            nml_doc,
            'BlueBrainProject_NMC/cADpyr229_L23_PC_5ecbf9b163_0_0.cell.nml')

    xDim = 400 * scalex
    yDim = 500 * scaley
    zDim = 300 * scalez

    xs = -200
    ys = -150
    zs = 100

    #####   Synapses

    synAmpa1 = oc.add_exp_two_syn(nml_doc,
                                  id="synAmpa1",
                                  gbase="1nS",
                                  erev="0mV",
                                  tau_rise="0.5ms",
                                  tau_decay="5ms")

    synGaba1 = oc.add_exp_two_syn(nml_doc,
                                  id="synGaba1",
                                  gbase="2nS",
                                  erev="-80mV",
                                  tau_rise="1ms",
                                  tau_decay="20ms")

    #####   Input types

    pfs1 = oc.add_poisson_firing_synapse(nml_doc,
                                         id="psf1",
                                         average_rate="%s Hz" % input_rate,
                                         synapse_id=synAmpa1.id)

    #####   Populations

    popExc = oc.add_population_in_rectangular_region(network,
                                                     'popExc',
                                                     'HH_477127614',
                                                     num_exc,
                                                     xs,
                                                     ys,
                                                     zs,
                                                     xDim,
                                                     yDim,
                                                     zDim,
                                                     color='.8 0 0')

    popInh = oc.add_population_in_rectangular_region(network,
                                                     'popInh',
                                                     'HH_476686112',
                                                     num_inh,
                                                     xs,
                                                     ys,
                                                     zs,
                                                     xDim,
                                                     yDim,
                                                     zDim,
                                                     color='0 0 .8')
    if num_bbp == 1:
        popBBP = oc.add_single_cell_population(
            network,
            'popBBP',
            'cADpyr229_L23_PC_5ecbf9b163_0_0',
            z=200,
            color='0 .8 0')
    elif num_bbp > 1:

        popBBP = oc.add_population_in_rectangular_region(
            network,
            'popBBP',
            'cADpyr229_L23_PC_5ecbf9b163_0_0',
            num_bbp,
            xs,
            ys,
            zs,
            xDim,
            yDim,
            zDim,
            color='0 .8 0')

    #####   Projections

    total_conns = 0
    if connections:

        proj = oc.add_probabilistic_projection(network,
                                               "proj0",
                                               popExc,
                                               popExc,
                                               synAmpa1.id,
                                               0.5,
                                               delay=global_delay)
        total_conns += len(proj.connection_wds)

        proj = oc.add_probabilistic_projection(network,
                                               "proj1",
                                               popExc,
                                               popInh,
                                               synAmpa1.id,
                                               0.7,
                                               delay=global_delay)
        total_conns += len(proj.connection_wds)

        proj = oc.add_probabilistic_projection(network,
                                               "proj3",
                                               popInh,
                                               popExc,
                                               synGaba1.id,
                                               0.7,
                                               delay=global_delay)
        total_conns += len(proj.connection_wds)

        proj = oc.add_probabilistic_projection(network,
                                               "proj4",
                                               popInh,
                                               popInh,
                                               synGaba1.id,
                                               0.5,
                                               delay=global_delay)
        total_conns += len(proj.connection_wds)

        if num_bbp > 0:
            proj = oc.add_probabilistic_projection(network,
                                                   "proj5",
                                                   popExc,
                                                   popBBP,
                                                   synAmpa1.id,
                                                   0.5,
                                                   delay=global_delay)

        total_conns += len(proj.connection_wds)

    #####   Inputs

    oc.add_inputs_to_population(network,
                                "Stim0",
                                popExc,
                                pfs1.id,
                                all_cells=True)

    #####   Save NeuroML and LEMS Simulation files

    if num_bbp != 1:
        new_reference = 'Balanced_%scells_%sconns' % (num_bbp + num_exc +
                                                      num_inh, total_conns)
        network.id = new_reference
        nml_doc.id = new_reference

    nml_file_name = '%s.net.%s' % (network.id,
                                   'nml.h5' if format == 'hdf5' else 'nml')
    oc.save_network(nml_doc,
                    nml_file_name,
                    validate=(format == 'xml'),
                    format=format)

    if format == 'xml':

        plot_v = {popExc.id: [], popInh.id: []}
        save_v = {'%s_v.dat' % popExc.id: [], '%s_v.dat' % popInh.id: []}

        if num_bbp > 0:
            plot_v[popBBP.id] = []
            save_v['%s_v.dat' % popBBP.id] = []

        for i in range(min(max_in_pop_to_plot_and_save, num_exc)):
            plot_v[popExc.id].append("%s/%i/%s/v" %
                                     (popExc.id, i, popExc.component))
            save_v['%s_v.dat' % popExc.id].append(
                "%s/%i/%s/v" % (popExc.id, i, popExc.component))

        for i in range(min(max_in_pop_to_plot_and_save, num_inh)):
            plot_v[popInh.id].append("%s/%i/%s/v" %
                                     (popInh.id, i, popInh.component))
            save_v['%s_v.dat' % popInh.id].append(
                "%s/%i/%s/v" % (popInh.id, i, popInh.component))

        for i in range(min(max_in_pop_to_plot_and_save, num_bbp)):
            plot_v[popBBP.id].append("%s/%i/%s/v" %
                                     (popBBP.id, i, popBBP.component))
            save_v['%s_v.dat' % popBBP.id].append(
                "%s/%i/%s/v" % (popBBP.id, i, popBBP.component))

        lems_file_name = oc.generate_lems_simulation(
            nml_doc,
            network,
            nml_file_name,
            duration=duration,
            dt=0.025,
            gen_plots_for_all_v=False,
            gen_plots_for_quantities=plot_v,
            gen_saves_for_all_v=False,
            gen_saves_for_quantities=save_v,
            gen_spike_saves_for_all_somas=gen_spike_saves_for_all_somas)
    else:
        lems_file_name = None

    return nml_doc, nml_file_name, lems_file_name
                                   id="poissonFiringSyn100",
                                   average_rate="100 Hz",
                                   synapse_id='AMPA_noplast')
                                   
pg0 = oc.add_pulse_generator(nml_doc,
                       id="pg0",
                       delay="1000ms",
                       duration="1000ms",
                       amplitude="0.2nA")
                                     
                             
#####   Populations

pop0 = oc.add_population_in_rectangular_region(network,
                                              'pop0',
                                              'RS',
                                              scale_pop_size(1),
                                              0,offset,0,
                                              xDim,yDim,zDim)
offset+=yDim

pop1 = oc.add_population_in_rectangular_region(network,
                                              'pop1',
                                              'RS',
                                              scale_pop_size(1),
                                              0,offset,0,
                                              xDim,yDim,zDim)


#####   Projections

示例#15
0
def generate(reference="L23TraubDemo",
             num_rs=DEFAULT_RS_POP_SIZE,
             num_bask=DEFAULT_BASK_POP_SIZE,
             scalex=1,
             scaley=1,
             scalez=1,
             connections=False,
             poisson_inputs=True,
             offset_curent_range_pA=None,
             global_delay=0,
             duration=300,
             segments_to_plot_record={
                 'pop_rs': [0],
                 'pop_bask': [0]
             },
             format='xml'):

    nml_doc, network = oc.generate_network(reference)

    #oc.add_cell_and_channels(nml_doc, 'acnet2/pyr_4_sym.cell.nml','pyr_4_sym')
    oc.include_opencortex_cell(nml_doc, 'Thalamocortical/L23PyrRS.cell.nml')
    oc.include_opencortex_cell(nml_doc, 'Thalamocortical/SupBasket.cell.nml')

    xDim = 500 * scalex
    yDim = 200 * scaley
    zDim = 500 * scalez

    pop_rs = oc.add_population_in_rectangular_region(network, 'pop_rs',
                                                     'L23PyrRS', num_rs, 0, 0,
                                                     0, xDim, yDim, zDim)

    pop_bask = oc.add_population_in_rectangular_region(network, 'pop_bask',
                                                       'SupBasket', num_bask,
                                                       0, 0, 0, xDim, yDim,
                                                       zDim)

    syn0 = oc.add_exp_two_syn(nml_doc,
                              id="syn0",
                              gbase="1nS",
                              erev="0mV",
                              tau_rise="0.5ms",
                              tau_decay="10ms")

    syn1 = oc.add_exp_two_syn(nml_doc,
                              id="syn1",
                              gbase="2nS",
                              erev="0mV",
                              tau_rise="1ms",
                              tau_decay="15ms")

    if poisson_inputs:

        pfs = oc.add_poisson_firing_synapse(nml_doc,
                                            id="poissonFiringSyn",
                                            average_rate="150 Hz",
                                            synapse_id=syn0.id)

        oc.add_inputs_to_population(network,
                                    "Stim0",
                                    pop_rs,
                                    pfs.id,
                                    all_cells=True)
        '''                            
        oc.add_inputs_to_population(network,
                                    "Stim1",
                                    pop_bask,
                                    pfs.id,
                                    all_cells=True)'''
    if offset_curent_range_pA:

        for pop_id in offset_curent_range_pA:

            pop = next(p for p in network.populations if p.id == pop_id)

            pg0 = oc.add_pulse_generator(nml_doc,
                                         id="offset_current_%s" % pop.id,
                                         delay="0ms",
                                         duration="%sms" % duration,
                                         amplitude="1pA")

            import neuroml
            import random

            input_list = neuroml.InputList(id="inputs_offset_current_%s" %
                                           pop.id,
                                           component=pg0.id,
                                           populations=pop.id)

            network.input_lists.append(input_list)

            min_, max_ = offset_curent_range_pA[pop_id]

            for i in range(pop.get_size()):

                input = neuroml.InputW(
                    id=i,
                    target="../%s/%i/%s" % (pop.id, i, pop.component),
                    destination="synapses",
                    weight=(min_ + (max_ - min_) * random.random()))

                input_list.input_ws.append(input)

    total_conns = 0
    if connections:

        proj = oc.add_probabilistic_projection(network,
                                               "proj0",
                                               pop_rs,
                                               pop_bask,
                                               syn1.id,
                                               1,
                                               weight=1,
                                               delay=global_delay)
        '''
        proj = oc.add_targeted_projection(nml_doc,
                                        network,
                                        "proj0",
                                        presynaptic_population = pop_rs,
                                        postsynaptic_population = pop_bask,
                                        targeting_mode = 'convergent',
                                        synapse_list = [syn1.id],
                                        number_conns_per_cell = 1,
                                        pre_segment_group = 'axon_group',
                                        post_segment_group = 'dendrite_group',
                                        delays_dict = {syn1.id:2},
                                        weights_dict = {syn1.id:1})'''

        if proj:
            total_conns += len(proj.connection_wds)

    if num_rs != DEFAULT_RS_POP_SIZE or num_bask != DEFAULT_BASK_POP_SIZE:
        new_reference = '%s_%scells_%sconns' % (nml_doc.id, num_rs + num_bask,
                                                total_conns)
        network.id = new_reference
        nml_doc.id = new_reference

    nml_file_name = '%s.net.%s' % (network.id,
                                   'nml.h5' if format == 'hdf5' else 'nml')
    oc.save_network(nml_doc,
                    nml_file_name,
                    validate=(format == 'xml'),
                    format=format)

    if format == 'xml':
        gen_plots_for_quantities = {
        }  #  Dict with displays vs lists of quantity paths
        gen_saves_for_quantities = {
        }  #  Dict with file names vs lists of quantity paths

        for pop in segments_to_plot_record.keys():
            pop_nml = network.get_by_id(pop)
            if pop_nml is not None and pop_nml.size > 0:
                for i in range(int(pop_nml.size)):
                    gen_plots_for_quantities['Display_%s_%i_v' % (pop, i)] = []
                    gen_saves_for_quantities['Sim_%s.%s.%i.v.dat' %
                                             (nml_doc.id, pop, i)] = []

                    for seg in segments_to_plot_record[pop]:
                        quantity = '%s/%i/%s/%i/v' % (pop, i,
                                                      pop_nml.component, seg)
                        gen_plots_for_quantities['Display_%s_%i_v' %
                                                 (pop, i)].append(quantity)
                        gen_saves_for_quantities['Sim_%s.%s.%i.v.dat' %
                                                 (nml_doc.id, pop,
                                                  i)].append(quantity)

        lems_file_name = oc.generate_lems_simulation(
            nml_doc,
            network,
            nml_file_name,
            duration=duration,
            dt=0.025,
            gen_plots_for_all_v=False,
            gen_plots_for_quantities=gen_plots_for_quantities,
            gen_saves_for_all_v=False,
            gen_saves_for_quantities=gen_saves_for_quantities)
    else:
        lems_file_name = None

    return nml_doc, nml_file_name, lems_file_name
示例#16
0
def generate(reference="GapJunctions",
             num_pre=5,
             num_post=2,
             connections=True,
             duration=1000,
             segments_to_plot_record={
                 'pop_pre': [0],
                 'pop_post': [0]
             },
             format='xml'):

    nml_doc, network = oc.generate_network(reference)

    oc.include_opencortex_cell(nml_doc, 'acnet2/pyr_4_sym.cell.nml')

    xDim = 500
    yDim = 50
    zDim = 500

    pop_pre = oc.add_population_in_rectangular_region(network, 'pop_pre',
                                                      'pyr_4_sym', num_pre, 0,
                                                      0, 0, xDim, yDim, zDim)

    pop_post = oc.add_population_in_rectangular_region(network, 'pop_post',
                                                       'pyr_4_sym', num_post,
                                                       0, yDim, 0, xDim,
                                                       yDim + yDim, zDim)

    ampa_syn = oc.add_exp_two_syn(nml_doc,
                                  id="AMPA_syn",
                                  gbase="30e-9S",
                                  erev="0mV",
                                  tau_rise="0.003s",
                                  tau_decay="0.0031s")

    gj_syn = oc.add_gap_junction_synapse(nml_doc, id="gj0", conductance="5nS")

    pfs = oc.add_poisson_firing_synapse(nml_doc,
                                        id="poissonFiringSyn",
                                        average_rate="30 Hz",
                                        synapse_id=ampa_syn.id)

    oc.add_inputs_to_population(network,
                                "Stim0",
                                pop_pre,
                                pfs.id,
                                all_cells=True)

    total_conns = 0
    if connections:

        this_syn = gj_syn.id
        proj = oc.add_targeted_electrical_projection(
            nml_doc,
            network,
            "Proj0",
            pop_pre,
            pop_post,
            targeting_mode='convergent',
            synapse_list=[this_syn],
            pre_segment_group='soma_group',
            post_segment_group='dendrite_group',
            number_conns_per_cell=3)
        if proj:
            total_conns += len(proj[0].electrical_connections) + len(
                proj[0].electrical_connection_instances)

    nml_file_name = '%s.net.%s' % (network.id,
                                   'nml.h5' if format == 'hdf5' else 'nml')
    oc.save_network(nml_doc,
                    nml_file_name,
                    validate=(format == 'xml'),
                    format=format)

    if format == 'xml':

        gen_plots_for_quantities = {
        }  #  Dict with displays vs lists of quantity paths
        gen_saves_for_quantities = {
        }  #  Dict with file names vs lists of quantity paths

        for pop in segments_to_plot_record.keys():
            pop_nml = network.get_by_id(pop)
            if pop_nml is not None and pop_nml.size > 0:

                group = len(segments_to_plot_record[pop]) == 1
                if group:
                    display = 'Display_%s_v' % (pop)
                    file_ = 'Sim_%s.%s.v.dat' % (nml_doc.id, pop)
                    gen_plots_for_quantities[display] = []
                    gen_saves_for_quantities[file_] = []

                for i in range(int(pop_nml.size)):
                    if not group:
                        display = 'Display_%s_%i_v' % (pop, i)
                        file_ = 'Sim_%s.%s.%i.v.dat' % (nml_doc.id, pop, i)
                        gen_plots_for_quantities[display] = []
                        gen_saves_for_quantities[file_] = []

                    for seg in segments_to_plot_record[pop]:
                        quantity = '%s/%i/%s/%i/v' % (pop, i,
                                                      pop_nml.component, seg)
                        gen_plots_for_quantities[display].append(quantity)
                        gen_saves_for_quantities[file_].append(quantity)

        lems_file_name = oc.generate_lems_simulation(
            nml_doc,
            network,
            nml_file_name,
            duration=duration,
            dt=0.025,
            gen_plots_for_all_v=False,
            gen_plots_for_quantities=gen_plots_for_quantities,
            gen_saves_for_all_v=False,
            gen_saves_for_quantities=gen_saves_for_quantities)
    else:
        lems_file_name = None

    return nml_doc, nml_file_name, lems_file_name
示例#17
0
def generate(reference = "Weights",
             num_each = 6,
             connections=True,
             duration = 1000,
             format='xml'):

    nml_doc, network = oc.generate_network(reference)

    cell_id = 'HH_477127614'
    cell = oc.include_opencortex_cell(nml_doc, 'AllenInstituteCellTypesDB_HH/%s.cell.nml'%cell_id)

    xDim = 500
    yDim = 500
    zDim = 30

    pop_pre = oc.add_population_in_rectangular_region(network, 'pop_pre',
                                                  cell_id, num_each,
                                                  0,0,0, xDim,yDim,zDim,
                                                  color='.8 0 0')

    pop_post_chem_exc = oc.add_population_in_rectangular_region(network, 'pop_post_chem_exc',
                                                  cell_id, num_each+1,
                                                  0,yDim,0, xDim,yDim,zDim,
                                                  color='0 0 .8')

    pop_post_chem_inh = oc.add_population_in_rectangular_region(network, 'pop_post_chem_inh',
                                                  cell_id, num_each+2,
                                                  xDim,yDim,0, xDim,yDim,zDim,
                                                  color='0 .8 .8')
                                                  
    pop_post_cont = oc.add_population_in_rectangular_region(network, 'pop_post_cont',
                                                  cell_id, num_each+3,
                                                  xDim,0,0, xDim,yDim,zDim,
                                                  color='0 .8 0')

    ampa_syn = oc.add_exp_two_syn(nml_doc, id="AMPA_syn", 
                             gbase="10nS", erev="0mV",
                             tau_rise="2ms", tau_decay="10ms")
                             
    gaba_syn = oc.add_exp_two_syn(nml_doc, id="GABA_syn", 
                             gbase="10nS", erev="-80mV",
                             tau_rise="3ms", tau_decay="30ms")

    gj_syn = oc.add_gap_junction_synapse(nml_doc, id="gj0", 
                             conductance=".05nS")
                             
    
    analog_syn = GradedSynapse(id='analog_syn',
                             conductance="10nS",
                             delta="5mV",
                             Vth="-35mV",
                             k="0.025per_ms",
                             erev="0mV")
    silent_syn = SilentSynapse(id="silent1")
    
    nml_doc.graded_synapses.append(analog_syn)
    nml_doc.silent_synapses.append(silent_syn)


    pfs = oc.add_poisson_firing_synapse(nml_doc, id="poissonFiringSyn",
                                       average_rate="10 Hz", synapse_id=ampa_syn.id)

    oc.add_inputs_to_population(network, "Stim0",
                                pop_pre, pfs.id, all_cells=True)


    if connections:
        
        proj_chem_exc = oc.add_probabilistic_projection(network,
                                "proj_chem_exc",
                                pop_pre,
                                pop_post_chem_exc,
                                ampa_syn.id,
                                0.7,
                                weight=1,
                                delay=5)
                                
        for conn in proj_chem_exc.connection_wds:
            if conn.get_pre_cell_id() < 3 and conn.get_post_cell_id() < 3:
                conn.weight = 0.5
        
        proj_chem_inh = oc.add_probabilistic_projection(network,
                                "proj_chem_inh",
                                pop_pre,
                                pop_post_chem_inh,
                                gaba_syn.id,
                                0.7,
                                weight=1,
                                delay=5)
                                
        for conn in proj_chem_inh.connection_wds:
            if conn.get_pre_cell_id() < 3 and conn.get_post_cell_id() < 3:
                conn.weight = 2
            
        
        proj_cont = ContinuousProjection(id='proj_cont', \
                           presynaptic_population=pop_pre.id,
                           postsynaptic_population=pop_post_cont.id)
        network.continuous_projections.append(proj_cont)
        
        for i in range(pop_pre.get_size()):
            for j in range(pop_post_cont.get_size()):
                conn0 = ContinuousConnectionInstanceW(id='%s'%(j+i*pop_pre.get_size()), \
                           pre_cell='../%s/%s/%s'%(pop_pre.id,i,cell_id),
                           post_cell='../%s/%s/%s'%(pop_post_cont.id,j,cell_id),
                           pre_component=silent_syn.id,
                           post_component=analog_syn.id,
                           weight=(i+j)/10.0)
                proj_cont.continuous_connection_instance_ws.append(conn0)
        
        
            
        gj_pops = [pop_pre, pop_post_chem_exc, pop_post_chem_inh, pop_post_cont]
        
        for pre in gj_pops:
            for post in gj_pops:
                
                proj_gap = oc.add_targeted_electrical_projection(nml_doc, 
                                                network,
                                                "proj_",
                                                pre,
                                                post,
                                                targeting_mode='convergent',
                                                synapse_list=[gj_syn.id],
                                                pre_segment_group = 'soma_group',
                                                post_segment_group = 'soma_group',
                                                number_conns_per_cell=3)

                for conn in network.electrical_projections[-1].electrical_connection_instance_ws:
                    conn.weight = conn.get_pre_cell_id() + conn.get_post_cell_id()


    nml_file_name = '%s.net.%s'%(network.id,'nml.h5' if format == 'hdf5' else 'nml')
    target_dir = 'HDF5/' if format == 'hdf5' else './'
    
    oc.save_network(nml_doc, 
                    nml_file_name, 
                    validate=(format=='xml'),
                    format = format,
                    target_dir=target_dir)

    if format=='xml':

        lems_file_name = oc.generate_lems_simulation(nml_doc, network, 
                                nml_file_name, 
                                duration =      duration, 
                                dt =            0.025,
                                gen_plots_for_all_v = True,
                                gen_saves_for_all_v = True)
    else:
        lems_file_name = None

    return nml_doc, nml_file_name, lems_file_name
示例#18
0
def generate(reference="Balanced",
             scalePops=1,
             scalex=1,
             scaley=1,
             scalez=1,
             connections=True,
             connections_scaling=1,
             duration=1000,
             global_delay=2,
             max_in_pop_to_plot_and_save=5,
             gen_spike_saves_for_all_somas=True,
             deterministic=True,
             format='xml'):

    num_exc = scale_pop_size(80, scalePops)
    num_inh = scale_pop_size(40, scalePops)

    if scalePops != 1:
        reference += '_%s' % scalePops

    nml_doc, network = oc.generate_network(reference)

    oc.include_opencortex_cell(
        nml_doc, 'AllenInstituteCellTypesDB_HH/HH_477127614.cell.nml')
    oc.include_opencortex_cell(
        nml_doc, 'AllenInstituteCellTypesDB_HH/HH_476686112.cell.nml')

    xDim = 400 * scalex
    yDim = 500 * scaley
    zDim = 300 * scalez

    xs = -200
    ys = -150
    zs = 100

    #####   Synapses

    synAmpa1 = oc.add_exp_two_syn(nml_doc,
                                  id="synAmpa1",
                                  gbase="1nS",
                                  erev="0mV",
                                  tau_rise="0.5ms",
                                  tau_decay="5ms")

    synGaba1 = oc.add_exp_two_syn(nml_doc,
                                  id="synGaba1",
                                  gbase="2nS",
                                  erev="-80mV",
                                  tau_rise="1ms",
                                  tau_decay="20ms")

    #####   Input types

    if not deterministic:
        pfs1 = oc.add_poisson_firing_synapse(nml_doc,
                                             id="psf1",
                                             average_rate="150 Hz",
                                             synapse_id=synAmpa1.id)

    #####   Populations

    popExc = oc.add_population_in_rectangular_region(network, 'popExc',
                                                     'HH_477127614', num_exc,
                                                     xs, ys, zs, xDim, yDim,
                                                     zDim)

    popInh = oc.add_population_in_rectangular_region(network, 'popInh',
                                                     'HH_476686112', num_inh,
                                                     xs, ys, zs, xDim, yDim,
                                                     zDim)

    #####   Projections

    total_conns = 0
    if connections:
        proj = oc.add_probabilistic_projection(network,
                                               "proj0",
                                               popExc,
                                               popExc,
                                               synAmpa1.id,
                                               connections_scaling * 0.3,
                                               delay=global_delay)
        total_conns += len(proj.connection_wds)

        proj = oc.add_probabilistic_projection(network,
                                               "proj1",
                                               popExc,
                                               popInh,
                                               synAmpa1.id,
                                               connections_scaling * 0.5,
                                               delay=global_delay)
        total_conns += len(proj.connection_wds)

        proj = oc.add_probabilistic_projection(network,
                                               "proj3",
                                               popInh,
                                               popExc,
                                               synGaba1.id,
                                               connections_scaling * 0.7,
                                               delay=global_delay)
        total_conns += len(proj.connection_wds)

        proj = oc.add_probabilistic_projection(network,
                                               "proj4",
                                               popInh,
                                               popInh,
                                               synGaba1.id,
                                               connections_scaling * 0.5,
                                               delay=global_delay)
        total_conns += len(proj.connection_wds)

    #####   Inputs

    if not deterministic:
        oc.add_inputs_to_population(network,
                                    "Stim0",
                                    popExc,
                                    pfs1.id,
                                    all_cells=True)

    else:

        for i in range(num_exc):

            pg = oc.add_pulse_generator(nml_doc,
                                        id="pg_%i" % i,
                                        delay="0ms",
                                        duration="10000ms",
                                        amplitude="%snA" % (random() * 0.5))

            oc.add_inputs_to_population(network,
                                        "Stim_%i" % i,
                                        popExc,
                                        pg.id,
                                        all_cells=False,
                                        only_cells=[i])

    #####   Save NeuroML and LEMS Simulation files

    nml_file_name = '%s%s.net.%s' % ('XH_' if format == 'xml_hdf5' else '',
                                     network.id,
                                     'nml.h5' if format == 'hdf5' else 'nml')

    oc.save_network(nml_doc,
                    nml_file_name,
                    validate=(format == 'xml'),
                    format=format)

    plot_v = {popExc.id: [], popInh.id: []}
    save_v = {'%s_v.dat' % popExc.id: [], '%s_v.dat' % popInh.id: []}

    for i in range(min(max_in_pop_to_plot_and_save, num_exc)):
        plot_v[popExc.id].append("%s/%i/%s/v" %
                                 (popExc.id, i, popExc.component))
        save_v['%s_v.dat' % popExc.id].append("%s/%i/%s/v" %
                                              (popExc.id, i, popExc.component))

    for i in range(min(max_in_pop_to_plot_and_save, num_inh)):
        plot_v[popInh.id].append("%s/%i/%s/v" %
                                 (popInh.id, i, popInh.component))
        save_v['%s_v.dat' % popInh.id].append("%s/%i/%s/v" %
                                              (popInh.id, i, popInh.component))

    lems_file_name = "LEMS_%s.xml" % network.id
    if format != 'xml':
        lems_file_name = "LEMS_%s_%s.xml" % (network.id, format)

    lems_file_name = oc.generate_lems_simulation(
        nml_doc,
        network,
        nml_file_name,
        duration=duration,
        dt=0.025,
        gen_plots_for_all_v=False,
        gen_plots_for_quantities=plot_v,
        gen_saves_for_all_v=False,
        gen_saves_for_quantities=save_v,
        gen_spike_saves_for_all_somas=gen_spike_saves_for_all_somas,
        lems_file_name=lems_file_name)

    return nml_doc, nml_file_name, lems_file_name
示例#19
0
pg0 = oc.add_pulse_generator(nml_doc,
                             id="pg0",
                             delay="50ms",
                             duration="400ms",
                             amplitude="0.6nA")

pg1 = oc.add_pulse_generator(nml_doc,
                             id="pg1",
                             delay="50ms",
                             duration="400ms",
                             amplitude="0.5nA")

#####   Populations

pop_iaf = oc.add_population_in_rectangular_region(network, 'pop_iaf', 'iaf', 5,
                                                  0, offset, 0, xDim, yDim,
                                                  zDim)
offset += yDim

pop_rs = oc.add_population_in_rectangular_region(network, 'pop_rs', 'RS', 5, 0,
                                                 offset, 0, xDim, yDim, zDim)

#####   Projections

oc.add_probabilistic_projection(network, "proj0", pop_iaf, pop_rs, synAmpa1.id,
                                0.5)

#####   Inputs

oc.add_inputs_to_population(network,
                            "Stim0",
示例#20
0
def generate(reference="DendConn",
             num_pyr=4,
             num_bask=0,
             scalex=1,
             scaley=1,
             scalez=1,
             connections=True,
             global_delay=0,
             duration=500,
             segments_to_plot_record={
                 'pop_pyr': [0],
                 'pop_bask': [0]
             },
             format='xml'):

    nml_doc, network = oc.generate_network(reference)

    oc.include_opencortex_cell(nml_doc, 'acnet2/pyr_4_sym.cell.nml')
    oc.include_opencortex_cell(nml_doc, 'acnet2/bask.cell.nml')

    xDim = 500 * scalex
    yDim = 50 * scaley
    zDim = 500 * scalez

    pop_pyr = oc.add_population_in_rectangular_region(network, 'pop_pyr',
                                                      'pyr_4_sym', num_pyr, 0,
                                                      0, 0, xDim, yDim, zDim)

    pop_bask = oc.add_population_in_rectangular_region(network, 'pop_bask',
                                                       'bask', num_bask, 0,
                                                       yDim, 0, xDim,
                                                       yDim + yDim, zDim)

    ampa_syn = oc.add_exp_two_syn(nml_doc,
                                  id="AMPA_syn",
                                  gbase="30e-9S",
                                  erev="0mV",
                                  tau_rise="0.003s",
                                  tau_decay="0.0031s")

    pg = oc.add_pulse_generator(nml_doc,
                                id="pg0",
                                delay="10ms",
                                duration="300ms",
                                amplitude="0.7nA")

    total_conns = 0
    if connections:

        this_syn = ampa_syn.id
        proj = oc.add_targeted_projection(network,
                                          "Proj_pyr_pyr",
                                          pop_pyr,
                                          pop_pyr,
                                          targeting_mode='convergent',
                                          synapse_list=[this_syn],
                                          pre_segment_group='soma_group',
                                          post_segment_group='dendrite_group',
                                          number_conns_per_cell=1,
                                          delays_dict={this_syn: global_delay})
        if proj:
            total_conns += len(proj[0].connection_wds)

    oc.add_targeted_inputs_to_population(network,
                                         "Stim0",
                                         pop_pyr,
                                         pg.id,
                                         segment_group='soma_group',
                                         number_per_cell=1,
                                         all_cells=False,
                                         only_cells=[0])

    nml_file_name = '%s.net.%s' % (network.id,
                                   'nml.h5' if format == 'hdf5' else 'nml')
    target_dir = 'HDF5/' if format == 'hdf5' else './'

    oc.save_network(nml_doc,
                    nml_file_name,
                    validate=(format == 'xml'),
                    format=format,
                    target_dir=target_dir)

    gen_plots_for_quantities = {
    }  #  Dict with displays vs lists of quantity paths
    gen_saves_for_quantities = {
    }  #  Dict with file names vs lists of quantity paths

    for pop in segments_to_plot_record.keys():
        pop_nml = network.get_by_id(pop)
        if pop_nml is not None and pop_nml.size > 0:

            group = len(segments_to_plot_record[pop]) == 1
            if group:
                display = 'Display_%s_v' % (pop)
                file_ = 'Sim_%s.%s.v.dat' % (nml_doc.id, pop)
                gen_plots_for_quantities[display] = []
                gen_saves_for_quantities[file_] = []

            for i in range(int(pop_nml.size)):
                if not group:
                    display = 'Display_%s_%i_v' % (pop, i)
                    file_ = 'Sim_%s.%s.%i.v.dat' % (nml_doc.id, pop, i)
                    gen_plots_for_quantities[display] = []
                    gen_saves_for_quantities[file_] = []

                for seg in segments_to_plot_record[pop]:
                    quantity = '%s/%i/%s/%i/v' % (pop, i, pop_nml.component,
                                                  seg)
                    gen_plots_for_quantities[display].append(quantity)
                    gen_saves_for_quantities[file_].append(quantity)

    lems_file_name = oc.generate_lems_simulation(
        nml_doc,
        network,
        target_dir + nml_file_name,
        duration=duration,
        dt=0.025,
        gen_plots_for_all_v=False,
        gen_plots_for_quantities=gen_plots_for_quantities,
        gen_saves_for_all_v=False,
        gen_saves_for_quantities=gen_saves_for_quantities,
        target_dir=target_dir)

    return nml_doc, nml_file_name, lems_file_name
示例#21
0
def generate(scalePops=1,
             percentage_exc_detailed=0,
             scalex=1,
             scaley=1,
             scalez=1,
             ratio_inh_exc=2,
             connections=True,
             duration=1000,
             input_rate=150,
             global_delay=2,
             max_in_pop_to_plot_and_save=5,
             format='xml',
             run_in_simulator=None):

    reference = ("Multiscale__g%s__i%s" % (ratio_inh_exc, input_rate)).replace(
        '.', '_')

    num_exc = scale_pop_size(80, scalePops)
    num_exc2 = int(0.5 + num_exc * percentage_exc_detailed / 100.0)
    num_exc -= num_exc2
    num_inh = scale_pop_size(40, scalePops)

    nml_doc, network = oc.generate_network(reference)

    oc.include_opencortex_cell(
        nml_doc, 'AllenInstituteCellTypesDB_HH/HH_477127614.cell.nml')
    oc.include_opencortex_cell(
        nml_doc, 'AllenInstituteCellTypesDB_HH/HH_476686112.cell.nml')
    oc.include_opencortex_cell(nml_doc,
                               'L23Pyr_SmithEtAl2013/L23_NoHotSpot.cell.nml')

    xDim = 1000 * scalex
    yDim = 300 * scaley
    zDim = 1000 * scalez

    xs = -200
    ys = -150
    zs = 100

    #####   Synapses

    exc_syn_nS = 1.

    synAmpa1 = oc.add_exp_two_syn(nml_doc,
                                  id="synAmpa1",
                                  gbase="%snS" % exc_syn_nS,
                                  erev="0mV",
                                  tau_rise="0.5ms",
                                  tau_decay="5ms")

    synGaba1 = oc.add_exp_two_syn(nml_doc,
                                  id="synGaba1",
                                  gbase="%snS" % (exc_syn_nS * ratio_inh_exc),
                                  erev="-80mV",
                                  tau_rise="1ms",
                                  tau_decay="20ms")

    #####   Input types

    pfs1 = oc.add_poisson_firing_synapse(nml_doc,
                                         id="psf1",
                                         average_rate="%s Hz" % input_rate,
                                         synapse_id=synAmpa1.id)

    #####   Populations

    popExc = oc.add_population_in_rectangular_region(network,
                                                     'popExc',
                                                     'HH_477127614',
                                                     num_exc,
                                                     xs,
                                                     ys,
                                                     zs,
                                                     xDim,
                                                     yDim,
                                                     zDim,
                                                     color='0 0 1')

    popExc2 = oc.add_population_in_rectangular_region(network,
                                                      'popExc2',
                                                      'L23_NoHotSpot',
                                                      num_exc2,
                                                      xs,
                                                      ys,
                                                      zs,
                                                      xDim,
                                                      yDim,
                                                      zDim,
                                                      color='0 1 0')

    allExc = [popExc, popExc2]

    popInh = oc.add_population_in_rectangular_region(network,
                                                     'popInh',
                                                     'HH_476686112',
                                                     num_inh,
                                                     xs,
                                                     ys,
                                                     zs,
                                                     xDim,
                                                     yDim,
                                                     zDim,
                                                     color='1 0 0')

    #####   Projections

    if connections:

        for pop1 in allExc:

            for pop2 in allExc:
                proj = oc.add_probabilistic_projection(network,
                                                       "proj0",
                                                       pop1,
                                                       pop2,
                                                       synAmpa1.id,
                                                       0.5,
                                                       delay=global_delay)

            proj = oc.add_probabilistic_projection(network,
                                                   "proj1",
                                                   pop1,
                                                   popInh,
                                                   synAmpa1.id,
                                                   0.7,
                                                   delay=global_delay)

            proj = oc.add_probabilistic_projection(network,
                                                   "proj2",
                                                   popInh,
                                                   pop1,
                                                   synGaba1.id,
                                                   0.7,
                                                   delay=global_delay)

        proj = oc.add_probabilistic_projection(network,
                                               "proj3",
                                               popInh,
                                               popInh,
                                               synGaba1.id,
                                               0.5,
                                               delay=global_delay)

    #####   Inputs

    for pop in allExc:
        oc.add_inputs_to_population(network,
                                    "Stim_%s" % pop.id,
                                    pop,
                                    pfs1.id,
                                    all_cells=True)

    #####   Save NeuroML and LEMS Simulation files

    target_dir = './temp/'

    nml_file_name = '%s.net.%s' % (network.id,
                                   'nml.h5' if format == 'hdf5' else 'nml')
    oc.save_network(nml_doc,
                    nml_file_name,
                    validate=(format == 'xml'),
                    format=format,
                    target_dir=target_dir)

    if format == 'xml':

        plot_v = {popExc.id: [], popExc2.id: [], popInh.id: []}
        exc_traces = '%s_%s_v.dat' % (network.id, popExc.id)
        exc2_traces = '%s_%s_v.dat' % (network.id, popExc2.id)
        inh_traces = '%s_%s_v.dat' % (network.id, popInh.id)
        save_v = {exc_traces: [], inh_traces: [], exc2_traces: []}

        for i in range(min(max_in_pop_to_plot_and_save, num_exc)):
            plot_v[popExc.id].append("%s/%i/%s/v" %
                                     (popExc.id, i, popExc.component))
            save_v[exc_traces].append("%s/%i/%s/v" %
                                      (popExc.id, i, popExc.component))

        for i in range(min(max_in_pop_to_plot_and_save, num_exc2)):
            plot_v[popExc2.id].append("%s/%i/%s/v" %
                                      (popExc2.id, i, popExc2.component))
            save_v[exc2_traces].append("%s/%i/%s/v" %
                                       (popExc2.id, i, popExc2.component))

        for i in range(min(max_in_pop_to_plot_and_save, num_inh)):
            plot_v[popInh.id].append("%s/%i/%s/v" %
                                     (popInh.id, i, popInh.component))
            save_v[inh_traces].append("%s/%i/%s/v" %
                                      (popInh.id, i, popInh.component))

        gen_spike_saves_for_all_somas = run_in_simulator != 'jNeuroML_NetPyNE'

        lems_file_name = oc.generate_lems_simulation(
            nml_doc,
            network,
            target_dir + nml_file_name,
            duration=duration,
            dt=0.025,
            gen_plots_for_all_v=False,
            gen_plots_for_quantities=plot_v,
            gen_saves_for_all_v=False,
            gen_saves_for_quantities=save_v,
            gen_spike_saves_for_all_somas=gen_spike_saves_for_all_somas,
            target_dir=target_dir)

        if run_in_simulator:

            print("Running %s in %s" % (lems_file_name, run_in_simulator))

            traces, events = oc.simulate_network(lems_file_name,
                                                 run_in_simulator,
                                                 max_memory='4000M',
                                                 nogui=True,
                                                 load_saved_data=True,
                                                 reload_events=True,
                                                 plot=False,
                                                 verbose=False)

            print("Reloaded traces: %s" % traces.keys())
            #print("Reloaded events: %s"%events.keys())

            use_events_for_rates = False

            exc_rate = 0
            inh_rate = 0

            if use_events_for_rates:
                if (run_in_simulator == 'jNeuroML_NetPyNE'):
                    raise (
                        'Saving of spikes (and so calculation of rates) not yet supported in jNeuroML_NetPyNE'
                    )
                for ek in events.keys():
                    rate = 1000 * len(events[ek]) / float(duration)
                    print("Cell %s has rate %s Hz" % (ek, rate))
                    if 'popExc' in ek:
                        exc_rate += rate / num_exc
                    if 'popInh' in ek:
                        inh_rate += rate / num_inh

            else:
                tot_exc_rate = 0
                exc_cells = 0
                tot_inh_rate = 0
                inh_cells = 0
                tt = [t * 1000 for t in traces['t']]
                for tk in traces.keys():
                    if tk != 't':
                        rate = get_rate_from_trace(
                            tt, [v * 1000 for v in traces[tk]])
                        print("Cell %s has rate %s Hz" % (tk, rate))
                        if 'popExc' in tk:
                            tot_exc_rate += rate
                            exc_cells += 1
                        if 'popInh' in tk:
                            tot_inh_rate += rate
                            inh_cells += 1

                exc_rate = tot_exc_rate / exc_cells
                inh_rate = tot_inh_rate / inh_cells

            print("Run %s: Exc rate: %s Hz; Inh rate %s Hz" %
                  (reference, exc_rate, inh_rate))

            return exc_rate, inh_rate, traces

    else:
        lems_file_name = None

    return nml_doc, nml_file_name, lems_file_name
示例#22
0
def generate(reference = "DendConn",
             num_pyr = 4,
             num_bask = 0,
             scalex=1,
             scaley=1,
             scalez=1,
             connections=True,
             global_delay = 0,
             duration = 500,
             segments_to_plot_record = {'pop_pyr':[0],'pop_bask':[0]},
             format='xml'):


    nml_doc, network = oc.generate_network(reference)

    oc.include_opencortex_cell(nml_doc, 'acnet2/pyr_4_sym.cell.nml')
    oc.include_opencortex_cell(nml_doc, 'acnet2/bask.cell.nml')
    
    xDim = 500*scalex
    yDim = 50*scaley
    zDim = 500*scalez

    pop_pyr = oc.add_population_in_rectangular_region(network, 'pop_pyr',
                                                  'pyr_4_sym', num_pyr,
                                                  0,0,0, xDim,yDim,zDim)

    pop_bask = oc.add_population_in_rectangular_region(network, 'pop_bask',
                                                  'bask', num_bask,
                                                  0,yDim,0, xDim,yDim+yDim,zDim)
                   
    ampa_syn = oc.add_exp_two_syn(nml_doc, id="AMPA_syn", 
                             gbase="30e-9S", erev="0mV",
                             tau_rise="0.003s", tau_decay="0.0031s")


    pg = oc.add_pulse_generator(nml_doc,
                           id="pg0",
                           delay="10ms",
                           duration="300ms",
                           amplitude="0.7nA")          
                                
    total_conns = 0
    if connections:

        this_syn=ampa_syn.id
        proj = oc.add_targeted_projection(network,
                                        "Proj_pyr_pyr",
                                        pop_pyr,
                                        pop_pyr,
                                        targeting_mode='convergent',
                                        synapse_list=[this_syn],
                                        pre_segment_group = 'soma_group',
                                        post_segment_group = 'dendrite_group',
                                        number_conns_per_cell=1,
                                        delays_dict = {this_syn:global_delay})
        if proj:                           
            total_conns += len(proj[0].connection_wds)


            
            
    oc.add_targeted_inputs_to_population(network, "Stim0",
                                pop_pyr, pg.id, 
                                segment_group='soma_group',
                                number_per_cell = 1,
                                all_cells=False,
                                only_cells=[0])
        
    
        
    nml_file_name = '%s.net.%s'%(network.id,'nml.h5' if format == 'hdf5' else 'nml')
    target_dir = 'HDF5/' if format == 'hdf5' else './'
    
    oc.save_network(nml_doc, 
                    nml_file_name, 
                    validate=(format=='xml'),
                    format = format,
                    target_dir=target_dir)


    gen_plots_for_quantities = {}   #  Dict with displays vs lists of quantity paths
    gen_saves_for_quantities = {}   #  Dict with file names vs lists of quantity paths

    for pop in segments_to_plot_record.keys():
        pop_nml = network.get_by_id(pop)
        if pop_nml is not None and pop_nml.size>0:

            group = len(segments_to_plot_record[pop]) == 1
            if group:
                display = 'Display_%s_v'%(pop)
                file_ = 'Sim_%s.%s.v.dat'%(nml_doc.id,pop)
                gen_plots_for_quantities[display] = []
                gen_saves_for_quantities[file_] = []

            for i in range(int(pop_nml.size)):
                if not group:
                    display = 'Display_%s_%i_v'%(pop,i)
                    file_ = 'Sim_%s.%s.%i.v.dat'%(nml_doc.id,pop,i)
                    gen_plots_for_quantities[display] = []
                    gen_saves_for_quantities[file_] = []

                for seg in segments_to_plot_record[pop]:
                    quantity = '%s/%i/%s/%i/v'%(pop,i,pop_nml.component,seg)
                    gen_plots_for_quantities[display].append(quantity)
                    gen_saves_for_quantities[file_].append(quantity)

    lems_file_name = oc.generate_lems_simulation(nml_doc, 
                            network, 
                            target_dir+nml_file_name, 
                            duration =      duration, 
                            dt =            0.025,
                            gen_plots_for_all_v = False,
                            gen_plots_for_quantities = gen_plots_for_quantities,
                            gen_saves_for_all_v = False,
                            gen_saves_for_quantities = gen_saves_for_quantities,
                            target_dir=target_dir)
                                
    return nml_doc, nml_file_name, lems_file_name
示例#23
0
def generate(cell_id,
             duration,
             reference,
             format='hdf5',
             num_cells=10,
             simulator=None):

    #Insyn = int(Ensyn * 0.2)
    #bInsyn = int(bEnsyn * 0.2)

    cell_file = '../%s.cell.nml' % cell_id
    if '/' in cell_id:
        cell_id = cell_id.split('/')[-1]

    nml_doc, network = oc.generate_network(reference, temperature='35degC')

    oc.include_neuroml2_cell_and_channels(nml_doc, cell_file, cell_id)

    pop = oc.add_population_in_rectangular_region(network, 'L23_pop', cell_id,
                                                  num_cells, 0, 0, 0, 100, 100,
                                                  100)

    to_plot = {'Some_voltages': []}
    to_save = {'%s_%s_voltages.dat' % (reference, cell_id): []}

    interesting_seg_ids = [0, 200, 1000, 2000, 2500,
                           2949]  # [soma, .. some dends .. , axon]
    interesting_seg_ids = [0]  # [soma, .. some dends .. , axon]

    pg0 = oc.add_pulse_generator(nml_doc,
                                 id="pg0",
                                 delay="200ms",
                                 duration="600ms",
                                 amplitude="0.4nA")

    pg1 = oc.add_pulse_generator(nml_doc,
                                 id="pg1",
                                 delay="100ms",
                                 duration="400ms",
                                 amplitude="0.02nA")

    oc.add_targeted_inputs_to_population(network,
                                         "PG_fixed",
                                         pop,
                                         pg0.id,
                                         segment_group='soma_group',
                                         number_per_cell=1,
                                         all_cells=True)

    oc.add_targeted_inputs_to_population(
        network,
        "PG_variable",
        pop,
        'cond0',  # from ../../../ConductanceClamp.xml
        segment_group='soma_group',
        number_per_cell=1,
        all_cells=True,
        weights='random()')

    for i in range(num_cells):

        for seg_id in interesting_seg_ids:
            to_plot.values()[0].append('%s/%s/%s/%s/v' %
                                       (pop.id, i, pop.component, seg_id))
            to_save.values()[0].append('%s/%s/%s/%s/v' %
                                       (pop.id, i, pop.component, seg_id))

    nml_file_name = '%s.net.nml' % network.id + ('.h5'
                                                 if format == 'hdf5' else '')

    target_dir = './'

    oc.save_network(nml_doc,
                    nml_file_name,
                    validate=False,
                    use_subfolder=True,
                    target_dir=target_dir,
                    format=format)

    lems_file_name, lems_sim = oc.generate_lems_simulation(
        nml_doc,
        network,
        nml_file_name,
        duration,
        dt=0.025,
        target_dir=target_dir,
        include_extra_lems_files=['../../../ConductanceClamp.xml'],
        gen_plots_for_all_v=False,
        plot_all_segments=False,
        gen_plots_for_quantities=
        to_plot,  #  Dict with displays vs lists of quantity paths
        gen_saves_for_all_v=False,
        save_all_segments=False,
        gen_saves_for_quantities=
        to_save,  #  Dict with file names vs lists of quantity paths
        verbose=True)

    if simulator:

        print("Running %s for %sms in %s" %
              (lems_file_name, duration, simulator))

        traces, events = oc.simulate_network(lems_file_name,
                                             simulator,
                                             max_memory='4000M',
                                             nogui=True,
                                             load_saved_data=True,
                                             reload_events=True,
                                             plot=False,
                                             verbose=True,
                                             num_processors=min(num_cells, 16))