Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private variables #192

Merged
merged 8 commits into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions spynnaker8/models/data_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class DataCache(object):
and holds a cache for the variable specific data
"""

__slots__ = ("_cache",
"_description",
"_label",
"_rec_datetime",
"_recording_start_time",
"_segment_number",
"_t")
__slots__ = ("__cache",
"__description",
"__label",
"__rec_datetime",
"__recording_start_time",
"__segment_number",
"__t")

def __init__(self, label, description, segment_number,
recording_start_time, t):
Expand All @@ -31,45 +31,45 @@ def __init__(self, label, description, segment_number,
:param t: time
"""
# pylint: disable=too-many-arguments
self._label = label
self._description = description
self._segment_number = segment_number
self._recording_start_time = recording_start_time
self._t = t
self._cache = dict()
self._rec_datetime = None
self.__label = label
self.__description = description
self.__segment_number = segment_number
self.__recording_start_time = recording_start_time
self.__t = t
self.__cache = dict()
self.__rec_datetime = None

@property
def variables(self):
""" Provides a list of which variables data has been cached for

:rtype: Iterator (str)
"""
return self._cache.keys()
return self.__cache.keys()

@property
def label(self):
return self._label
return self.__label

@property
def description(self):
return self._description
return self.__description

@property
def segment_number(self):
return self._segment_number
return self.__segment_number

@property
def recording_start_time(self):
return self._recording_start_time
return self.__recording_start_time

@property
def t(self):
return self._t
return self.__t

@property
def rec_datetime(self):
return self._rec_datetime
return self.__rec_datetime

def has_data(self, variable):
""" Checks if data for a variable has been cached
Expand All @@ -79,7 +79,7 @@ def has_data(self, variable):
:return: True if there is cached data
:rtype: bool
"""
return variable in self._cache
return variable in self.__cache

def get_data(self, variable):
""" Get the variable cache for the named variable
Expand All @@ -89,7 +89,7 @@ def get_data(self, variable):
:return: The cache data, IDs, indexes and units
:rtype: VariableCache
"""
return self._cache[variable]
return self.__cache[variable]

def save_data(self, variable, data, indexes, n_neurons, units,
sampling_interval):
Expand All @@ -108,7 +108,7 @@ def save_data(self, variable, data, indexes, n_neurons, units,
:type units: str
:rtype: None
"""
self._rec_datetime = datetime.now()
self.__rec_datetime = datetime.now()
variable_cache = VariableCache(
data, indexes, n_neurons, units, sampling_interval)
self._cache[variable] = variable_cache
self.__cache[variable] = variable_cache
59 changes: 32 additions & 27 deletions spynnaker8/models/populations/idmixin.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
# Alternative implemenation for
# Alternative implementation for
# https://github.com/NeuralEnsemble/PyNN/blob/master/pyNN/common/populations.py


class IDMixin(object):
__slots__ = ["_id", "_population"]
__slots__ = ("__id", "__population")
__realslots__ = tuple("_IDMixin" + item for item in __slots__)

def __init__(self, population, id): # @ReservedAssignment
self._id = id
self._population = population
self.__id = id
self.__population = population

# NON-PYNN API CALL
# NON-PYNN API CALLS
@property
def id(self):
return self._id
return self.__id

@property
def _population(self):
return self.__population

def __getattr__(self, name):
try:
return self._population.get_by_selector(
selector=self._id, parameter_names=name)[0]
return self.__population.get_by_selector(
selector=self.__id, parameter_names=name)[0]
except Exception as ex:
try:
# try initialisable variable
return self._population.get_initial_value(
selector=self._id, variable=name)[0]
return self.__population.get_initial_value(
selector=self.__id, variable=name)[0]
except Exception:
# that failed too so raise the better original exception
raise ex

def __setattr__(self, name, value):
if name in self.__slots__:
if name in self.__realslots__:
object.__setattr__(self, name, value)
else:
try:
self._population.set_by_selector(self._id, name, value)
self.__population.set_by_selector(self.__id, name, value)
except Exception as ex:
try:
# try initialisable variable
return self._population.set_initial_value(
selector=self._id, variable=name, value=value)
return self.__population.set_initial_value(
selector=self.__id, variable=name, value=value)
except Exception:
# that failed too so raise the better original exception
raise ex
Expand All @@ -47,19 +52,19 @@ def set_parameters(self, **parameters):
arguments.
"""
for (name, value) in parameters.items():
self._population.set_by_selector(self._id, name, value)
self.__population.set_by_selector(self.__id, name, value)

def get_parameters(self):
""" Return a dict of all cell parameters.
"""
results = dict()
for name in self.celltype.get_parameter_names():
results[name] = self._population.get_by_selector(self._id, name)
results[name] = self.__population.get_by_selector(self.__id, name)
return results

@property
def celltype(self):
return self._population.celltype
return self.__population.celltype

@property
def is_standard_cell(self):
Expand All @@ -69,21 +74,21 @@ def _set_position(self, pos):
""" Set the cell position in 3D space.\
Cell positions are stored in an array in the parent Population.
"""
self._population.positions[self._id] = pos # pragma: no cover
self.__population.positions[self.__id] = pos # pragma: no cover

def _get_position(self):
""" Return the cell position in 3D space.\
Cell positions are stored in an array in the parent Population,\
if any, or within the ID object otherwise. Positions are generated\
the first time they are requested and then cached.
"""
return self._population.positions[:, self._id] # pragma: no cover
return self.__population.positions[:, self.__id] # pragma: no cover

position = property(_get_position, _set_position)

@property
def local(self):
return self._population.is_local(self._id)
return self.__population.is_local(self.__id)

def inject(self, current_source):
""" Inject current from a current source object into the cell.
Expand All @@ -93,31 +98,31 @@ def inject(self, current_source):
def get_initial_value(self, variable):
""" Get the initial value of a state variable of the cell.
"""
return self._population.get_initial_value(variable, self._id)
return self.__population.get_initial_value(variable, self.__id)

def set_initial_value(self, variable, value):
""" Set the initial value of a state variable of the cell.
"""
self._population.set_initial_value(variable, value, self._id)
self.__population.set_initial_value(variable, value, self.__id)

def as_view(self):
""" Return a PopulationView containing just this cell.
"""
return self._population[self._id]
return self.__population[self.__id]

def __eq__(self, other):
if not isinstance(other, IDMixin):
return False
return self._population == other._population and \
self._id == other._id
return self.__population == other._population and \
self.__id == other.id

def __ne__(self, other):
if not isinstance(other, IDMixin):
return True
return not self.__eq__(other)

def __str__(self):
return str(self._population) + "[" + str(self._id) + "]"
return str(self.__population) + "[" + str(self.__id) + "]"

def __repr__(self):
return repr(self._population) + "[" + str(self._id) + "]"
return repr(self.__population) + "[" + str(self.__id) + "]"
6 changes: 3 additions & 3 deletions spynnaker8/models/populations/population.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,13 @@ def describe(self, template='population_default.txt', engine='default'):
"structure": None,
"size": self.size,
"size_local": self.size,
"first_id": self._first_id,
"last_id": self._last_id,
"first_id": self.first_id,
"last_id": self.last_id,
}
context.update(self._annotations)
if self.size > 0:
context.update({
"local_first_id": self._first_id,
"local_first_id": self.first_id,
"cell_parameters": {}})
if self._structure:
context["structure"] = self._structure.describe(template=None)
Expand Down
1 change: 1 addition & 0 deletions spynnaker8/models/populations/population_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class PopulationBase(object):

Mainly pass through and not implemented
"""
__slots__ = []

@property
def local_cells(self):
Expand Down
Loading