本文整理汇总了Python中brian2.groups.group.CodeRunner类的典型用法代码示例。如果您正苦于以下问题:Python CodeRunner类的具体用法?Python CodeRunner怎么用?Python CodeRunner使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CodeRunner类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, source, name='ratemonitor*',
codeobj_class=None):
#: The group we are recording from
self.source = source
self.codeobj_class = codeobj_class
CodeRunner.__init__(self, group=self, code='', template='ratemonitor',
clock=source.clock, when='end', order=0, name=name)
self.add_dependency(source)
self.variables = Variables(self)
# Handle subgroups correctly
start = getattr(source, 'start', 0)
stop = getattr(source, 'stop', len(source))
self.variables.add_constant('_source_start', Unit(1), start)
self.variables.add_constant('_source_stop', Unit(1), stop)
self.variables.add_reference('_spikespace', source)
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_reference('_num_source_neurons', source, 'N')
self.variables.add_array('N', unit=Unit(1), dtype=np.int32, size=1,
scalar=True, read_only=True)
self.variables.create_clock_variables(self._clock,
prefix='_clock_')
self._enable_group_attributes()
示例2: before_run
def before_run(self, run_namespace=None, level=0):
if self._group.dt != self._stored_dt:
raise NotImplementedError('The dt used for simulating %s changed '
'after the PoissonInput source was '
'created.' % self.group.name)
CodeRunner.before_run(self, run_namespace=run_namespace,
level=level+1)
示例3: __init__
def __init__(self, source, record=True, when='end', order=0,
name='spikemonitor*', codeobj_class=None):
self.record = bool(record)
#: The source we are recording from
self.source =source
self.codeobj_class = codeobj_class
CodeRunner.__init__(self, group=self, code='', template='spikemonitor',
name=name, clock=source.clock, when=when,
order=order)
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_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_arange('_source_i', size=len(source))
self.variables.add_array('_count', size=len(source), unit=Unit(1),
dtype=np.int32, read_only=True,
index='_source_i')
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.variables.create_clock_variables(self._clock,
prefix='_clock_')
self._enable_group_attributes()
示例4: before_run
def before_run(self, run_namespace=None, level=0):
# execute code to initalize the spike queue
if self._initialise_queue_codeobj is None:
self._initialise_queue_codeobj = create_runner_codeobj(self,
'', # no code,
'synapses_initialise_queue',
name=self.name+'_initialise_queue',
check_units=False,
additional_variables=self.variables,
run_namespace=run_namespace,
level=level+1)
self._initialise_queue_codeobj()
CodeRunner.before_run(self, run_namespace, level=level+1)
# we insert rather than replace because CodeRunner puts a CodeObject in updaters already
if self._pushspikes_codeobj is None:
self._pushspikes_codeobj = create_runner_codeobj(self,
'', # no code
'synapses_push_spikes',
name=self.name+'_push_spikes',
check_units=False,
additional_variables=self.variables,
run_namespace=run_namespace,
level=level+1)
self._code_objects.insert(0, weakref.proxy(self._pushspikes_codeobj))
示例5: before_run
def before_run(self, run_namespace):
# execute code to initalize the data structures
if self._prepare_codeobj is None:
self._prepare_codeobj = create_runner_codeobj(self.group,
'', # no code,
'spatialneuron_prepare',
name=self.name+'_spatialneuron_prepare',
check_units=False,
additional_variables=self.variables,
run_namespace=run_namespace)
self._prepare_codeobj()
# Raise a warning if the slow pure numpy version is used
# For simplicity, we check which CodeObject class the _prepare_codeobj
# is using, this will be the same as the main state updater
from brian2.codegen.runtime.numpy_rt.numpy_rt import NumpyCodeObject
if isinstance(self._prepare_codeobj, NumpyCodeObject):
# If numpy is used, raise a warning if scipy is not present
try:
import scipy
except ImportError:
logger.info(('SpatialNeuron will use numpy to do the numerical '
'integration -- this will be very slow. Either '
'switch to a different code generation target '
'(e.g. weave or cython) or install scipy.'),
once=True)
CodeRunner.before_run(self, run_namespace)
示例6: __init__
def __init__(self, source, name='ratemonitor*',
codeobj_class=None):
#: The group we are recording from
self.source = source
scheduler = Scheduler(clock=source.clock, when='end')
self.codeobj_class = codeobj_class
CodeRunner.__init__(self, group=self, template='ratemonitor',
when=scheduler, name=name)
self.variables = Variables(self)
self.variables.add_reference('_spikespace',
source.variables['_spikespace'])
self.variables.add_reference('_clock_t', source.variables['t'])
self.variables.add_reference('_clock_dt', source.variables['dt'])
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_reference('_num_source_neurons',
source.variables['N'])
self.variables.add_attribute_variable('N', unit=Unit(1), obj=self,
attribute='_N', dtype=np.int32)
self._N = 0
self._enable_group_attributes()
示例7: run
def run(self):
CodeRunner.run(self)
# Solve the linear system connecting branches
self.P[:] = 0
self.B[:] = 0
self.fill_matrix(self.group.morphology)
self.V = solve(self.P, self.B) # This code could be generated at initialization
# Calculate solutions by linear combination
self.linear_combination(self.group.morphology)
示例8: __init__
def __init__(self, group, method, clock, order=0):
# group is the neuron (a group of compartments)
self.method_choice = method
self.group = weakref.proxy(group)
CodeRunner.__init__(self, group,
'spatialstateupdate',
code='''_gtot = gtot__private
_I0 = I0__private''',
clock=clock,
when='groups',
order=order,
name=group.name + '_spatialstateupdater*',
check_units=False)
n = len(group) # total number of compartments
segments = self.number_branches(group.morphology)
self.variables = Variables(self, default_index='_segment_idx')
self.variables.add_reference('N', group)
self.variables.add_arange('_compartment_idx', size=n)
self.variables.add_arange('_segment_idx', size=segments)
self.variables.add_arange('_segment_root_idx', size=segments+1)
self.variables.add_arange('_P_idx', size=(segments+1)**2)
self.variables.add_array('_invr', unit=siemens, size=n, constant=True,
index='_compartment_idx')
self.variables.add_array('_P', unit=Unit(1), size=(segments+1)**2,
constant=True, index='_P_idx')
self.variables.add_array('_B', unit=Unit(1), size=segments+1,
constant=True, index='_segment_root_idx')
self.variables.add_array('_morph_i', unit=Unit(1), size=segments,
dtype=np.int32, constant=True)
self.variables.add_array('_morph_parent_i', unit=Unit(1), size=segments,
dtype=np.int32, constant=True)
self.variables.add_array('_starts', unit=Unit(1), size=segments,
dtype=np.int32, constant=True)
self.variables.add_array('_ends', unit=Unit(1), size=segments,
dtype=np.int32, constant=True)
self.variables.add_array('_invr0', unit=siemens, size=segments,
constant=True)
self.variables.add_array('_invrn', unit=siemens, size=segments,
constant=True)
self._enable_group_attributes()
# The morphology is considered fixed (length etc. can still be changed,
# though)
# Traverse it once to get a flattened representation
self._temp_morph_i = np.zeros(segments, dtype=np.int32)
self._temp_morph_parent_i = np.zeros(segments, dtype=np.int32)
self._temp_starts = np.zeros(segments, dtype=np.int32)
self._temp_ends = np.zeros(segments, dtype=np.int32)
self._pre_calc_iteration(self.group.morphology)
self._morph_i = self._temp_morph_i
self._morph_parent_i = self._temp_morph_parent_i
self._starts = self._temp_starts
self._ends = self._temp_ends
self._prepare_codeobj = None
示例9: __init__
def __init__(self, group, method):
self.method_choice = method
CodeRunner.__init__(self, group,
'stateupdate',
when=(group.clock, 'groups'),
name=group.name + '_stateupdater',
check_units=False)
self.method = StateUpdateMethod.determine_stateupdater(self.group.equations,
self.group.variables,
method)
示例10: before_run
def before_run(self, run_namespace=None, level=0):
# execute code to initalize the data structures
if self._prepare_codeobj is None:
self._prepare_codeobj = create_runner_codeobj(self.group,
'', # no code,
'spatialneuron_prepare',
name=self.name+'_spatialneuron_prepare',
check_units=False,
additional_variables=self.variables,
run_namespace=run_namespace,
level=level+1)
self._prepare_codeobj()
CodeRunner.before_run(self, run_namespace, level=level + 1)
示例11: __init__
def __init__(self, group, method):
# group is the neuron (a group of compartments)
self.method_choice = method
self._isprepared = False
CodeRunner.__init__(self, group,
'spatialstateupdate',
when=(group.clock, 'groups', 1),
name=group.name + '_spatialstateupdater*',
check_units=False)
self.abstract_code = '''
_gtot = gtot__private
_I0 = I0__private
'''
N = len(self.group)
self.ab_star = zeros((3, N))
self.ab_plus = zeros((3, N))
self.ab_minus = zeros((3, N))
self.b_plus = zeros(N)
self.b_minus = zeros(N)
self.v_star = zeros(N)
self.u_plus = zeros(N)
self.u_minus = zeros(N)
self.variables = Variables(self)
# These 5 variables are constant after prepare()
self.variables.add_array('ab_star', Unit(1), 3 * N,
values=self.ab_star.flatten(),
dtype=self.ab_star.dtype)
self.variables.add_array('ab_plus', Unit(1), 3 * N,
values=self.ab_plus.flatten(),
dtype=self.ab_plus.dtype)
self.variables.add_array('ab_minus', Unit(1), 3 * N,
values=self.ab_minus.flatten(),
dtype=self.ab_minus.dtype)
self.variables.add_array('b_plus', Unit(1), N, values=self.b_plus,
dtype=self.b_plus.dtype)
self.variables.add_array('b_minus', Unit(1), N, values=self.b_minus,
dtype=self.b_minus.dtype)
# These 3 variables change every time step
self.variables.add_array('v_star', Unit(1), N, values=self.v_star,
dtype=self.v_star.dtype)
self.variables.add_array('u_plus', Unit(1), N, values=self.u_plus,
dtype=self.u_plus.dtype)
self.variables.add_array('u_minus', Unit(1), N, values=self.u_minus,
dtype=self.u_minus.dtype)
示例12: __init__
def __init__(self, target, target_var, N, rate, weight, when='synapses',
order=0):
if target_var not in target.variables:
raise KeyError('%s is not a variable of %s' % (target_var, target.name))
if isinstance(weight, basestring):
weight = '(%s)' % weight
else:
weight_unit = get_unit(weight)
weight = repr(weight)
target_unit = target.variables[target_var].unit
# This will be checked automatically in the abstract code as well
# but doing an explicit check here allows for a clearer error
# message
if not have_same_dimensions(weight_unit, target_unit):
raise DimensionMismatchError(('The provided weight does not '
'have the same unit as the '
'target variable "%s"') % target_var,
weight_unit.dim,
target_unit.dim)
binomial_sampling = BinomialFunction(N, rate*target.clock.dt,
name='poissoninput_binomial*')
code = '{targetvar} += {binomial}()*{weight}'.format(targetvar=target_var,
binomial=binomial_sampling.name,
weight=weight)
self._stored_dt = target.dt_[:] # make a copy
# FIXME: we need an explicit reference here for on-the-fly subgroups
# For example: PoissonInput(group[:N], ...)
self._group = target
CodeRunner.__init__(self,
group=target,
template='stateupdate',
code=code,
user_code='',
when=when,
order=order,
name='poissoninput*',
clock=target.clock
)
self.variables = Variables(self)
self.variables._add_variable(binomial_sampling.name, binomial_sampling)
示例13: __init__
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()
示例14: __init__
def __init__(self, group, method, clock, order=0):
# group is the neuron (a group of compartments)
self.method_choice = method
self.group = weakref.proxy(group)
compartments = len(group) # total number of compartments
branches = self.number_branches(group.morphology)
CodeRunner.__init__(self, group,
'spatialstateupdate',
code='''_gtot = gtot__private
_I0 = I0__private''',
clock=clock,
when='groups',
order=order,
name=group.name + '_spatialstateupdater*',
check_units=False,
template_kwds={'number_branches': branches})
# The morphology is considered fixed (length etc. can still be changed,
# though)
# Traverse it once to get a flattened representation
self._temp_morph_i = np.zeros(branches, dtype=np.int32)
self._temp_morph_parent_i = np.zeros(branches, dtype=np.int32)
# for the following: a smaller array of size no_segments x max_no_children would suffice...
self._temp_morph_children = np.zeros((branches+1, branches), dtype=np.int32)
# children count per branch: determines the no of actually used elements of the array above
self._temp_morph_children_num = np.zeros(branches+1, dtype=np.int32)
# each branch is child of exactly one parent (and we say the first branch i=1 is child of branch i=0)
# here we store the indices j-1->k of morph_children_i[i,k] = j
self._temp_morph_idxchild = np.zeros(branches, dtype=np.int32)
self._temp_starts = np.zeros(branches, dtype=np.int32)
self._temp_ends = np.zeros(branches, dtype=np.int32)
self._pre_calc_iteration(self.group.morphology)
# flattened and reduce children indices
max_children = max(self._temp_morph_children_num)
self._temp_morph_children = self._temp_morph_children[:,:max_children].reshape(-1)
self.variables = Variables(self, default_index='_branch_idx')
self.variables.add_reference('N', group)
# One value per compartment
self.variables.add_arange('_compartment_idx', size=compartments)
self.variables.add_array('_invr', unit=siemens, size=compartments,
constant=True, index='_compartment_idx')
# one value per branch
self.variables.add_arange('_branch_idx', size=branches)
self.variables.add_array('_P_parent', unit=Unit(1), size=branches,
constant=True) # elements below diagonal
self.variables.add_array('_morph_idxchild', unit=Unit(1), size=branches,
dtype=np.int32, constant=True)
self.variables.add_arrays(['_morph_i', '_morph_parent_i',
'_starts', '_ends'], unit=Unit(1),
size=branches, dtype=np.int32, constant=True)
self.variables.add_arrays(['_invr0', '_invrn'], unit=siemens,
size=branches, constant=True)
# one value per branch + 1 value for the root
self.variables.add_arange('_branch_root_idx', size=branches+1)
self.variables.add_array('_P_diag', unit=Unit(1), size=branches+1,
constant=True, index='_branch_root_idx')
self.variables.add_array('_B', unit=Unit(1), size=branches+1,
constant=True, index='_branch_root_idx')
self.variables.add_arange('_morph_children_num_idx', size=branches+1)
self.variables.add_array('_morph_children_num', unit=Unit(1),
size=branches+1, dtype=np.int32, constant=True,
index='_morph_children_num_idx')
# 2D matrices of size (branches + 1) x max children per branch
# Note that this data structure wastes space if the number of children
# per branch is very different. In practice, however, this should not
# matter much, since branches will normally have 0, 1, or 2 children
# (e.g. SWC files on neuromporh are strictly binary trees)
self.variables.add_array('_P_children', unit=Unit(1),
size=(branches+1)*max_children,
constant=True) # elements above diagonal
self.variables.add_arange('_morph_children_idx',
size=(branches+1)*max_children)
self.variables.add_array('_morph_children', unit=Unit(1),
size=(branches+1)*max_children,
dtype=np.int32, constant=True,
index='_morph_children_idx')
self._enable_group_attributes()
self._morph_i = self._temp_morph_i
self._morph_parent_i = self._temp_morph_parent_i
self._morph_children_num = self._temp_morph_children_num
self._morph_children = self._temp_morph_children
self._morph_idxchild = self._temp_morph_idxchild
self._starts = self._temp_starts
self._ends = self._temp_ends
self._prepare_codeobj = None
示例15: __init__
def __init__(self, source, variables, record=None, dt=None, clock=None,
when='end', order=0, name='statemonitor*', codeobj_class=None):
self.source = source
# Make the monitor use the explicitly defined namespace of its source
# group (if it exists)
self.namespace = getattr(source, 'namespace', None)
self.codeobj_class = codeobj_class
# run by default on source clock at the end
if dt is None and clock is None:
clock = source.clock
# 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 hasattr(record, '_indices'):
# The ._indices method always returns absolute indices
# If the source is already a subgroup of another group, we therefore
# have to shift the indices to become relative to the subgroup
record = record._indices() - getattr(source, '_offset', 0)
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, numbers.Number):
record = np.array([record], dtype=np.int32)
else:
record = np.asarray(record, dtype=np.int32)
#: The array of recorded indices
self.record = 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,
clock=clock,
dt=dt,
when=when,
order=order,
check_units=False)
self.add_dependency(source)
# Setup variables
self.variables = Variables(self)
self.variables.add_dynamic_array('t', size=0, unit=second,
constant=False, constant_size=False)
self.variables.add_attribute_variable('N', unit=Unit(1),
dtype=np.int32,
obj=self, attribute='_N')
self.variables.add_array('_indices', size=len(self.record),
unit=Unit(1), dtype=self.record.dtype,
constant=True, read_only=True,
values=self.record)
self.variables.create_clock_variables(self._clock,
prefix='_clock_')
for varname in variables:
var = source.variables[varname]
if var.scalar and len(self.record) > 1:
logger.warn(('Variable %s is a shared variable but it will be '
'recorded once for every target.' % varname),
once=True)
index = source.variables.indices[varname]
self.variables.add_reference(varname, source, varname, index=index)
if not index in ('_idx', '0') and index not in variables:
self.variables.add_reference(index, source)
self.variables.add_dynamic_array('_recorded_' + varname,
size=(0, len(self.record)),
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]
#.........这里部分代码省略.........