Skip to content

Commit

Permalink
Rename the index attribute to item_mapping to not overuse the word "i…
Browse files Browse the repository at this point in the history
…ndex".
  • Loading branch information
mstimberg committed Jul 28, 2013
1 parent bd3ff5c commit 851c302
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
16 changes: 8 additions & 8 deletions brian2/groups/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
from brian2.equations.unitcheck import check_units_statements
from brian2.utils.logger import get_logger

__all__ = ['Group', 'GroupCodeRunner', 'GroupIndices']
__all__ = ['Group', 'GroupCodeRunner', 'GroupItemMapping']

logger = get_logger(__name__)


class GroupIndices(Index):
class GroupItemMapping(Index):

def __init__(self, name, N):
def __init__(self, N):
self.N = N
self._indices = np.arange(self.N)
self.specifiers = {'i': ArrayVariable('i',
Expand Down Expand Up @@ -66,16 +66,16 @@ class Group(object):
def __init__(self):
if not hasattr(self, 'specifiers'):
raise ValueError('Classes derived from Group need specifiers attribute.')
if not hasattr(self, 'index'):
if not hasattr(self, 'item_mapping'):
try:
N = len(self)
except TypeError:
raise ValueError(('Classes derived from Group need an index '
raise ValueError(('Classes derived from Group need an item_mapping '
'attribute, or a length to automatically '
'provide 1-d indexing'))
self.index = GroupIndices('_element', N)
self.item_mapping = GroupItemMapping(N)
if not hasattr(self, 'indices'):
self.indices = {'_element': self.index}
self.indices = {'_element': self.item_mapping}
if not hasattr(self, 'variable_indices'):
self.variable_indices = defaultdict(lambda: '_element')
if not hasattr(self, 'codeobj_class'):
Expand Down Expand Up @@ -183,7 +183,7 @@ def _set_with_code(self, specifier, group_indices, code,
additional_namespace = ('implicit-namespace', namespace)
# TODO: Find a name that makes sense for reset and variable setting
# with code
additional_specifiers = self.index.specifiers
additional_specifiers = self.item_mapping.specifiers
additional_specifiers['_spikes'] = ArrayVariable('_spikes',
Unit(1),
group_indices.astype(np.int32),
Expand Down
56 changes: 28 additions & 28 deletions brian2/synapses/synapses.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,18 @@ def __init__(self, synapses, code, prepost, objname=None):
self.code = code
if prepost == 'pre':
self.source = synapses.source
self.synapse_indices = synapses.index.pre_synaptic
self.synapse_indices = synapses.item_mapping.pre_synaptic
elif prepost == 'post':
self.source = synapses.target
self.synapse_indices = synapses.index.post_synaptic
self.synapse_indices = synapses.item_mapping.post_synaptic
else:
raise ValueError('prepost argument has to be either "pre" or '
'"post"')
self.synapses = synapses
self._delays = DynamicArray1D(len(synapses.index), dtype=np.float64)
self._delays = DynamicArray1D(synapses.N, dtype=np.float64)
# Register the object with the `SynapticIndex` object so it gets
# automatically resized
synapses.index.register_variable(self._delays)
synapses.item_mapping.register_variable(self._delays)
self.queue = SpikeQueue()
self.spiking_synapses = []
self.specifiers = {'_spiking_synapses': AttributeVariable(Unit(1),
Expand Down Expand Up @@ -159,11 +159,11 @@ def __init__(self, synapses, code, prepost, objname=None):
#: The simulation dt (necessary for the delays)
self.dt = self.synapses.clock.dt_

self.index = synapses.index
self.item_mapping = synapses.item_mapping

self.indices = {'_element': self.index,
'_presynaptic': self.index.synaptic_pre,
'_postsynaptic': self.index.synaptic_post}
self.indices = {'_element': self.item_mapping,
'_presynaptic': self.item_mapping.synaptic_pre,
'_postsynaptic': self.item_mapping.synaptic_post}

# Enable access to the delay attribute via the specifier
Group.__init__(self)
Expand Down Expand Up @@ -299,7 +299,7 @@ def _synapse_numbers(pre_neurons, post_neurons):
return synapse_numbers


class SynapticIndices(Index):
class SynapticItemMapping(Index):
'''
Convenience object to store the synaptic indices.
Expand Down Expand Up @@ -394,14 +394,14 @@ def _add_synapses(self, sources, targets, n, p, condition=None,
new_N = old_N + new_synapses
self._resize(new_N)

self.synaptic_pre[old_N:new_N] = self.source.index[sources]
self.synaptic_post[old_N:new_N] = self.target.index[targets]
self.synaptic_pre[old_N:new_N] = self.source.item_mapping[sources]
self.synaptic_post[old_N:new_N] = self.target.item_mapping[targets]
synapse_idx = old_N
for source, target in zip(sources, targets):
synapses = self.pre_synaptic[self.source.index[source]]
synapses = self.pre_synaptic[self.source.item_mapping[source]]
synapses.resize(len(synapses) + 1)
synapses[-1] = synapse_idx
synapses = self.post_synaptic[self.source.index[target]]
synapses = self.post_synaptic[self.source.item_mapping[target]]
synapses.resize(len(synapses) + 1)
synapses[-1] = synapse_idx
synapse_idx += 1
Expand All @@ -413,10 +413,10 @@ def _add_synapses(self, sources, targets, n, p, condition=None,
additional_namespace = ('implicit-namespace', namespace)
specifiers = {
'_source_neurons': ArrayVariable('_source_neurons', Unit(1),
self.source.index[:],
self.source.item_mapping[:],
constant=True),
'_target_neurons': ArrayVariable('_target_neurons', Unit(1),
self.target.index[:],
self.target.item_mapping[:],
constant=True),
# The template needs to have access to the DynamicArray here,
# having access to the underlying array (which would be much
Expand Down Expand Up @@ -652,11 +652,11 @@ def __init__(self, source, target=None, model=None, pre=None, post=None,
self._queues = {}
self._delays = {}

self.index = SynapticIndices(self)
self.item_mapping = SynapticItemMapping(self)
# Allow S.i instead of S.indices.i, etc.
self.i = self.index.i
self.j = self.index.j
self.k = self.index.k
self.i = self.item_mapping.i
self.j = self.item_mapping.j
self.k = self.item_mapping.k

# Setup specifiers
self.specifiers = self._create_specifiers()
Expand Down Expand Up @@ -710,7 +710,7 @@ def __init__(self, source, target=None, model=None, pre=None, post=None,
'specified in units '
'of seconds'))
updater = getattr(self, pathway)
self.index.unregister_variable(updater._delays)
self.item_mapping.unregister_variable(updater._delays)
del updater._delays
# For simplicity, store the delay as a one-element array
# so that for example updater._delays[:] works.
Expand Down Expand Up @@ -759,7 +759,7 @@ def __init__(self, source, target=None, model=None, pre=None, post=None,
# Activate name attribute access
Group.__init__(self)

N = property(fget=lambda self: self.index.N,
N = property(fget=lambda self: self.item_mapping.N,
doc='Total number of synapses')

def __len__(self):
Expand Down Expand Up @@ -851,15 +851,15 @@ def _create_specifiers(self):
constant=True),
'_synaptic_pre': DynamicArrayVariable('_synaptic_pre',
Unit(1),
self.index.synaptic_pre),
self.item_mapping.synaptic_pre),
'_synaptic_post': DynamicArrayVariable('_synaptic_pre',
Unit(1),
self.index.synaptic_post),
self.item_mapping.synaptic_post),
# We don't need "proper" specifier for these -- they go
# back to Python code currently
'_pre_synaptic': Variable(Unit(1), self.index.pre_synaptic),
'_pre_synaptic': Variable(Unit(1), self.item_mapping.pre_synaptic),
'_post_synaptic': Variable(Unit(1),
self.index.post_synaptic)})
self.item_mapping.post_synaptic)})

for eq in itertools.chain(self.equations.itervalues(),
self.event_driven.itervalues() if self.event_driven is not None else []):
Expand All @@ -878,7 +878,7 @@ def _create_specifiers(self):
is_bool=eq.is_bool)})
# Register the array with the `SynapticIndex` object so it gets
# automatically resized
self.index.register_variable(array)
self.item_mapping.register_variable(array)
elif eq.type == STATIC_EQUATION:
s.update({eq.varname: Subexpression(eq.unit,
brian_prefs['core.default_scalar_dtype'],
Expand Down Expand Up @@ -995,7 +995,7 @@ def connect(self, pre_or_cond, post=None, p=1., n=1, level=0):
if i.ndim > 1:
raise ValueError('Can only use 1-dimensional indices')

self.index._add_synapses(i, j, n, p, level=level+1)
self.item_mapping._add_synapses(i, j, n, p, level=level+1)
elif isinstance(pre_or_cond, (basestring, bool)):
if pre_or_cond is False:
return # nothing to do...
Expand All @@ -1011,7 +1011,7 @@ def connect(self, pre_or_cond, post=None, p=1., n=1, level=0):
if not isinstance(p, (float, basestring)):
raise TypeError('p has to be a float or a string evaluating '
'to an float, is type %s instead.' % type(n))
self.index._add_synapses(None, None, n, p, condition=pre_or_cond,
self.item_mapping._add_synapses(None, None, n, p, condition=pre_or_cond,
level=level+1)
else:
raise TypeError(('First argument has to be an index or a '
Expand Down

0 comments on commit 851c302

Please sign in to comment.