def _extract_pynn_components_to_neuroml(nl_model, nml_doc=None):
    """
    Parse the NeuroMLlite description for cell, synapses and inputs described as 
    PyNN elements (e.g. IF_cond_alpha, DCSource) and parameters, and convert 
    these to the equivalent elements in a NeuroMLDocument
    """
    
    if nml_doc == None:
        from neuroml import NeuroMLDocument
        nml_doc = NeuroMLDocument(id="temp")
    
    for c in nl_model.cells:
        
        if c.pynn_cell:
            
            if nml_doc.get_by_id(c.id) == None:
                import pyNN.neuroml
                cell_params = c.parameters if c.parameters else {}
                #print('------- %s: %s' % (c, cell_params))
                for p in cell_params:
                    cell_params[p] = evaluate(cell_params[p], nl_model.parameters)
                #print('====== %s: %s' % (c, cell_params))
                for proj in nl_model.projections:

                    synapse = nl_model.get_child(proj.synapse, 'synapses')
                    post_pop = nl_model.get_child(proj.postsynaptic, 'populations')
                    if post_pop.component == c.id:
                        #print("--------- Cell %s in post pop %s of %s uses %s"%(c.id,post_pop.id, proj.id, synapse))

                        if synapse.pynn_receptor_type == 'excitatory':
                            post = '_E'
                        elif synapse.pynn_receptor_type == 'inhibitory':
                            post = '_I'
                        for p in synapse.parameters:
                            cell_params['%s%s' % (p, post)] = synapse.parameters[p]

                temp_cell = eval('pyNN.neuroml.%s(**cell_params)' % c.pynn_cell)
                if c.pynn_cell != 'SpikeSourcePoisson':
                    temp_cell.default_initial_values['v'] = temp_cell.parameter_space['v_rest'].base_value

                cell_id = temp_cell.add_to_nml_doc(nml_doc, None)
                cell = nml_doc.get_by_id(cell_id)
                cell.id = c.id
            
    for s in nl_model.synapses:
        if nml_doc.get_by_id(s.id) == None:
            
            if s.pynn_synapse_type and s.pynn_receptor_type:
                import neuroml
                
                if s.pynn_synapse_type == 'cond_exp':
                    syn = neuroml.ExpCondSynapse(id=s.id, tau_syn=s.parameters['tau_syn'], e_rev=s.parameters['e_rev'])
                    nml_doc.exp_cond_synapses.append(syn)
                elif s.pynn_synapse_type == 'cond_alpha':
                    syn = neuroml.AlphaCondSynapse(id=s.id, tau_syn=s.parameters['tau_syn'], e_rev=s.parameters['e_rev'])
                    nml_doc.alpha_cond_synapses.append(syn)
                elif s.pynn_synapse_type == 'curr_exp':
                    syn = neuroml.ExpCurrSynapse(id=s.id, tau_syn=s.parameters['tau_syn'])
                    nml_doc.exp_curr_synapses.append(syn)
                elif s.pynn_synapse_type == 'curr_alpha':
                    syn = neuroml.AlphaCurrSynapse(id=s.id, tau_syn=s.parameters['tau_syn'])
                    nml_doc.alpha_curr_synapses.append(syn)
    
    for i in nl_model.input_sources:
        
        #if nml_doc.get_by_id(i.id) == None:
      
        if i.pynn_input:
            import pyNN.neuroml
            input_params = i.parameters if i.parameters else {}
            exec('input__%s = pyNN.neuroml.%s(**input_params)' % (i.id, i.pynn_input))
            exec('temp_input = input__%s' % i.id)
            pg_id = temp_input.add_to_nml_doc(nml_doc, None)
            #for pp in nml_doc.pulse_generators:
            #    print('PG: %s: %s'%(pp,pp.id))
            pg = nml_doc.get_by_id(pg_id)
            pg.id = i.id
            
    return nml_doc
def _extract_pynn_components_to_neuroml ( nl_model , nml_doc = None ) : if nml_doc == None : from neuroml import NeuroMLDocument nml_doc = NeuroMLDocument ( id = "temp" ) for c in nl_model . cells : if c . pynn_cell : if nml_doc . get_by_id ( c . id ) == None : import pyNN . neuroml cell_params = c . parameters if c . parameters else { } for p in cell_params : cell_params [ p ] = evaluate ( cell_params [ p ] , nl_model . parameters ) for proj in nl_model . projections : synapse = nl_model . get_child ( proj . synapse , 'synapses' ) post_pop = nl_model . get_child ( proj . postsynaptic , 'populations' ) if post_pop . component == c . id : if synapse . pynn_receptor_type == 'excitatory' : post = '_E' elif synapse . pynn_receptor_type == 'inhibitory' : post = '_I' for p in synapse . parameters : cell_params [ '%s%s' % ( p , post ) ] = synapse . parameters [ p ] temp_cell = eval ( 'pyNN.neuroml.%s(**cell_params)' % c . pynn_cell ) if c . pynn_cell != 'SpikeSourcePoisson' : temp_cell . default_initial_values [ 'v' ] = temp_cell . parameter_space [ 'v_rest' ] . base_value cell_id = temp_cell . add_to_nml_doc ( nml_doc , None ) cell = nml_doc . get_by_id ( cell_id ) cell . id = c . id for s in nl_model . synapses : if nml_doc . get_by_id ( s . id ) == None : if s . pynn_synapse_type and s . pynn_receptor_type : import neuroml if s . pynn_synapse_type == 'cond_exp' : syn = neuroml . ExpCondSynapse ( id = s . id , tau_syn = s . parameters [ 'tau_syn' ] , e_rev = s . parameters [ 'e_rev' ] ) nml_doc . exp_cond_synapses . append ( syn ) elif s . pynn_synapse_type == 'cond_alpha' : syn = neuroml . AlphaCondSynapse ( id = s . id , tau_syn = s . parameters [ 'tau_syn' ] , e_rev = s . parameters [ 'e_rev' ] ) nml_doc . alpha_cond_synapses . append ( syn ) elif s . pynn_synapse_type == 'curr_exp' : syn = neuroml . ExpCurrSynapse ( id = s . id , tau_syn = s . parameters [ 'tau_syn' ] ) nml_doc . exp_curr_synapses . append ( syn ) elif s . pynn_synapse_type == 'curr_alpha' : syn = neuroml . AlphaCurrSynapse ( id = s . id , tau_syn = s . parameters [ 'tau_syn' ] ) nml_doc . alpha_curr_synapses . append ( syn ) for i in nl_model . input_sources : if i . pynn_input : import pyNN . neuroml input_params = i . parameters if i . parameters else { } exec ( 'input__%s = pyNN.neuroml.%s(**input_params)' % ( i . id , i . pynn_input ) ) exec ( 'temp_input = input__%s' % i . id ) pg_id = temp_input . add_to_nml_doc ( nml_doc , None ) pg = nml_doc . get_by_id ( pg_id ) pg . id = i . id return nml_doc
Parse the NeuroMLlite description for cell, synapses and inputs described as 
    PyNN elements (e.g. IF_cond_alpha, DCSource) and parameters, and convert 
    these to the equivalent elements in a NeuroMLDocument