def build_lems_for_model(src):
    model = lems.Model()

    model.add(lems.Dimension('time', t=1))
    # model.add(lems.Dimension('au'))

    # primary element of the model is a mass model component
    mass = lems.ComponentType(src.name, extends="baseCellMembPot")
    model.add(mass)
    
    ######### Adding v is required to ease mapping to NEURON...
    mass.dynamics.add(lems.StateVariable(name="v", dimension="voltage", exposure="v"))
    
    mass.add(lems.Attachments(name="synapses",type_="basePointCurrentDL"))
    

    for input in src.input:
        mass.dynamics.add(lems.DerivedVariable(name=input, 
                                               dimension='none',
                                               exposure=input,
                                               select='synapses[*]/I',
                                               reduce='add'))
        mass.add(lems.Exposure(input, 'none'))
        
        
    for key, val in src.const.items():
        mass.add(lems.Parameter(key, 'none'))  # TODO units
        
    
        
    mass.add(lems.Constant(name="MSEC", dimension="time", value="1ms"))
    mass.add(lems.Constant(name="PI", dimension="none", value="3.14159265359"))

    states = []
    der_vars = []
    # for key in src.param:
    #     mass.add(lems.Parameter(key, 'au'))  # TODO units

    for key, val in src.auxex:
        val = val.replace('**', '^')
        mass.dynamics.add(lems.DerivedVariable(key, value=val))

    for key in src.obsrv:
        name_dv = key.replace('(','_').replace(')','').replace(' - ','_min_')
        mass.dynamics.add(lems.DerivedVariable(name_dv, value=key, exposure=name_dv))
        mass.add(lems.Exposure(name_dv, 'none'))

    for src_svar in src.state_space:
        name = src_svar.name
        ddt = src_svar.drift.replace('**', '^')
        mass.dynamics.add(lems.StateVariable(name, 'none', name))
        mass.dynamics.add(lems.TimeDerivative(name, '(%s)/MSEC'%ddt))
        mass.add(lems.Exposure(name, 'none'))
        
    ''' On condition is not need on the model but NeuroML requires its definition -->
            <OnCondition test="r .lt. 0">
                <EventOut port="spike"/>
            </OnCondition>'''
            
    oc = lems.OnCondition(test='v .gt. 0')
    oc.actions.append(lems.EventOut(port='spike'))
    mass.dynamics.add(oc)

    return model
Example #2
0
model.add(lems.Unit('milliVolt', 'mV', 'voltage', -3))
model.add(lems.Unit('milliSecond', 'ms', 'time', -3))
model.add(lems.Unit('microFarad', 'uF', 'capacitance', -12))

iaf1 = lems.ComponentType('iaf1')
model.add(iaf1)

iaf1.add(lems.Parameter('threshold', 'voltage'))
iaf1.add(lems.Parameter('reset', 'voltage'))
iaf1.add(lems.Parameter('refractoryPeriod', 'time'))
iaf1.add(lems.Parameter('capacitance', 'capacitance'))
iaf1.add(lems.Exposure('vexp', 'voltage'))
dp = lems.DerivedParameter('range', 'threshold - reset', 'voltage')
iaf1.add(dp)

iaf1.dynamics.add(lems.StateVariable('v','voltage', 'vexp')) 
iaf1.dynamics.add(lems.DerivedVariable('v2',dimension='voltage', value='v*2'))
cdv = lems.ConditionalDerivedVariable('v_abs','voltage')
cdv.add(lems.Case('v .geq. 0','v'))
cdv.add(lems.Case('v .lt. 0','-1*v'))
iaf1.dynamics.add(cdv)


model.add(lems.Component('celltype_a', iaf1.name))
model.add(lems.Component('celltype_b', iaf1.name, threshold="20mV"))

fn = '/tmp/model.xml'
model.export_to_file(fn)

print("----------------------------------------------")
print(open(fn,'r').read())
Example #3
0
    def add_neurongroup(self, neurongrp, index_neurongrp, initializers):
        """
        Add NeuronGroup to self._model
    
        If number of elements is 1 it adds component of that type, if
        it's bigger, the network is created by calling:
        `make_multiinstantiate`.

        Parameters
        ----------
        neurongrp : dict
            Standard dictionary representation of NeuronGroup object
        index_neurongrp : int
            Index of neurongroup in the network
        initializers : list
            List of initializers defined in the network
        """
        # get name of the neurongrp
        component_name = neurongrp['name']
        self._model_namespace["neuronname"] = component_name
        # add BASE_CELL component
        self._component_type = lems.ComponentType(component_name,
                                                  extends=BASE_CELL)
        # get identifiers attached to neurongrp and create special_properties dict
        identifiers = []
        special_properties = {}

        if 'identifiers' in neurongrp:
            identifiers = neurongrp['identifiers']

        for initializer in initializers:
            if 'identifiers' in initializer:
                identifiers.update(initializer['identifiers'])

        for identifier in identifiers.keys():
            special_properties.update({identifier: None})

        # add the identifers as properties of the component
        for param in self._determine_properties(identifiers):
            self._component_type.add(param)
        # common things for every neuron definition
        # TODO: Is this same for custom events too?
        self._component_type.add(lems.EventPort(name='spike', direction='out'))

        # dynamics of the network
        dynamics = lems.Dynamics()
        # get neurongrp equations
        equations = neurongrp['equations']
        # loop over the variables
        initializer_vars = []
        for initializer in initializers:
            if initializer['source'] == neurongrp['name']:
                initializer_vars.append(initializer['variable'])

        for var in equations.keys():

            # determine the dimension
            dimension = _determine_dimension(equations[var]['unit'])
            # add to all_params_unit
            self._all_params_unit[var] = dimension
            # identify diff eqns to add Exposure
            if equations[var]['type'] == 'differential equation':
                state_var = lems.StateVariable(var,
                                               dimension=dimension,
                                               exposure=var)
                self._component_type.add(
                    lems.Exposure(var, dimension=dimension))
                dynamics.add_state_variable(state_var)
            else:
                if var in initializer_vars and 'i' in initializer['value']:
                    self._component_type.add(lems.Property(var, dimension))
                    special_properties[var] = initializer['value']
                    continue
                state_var = lems.StateVariable(var, dimension=dimension)
                dynamics.add_state_variable(state_var)

        # what happens at initialization
        onstart = lems.OnStart()
        # loop over the initializers
        for var in equations.keys():
            if var in (NOT_REFRACTORY, LAST_SPIKE):
                continue
            # check the initializer is connected to this neurongrp
            if var not in initializer_vars:
                continue
            if var in special_properties:
                continue
            for initializer in initializers:
                if initializer['variable'] == var and initializer[
                        'source'] == neurongrp['name']:
                    init_value = initializer['value']
            if type(init_value) != str:
                value = brian_unit_to_lems(init_value)
            else:
                value = renderer.render_expr(str(init_value))
            # add to onstart
            onstart.add(lems.StateAssignment(var, value))
        dynamics.add(onstart)

        # check whether refractoriness is defined
        if ('events' in neurongrp and 'spike' in neurongrp['events']
                and 'refractory' in neurongrp['events']['spike']):
            # if refractoriness, we create separate regimes for integrating
            integr_regime = lems.Regime(INTEGRATING, dynamics,
                                        True)  # True -> initial regime
            # check spike event
            # NOTE: isn't refractory only for spike events?
            for spike_flag, on_cond in self._event_builder(
                    neurongrp['events']):
                if spike_flag:
                    # if spike occured we make transition to refractory regime
                    on_cond.add_action(lems.Transition(REFRACTORY))
                integr_regime.add_event_handler(on_cond)
            # add refractory regime
            refrac_regime = lems.Regime(REFRACTORY, dynamics)
            # make lastspike variable and initialize it
            refrac_regime.add_state_variable(
                lems.StateVariable(LAST_SPIKE, dimension='time'))
            oe = lems.OnEntry()
            oe.add(lems.StateAssignment(LAST_SPIKE, 't'))
            refrac_regime.add(oe)
            # after refractory time we make transition to integrating regime
            refractory_code = neurongrp['events']['spike']['refractory']
            if not _equation_separator(str(refractory_code)):
                # if there is no specific variable given, we assume
                # that this is time condition
                ref_oc = lems.OnCondition('t .gt. ( {0} + {1} )'.format(
                    LAST_SPIKE, brian_unit_to_lems(refractory_code)))
            else:
                ref_oc = lems.OnCondition(
                    renderer.render_expr(refractory_code))
            ref_trans = lems.Transition(INTEGRATING)
            ref_oc.add_action(ref_trans)
            refrac_regime.add_event_handler(ref_oc)
            # identify variables with differential equation
            for var in neurongrp['equations']:
                if neurongrp['equations'][var][
                        'type'] == 'differential equation':
                    diff_var = neurongrp['equations'][var]
                    # get the expression
                    td = lems.TimeDerivative(
                        var, renderer.render_expr(diff_var['expr']))
                    # check flags for UNLESS_REFRACTORY TODO: is this available in 'flags' key?
                    if 'flags' in diff_var:
                        # if unless refratory we add only do integration regime
                        if UNLESS_REFRACTORY in diff_var['flags']:
                            integr_regime.add_time_derivative(td)
                            continue
                    # add time derivative to both regimes
                    integr_regime.add_time_derivative(td)
                    refrac_regime.add_time_derivative(td)
            # add the regimes to dynamics
            dynamics.add_regime(integr_regime)
            dynamics.add_regime(refrac_regime)

        else:
            # adding events directly to dynamics
            for spike_flag, on_cond in self._event_builder(
                    neurongrp['events']):
                dynamics.add_event_handler(on_cond)
            # get variables with diff eqns
            for var in neurongrp['equations']:
                if neurongrp['equations'][var][
                        'type'] == 'differential equation':
                    diff_var = neurongrp['equations'][var]
                    td = lems.TimeDerivative(
                        var, renderer.render_expr(diff_var['expr']))
                    # add to dynamics
                    dynamics.add_time_derivative(td)
        # add dynamics to _component_type
        self._component_type.dynamics = dynamics
        # add _component_type to _model
        self._model.add_component_type(self._component_type)
        # get identifiers
        paramdict = dict()
        for ident_name, ident_value in neurongrp['identifiers'].items():
            paramdict[ident_name] = self._unit_lems_validator(ident_value)
        # if more than one neuron use multiinstantiate
        if neurongrp['N'] == 1:
            self._model.add(
                lems.Component("n{}".format(index_neurongrp), component_name,
                               **paramdict))
        else:
            self.make_multiinstantiate(special_properties, component_name,
                                       paramdict, neurongrp['N'])
Example #4
0
        name="rho",
        dimension="none",
        description="Related to the Rayleigh number, also named r elsewhere"))

lorenz.add(lems.Parameter(name="x0", dimension="none"))
lorenz.add(lems.Parameter(name="y0", dimension="none"))
lorenz.add(lems.Parameter(name="z0", dimension="none"))

lorenz.add(lems.Exposure(name="x", dimension="none"))
lorenz.add(lems.Exposure(name="y", dimension="none"))
lorenz.add(lems.Exposure(name="z", dimension="none"))

lorenz.add(lems.Constant(name="sec", value="1s", dimension="time"))

lorenz.dynamics.add(
    lems.StateVariable(name="x", dimension="none", exposure="x"))
lorenz.dynamics.add(
    lems.StateVariable(name="y", dimension="none", exposure="y"))
lorenz.dynamics.add(
    lems.StateVariable(name="z", dimension="none", exposure="z"))

lorenz.dynamics.add(
    lems.TimeDerivative(variable="x", value="( sigma * (y - x)) / sec"))
lorenz.dynamics.add(
    lems.TimeDerivative(variable="y", value="( rho * x - y - x * z ) / sec"))
lorenz.dynamics.add(
    lems.TimeDerivative(variable="z", value="( x * y - beta * z) / sec"))

onstart = lems.OnStart()
onstart.add(lems.StateAssignment(variable="x", value="x0"))
onstart.add(lems.StateAssignment(variable="y", value="y0"))
Example #5
0
    def add_neurongroup(self, obj, idx_of_ng, namespace, initializers):
        """
        Adds NeuronGroup object *obj* to self._model.

        If number of elements is 1 it adds component of that type, if
        it's bigger, the network is created by calling:
        `make_multiinstantiate`.

        Parameters
        ----------
        obj : brian2.NeuronGroup
            NeuronGroup object to parse
        idx_of_ng : int
            index of neurongroup
        namespace : dict
            dictionary with all neccassary variables definition
        initializers : dict
            initial values for all model variables
        """
        if hasattr(obj, "namespace") and not obj.namespace:
            obj.namespace = namespace
        self._nr_of_neurons = obj.N  # maybe not the most robust solution
        ct_name = "neuron{}".format(idx_of_ng+1)
        self._model_namespace["neuronname"] = ct_name
        self._component_type = lems.ComponentType(ct_name, extends=BASE_CELL)
        # adding parameters
        special_properties = {}
        for key in obj.namespace.keys():
            special_properties[key] = None
        for param in self._determine_properties(obj.namespace):
            self._component_type.add(param)
        # common things for every neuron definition
        self._component_type.add(lems.EventPort(name='spike', direction='out'))
        # dynamics of the network
        dynamics = lems.Dynamics()
        ng_equations = obj.user_equations
        for var in ng_equations:
            if ng_equations[var].type == DIFFERENTIAL_EQUATION:
                dim_ = _determine_dimension(ng_equations[var].unit)
                sv_ = lems.StateVariable(var, dimension=dim_, exposure=var)
                self._all_params_unit[var] = dim_
                dynamics.add_state_variable(sv_)
                self._component_type.add(lems.Exposure(var, dimension=dim_))
            elif ng_equations[var].type in (PARAMETER, SUBEXPRESSION):
                if var == NOT_REFRACTORY:
                    continue
                dim_ = _determine_dimension(ng_equations[var].unit)
                self._all_params_unit[var] = dim_
                # all initializers contatining iterator need to be assigned
                # as a property
                # i is default iterator in Brian2
                if var in initializers and "i" in get_identifiers(str(initializers[var])):
                    self._component_type.add(lems.Property(var, dim_))
                    special_properties[var] = initializers[var]
                    continue
                sv_ = lems.StateVariable(var, dimension=dim_)
                dynamics.add_state_variable(sv_)
        # what happens at initialization
        onstart = lems.OnStart()
        for var in obj.equations.names:
            if var in (NOT_REFRACTORY, LAST_SPIKE):
                continue
            if var not in initializers:
                continue
            if var in special_properties:
                continue
            init_value = initializers[var]
            if type(init_value) != str:
                init_value = brian_unit_to_lems(init_value)
            else:
                init_value = renderer.render_expr(str(init_value))
            onstart.add(lems.StateAssignment(var, init_value))
        dynamics.add(onstart)

        if obj._refractory:
            # if refractoriness, we create separate regimes
            # - for integrating
            integr_regime = lems.Regime(INTEGRATING, dynamics, True)  # True -> initial regime
            for spike_flag, oc in self._event_builder(obj.events, obj.event_codes):
                if spike_flag:
                    # if spike occured we make transition to refractory regime
                    oc.add_action(lems.Transition(REFRACTORY))
                integr_regime.add_event_handler(oc)
            # - for refractory
            refrac_regime = lems.Regime(REFRACTORY, dynamics)
            # we make lastspike variable and initialize it
            refrac_regime.add_state_variable(lems.StateVariable(LAST_SPIKE, dimension='time'))
            oe = lems.OnEntry()
            oe.add(lems.StateAssignment(LAST_SPIKE, 't'))
            refrac_regime.add(oe)
            # after time spiecified in _refractory we make transition
            # to integrating regime
            if not _equation_separator(str(obj._refractory)):
                # if there is no specific variable given, we assume
                # that this is time condition
                ref_oc = lems.OnCondition('t .gt. ( {0} + {1} )'.format(LAST_SPIKE, brian_unit_to_lems(obj._refractory)))
            else:
                ref_oc = lems.OnCondition(renderer.render_expr(obj._refractory))
            ref_trans = lems.Transition(INTEGRATING)
            ref_oc.add_action(ref_trans)
            refrac_regime.add_event_handler(ref_oc)
            for var in obj.user_equations.diff_eq_names:
                td = lems.TimeDerivative(var, renderer.render_expr(str(ng_equations[var].expr)))
                # if unless refratory we add only do integration regime
                if UNLESS_REFRACTORY in ng_equations[var].flags:
                    integr_regime.add_time_derivative(td)
                    continue
                integr_regime.add_time_derivative(td)
                refrac_regime.add_time_derivative(td)
            dynamics.add_regime(integr_regime)
            dynamics.add_regime(refrac_regime)
        else:
            # here we add events directly to dynamics
            for spike_flag, oc in self._event_builder(obj.events, obj.event_codes):
                dynamics.add_event_handler(oc)
            for var in obj.user_equations.diff_eq_names:
                td = lems.TimeDerivative(var, renderer.render_expr(str(ng_equations[var].expr)))
                dynamics.add_time_derivative(td)

        self._component_type.dynamics = dynamics
        # making componenttype is done so we add it to the model
        self._model.add_component_type(self._component_type)
        obj.namespace.pop("init", None)                # kick out init
        # adding component to the model
        paramdict = dict()
        for param in obj.namespace:
            paramdict[param] = self._unit_lems_validator(obj.namespace[param])
        if obj.N == 1:
            self._model.add(lems.Component("n{}".format(idx_of_ng), ct_name, **paramdict))
        else:
            self.make_multiinstantiate(special_properties, ct_name, paramdict)
Example #6
0
def mdf_to_neuroml(graph, save_to=None, format=None, run_duration_sec=2):

    print("Converting graph: %s to NeuroML" % (graph.id))

    net = neuromllite.Network(id=graph.id)
    net.notes = "NeuroMLlite export of {} graph: {}".format(
        format if format else "MDF",
        graph.id,
    )

    model = lems.Model()
    lems_definitions = "%s_lems_definitions.xml" % graph.id

    for node in graph.nodes:
        print("    Node: %s" % node.id)

        node_comp_type = "%s__definition" % node.id
        node_comp = "%s__instance" % node.id

        # Create the ComponentType which defines behaviour of the general class
        ct = lems.ComponentType(node_comp_type, extends="baseCellMembPotDL")
        ct.add(lems.Attachments("only_input_port", "basePointCurrentDL"))
        ct.dynamics.add(
            lems.DerivedVariable(name="V",
                                 dimension="none",
                                 value="0",
                                 exposure="V"))
        model.add(ct)

        # Define the Component - an instance of the ComponentType
        comp = lems.Component(node_comp, node_comp_type)
        model.add(comp)

        cell = neuromllite.Cell(id=node_comp,
                                lems_source_file=lems_definitions)
        net.cells.append(cell)

        pop = neuromllite.Population(
            id=node.id,
            size=1,
            component=cell.id,
            properties={
                "color": "0.2 0.2 0.2",
                "radius": 3
            },
        )
        net.populations.append(pop)

        if len(node.input_ports) > 1:
            raise Exception(
                "Currently only max 1 input port supported in NeuroML...")

        for ip in node.input_ports:
            ct.add(lems.Exposure(ip.id, "none"))
            ct.dynamics.add(
                lems.DerivedVariable(
                    name=ip.id,
                    dimension="none",
                    select="only_input_port[*]/I",
                    reduce="add",
                    exposure=ip.id,
                ))

        on_start = None

        for p in node.parameters:
            print("Converting %s" % p)
            if p.value is not None:
                try:
                    v_num = float(p.value)
                    ct.add(lems.Parameter(p.id, "none"))
                    comp.parameters[p.id] = v_num
                    print(comp.parameters[p.id])
                except Exception as e:

                    ct.add(lems.Exposure(p.id, "none"))
                    dv = lems.DerivedVariable(
                        name=p.id,
                        dimension="none",
                        value="%s" % (p.value),
                        exposure=p.id,
                    )
                    ct.dynamics.add(dv)

            elif p.function is not None:
                ct.add(lems.Exposure(p.id, "none"))
                func_info = mdf_functions[p.function]
                expr = func_info["expression_string"]
                expr2 = substitute_args(expr, p.args)
                for arg in p.args:
                    expr += ";{}={}".format(arg, p.args[arg])
                dv = lems.DerivedVariable(name=p.id,
                                          dimension="none",
                                          value="%s" % (expr2),
                                          exposure=p.id)
                ct.dynamics.add(dv)
            else:
                ct.add(lems.Exposure(p.id, "none"))
                ct.dynamics.add(
                    lems.StateVariable(name=p.id,
                                       dimension="none",
                                       exposure=p.id))
                if p.default_initial_value:
                    if on_start is None:
                        on_start = lems.OnStart()
                        ct.dynamics.add(on_start)
                    sa = lems.StateAssignment(
                        variable=p.id,
                        value=str(evaluate_expr(p.default_initial_value)))
                    on_start.actions.append(sa)

                if p.time_derivative:
                    td = lems.TimeDerivative(variable=p.id,
                                             value=p.time_derivative)
                    ct.dynamics.add(td)

        if len(node.output_ports) > 1:
            raise Exception(
                "Currently only max 1 output port supported in NeuroML...")

        for op in node.output_ports:
            ct.add(lems.Exposure(op.id, "none"))
            ct.dynamics.add(
                lems.DerivedVariable(name=op.id,
                                     dimension="none",
                                     value=op.value,
                                     exposure=op.id))
            only_output_port = "only_output_port"
            ct.add(lems.Exposure(only_output_port, "none"))
            ct.dynamics.add(
                lems.DerivedVariable(
                    name=only_output_port,
                    dimension="none",
                    value=op.id,
                    exposure=only_output_port,
                ))

    if len(graph.edges) > 0:

        model.add(
            lems.Include(
                os.path.join(os.path.dirname(__file__),
                             "syn_definitions.xml")))
        rsDL = neuromllite.Synapse(id="rsDL",
                                   lems_source_file=lems_definitions)
        net.synapses.append(rsDL)
        # syn_id = 'silentSyn'
        # silentSynDL = neuromllite.Synapse(id=syn_id, lems_source_file=lems_definitions)

    for edge in graph.edges:
        print(f"    Edge: {edge.id} connects {edge.sender} to {edge.receiver}")

        ssyn_id = "silentSyn_proj_%s" % edge.id
        ssyn_id = "silentSyn_proj_%s" % edge.id
        # ssyn_id = 'silentSynX'
        silentDLin = neuromllite.Synapse(id=ssyn_id,
                                         lems_source_file=lems_definitions)

        model.add(lems.Component(ssyn_id, "silentRateSynapseDL"))

        net.synapses.append(silentDLin)

        net.projections.append(
            neuromllite.Projection(
                id="proj_%s" % edge.id,
                presynaptic=edge.sender,
                postsynaptic=edge.receiver,
                synapse=rsDL.id,
                pre_synapse=silentDLin.id,
                type="continuousProjection",
                weight=1,
                random_connectivity=neuromllite.RandomConnectivity(
                    probability=1),
            ))

    # Much more todo...
    model.export_to_file(lems_definitions)

    print("Nml net: %s" % net)
    if save_to:
        new_file = net.to_json_file(save_to)
        print("Saved NML to: %s" % save_to)

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

    simtime = 1000 * run_duration_sec
    dt = 0.1
    sim = neuromllite.Simulation(
        id="Sim%s" % net.id,
        network=new_file,
        duration=simtime,
        dt=dt,
        seed=123,
        recordVariables={"OUTPUT": {
            "all": "*"
        }},
    )

    recordVariables = {}
    for node in graph.nodes:
        for ip in node.input_ports:
            if not ip.id in recordVariables:
                recordVariables[ip.id] = {}
            recordVariables[ip.id][node.id] = 0

        for p in node.parameters:
            if p.is_stateful():
                if not p.id in recordVariables:
                    recordVariables[p.id] = {}
                recordVariables[p.id][node.id] = 0

        for op in node.output_ports:
            if not op.id in recordVariables:
                recordVariables[op.id] = {}
            recordVariables[op.id][node.id] = 0

    sim.recordVariables = recordVariables
    if save_to:
        sf = sim.to_json_file()

        print("Saved Simulation to: %s" % sf)

    return net, sim