def __init__(self, source, when=None, name='ratemonitor*', codeobj_class=None): self.source = weakref.proxy(source) # run by default on source clock at the end scheduler = Scheduler(when) if not scheduler.defined_clock: scheduler.clock = source.clock if not scheduler.defined_when: scheduler.when = 'end' self.codeobj_class = codeobj_class BrianObject.__init__(self, when=scheduler, name=name) # create data structures self.reinit() self.variables = {'t': AttributeVariable(second, self.clock, 't'), 'dt': AttributeVariable(second, self.clock, 'dt', constant=True), '_spikes': AttributeVariable(Unit(1), self.source, 'spikes'), # The template needs to have access to the # DynamicArray here, having access to the underlying # array is not enough since we want to do the resize # in the template '_rate': Variable(Unit(1), self._rate), '_t': Variable(Unit(1), self._t), '_num_source_neurons': Variable(Unit(1), len(self.source))}
def custom_operation(self, code, when=None, name=None): ''' Returns a `CodeRunner` that runs abstract code in the group's namespace. Parameters ---------- code : str The abstract code to run. when : `Scheduler`, optional When to run, by default in the 'stateupdate' slot with the same clock as the group. name : str, optional A unique name, if non is given the name of the group appended with 'custom_operation', 'custom_operation_1', etc. will be used. If a name is given explicitly, it will be used as given (i.e. the group name will not be prepended automatically). ''' when = Scheduler(when) if not when.defined_clock: when.clock = self.clock if name is None: name = self.name + '_custom_operation*' runner = CodeRunner(self, 'stateupdate', code=code, name=name, when=when) return runner
def __init__(self, source, variables, record=None, when=None, name='statemonitor*', codeobj_class=None): self.source = weakref.proxy(source) self.codeobj_class = codeobj_class # run by default on source clock at the end scheduler = Scheduler(when) if not scheduler.defined_clock: scheduler.clock = source.clock if not scheduler.defined_when: scheduler.when = 'end' BrianObject.__init__(self, when=scheduler, name=name) # variables should always be a list of strings if variables is True: variables = source.equations.names elif isinstance(variables, str): variables = [variables] #: The variables to record self.record_variables = variables # record should always be an array of ints self.record_all = False if record is True: self.record_all = True record = source.item_mapping[:] elif record is None or record is False: record = np.array([], dtype=np.int32) elif isinstance(record, int): record = np.array([source.item_mapping[record]], dtype=np.int32) else: record = np.array(source.item_mapping[record], dtype=np.int32) #: The array of recorded indices self.indices = record # create data structures self.reinit() # Setup variables self.variables = {} for varname in variables: var = source.variables[varname] if not (np.issubdtype(var.dtype, np.float64) and np.issubdtype(np.float64, var.dtype)): raise NotImplementedError(('Cannot record %s with data type ' '%s, currently only values stored as ' 'doubles can be recorded.') % (varname, var.dtype)) self.variables[varname] = var self.variables['_recorded_'+varname] = Variable(Unit(1), self._values[varname]) self.variables['_t'] = Variable(Unit(1), self._t) self.variables['_clock_t'] = AttributeVariable(second, self.clock, 't_') self.variables['_indices'] = ArrayVariable('_indices', Unit(1), self.indices, constant=True) self._group_attribute_access_active = True
def runner(self, code, when=None, name=None): ''' Returns a `CodeRunner` that runs abstract code in the groups namespace Parameters ---------- code : str The abstract code to run. when : `Scheduler`, optional When to run, by default in the 'start' slot with the same clock as the group. name : str, optional A unique name, if non is given the name of the group appended with 'runner', 'runner_1', etc. will be used. If a name is given explicitly, it will be used as given (i.e. the group name will not be prepended automatically). ''' when = Scheduler(when) if not when.defined_clock: when.clock = self.clock if name is None: name = self.name + '_runner*' runner = CodeRunner(self, 'stateupdate', code=code, name=name, when=when) return runner
def __init__(self, source, when=None, name='ratemonitor*', codeobj_class=None): self.source = weakref.proxy(source) # run by default on source clock at the end scheduler = Scheduler(when) if not scheduler.defined_clock: scheduler.clock = source.clock if not scheduler.defined_when: scheduler.when = 'end' self.codeobj_class = codeobj_class BrianObject.__init__(self, when=scheduler, name=name) # create data structures self.reinit() self.variables = { 't': AttributeVariable(second, self.clock, 't'), 'dt': AttributeVariable(second, self.clock, 'dt', constant=True), '_spikes': AttributeVariable(Unit(1), self.source, 'spikes'), # The template needs to have access to the # DynamicArray here, having access to the underlying # array is not enough since we want to do the resize # in the template '_rate': Variable(Unit(1), self._rate), '_t': Variable(Unit(1), self._t), '_num_source_neurons': Variable(Unit(1), len(self.source)) }
def __init__(self, source, variables, record=None, when=None, name='statemonitor*', codeobj_class=None): self.source = weakref.proxy(source) self.codeobj_class = codeobj_class # run by default on source clock at the end scheduler = Scheduler(when) if not scheduler.defined_clock: scheduler.clock = source.clock if not scheduler.defined_when: scheduler.when = 'end' BrianObject.__init__(self, when=scheduler, name=name) # variables should always be a list of strings if variables is True: variables = source.equations.names elif isinstance(variables, str): variables = [variables] #: The variables to record self.record_variables = variables # record should always be an array of ints self.record_all = False if record is True: self.record_all = True record = np.arange(len(source), dtype=np.int32) elif record is None or record is False: record = np.array([], dtype=np.int32) elif isinstance(record, int): record = np.array([record], dtype=np.int32) else: record = np.asarray(record, dtype=np.int32) #: The array of recorded indices self.indices = record # Setup variables self.variables = Variables(self) for varname in variables: var = source.variables[varname] self.variables.add_reference(varname, var, index=source.variables.indices[varname]) self.variables.add_dynamic_array('_recorded_' + varname, size=(0, len(self.indices)), unit=var.unit, dtype=var.dtype, constant=False, constant_size=False, is_bool=var.is_bool) self.variables.add_dynamic_array('_t', size=0, unit=Unit(1), constant=False, constant_size=False) self.variables.add_attribute_variable('_clock_t', second, self.clock, 't_') self.variables.add_array('_indices', size=len(self.indices), unit=Unit(1), dtype=self.indices.dtype, constant=True, read_only=True) self.variables['_indices'].set_value(self.indices) self._group_attribute_access_active = True
def __init__(self, source, record=True, when=None, name='spikemonitor*', codeobj_class=None): self.record = bool(record) #: The source we are recording from self.source = source # run by default on source clock at the end scheduler = Scheduler(when) if not scheduler.defined_clock: scheduler.clock = source.clock if not scheduler.defined_when: scheduler.when = 'end' self.codeobj_class = codeobj_class CodeRunner.__init__(self, group=self, template='spikemonitor', name=name, when=scheduler) # Handle subgroups correctly start = getattr(source, 'start', 0) stop = getattr(source, 'stop', len(source)) self.variables = Variables(self) self.variables.add_clock_variables(scheduler.clock, prefix='_clock_') self.variables.add_reference('_spikespace', source.variables['_spikespace']) self.variables.add_dynamic_array('i', size=0, unit=Unit(1), dtype=np.int32, constant_size=False) self.variables.add_dynamic_array('t', size=0, unit=second, constant_size=False) self.variables.add_array('_count', size=len(source), unit=Unit(1), dtype=np.int32) self.variables.add_constant('_source_start', Unit(1), start) self.variables.add_constant('_source_stop', Unit(1), stop) self.variables.add_attribute_variable('N', unit=Unit(1), obj=self, attribute='_N', dtype=np.int32) self._N = 0 self._enable_group_attributes()
def __init__(self, source, record=True, when=None, name=None): self.source = weakref.proxy(source) self.record = bool(record) # run by default on source clock at the end scheduler = Scheduler(when) if not scheduler.defined_clock: scheduler.clock = source.clock if not scheduler.defined_when: scheduler.when = 'end' BrianObject.__init__(self, when=scheduler, name=name) # create data structures self.reinit()
def __init__(self, source, record=True, when=None, name='spikemonitor*', codeobj_class=None): self.record = bool(record) #: The source we are recording from self.source =source # run by default on source clock at the end scheduler = Scheduler(when) if not scheduler.defined_clock: scheduler.clock = source.clock if not scheduler.defined_when: scheduler.when = 'end' self.codeobj_class = codeobj_class CodeRunner.__init__(self, group=self, template='spikemonitor', name=name, when=scheduler) self.add_dependency(source) # Handle subgroups correctly start = getattr(source, 'start', 0) stop = getattr(source, 'stop', len(source)) self.variables = Variables(self) self.variables.add_clock_variables(scheduler.clock, prefix='_clock_') self.variables.add_reference('_spikespace', source) self.variables.add_dynamic_array('i', size=0, unit=Unit(1), dtype=np.int32, constant_size=False) self.variables.add_dynamic_array('t', size=0, unit=second, constant_size=False) self.variables.add_array('_count', size=len(source), unit=Unit(1), dtype=np.int32) self.variables.add_constant('_source_start', Unit(1), start) self.variables.add_constant('_source_stop', Unit(1), stop) self.variables.add_attribute_variable('N', unit=Unit(1), obj=self, attribute='_N', dtype=np.int32) self._enable_group_attributes()
def __init__(self, source, when=None, name='ratemonitor*', codeobj_class=None): # run by default on source clock at the end scheduler = Scheduler(when) if not scheduler.defined_clock: scheduler.clock = source.clock if not scheduler.defined_when: scheduler.when = 'end' self.codeobj_class = codeobj_class BrianObject.__init__(self, when=scheduler, name=name) self.variables = Variables(self) self.variables.add_clock_variables(self.clock) self.variables.add_dynamic_array('_rate', size=0, unit=hertz, constant_size=False) self.variables.add_dynamic_array('_t', size=0, unit=second, constant_size=False) self.variables.add_constant('_num_source_neurons', unit=Unit(1), value=len(source)) GroupCodeRunner.__init__(self, source, 'ratemonitor', when=scheduler)
def __init__(self, source, record=True, when=None, name='spikemonitor*', codeobj_class=None): self.source = weakref.proxy(source) self.record = bool(record) # run by default on source clock at the end scheduler = Scheduler(when) if not scheduler.defined_clock: scheduler.clock = source.clock if not scheduler.defined_when: scheduler.when = 'end' self.codeobj_class = codeobj_class BrianObject.__init__(self, when=scheduler, name=name) # create data structures self.reinit() # Handle subgroups correctly start = getattr(self.source, 'start', 0) end = getattr(self.source, 'end', len(self.source)) self.variables = {'t': AttributeVariable(second, self.clock, 't'), '_spikes': AttributeVariable(Unit(1), self.source, 'spikes'), # The template needs to have access to the # DynamicArray here, having access to the underlying # array is not enough since we want to do the resize # in the template '_i': Variable(Unit(1), self._i), '_t': Variable(Unit(1), self._t), '_count': ArrayVariable('_count', Unit(1), self.count), '_source_start': Variable(Unit(1), start, constant=True), '_source_end': Variable(Unit(1), end, constant=True)}
def __init__(self, source, variables, record=None, when=None, name=None): self.source = weakref.proxy(source) # run by default on source clock at the end scheduler = Scheduler(when) if not scheduler.defined_clock: scheduler.clock = source.clock if not scheduler.defined_when: scheduler.when = 'end' BrianObject.__init__(self, when=scheduler, name=name) # variables should always be a list of strings if variables is True: variables = source.units.keys() elif isinstance(variables, str): variables = [variables] self.variables = variables # record should always be an array of ints if record is None or record is False: record = array([], dtype=int) elif record is True: record = arange(len(source)) else: record = array(record, dtype=int) #: The array of recorded indices self.indices = record # create data structures self.reinit() # initialise Group access self.units = dict((var, source.units[var]) for var in variables) self.arrays = {} Group.__init__(self)
def __init__(self, source, variables, record=None, when=None, name='statemonitor*', codeobj_class=None): self.source = weakref.proxy(source) self.codeobj_class = codeobj_class # run by default on source clock at the end scheduler = Scheduler(when) if not scheduler.defined_clock: scheduler.clock = source.clock if not scheduler.defined_when: scheduler.when = 'end' # variables should always be a list of strings if variables is True: variables = source.equations.names elif isinstance(variables, str): variables = [variables] #: The variables to record self.record_variables = variables # record should always be an array of ints self.record_all = False if record is True: self.record_all = True record = np.arange(len(source), dtype=np.int32) elif record is None or record is False: record = np.array([], dtype=np.int32) elif isinstance(record, int): record = np.array([record], dtype=np.int32) else: record = np.asarray(record, dtype=np.int32) #: The array of recorded indices self.indices = record self.n_indices = len(record) # Some dummy code so that code generation takes care of the indexing # and subexpressions code = ['_to_record_%s = %s' % (v, v) for v in self.record_variables] code = '\n'.join(code) CodeRunner.__init__(self, group=self, template='statemonitor', code=code, name=name, when=scheduler, check_units=False) # Setup variables self.variables = Variables(self) self.variables.add_dynamic_array('t', size=0, unit=second, constant=False, constant_size=False) if scheduler.clock is source.clock: self.variables.add_reference('_clock_t', source.variables['t']) else: self.variables.add_attribute_variable('_clock_t', unit=second, obj=scheduler.clock, attribute='t_') self.variables.add_attribute_variable('N', unit=Unit(1), dtype=np.int32, obj=self, attribute='_N') self.variables.add_array('_indices', size=len(self.indices), unit=Unit(1), dtype=self.indices.dtype, constant=True, read_only=True) self.variables['_indices'].set_value(self.indices) for varname in variables: var = source.variables[varname] if var.scalar and len(self.indices) > 1: logger.warn(('Variable %s is a scalar variable but it will be ' 'recorded once for every target.' % varname), once=True) index = source.variables.indices[varname] self.variables.add_reference(varname, var, index=index) if not index in ('_idx', '0') and index not in variables: self.variables.add_reference(index, source.variables[index]) # For subexpressions, we also need all referred variables (if they # are not already present, e.g. the t as _clock_t if isinstance(var, Subexpression): for subexpr_varname in var.identifiers: if subexpr_varname in source.variables: source_var = source.variables[subexpr_varname] index = source.variables.indices[subexpr_varname] if index != '_idx' and index not in variables: self.variables.add_reference(index, source.variables[index]) if not source_var in self.variables.values(): source_index = source.variables.indices[subexpr_varname] # `translate_subexpression` will later replace # the name used in the original subexpression with # _source_varname self.variables.add_reference('_source_'+subexpr_varname, source_var, index=source_index) self.variables.add_dynamic_array('_recorded_' + varname, size=(0, len(self.indices)), unit=var.unit, dtype=var.dtype, constant=False, constant_size=False) for varname in self.record_variables: var = self.source.variables[varname] self.variables.add_auxiliary_variable('_to_record_' + varname, unit=var.unit, dtype=var.dtype, scalar=var.scalar) self.recorded_variables = dict([(varname, self.variables['_recorded_'+varname]) for varname in self.record_variables]) recorded_names = ['_recorded_'+varname for varname in self.record_variables] self.needed_variables = recorded_names self.template_kwds = template_kwds={'_recorded_variables': self.recorded_variables} self._N = 0 self._enable_group_attributes()
def __init__(self, source, variables, record=None, when=None, name='statemonitor*', codeobj_class=None): self.source = weakref.proxy(source) self.codeobj_class = codeobj_class # run by default on source clock at the end scheduler = Scheduler(when) if not scheduler.defined_clock: scheduler.clock = source.clock if not scheduler.defined_when: scheduler.when = 'end' # variables should always be a list of strings if variables is True: variables = source.equations.names elif isinstance(variables, str): variables = [variables] #: The variables to record self.record_variables = variables # record should always be an array of ints self.record_all = False if record is True: self.record_all = True record = np.arange(len(source), dtype=np.int32) elif record is None or record is False: record = np.array([], dtype=np.int32) elif isinstance(record, int): record = np.array([record], dtype=np.int32) else: record = np.asarray(record, dtype=np.int32) #: The array of recorded indices self.indices = record self.n_indices = len(record) # Some dummy code so that code generation takes care of the indexing # and subexpressions code = ['_to_record_%s = %s' % (v, v) for v in self.record_variables] code = '\n'.join(code) CodeRunner.__init__(self, group=self, template='statemonitor', code=code, name=name, when=scheduler, check_units=False) # Setup variables self.variables = Variables(self) self.variables.add_dynamic_array('t', size=0, unit=second, constant=False, constant_size=False) if scheduler.clock is source.clock: self.variables.add_reference('_clock_t', source.variables['t']) else: self.variables.add_attribute_variable('_clock_t', unit=second, obj=scheduler.clock, attribute='t_') self.variables.add_attribute_variable('N', unit=Unit(1), dtype=np.int32, obj=self, attribute='_N') self.variables.add_array('_indices', size=len(self.indices), unit=Unit(1), dtype=self.indices.dtype, constant=True, read_only=True) self.variables['_indices'].set_value(self.indices) for varname in variables: var = source.variables[varname] if var.scalar and len(self.indices) > 1: logger.warn(('Variable %s is a scalar variable but it will be ' 'recorded once for every target.' % varname), once=True) index = source.variables.indices[varname] self.variables.add_reference(varname, var, index=index) if not index in ('_idx', '0') and index not in variables: self.variables.add_reference(index, source.variables[index]) # For subexpressions, we also need all referred variables (if they # are not already present, e.g. the t as _clock_t if isinstance(var, Subexpression): for subexpr_varname in var.identifiers: if subexpr_varname in source.variables: source_var = source.variables[subexpr_varname] index = source.variables.indices[subexpr_varname] if index != '_idx' and index not in variables: self.variables.add_reference( index, source.variables[index]) if not source_var in self.variables.values(): source_index = source.variables.indices[ subexpr_varname] # `translate_subexpression` will later replace # the name used in the original subexpression with # _source_varname self.variables.add_reference('_source_' + subexpr_varname, source_var, index=source_index) self.variables.add_dynamic_array('_recorded_' + varname, size=(0, len(self.indices)), unit=var.unit, dtype=var.dtype, constant=False, constant_size=False) for varname in self.record_variables: var = self.source.variables[varname] self.variables.add_auxiliary_variable('_to_record_' + varname, unit=var.unit, dtype=var.dtype, scalar=var.scalar) self.recorded_variables = dict([ (varname, self.variables['_recorded_' + varname]) for varname in self.record_variables ]) recorded_names = [ '_recorded_' + varname for varname in self.record_variables ] self.needed_variables = recorded_names self.template_kwds = template_kwds = { '_recorded_variables': self.recorded_variables } self._N = 0 self._enable_group_attributes()