Example #1
0
    def __init__(self, filterbank, targetvar, *args, **kwds):
        self.targetvar = targetvar
        self.filterbank = filterbank
        self.buffer = None
        filterbank.buffer_init()

        # Sanitize the clock - does it have the right dt value?
        if 'clock' in kwds:
            if int(1/kwds['clock'].dt)!=int(filterbank.samplerate):
                raise ValueError('Clock should have 1/dt=samplerate')
            kwds['clock'] = Clock(dt=float(kwds['clock'].dt)*second)
        else:
            kwds['clock'] = Clock(dt=1*second/float(filterbank.samplerate))        
        
        buffersize = kwds.pop('buffersize', 32)
        if not isinstance(buffersize, int):
            buffersize = int(buffersize*self.samplerate)
        self.buffersize = buffersize
        self.buffer_pointer = buffersize
        self.buffer_start = -buffersize
        
        NeuronGroup.__init__(self, filterbank.nchannels, *args, **kwds)
        
        @network_operation(clock=self.clock, when='start')
        def apply_filterbank_output():
            if self.buffer_pointer>=self.buffersize:
                self.buffer_pointer = 0
                self.buffer_start += self.buffersize
                self.buffer = self.filterbank.buffer_fetch(self.buffer_start, self.buffer_start+self.buffersize)
            setattr(self, targetvar, self.buffer[self.buffer_pointer, :])
            self.buffer_pointer += 1
        
        self.contained_objects.append(apply_filterbank_output)
Example #2
0
    def __init__(self,
                 dt=None,
                 clock=None,
                 when='start',
                 order=0,
                 name='brianobject*'):

        if dt is not None and clock is not None:
            raise ValueError(
                'Can only specify either a dt or a clock, not both.')

        if not isinstance(when, basestring):
            # Give some helpful error messages for users coming from the alpha
            # version
            if isinstance(when, Clock):
                raise TypeError(("Do not use the 'when' argument for "
                                 "specifying a clock, either provide a "
                                 "timestep for the 'dt' argument or a Clock "
                                 "object for 'clock'."))
            if isinstance(when, tuple):
                raise TypeError("Use the separate keyword arguments, 'dt' (or "
                                "'clock'), 'when', and 'order' instead of "
                                "providing a tuple for 'when'. Only use the "
                                "'when' argument for the scheduling slot.")
            # General error
            raise TypeError("The 'when' argument has to be a string "
                            "specifying the scheduling slot (e.g. 'start').")

        Nameable.__init__(self, name)

        #: The clock used for simulating this object
        self._clock = clock
        if clock is None:
            if dt is not None:
                self._clock = Clock(dt=dt, name=self.name + '_clock*')
            else:
                self._clock = defaultclock

        #: Used to remember the `Network` in which this object has been included
        #: before, to raise an error if it is included in a new `Network`
        self._network = None

        #: The ID string determining when the object should be updated in `Network.run`.
        self.when = when

        #: The order in which objects with the same clock and ``when`` should be updated
        self.order = order

        self._dependencies = set()
        self._contained_objects = []
        self._code_objects = []

        self._active = True

        #: The scope key is used to determine which objects are collected by magic
        self._scope_key = self._scope_current_key

        logger.debug("Created BrianObject with name {self.name}, "
                     "clock={self._clock}, "
                     "when={self.when}, order={self.order}".format(self=self))
Example #3
0
 def activate(self, build_on_run, **kwargs):
     '''
     Called when this device is set as the current device.
     '''
     if self.defaultclock is None:
         self.defaultclock = Clock(dt=0.1 * ms, name='defaultclock')
     self._set_maximum_run_time(None)
     self.build_on_run = build_on_run
     self.build_options = dict(kwargs)
Example #4
0
    def activate(self, build_on_run=True, **kwargs):
        '''
        Called when this device is set as the current device.
        '''
        from brian2.core.clocks import Clock  # avoid import issues

        if self.defaultclock is None:
            self.defaultclock = Clock(dt=0.1*ms, name='defaultclock')
        self._set_maximum_run_time(None)
        self.build_on_run = build_on_run
        self.build_options = dict(kwargs)
Example #5
0
    def __init__(self,
                 dt=None,
                 clock=None,
                 when='start',
                 order=0,
                 name='brianobject*'):
        # Setup traceback information for this object
        creation_stack = []
        bases = []
        for modulename in ['brian2']:
            if modulename in sys.modules:
                base, _ = os.path.split(sys.modules[modulename].__file__)
                bases.append(base)
        for fname, linenum, funcname, line in traceback.extract_stack():
            if all(base not in fname for base in bases):
                s = '  File "{fname}", line {linenum}, in {funcname}\n    {line}'.format(
                    fname=fname, linenum=linenum, funcname=funcname, line=line)
                creation_stack.append(s)
        creation_stack = [''] + creation_stack
        #: A string indicating where this object was created (traceback with any parts of Brian code removed)
        self._creation_stack = (
            'Object was created here (most recent call only, full details in '
            'debug log):\n' + creation_stack[-1])
        self._full_creation_stack = 'Object was created here:\n' + '\n'.join(
            creation_stack)

        if dt is not None and clock is not None:
            raise ValueError(
                'Can only specify either a dt or a clock, not both.')

        if not isinstance(when, basestring):
            from brian2.core.clocks import Clock
            # Give some helpful error messages for users coming from the alpha
            # version
            if isinstance(when, Clock):
                raise TypeError(("Do not use the 'when' argument for "
                                 "specifying a clock, either provide a "
                                 "timestep for the 'dt' argument or a Clock "
                                 "object for 'clock'."))
            if isinstance(when, tuple):
                raise TypeError("Use the separate keyword arguments, 'dt' (or "
                                "'clock'), 'when', and 'order' instead of "
                                "providing a tuple for 'when'. Only use the "
                                "'when' argument for the scheduling slot.")
            # General error
            raise TypeError("The 'when' argument has to be a string "
                            "specifying the scheduling slot (e.g. 'start').")

        Nameable.__init__(self, name)

        #: The clock used for simulating this object
        self._clock = clock
        if clock is None:
            from brian2.core.clocks import Clock, defaultclock
            if dt is not None:
                self._clock = Clock(dt=dt, name=self.name + '_clock*')
            else:
                self._clock = defaultclock

        if getattr(self._clock, '_is_proxy', False):
            from brian2.devices.device import get_device
            self._clock = get_device().defaultclock

        #: Used to remember the `Network` in which this object has been included
        #: before, to raise an error if it is included in a new `Network`
        self._network = None

        #: The ID string determining when the object should be updated in `Network.run`.
        self.when = when

        #: The order in which objects with the same clock and ``when`` should be updated
        self.order = order

        self._dependencies = set()
        self._contained_objects = []
        self._code_objects = []

        self._active = True

        #: The scope key is used to determine which objects are collected by magic
        self._scope_key = self._scope_current_key

        logger.diagnostic(
            "Created BrianObject with name {self.name}, "
            "clock={self._clock}, "
            "when={self.when}, order={self.order}".format(self=self))