def generate_and_run(simulation, <EOL>simulator, <EOL>network=None, <EOL>return_results=False, <EOL>base_dir=None,<EOL>target_dir=None,<EOL>num_processors=<NUM_LIT:1>):
if network == None:<EOL><INDENT>network = load_network_json(simulation.network)<EOL><DEDENT>print_v("<STR_LIT>" % (network.id, simulator))<EOL>if simulator == '<STR_LIT>':<EOL><INDENT>_generate_neuron_files_from_neuroml(network, dir_for_mod_files=target_dir)<EOL>from neuromllite.NeuronHandler import NeuronHandler<EOL>nrn_handler = NeuronHandler()<EOL>for c in network.cells:<EOL><INDENT>if c.neuroml2_source_file:<EOL><INDENT>src_dir = os.path.dirname(os.path.abspath(c.neuroml2_source_file))<EOL>nrn_handler.executeHoc('<STR_LIT>' % (src_dir, c.id))<EOL><DEDENT><DEDENT>generate_network(network, nrn_handler, generate_network, base_dir)<EOL>if return_results:<EOL><INDENT>raise NotImplementedError("<STR_LIT>")<EOL><DEDENT><DEDENT>elif simulator.lower() == '<STR_LIT>': <EOL><INDENT>from neuromllite.SonataHandler import SonataHandler<EOL>sonata_handler = SonataHandler()<EOL>generate_network(network, sonata_handler, always_include_props=True, base_dir=base_dir)<EOL>print_v("<STR_LIT>")<EOL><DEDENT>elif simulator.lower().startswith('<STR_LIT>'): <EOL><INDENT>from neuromllite.GraphVizHandler import GraphVizHandler, engines<EOL>try:<EOL><INDENT>if simulator[-<NUM_LIT:1>].isalpha():<EOL><INDENT>print(simulator)<EOL>print(simulator[<NUM_LIT:5>:])<EOL>print(simulator[<NUM_LIT:5>:-<NUM_LIT:1>])<EOL>engine = engines[simulator[-<NUM_LIT:1>]]<EOL>level = int(simulator[<NUM_LIT:5>:-<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>engine = '<STR_LIT>'<EOL>level = int(simulator[<NUM_LIT:5>:])<EOL><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>print(e)<EOL>print_v("<STR_LIT>"%simulator)<EOL>print_v("<STR_LIT>")<EOL>return<EOL><DEDENT>handler = GraphVizHandler(level, engine=engine, nl_network=network)<EOL>generate_network(network, handler, always_include_props=True, base_dir=base_dir)<EOL>print_v("<STR_LIT>")<EOL><DEDENT>elif simulator.lower().startswith('<STR_LIT>'): <EOL><INDENT>from neuromllite.MatrixHandler import MatrixHandler<EOL>try:<EOL><INDENT>level = int(simulator[<NUM_LIT:6>:])<EOL><DEDENT>except:<EOL><INDENT>print_v("<STR_LIT>"%simulator)<EOL>print_v("<STR_LIT>")<EOL>return<EOL><DEDENT>handler = MatrixHandler(level, nl_network=network)<EOL>generate_network(network, handler, always_include_props=True, base_dir=base_dir)<EOL>print_v("<STR_LIT>")<EOL><DEDENT>elif simulator.startswith('<STR_LIT>'):<EOL><INDENT>simulator_name = simulator.split('<STR_LIT:_>')[<NUM_LIT:1>].lower()<EOL>from neuromllite.PyNNHandler import PyNNHandler<EOL>pynn_handler = PyNNHandler(simulator_name, simulation.dt, network.id)<EOL>syn_cell_params = {}<EOL>for proj in network.projections:<EOL><INDENT>synapse = network.get_child(proj.synapse, '<STR_LIT>')<EOL>post_pop = network.get_child(proj.postsynaptic, '<STR_LIT>')<EOL>if not post_pop.component in syn_cell_params:<EOL><INDENT>syn_cell_params[post_pop.component] = {}<EOL><DEDENT>for p in synapse.parameters:<EOL><INDENT>post = '<STR_LIT>'<EOL>if synapse.pynn_receptor_type == "<STR_LIT>":<EOL><INDENT>post = '<STR_LIT>'<EOL><DEDENT>elif synapse.pynn_receptor_type == "<STR_LIT>":<EOL><INDENT>post = '<STR_LIT>'<EOL><DEDENT>syn_cell_params[post_pop.component]['<STR_LIT>' % (p, post)] = synapse.parameters[p]<EOL><DEDENT><DEDENT>cells = {}<EOL>for c in network.cells:<EOL><INDENT>if c.pynn_cell:<EOL><INDENT>cell_params = {}<EOL>if c.parameters: <EOL><INDENT>for p in c.parameters:<EOL><INDENT>cell_params[p] = evaluate(c.parameters[p], network.parameters)<EOL><DEDENT><DEDENT>dont_set_here = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>for d in dont_set_here:<EOL><INDENT>if d in c.parameters:<EOL><INDENT>raise Exception('<STR_LIT>'+<EOL>'<STR_LIT>' % d)<EOL><DEDENT><DEDENT>if c.id in syn_cell_params:<EOL><INDENT>cell_params.update(syn_cell_params[c.id])<EOL><DEDENT>print_v("<STR_LIT>" % cell_params)<EOL>exec('<STR_LIT>' % (c.id, c.pynn_cell))<EOL>if c.pynn_cell != '<STR_LIT>':<EOL><INDENT>exec("<STR_LIT>" % (c.id, c.id))<EOL><DEDENT><DEDENT><DEDENT>pynn_handler.set_cells(cells)<EOL>receptor_types = {}<EOL>for s in network.synapses:<EOL><INDENT>if s.pynn_receptor_type:<EOL><INDENT>receptor_types[s.id] = s.pynn_receptor_type<EOL><DEDENT><DEDENT>pynn_handler.set_receptor_types(receptor_types)<EOL>for input_source in network.input_sources:<EOL><INDENT>if input_source.pynn_input:<EOL><INDENT>pynn_handler.add_input_source(input_source)<EOL><DEDENT><DEDENT>generate_network(network, pynn_handler, always_include_props=True, base_dir=base_dir)<EOL>for pid in pynn_handler.populations:<EOL><INDENT>pop = pynn_handler.populations[pid]<EOL>if '<STR_LIT:all>' in simulation.recordTraces or pop.label in simulation.recordTraces:<EOL><INDENT>if pop.can_record('<STR_LIT:v>'):<EOL><INDENT>pop.record('<STR_LIT:v>')<EOL><DEDENT><DEDENT><DEDENT>pynn_handler.sim.run(simulation.duration)<EOL>pynn_handler.sim.end()<EOL>traces = {}<EOL>events = {}<EOL>if not '<STR_LIT>' in simulator:<EOL><INDENT>from neo.io import PyNNTextIO<EOL>for pid in pynn_handler.populations:<EOL><INDENT>pop = pynn_handler.populations[pid]<EOL>if '<STR_LIT:all>' in simulation.recordTraces or pop.label in simulation.recordTraces:<EOL><INDENT>filename = "<STR_LIT>" % (simulation.id, pop.label)<EOL>all_columns = []<EOL>print_v("<STR_LIT>" % (pop.label, filename))<EOL>for i in range(len(pop)):<EOL><INDENT>if pop.can_record('<STR_LIT:v>'):<EOL><INDENT>ref = '<STR_LIT>'%(pop.label,i)<EOL>traces[ref] = []<EOL>data = pop.get_data('<STR_LIT:v>', gather=False)<EOL>for segment in data.segments:<EOL><INDENT>vm = segment.analogsignals[<NUM_LIT:0>].transpose()[i]<EOL>if len(all_columns) == <NUM_LIT:0>:<EOL><INDENT>tt = np.array([t * simulation.dt / <NUM_LIT> for t in range(len(vm))])<EOL>all_columns.append(tt)<EOL><DEDENT>vm_si = [float(v / <NUM_LIT>) for v in vm]<EOL>traces[ref] = vm_si<EOL>all_columns.append(vm_si)<EOL><DEDENT>times_vm = np.array(all_columns).transpose()<EOL><DEDENT><DEDENT>np.savetxt(filename, times_vm, delimiter='<STR_LIT:\t>', fmt='<STR_LIT:%s>')<EOL><DEDENT><DEDENT><DEDENT>if return_results:<EOL><INDENT>_print_result_info(traces, events)<EOL>return traces, events<EOL><DEDENT><DEDENT>elif simulator == '<STR_LIT>':<EOL><INDENT>if target_dir==None:<EOL><INDENT>target_dir='<STR_LIT>'<EOL><DEDENT>_generate_neuron_files_from_neuroml(network, dir_for_mod_files=target_dir)<EOL>from netpyne import specs<EOL>from netpyne import sim<EOL>from netpyne.conversion.neuromlFormat import NetPyNEBuilder<EOL>import pprint; pp = pprint.PrettyPrinter(depth=<NUM_LIT:6>)<EOL>netParams = specs.NetParams()<EOL>simConfig = specs.SimConfig()<EOL>netpyne_handler = NetPyNEBuilder(netParams, simConfig=simConfig, verbose=True)<EOL>generate_network(network, netpyne_handler, base_dir=base_dir)<EOL>netpyne_handler.finalise()<EOL>simConfig = specs.SimConfig() <EOL>simConfig.tstop = simulation.duration<EOL>simConfig.duration = simulation.duration<EOL>simConfig.dt = simulation.dt<EOL>simConfig.seed = simulation.seed<EOL>simConfig.recordStep = simulation.dt<EOL>simConfig.recordCells = ['<STR_LIT:all>'] <EOL>simConfig.recordTraces = {}<EOL>for pop in list(netpyne_handler.popParams.values()):<EOL><INDENT>if '<STR_LIT:all>' in simulation.recordTraces or pop.id in simulation.recordTraces:<EOL><INDENT>for i in pop['<STR_LIT>']:<EOL><INDENT>id = pop['<STR_LIT>']<EOL>index = i['<STR_LIT>']<EOL>simConfig.recordTraces['<STR_LIT>' % (id, index)] = {'<STR_LIT>':'<STR_LIT>', '<STR_LIT>':<NUM_LIT:0.5>, '<STR_LIT>':'<STR_LIT:v>', '<STR_LIT>':{'<STR_LIT>':id, '<STR_LIT>':index}}<EOL><DEDENT><DEDENT><DEDENT>simConfig.saveDat = True<EOL>print_v("<STR_LIT>")<EOL>pp.pprint(netParams.todict())<EOL>sim.initialize(netParams, simConfig)  <EOL>sim.net.createPops()  <EOL>cells = sim.net.createCells()                 <EOL>for proj_id in list(netpyne_handler.projection_infos.keys()):<EOL><INDENT>projName, prePop, postPop, synapse, ptype = netpyne_handler.projection_infos[proj_id]<EOL>print_v("<STR_LIT>" % (projName, ptype, prePop, postPop, synapse))<EOL>preComp = netpyne_handler.pop_ids_vs_components[prePop]<EOL>for conn in netpyne_handler.connections[projName]:<EOL><INDENT>pre_id, pre_seg, pre_fract, post_id, post_seg, post_fract, delay, weight = conn<EOL>connParam = {'<STR_LIT>':delay, '<STR_LIT>':weight, '<STR_LIT>':<NUM_LIT:1>, '<STR_LIT>':post_seg, '<STR_LIT>':post_fract}<EOL>if ptype == '<STR_LIT>':<EOL><INDENT>if weight != <NUM_LIT:1>:<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT>connParam = {'<STR_LIT>': <NUM_LIT:1>, <EOL>'<STR_LIT>': post_seg, <EOL>'<STR_LIT>': post_fract, <EOL>'<STR_LIT>': True, <EOL>'<STR_LIT>': weight}<EOL><DEDENT>else:<EOL><INDENT>connParam = {'<STR_LIT>': delay,<EOL>'<STR_LIT>': weight,<EOL>'<STR_LIT>': <NUM_LIT:1>, <EOL>'<STR_LIT>': post_seg, <EOL>'<STR_LIT>': post_fract} <EOL><DEDENT>connParam['<STR_LIT>'] = synapse<EOL>if post_id in sim.net.gid2lid:  <EOL><INDENT>sim.net._addCellConn(connParam, pre_id, post_id)<EOL><DEDENT><DEDENT><DEDENT>stims = sim.net.addStims()                    <EOL>simData = sim.setupRecording()              <EOL>sim.runSim()                      <EOL>sim.gatherData()                  <EOL>sim.saveData()                    <EOL>if return_results:<EOL><INDENT>raise NotImplementedError("<STR_LIT>")<EOL><DEDENT><DEDENT>elif simulator == '<STR_LIT>' or  simulator == '<STR_LIT>' or simulator == '<STR_LIT>':<EOL><INDENT>from pyneuroml.lems import generate_lems_file_for_neuroml<EOL>from pyneuroml import pynml<EOL>lems_file_name = '<STR_LIT>' % simulation.id<EOL>nml_file_name, nml_doc = generate_neuroml2_from_network(network, base_dir=base_dir, target_dir=target_dir)<EOL>included_files = ['<STR_LIT>']<EOL>for c in network.cells:        <EOL><INDENT>if c.lems_source_file:<EOL><INDENT>included_files.append(c.lems_source_file)<EOL><DEDENT><DEDENT>'''<STR_LIT>'''<EOL>if network.synapses:<EOL><INDENT>for s in network.synapses:  <EOL><INDENT>if s.lems_source_file:<EOL><INDENT>included_files.append(s.lems_source_file)<EOL><DEDENT><DEDENT><DEDENT>print_v("<STR_LIT>" % simulator)<EOL>pops_plot_save = []<EOL>pops_spike_save = []<EOL>gen_plots_for_quantities = {}<EOL>gen_saves_for_quantities = {}<EOL>for p in network.populations:<EOL><INDENT>if simulation.recordTraces and ('<STR_LIT:all>' in simulation.recordTraces or p.id in simulation.recordTraces):<EOL><INDENT>pops_plot_save.append(p.id)<EOL><DEDENT>if simulation.recordSpikes and ('<STR_LIT:all>' in simulation.recordSpikes or p.id in simulation.recordSpikes):<EOL><INDENT>pops_spike_save.append(p.id)<EOL><DEDENT>if simulation.recordRates and ('<STR_LIT:all>' in simulation.recordRates or p.id in simulation.recordRates):<EOL><INDENT>size = evaluate(p.size, network.parameters)<EOL>for i in range(size):<EOL><INDENT>quantity = '<STR_LIT>' % (p.id, i, p.component)<EOL>gen_plots_for_quantities['<STR_LIT>' % (p.id, i)] = [quantity]<EOL>gen_saves_for_quantities['<STR_LIT>' % (p.id, i)] = [quantity]<EOL><DEDENT><DEDENT>if simulation.recordVariables:<EOL><INDENT>for var in simulation.recordVariables:<EOL><INDENT>to_rec = simulation.recordVariables[var]<EOL>if ('<STR_LIT:all>' in to_rec or p.id in to_rec):<EOL><INDENT>size = evaluate(p.size, network.parameters)<EOL>for i in range(size):<EOL><INDENT>quantity = '<STR_LIT>' % (p.id, i, p.component,var)<EOL>gen_plots_for_quantities['<STR_LIT>' % (p.id, i, var)] = [quantity]<EOL>gen_saves_for_quantities['<STR_LIT>' % (p.id, i, var)] = [quantity]<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>generate_lems_file_for_neuroml(simulation.id, <EOL>nml_file_name, <EOL>network.id, <EOL>simulation.duration, <EOL>simulation.dt, <EOL>lems_file_name,<EOL>target_dir=target_dir if target_dir else '<STR_LIT:.>',<EOL>nml_doc=nml_doc, <EOL>include_extra_files=included_files,<EOL>gen_plots_for_all_v=False,<EOL>plot_all_segments=False,<EOL>gen_plots_for_quantities=gen_plots_for_quantities, <EOL>gen_plots_for_only_populations=pops_plot_save, <EOL>gen_saves_for_all_v=False,<EOL>save_all_segments=False,<EOL>gen_saves_for_only_populations=pops_plot_save, <EOL>gen_saves_for_quantities=gen_saves_for_quantities, <EOL>gen_spike_saves_for_all_somas=False,<EOL>gen_spike_saves_for_only_populations=pops_spike_save, <EOL>gen_spike_saves_for_cells={}, <EOL>spike_time_format='<STR_LIT>',<EOL>copy_neuroml=True,<EOL>lems_file_generate_seed=<NUM_LIT>,<EOL>report_file_name='<STR_LIT>' % simulation.id,<EOL>simulation_seed=simulation.seed if simulation.seed else <NUM_LIT>,<EOL>verbose=True)<EOL>lems_file_name = _locate_file(lems_file_name, target_dir)<EOL>if simulator == '<STR_LIT>':<EOL><INDENT>results = pynml.run_lems_with_jneuroml(lems_file_name, <EOL>nogui=True, <EOL>load_saved_data=return_results, <EOL>reload_events=return_results)<EOL><DEDENT>elif simulator == '<STR_LIT>':<EOL><INDENT>results = pynml.run_lems_with_jneuroml_neuron(lems_file_name, <EOL>nogui=True, <EOL>load_saved_data=return_results, <EOL>reload_events=return_results)<EOL><DEDENT>elif simulator == '<STR_LIT>':<EOL><INDENT>results = pynml.run_lems_with_jneuroml_netpyne(lems_file_name, <EOL>nogui=True, <EOL>verbose=True, <EOL>load_saved_data=return_results, <EOL>reload_events=return_results,<EOL>num_processors=num_processors)<EOL><DEDENT>print_v("<STR_LIT>" % (lems_file_name, simulator, return_results))<EOL>if return_results:<EOL><INDENT>traces, events = results<EOL>_print_result_info(traces, events)<EOL>return results<EOL><DEDENT><DEDENT>
Generates the network in the specified simulator and runs, if appropriate