Changeset 467

Show
Ignore:
Timestamp:
10/02/08 16:58:17 (2 months ago)
Author:
apdavison
Message:

pcsim Connectors now handle arrays of weights/delays (see ticket:116).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/__init__.py

    r466 r467  
    22__all__ = ["common", "random", "nest1", "nest2", "neuron", "neuron2", "pcsim", 'brian' ] 
    33 
     4# 
  • trunk/src/pcsim/__init__.py

    r465 r467  
    997997         
    998998        weight = self.getWeight(weight) 
    999         is_conductance = hasattr(self.post.pcsim_population.object(0),'ErevExc') 
    1000         if isinstance(weight, pyNN.random.RandomDistribution)
     999        self.is_conductance = hasattr(self.post.pcsim_population.object(0),'ErevExc') 
     1000        if isinstance(weight, pyNN.random.RandomDistribution) or hasattr(weight, '__len__')
    10011001            w = 1. 
    10021002        else: 
    1003             w = self.convertWeight(weight, is_conductance) 
     1003            w = self.convertWeight(weight, self.is_conductance) 
    10041004         
    10051005        delay  = self.getDelay(delay) 
    1006         if isinstance(delay, pyNN.random.RandomDistribution)
     1006        if isinstance(delay, pyNN.random.RandomDistribution) or hasattr(delay, '__len__')
    10071007            d = pcsim_globals.minDelay/1000. 
    10081008        else: 
     
    10331033        if isinstance(weight, pyNN.random.RandomDistribution): 
    10341034            self.randomizeWeights(weight) 
     1035        elif hasattr(weight, '__len__'): 
     1036            assert len(weight) == len(self), "Weight array does not have the same number of elements as the Projection %d != %d" % (len(weight),len(self)) 
     1037            self.setWeights(weight) 
    10351038         
    10361039        if isinstance(delay, pyNN.random.RandomDistribution): 
    10371040            self.randomizeDelays(delay) 
     1041        elif hasattr(delay, '__len__'): 
     1042            assert len(delay) == len(self), "Weight array does not have the same number of elements as the Projection %d != %d" % (len(weight),len(self)) 
     1043            self.setDelays(delay) 
    10381044         
    10391045        if not label: 
     
    10611067        synapses. 
    10621068        """ 
    1063         is_conductance = hasattr(self.post.pcsim_population.object(0),'ErevExc') 
    1064         w = self.convertWeight(w, is_conductance) 
     1069        w = self.convertWeight(w, self.is_conductance) 
    10651070        if isinstance(w, float) or isinstance(w, int): 
    10661071            for i in range(len(self)): 
     
    10771082        # argument type. It could make for easier-to-read simulation code to 
    10781083        # give it a separate name, though. Comments? 
    1079         is_conductance = hasattr(self.post.pcsim_population.object(0),'ErevExc') 
    1080         rand_distr = self.convertWeight(rand_distr, is_conductance) 
     1084        rand_distr = self.convertWeight(rand_distr, self.is_conductance) 
    10811085        weights = rand_distr.next(len(self)) 
    10821086        for i in range(len(self)): 
     
    10941098                pcsim_globals.net.object(self.pcsim_projection[i]).delay = d 
    10951099        else: 
     1100            assert 1000.0*min(d) >= pcsim_globals.minDelay, "Smallest delay %g ms must be smaller than %g ms" % (min(d), pcsim_globals.minDelay) 
    10961101            for i in range(len(self)): 
    10971102                pcsim_globals.net.object(self.pcsim_projection[i]).delay = d[i] 
     
    11051110        for i in range(len(self)): 
    11061111            pcsim_globals.net.object(self.pcsim_projection[i]).delay = delays[i] 
     1112     
     1113    def getWeights(self, format='list', gather=True): 
     1114        """ 
     1115        Possible formats are: a list of length equal to the number of connections 
     1116        in the projection, a 2D weight array (with zero or None for non-existent 
     1117        connections). 
     1118        """ 
     1119        if format == 'list': 
     1120            if self.is_conductance: 
     1121                A = 1e6 # S --> uS 
     1122            else: 
     1123                A = 1e9 # A --> nA 
     1124            return [A*self.pcsim_projection.object(i).W for i in xrange(self.pcsim_projection.size())] 
     1125        elif format == 'array': 
     1126            raise Exception("Not yet implemented") 
     1127        else: 
     1128            raise Exception("Valid formats are 'list' and 'array'") 
     1129     
     1130    def getDelays(self, format='list', gather=True): 
     1131        """ 
     1132        Possible formats are: a list of length equal to the number of connections 
     1133        in the projection, a 2D weight array (with zero or None for non-existent 
     1134        connections). 
     1135        """ 
     1136        if format == 'list': 
     1137            A = 1e3 # s --> ms 
     1138            return [A*self.pcsim_projection.object(i).delay for i in xrange(self.pcsim_projection.size())] 
     1139        elif format == 'array': 
     1140            raise Exception("Not yet implemented") 
     1141        else: 
     1142            raise Exception("Valid formats are 'list' and 'array'") 
    11071143     
    11081144    # --- Methods for writing/reading information to/from file. ---------------- 
  • trunk/src/pcsim/connectors.py

    r466 r467  
    9090        # pcsim does not yet deal with having lists of weights, delays, so for now we just return 0 values 
    9191        # and will set the weights, delays later 
    92         return decider, wiring_method, 0.0, 0.0 
     92        return decider, wiring_method, self.weights, self.delays 
    9393 
    9494class FromFileConnector(common.FromFileConnector): 
     
    114114        # pcsim does not yet deal with having lists of weights, delays, so for now we just return 0 values 
    115115        # and will set the weights, delays later 
    116         return decider, wiring_method, 0.0, 0.0 
     116        return decider, wiring_method, self.weights, self.delays 
  • trunk/test/unittests/pcsimtests_population.py

    r466 r467  
    1616import numpy.random 
    1717 
     18def arrays_almost_equal(a, b, threshold): 
     19    return (abs(a-b) < threshold).all() 
     20 
     21def max_array_diff(a, b): 
     22    return max(abs(a-b)) 
    1823 
    1924class PopulationInitTest(unittest.TestCase): 
     
    341346         
    342347    def testWithWeightArray(self): 
    343         prj2 = Projection(self.source33, self.target33, OneToOneConnector(weights=numpy.linspace(0.1,0.9,9))) 
    344         assert len(prj1) == self.source33.size # just check there are no Exceptions raised for now. Need also to check weights are properly set 
    345 #          
     348        w = numpy.linspace(0.1,0.9,9) 
     349        prj1 = Projection(self.source33, self.target33, OneToOneConnector(weights=w)) 
     350        assert len(prj1) == self.source33.size 
     351        w_out = numpy.array(prj1.getWeights(format='list')) 
     352        assert arrays_almost_equal(w_out, w, 1e-7), "Max difference is %g" % max_array_diff(w_out,w) 
     353     
     354    def testWithDelayArray(self): 
     355        d = numpy.linspace(1.1,1.9,9) 
     356        prj1 = Projection(self.source33, self.target33, OneToOneConnector(delays=d)) 
     357        assert len(prj1) == self.source33.size 
     358        d_out = numpy.array(prj1.getDelays(format='list')) 
     359        assert arrays_almost_equal(d_out, d, 1e-7), "Max difference is %g" % max_array_diff(d_out,d) 
     360     
    346361#     def testSaveAndLoad(self): 
    347362#         prj1 = neuron.Projection(self.source33, self.target33, 'oneToOne')