Beispiel #1
0
 def _build_tvb_to_nest_parameter_input(self, interface):
     # One interface for every combination NEST node
     # and TVB state variable to be transmitted
     # from TVB to NEST
     connections = interface["connections"]
     if isinstance(connections, string_types):
         connections = {connections: slice(None)}  # return all population types
     interfaces = IndexedOrderedDict(OrderedDict({}))
     default_parameter = NEST_INPUT_PARAMETERS[interface["model"]]
     for name, populations in connections.items():
         try:
             tvb_coupling_id = self.tvb_model.cvar.tolist().index(
                 self.tvb_model.state_variables.index(name))
         except:
             raise ValueError("Failed to compute the coupling index of TVB state variable %s!" % name)
         interfaces.update({name: TVBNESTParameterInterface(self.nest_instance,
                                                            name,
                                                            interface["model"],
                                                            interface.get("parameter", default_parameter),
                                                            OrderedDict({}),
                                                            tvb_coupling_id,
                                                            interface.get("sign", 1))})
         for node in self.nest_nodes.values():
             interfaces[name].update({node.label: node[populations]})
     return interfaces
Beispiel #2
0
def build_and_connect_devices(nest_instance, devices, nest_nodes):
    from six import string_types
    # Build devices by their model (IndexedOrderedDict),
    # target nodes (IndexedOrderedDict),
    # and population (IndexedOrderedDict) for faster reading
    nest_devices = IndexedOrderedDict(OrderedDict(
        {}))  # TODO: find out why it copies nest_nodes if not {} in the input
    for device in ensure_list(devices):
        dev_model = device["model"]
        if dev_model in NESTInputDeviceDict.keys():
            build_device = \
                lambda nest_instance, device, populations: \
                    build_and_connect_input_device(nest_instance, device, populations)
        elif dev_model in NESTOutputDeviceDict.keys():
            build_device = \
                lambda nest_instance, device, populations: \
                    build_and_connect_output_device(nest_instance, device, populations)
        else:
            raise ValueError(
                "Device model %s is neither one of available input devices:\n%s\n"
                "nor of output ones!:\n%s" %
                (dev_model, NESTInputDeviceDict.keys(),
                 NESTOutputDeviceDict.keys()))
        device_target_nodes = device.pop("nodes", None)
        if device_target_nodes is None:
            device_target_nodes = nest_nodes.values()
        else:
            device_target_nodes = nest_nodes[device_target_nodes]
        # Determine the connections from variables to measure/stimulate to NEST node populations
        connections = device["connections"]  # either a variable name or a dict
        if isinstance(connections, string_types):
            connections = {
                connections: slice(None)
            }  # return all population types
        # For every distinct quantity to be measured from NEST or stimulated towards NEST nodes...
        for name, populations in connections.items():
            # This set of devices will be for variable...
            nest_devices.update(
                {name: NESTDeviceSet(name, dev_model, OrderedDict({}))})
            # and for every target region node...
            for node in device_target_nodes:
                # and for every target node and population group...
                # create a device
                nest_devices[name].update({
                    node.label:
                    build_device(nest_instance, device, node[populations])
                })
    return nest_devices
Beispiel #3
0
 def _build_tvb_to_nest_input_devices(self, interface):
     # One NEST stimulation device for every combination of
     # TVB node and state variable to be transmitted from TVB to NEST
     model = interface["model"]
     connections = interface["connections"]
     sign = interface.get("sign", 1)
     if isinstance(connections, string_types):
         connections = {connections: slice(None)}  # return all population types
     interfaces = IndexedOrderedDict(OrderedDict({}))
     for name, populations in connections.items():
         try:
             tvb_sv_id = self.tvb_model.state_variables.index(name)
         except:
             raise ValueError("Failed to compute the index of TVB state variable %s!" % name)
         interfaces.update({name: TVBtoNESTinterface(name, model, tvb_sv_id=tvb_sv_id)})
         for tvb_id in self.tvb_nodes_ids:
             # Generate a device for every TVB node to be represented in NEST network...
             try:
                 input_device = build_input_device(self.nest_instance, interface, config=self.config)
             except:
                 raise ValueError("Failed to create NEST device %s!" % model)
             # ...and connect it to every NEST node
             for i_node, node in enumerate(self.nest_nodes.values()):
                 # ...with the corresponding weight, sign, and delay
                 weight = sign * self.connectivity.weights[self.nest_nodes_ids[i_node], tvb_id]
                 delay = self.assert_delay(
                     self.connectivity.delays[
                         self.nest_nodes_ids[i_node], tvb_id])
                 self.nest_instance.Connect(input_device.device, node[populations],
                                            syn_spec={"weight": weight, "delay": delay})
             input_device.update_number_of_connections()
             interfaces[name].update({self.connectivity.region_labels[tvb_id]: input_device})
     return interfaces
Beispiel #4
0
 def build_nest_to_tvb_interfaces(self):
     # One NEST output device for every combination of NEST mode
     # and TVB state variable/parameter to be transmitted
     # from NEST to TVB
     devices = build_and_connect_output_devices(self.nest_instance,
                                                self.nest_to_tvb_interfaces,
                                                self.nest_nodes)
     interfaces = IndexedOrderedDict(OrderedDict({}))
     for name, device_set in devices.items():
         try:
             tvb_sv_id = self.tvb_model.state_variables.index(name)
         except:
             tvb_sv_id = None  # it might be a TVB parameter, not a state variable
         interfaces.update({name: NESTtoTVBinterface(name, device_set.model, OrderedDict({})). \
                           from_device_set(device_set, tvb_sv_id)})
     return interfaces
Beispiel #5
0
 def __init__(self,
              nest_instance,
              node_ordered_dict=OrderedDict({}),
              label=""):
     super(NESTRegionNode, self).__init__(node_ordered_dict)
     self.nest_instance = nest_instance
     self.label = str(label)
Beispiel #6
0
 def GetStatus(self, attrs, nodes=None, return_values=False):
     if nodes is None or len(nodes) == 0:
         nodes = self._dict.keys()
     vals = OrderedDict({})
     for attr in ensure_list(attrs):
         this_attr = []
         for node in ensure_list(nodes):
             this_attr.append(getattr(self._dict[node], attr))
         vals.update({attr: this_attr})
     if return_values:
         vals = vals.values()
         if len(vals) == 1:
             return vals[0]
         else:
             return vals
     else:
         return vals
Beispiel #7
0
 def build_nest_nodes(self):
     self.nodes = IndexedOrderedDict(OrderedDict({}))
     for node_label in self.nest_nodes_labels:  # For every NEST node
         # ...generate a network of spiking population
         self.nodes.update(
             {node_label: self.build_nest_populations(node_label)})
         self.connect_nest_node_populations(
             self.nodes[node_label])  # ...and connect them
Beispiel #8
0
 def __init__(self, name="", model="", device_set=OrderedDict({})):
     self.name = str(name)
     self.model = str(model)
     if not (isinstance(device_set, dict) and np.all(
         [isinstance(device, NESTDevice)
          for device in device_set.values()])):
         raise ValueError(
             "Input device_set is not a IndexedOrderedDict of NESTDevice objects!:\n%s"
             % str(device_set))
     super(NESTDeviceSet, self).__init__(device_set)
     self.update_model()
     LOG.info("%s of model %s for %s created!" %
              (self.__class__, self.model, self.name))
Beispiel #9
0
 def build_nest_populations(self, label):
     # Generate a NEST spiking network population...
     node = NESTRegionNode(self.nest_instance, OrderedDict({}), label)
     for iP, (name, model, sizes, params) in \
             enumerate(zip(self.populations_names,
                           self.populations_models,
                           self.populations_sizes,
                           self.populations_params)):
         node.update(
             {name: self.nest_instance.Create(model, sizes, params=params)})
         # ...and connect it to itself:
         self.connect_population(node[name], iP)
     return node
Beispiel #10
0
 def __init__(self,
              nest_instance,
              name,
              model,
              parameter="",
              neurons=OrderedDict({}),
              tvb_coupling_id=0,
              sign=1):
     self.nest_instance = nest_instance
     if not (isinstance(neurons, dict) and np.all(
         [isinstance(neuron, tuple()) for neuron in neurons.values()])):
         raise ValueError(
             "Input neurons is not a IndexedOrderedDict of tuples!:\n" %
             str(neurons))
     super(TVBNESTParameterInterface, self).__init__(neurons)
     self.name = str(name)
     self.model = str(model)
     if self.model not in NEST_INPUT_PARAMETERS.keys():
         raise ValueError(
             "model %s is not one of the available parameter interfaces!" %
             self.model)
     self.parameter = str(parameter)
     if len(parameter) == 0:
         self.parameter = NEST_INPUT_PARAMETERS[self.model]
     else:
         if self.parameter != NEST_INPUT_PARAMETERS[self.model]:
             LOG.warning("Parameter %s is different to the default one %s "
                         "for parameter interface model %s" %
                         (self.parameter, NEST_INPUT_PARAMETERS[self.model],
                          self.model))
     self.tvb_coupling_id = int(tvb_coupling_id)
     self.sign = int(sign)
     if sign not in [-1, 1]:
         raise ValueError("Sign %s is neither 1 nor -1!" % str(self.sign))
     LOG.info("%s of model %s for %s created!" %
              (self.__class__, self.model, self.name))