def parse(self, handler):
<EOL>main_config_filename = os.path.abspath(self.parameters['<STR_LIT:filename>'])<EOL>config = load_json(main_config_filename)<EOL>self.init_substitutes = {'<STR_LIT:.>':'<STR_LIT>'%os.path.dirname(main_config_filename),<EOL>'<STR_LIT>':'<STR_LIT>'%os.path.dirname(os.path.dirname(main_config_filename))}<EOL>self.substitutes = {'<STR_LIT>':'<STR_LIT:%s>'%os.path.dirname(main_config_filename)}<EOL>if '<STR_LIT>' in config:<EOL><INDENT>self.network_config = load_json(self.subs(config['<STR_LIT>']))<EOL><DEDENT>else:<EOL><INDENT>self.network_config = config<EOL><DEDENT>if '<STR_LIT>' in config:<EOL><INDENT>self.simulation_config = load_json(self.subs(config['<STR_LIT>']))<EOL><DEDENT>else:<EOL><INDENT>self.simulation_config = None<EOL><DEDENT>for m in self.network_config['<STR_LIT>']:<EOL><INDENT>path = self.subs(self.network_config['<STR_LIT>'][m])<EOL>self.substitutes[m] = path<EOL><DEDENT>if '<STR_LIT:id>' in self.parameters:<EOL><INDENT>id = self.parameters['<STR_LIT:id>']<EOL><DEDENT>else:<EOL><INDENT>id = '<STR_LIT>'<EOL><DEDENT>if id[<NUM_LIT:0>].isdigit():  <EOL><INDENT>id='<STR_LIT>'%id<EOL><DEDENT>self.handler = handler<EOL>notes = "<STR_LIT>"%main_config_filename<EOL>handler.handle_document_start(id, notes)<EOL>handler.handle_network(id, notes)<EOL>self.node_types = {}<EOL>for n in self.network_config['<STR_LIT>']['<STR_LIT>']:<EOL><INDENT>nodes_file = self.subs(n['<STR_LIT>'])<EOL>node_types_file = self.subs(n['<STR_LIT>'])<EOL>print_v("<STR_LIT>"%(nodes_file,node_types_file))<EOL>h5file=tables.open_file(nodes_file,mode='<STR_LIT:r>')<EOL>print_v("<STR_LIT>"%(h5file.filename))<EOL>self.parse_group(h5file.root.nodes)<EOL>h5file.close()<EOL>self.node_types[self.current_sonata_pop] = load_csv_props(node_types_file)<EOL>self.current_sonata_pop = None<EOL><DEDENT>self.edges_info = {}<EOL>self.conn_info = {}<EOL>if '<STR_LIT>' in self.network_config['<STR_LIT>']:<EOL><INDENT>for e in self.network_config['<STR_LIT>']['<STR_LIT>']:<EOL><INDENT>edges_file = self.subs(e['<STR_LIT>'])<EOL>edge_types_file = self.subs(e['<STR_LIT>'])<EOL>print_v("<STR_LIT>"%(edges_file,edge_types_file))<EOL>h5file=tables.open_file(edges_file,mode='<STR_LIT:r>')<EOL>print_v("<STR_LIT>"%(h5file.filename))<EOL>self.parse_group(h5file.root.edges)<EOL>h5file.close()<EOL>self.edges_info[self.current_edge] = load_csv_props(edge_types_file)<EOL>self.current_edge = None<EOL><DEDENT><DEDENT>for sonata_pop in self.cell_info:<EOL><INDENT>types_vs_pops = {}<EOL>for type in self.cell_info[sonata_pop]['<STR_LIT>']:<EOL><INDENT>node_type_info = self.node_types[sonata_pop][type]<EOL>model_name_type = node_type_info['<STR_LIT>'] if '<STR_LIT>' in node_type_infoelse (node_type_info['<STR_LIT>'] if '<STR_LIT>' in node_type_info else node_type_info['<STR_LIT>'])<EOL>model_type = node_type_info['<STR_LIT>']<EOL>model_template = node_type_info['<STR_LIT>'] if '<STR_LIT>' in node_type_info else '<STR_LIT>'<EOL>nml_pop_id = '<STR_LIT>'%(sonata_pop,model_name_type,type) <EOL>print_v("<STR_LIT>"%(nml_pop_id, node_type_info))<EOL>size = self.cell_info[sonata_pop]['<STR_LIT>'][type]<EOL>if model_type=='<STR_LIT>' and model_template=='<STR_LIT>':<EOL><INDENT>raise Exception('<STR_LIT>'%model_template)<EOL>pop_comp = '<STR_LIT>'%nml_pop_id <EOL>self.pop_comp_info[pop_comp] = {}<EOL>self.pop_comp_info[pop_comp]['<STR_LIT>'] = model_type<EOL>dynamics_params_file = self.subs(self.network_config['<STR_LIT>']['<STR_LIT>']) +'<STR_LIT:/>'+node_type_info['<STR_LIT>']<EOL>self.pop_comp_info[pop_comp]['<STR_LIT>'] = load_json(dynamics_params_file)<EOL><DEDENT>elif model_type=='<STR_LIT>' and model_template=='<STR_LIT>':<EOL><INDENT>pop_comp = '<STR_LIT>'%nml_pop_id <EOL>self.pop_comp_info[pop_comp] = {}<EOL>self.pop_comp_info[pop_comp]['<STR_LIT>'] = model_type<EOL>self.pop_comp_info[pop_comp]['<STR_LIT>'] = model_template<EOL>dynamics_params_file = self.subs(self.network_config['<STR_LIT>']['<STR_LIT>']) +'<STR_LIT:/>'+node_type_info['<STR_LIT>']<EOL>self.pop_comp_info[pop_comp]['<STR_LIT>'] = load_json(dynamics_params_file)<EOL><DEDENT>else:<EOL><INDENT>pop_comp = DUMMY_CELL<EOL>self.pop_comp_info[pop_comp] = {}<EOL>self.pop_comp_info[pop_comp]['<STR_LIT>'] = pop_comp<EOL><DEDENT>self.nml_pop_vs_comps[nml_pop_id] = pop_comp<EOL>properties = {}<EOL>properties['<STR_LIT>']=type<EOL>properties['<STR_LIT>']=sonata_pop<EOL>properties['<STR_LIT>']=sonata_pop<EOL>for i in node_type_info:<EOL><INDENT>properties[i]=node_type_info[i]<EOL>if i=='<STR_LIT>':<EOL><INDENT>properties['<STR_LIT:type>']=node_type_info[i].upper()<EOL><DEDENT><DEDENT>color = '<STR_LIT>'%(self.myrandom.random(),self.myrandom.random(),self.myrandom.random())<EOL>try:<EOL><INDENT>import opencortex.utils.color as occ<EOL>interneuron = '<STR_LIT>' in nml_pop_id or '<STR_LIT>' in nml_pop_id<EOL>if '<STR_LIT>' in nml_pop_id:<EOL><INDENT>color = occ.L23_INTERNEURON if interneuron else occ.L23_PRINCIPAL_CELL<EOL>pop.properties.append(neuroml.Property('<STR_LIT>','<STR_LIT>'))<EOL><DEDENT>if '<STR_LIT>' in nml_pop_id:<EOL><INDENT>color = occ.L4_INTERNEURON if interneuron else occ.L4_PRINCIPAL_CELL<EOL>pop.properties.append(neuroml.Property('<STR_LIT>','<STR_LIT>'))<EOL><DEDENT>if '<STR_LIT>' in nml_pop_id:<EOL><INDENT>color = occ.L5_INTERNEURON if interneuron else occ.L5_PRINCIPAL_CELL<EOL>pop.properties.append(neuroml.Property('<STR_LIT>','<STR_LIT>'))<EOL><DEDENT>if '<STR_LIT>' in nml_pop_id:<EOL><INDENT>color = occ.L6_INTERNEURON if interneuron else occ.L6_PRINCIPAL_CELL<EOL>pop.properties.append(neuroml.Property('<STR_LIT>','<STR_LIT>'))<EOL><DEDENT><DEDENT>except:<EOL><INDENT>pass <EOL><DEDENT>properties['<STR_LIT>']=color<EOL>if True or not '<STR_LIT>' in self.cell_info[sonata_pop]['<STR_LIT:0>']:<EOL><INDENT>properties={}  <EOL><DEDENT>if model_type != '<STR_LIT>':<EOL><INDENT>self.handler.handle_population(nml_pop_id, <EOL>pop_comp, <EOL>size,<EOL>component_obj=None,<EOL>properties=properties)<EOL><DEDENT>types_vs_pops[type] = nml_pop_id<EOL><DEDENT>self.cell_info[sonata_pop]['<STR_LIT>'] = {}  <EOL>self.cell_info[sonata_pop]['<STR_LIT>'] = {}   <EOL>for i in self.cell_info[sonata_pop]['<STR_LIT>']:<EOL><INDENT>pop = types_vs_pops[self.cell_info[sonata_pop]['<STR_LIT>'][i]]<EOL>if not pop in self.cell_info[sonata_pop]['<STR_LIT>']:<EOL><INDENT>self.cell_info[sonata_pop]['<STR_LIT>'][pop] = <NUM_LIT:0><EOL><DEDENT>index = self.cell_info[sonata_pop]['<STR_LIT>'][pop]<EOL>self.cell_info[sonata_pop]['<STR_LIT>'][i] = (pop, index)<EOL>if not pop in self.nml_ids_vs_gids:<EOL><INDENT>self.nml_ids_vs_gids[pop] = {}<EOL><DEDENT>self.nml_ids_vs_gids[pop][index] = (sonata_pop, i)<EOL>if i in self.cell_info[sonata_pop]['<STR_LIT:0>']['<STR_LIT>']: <EOL><INDENT>if not pop in self.nml_pops_having_locations: <EOL><INDENT>self.nml_pops_having_locations.append(pop)<EOL><DEDENT>pos = self.cell_info[sonata_pop]['<STR_LIT:0>']['<STR_LIT>'][i]<EOL>self.handler.handle_location(index, <EOL>pop, <EOL>pop_comp, <EOL>pos['<STR_LIT:x>'] if '<STR_LIT:x>' in pos and pos['<STR_LIT:x>'] is not None else <NUM_LIT:0>, <EOL>pos['<STR_LIT:y>'] if '<STR_LIT:y>' in pos and pos['<STR_LIT:y>'] is not None else <NUM_LIT:0>, <EOL>pos['<STR_LIT:z>'] if '<STR_LIT:z>' in pos and pos['<STR_LIT:z>'] is not None else <NUM_LIT:0>)<EOL><DEDENT>self.cell_info[sonata_pop]['<STR_LIT>'][pop]+=<NUM_LIT:1><EOL><DEDENT><DEDENT>if self.simulation_config:<EOL><INDENT>if self.simulation_config:<EOL><INDENT>for m in self.simulation_config['<STR_LIT>']:<EOL><INDENT>path = self.subs(self.simulation_config['<STR_LIT>'][m])<EOL>self.substitutes[m] = path<EOL><DEDENT><DEDENT>for s1 in ['<STR_LIT>']:<EOL><INDENT>for k in self.simulation_config[s1]:<EOL><INDENT>self.simulation_config[s1][k] = self.subs(self.simulation_config[s1][k])<EOL><DEDENT><DEDENT>for s1 in ['<STR_LIT>']:<EOL><INDENT>for s2 in self.simulation_config[s1]:<EOL><INDENT>for k in self.simulation_config[s1][s2]:<EOL><INDENT>self.simulation_config[s1][s2][k] = self.subs(self.simulation_config[s1][s2][k])<EOL><DEDENT><DEDENT><DEDENT>if '<STR_LIT>' in self.simulation_config:<EOL><INDENT>node_sets = load_json(self.subs(self.simulation_config['<STR_LIT>']))<EOL>self.simulation_config['<STR_LIT>'] = node_sets <EOL><DEDENT>if not '<STR_LIT>' in self.simulation_config:<EOL><INDENT>self.simulation_config['<STR_LIT>'] = {}<EOL><DEDENT>for sonata_pop in self.cell_info:<EOL><INDENT>self.node_set_mappings[sonata_pop] = {}<EOL>for sindex in self.cell_info[sonata_pop]['<STR_LIT>']:<EOL><INDENT>nml_pop = self.cell_info[sonata_pop]['<STR_LIT>'][sindex][<NUM_LIT:0>]<EOL>nml_index = self.cell_info[sonata_pop]['<STR_LIT>'][sindex][<NUM_LIT:1>]<EOL>if not nml_pop in self.node_set_mappings[sonata_pop]:<EOL><INDENT>self.node_set_mappings[sonata_pop][nml_pop] = []<EOL><DEDENT>self.node_set_mappings[sonata_pop][nml_pop].append(nml_index)<EOL><DEDENT><DEDENT>for node_set in self.simulation_config['<STR_LIT>']:<EOL><INDENT>self.node_set_mappings[node_set] = {}<EOL>node_set_props = self.simulation_config['<STR_LIT>'][node_set]<EOL>for sonata_pop in self.cell_info:<EOL><INDENT>for sindex in self.cell_info[sonata_pop]['<STR_LIT>']:<EOL><INDENT>type = self.cell_info[sonata_pop]['<STR_LIT>'][sindex]<EOL>type_info = self.node_types[sonata_pop][type]<EOL>nml_pop = self.cell_info[sonata_pop]['<STR_LIT>'][sindex][<NUM_LIT:0>]<EOL>nml_index = self.cell_info[sonata_pop]['<STR_LIT>'][sindex][<NUM_LIT:1>]<EOL>if '<STR_LIT>' in node_set_props and node_set_props['<STR_LIT>'] == sonata_pop:<EOL><INDENT>if '<STR_LIT>' in node_set_props and sindex in node_set_props['<STR_LIT>']:<EOL><INDENT>if not nml_pop in self.node_set_mappings[node_set]:<EOL><INDENT>self.node_set_mappings[node_set][nml_pop] = []<EOL><DEDENT>self.node_set_mappings[node_set][nml_pop].append(nml_index)<EOL><DEDENT><DEDENT>matches = _matches_node_set_props(type_info, node_set_props)<EOL>if matches:<EOL><INDENT>if not nml_pop in self.node_set_mappings[node_set]:<EOL><INDENT>self.node_set_mappings[node_set][nml_pop] = []<EOL><DEDENT>self.node_set_mappings[node_set][nml_pop].append(nml_index)<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>for input in self.simulation_config['<STR_LIT>']:<EOL><INDENT>info = self.simulation_config['<STR_LIT>'][input]<EOL>self.input_comp_info[input] = {}<EOL>self.input_comp_info[input][info['<STR_LIT>']] = {}<EOL>node_set = info['<STR_LIT>']<EOL>if info['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>comp = '<STR_LIT>'%input<EOL>self.input_comp_info[input][info['<STR_LIT>']][comp] = {'<STR_LIT>':info['<STR_LIT>'],'<STR_LIT>':info['<STR_LIT>'],'<STR_LIT>':info['<STR_LIT>']}<EOL>for nml_pop_id in self.node_set_mappings[node_set]:<EOL><INDENT>input_list_id = '<STR_LIT>'%(input,nml_pop_id)<EOL>indices = self.node_set_mappings[node_set][nml_pop_id]<EOL>self.handler.handle_input_list(input_list_id, <EOL>nml_pop_id, <EOL>comp, <EOL>len(indices))<EOL>count = <NUM_LIT:0><EOL>for index in indices:<EOL><INDENT>self.handler.handle_single_input(input_list_id, <EOL>count, <EOL>cellId = index, <EOL>segId = <NUM_LIT:0>, <EOL>fract = <NUM_LIT:0.5>)<EOL>count+=<NUM_LIT:1><EOL><DEDENT><DEDENT><DEDENT>elif info['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>node_info = self.cell_info[node_set]<EOL>from pyneuroml.plot.PlotSpikes import read_sonata_spikes_hdf5_file<EOL>ids_times = read_sonata_spikes_hdf5_file(self.subs(info['<STR_LIT>']))<EOL>for id in ids_times:<EOL><INDENT>times = ids_times[id]<EOL>if id in node_info['<STR_LIT>']:<EOL><INDENT>nml_pop_id, cell_id = node_info['<STR_LIT>'][id] <EOL>print_v("<STR_LIT>"%(id, node_set, nml_pop_id, cell_id, len(times)))<EOL>component = '<STR_LIT>'%(nml_pop_id,cell_id)<EOL>self.input_comp_info[input][info['<STR_LIT>']][component] ={'<STR_LIT:id>': cell_id, '<STR_LIT>': times}<EOL>'''<STR_LIT>'''<EOL><DEDENT>else:<EOL><INDENT>print_v("<STR_LIT>"%(id, node_set))<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>raise Exception("<STR_LIT>"%(info['<STR_LIT>']))<EOL><DEDENT><DEDENT>projections_created = []<EOL>for conn in self.conn_info:<EOL><INDENT>pre_node = self.conn_info[conn]['<STR_LIT>']<EOL>post_node = self.conn_info[conn]['<STR_LIT>']<EOL>for i in range(len(self.conn_info[conn]['<STR_LIT>'])):<EOL><INDENT>pre_id = self.conn_info[conn]['<STR_LIT>'][i]<EOL>post_id = self.conn_info[conn]['<STR_LIT>'][i]<EOL>nsyns = self.conn_info[conn]['<STR_LIT>'][i] if '<STR_LIT>' in self.conn_info[conn] else <NUM_LIT:1><EOL>type = self.conn_info[conn]['<STR_LIT>'][i]<EOL>pre_pop,pre_i = self.cell_info[pre_node]['<STR_LIT>'][pre_id]<EOL>post_pop,post_i = self.cell_info[post_node]['<STR_LIT>'][post_id]<EOL>cell_type_pre = self.cell_info[pre_node]['<STR_LIT>'][pre_id]<EOL>pop_type_pre = self.node_types[pre_node][cell_type_pre]['<STR_LIT>']<EOL>synapse = self.edges_info[conn][type]['<STR_LIT>'].split('<STR_LIT:.>')[<NUM_LIT:0>]<EOL>self.syn_comp_info[synapse] = {}<EOL>dynamics_params_file = self.subs(self.network_config['<STR_LIT>']['<STR_LIT>']) +'<STR_LIT:/>'+self.edges_info[conn][type]['<STR_LIT>']<EOL>self.syn_comp_info[synapse]['<STR_LIT>'] = load_json(dynamics_params_file)<EOL>proj_id = '<STR_LIT>'%(pre_pop,post_pop,synapse)<EOL>sign = self.syn_comp_info[synapse]['<STR_LIT>']['<STR_LIT>'] if '<STR_LIT>' in self.syn_comp_info[synapse]['<STR_LIT>'] else <NUM_LIT:1><EOL>weight = self.edges_info[conn][type]['<STR_LIT>'] if '<STR_LIT>' in self.edges_info[conn][type] else <NUM_LIT:1.0><EOL>syn_weight_edge_group_0 = self.conn_info[conn]['<STR_LIT>'][i] if '<STR_LIT>' in self.conn_info[conn] else None<EOL>if syn_weight_edge_group_0:<EOL><INDENT>weight = syn_weight_edge_group_0<EOL><DEDENT>weight_scale = <NUM_LIT><EOL>if '<STR_LIT>' in self.syn_comp_info[synapse]['<STR_LIT>']:<EOL><INDENT>weight_scale = <NUM_LIT:1><EOL><DEDENT>weight=weight_scale * sign * weight * nsyns<EOL>delay = self.edges_info[conn][type]['<STR_LIT>'] if '<STR_LIT>' in self.edges_info[conn][type] else <NUM_LIT:0><EOL>if not pop_type_pre == '<STR_LIT>':<EOL><INDENT>if not proj_id in projections_created:<EOL><INDENT>self.handler.handle_projection(proj_id, <EOL>pre_pop, <EOL>post_pop, <EOL>synapse)<EOL>projections_created.append(proj_id)<EOL><DEDENT>self.handler.handle_connection(proj_id, <EOL>i, <EOL>pre_pop, <EOL>post_pop, <EOL>synapse,pre_i,post_i,weight=weight,delay=delay)<EOL><DEDENT>else:<EOL><INDENT>component = '<STR_LIT>'%(pre_pop,pre_i)<EOL>input_list_id = '<STR_LIT>'%(component,post_pop,post_i,i)<EOL>self.handler.handle_input_list(input_list_id, <EOL>post_pop, <EOL>component, <EOL><NUM_LIT:1>)<EOL>self.handler.handle_single_input(input_list_id, <EOL><NUM_LIT:0>, <EOL>cellId = post_i, <EOL>segId = <NUM_LIT:0>, <EOL>fract = <NUM_LIT:0.5>,<EOL>weight=weight)<EOL><DEDENT><DEDENT><DEDENT>"""<STR_LIT>"""<EOL>
Main method to parse the Sonata files and call the appropriate methods
in the handler