Skip to content

Commit

Permalink
Merge pull request #698 from apdavison/improve-warnings
Browse files Browse the repository at this point in the history
Move warnings about missing features to the point where they are used
  • Loading branch information
apdavison committed Dec 18, 2020
2 parents 649693a + 08da731 commit 1ffc91c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 60 deletions.
6 changes: 2 additions & 4 deletions pyNN/connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,10 +939,8 @@ def __init__(self, cset, safe=True, callback=None):
"""
Connector.__init__(self, safe=safe, callback=callback)
self.cset = cset
if csa.arity(cset) == 0:
pass
else:
assert csa.arity(cset) == 2, 'must specify mask or connection-set with arity 2'
arity = csa.arity(cset)
assert arity in (0, 2), 'must specify mask or connection-set with arity 0 or 2'
else:
def __init__(self, cset, safe=True, callback=None):
raise RuntimeError("CSAConnector not available---couldn't import csa module")
Expand Down
5 changes: 0 additions & 5 deletions pyNN/nest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@

logger = logging.getLogger("PyNN")

try:
nest.Install('pynn_extensions')
except nest.kernel.NESTError as err:
warnings.warn("Unable to install NEST extensions. Certain models may not be available.")


# ==============================================================================
# Utility functions
Expand Down
83 changes: 35 additions & 48 deletions pyNN/nest/connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

import logging
from warnings import warn
import nest
try:
import csa
Expand All @@ -28,70 +29,56 @@
FromFileConnector,
CloneConnector,
ArrayConnector,
FixedTotalNumberConnector)
FixedTotalNumberConnector,
CSAConnector as DefaultCSAConnector)

from .random import NativeRNG, NEST_RDEV_TYPES


logger = logging.getLogger("PyNN")


if not nest.ll_api.sli_func("statusdict/have_libneurosim ::"):
class CSAConnector(DefaultCSAConnector):
"""
Use the Connection-Set Algebra (Djurfeldt, 2012) to connect
cells. This is an optimized variant of CSAConnector, which
iterates the connection-set on the C++ level in NEST.
print(("CSAConnector: libneurosim support not available in NEST.\n" +
"Falling back on PyNN's default CSAConnector.\n" +
"Please re-compile NEST using --with-libneurosim=PATH"))
See Djurfeldt et al. (2014) doi:10.3389/fninf.2014.00043 for
more details about the new interface and a comparison between
this and PyNN's native CSAConnector.
from pyNN.connectors import CSAConnector
Takes any of the standard :class:`Connector` optional
arguments and, in addition:
else:
`cset`:
a connection set object.
"""

class CSAConnector(Connector):
"""
Use the Connection-Set Algebra (Djurfeldt, 2012) to connect
cells. This is an optimized variant of CSAConnector, which
iterates the connection-set on the C++ level in NEST.
See Djurfeldt et al. (2014) doi:10.3389/fninf.2014.00043 for
more details about the new interface and a comparison between
this and PyNN's native CSAConnector.
Takes any of the standard :class:`Connector` optional
arguments and, in addition:
`cset`:
a connection set object.
"""
parameter_names = ('cset',)

if haveCSA:
def __init__(self, cset, safe=True, callback=None):
"""
"""
Connector.__init__(self, safe=safe, callback=callback)
self.cset = cset
arity = csa.arity(cset)
assert arity in (0, 2), 'must specify mask or connection-set with arity 0 or 2'
def connect(self, projection):
if nest.ll_api.sli_func("statusdict/have_libneurosim ::"):
return self.cg_connect(projection)
else:
def __init__(self, cset, safe=True, callback=None):
raise RuntimeError("CSAConnector not available---couldn't import csa module")
warn("Note: using the default CSAConnector. To use the accelerated version for NEST,\n"
"Please re-compile NEST using --with-libneurosim=PATH")
return super(CSAConnector, self).connect(projection)

def connect(self, projection):
"""Connect-up a Projection."""
def cg_connect(self, projection):
"""Connect-up a Projection using the Connection Generator interface"""

presynaptic_cells = projection.pre.all_cells.astype('int64')
postsynaptic_cells = projection.post.all_cells.astype('int64')
presynaptic_cells = projection.pre.all_cells.astype('int64')
postsynaptic_cells = projection.post.all_cells.astype('int64')

if csa.arity(self.cset) == 2:
param_map = {'weight': 0, 'delay': 1}
nest.CGConnect(presynaptic_cells, postsynaptic_cells, self.cset,
param_map, projection.nest_synapse_model)
else:
nest.CGConnect(presynaptic_cells, postsynaptic_cells, self.cset,
model=projection.nest_synapse_model)
if csa.arity(self.cset) == 2:
param_map = {'weight': 0, 'delay': 1}
nest.CGConnect(presynaptic_cells, postsynaptic_cells, self.cset,
param_map, projection.nest_synapse_model)
else:
nest.CGConnect(presynaptic_cells, postsynaptic_cells, self.cset,
model=projection.nest_synapse_model)

projection._connections = None # reset the caching of the connection list, since this will have to be recalculated
projection._sources.extend(presynaptic_cells)
projection._connections = None # reset the caching of the connection list, since this will have to be recalculated
projection._sources.extend(presynaptic_cells)


class NESTConnectorMixin(object):
Expand Down
5 changes: 5 additions & 0 deletions pyNN/nest/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class _State(common.control.BaseState):

def __init__(self):
super(_State, self).__init__()
try:
nest.Install('pynn_extensions')
self.extensions_loaded = True
except nest.kernel.NESTError as err:
self.extensions_loaded = False
self.initialized = False
self.optimize = False
self.spike_precision = "off_grid"
Expand Down
4 changes: 2 additions & 2 deletions pyNN/nest/standardmodels/synapses.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class StaticSynapse(synapses.StaticSynapse, NESTSynapseMixin):

class STDPMechanism(synapses.STDPMechanism, NESTSynapseMixin):
"""Specification of STDP models."""

base_translations = build_translations(
('weight', 'weight', 1000.0), # nA->pA, uS->nS
('delay', 'delay'),
('dendritic_delay_fraction', 'dendritic_delay_fraction')
) # will be extended by translations from timing_dependence, etc.

def __init__(self, timing_dependence=None, weight_dependence=None,
voltage_dependence=None, dendritic_delay_fraction=1.0,
weight=0.0, delay=None):
Expand Down
11 changes: 10 additions & 1 deletion pyNN/nest/synapses.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import nest

from pyNN.models import BaseSynapseType
from pyNN.errors import NoModelAvailableError
from .simulator import state
from .conversion import make_pynn_compatible, make_sli_compatible

Expand Down Expand Up @@ -40,7 +41,15 @@ def _get_nest_synapse_model(self):
synapse_defaults[name] = value.evaluate(simplify=True)
synapse_defaults = make_sli_compatible(synapse_defaults)
synapse_defaults.pop("tau_minus", None)
nest.SetDefaults(self.nest_name + '_lbl', synapse_defaults)
try:
nest.SetDefaults(self.nest_name + '_lbl', synapse_defaults)
except nest.lib.hl_api_exceptions.NESTError as err:
if not state.extensions_loaded:
raise NoModelAvailableError(
"{self.__class__.__name__} is not available."
"There was a problem loading NEST extensions".format(self=self)
)
raise
return self.nest_name + '_lbl'

def _get_minimum_delay(self):
Expand Down

0 comments on commit 1ffc91c

Please sign in to comment.