本文整理汇总了Python中brian2.core.names.Nameable类的典型用法代码示例。如果您正苦于以下问题:Python Nameable类的具体用法?Python Nameable怎么用?Python Nameable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Nameable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, *objs, **kwds):
#: The list of objects in the Network, should not normally be modified
#: directly.
#: Note that in a `MagicNetwork`, this attribute only contains the
#: objects during a run: it is filled in `before_run` and emptied in
#: `after_run`
self.objects = []
name = kwds.pop('name', 'network*')
if kwds:
raise TypeError("Only keyword argument to Network is 'name'.")
Nameable.__init__(self, name=name)
#: Current time as a float
self.t_ = 0.0
for obj in objs:
self.add(obj)
#: Stored state of objects (store/restore)
self._stored_state = {}
# Stored profiling information (if activated via the keyword option)
self._profiling_info = None
self._schedule = None
示例2: __init__
def __init__(self, when=None, name=None):
scheduler = Scheduler(when)
when = scheduler.when
order = scheduler.order
clock = scheduler.clock
Nameable.__init__(self, name)
#: The ID string determining when the object should be updated in :meth:`Network.run`.
self.when = when
#: The order in which objects with the same clock and ``when`` should be updated
self.order = order
# #: The `Clock` determining when the object should be updated.
# self.clock = clock
self._clock = clock
self._contained_objects = []
self._active = True
logger.debug(
"Created BrianObject with name {self.name}, "
"clock name {self.clock.name}, "
"when={self.when}, order={self.order}".format(self=self)
)
示例3: __init__
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):
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.debug("Created BrianObject with name {self.name}, "
"clock={self._clock}, "
"when={self.when}, order={self.order}".format(self=self))
示例4: __init__
def __init__(self, dt, name='clock*'):
self._i = 0
#: The internally used dt. Note that right after a change of dt, this
#: will not equal the new dt (which is stored in `Clock._new_dt`). Call
#: `Clock._set_t_update_t` to update the internal clock representation.
self._dt = float(dt)
self._new_dt = None
Nameable.__init__(self, name=name)
logger.debug("Created clock {self.name} with dt={self._dt}".format(self=self))
示例5: __init__
def __init__(self, owner, code, variables, name='codeobject*'):
Nameable.__init__(self, name=name)
try:
owner = weakref.proxy(owner)
except TypeError:
pass # if owner was already a weakproxy then this will be the error raised
self.owner = owner
self.code = code
self.variables = variables
示例6: __init__
def __init__(self, values, dt, name=None):
if name is None:
name = '_timedarray*'
Nameable.__init__(self, name)
unit = get_unit(values)
values = np.asarray(values)
self.values = values
dt = float(dt)
self.dt = dt
# Python implementation (with units), used when calling the TimedArray
# directly, outside of a simulation
@check_units(t=second, result=unit)
def timed_array_func(t):
i = np.clip(np.int_(np.float_(t) / dt + 0.5), 0, len(values)-1)
return values[i] * unit
Function.__init__(self, pyfunc=timed_array_func)
# Implementation for numpy, without units
def unitless_timed_array_func(t):
i = np.clip(np.int_(np.float_(t) / dt + 0.5), 0, len(values)-1)
return values[i]
unitless_timed_array_func._arg_units = [second]
unitless_timed_array_func._return_unit = unit
# Implementation for C++
cpp_code = {'support_code': '''
inline double _timedarray_%NAME%(const double t, const double _dt, const int _num_values, const double* _values)
{
int i = (int)(t/_dt + 0.5); // rounds to nearest int for positive values
if(i<0)
i = 0;
if(i>=_num_values)
i = _num_values-1;
return _values[i];
}
'''.replace('%NAME%', self.name),
'hashdefine_code': '''
#define %NAME%(t) _timedarray_%NAME%(t, _%NAME%_dt, _%NAME%_num_values, _%NAME%_values)
'''.replace('%NAME%', self.name)}
namespace = {'_%s_dt' % self.name: self.dt,
'_%s_num_values' % self.name: len(self.values),
'_%s_values' % self.name: self.values}
add_implementations(self, codes={'cpp': cpp_code,
'numpy': unitless_timed_array_func},
namespaces={'cpp': namespace},
names={'cpp': self.name})
示例7: __init__
def __init__(self, owner, code, variables, variable_indices,
template_name, template_source, compiler_kwds,
name='codeobject*'):
Nameable.__init__(self, name=name)
try:
owner = weakref.proxy(owner)
except TypeError:
pass # if owner was already a weakproxy then this will be the error raised
self.owner = owner
self.code = code
self.variables = variables
self.variable_indices = variable_indices
self.template_name = template_name
self.template_source = template_source
self.compiler_kwds = compiler_kwds
示例8: __init__
def __init__(self, values, dt, name=None):
if name is None:
name = '_timedarray*'
Nameable.__init__(self, name)
unit = get_unit(values)
self.unit = unit
values = np.asarray(values, dtype=np.double)
self.values = values
dt = float(dt)
self.dt = dt
if values.ndim == 1:
self._init_1d()
elif values.ndim == 2:
self._init_2d()
else:
raise NotImplementedError(('Only 1d and 2d arrays are supported '
'for TimedArray'))
示例9: __init__
def __init__(self, *objs, **kwds):
#: The list of objects in the Network, should not normally be modified directly
#:
#: Stores `weakref.proxy` references to the objects.
self.objects = []
name = kwds.pop('name', None)
if kwds:
raise TypeError("Only keyword argument to Network is name")
Nameable.__init__(self, name=name)
self._prepared = False
for obj in objs:
self.add(obj)
#: Current time as a float
self.t_ = 0.0
示例10: clear
def clear(erase=False):
'''
Stops all Brian objects from being automatically detected
Stops objects from being tracked by `run` and `reinit`.
Use this if you are seeing `MagicError` on repeated runs.
Parameters
----------
erase : bool, optional
If set to ``True``, all data attributes of all Brian objects
will be set to ``None``. This
can help solve problems with circular references stopping objects
from being garbage collected, and is a quick way to ensure that all
memory associated to Brian objects is deleted.
Notes
-----
Removes the objects from ``BrianObject.__instances__()`` and
``Nameable.__instances__()``.
Will also set the
`BrianObject.active` flag to ``False`` for already existing `Network`
objects. Calls a garbage collection on completion.
See ALso
--------
run, reinit, MagicError
'''
if erase:
instances = set(BrianObject.__instances__())
for obj in instances:
obj = obj()
if obj is None:
continue
for k, v in obj.__dict__.iteritems():
object.__setattr__(obj, k, None)
BrianObject.__instances__().clear()
Nameable.__instances__().clear()
gc.collect()
示例11: __init__
def __init__(self, *objs, **kwds):
#: The list of objects in the Network, should not normally be modified
#: directly.
#: Note that in a `MagicNetwork`, this attribute only contains the
#: objects during a run: it is filled in `before_run` and emptied in
#: `after_run`
self.objects = []
name = kwds.pop('name', 'network*')
if kwds:
raise TypeError("Only keyword argument to Network is 'name'.")
Nameable.__init__(self, name=name)
for obj in objs:
self.add(obj)
#: Current time as a float
self.t_ = 0.0
示例12: __init__
def __init__(self, dt, name='clock*'):
# We need a name right away because some devices (e.g. cpp_standalone)
# need a name for the object when creating the variables
Nameable.__init__(self, name=name)
#: Note that right after a change of dt, this
#: will not equal the new dt (which is stored in `Clock._new_dt`). Call
#: `Clock._set_t_update_t` to update the internal clock representation.
self._new_dt = None
self.variables = Variables(self)
self.variables.add_array('timestep', unit=Unit(1), size=1,
dtype=np.uint64, read_only=True, scalar=True)
self.variables.add_array('t', unit=second, size=1,
dtype=np.double, read_only=True, scalar=True)
self.variables.add_array('dt', unit=second, size=1, values=float(dt),
dtype=np.float, read_only=True, constant=True,
scalar=True)
self.variables.add_constant('N', unit=Unit(1), value=1)
self._enable_group_attributes()
self.dt = dt
logger.diagnostic("Created clock {name} with dt={dt}".format(name=self.name,
dt=self.dt))
示例13: __init__
def __init__(self, *objs, **kwds):
#: The list of objects in the Network, should not normally be modified
#: directly
#:
#: Stores references or `weakref.proxy` references to the objects
#: (depending on `weak_references`)
self.objects = []
name = kwds.pop('name', 'network*')
#: Whether the network only stores weak references to the objects
self.weak_references = kwds.pop('weak_references', False)
if kwds:
raise TypeError("Only keyword arguments to Network are name "
"and weak_references")
Nameable.__init__(self, name=name)
for obj in objs:
self.add(obj)
#: Current time as a float
self.t_ = 0.0
示例14: __init__
def __init__(self, source, event, variables=None, record=True,
when=None, order=None, name='eventmonitor*',
codeobj_class=None):
if not isinstance(source, SpikeSource):
raise TypeError(('%s can only monitor groups producing spikes '
'(such as NeuronGroup), but the given argument '
'is of type %s.') % (self.__class__.__name__,
type(source)))
#: The source we are recording from
self.source = source
#: Whether to record times and indices of events
self.record = record
if when is None:
if order is not None:
raise ValueError('Cannot specify order if when is not specified.')
if hasattr(source, 'thresholder'):
parent_obj = source.thresholder[event]
else:
parent_obj = source
when = parent_obj.when
order = parent_obj.order + 1
elif order is None:
order = 0
#: The event that we are listening to
self.event = event
if variables is None:
variables = {}
elif isinstance(variables, basestring):
variables = {variables}
#: The additional variables that will be recorded
self.record_variables = set(variables)
for variable in variables:
if variable not in source.variables:
raise ValueError(("'%s' is not a variable of the recorded "
"group" % variable))
if self.record:
self.record_variables |= {'i', 't'}
# Some dummy code so that code generation takes care of the indexing
# and subexpressions
code = ['_to_record_%s = _source_%s' % (v, v)
for v in self.record_variables]
code = '\n'.join(code)
self.codeobj_class = codeobj_class
# Since this now works for general events not only spikes, we have to
# pass the information about which variable to use to the template,
# it can not longer simply refer to "_spikespace"
eventspace_name = '_{}space'.format(event)
# Handle subgroups correctly
start = getattr(source, 'start', 0)
stop = getattr(source, 'stop', len(source))
Nameable.__init__(self, name=name)
self.variables = Variables(self)
self.variables.add_reference(eventspace_name, source)
for variable in self.record_variables:
source_var = source.variables[variable]
self.variables.add_reference('_source_%s' % variable,
source, variable)
self.variables.add_auxiliary_variable('_to_record_%s' % variable,
unit=source_var.unit,
dtype=source_var.dtype)
self.variables.add_dynamic_array(variable, size=0,
unit=source_var.unit,
dtype=source_var.dtype,
read_only=True)
self.variables.add_arange('_source_idx', size=len(source))
self.variables.add_array('count', size=len(source), unit=Unit(1),
dtype=np.int32, read_only=True,
index='_source_idx')
self.variables.add_constant('_source_start', Unit(1), start)
self.variables.add_constant('_source_stop', Unit(1), stop)
self.variables.add_array('N', unit=Unit(1), size=1, dtype=np.int32,
read_only=True, scalar=True)
record_variables = {varname: self.variables[varname]
for varname in self.record_variables}
template_kwds = {'eventspace_variable': source.variables[eventspace_name],
'record_variables': record_variables,
'record': self.record}
needed_variables = {eventspace_name} | self.record_variables
CodeRunner.__init__(self, group=self, code=code, template='spikemonitor',
name=None, # The name has already been initialized
clock=source.clock, when=when,
order=order, needed_variables=needed_variables,
template_kwds=template_kwds)
self.variables.create_clock_variables(self._clock,
prefix='_clock_')
#.........这里部分代码省略.........
示例15: __init__
def __init__(
self,
source,
event,
variables=None,
record=True,
when=None,
order=None,
name="eventmonitor*",
codeobj_class=None,
):
#: The source we are recording from
self.source = source
#: Whether to record times and indices of events
self.record = record
if when is None:
if order is not None:
raise ValueError("Cannot specify order if when is not specified.")
if hasattr(source, "thresholder"):
parent_obj = source.thresholder[event]
else:
parent_obj = source
when = parent_obj.when
order = parent_obj.order + 1
elif order is None:
order = 0
#: The event that we are listening to
self.event = event
if variables is None:
variables = {}
elif isinstance(variables, basestring):
variables = {variables}
#: The additional variables that will be recorded
self.record_variables = set(variables)
for variable in variables:
if variable not in source.variables:
raise ValueError(("'%s' is not a variable of the recorded " "group" % variable))
if self.record:
self.record_variables |= {"i", "t"}
# Some dummy code so that code generation takes care of the indexing
# and subexpressions
code = ["_to_record_%s = _source_%s" % (v, v) for v in self.record_variables]
code = "\n".join(code)
self.codeobj_class = codeobj_class
# Since this now works for general events not only spikes, we have to
# pass the information about which variable to use to the template,
# it can not longer simply refer to "_spikespace"
eventspace_name = "_{}space".format(event)
# Handle subgroups correctly
start = getattr(source, "start", 0)
stop = getattr(source, "stop", len(source))
Nameable.__init__(self, name=name)
self.variables = Variables(self)
self.variables.add_reference(eventspace_name, source)
for variable in self.record_variables:
source_var = source.variables[variable]
self.variables.add_reference("_source_%s" % variable, source, variable)
self.variables.add_auxiliary_variable(
"_to_record_%s" % variable, unit=source_var.unit, dtype=source_var.dtype
)
self.variables.add_dynamic_array(
variable, size=0, unit=source_var.unit, dtype=source_var.dtype, constant_size=False
)
self.variables.add_arange("_source_idx", size=len(source))
self.variables.add_array(
"count", size=len(source), unit=Unit(1), dtype=np.int32, read_only=True, index="_source_idx"
)
self.variables.add_constant("_source_start", Unit(1), start)
self.variables.add_constant("_source_stop", Unit(1), stop)
self.variables.add_array("N", unit=Unit(1), size=1, dtype=np.int32, read_only=True, scalar=True)
record_variables = {varname: self.variables[varname] for varname in self.record_variables}
template_kwds = {
"eventspace_variable": source.variables[eventspace_name],
"record_variables": record_variables,
"record": self.record,
}
needed_variables = {eventspace_name} | self.record_variables
CodeRunner.__init__(
self,
group=self,
code=code,
template="spikemonitor",
name=None, # The name has already been initialized
clock=source.clock,
when=when,
order=order,
#.........这里部分代码省略.........