def generate_neuroml2_from_network(nl_model, <EOL>nml_file_name=None, <EOL>print_summary=True, <EOL>seed=<NUM_LIT>, <EOL>format='<STR_LIT>', <EOL>base_dir=None,<EOL>copy_included_elements=False,<EOL>target_dir=None,<EOL>validate=False):
print_v("<STR_LIT>" % (nl_model.id, '<STR_LIT>' <EOL>% (base_dir, target_dir) if base_dir or target_dir else '<STR_LIT>'))<EOL>import neuroml<EOL>from neuroml.hdf5.NetworkBuilder import NetworkBuilder<EOL>neuroml_handler = NetworkBuilder()<EOL>generate_network(nl_model, neuroml_handler, seed=seed, base_dir=base_dir)<EOL>nml_doc = neuroml_handler.get_nml_doc()<EOL>for i in nl_model.input_sources:<EOL><INDENT>if nml_doc.get_by_id(i.id) == None:<EOL><INDENT>if i.neuroml2_source_file:<EOL><INDENT>incl = neuroml.IncludeType(_locate_file(i.neuroml2_source_file, base_dir))<EOL>if not incl in nml_doc.includes:<EOL><INDENT>nml_doc.includes.append(incl)<EOL><DEDENT><DEDENT>if i.neuroml2_input: <EOL><INDENT>input_params = i.parameters if i.parameters else {}<EOL>if i.neuroml2_input.lower() == '<STR_LIT>':<EOL><INDENT>input = neuroml.PulseGenerator(id=i.id)<EOL>nml_doc.pulse_generators.append(input)<EOL><DEDENT>elif i.neuroml2_input.lower() == '<STR_LIT>':<EOL><INDENT>input = neuroml.PulseGeneratorDL(id=i.id)<EOL>nml_doc.pulse_generator_dls.append(input)<EOL><DEDENT>elif i.neuroml2_input.lower() == '<STR_LIT>':<EOL><INDENT>input = neuroml.PoissonFiringSynapse(id=i.id)<EOL>nml_doc.poisson_firing_synapses.append(input)<EOL><DEDENT>for p in input_params:<EOL><INDENT>exec('<STR_LIT>' % (p, evaluate(input_params[p], nl_model.parameters)))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>for c in nl_model.cells:<EOL><INDENT>if c.neuroml2_source_file:<EOL><INDENT>incl = neuroml.IncludeType(_locate_file(c.neuroml2_source_file, base_dir))<EOL>found_cell = False<EOL>for cell in nml_doc.cells:<EOL><INDENT>if cell.id == c.id:<EOL><INDENT>nml_doc.cells.remove(cell) <EOL>nml_doc.includes.append(incl) <EOL>found_cell = True<EOL><DEDENT><DEDENT>if not found_cell:<EOL><INDENT>for p in nl_model.populations:<EOL><INDENT>if p.component == c.id:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>if not incl in nml_doc.includes:<EOL><INDENT>nml_doc.includes.append(incl) <EOL><DEDENT><DEDENT>'''<STR_LIT>'''<EOL>if c.neuroml2_cell: <EOL><INDENT>cell_params = c.parameters if c.parameters else {}<EOL>if c.neuroml2_cell.lower() == '<STR_LIT>':<EOL><INDENT>cell = neuroml.SpikeGenerator(id=c.id)<EOL>nml_doc.spike_generators.append(cell)<EOL><DEDENT>elif c.neuroml2_cell.lower() == '<STR_LIT>':<EOL><INDENT>cell = neuroml.SpikeGeneratorPoisson(id=c.id)<EOL>nml_doc.spike_generator_poissons.append(cell)<EOL><DEDENT>elif c.neuroml2_cell.lower() == '<STR_LIT>':<EOL><INDENT>cell = neuroml.SpikeGeneratorRefPoisson(id=c.id)<EOL>nml_doc.spike_generator_ref_poissons.append(cell)<EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>'%c.neuroml2_cell)<EOL><DEDENT>for p in cell_params:<EOL><INDENT>exec('<STR_LIT>' % (p, evaluate(cell_params[p], nl_model.parameters)))<EOL><DEDENT><DEDENT><DEDENT>for s in nl_model.synapses:<EOL><INDENT>if nml_doc.get_by_id(s.id) == None:<EOL><INDENT>if s.neuroml2_source_file:<EOL><INDENT>incl = neuroml.IncludeType(_locate_file(s.neuroml2_source_file, base_dir))<EOL>if not incl in nml_doc.includes:<EOL><INDENT>nml_doc.includes.append(incl) <EOL><DEDENT><DEDENT><DEDENT><DEDENT>_extract_pynn_components_to_neuroml(nl_model, nml_doc)<EOL>if print_summary:<EOL><INDENT>print_v(nml_doc.summary())<EOL><DEDENT>if target_dir == None:<EOL><INDENT>target_dir = base_dir<EOL><DEDENT>if format == '<STR_LIT>':<EOL><INDENT>if not nml_file_name:<EOL><INDENT>nml_file_name = _locate_file('<STR_LIT>' % nml_doc.id, target_dir)<EOL><DEDENT>from neuroml.writers import NeuroMLWriter<EOL>NeuroMLWriter.write(nml_doc, nml_file_name)<EOL><DEDENT>if format == '<STR_LIT>':<EOL><INDENT>if not nml_file_name:<EOL><INDENT>nml_file_name = _locate_file('<STR_LIT>' % nml_doc.id, target_dir)<EOL><DEDENT>from neuroml.writers import NeuroMLHdf5Writer<EOL>NeuroMLHdf5Writer.write(nml_doc, nml_file_name)<EOL><DEDENT>print_v("<STR_LIT>" % nml_file_name)<EOL>if validate and format == '<STR_LIT>':<EOL><INDENT>from pyneuroml import pynml<EOL>success = pynml.validate_neuroml2(nml_file_name, verbose_validate=False)<EOL>if success:<EOL><INDENT>print_v('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>print_v('<STR_LIT>')<EOL><DEDENT><DEDENT>return nml_file_name, nml_doc<EOL>
Generate and save NeuroML2 file (in either XML or HDF5 format) from the 
NeuroMLlite description