def generate_network(nl_model, <EOL>handler, <EOL>seed=<NUM_LIT>, <EOL>always_include_props=False,<EOL>include_connections=True,<EOL>include_inputs=True,<EOL>base_dir=None):
pop_locations = {}<EOL>cell_objects = {}<EOL>synapse_objects = {}<EOL>print_v("<STR_LIT>" % (nl_model.id, <EOL>'<STR_LIT>' % base_dir if base_dir else '<STR_LIT>'))<EOL>rng = random.Random(seed)<EOL>if nl_model.network_reader:<EOL><INDENT>exec('<STR_LIT>' % (nl_model.network_reader.type, nl_model.network_reader.type))<EOL>exec('<STR_LIT>' % (nl_model.network_reader.type))<EOL>network_reader.parameters = nl_model.network_reader.parameters<EOL>network_reader.parse(handler)<EOL>pop_locations = network_reader.get_locations()<EOL><DEDENT>else:<EOL><INDENT>notes = "<STR_LIT>" % nl_model.id<EOL>notes += "<STR_LIT>" % (seed)<EOL>if nl_model.parameters:<EOL><INDENT>notes += "<STR_LIT>" <EOL>for p in nl_model.parameters:<EOL><INDENT>notes += "<STR_LIT>" % (p, nl_model.parameters[p])<EOL><DEDENT><DEDENT>handler.handle_document_start(nl_model.id, notes)<EOL>temperature = '<STR_LIT>' % nl_model.temperature if nl_model.temperature else None <EOL>handler.handle_network(nl_model.id, nl_model.notes, temperature=temperature)<EOL><DEDENT>nml2_doc_temp = _extract_pynn_components_to_neuroml(nl_model)<EOL>for c in nl_model.cells:<EOL><INDENT>if c.neuroml2_source_file:<EOL><INDENT>from pyneuroml import pynml<EOL>nml2_doc = pynml.read_neuroml2_file(_locate_file(c.neuroml2_source_file, base_dir), <EOL>include_includes=True)<EOL>cell_objects[c.id] = nml2_doc.get_by_id(c.id)<EOL><DEDENT>if c.pynn_cell:<EOL><INDENT>cell_objects[c.id] = nml2_doc_temp.get_by_id(c.id)<EOL><DEDENT><DEDENT>for s in nl_model.synapses:<EOL><INDENT>if s.neuroml2_source_file:<EOL><INDENT>from pyneuroml import pynml<EOL>nml2_doc = pynml.read_neuroml2_file(_locate_file(s.neuroml2_source_file, base_dir), <EOL>include_includes=True)<EOL>synapse_objects[s.id] = nml2_doc.get_by_id(s.id)<EOL><DEDENT>if s.pynn_synapse:<EOL><INDENT>synapse_objects[s.id] = nml2_doc_temp.get_by_id(s.id)<EOL><DEDENT><DEDENT>for p in nl_model.populations:<EOL><INDENT>size = evaluate(p.size, nl_model.parameters)<EOL>properties = p.properties if p.properties else {}<EOL>if p.random_layout:<EOL><INDENT>properties['<STR_LIT>'] = p.random_layout.region<EOL><DEDENT>if not p.random_layout and not p.single_location and not always_include_props:<EOL><INDENT>properties = {} <EOL><DEDENT>if p.notes:<EOL><INDENT>handler.handle_population(p.id, <EOL>p.component, <EOL>size, <EOL>cell_objects[p.component] if p.component in cell_objects else None,<EOL>properties=properties,<EOL>notes=p.notes)<EOL><DEDENT>else:<EOL><INDENT>handler.handle_population(p.id, <EOL>p.component, <EOL>size, <EOL>cell_objects[p.component] if p.component in cell_objects else None,<EOL>properties=properties)<EOL><DEDENT>pop_locations[p.id] = np.zeros((size, <NUM_LIT:3>))<EOL>for i in range(size):<EOL><INDENT>if p.random_layout:<EOL><INDENT>region = nl_model.get_child(p.random_layout.region, '<STR_LIT>')<EOL>x = region.x + rng.random() * region.width<EOL>y = region.y + rng.random() * region.height<EOL>z = region.z + rng.random() * region.depth<EOL>pop_locations[p.id][i] = (x, y, z)<EOL>handler.handle_location(i, p.id, p.component, x, y, z)<EOL><DEDENT>if p.single_location:<EOL><INDENT>loc = p.single_location.location<EOL>x = loc.x<EOL>y = loc.y<EOL>z = loc.z<EOL>pop_locations[p.id][i] = (x, y, z)<EOL>handler.handle_location(i, p.id, p.component, x, y, z)<EOL><DEDENT><DEDENT>if hasattr(handler, '<STR_LIT>'):<EOL><INDENT>handler.finalise_population(p.id)<EOL><DEDENT><DEDENT>if include_connections:<EOL><INDENT>for p in nl_model.projections:<EOL><INDENT>type = p.type if p.type else '<STR_LIT>'<EOL>handler.handle_projection(p.id, <EOL>p.presynaptic, <EOL>p.postsynaptic, <EOL>p.synapse,<EOL>synapse_obj=synapse_objects[p.synapse] if p.synapse in synapse_objects else None,<EOL>pre_synapse_obj=synapse_objects[p.pre_synapse] if p.pre_synapse in synapse_objects else None,<EOL>type=type)<EOL>delay = p.delay if p.delay else <NUM_LIT:0><EOL>weight = p.weight if p.weight else <NUM_LIT:1><EOL>conn_count = <NUM_LIT:0><EOL>if p.random_connectivity:<EOL><INDENT>for pre_i in range(len(pop_locations[p.presynaptic])):<EOL><INDENT>for post_i in range(len(pop_locations[p.postsynaptic])):<EOL><INDENT>flip = rng.random()<EOL>if flip < p.random_connectivity.probability:<EOL><INDENT>weight = evaluate(weight, nl_model.parameters)<EOL>delay = evaluate(delay, nl_model.parameters)<EOL>handler.handle_connection(p.id, <EOL>conn_count, <EOL>p.presynaptic, <EOL>p.postsynaptic, <EOL>p.synapse,pre_i,post_i,preSegId=<NUM_LIT:0>,preFract=<NUM_LIT:0.5>,postSegId=<NUM_LIT:0>,postFract=<NUM_LIT:0.5>,delay=delay,weight=weight)<EOL>conn_count += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT><DEDENT>if p.convergent_connectivity:<EOL><INDENT>for post_i in range(len(pop_locations[p.postsynaptic])):<EOL><INDENT>for count in range(int(p.convergent_connectivity.num_per_post)):<EOL><INDENT>found = False<EOL>while not found:<EOL><INDENT>pre_i = int(rng.random()*len(pop_locations[p.presynaptic]))<EOL>if p.presynaptic==p.postsynaptic and pre_i==post_i:<EOL><INDENT>found=False<EOL><DEDENT>else:<EOL><INDENT>found=True<EOL><DEDENT><DEDENT>weight = evaluate(weight, nl_model.parameters)<EOL>delay = evaluate(delay, nl_model.parameters)<EOL>print_v("<STR_LIT>"%(conn_count, pre_i, post_i, count, p.convergent_connectivity.num_per_post, weight, delay))<EOL>handler.handle_connection(p.id, <EOL>conn_count, <EOL>p.presynaptic, <EOL>p.postsynaptic, <EOL>p.synapse,pre_i,post_i,preSegId=<NUM_LIT:0>,preFract=<NUM_LIT:0.5>,postSegId=<NUM_LIT:0>,postFract=<NUM_LIT:0.5>,delay=delay,weight=weight)<EOL>conn_count += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>elif p.one_to_one_connector:<EOL><INDENT>for i in range(min(len(pop_locations[p.presynaptic]), len(pop_locations[p.postsynaptic]))):<EOL><INDENT>weight = evaluate(weight, nl_model.parameters)<EOL>delay = evaluate(delay, nl_model.parameters)<EOL>handler.handle_connection(p.id, <EOL>conn_count, <EOL>p.presynaptic, <EOL>p.postsynaptic, <EOL>p.synapse,i,i,preSegId=<NUM_LIT:0>,preFract=<NUM_LIT:0.5>,postSegId=<NUM_LIT:0>,postFract=<NUM_LIT:0.5>,delay=delay,weight=weight)<EOL>conn_count += <NUM_LIT:1><EOL><DEDENT><DEDENT>handler.finalise_projection(p.id, <EOL>p.presynaptic, <EOL>p.postsynaptic, <EOL>p.synapse)<EOL><DEDENT><DEDENT>if include_inputs:   <EOL><INDENT>for input in nl_model.inputs:<EOL><INDENT>handler.handle_input_list(input.id, <EOL>input.population, <EOL>input.input_source, <EOL>size=<NUM_LIT:0>, <EOL>input_comp_obj=None)<EOL>input_count = <NUM_LIT:0>      <EOL>for i in range(len(pop_locations[input.population])):<EOL><INDENT>flip = rng.random()<EOL>weight = input.weight if input.weight else <NUM_LIT:1><EOL>if flip * <NUM_LIT> < input.percentage:<EOL><INDENT>number_per_cell = evaluate(input.number_per_cell, nl_model.parameters) if input.number_per_cell else <NUM_LIT:1><EOL>for j in range(number_per_cell):<EOL><INDENT>handler.handle_single_input(input.id, <EOL>input_count, <EOL>i,<EOL>weight=evaluate(weight, nl_model.parameters))<EOL>input_count += <NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>handler.finalise_input_source(input.id)<EOL><DEDENT><DEDENT>if hasattr(handler, '<STR_LIT>'):<EOL><INDENT>handler.finalise_document()<EOL><DEDENT>
Generate the network model as described in NeuroMLlite in a specific handler,
e.g. NeuroMLHandler, PyNNHandler, etc.