def add_input_source(self, input_source, network):
        input_params = input_source.parameters if input_source.parameters else {}

        for ip in input_params:
            input_params[ip] = evaluate(input_params[ip], network.parameters)
               
        ''' This is a quick hack to support noisyCurrentSource before that type is integrated into the 
            core of NeuroML...
            TODO: remove when integrated!
        '''
        if input_source.lems_source_file and 'noisyCurrentSource' in input_source.id:
            pynn_input_params = {}
            for p in input_params:
                if p=='delay':
                    pynn_input_params['start'] = convert_to_units(input_params[p],'ms')
                elif p=='duration':
                    pynn_input_params['stop'] = convert_to_units(input_params[p],'ms') + convert_to_units(input_params['delay'],'ms')
                elif p=='mean':
                    pynn_input_params['mean'] = convert_to_units(input_params[p],'nA')
                elif p=='stdev':
                    pynn_input_params['stdev'] = convert_to_units(input_params[p],'nA')
                elif p=='noiseDt':
                    pynn_input_params['dt'] = convert_to_units(input_params[p],'ms')
                else:
                    raise Exception('Parameter %s=%s is not appropriate for inout %s'%(p,input_params[p],input_source.id))
           
            exec('self.input_sources["%s"] = self.sim.NoisyCurrentSource(**pynn_input_params)'%(input_source.id))
        else:
            exec('self.input_sources["%s"] = self.sim.%s(**input_params)'%(input_source.id,input_source.pynn_input))
Beispiel #2
0
    def _eval_at_all(self, expr, parameters, traces):
        tr_present = []
        for t in traces:
            if t != "t" and t in expr:
                tr_present.append(t)

        ret_val = []

        num_vals = len(traces[tr_present[0]])
        print_(
            "Evaluating %s with traces: %s (%i values) and params: %s"
            % (expr, tr_present, num_vals, parameters.keys()),
            self.verbose,
        )

        for i in range(num_vals):
            noo = expr
            for t in tr_present:
                noo = noo.replace(t, str(traces[tr_present[0]][i]))
                # print_v('%i: %s -> %s'%(i, expr, noo))

            r = evaluate(noo, parameters)
            ret_val.append(float(r))
        print_(
            "Generated: %s->%s (#%s)" % (ret_val[0], ret_val[-1], len(ret_val)),
            self.verbose,
        )
        return ret_val
Beispiel #3
0
    def handle_projection(self, projName, prePop, postPop, synapse, hasWeights=False, hasDelays=False, type="projection", synapse_obj=None, pre_synapse_obj=None):

        shape = self.EXC_CONN_ARROW_SHAPE
        line = 'solid'
            
        # Could be used in a network with no explicit connections, but weight 
        # between populations set at high level projection element in NeuroMLlite
        projection_weight = 1.0
        
        self.proj_pre_pops[projName] = prePop
        self.proj_post_pops[projName] = postPop
        self.proj_types[projName] = type
        
        if prePop in self.pop_types:
            if 'I' in self.pop_types[prePop]:
                shape = self.INH_CONN_ARROW_SHAPE
                
        if type=='electricalProjection':
                shape = self.GAP_CONN_ARROW_SHAPE
                line = 'dashed'
                
        if type=='continuousProjection':
                shape = self.CONT_CONN_ARROW_SHAPE
                line = 'solid'
            
        if synapse_obj:
            self.proj_syn_objs[projName] = synapse_obj
            erev = self.get_reversal_potential_mV(synapse_obj)
            if erev!=None and erev < self.CUTOFF_INH_SYN_MV:
                shape = self.INH_CONN_ARROW_SHAPE
                
        if self.nl_network:
            syn = self.nl_network.get_child(synapse,'synapses')
            if syn:
                if syn.parameters:
                    if 'e_rev' in syn.parameters and syn.parameters['e_rev']<self.CUTOFF_INH_SYN_MV:
                        shape = self.INH_CONN_ARROW_SHAPE
            
            proj = self.nl_network.get_child(projName,'projections')  
            if proj:
                if proj.weight:
                    proj_weight = evaluate(proj.weight, self.nl_network.parameters)
                    if proj_weight<0:
                        shape = self.INH_CONN_ARROW_SHAPE
                    projection_weight = abs(proj_weight)
                        
        
        self.proj_weights[projName] = projection_weight
        self.proj_shapes[projName] = shape
        self.proj_lines[projName] = line
        self.proj_conns[projName] = 0
        self.proj_tot_weight[projName] = 0
        
        if self.is_cell_level():
            pre_size = self.pop_sizes[prePop]
            post_size = self.pop_sizes[postPop]
            self.proj_individual_weights[projName] = np.zeros((pre_size, post_size))
            self.proj_individual_conn_numbers[projName] = np.zeros((pre_size, post_size))
            self.proj_individual_scaled_weights[projName] = np.zeros((pre_size, post_size))
Beispiel #4
0
    def add_input_source(self, input_source, network):
        input_params = input_source.parameters if input_source.parameters else {}

        for ip in input_params:
            input_params[ip] = evaluate(input_params[ip], network.parameters)

        exec('self.input_sources["%s"] = self.sim.%s(**input_params)' %
             (input_source.id, input_source.pynn_input))
    def _convert_value(self, val):

        funcs = ['exp']
        for f in funcs:
            if '%s(' % f in val:
                val = val.replace('%s(' % f, 'math.%s(' % f)

        val = evaluate(val)  # catch if it's an int etc.
        return val
 def handle_projection(self, projName, prePop, postPop, synapse, hasWeights=False, hasDelays=False, type="projection", synapse_obj=None, pre_synapse_obj=None):
         
     weight = 1.0
     self.proj_pre_pops[projName] = prePop
     self.proj_post_pops[projName] = postPop
     
     proj_type = 'excitatory'
     
     if prePop in self.pop_types:
         if 'I' in self.pop_types[prePop]:
             proj_type = 'inhibitory'
             
     if type=='electricalProjection':
         proj_type = 'gap_junction'
         
     if synapse_obj:
         self.proj_syn_objs[projName] = synapse_obj
         erev = self.get_reversal_potential_mV(synapse_obj)
         if erev!=None and erev < self.CUTOFF_INH_SYN_MV:
             proj_type = 'inhibitory'
             
     if self.nl_network:
         syn = self.nl_network.get_child(synapse,'synapses')
         if syn:
             if syn.parameters:
                 if 'e_rev' in syn.parameters and syn.parameters['e_rev']<self.CUTOFF_INH_SYN_MV:
                     proj_type = 'inhibitory'
         
         proj = self.nl_network.get_child(projName,'projections')  
         if proj:
             if proj.weight:
                 proj_weight = evaluate(proj.weight, self.nl_network.parameters, self.rng)
                 if proj_weight<0:
                     proj_type = 'inhibitory'
                 weight = float(abs(proj_weight))
                 
     if type=='continuousProjection':
         proj_type += 'continuous'
                     
     
     self.proj_weights[projName] = float(weight)
     self.proj_types[projName] = proj_type
     self.proj_conns[projName] = 0
     self.proj_tot_weight[projName] = 0
     
     if self.is_cell_level():
         pre_size = self.pop_sizes[prePop]
         post_size = self.pop_sizes[postPop]
         self.proj_individual_weights[projName] = np.zeros((pre_size, post_size))
         self.proj_individual_conn_numbers[projName] = np.zeros((pre_size, post_size))
         self.proj_individual_scaled_weights[projName] = np.zeros((pre_size, post_size))
         self.proj_delays[projName] = np.zeros((pre_size, post_size))
     
     print_v("New projection: %s, %s->%s, weights? %s, type: %s"%(projName, prePop, postPop, weight, proj_type))
def create_arbor_cell(cell, nl_network, gid):

    if cell.arbor_cell=='cable_cell':

        default_tree = arbor.segment_tree()
        radius = evaluate(cell.parameters['radius'], nl_network.parameters) if 'radius' in cell.parameters else 3

        default_tree.append(arbor.mnpos, arbor.mpoint(-1*radius, 0, 0, radius), arbor.mpoint(radius, 0, 0, radius), tag=1)

        labels = arbor.label_dict({'soma':   '(tag 1)',
                                   'center': '(location 0 0.5)'})

        labels['root'] = '(root)'

        decor = arbor.decor()

        v_init = evaluate(cell.parameters['v_init'], nl_network.parameters) if 'v_init' in cell.parameters else -70
        decor.set_property(Vm=v_init)

        decor.paint('"soma"', cell.parameters['mechanism'])

        if gid==0:
            ic = arbor.iclamp( nl_network.parameters['input_del'], nl_network.parameters['input_dur'], nl_network.parameters['input_amp'])
            print_v("Stim: %s"%ic)
            decor.place('"center"', ic)

        decor.place('"center"', arbor.spike_detector(-10))


        # (2) Mark location for synapse at the midpoint of branch 1 (the first dendrite).
        labels['synapse_site'] = '(location 0 0.5)'
        # (4) Attach a single synapse.
        decor.place('"synapse_site"', 'expsyn')

        default_cell = arbor.cable_cell(default_tree, labels, decor)

        print_v("Created a new cell for gid %i: %s"%(gid,cell))
        print_v("%s"%(default_cell))

        return default_cell
Beispiel #8
0
    def add_input_source(self, input_source, network):
        input_params = input_source.parameters if input_source.parameters else {}

        for ip in input_params:
            input_params[ip] = evaluate(input_params[ip], network.parameters)
        """ This is a quick hack to support noisyCurrentSource before that type is integrated into the 
            core of NeuroML...
            TODO: remove when integrated!
        """
        if input_source.lems_source_file and "noisyCurrentSource" in input_source.id:
            pynn_input_params = {}
            for p in input_params:
                if p == "delay":
                    pynn_input_params["start"] = convert_to_units(
                        input_params[p], "ms")
                elif p == "duration":
                    pynn_input_params["stop"] = convert_to_units(
                        input_params[p], "ms") + convert_to_units(
                            input_params["delay"], "ms")
                elif p == "mean":
                    pynn_input_params["mean"] = convert_to_units(
                        input_params[p], "nA")
                elif p == "stdev":
                    pynn_input_params["stdev"] = convert_to_units(
                        input_params[p], "nA")
                elif p == "noiseDt":
                    pynn_input_params["dt"] = convert_to_units(
                        input_params[p], "ms")
                else:
                    raise Exception(
                        "Parameter %s=%s is not appropriate for inout %s" %
                        (p, input_params[p], input_source.id))

            exec(
                'self.input_sources["%s"] = self.sim.NoisyCurrentSource(**pynn_input_params)'
                % (input_source.id))
        else:
            exec('self.input_sources["%s"] = self.sim.%s(**input_params)' %
                 (input_source.id, input_source.pynn_input))
    def handle_population(self, 
                          population_id, 
                          component, size=-1, 
                          component_obj=None, 
                          properties={}):
        
        sizeInfo = " as yet unspecified size"
        if size>=0:
            sizeInfo = ", size: "+ str(size)+ " cells"
        if component_obj:
            compInfo = " (%s)"%component_obj.__class__.__name__
        else:
            compInfo=""
            
        print_v("Population: "+population_id+", component: "+component+compInfo+sizeInfo)
        
        if size>=0:
            for i in range(size):
                node_id = '%s_%i'%(population_id, i)
                node = {}
                node['type'] = {}
                node['name'] = node_id
                #node['type']['NeuroML'] = component
                
                comp = self.nl_network.get_child(component, 'cells')
                base_dir = './' # for now...
                fname = locate_file(comp.lems_source_file, base_dir)
                model = lems.Model()
                model.import_from_file(fname)
                lems_comp = model.components.get(component)
                print('Cell: [%s] comes from %s and in Lems is: %s'%(comp,fname, lems_comp))
                comp_type = lems_comp.type
                
                type = "The type of %s"%comp_type
                function = "The function of %s"%comp_type
                
                if comp_type == 'pnlLinearFunctionTM':
                    function = 'Linear'
                    type = "TransferMechanism" 
                elif comp_type == 'inputNode':
                    function = 'Linear'
                    type = "TransferMechanism"
                elif comp_type == 'pnlLogisticFunctionTM':
                    function = 'Logistic'
                    type = "TransferMechanism"
                elif comp_type == 'pnlExponentialFunctionTM':
                    function = 'Exponential'
                    type = "TransferMechanism"
                elif comp_type == 'pnlSimpleIntegratorMechanism':
                    function = 'SimpleIntegrator'
                    type = "IntegratorMechanism"
                
                
                
                node['type']["PNL"] = type
                node['type']["generic"] = None
                node['parameters'] = {}
                node['parameters']['PNL'] = {}
                node['functions'] = []
                func_info = {}
                func_info['type']={}
                func_info['type']['generic']=function
                func_info['name']='Function_%s'%function
                func_info['args']={}
                for p in lems_comp.parameters:
                    func_info['args'][p] = {}
                    func_info['args'][p]['type'] = 'float'
                    func_info['args'][p]['source'] = '%s.input_ports.%s'%(node_id,p)
                    
                    if comp.parameters is not None and p in comp.parameters:
                        func_info['args'][p]['value'] = evaluate(comp.parameters[p], self.nl_network.parameters)
                    else:
                        func_info['args'][p]['value'] = evaluate(lems_comp.parameters[p]) # evaluate to ensure strings -> ints/floats etc
                
                node['functions'].append(func_info)
                self.bids_mdf_graph['nodes'][node_id] = node

            pop_node_id = '%s'%(population_id)
            pop_node = {}
            pop_node['type'] = {}
            pop_node['name'] = pop_node_id
            pop_node['type']['NeuroML'] = component
            pop_node['parameters'] = {}
            pop_node['parameters']['size'] = size
            pop_node['functions'] = {}
            self.bids_mdf_graph_hl['nodes'][pop_node_id] = pop_node
Beispiel #10
0
 def _get_pop_size(self, pop_id):
     pop = self.network.get_child(pop_id, 'populations')
     return evaluate(pop.size, self.network.parameters)
Beispiel #11
0
    def create_arbor_cell(self, cell, gid, pop_id, index):

        if cell.arbor_cell == "cable_cell":

            default_tree = arbor.segment_tree()
            radius = (evaluate(cell.parameters["radius"],
                               self.nl_network.parameters)
                      if "radius" in cell.parameters else 3)

            default_tree.append(
                arbor.mnpos,
                arbor.mpoint(-1 * radius, 0, 0, radius),
                arbor.mpoint(radius, 0, 0, radius),
                tag=1,
            )

            labels = arbor.label_dict({
                "soma": "(tag 1)",
                "center": "(location 0 0.5)"
            })

            labels["root"] = "(root)"

            decor = arbor.decor()

            v_init = (evaluate(cell.parameters["v_init"],
                               self.nl_network.parameters)
                      if "v_init" in cell.parameters else -70)
            decor.set_property(Vm=v_init)

            decor.paint('"soma"', arbor.density(cell.parameters["mechanism"]))

            decor.place('"center"', arbor.spike_detector(0), "detector")

            for ip in self.input_info:
                if self.input_info[ip][0] == pop_id:
                    print_v("Stim: %s (%s) being placed on %s" %
                            (ip, self.input_info[ip], pop_id))
                    for il in self.input_lists[ip]:
                        cellId, segId, fract, weight = il
                        if cellId == index:
                            if self.input_info[ip][
                                    1] == 'i_clamp':  # TODO: remove hardcoding of this...
                                ic = arbor.iclamp(
                                    self.nl_network.parameters["input_del"],
                                    self.nl_network.parameters["input_dur"],
                                    self.nl_network.parameters["input_amp"],
                                )
                                print_v("Stim: %s on %s" % (ic, gid))
                                decor.place('"center"', ic, "iclamp")

            # (2) Mark location for synapse at the midpoint of branch 1 (the first dendrite).
            labels["synapse_site"] = "(location 0 0.5)"
            # (4) Attach a single synapse.
            decor.place('"synapse_site"', arbor.synapse("expsyn"), "syn")

            default_cell = arbor.cable_cell(default_tree, labels, decor)

            print_v("Created a new cell for gid %i: %s" % (gid, cell))
            print_v("%s" % (default_cell))

            return default_cell
Beispiel #12
0
def generate_neuroml2_from_network(nl_model,
                                   nml_file_name=None,
                                   print_summary=True,
                                   seed=1234,
                                   format='xml',
                                   base_dir=None,
                                   copy_included_elements=False,
                                   target_dir=None,
                                   validate=False):
    """
    Generate and save NeuroML2 file (in either XML or HDF5 format) from the 
    NeuroMLlite description
    """

    print_v("Generating NeuroML2 for %s%s..." %
            (nl_model.id, ' (base dir: %s; target dir: %s)' %
             (base_dir, target_dir) if base_dir or target_dir else ''))

    import neuroml
    from neuroml.hdf5.NetworkBuilder import NetworkBuilder

    neuroml_handler = NetworkBuilder()

    generate_network(nl_model, neuroml_handler, seed=seed, base_dir=base_dir)

    nml_doc = neuroml_handler.get_nml_doc()

    for i in nl_model.input_sources:

        if nml_doc.get_by_id(i.id) == None:
            if i.neuroml2_source_file:

                incl = neuroml.IncludeType(
                    _locate_file(i.neuroml2_source_file, base_dir))
                if not incl in nml_doc.includes:
                    nml_doc.includes.append(incl)

            if i.neuroml2_input:
                input_params = i.parameters if i.parameters else {}

                # TODO make more generic...

                if i.neuroml2_input.lower() == 'pulsegenerator':
                    input = neuroml.PulseGenerator(id=i.id)
                    nml_doc.pulse_generators.append(input)

                elif i.neuroml2_input.lower() == 'pulsegeneratordl':
                    input = neuroml.PulseGeneratorDL(id=i.id)
                    nml_doc.pulse_generator_dls.append(input)

                elif i.neuroml2_input.lower() == 'poissonfiringsynapse':
                    input = neuroml.PoissonFiringSynapse(id=i.id)
                    nml_doc.poisson_firing_synapses.append(input)

                for p in input_params:
                    exec('input.%s = "%s"' %
                         (p, evaluate(input_params[p], nl_model.parameters)))

    for c in nl_model.cells:
        if c.neuroml2_source_file:

            incl = neuroml.IncludeType(
                _locate_file(c.neuroml2_source_file, base_dir))
            found_cell = False
            for cell in nml_doc.cells:
                if cell.id == c.id:
                    nml_doc.cells.remove(
                        cell
                    )  # Better to use imported cell file; will have channels, etc.
                    nml_doc.includes.append(incl)
                    found_cell = True

            if not found_cell:
                for p in nl_model.populations:
                    if p.component == c.id:
                        pass

            if not incl in nml_doc.includes:
                nml_doc.includes.append(incl)
        '''  Needed???
        if c.lems_source_file:      
            incl = neuroml.IncludeType(_locate_file(c.lems_source_file, base_dir))
            if not incl in nml_doc.includes:
                nml_doc.includes.append(incl)'''

        if c.neuroml2_cell:

            cell_params = c.parameters if c.parameters else {}
            # TODO make more generic...
            if c.neuroml2_cell.lower() == 'spikegenerator':
                cell = neuroml.SpikeGenerator(id=c.id)
                nml_doc.spike_generators.append(cell)
            elif c.neuroml2_cell.lower() == 'spikegeneratorpoisson':
                cell = neuroml.SpikeGeneratorPoisson(id=c.id)
                nml_doc.spike_generator_poissons.append(cell)
            elif c.neuroml2_cell.lower() == 'spikegeneratorrefpoisson':
                cell = neuroml.SpikeGeneratorRefPoisson(id=c.id)
                nml_doc.spike_generator_ref_poissons.append(cell)
            else:
                raise Exception(
                    'The neuroml2_cell: %s is not yet supported...' %
                    c.neuroml2_cell)

            for p in cell_params:
                exec('cell.%s = "%s"' %
                     (p, evaluate(cell_params[p], nl_model.parameters)))

    for s in nl_model.synapses:
        if nml_doc.get_by_id(s.id) == None:
            if s.neuroml2_source_file:
                incl = neuroml.IncludeType(
                    _locate_file(s.neuroml2_source_file, base_dir))
                if not incl in nml_doc.includes:
                    nml_doc.includes.append(incl)

    # Look for and add the PyNN based elements to the NeuroMLDocument
    _extract_pynn_components_to_neuroml(nl_model, nml_doc)

    if print_summary:
        # Print info
        print_v(nml_doc.summary())

    # Save to file
    if target_dir == None:
        target_dir = base_dir
    if format == 'xml':
        if not nml_file_name:
            nml_file_name = _locate_file('%s.net.nml' % nml_doc.id, target_dir)
        from neuroml.writers import NeuroMLWriter
        NeuroMLWriter.write(nml_doc, nml_file_name)

    if format == 'hdf5':
        if not nml_file_name:
            nml_file_name = _locate_file('%s.net.nml.h5' % nml_doc.id,
                                         target_dir)
        from neuroml.writers import NeuroMLHdf5Writer
        NeuroMLHdf5Writer.write(nml_doc, nml_file_name)

    print_v("Written NeuroML to %s" % nml_file_name)

    if validate and format == 'xml':

        from pyneuroml import pynml
        success = pynml.validate_neuroml2(nml_file_name,
                                          verbose_validate=False)
        if success:
            print_v('Generated file is valid NeuroML2!')
        else:
            print_v('Generated file is NOT valid NeuroML2!')

    return nml_file_name, nml_doc
Beispiel #13
0
    def handle_projection(
        self,
        projName,
        prePop,
        postPop,
        synapse,
        hasWeights=False,
        hasDelays=False,
        type="projection",
        synapse_obj=None,
        pre_synapse_obj=None,
    ):

        shape = self.EXC_CONN_ARROW_SHAPE
        line = "solid"

        # Could be used in a network with no explicit connections, but weight
        # between populations set at high level projection element in NeuroMLlite
        projection_weight = 1.0

        if not (prePop in self.pop_sizes and postPop in self.pop_sizes):
            print_v("Ignoring projection, as one of pops empty...")
            return

        self.proj_pre_pops[projName] = prePop
        self.proj_post_pops[projName] = postPop
        self.proj_types[projName] = type

        if prePop in self.pop_types:
            if "I" in self.pop_types[prePop]:
                shape = self.INH_CONN_ARROW_SHAPE

        if type == "electricalProjection":
            shape = self.GAP_CONN_ARROW_SHAPE
            line = "dashed"

        if type == "continuousProjection":
            shape = self.CONT_CONN_ARROW_SHAPE
            line = "solid"

        if synapse_obj:
            self.proj_syn_objs[projName] = synapse_obj
            erev = self.get_reversal_potential_mV(synapse_obj)
            if erev != None and erev < self.CUTOFF_INH_SYN_MV:
                shape = self.INH_CONN_ARROW_SHAPE

        if self.nl_network:
            syn = self.nl_network.get_child(synapse, "synapses")
            if syn:
                if syn.parameters:
                    if ("e_rev" in syn.parameters and
                            syn.parameters["e_rev"] < self.CUTOFF_INH_SYN_MV):
                        shape = self.INH_CONN_ARROW_SHAPE

            proj = self.nl_network.get_child(projName, "projections")
            if proj:
                if proj.weight:
                    proj_weight = evaluate(proj.weight,
                                           self.nl_network.parameters,
                                           self.rng)
                    if proj_weight < 0:
                        shape = self.INH_CONN_ARROW_SHAPE
                    projection_weight = abs(proj_weight)

        self.proj_weights[projName] = projection_weight
        self.proj_shapes[projName] = shape
        self.proj_lines[projName] = line
        self.proj_conns[projName] = 0
        self.proj_tot_weight[projName] = 0

        if self.is_cell_level():
            pre_size = self.pop_sizes[prePop]
            post_size = self.pop_sizes[postPop]
            self.proj_individual_weights[projName] = np.zeros(
                (pre_size, post_size))
            self.proj_individual_conn_numbers[projName] = np.zeros(
                (pre_size, post_size))
            self.proj_individual_scaled_weights[projName] = np.zeros(
                (pre_size, post_size))
            self.proj_delays[projName] = np.zeros((pre_size, post_size))
Beispiel #14
0
def generate(ref="Example6_PyNN", add_inputs=True):

    ################################################################################
    ###   Build new network

    net = Network(id=ref, notes="Another network for PyNN - work in progress...")

    net.parameters = {
        "N_scaling": 0.005,
        "layer_height": 400,
        "width": 100,
        "depth": 100,
        "input_weight": 0.1,
    }

    cell = Cell(id="CorticalCell", pynn_cell="IF_curr_exp")
    cell.parameters = {
        "cm": 0.25,  # nF
        "i_offset": 0.0,  # nA
        "tau_m": 10.0,  # ms
        "tau_refrac": 2.0,  # ms
        "v_reset": -65.0,  # mV
        "v_rest": -65.0,  # mV
        "v_thresh": -50.0,  # mV
    }
    net.cells.append(cell)

    if add_inputs:
        input_cell = Cell(id="InputCell", pynn_cell="SpikeSourcePoisson")
        input_cell.parameters = {"start": 0, "duration": 10000000000, "rate": 150}
        net.cells.append(input_cell)

    e_syn = Synapse(
        id="ampa",
        pynn_receptor_type="excitatory",
        pynn_synapse_type="curr_exp",
        parameters={"tau_syn": 0.5},
    )
    net.synapses.append(e_syn)
    i_syn = Synapse(
        id="gaba",
        pynn_receptor_type="inhibitory",
        pynn_synapse_type="curr_exp",
        parameters={"tau_syn": 0.5},
    )
    net.synapses.append(i_syn)

    N_full = {
        "L23": {"E": 20683, "I": 5834},
        "L4": {"E": 21915, "I": 5479},
        "L5": {"E": 4850, "I": 1065},
        "L6": {"E": 14395, "I": 2948},
    }

    scale = 0.1

    pops = []
    input_pops = []
    pop_dict = {}

    layers = ["L23"]
    layers = ["L23", "L4", "L5", "L6"]

    for l in layers:

        i = 3 - layers.index(l)
        r = RectangularRegion(
            id=l,
            x=0,
            y=i * net.parameters["layer_height"],
            z=0,
            width=net.parameters["width"],
            height=net.parameters["layer_height"],
            depth=net.parameters["depth"],
        )
        net.regions.append(r)

        for t in ["E", "I"]:

            try:
                import opencortex.utils.color as occ

                if l == "L23":
                    if t == "E":
                        color = occ.L23_PRINCIPAL_CELL
                    if t == "I":
                        color = occ.L23_INTERNEURON
                if l == "L4":
                    if t == "E":
                        color = occ.L4_PRINCIPAL_CELL
                    if t == "I":
                        color = occ.L4_INTERNEURON
                if l == "L5":
                    if t == "E":
                        color = occ.L5_PRINCIPAL_CELL
                    if t == "I":
                        color = occ.L5_INTERNEURON
                if l == "L6":
                    if t == "E":
                        color = occ.L6_PRINCIPAL_CELL
                    if t == "I":
                        color = occ.L6_INTERNEURON

            except:
                color = ".8 0 0" if t == "E" else "0 0 1"

            pop_id = "%s_%s" % (l, t)
            pops.append(pop_id)
            ref = "l%s%s" % (l[1:], t.lower())
            exec(
                ref
                + " = Population(id=pop_id, size='int(%s*N_scaling)'%N_full[l][t], component=cell.id, properties={'color':color, 'type':t})"
            )
            exec("%s.random_layout = RandomLayout(region = r.id)" % ref)
            exec("net.populations.append(%s)" % ref)
            exec("pop_dict['%s'] = %s" % (pop_id, ref))

            if add_inputs:
                color = ".8 .8 .8"
                input_id = "%s_%s_input" % (l, t)
                input_pops.append(input_id)
                input_ref = "l%s%s_i" % (l[1:], t.lower())
                exec(
                    input_ref
                    + " = Population(id=input_id, size='int(%s*N_scaling)'%N_full[l][t], component=input_cell.id, properties={'color':color})"
                )
                exec("%s.random_layout = RandomLayout(region = r.id)" % input_ref)
                exec("net.populations.append(%s)" % input_ref)

        # l23i = Population(id='L23_I', size=int(100*scale), component=cell.id, properties={'color':})
        # l23ei = Population(id='L23_E_input', size=int(100*scale), component=input_cell.id)
        # l23ii = Population(id='L23_I_input', size=int(100*scale), component=input_cell.id)

    # net.populations.append(l23e)
    # net.populations.append(l23ei)
    # net.populations.append(l23i)
    # net.populations.append(l23ii)

    conn_probs = [
        [0.1009, 0.1689, 0.0437, 0.0818, 0.0323, 0.0, 0.0076, 0.0],
        [0.1346, 0.1371, 0.0316, 0.0515, 0.0755, 0.0, 0.0042, 0.0],
        [0.0077, 0.0059, 0.0497, 0.135, 0.0067, 0.0003, 0.0453, 0.0],
        [0.0691, 0.0029, 0.0794, 0.1597, 0.0033, 0.0, 0.1057, 0.0],
        [0.1004, 0.0622, 0.0505, 0.0057, 0.0831, 0.3726, 0.0204, 0.0],
        [0.0548, 0.0269, 0.0257, 0.0022, 0.06, 0.3158, 0.0086, 0.0],
        [0.0156, 0.0066, 0.0211, 0.0166, 0.0572, 0.0197, 0.0396, 0.2252],
        [0.0364, 0.001, 0.0034, 0.0005, 0.0277, 0.008, 0.0658, 0.1443],
    ]

    if add_inputs:
        for p in pops:
            proj = Projection(
                id="proj_input_%s" % p,
                presynaptic="%s_input" % p,
                postsynaptic=p,
                synapse=e_syn.id,
                delay=2,
                weight="input_weight",
            )
            proj.one_to_one_connector = OneToOneConnector()
            net.projections.append(proj)

    for pre_i in range(len(pops)):
        for post_i in range(len(pops)):
            pre = pops[pre_i]
            post = pops[post_i]
            prob = conn_probs[post_i][pre_i]  #######   TODO: check!!!!
            weight = 1
            syn = e_syn
            if prob > 0:
                if "I" in pre:
                    weight = -1
                    syn = i_syn
                proj = Projection(
                    id="proj_%s_%s" % (pre, post),
                    presynaptic=pre,
                    postsynaptic=post,
                    synapse=syn.id,
                    delay=1,
                    weight=weight,
                )
                proj.random_connectivity = RandomConnectivity(probability=prob)
                net.projections.append(proj)

    print(net.to_json())
    new_file = net.to_json_file("%s.json" % net.id)

    ################################################################################
    ###   Build Simulation object & save as JSON

    record_traces = {}
    record_spikes = {}

    from neuromllite.utils import evaluate

    for p in pops:
        forecast_size = evaluate(pop_dict[p].size, net.parameters)
        record_traces[p] = list(range(min(2, forecast_size)))
        record_spikes[p] = "*"
    for ip in input_pops:
        record_spikes[ip] = "*"

    sim = Simulation(
        id="Sim%s" % net.id,
        network=new_file,
        duration="100",
        dt="0.025",
        seed=1234,
        record_traces=record_traces,
        record_spikes=record_spikes,
    )

    sim.to_json_file()

    return sim, net
Beispiel #15
0
    def handle_population(self,
                          population_id,
                          component,
                          size=-1,
                          component_obj=None,
                          properties={}):

        sizeInfo = " as yet unspecified size"
        if size >= 0:
            sizeInfo = ", size: " + str(size) + " cells"
        if component_obj:
            compInfo = " (%s)" % component_obj.__class__.__name__
        else:
            compInfo = ""

        print_v("Population: " + population_id + ", component: " + component +
                compInfo + sizeInfo)

        if size >= 0:
            for i in range(size):
                node_id = '%s_%i' % (population_id, i)
                node = {}
                #node['type'] = {}
                #node['name'] = node_id
                #node['type']['NeuroML'] = component

                comp = self.nl_network.get_child(component, 'cells')
                base_dir = './'  # for now...

                node['parameters'] = {}
                node['input_ports'] = {}
                node['output_ports'] = {}
                if comp is not None and comp.lems_source_file:
                    fname = locate_file(comp.lems_source_file, base_dir)

                    model = MDFHandler._load_lems_file_with_neuroml2_types(
                        fname)

                    #print('All comp types: %s'%model.component_types.keys())
                    #print('All comps: %s'%model.components.keys())
                    lems_comp = model.components.get(component)
                    comp_type_name = lems_comp.type
                    lems_comp_type = model.component_types.get(comp_type_name)
                    notes = 'Cell: [%s] is defined in %s and in Lems is: %s' % (
                        comp, fname, lems_comp)

                    node['notes'] = notes

                    for p in lems_comp.parameters:
                        node['parameters'][p] = {
                            'value':
                            get_value_in_si(evaluate(lems_comp.parameters[p]))
                        }

                    for c in lems_comp_type.constants:
                        node['parameters'][c.name] = {
                            'value': get_value_in_si(c.value)
                        }

                    for dv in lems_comp_type.dynamics.derived_variables:

                        if dv.name == 'INPUT':
                            node['input_ports'][dv.name] = {}
                        else:
                            if dv.exposure:
                                #<DerivedVariable name="OUTPUT" dimension="none" exposure="OUTPUT" value="variable"/>
                                node['output_ports'][dv.exposure] = {
                                    'value': self._convert_value(dv.value)
                                }

                    for sv in lems_comp_type.dynamics.state_variables:
                        node['parameters'][sv.name] = {}

                    print(dir(lems_comp_type.dynamics))

                    for os in lems_comp_type.dynamics.event_handlers:
                        if type(os) == lems.OnStart:
                            for a in os.actions:
                                if type(a) == lems.StateAssignment:
                                    node['parameters'][a.variable][
                                        'default_initial_value'] = a.value

                    for td in lems_comp_type.dynamics.time_derivatives:
                        node['parameters'][td.variable][
                            'time_derivative'] = self._convert_value(td.value)

                if self.pnl_additions:
                    node['type']["PNL"] = type
                    node['type']["generic"] = None
                #node['parameters']['PNL'] = {}
                '''
                node['functions'] = []
                func_info = {}

                if self.pnl_additions:
                    func_info['type']={}
                    func_info['type']['generic']=function
                func_info['name']='Function_%s'%function
                func_info['args']={}
                for p in lems_comp.parameters:
                    func_info['args'][p] = {}
                    func_info['args'][p]['type'] = 'float'
                    func_info['args'][p]['source'] = '%s.input_ports.%s'%(node_id,p)

                    if comp.parameters is not None and p in comp.parameters:
                        func_info['args'][p]['value'] = evaluate(comp.parameters[p], self.nl_network.parameters)
                    else:
                        func_info['args'][p]['value'] = evaluate(lems_comp.parameters[p]) # evaluate to ensure strings -> ints/floats etc

                node['functions'].append(func_info)'''
                self.mdf_graph['nodes'][node_id] = node
            '''
Beispiel #16
0
def generate_network(nl_model,
                     handler,
                     seed=1234,
                     always_include_props=False,
                     include_connections=True,
                     include_inputs=True,
                     base_dir=None):
    """
    Generate the network model as described in NeuroMLlite in a specific handler,
    e.g. NeuroMLHandler, PyNNHandler, etc.
    """

    pop_locations = {}
    cell_objects = {}
    synapse_objects = {}

    print_v("Starting net generation for %s%s..." %
            (nl_model.id, ' (base dir: %s)' % base_dir if base_dir else ''))
    rng = random.Random(seed)

    if nl_model.network_reader:

        exec('from neuromllite.%s import %s' %
             (nl_model.network_reader.type, nl_model.network_reader.type))

        exec('network_reader = %s()' % (nl_model.network_reader.type))
        network_reader.parameters = nl_model.network_reader.parameters

        network_reader.parse(handler)
        pop_locations = network_reader.get_locations()

    else:
        from neuromllite import __version__ as nmlv
        notes = "Generated by NeuroMLlite v%s" % nmlv
        notes += "\n    Generated network: %s" % nl_model.id
        notes += "\n    Generation seed: %i" % (seed)
        if nl_model.parameters:
            notes += "\n    NeuroMLlite parameters: "
            for p in sorted(nl_model.parameters.keys()):
                notes += "\n        %s = %s" % (p, nl_model.parameters[p])
        handler.handle_document_start(nl_model.id, notes)
        temperature = '%sdegC' % nl_model.temperature if nl_model.temperature else None
        handler.handle_network(nl_model.id,
                               nl_model.notes,
                               temperature=temperature)

    nml2_doc_temp = _extract_pynn_components_to_neuroml(nl_model)

    for c in nl_model.cells:

        if c.neuroml2_source_file:
            from pyneuroml import pynml
            nml2_doc = pynml.read_neuroml2_file(_locate_file(
                c.neuroml2_source_file, base_dir),
                                                include_includes=True)
            cell_objects[c.id] = nml2_doc.get_by_id(c.id)

        if c.pynn_cell:
            cell_objects[c.id] = nml2_doc_temp.get_by_id(c.id)

    for s in nl_model.synapses:
        if s.neuroml2_source_file:
            from pyneuroml import pynml
            nml2_doc = pynml.read_neuroml2_file(_locate_file(
                s.neuroml2_source_file, base_dir),
                                                include_includes=True)
            synapse_objects[s.id] = nml2_doc.get_by_id(s.id)

        if s.pynn_synapse:
            synapse_objects[s.id] = nml2_doc_temp.get_by_id(s.id)

    for p in nl_model.populations:

        size = evaluate(p.size, nl_model.parameters)
        properties = p.properties if p.properties else {}

        if p.random_layout:
            properties['region'] = p.random_layout.region
        if p.relative_layout:
            properties['region'] = p.relative_layout.region

        if not p.random_layout and not p.single_location and not p.relative_layout and not always_include_props:

            # If there are no positions (abstract network), and <property>
            # is added to <population>, jLems doesn't like it... (it has difficulty
            # interpreting pop0[0]/v, etc.)
            # So better not to give properties...
            properties = {}

        if p.notes:
            handler.handle_population(p.id,
                                      p.component,
                                      size,
                                      cell_objects[p.component]
                                      if p.component in cell_objects else None,
                                      properties=properties,
                                      notes=p.notes)
        else:
            handler.handle_population(p.id,
                                      p.component,
                                      size,
                                      cell_objects[p.component]
                                      if p.component in cell_objects else None,
                                      properties=properties)

        pop_locations[p.id] = np.zeros((size, 3))

        for i in range(size):
            if p.random_layout:
                region = nl_model.get_child(p.random_layout.region, 'regions')

                x = region.x + rng.random() * region.width
                y = region.y + rng.random() * region.height
                z = region.z + rng.random() * region.depth
                pop_locations[p.id][i] = (x, y, z)

                handler.handle_location(i, p.id, p.component, x, y, z)

            if p.single_location:
                loc = p.single_location.location
                x = loc.x
                y = loc.y
                z = loc.z
                pop_locations[p.id][i] = (x, y, z)

                handler.handle_location(i, p.id, p.component, x, y, z)

            if p.relative_layout:
                print_v("Generating population with layout: %s" %
                        p.relative_layout)
                region = nl_model.get_child(p.relative_layout.region,
                                            'regions')
                x = p.relative_layout.x + region.x
                y = p.relative_layout.y + region.y
                z = p.relative_layout.z + region.z
                pop_locations[p.id][i] = (x, y, z)
                handler.handle_location(i, p.id, p.component, x, y, z)

        if hasattr(handler, 'finalise_population'):
            handler.finalise_population(p.id)

    if include_connections:
        for p in nl_model.projections:

            type = p.type if p.type else 'projection'

            delay = evaluate(p.delay, nl_model.parameters) if p.delay else 0
            weight = evaluate(p.weight, nl_model.parameters) if p.weight else 1

            if weight != 0:
                handler.handle_projection(
                    p.id,
                    p.presynaptic,
                    p.postsynaptic,
                    p.synapse,
                    synapse_obj=synapse_objects[p.synapse]
                    if p.synapse in synapse_objects else None,
                    pre_synapse_obj=synapse_objects[p.pre_synapse]
                    if p.pre_synapse in synapse_objects else None,
                    type=type)

                conn_count = 0
                if p.random_connectivity:
                    for pre_i in range(len(pop_locations[p.presynaptic])):
                        for post_i in range(len(
                                pop_locations[p.postsynaptic])):
                            flip = rng.random()
                            #print("Is cell %i conn to %i, prob %s - %s"%(pre_i, post_i, flip, p.random_connectivity.probability))
                            if flip < p.random_connectivity.probability:
                                #print_v("Adding connection %i with weight: %s, delay: %s"%(conn_count, weight, delay))
                                handler.handle_connection(p.id,
                                                          conn_count,
                                                          p.presynaptic,
                                                          p.postsynaptic,
                                                          p.synapse, \
                                                          pre_i, \
                                                          post_i, \
                                                          preSegId=0, \
                                                          preFract=0.5, \
                                                          postSegId=0, \
                                                          postFract=0.5, \
                                                          delay=delay, \
                                                          weight=weight)
                                conn_count += 1

                if p.convergent_connectivity:

                    for post_i in range(len(pop_locations[p.postsynaptic])):

                        for count in range(
                                int(p.convergent_connectivity.num_per_post)):
                            found = False
                            while not found:
                                pre_i = int(rng.random() *
                                            len(pop_locations[p.presynaptic]))
                                if p.presynaptic == p.postsynaptic and pre_i == post_i:
                                    found = False
                                else:
                                    found = True

                            print_v(
                                "Adding connection %i (%i->%i; %i to %s of post) with weight: %s, delay: %s"
                                % (conn_count, pre_i, post_i, count,
                                   p.convergent_connectivity.num_per_post,
                                   weight, delay))
                            handler.handle_connection(p.id,
                                                      conn_count,
                                                      p.presynaptic,
                                                      p.postsynaptic,
                                                      p.synapse, \
                                                      pre_i, \
                                                      post_i, \
                                                      preSegId=0, \
                                                      preFract=0.5, \
                                                      postSegId=0, \
                                                      postFract=0.5, \
                                                      delay=delay, \
                                                      weight=weight)
                            conn_count += 1

                elif p.one_to_one_connector:
                    for i in range(
                            min(len(pop_locations[p.presynaptic]),
                                len(pop_locations[p.postsynaptic]))):

                        #print_v("Adding connection %i with weight: %s, delay: %s"%(conn_count, weight, delay))
                        handler.handle_connection(p.id,
                                                  conn_count,
                                                  p.presynaptic,
                                                  p.postsynaptic,
                                                  p.synapse, \
                                                  i, \
                                                  i, \
                                                  preSegId=0, \
                                                  preFract=0.5, \
                                                  postSegId=0, \
                                                  postFract=0.5, \
                                                  delay=delay, \
                                                  weight=weight)
                        conn_count += 1

                handler.finalise_projection(p.id, p.presynaptic,
                                            p.postsynaptic, p.synapse)

    if include_inputs:
        for input in nl_model.inputs:

            handler.handle_input_list(input.id,
                                      input.population,
                                      input.input_source,
                                      size=0,
                                      input_comp_obj=None)

            input_count = 0
            for i in range(len(pop_locations[input.population])):
                flip = rng.random()
                weight = input.weight if input.weight else 1
                if flip * 100. < input.percentage:

                    number_per_cell = evaluate(
                        input.number_per_cell,
                        nl_model.parameters) if input.number_per_cell else 1

                    for j in range(number_per_cell):
                        handler.handle_single_input(input.id,
                                                    input_count,
                                                    i,
                                                    weight=evaluate(
                                                        weight,
                                                        nl_model.parameters))
                        input_count += 1

            handler.finalise_input_source(input.id)

    if hasattr(handler, 'finalise_document'):
        handler.finalise_document()
Beispiel #17
0
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
Beispiel #18
0
def generate(ref='Example6_PyNN', add_inputs=True):

    ################################################################################
    ###   Build new network

    net = Network(id=ref,
                  notes='Another network for PyNN - work in progress...')

    net.parameters = {
        'N_scaling': 0.005,
        'layer_height': 400,
        'width': 100,
        'depth': 100,
        'input_weight': 0.1
    }

    cell = Cell(id='CorticalCell', pynn_cell='IF_curr_exp')
    cell.parameters = {
        'cm': 0.25,  # nF
        'i_offset': 0.0,  # nA
        'tau_m': 10.0,  # ms
        'tau_refrac': 2.0,  # ms
        'v_reset': -65.0,  # mV
        'v_rest': -65.0,  # mV
        'v_thresh': -50.0  # mV
    }
    net.cells.append(cell)

    if add_inputs:
        input_cell = Cell(id='InputCell', pynn_cell='SpikeSourcePoisson')
        input_cell.parameters = {
            'start': 0,
            'duration': 10000000000,
            'rate': 150
        }
        net.cells.append(input_cell)

    e_syn = Synapse(id='ampa',
                    pynn_receptor_type='excitatory',
                    pynn_synapse_type='curr_exp',
                    parameters={'tau_syn': 0.5})
    net.synapses.append(e_syn)
    i_syn = Synapse(id='gaba',
                    pynn_receptor_type='inhibitory',
                    pynn_synapse_type='curr_exp',
                    parameters={'tau_syn': 0.5})
    net.synapses.append(i_syn)

    N_full = {
        'L23': {
            'E': 20683,
            'I': 5834
        },
        'L4': {
            'E': 21915,
            'I': 5479
        },
        'L5': {
            'E': 4850,
            'I': 1065
        },
        'L6': {
            'E': 14395,
            'I': 2948
        }
    }

    scale = 0.1

    pops = []
    input_pops = []
    pop_dict = {}

    layers = ['L23']
    layers = ['L23', 'L4', 'L5', 'L6']

    for l in layers:

        i = 3 - layers.index(l)
        r = RectangularRegion(id=l,
                              x=0,
                              y=i * net.parameters['layer_height'],
                              z=0,
                              width=net.parameters['width'],
                              height=net.parameters['layer_height'],
                              depth=net.parameters['depth'])
        net.regions.append(r)

        for t in ['E', 'I']:

            try:
                import opencortex.utils.color as occ
                if l == 'L23':
                    if t == 'E': color = occ.L23_PRINCIPAL_CELL
                    if t == 'I': color = occ.L23_INTERNEURON
                if l == 'L4':
                    if t == 'E': color = occ.L4_PRINCIPAL_CELL
                    if t == 'I': color = occ.L4_INTERNEURON
                if l == 'L5':
                    if t == 'E': color = occ.L5_PRINCIPAL_CELL
                    if t == 'I': color = occ.L5_INTERNEURON
                if l == 'L6':
                    if t == 'E': color = occ.L6_PRINCIPAL_CELL
                    if t == 'I': color = occ.L6_INTERNEURON

            except:
                color = '.8 0 0' if t == 'E' else '0 0 1'

            pop_id = '%s_%s' % (l, t)
            pops.append(pop_id)
            ref = 'l%s%s' % (l[1:], t.lower())
            exec(
                ref +
                " = Population(id=pop_id, size='int(%s*N_scaling)'%N_full[l][t], component=cell.id, properties={'color':color, 'type':t})"
            )
            exec("%s.random_layout = RandomLayout(region = r.id)" % ref)
            exec("net.populations.append(%s)" % ref)
            exec("pop_dict['%s'] = %s" % (pop_id, ref))

            if add_inputs:
                color = '.8 .8 .8'
                input_id = '%s_%s_input' % (l, t)
                input_pops.append(input_id)
                input_ref = 'l%s%s_i' % (l[1:], t.lower())
                exec(
                    input_ref +
                    " = Population(id=input_id, size='int(%s*N_scaling)'%N_full[l][t], component=input_cell.id, properties={'color':color})"
                )
                exec("%s.random_layout = RandomLayout(region = r.id)" %
                     input_ref)
                exec("net.populations.append(%s)" % input_ref)

        #l23i = Population(id='L23_I', size=int(100*scale), component=cell.id, properties={'color':})
        #l23ei = Population(id='L23_E_input', size=int(100*scale), component=input_cell.id)
        #l23ii = Population(id='L23_I_input', size=int(100*scale), component=input_cell.id)

    #net.populations.append(l23e)
    #net.populations.append(l23ei)
    #net.populations.append(l23i)
    #net.populations.append(l23ii)

    conn_probs = [
        [0.1009, 0.1689, 0.0437, 0.0818, 0.0323, 0., 0.0076, 0.],
        [0.1346, 0.1371, 0.0316, 0.0515, 0.0755, 0., 0.0042, 0.],
        [0.0077, 0.0059, 0.0497, 0.135, 0.0067, 0.0003, 0.0453, 0.],
        [0.0691, 0.0029, 0.0794, 0.1597, 0.0033, 0., 0.1057, 0.],
        [0.1004, 0.0622, 0.0505, 0.0057, 0.0831, 0.3726, 0.0204, 0.],
        [0.0548, 0.0269, 0.0257, 0.0022, 0.06, 0.3158, 0.0086, 0.],
        [0.0156, 0.0066, 0.0211, 0.0166, 0.0572, 0.0197, 0.0396, 0.2252],
        [0.0364, 0.001, 0.0034, 0.0005, 0.0277, 0.008, 0.0658, 0.1443]
    ]

    if add_inputs:
        for p in pops:
            proj = Projection(id='proj_input_%s' % p,
                              presynaptic='%s_input' % p,
                              postsynaptic=p,
                              synapse=e_syn.id,
                              delay=2,
                              weight='input_weight')
            proj.one_to_one_connector = OneToOneConnector()
            net.projections.append(proj)

    for pre_i in range(len(pops)):
        for post_i in range(len(pops)):
            pre = pops[pre_i]
            post = pops[post_i]
            prob = conn_probs[post_i][pre_i]  #######   TODO: check!!!!
            weight = 1
            syn = e_syn
            if prob > 0:
                if 'I' in pre:
                    weight = -1
                    syn = i_syn
                proj = Projection(id='proj_%s_%s' % (pre, post),
                                  presynaptic=pre,
                                  postsynaptic=post,
                                  synapse=syn.id,
                                  delay=1,
                                  weight=weight)
                proj.random_connectivity = RandomConnectivity(probability=prob)
                net.projections.append(proj)

    print(net.to_json())
    new_file = net.to_json_file('%s.json' % net.id)

    ################################################################################
    ###   Build Simulation object & save as JSON

    recordTraces = {}
    recordSpikes = {}

    from neuromllite.utils import evaluate
    for p in pops:
        forecast_size = evaluate(pop_dict[p].size, net.parameters)
        recordTraces[p] = list(range(min(2, forecast_size)))
        recordSpikes[p] = '*'
    for ip in input_pops:
        recordSpikes[ip] = '*'

    sim = Simulation(id='Sim%s' % net.id,
                     network=new_file,
                     duration='100',
                     dt='0.025',
                     seed=1234,
                     recordTraces=recordTraces,
                     recordSpikes=recordSpikes)

    sim.to_json_file()

    return sim, net
Beispiel #19
0
def generate_and_run(simulation,
                     simulator,
                     network=None,
                     return_results=False,
                     base_dir=None,
                     target_dir=None,
                     num_processors=1):
    """
    Generates the network in the specified simulator and runs, if appropriate
    """

    if network == None:
        network = load_network_json(simulation.network)

    print_v("Generating network %s and running in simulator: %s..." %
            (network.id, simulator))

    if simulator == 'NEURON':

        _generate_neuron_files_from_neuroml(network,
                                            dir_for_mod_files=target_dir)

        from neuromllite.NeuronHandler import NeuronHandler

        nrn_handler = NeuronHandler()

        for c in network.cells:
            if c.neuroml2_source_file:
                src_dir = os.path.dirname(
                    os.path.abspath(c.neuroml2_source_file))
                nrn_handler.executeHoc('load_file("%s/%s.hoc")' %
                                       (src_dir, c.id))

        generate_network(network, nrn_handler, generate_network, base_dir)
        if return_results:
            raise NotImplementedError(
                "Reloading results not supported in Neuron yet...")

    elif simulator.lower() == 'sonata':  # Will not "run" obviously...

        from neuromllite.SonataHandler import SonataHandler

        sonata_handler = SonataHandler()

        generate_network(network,
                         sonata_handler,
                         always_include_props=True,
                         base_dir=base_dir)

        print_v("Done with Sonata...")

    elif simulator.lower().startswith('graph'):  # Will not "run" obviously...

        from neuromllite.GraphVizHandler import GraphVizHandler, engines

        try:
            if simulator[-1].isalpha():
                engine = engines[simulator[-1]]
                level = int(simulator[5:-1])
            else:
                engine = 'dot'
                level = int(simulator[5:])

        except Exception as e:
            print_v("Error parsing: %s: %s" % (simulator, e))
            print_v(
                "Graphs of the network structure can be generated at many levels of detail (1-6, required) and laid out using GraphViz engines (d - dot (default); c - circo; n - neato; f - fdp), so use: -graph3c, -graph2, -graph4f etc."
            )
            return

        handler = GraphVizHandler(level, engine=engine, nl_network=network)

        generate_network(network,
                         handler,
                         always_include_props=True,
                         base_dir=base_dir)

        print_v("Done with GraphViz...")

    elif simulator.lower().startswith('matrix'):  # Will not "run" obviously...

        from neuromllite.MatrixHandler import MatrixHandler

        try:
            level = int(simulator[6:])
        except:
            print_v("Error parsing: %s" % simulator)
            print_v(
                "Matrices of the network structure can be generated at many levels of detail (1-n, required), so use: -matrix1, -matrix2, etc."
            )
            return

        handler = MatrixHandler(level, nl_network=network)

        generate_network(network,
                         handler,
                         always_include_props=True,
                         base_dir=base_dir)

        print_v("Done with MatrixHandler...")

    elif simulator.startswith('PyNN'):

        #_generate_neuron_files_from_neuroml(network)
        simulator_name = simulator.split('_')[1].lower()

        from neuromllite.PyNNHandler import PyNNHandler

        pynn_handler = PyNNHandler(simulator_name, simulation.dt, network.id)

        syn_cell_params = {}
        for proj in network.projections:

            synapse = network.get_child(proj.synapse, 'synapses')
            post_pop = network.get_child(proj.postsynaptic, 'populations')

            if not post_pop.component in syn_cell_params:
                syn_cell_params[post_pop.component] = {}
            for p in synapse.parameters:
                post = ''
                if synapse.pynn_receptor_type == "excitatory":
                    post = '_E'
                elif synapse.pynn_receptor_type == "inhibitory":
                    post = '_I'
                syn_cell_params[post_pop.component][
                    '%s%s' % (p, post)] = synapse.parameters[p]

        cells = {}
        for c in network.cells:
            if c.pynn_cell:
                cell_params = {}
                if c.parameters:
                    for p in c.parameters:
                        cell_params[p] = evaluate(c.parameters[p],
                                                  network.parameters)

                dont_set_here = [
                    'tau_syn_E', 'e_rev_E', 'tau_syn_I', 'e_rev_I'
                ]
                for d in dont_set_here:
                    if d in c.parameters:
                        raise Exception(
                            'Synaptic parameters like %s should be set ' +
                            'in individual synapses, not in the list of parameters associated with the cell'
                            % d)
                if c.id in syn_cell_params:
                    cell_params.update(syn_cell_params[c.id])
                print_v("Creating cell with params: %s" % cell_params)
                exec('cells["%s"] = pynn_handler.sim.%s(**cell_params)' %
                     (c.id, c.pynn_cell))

                if c.pynn_cell != 'SpikeSourcePoisson':
                    exec(
                        "cells['%s'].default_initial_values['v'] = cells['%s'].parameter_space['v_rest'].base_value"
                        % (c.id, c.id))

        pynn_handler.set_cells(cells)

        receptor_types = {}
        for s in network.synapses:
            if s.pynn_receptor_type:
                receptor_types[s.id] = s.pynn_receptor_type

        pynn_handler.set_receptor_types(receptor_types)

        for input_source in network.input_sources:
            if input_source.pynn_input:
                pynn_handler.add_input_source(input_source)

        generate_network(network,
                         pynn_handler,
                         always_include_props=True,
                         base_dir=base_dir)

        for pid in pynn_handler.populations:
            pop = pynn_handler.populations[pid]
            if 'all' in simulation.recordTraces or pop.label in simulation.recordTraces:
                if pop.can_record('v'):
                    pop.record('v')

        pynn_handler.sim.run(simulation.duration)
        pynn_handler.sim.end()

        traces = {}
        events = {}

        if not 'NeuroML' in simulator:
            from neo.io import PyNNTextIO

            for pid in pynn_handler.populations:
                pop = pynn_handler.populations[pid]

                if 'all' in simulation.recordTraces or pop.label in simulation.recordTraces:

                    filename = "%s.%s.v.dat" % (simulation.id, pop.label)
                    all_columns = []
                    print_v("Writing data for %s to %s" %
                            (pop.label, filename))
                    for i in range(len(pop)):
                        if pop.can_record('v'):
                            ref = '%s[%i]' % (pop.label, i)
                            traces[ref] = []
                            data = pop.get_data('v', gather=False)
                            for segment in data.segments:
                                vm = segment.analogsignals[0].transpose()[i]

                                if len(all_columns) == 0:
                                    tt = np.array([
                                        t * simulation.dt / 1000.
                                        for t in range(len(vm))
                                    ])
                                    all_columns.append(tt)
                                vm_si = [float(v / 1000.) for v in vm]
                                traces[ref] = vm_si
                                all_columns.append(vm_si)

                            times_vm = np.array(all_columns).transpose()

                    np.savetxt(filename, times_vm, delimiter='\t', fmt='%s')

        if return_results:
            _print_result_info(traces, events)
            return traces, events

    elif simulator == 'NetPyNE':

        if target_dir == None:
            target_dir = './'

        _generate_neuron_files_from_neuroml(network,
                                            dir_for_mod_files=target_dir)

        from netpyne import specs
        from netpyne import sim
        # Note NetPyNE from this branch is required: https://github.com/Neurosim-lab/netpyne/tree/neuroml_updates
        from netpyne.conversion.neuromlFormat import NetPyNEBuilder

        import pprint
        pp = pprint.PrettyPrinter(depth=6)

        netParams = specs.NetParams()
        simConfig = specs.SimConfig()
        netpyne_handler = NetPyNEBuilder(netParams,
                                         simConfig=simConfig,
                                         verbose=True)

        generate_network(network, netpyne_handler, base_dir=base_dir)

        netpyne_handler.finalise()

        simConfig = specs.SimConfig()
        simConfig.tstop = simulation.duration
        simConfig.duration = simulation.duration
        simConfig.dt = simulation.dt
        simConfig.seed = simulation.seed
        simConfig.recordStep = simulation.dt

        simConfig.recordCells = ['all']
        simConfig.recordTraces = {}

        for pop in netpyne_handler.popParams.values():
            if 'all' in simulation.recordTraces or pop.id in simulation.recordTraces:
                for i in pop['cellsList']:
                    id = pop['pop']
                    index = i['cellLabel']
                    simConfig.recordTraces['v_%s_%s' % (id, index)] = {
                        'sec': 'soma',
                        'loc': 0.5,
                        'var': 'v',
                        'conds': {
                            'pop': id,
                            'cellLabel': index
                        }
                    }

        simConfig.saveDat = True

        print_v("NetPyNE netParams: ")
        pp.pprint(netParams.todict())
        #print_v("NetPyNE simConfig: ")
        #pp.pprint(simConfig.todict())

        sim.initialize(
            netParams,
            simConfig)  # create network object and set cfg and net params

        sim.net.createPops()
        cells = sim.net.createCells(
        )  # instantiate network cells based on defined populations

        for proj_id in netpyne_handler.projection_infos.keys():
            projName, prePop, postPop, synapse, ptype = netpyne_handler.projection_infos[
                proj_id]
            print_v("Creating connections for %s (%s): %s->%s via %s" %
                    (projName, ptype, prePop, postPop, synapse))

            preComp = netpyne_handler.pop_ids_vs_components[prePop]

            for conn in netpyne_handler.connections[projName]:

                pre_id, pre_seg, pre_fract, post_id, post_seg, post_fract, delay, weight = conn

                #connParam = {'delay':delay,'weight':weight,'synsPerConn':1, 'sec':post_seg, 'loc':post_fract, 'threshold':threshold}
                connParam = {
                    'delay': delay,
                    'weight': weight,
                    'synsPerConn': 1,
                    'sec': post_seg,
                    'loc': post_fract
                }

                if ptype == 'electricalProjection':

                    if weight != 1:
                        raise Exception(
                            'Cannot yet support inputs where weight !=1!')
                    connParam = {
                        'synsPerConn': 1,
                        'sec': post_seg,
                        'loc': post_fract,
                        'gapJunction': True,
                        'weight': weight
                    }
                else:
                    connParam = {
                        'delay': delay,
                        'weight': weight,
                        'synsPerConn': 1,
                        'sec': post_seg,
                        'loc': post_fract
                    }
                    #'threshold': threshold}

                connParam['synMech'] = synapse

                if post_id in sim.net.gid2lid:  # check if postsyn is in this node's list of gids
                    sim.net._addCellConn(connParam, pre_id, post_id)

        stims = sim.net.addStims(
        )  # add external stimulation to cells (IClamps etc)
        simData = sim.setupRecording(
        )  # setup variables to record for each cell (spikes, V traces, etc)
        sim.runSim()  # run parallel Neuron simulation
        sim.gatherData()  # gather spiking data and cell info from each node
        sim.saveData(
        )  # save params, cell info and sim output to file (pickle,mat,txt,etc)

        if return_results:
            raise NotImplementedError(
                "Reloading results not supported in NetPyNE yet...")

    elif simulator == 'jNeuroML' or simulator == 'jNeuroML_NEURON' or simulator == 'jNeuroML_NetPyNE':

        from pyneuroml.lems import generate_lems_file_for_neuroml
        from pyneuroml import pynml

        lems_file_name = 'LEMS_%s.xml' % simulation.id

        nml_file_name, nml_doc = generate_neuroml2_from_network(
            network, base_dir=base_dir, target_dir=target_dir)
        included_files = ['PyNN.xml']

        for c in network.cells:
            if c.lems_source_file:
                included_files.append(c.lems_source_file)
        '''
        if network.cells:
            for c in network.cells:
                included_files.append(c.neuroml2_source_file)
        '''
        if network.synapses:
            for s in network.synapses:
                if s.lems_source_file:
                    included_files.append(s.lems_source_file)

        print_v("Generating LEMS file prior to running in %s" % simulator)

        pops_plot_save = []
        pops_spike_save = []
        gen_plots_for_quantities = {}
        gen_saves_for_quantities = {}

        for p in network.populations:

            if simulation.recordTraces and ('all' in simulation.recordTraces or
                                            p.id in simulation.recordTraces):
                pops_plot_save.append(p.id)

            if simulation.recordSpikes and ('all' in simulation.recordSpikes or
                                            p.id in simulation.recordSpikes):
                pops_spike_save.append(p.id)

            if simulation.recordRates and ('all' in simulation.recordRates
                                           or p.id in simulation.recordRates):
                size = evaluate(p.size, network.parameters)
                for i in range(size):
                    quantity = '%s/%i/%s/r' % (p.id, i, p.component)
                    gen_plots_for_quantities['%s_%i_r' %
                                             (p.id, i)] = [quantity]
                    gen_saves_for_quantities['%s_%i.r.dat' %
                                             (p.id, i)] = [quantity]

            if simulation.recordVariables:
                for var in simulation.recordVariables:
                    to_rec = simulation.recordVariables[var]
                    if ('all' in to_rec or p.id in to_rec):
                        size = evaluate(p.size, network.parameters)
                        for i in range(size):
                            quantity = '%s/%i/%s/%s' % (p.id, i, p.component,
                                                        var)
                            gen_plots_for_quantities['%s_%i_%s' %
                                                     (p.id, i, var)] = [
                                                         quantity
                                                     ]
                            gen_saves_for_quantities['%s_%i.%s.dat' %
                                                     (p.id, i, var)] = [
                                                         quantity
                                                     ]

        generate_lems_file_for_neuroml(
            simulation.id,
            nml_file_name,
            network.id,
            simulation.duration,
            simulation.dt,
            lems_file_name,
            target_dir=target_dir if target_dir else '.',
            nml_doc=
            nml_doc,  # Use this if the nml doc has already been loaded (to avoid delay in reload)
            include_extra_files=included_files,
            gen_plots_for_all_v=False,
            plot_all_segments=False,
            gen_plots_for_quantities=
            gen_plots_for_quantities,  # Dict with displays vs lists of quantity paths
            gen_plots_for_only_populations=
            pops_plot_save,  # List of populations, all pops if = []
            gen_saves_for_all_v=False,
            save_all_segments=False,
            gen_saves_for_only_populations=
            pops_plot_save,  # List of populations, all pops if = []
            gen_saves_for_quantities=
            gen_saves_for_quantities,  # Dict with file names vs lists of quantity paths
            gen_spike_saves_for_all_somas=False,
            gen_spike_saves_for_only_populations=
            pops_spike_save,  # List of populations, all pops if = []
            gen_spike_saves_for_cells=
            {},  # Dict with file names vs lists of quantity paths
            spike_time_format='ID_TIME',
            copy_neuroml=True,
            lems_file_generate_seed=12345,
            report_file_name='report.%s.txt' % simulation.id,
            simulation_seed=simulation.seed if simulation.seed else 12345,
            verbose=True)

        lems_file_name = _locate_file(lems_file_name, target_dir)

        if simulator == 'jNeuroML':
            results = pynml.run_lems_with_jneuroml(
                lems_file_name,
                nogui=True,
                load_saved_data=return_results,
                reload_events=return_results)
        elif simulator == 'jNeuroML_NEURON':
            results = pynml.run_lems_with_jneuroml_neuron(
                lems_file_name,
                nogui=True,
                load_saved_data=return_results,
                reload_events=return_results)
        elif simulator == 'jNeuroML_NetPyNE':
            results = pynml.run_lems_with_jneuroml_netpyne(
                lems_file_name,
                nogui=True,
                verbose=True,
                load_saved_data=return_results,
                reload_events=return_results,
                num_processors=num_processors)

        print_v("Finished running LEMS file %s in %s (returning results: %s)" %
                (lems_file_name, simulator, return_results))

        if return_results:
            traces, events = results
            _print_result_info(traces, events)
            return results  # traces, events =
    def handle_population(self,
                          population_id,
                          component,
                          size=-1,
                          component_obj=None,
                          properties={}):

        sizeInfo = " as yet unspecified size"
        if size >= 0:
            sizeInfo = ", size: " + str(size) + " cells"
        if component_obj:
            compInfo = " (%s)" % component_obj.__class__.__name__
        else:
            compInfo = ""

        print_v("Population: " + population_id + ", component: " + component +
                compInfo + sizeInfo)

        if size >= 0:
            for i in range(size):
                node_id = "%s_%i" % (population_id, i)
                node = {}
                node["type"] = {}
                node["name"] = node_id
                # node['type']['NeuroML'] = component

                comp = self.nl_network.get_child(component, "cells")
                base_dir = "./"  # for now...
                fname = locate_file(comp.lems_source_file, base_dir)
                model = lems.Model()
                model.import_from_file(fname)
                lems_comp = model.components.get(component)
                print("Cell: [%s] comes from %s and in Lems is: %s" %
                      (comp, fname, lems_comp))
                comp_type = lems_comp.type

                type = "The type of %s" % comp_type
                function = "The function of %s" % comp_type

                if comp_type == "pnlLinearFunctionTM":
                    function = "Linear"
                    type = "TransferMechanism"
                elif comp_type == "inputNode":
                    function = "Linear"
                    type = "TransferMechanism"
                elif comp_type == "pnlLogisticFunctionTM":
                    function = "Logistic"
                    type = "TransferMechanism"
                elif comp_type == "pnlExponentialFunctionTM":
                    function = "Exponential"
                    type = "TransferMechanism"
                elif comp_type == "pnlSimpleIntegratorMechanism":
                    function = "SimpleIntegrator"
                    type = "IntegratorMechanism"
                elif comp_type == "fnCell":
                    function = "FitzHughNagumoIntegrator"
                    type = "IntegratorMechanism"

                node["type"]["PNL"] = type
                node["type"]["generic"] = None
                node["parameters"] = {}
                node["parameters"]["PNL"] = {}
                node["functions"] = []
                func_info = {}
                func_info["type"] = {}
                func_info["type"]["generic"] = function
                func_info["name"] = "Function_%s" % function
                func_info["args"] = {}
                for p in lems_comp.parameters:
                    func_info["args"][p] = {}
                    func_info["args"][p]["type"] = "float"
                    func_info["args"][p]["source"] = "%s.input_ports.%s" % (
                        node_id, p)

                    if comp.parameters is not None and p in comp.parameters:
                        func_info["args"][p]["value"] = evaluate(
                            comp.parameters[p], self.nl_network.parameters)
                    else:
                        func_info["args"][p]["value"] = evaluate(
                            lems_comp.parameters[p]
                        )  # evaluate to ensure strings -> ints/floats etc

                node["functions"].append(func_info)
                self.bids_mdf_graph["nodes"][node_id] = node

            pop_node_id = "%s" % (population_id)
            pop_node = {}
            pop_node["type"] = {}
            pop_node["name"] = pop_node_id
            pop_node["type"]["NeuroML"] = component
            pop_node["parameters"] = {}
            pop_node["parameters"]["size"] = size
            pop_node["functions"] = {}
            self.bids_mdf_graph_hl["nodes"][pop_node_id] = pop_node