Changeset 362

Show
Ignore:
Timestamp:
06/12/08 18:22:53 (5 months ago)
Author:
apdavison
Message:

Copied the changes made to trunk in r361

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/0.4/src/neuron/__init__.py

    r352 r362  
    13051305        return c.connect(self) 
    13061306     
    1307     def _fixedNumberPre(self, parameters): 
    1308         """Each presynaptic cell makes a fixed number of connections.""" 
     1307    def _fixedNumber(self, parameters, connector_class): 
    13091308        allow_self_connections = True 
    13101309        if type(parameters) == types.IntType: 
    13111310            n = parameters 
    13121311            assert n > 0 
    1313             fixed = True 
    13141312        elif type(parameters) == types.DictType: 
    13151313            if parameters.has_key('n'): # all cells have same number of connections 
    13161314                n = int(parameters['n']) 
    13171315                assert n > 0 
    1318                 fixed = True 
    13191316            elif parameters.has_key('rand_distr'): # number of connections per cell follows a distribution 
    1320                 rand_distr = parameters['rand_distr'] 
    1321                 assert isinstance(rand_distr, RandomDistribution) 
    1322                 fixed = False 
     1317                n = parameters['rand_distr'] 
     1318                assert isinstance(n, RandomDistribution) 
    13231319            if parameters.has_key('allow_self_connections'): 
    13241320                allow_self_connections = parameters['allow_self_connections'] 
    13251321        elif isinstance(parameters, RandomDistribution): 
    1326             rand_distr = parameters 
    1327             fixed = False 
     1322            n = parameters 
    13281323        else: 
    13291324            raise Exception("Invalid argument type: should be an integer, dictionary or RandomDistribution object.") 
    1330         hoc_commands = [] 
    1331          
    1332         if self.rng: 
    1333             rng = self.rng 
    1334         else: 
    1335             rng = numpy.random 
    1336         for src in self.pre.gidlist:             
    1337             # pick n neurons at random 
    1338             if not fixed: 
    1339                 n = rand_distr.next() 
    1340             for tgt in rng.permutation(self.post.gidlist)[0:n]: 
    1341                 if allow_self_connections or (src != tgt): 
    1342                     hoc_commands += self.__connect(src, tgt) 
    1343         return hoc_commands 
     1325        c = connector_class(n=n, allow_self_connections=allow_self_connections) 
     1326        return c.connect(self) 
     1327     
     1328    def _fixedNumberPre(self, parameters): 
     1329        """Each presynaptic cell makes a fixed number of connections.""" 
     1330        return self._fixedNumber(parameters, FixedNumberPreConnector) 
    13441331             
    13451332    def _fixedNumberPost(self, parameters): 
    13461333        """Each postsynaptic cell receives a fixed number of connections.""" 
    1347         allow_self_connections = True 
    1348         if type(parameters) == types.IntType: 
    1349             n = parameters 
    1350             assert n > 0 
    1351             fixed = True 
    1352         elif type(parameters) == types.DictType: 
    1353             if parameters.has_key('n'): # all cells have same number of connections 
    1354                 n = int(parameters['n']) 
    1355                 assert n > 0 
    1356                 fixed = True 
    1357             elif parameters.has_key('rand_distr'): # number of connections per cell follows a distribution 
    1358                 rand_distr = parameters['rand_distr'] 
    1359                 assert isinstance(rand_distr, RandomDistribution) 
    1360                 fixed = False 
    1361             if parameters.has_key('allow_self_connections'): 
    1362                 allow_self_connections = parameters['allow_self_connections'] 
    1363         elif isinstance(parameters, RandomDistribution): 
    1364             rand_distr = parameters 
    1365             fixed = False 
    1366         else: 
    1367             raise Exception("Invalid argument type: should be an integer, dictionary or RandomDistribution object.") 
    1368         hoc_commands = [] 
    1369          
    1370         if self.rng: 
    1371             rng = self.rng 
    1372         else: 
    1373             rng = numpy.random 
    1374         for tgt in self.post.gidlist:             
    1375             # pick n neurons at random 
    1376             if not fixed: 
    1377                 n = rand_distr.next() 
    1378             for src in rng.permutation(self.pre.gidlist)[0:n]: 
    1379                 if allow_self_connections or (src != tgt): 
    1380                     hoc_commands += self.__connect(src, tgt) 
    1381         return hoc_commands 
     1334        return self._fixedNumber(parameters, FixedNumberPostConnector) 
    13821335     
    13831336    def _fromFile(self, parameters): 
  • branches/0.4/src/neuron/connectors.py

    r336 r362  
    195195        return hoc_commands 
    196196 
    197  
    198 class FixedNumberPreConnector(common.FixedNumberPreConnector, HocConnector): 
    199      
    200     def connect(self, projection): 
    201         raise Exception("Not implemented yet !") 
    202  
    203  
    204 class FixedNumberPostConnector(common.FixedNumberPostConnector, HocConnector): 
    205      
    206     def connect(self, projection): 
    207         raise Exception("Not implemented yet !") 
     197class _FixedNumberConnector(common.FixedNumberPreConnector, HocConnector): 
     198     
     199    def _connect(self, projection, x_list, y_list, type): 
     200        weight = self.getWeight(self.weights) 
     201        delay = self.getDelay(self.delays) 
     202        hoc_commands = [] 
     203         
     204        if projection.rng: 
     205            if isinstance(projection.rng, NativeRNG): 
     206                raise Exception("NativeRNG not yet supported for the FixedNumberPreConnector") 
     207            rng = projection.rng 
     208        else: 
     209            rng = numpy.random 
     210        for y in y_list:             
     211            # pick n neurons at random 
     212            if hasattr(self, 'rand_distr'): 
     213                n = self.rand_distr.next() 
     214            elif hasattr(self, 'n'): 
     215                n = self.n 
     216            candidates = x_list 
     217            xs = [] 
     218            while len(xs) < n: # if the number of requested cells is larger than the size of the 
     219                                    # presynaptic population, we allow multiple connections for a given cell 
     220                xs += [candidates[candidates.index(id)] for id in rng.permutation(candidates)[0:n]] 
     221                # have to use index() because rng.permutation returns ints, not ID objects 
     222            xs = xs[:n] 
     223            for x in xs: 
     224                if self.allow_self_connections or (x != y): 
     225                    if hasattr(weight, 'next'): 
     226                        w = weight.next() 
     227                    else: 
     228                        w = weight 
     229                    if hasattr(delay, 'next'): 
     230                        d = delay.next() 
     231                    else: 
     232                        d = delay 
     233                    if type == 'pre': 
     234                        hoc_commands += self.singleConnect(projection, x, y, w, d) 
     235                    elif type == 'post': 
     236                        hoc_commands += self.singleConnect(projection, y, x, w, d) 
     237                    else: 
     238                        raise Exception('Problem in _FixedNumberConnector') 
     239        return hoc_commands 
     240 
     241 
     242class FixedNumberPreConnector(_FixedNumberConnector): 
     243     
     244    def connect(self, projection): 
     245        return self._connect(projection, projection.pre.gidlist, projection.post.gidlist, 'pre') 
     246 
     247 
     248class FixedNumberPostConnector(_FixedNumberConnector): 
     249      
     250    def connect(self, projection): 
     251        return self._connect(projection, projection.post.gidlist, projection.pre.gidlist, 'post') 
    208252 
    209253 
  • branches/0.4/test/neurontests.py

    r335 r362  
    578578                assert prj1.connections == prj2.connections, "%s %s" % (rngclass, expr) 
    579579 
     580    def testFixedNumberPre(self): 
     581        c1 = neuron.FixedNumberPreConnector(10) 
     582        c2 = neuron.FixedNumberPreConnector(3) 
     583        c3 = neuron.FixedNumberPreConnector(random.RandomDistribution('poisson',[5])) 
     584        for srcP in [self.source5, self.source22]: 
     585            for tgtP in [self.target6, self.target33]: 
     586                for c in c1, c2: 
     587                    prj1 = neuron.Projection(srcP, tgtP, c) 
     588                    self.assertEqual(len(prj1.connections), c.n*len(tgtP)) 
     589                prj3 = neuron.Projection(srcP, tgtP, c3) # just a test that no Exceptions are raised 
     590 
     591    def testFixedNumberPost(self): 
     592        c1 = neuron.FixedNumberPostConnector(10) 
     593        c2 = neuron.FixedNumberPostConnector(3) 
     594        c3 = neuron.FixedNumberPostConnector(random.RandomDistribution('poisson',[5])) 
     595        for srcP in [self.source5, self.source22]: 
     596            for tgtP in [self.target6, self.target33]: 
     597                for c in c1, c2: 
     598                    prj1 = neuron.Projection(srcP, tgtP, c) 
     599                    self.assertEqual(len(prj1.connections), c.n*len(srcP)) 
     600                prj2 = neuron.Projection(srcP, tgtP, c3) # just a test that no Exceptions are raised 
     601 
    580602    def testSaveAndLoad(self): 
    581603        prj1 = neuron.Projection(self.source33, self.target33, 'oneToOne')