def run(self): try: exec self.compiled_code in self.namespace except Exception as exc: message = ('An exception occured during the execution of code ' 'object {}.\n').format(self.name) lines = self.code.split('\n') message += 'The error was raised in the following line:\n' _, _, tb = sys.exc_info() tb = tb.tb_next # Line in the code object's code message += lines[tb.tb_lineno - 1] + '\n' raise brian_object_exception(message, self.owner, exc) # output variables should land in the variable name _return_values if '_return_values' in self.namespace: return self.namespace['_return_values']
def run_block(self, block): compiled_code = self.compiled_code[block] if not compiled_code: return try: exec(compiled_code, self.namespace) except Exception as exc: code = getattr(self.code, block) message = ('An exception occured during the execution of the {} ' 'block of code object {}.\n').format(block, self.name) lines = code.split('\n') message += 'The error was raised in the following line:\n' _, _, tb = sys.exc_info() tb = tb.tb_next # Line in the code object's code message += lines[tb.tb_lineno - 1] + '\n' raise brian_object_exception(message, self.owner, exc) # output variables should land in the variable name _return_values if '_return_values' in self.namespace: return self.namespace['_return_values']
def before_run(self, run_namespace): """ before_run(namespace) Prepares the `Network` for a run. Objects in the `Network` are sorted into the correct running order, and their `BrianObject.before_run` methods are called. Parameters ---------- run_namespace : dict-like, optional A namespace in which objects which do not define their own namespace will be run. """ from brian2.devices.device import get_device, all_devices prefs.check_all_validated() # Check names in the network for uniqueness names = [obj.name for obj in self.objects] non_unique_names = [name for name, count in Counter(names).iteritems() if count > 1] if len(non_unique_names): formatted_names = ", ".join("'%s'" % name for name in non_unique_names) raise ValueError( "All objects in a network need to have unique " "names, the following name(s) were used more than " "once: %s" % formatted_names ) self._stopped = False Network._globally_stopped = False device = get_device() if device.network_schedule is not None: # The device defines a fixed network schedule if device.network_schedule != self.schedule: # TODO: The human-readable name of a device should be easier to get device_name = all_devices.keys()[all_devices.values().index(device)] logger.warn( ( "The selected device '{device_name}' only " "supports a fixed schedule, but this schedule is " "not consistent with the network's schedule. The " "simulation will use the device's schedule.\n" "Device schedule: {device.network_schedule}\n" "Network schedule: {net.schedule}\n" "Set the network schedule explicitly or set the " "core.network.default_schedule preference to " "avoid this warning." ).format(device_name=device_name, device=device, net=self), name_suffix="schedule_conflict", once=True, ) self._sort_objects() logger.debug( "Preparing network {self.name} with {numobj} " "objects: {objnames}".format( self=self, numobj=len(self.objects), objnames=", ".join(obj.name for obj in self.objects) ), "before_run", ) self.check_dependencies() for obj in self.objects: if obj.active: try: obj.before_run(run_namespace) except Exception as ex: raise brian_object_exception("An error occurred when preparing an object.", obj, ex) # Check that no object has been run as part of another network before for obj in self.objects: if obj._network is None: obj._network = self.id elif obj._network != self.id: raise RuntimeError( ( "%s has already been run in the " "context of another network. Use " "add/remove to change the objects " "in a simulated network instead of " "creating a new one." ) % obj.name ) logger.debug( "Network {self.name} uses {num} " "clocks: {clocknames}".format( self=self, num=len(self._clocks), clocknames=", ".join("%s (dt=%s)" % (obj.name, obj.dt) for obj in self._clocks), ), "before_run", )
def before_run(self, run_namespace): ''' before_run(namespace) Prepares the `Network` for a run. Objects in the `Network` are sorted into the correct running order, and their `BrianObject.before_run` methods are called. Parameters ---------- run_namespace : dict-like, optional A namespace in which objects which do not define their own namespace will be run. ''' from brian2.devices.device import get_device, all_devices prefs.check_all_validated() # Check names in the network for uniqueness names = [obj.name for obj in self.objects] non_unique_names = [ name for name, count in Counter(names).iteritems() if count > 1 ] if len(non_unique_names): formatted_names = ', '.join("'%s'" % name for name in non_unique_names) raise ValueError( 'All objects in a network need to have unique ' 'names, the following name(s) were used more than ' 'once: %s' % formatted_names) self._stopped = False Network._globally_stopped = False device = get_device() if device.network_schedule is not None: # The device defines a fixed network schedule if device.network_schedule != self.schedule: # TODO: The human-readable name of a device should be easier to get device_name = all_devices.keys()[all_devices.values().index( device)] logger.warn( ("The selected device '{device_name}' only " "supports a fixed schedule, but this schedule is " "not consistent with the network's schedule. The " "simulation will use the device's schedule.\n" "Device schedule: {device.network_schedule}\n" "Network schedule: {net.schedule}\n" "Set the network schedule explicitly or set the " "core.network.default_schedule preference to " "avoid this warning.").format(device_name=device_name, device=device, net=self), name_suffix='schedule_conflict', once=True) self._sort_objects() logger.debug( "Preparing network {self.name} with {numobj} " "objects: {objnames}".format( self=self, numobj=len(self.objects), objnames=', '.join(obj.name for obj in self.objects)), "before_run") self.check_dependencies() for obj in self.objects: if obj.active: try: obj.before_run(run_namespace) except Exception as ex: raise brian_object_exception( "An error occurred when preparing an object.", obj, ex) # Check that no object has been run as part of another network before for obj in self.objects: if obj._network is None: obj._network = self.id elif obj._network != self.id: raise RuntimeError(('%s has already been run in the ' 'context of another network. Use ' 'add/remove to change the objects ' 'in a simulated network instead of ' 'creating a new one.') % obj.name) logger.debug( "Network {self.name} uses {num} " "clocks: {clocknames}".format( self=self, num=len(self._clocks), clocknames=', '.join('%s (dt=%s)' % (obj.name, obj.dt) for obj in self._clocks)), "before_run")