Пример #1
0
    def before_run(self, run_namespace=None, level=0):
        '''
        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
        ----------
        namespace : dict-like, optional
            A namespace in which objects which do not define their own
            namespace will be run.
        '''
        # A garbage collection here can be useful to free memory if we have
        # multiple runs
        gc.collect()

        prefs.check_all_validated()
        
        self._stopped = False
        Network._globally_stopped = False
        
        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, level=level+2)
                except DimensionMismatchError as ex:
                    raise DimensionMismatchError(('An error occured preparing '
                                                  'object "%s":\n%s') % (obj.name,
                                                                          ex.desc),
                                                 *ex.dims)

        # 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} has {num} "
                     "clocks: {clocknames}".format(self=self,
                        num=len(self._clocks),
                        clocknames=', '.join(obj.name for obj in self._clocks)),
                     "before_run")
Пример #2
0
    def before_run(self, run_namespace=None, level=0):
        '''
        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
        ----------
        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, level=level+2)
                except DimensionMismatchError as ex:
                    raise DimensionMismatchError(('An error occured preparing '
                                                  'object "%s":\n%s') % (obj.name,
                                                                          ex.desc),
                                                 *ex.dims)

        # 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} has {num} "
                     "clocks: {clocknames}".format(self=self,
                        num=len(self._clocks),
                        clocknames=', '.join(obj.name for obj in self._clocks)),
                     "before_run")
Пример #3
0
    def before_run(self, run_namespace=None, level=0):
        '''
        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
        ----------
        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, level=level + 2)
                except DimensionMismatchError as ex:
                    raise DimensionMismatchError(
                        ('An error occured preparing '
                         'object "%s":\n%s') % (obj.name, ex.desc), *ex.dims)

        # 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} has {num} "
            "clocks: {clocknames}".format(
                self=self,
                num=len(self._clocks),
                clocknames=', '.join(obj.name for obj in self._clocks)),
            "before_run")