def generate(reference = "SimpleNet", scale=1, format='xml'): population_size = scale_pop_size(3,scale) nml_doc, network = oc.generate_network(reference) oc.add_cell_and_channels(nml_doc, 'izhikevich/RS.cell.nml','RS') pop = oc.add_population_in_rectangular_region(network, 'RS_pop', 'RS', population_size, 0,0,0, 100,100,100) 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)
def generate(reference="SimpleNet", scale=1, format='xml'): population_size = scale_pop_size(3, scale) nml_doc, network = oc.generate_network(reference) oc.add_cell_and_channels(nml_doc, 'izhikevich/RS.cell.nml', 'RS') pop = oc.add_population_in_rectangular_region(network, 'RS_pop', 'RS', population_size, 0, 0, 0, 100, 100, 100) 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)
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]) oc.add_inputs_to_population(network, "Stim1", pop0, pg1.id, only_cells=[1]) oc.add_inputs_to_population(network, "Stim2", pop0, pfs.id, all_cells=True) nml_file_name = '%s.net.nml'%network.id oc.save_network(nml_doc, nml_file_name, validate=True)
offset+=yDim ##### Projections oc.add_probabilistic_projection(network, "proj0", popIaf, popIzh, synAmpa1.id, 0.5) ##### Inputs oc.add_inputs_to_population(network, "Stim0", popIzh, pfsStrong.id, all_cells=True) oc.add_inputs_to_population(network, "Stim1", popIaf, pfs200.id, all_cells=True) oc.add_inputs_to_population(network, "Stim2", popIafRef, pfs200.id, all_cells=True) oc.add_inputs_to_population(network, "Stim3", popPyrS, pfs100.id, all_cells=True) oc.add_inputs_to_population(network, "Stim4",
def generate_lems(glif_dir, curr_pA, show_plot=True): os.chdir(glif_dir) with open('model_metadata.json', "r") as json_file: model_metadata = json.load(json_file) with open('neuron_config.json', "r") as json_file: neuron_config = json.load(json_file) with open('ephys_sweeps.json', "r") as json_file: ephys_sweeps = json.load(json_file) template_cell = '''<Lems> <%s %s/> </Lems> ''' type = '???' print(model_metadata['name']) if '(LIF)' in model_metadata['name']: type = 'glifCell' if '(LIF-ASC)' in model_metadata['name']: type = 'glifAscCell' if '(LIF-R)' in model_metadata['name']: type = 'glifRCell' if '(LIF-R-ASC)' in model_metadata['name']: type = 'glifRAscCell' if '(LIF-R-ASC-A)' in model_metadata['name']: type = 'glifRAscATCell' cell_id = 'GLIF_%s'%glif_dir attributes = "" attributes +=' id="%s"'%cell_id attributes +='\n C="%s F"'%neuron_config["C"] attributes +='\n leakReversal="%s V"'%neuron_config["El"] attributes +='\n reset="%s V"'%neuron_config["El"] attributes +='\n thresh="%s V"'%( float(neuron_config["th_inf"]) * float(neuron_config["coeffs"]["th_inf"])) attributes +='\n leakConductance="%s S"'%(1/float(neuron_config["R_input"])) if 'Asc' in type: attributes +='\n tau1="%s s"'%neuron_config["asc_tau_array"][0] attributes +='\n tau2="%s s"'%neuron_config["asc_tau_array"][1] attributes +='\n amp1="%s A"'% ( float(neuron_config["asc_amp_array"][0]) * float(neuron_config["coeffs"]["asc_amp_array"][0]) ) attributes +='\n amp2="%s A"'% ( float(neuron_config["asc_amp_array"][1]) * float(neuron_config["coeffs"]["asc_amp_array"][1]) ) if 'glifR' in type: attributes +='\n bs="%s per_s"'%neuron_config["threshold_dynamics_method"]["params"]["b_spike"] attributes +='\n deltaThresh="%s V"'%neuron_config["threshold_dynamics_method"]["params"]["a_spike"] attributes +='\n fv="%s"'%neuron_config["voltage_reset_method"]["params"]["a"] attributes +='\n deltaV="%s V"'%neuron_config["voltage_reset_method"]["params"]["b"] if 'glifRAscATCell' in type: attributes +='\n bv="%s per_s"'%neuron_config["threshold_dynamics_method"]["params"]["b_voltage"] attributes +='\n a="%s per_s"'%neuron_config["threshold_dynamics_method"]["params"]["a_voltage"] file_contents = template_cell%(type, attributes) print(file_contents) cell_file_name = '%s.xml'%(cell_id) cell_file = open(cell_file_name,'w') cell_file.write(file_contents) cell_file.close() import opencortex.build as oc nml_doc, network = oc.generate_network("Test_%s"%glif_dir) pop = oc.add_single_cell_population(network, 'pop_%s'%glif_dir, cell_id) pg = oc.add_pulse_generator(nml_doc, id="pg0", delay="100ms", duration="1000ms", amplitude="%s pA"%curr_pA) oc.add_inputs_to_population(network, "Stim0", pop, pg.id, all_cells=True) nml_file_name = '%s.net.nml'%network.id oc.save_network(nml_doc, nml_file_name, validate=True) thresh = 'thresh' if 'glifR' in type: thresh = 'threshTotal' lems_file_name = oc.generate_lems_simulation(nml_doc, network, nml_file_name, include_extra_lems_files = [cell_file_name,'../GLIFs.xml'], duration = 1200, dt = 0.01, gen_saves_for_quantities = {'thresh.dat':['pop_%s/0/GLIF_%s/%s'%(glif_dir,glif_dir,thresh)]}, gen_plots_for_quantities = {'Threshold':['pop_%s/0/GLIF_%s/%s'%(glif_dir,glif_dir,thresh)]}) results = pynml.run_lems_with_jneuroml(lems_file_name, nogui=True, load_saved_data=True) print("Ran simulation; results reloaded for: %s"%results.keys()) info = "Model %s; %spA stimulation"%(glif_dir,curr_pA) times = [results['t']] vs = [results['pop_%s/0/GLIF_%s/v'%(glif_dir,glif_dir)]] labels = ['LEMS - jNeuroML'] original_model_v = 'original.v.dat' if os.path.isfile(original_model_v): data, indices = pynml.reload_standard_dat_file(original_model_v) times.append(data['t']) vs.append(data[0]) labels.append('Allen SDK') pynml.generate_plot(times, vs, "Membrane potential; %s"%info, xaxis = "Time (s)", yaxis = "Voltage (V)", labels = labels, grid = True, show_plot_already=False, save_figure_to='Comparison_%ipA.png'%(curr_pA)) times = [results['t']] vs = [results['pop_%s/0/GLIF_%s/%s'%(glif_dir,glif_dir,thresh)]] labels = ['LEMS - jNeuroML'] original_model_th = 'original.thresh.dat' if os.path.isfile(original_model_th): data, indeces = pynml.reload_standard_dat_file(original_model_th) times.append(data['t']) vs.append(data[0]) labels.append('Allen SDK') pynml.generate_plot(times, vs, "Threshold; %s"%info, xaxis = "Time (s)", yaxis = "Voltage (V)", labels = labels, grid = True, show_plot_already=show_plot, save_figure_to='Comparison_Threshold_%ipA.png'%(curr_pA)) readme = ''' ## Model: %(id)s ### Original model %(name)s [Allen Cell Types DB electrophysiology page for specimen](http://celltypes.brain-map.org/mouse/experiment/electrophysiology/%(spec)s) [Neuron configuration](neuron_config.json); [model metadata](model_metadata.json); [electrophysiology summary](ephys_sweeps.json) #### Original traces: **Membrane potential** Current injection of %(curr)s pA ![Original](MembranePotential_%(curr)spA.png) **Threshold** ![Threshold](Threshold_%(curr)spA.png) ### Conversion to NeuroML 2 LEMS version of this model: [GLIF_%(id)s.xml](GLIF_%(id)s.xml) [Definitions of LEMS Component Types](../GLIFs.xml) for GLIFs. This model can be run locally by installing [jNeuroML](https://github.com/NeuroML/jNeuroML) and running: jnml LEMS_Test_%(id)s.xml #### Comparison: **Membrane potential** Current injection of %(curr)s pA ![Comparison](Comparison_%(curr)spA.png) **Threshold** ![Comparison](Comparison_Threshold_%(curr)spA.png)''' readme_file = open('README.md','w') curr_str = str(curr_pA) # @type curr_str str if curr_str.endswith('.0'): curr_str = curr_str[:-2] readme_file.write(readme%{"id":glif_dir,"name":model_metadata['name'],"spec":model_metadata["specimen_id"],"curr":curr_str}) readme_file.close() os.chdir('..') return model_metadata, neuron_config, ephys_sweeps
popInh, popExc, synGaba1.id, 0.7) oc.add_probabilistic_projection(network, "proj4", popInh, popInh, synGaba1.id, 0.5) ''' oc.add_probabilistic_projection(network, "proj5", popExc, popBBP, synAmpa1.id, 1)''' ##### Inputs oc.add_inputs_to_population(network, "Stim0", popExc, pfs1.id, all_cells=True) ##### Save NeuroML and LEMS Simulation files 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 = 1000, dt = 0.025)
def generate(reference = "Balanced", num_bbp =1, scalePops = 1, scalex=1, scaley=1, scalez=1, connections=True, duration = 1000, global_delay = 0, format='xml'): num_exc = scale_pop_size(80,scalePops) num_inh = scale_pop_size(40,scalePops) nml_doc, network = oc.generate_network(reference) oc.add_cell_and_channels(nml_doc, 'AllenInstituteCellTypesDB_HH/HH_464198958.cell.nml','HH_464198958') oc.add_cell_and_channels(nml_doc, 'AllenInstituteCellTypesDB_HH/HH_471141261.cell.nml','HH_471141261') if num_bbp>0: oc.add_cell_and_channels(nml_doc, 'BlueBrainProject_NMC/cADpyr229_L23_PC_5ecbf9b163_0_0.cell.nml', 'cADpyr229_L23_PC_5ecbf9b163_0_0') 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="150 Hz", synapse_id=synAmpa1.id) ##### Populations popExc = oc.add_population_in_rectangular_region(network, 'popExc', 'HH_464198958', num_exc, xs,ys,zs, xDim,yDim,zDim) popInh = oc.add_population_in_rectangular_region(network, 'popInh', 'HH_471141261', num_inh, xs,ys,zs, xDim,yDim,zDim) if num_bbp == 1: popBBP = oc.add_single_cell_population(network, 'popBBP', 'cADpyr229_L23_PC_5ecbf9b163_0_0', z=200) 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) ##### Projections total_conns = 0 if connections: proj = oc.add_probabilistic_projection(network, "proj0", popExc, popExc, synAmpa1.id, 0.3, delay = global_delay) total_conns += len(proj.connection_wds) proj = oc.add_probabilistic_projection(network, "proj1", popExc, popInh, synAmpa1.id, 0.5, 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': lems_file_name = oc.generate_lems_simulation(nml_doc, network, nml_file_name, duration = duration, dt = 0.025) else: lems_file_name = None return nml_doc, nml_file_name, lems_file_name
def generate(reference = "L23TraubDemo", num_rs =2, num_bask =2, scalex=1, scaley=1, scalez=1, connections=True, poisson_inputs=True, offset_curents=False, 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.add_cell_and_channels(nml_doc, 'Thalamocortical/L23PyrRS.cell.nml','L23PyrRS') oc.add_cell_and_channels(nml_doc, 'Thalamocortical/SupBasket.cell.nml','SupBasket') 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) if offset_curents: pg0 = oc.add_pulse_generator(nml_doc, id="pg0", delay="0ms", duration="%sms"%duration, amplitude="0.5nA") oc.add_inputs_to_population(network, "Stim0", pop_rs, pg0.id, all_cells=True) oc.add_inputs_to_population(network, "Stim0", pop_bask, pg0.id, all_cells=True) total_conns = 0 if connections: proj = oc.add_probabilistic_projection(network, "proj0", pop_rs, pop_bask, syn1.id, 0.3, weight=0.05, delay=global_delay) if proj: total_conns += len(proj.connection_wds) if num_rs != 2 or num_bask!=2: 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
pgHH = oc.add_pulse_generator(nml_doc, id="pgHH", delay="100ms", duration="300ms", amplitude="0.7nA") ''' pgBBP = oc.add_pulse_generator(nml_doc, id="pgBBP", delay="100ms", duration="300ms", amplitude="0.7nA")''' oc.add_inputs_to_population(network, "Stim0", popIzh, pgIzh.id, all_cells=True) oc.add_inputs_to_population(network, "Stim1", popHH, pgHH.id, all_cells=True) ''' oc.add_inputs_to_population(network, "Stim2", popBBP, pgBBP.id, all_cells=True)'''
def generate(reference = "ACNet", num_pyr = 48, num_bask = 12, scalex=1, scaley=1, scalez=1, connections=True, global_delay = 0, duration = 300, segments_to_plot_record = {'pop_pyr':[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.add_cell_and_channels(nml_doc, 'acnet2/bask.cell.nml','bask') 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") ampa_syn_inh = oc.add_exp_two_syn(nml_doc, id="AMPA_syn_inh", gbase="0.15e-9S", erev="0mV", tau_rise="0.003s", tau_decay="0.0031s") gaba_syn = oc.add_exp_two_syn(nml_doc, id="GABA_syn", gbase="0.6e-9S", erev="-0.080V", tau_rise="0.005s", tau_decay="0.012s") gaba_syn_inh = oc.add_exp_two_syn(nml_doc, id="GABA_syn_inh", gbase="0S", erev="-0.080V", tau_rise="0.003s", tau_decay="0.008s") 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_pyr, pfs.id, all_cells=True) total_conns = 0 if connections: this_syn=ampa_syn.id proj = oc.add_chem_projection0(nml_doc, 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=7, delays_dict = {this_syn:global_delay}) if proj: total_conns += len(proj[0].connection_wds) this_syn=ampa_syn_inh.id proj = oc.add_chem_projection0(nml_doc, network, "Proj_pyr_bask", pop_pyr, pop_bask, targeting_mode='convergent', synapse_list=[this_syn], pre_segment_group = 'soma_group', post_segment_group = 'all', number_conns_per_cell=21, delays_dict = {this_syn:global_delay}) if proj: total_conns += len(proj[0].connection_wds) this_syn=gaba_syn.id proj = oc.add_chem_projection0(nml_doc, network, "Proj_bask_pyr", pop_bask, pop_pyr, targeting_mode='convergent', synapse_list=[this_syn], pre_segment_group = 'soma_group', post_segment_group = 'all', number_conns_per_cell=21, delays_dict = {this_syn:global_delay}) if proj: total_conns += len(proj[0].connection_wds) this_syn=gaba_syn_inh.id proj = oc.add_chem_projection0(nml_doc, network, "Proj_bask_bask", pop_bask, pop_bask, targeting_mode='convergent', synapse_list=[this_syn], pre_segment_group = 'soma_group', post_segment_group = 'all', number_conns_per_cell=5, delays_dict = {this_syn:global_delay}) if proj: total_conns += len(proj[0].connection_wds) if num_pyr != 48 or num_bask!=12: new_reference = '%s_%scells_%sconns'%(nml_doc.id,num_pyr+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: 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
##### Projections oc.add_probabilistic_projection(network, "proj0", pop_iaf, pop_rs, synAmpa1.id, 0.5) ##### Inputs oc.add_inputs_to_population(network, "Stim0", pop_iaf, pg0.id, only_cells=[i for i in [0,2] if i<pop_iaf.size]) oc.add_inputs_to_population(network, "Stim1", pop_iaf, pg1.id, only_cells=[i for i in [3,4] if i<pop_iaf.size]) 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 = 500,
def generate_lems(glif_dir, curr_pA, show_plot=True): os.chdir(glif_dir) with open('model_metadata.json', "r") as json_file: model_metadata = json.load(json_file) with open('neuron_config.json', "r") as json_file: neuron_config = json.load(json_file) with open('ephys_sweeps.json', "r") as json_file: ephys_sweeps = json.load(json_file) template_cell = '''<Lems> <%s %s/> </Lems> ''' type = '???' print(model_metadata['name']) if '(LIF)' in model_metadata['name']: type = 'glifCell' if '(LIF-ASC)' in model_metadata['name']: type = 'glifAscCell' if '(LIF-R)' in model_metadata['name']: type = 'glifRCell' if '(LIF-R-ASC)' in model_metadata['name']: type = 'glifRAscCell' if '(LIF-R-ASC-A)' in model_metadata['name']: type = 'glifRAscATCell' cell_id = 'GLIF_%s' % glif_dir attributes = "" attributes += ' id="%s"' % cell_id attributes += '\n C="%s F"' % neuron_config["C"] attributes += '\n leakReversal="%s V"' % neuron_config["El"] attributes += '\n reset="%s V"' % neuron_config["El"] attributes += '\n thresh="%s V"' % (float( neuron_config["th_inf"]) * float(neuron_config["coeffs"]["th_inf"])) attributes += '\n leakConductance="%s S"' % ( 1 / float(neuron_config["R_input"])) if 'Asc' in type: attributes += '\n tau1="%s s"' % neuron_config[ "asc_tau_array"][0] attributes += '\n tau2="%s s"' % neuron_config[ "asc_tau_array"][1] attributes += '\n amp1="%s A"' % ( float(neuron_config["asc_amp_array"][0]) * float(neuron_config["coeffs"]["asc_amp_array"][0])) attributes += '\n amp2="%s A"' % ( float(neuron_config["asc_amp_array"][1]) * float(neuron_config["coeffs"]["asc_amp_array"][1])) if 'glifR' in type: attributes += '\n bs="%s per_s"' % neuron_config[ "threshold_dynamics_method"]["params"]["b_spike"] attributes += '\n deltaThresh="%s V"' % neuron_config[ "threshold_dynamics_method"]["params"]["a_spike"] attributes += '\n fv="%s"' % neuron_config[ "voltage_reset_method"]["params"]["a"] attributes += '\n deltaV="%s V"' % neuron_config[ "voltage_reset_method"]["params"]["b"] if 'glifRAscATCell' in type: attributes += '\n bv="%s per_s"' % neuron_config[ "threshold_dynamics_method"]["params"]["b_voltage"] attributes += '\n a="%s per_s"' % neuron_config[ "threshold_dynamics_method"]["params"]["a_voltage"] file_contents = template_cell % (type, attributes) print(file_contents) cell_file_name = '%s.xml' % (cell_id) cell_file = open(cell_file_name, 'w') cell_file.write(file_contents) cell_file.close() import opencortex.build as oc nml_doc, network = oc.generate_network("Test_%s" % glif_dir) pop = oc.add_single_cell_population(network, 'pop_%s' % glif_dir, cell_id) pg = oc.add_pulse_generator(nml_doc, id="pg0", delay="100ms", duration="1000ms", amplitude="%s pA" % curr_pA) oc.add_inputs_to_population(network, "Stim0", pop, pg.id, all_cells=True) nml_file_name = '%s.net.nml' % network.id oc.save_network(nml_doc, nml_file_name, validate=True) thresh = 'thresh' if 'glifR' in type: thresh = 'threshTotal' lems_file_name = oc.generate_lems_simulation( nml_doc, network, nml_file_name, include_extra_lems_files=[cell_file_name, '../GLIFs.xml'], duration=1200, dt=0.01, gen_saves_for_quantities={ 'thresh.dat': ['pop_%s/0/GLIF_%s/%s' % (glif_dir, glif_dir, thresh)] }, gen_plots_for_quantities={ 'Threshold': ['pop_%s/0/GLIF_%s/%s' % (glif_dir, glif_dir, thresh)] }) results = pynml.run_lems_with_jneuroml(lems_file_name, nogui=True, load_saved_data=True) print("Ran simulation; results reloaded for: %s" % results.keys()) info = "Model %s; %spA stimulation" % (glif_dir, curr_pA) times = [results['t']] vs = [results['pop_%s/0/GLIF_%s/v' % (glif_dir, glif_dir)]] labels = ['LEMS - jNeuroML'] original_model_v = 'original.v.dat' if os.path.isfile(original_model_v): data, indices = pynml.reload_standard_dat_file(original_model_v) times.append(data['t']) vs.append(data[0]) labels.append('Allen SDK') pynml.generate_plot(times, vs, "Membrane potential; %s" % info, xaxis="Time (s)", yaxis="Voltage (V)", labels=labels, grid=True, show_plot_already=False, save_figure_to='Comparison_%ipA.png' % (curr_pA)) times = [results['t']] vs = [results['pop_%s/0/GLIF_%s/%s' % (glif_dir, glif_dir, thresh)]] labels = ['LEMS - jNeuroML'] original_model_th = 'original.thresh.dat' if os.path.isfile(original_model_th): data, indeces = pynml.reload_standard_dat_file(original_model_th) times.append(data['t']) vs.append(data[0]) labels.append('Allen SDK') pynml.generate_plot(times, vs, "Threshold; %s" % info, xaxis="Time (s)", yaxis="Voltage (V)", labels=labels, grid=True, show_plot_already=show_plot, save_figure_to='Comparison_Threshold_%ipA.png' % (curr_pA)) readme = ''' ## Model: %(id)s ### Original model %(name)s [Allen Cell Types DB electrophysiology page for specimen](http://celltypes.brain-map.org/mouse/experiment/electrophysiology/%(spec)s) [Neuron configuration](neuron_config.json); [model metadata](model_metadata.json); [electrophysiology summary](ephys_sweeps.json) #### Original traces: **Membrane potential** Current injection of %(curr)s pA ![Original](MembranePotential_%(curr)spA.png) **Threshold** ![Threshold](Threshold_%(curr)spA.png) ### Conversion to NeuroML 2 LEMS version of this model: [GLIF_%(id)s.xml](GLIF_%(id)s.xml) [Definitions of LEMS Component Types](../GLIFs.xml) for GLIFs. This model can be run locally by installing [jNeuroML](https://github.com/NeuroML/jNeuroML) and running: jnml LEMS_Test_%(id)s.xml #### Comparison: **Membrane potential** Current injection of %(curr)s pA ![Comparison](Comparison_%(curr)spA.png) **Threshold** ![Comparison](Comparison_Threshold_%(curr)spA.png)''' readme_file = open('README.md', 'w') curr_str = str(curr_pA) # @type curr_str str if curr_str.endswith('.0'): curr_str = curr_str[:-2] readme_file.write( readme % { "id": glif_dir, "name": model_metadata['name'], "spec": model_metadata["specimen_id"], "curr": curr_str }) readme_file.close() os.chdir('..') return model_metadata, neuron_config, ephys_sweeps