Changeset 725

Show
Ignore:
Timestamp:
03/10/10 11:16:33 (2 years ago)
Author:
apdavison
Message:

A few connector-related fixes, plus a new unit test for distance-dependent weights.

Location:
trunk
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/connectors.py

    r722 r725  
    1919from numpy import arccos, arcsin, arctan, arctan2, ceil, cos, cosh, e, exp, \ 
    2020                  fabs, floor, fmod, hypot, ldexp, log, log10, modf, pi, power, \ 
    21                   sin, sinh, sqrt, tan, tanh 
     21                  sin, sinh, sqrt, tan, tanh, maximum, minimum 
    2222from pyNN import random, common, errors, core 
    2323from pyNN.space import Space 
     
    5858    """Base class for Connector classes.""" 
    5959     
    60     def __init__(self, weights=0.0, delays=None): 
     60    def __init__(self, weights=0.0, delays=None, space=None): 
    6161        self.w_index = 0 # should probably use a generator 
    6262        self.d_index = 0 # rather than storing these values 
     
    7171        else: 
    7272            self.delays = delays 
    73          
    7473        if delays is None: 
    7574            self.delays = common.get_min_delay() 
     75        if space is not None: 
     76            assert isinstance(space, Space) 
     77            self.space = space 
    7678         
    7779    def connect(self, projection): 
     
    9597        return delays 
    9698 
     99    def reset(self): 
     100        """ 
     101        After running connect(), this should be called to reset attributes that 
     102        have been modified to their initial values. 
     103        """ 
     104        if hasattr(self, "w_expr"): 
     105            self.weights = numpy.empty((1,0)) 
     106        if hasattr(self, "d_expr"): 
     107            self.delays = numpy.empty((1,0)) 
     108        self.w_index = 0 # should probably use a generator 
     109        self.d_index = 0 # rather than storing these values 
     110     
    97111     
    98112class ProbabilisticConnector(Connector): 
     
    147161            if len(targets) > 0: 
    148162                projection.connection_manager.connect(src, targets, weights, delays) 
    149  
     163        self.reset()         
    150164 
    151165class AllToAllConnector(ProbabilisticConnector): 
     
    169183                     to the global minimum delay. 
    170184        """ 
    171         Connector.__init__(self, weights, delays) 
     185        Connector.__init__(self, weights, delays, space) 
    172186        assert isinstance(allow_self_connections, bool) 
    173187        self.allow_self_connections = allow_self_connections 
     
    479493                     to the global minimum delay. 
    480494        """ 
    481         Connector.__init__(self, weights, delays) 
     495        Connector.__init__(self, weights, delays, space) 
    482496        assert isinstance(allow_self_connections, bool) 
    483497        self.allow_self_connections = allow_self_connections 
     
    521535                     to the global minimum delay. 
    522536        """ 
    523         Connector.__init__(self, weights, delays) 
     537        Connector.__init__(self, weights, delays, space) 
    524538        assert isinstance(d_expression, str) 
    525539        try: 
     
    529543            raise ZeroDivisionError("Error in the distance expression %s. %s" % (d_expression, err)) 
    530544        self.d_expression = d_expression 
    531         self.space = space 
    532545        assert isinstance(allow_self_connections, bool) 
    533546        self.allow_self_connections = allow_self_connections 
    534         assert isinstance(space, Space) 
    535         self.space = space 
    536547         
    537548         
  • trunk/src/nest/simulator.py

    r718 r725  
    219219        """The ID of the post-synaptic neuron.""" 
    220220        tgt = ID(nest.GetStatus([self.id()], 'target')[0]) 
    221         tgt.parent = self.parent.parent.pre 
     221        tgt.parent = self.parent.parent.post 
    222222        tgt.local = nest.GetStatus([tgt], 'local')[0] 
    223223        return tgt 
  • trunk/test/unittests/generictests.py

    r720 r725  
    99import os 
    1010import cPickle as pickle 
    11 from pyNN import common, random, utility, recording, errors 
     11from pyNN import common, random, utility, recording, errors, space 
    1212import glob 
    1313 
     
    873873                    self.assertAlmostEqual(prj1.connections[0].delay, 0.4, 6) # nest rounds delays to the timestep 
    874874          
     875    def testDistanceDependentWeights(self): 
     876        connectors = ( 
     877            sim.AllToAllConnector(weights="exp(-d/10.0)", space=space.Space(scale_factor=0.9)), 
     878            sim.FixedProbabilityConnector(0.8, weights="maximum(3-d, 0)", space=space.Space(offset=0.1)), 
     879            sim.DistanceDependentProbabilityConnector("abs(d<3)", weights="sin(d)", space=space.Space(offset=0.1, 
     880                                                                                                      scale_factor=0.9)), 
     881        ) 
     882        exp = numpy.exp 
     883        maximum = numpy.maximum 
     884        sin = numpy.sin 
     885        for srcP in [self.source5, self.source22]: 
     886            for tgtP in [self.target6, self.target33]: 
     887                for conn in connectors: 
     888                    print conn.w_expr 
     889                    prj = sim.Projection(srcP, tgtP, conn) 
     890                    first_connection = prj.connections[0] 
     891                    last_connection = prj.connections[-1] 
     892                    for c in first_connection, last_connection: 
     893                       d = space.distance(c.source, c.target) 
     894                       self.assertAlmostEqual(c.weight, eval(conn.w_expr), 10) 
    875895 
    876896class ProjectionSetTest(unittest.TestCase):