| 269 | | |
| 270 | | |
| 271 | | # ============================================================================== |
| 272 | | # Standard cells |
| 273 | | # ============================================================================== |
| 274 | | |
| 275 | | def build_translations(*translation_list): |
| 276 | | """ |
| 277 | | Build a translation dictionary from a list of translations/transformations. |
| 278 | | """ |
| 279 | | translations = {} |
| 280 | | for item in translation_list: |
| 281 | | assert 2 <= len(item) <= 4, "Translation tuples must have between 2 and 4 items. Actual content: %s" % str(item) |
| 282 | | pynn_name = item[0] |
| 283 | | sim_name = item[1] |
| 284 | | if len(item) == 2: # no transformation |
| 285 | | f = pynn_name |
| 286 | | g = sim_name |
| 287 | | elif len(item) == 3: # simple multiplicative factor |
| 288 | | scale_factor = item[2] |
| 289 | | f = "float(%g)*%s" % (scale_factor, pynn_name) |
| 290 | | g = "%s/float(%g)" % (sim_name, scale_factor) |
| 291 | | elif len(item) == 4: # more complex transformation |
| 292 | | f = item[2] |
| 293 | | g = item[3] |
| 294 | | translations[pynn_name] = {'translated_name': sim_name, |
| 295 | | 'forward_transform': f, |
| 296 | | 'reverse_transform': g} |
| 297 | | return translations |
| 298 | | |
| 299 | | class StandardModelType(object): |
| 300 | | """Base class for standardized cell model and synapse model classes.""" |
| 301 | | |
| 302 | | translations = {} |
| 303 | | default_parameters = {} |
| 304 | | |
| 305 | | def __init__(self, parameters): |
| 306 | | self.parameters = self.__class__.checkParameters(parameters, with_defaults=True) |
| 307 | | self.parameters = self.__class__.translate(self.parameters) |
| 308 | | |
| 309 | | @classmethod |
| 310 | | def checkParameters(cls, supplied_parameters, with_defaults=False): |
| 311 | | """ |
| 312 | | Returns a parameter dictionary, checking that each |
| 313 | | supplied_parameter is in the default_parameters and |
| 314 | | converts to the type of the latter. |
| 315 | | |
| 316 | | If with_defaults==True, parameters not in |
| 317 | | supplied_parameters are in the returned dictionary |
| 318 | | as in default_parameters. |
| 319 | | |
| 320 | | """ |
| 321 | | default_parameters = cls.default_parameters |
| 322 | | if with_defaults: |
| 323 | | parameters = copy.copy(default_parameters) |
| 324 | | else: |
| 325 | | parameters = {} |
| 326 | | if supplied_parameters: |
| 327 | | for k in supplied_parameters.keys(): |
| 328 | | if default_parameters.has_key(k): |
| 329 | | err_msg = "For %s in %s, expected %s, got %s (%s)" % \ |
| 330 | | (k, cls.__name__, type(default_parameters[k]), |
| 331 | | type(supplied_parameters[k]), supplied_parameters[k]) |
| 332 | | # same type |
| 333 | | if type(supplied_parameters[k]) == type(default_parameters[k]): |
| 334 | | parameters[k] = supplied_parameters[k] |
| 335 | | # float and something that can be converted to a float |
| 336 | | elif type(default_parameters[k]) == types.FloatType: |
| 337 | | try: |
| 338 | | parameters[k] = float(supplied_parameters[k]) |
| 339 | | except (ValueError, TypeError): |
| 340 | | raise errors.InvalidParameterValueError(err_msg) |
| 341 | | # list and something that can be transformed to a list |
| 342 | | elif type(default_parameters[k]) == types.ListType: |
| 343 | | try: |
| 344 | | parameters[k] = list(supplied_parameters[k]) |
| 345 | | except TypeError: |
| 346 | | raise errors.InvalidParameterValueError(err_msg) |
| 347 | | else: |
| 348 | | raise errors.InvalidParameterValueError(err_msg) |
| 349 | | else: |
| 350 | | raise errors.NonExistentParameterError(k, cls) |
| 351 | | return parameters |
| 352 | | |
| 353 | | @classmethod |
| 354 | | def translate(cls, parameters): |
| 355 | | """Translate standardized model parameters to simulator-specific parameters.""" |
| 356 | | parameters = cls.checkParameters(parameters, with_defaults=False) |
| 357 | | native_parameters = {} |
| 358 | | for name in parameters: |
| 359 | | D = cls.translations[name] |
| 360 | | pname = D['translated_name'] |
| 361 | | if is_listlike(cls.default_parameters[name]): |
| 362 | | parameters[name] = numpy.array(parameters[name]) |
| 363 | | try: |
| 364 | | pval = eval(D['forward_transform'], globals(), parameters) |
| 365 | | except NameError, errmsg: |
| 366 | | raise NameError("Problem translating '%s' in %s. Transform: '%s'. Parameters: %s. %s" \ |
| 367 | | % (pname, cls.__name__, D['forward_transform'], parameters, errmsg)) |
| 368 | | except ZeroDivisionError: |
| 369 | | pval = 1e30 # this is about the highest value hoc can deal with |
| 370 | | native_parameters[pname] = pval |
| 371 | | return native_parameters |
| 372 | | |
| 373 | | @classmethod |
| 374 | | def reverse_translate(cls, native_parameters): |
| 375 | | """Translate simulator-specific model parameters to standardized parameters.""" |
| 376 | | standard_parameters = {} |
| 377 | | for name,D in cls.translations.items(): |
| 378 | | if is_listlike(cls.default_parameters[name]): |
| 379 | | tname = D['translated_name'] |
| 380 | | native_parameters[tname] = numpy.array(native_parameters[tname]) |
| 381 | | try: |
| 382 | | standard_parameters[name] = eval(D['reverse_transform'], {}, native_parameters) |
| 383 | | except NameError, errmsg: |
| 384 | | raise NameError("Problem translating '%s' in %s. Transform: '%s'. Parameters: %s. %s" \ |
| 385 | | % (name, cls.__name__, D['reverse_transform'], native_parameters, errmsg)) |
| 386 | | return standard_parameters |
| 387 | | |
| 388 | | @classmethod |
| 389 | | def simple_parameters(cls): |
| 390 | | """Return a list of parameters for which there is a one-to-one |
| 391 | | correspondance between standard and native parameter values.""" |
| 392 | | return [name for name in cls.translations if cls.translations[name]['forward_transform'] == name] |
| 393 | | |
| 394 | | @classmethod |
| 395 | | def scaled_parameters(cls): |
| 396 | | """Return a list of parameters for which there is a unit change between |
| 397 | | standard and native parameter values.""" |
| 398 | | return [name for name in cls.translations if "float" in cls.translations[name]['forward_transform']] |
| 399 | | |
| 400 | | @classmethod |
| 401 | | def computed_parameters(cls): |
| 402 | | """Return a list of parameters whose values must be computed from |
| 403 | | more than one other parameter.""" |
| 404 | | return [name for name in cls.translations if name not in cls.simple_parameters()+cls.scaled_parameters()] |
| 405 | | |
| 406 | | def update_parameters(self, parameters): |
| 407 | | """ |
| 408 | | update self.parameters with those in parameters |
| 409 | | """ |
| 410 | | self.parameters.update(self.translate(parameters)) |
| 411 | | |
| 412 | | def describe(self, template='standard'): |
| 413 | | return str(self) |
| 414 | | |
| 415 | | |
| 416 | | class StandardCellType(StandardModelType): |
| 417 | | """Base class for standardized cell model classes.""" |
| 418 | | |
| 419 | | recordable = ['spikes', 'v', 'gsyn'] |
| 420 | | synapse_types = ('excitatory', 'inhibitory') |
| 421 | | conductance_based = True # over-ride for cells with current-based synapses |
| 422 | | always_local = False # over-ride for NEST spike sources |
| 423 | | |
| 424 | | |
| 425 | | class ModelNotAvailable(object): |
| 426 | | """Not available for this simulator.""" |
| 427 | | |
| 428 | | def __init__(self, *args, **kwargs): |
| 429 | | raise NotImplementedError("The %s model is not available for this simulator." % self.__class__.__name__) |
| | 247 | |
| 1378 | | # Synapse Dynamics classes |
| 1379 | | # ============================================================================== |
| 1380 | | |
| 1381 | | class SynapseDynamics(object): |
| 1382 | | """ |
| 1383 | | For specifying synapse short-term (faciliation, depression) and long-term |
| 1384 | | (STDP) plasticity. To be passed as the `synapse_dynamics` argument to |
| 1385 | | `Projection.__init__()` or `connect()`. |
| 1386 | | """ |
| 1387 | | |
| 1388 | | def __init__(self, fast=None, slow=None): |
| 1389 | | """ |
| 1390 | | Create a new specification for a dynamic synapse, combining a `fast` |
| 1391 | | component (short-term facilitation/depression) and a `slow` component |
| 1392 | | (long-term potentiation/depression). |
| 1393 | | """ |
| 1394 | | self.fast = fast |
| 1395 | | self.slow = slow |
| 1396 | | |
| 1397 | | def describe(self, template='standard'): |
| 1398 | | """ |
| 1399 | | Return a human-readable description of the synaptic properties. |
| 1400 | | """ |
| 1401 | | if template == 'standard': |
| 1402 | | lines = ["Short-term plasticity mechanism: $fast", |
| 1403 | | "Long-term plasticity mechanism: $slow"] |
| 1404 | | template = "\n".join(lines) |
| 1405 | | context = {'fast': self.fast and self.fast.describe() or 'None', |
| 1406 | | 'slow': self.slow and self.slow.describe() or 'None'} |
| 1407 | | if template == None: |
| 1408 | | return context |
| 1409 | | else: |
| 1410 | | return Template(template).substitute(context) |
| 1411 | | |
| 1412 | | |
| 1413 | | class ShortTermPlasticityMechanism(StandardModelType): |
| 1414 | | """Abstract base class for models of short-term synaptic dynamics.""" |
| 1415 | | |
| 1416 | | def __init__(self): |
| 1417 | | raise NotImplementedError |
| 1418 | | |
| 1419 | | |
| 1420 | | class STDPMechanism(object): |
| 1421 | | """Specification of STDP models.""" |
| 1422 | | |
| 1423 | | def __init__(self, timing_dependence=None, weight_dependence=None, |
| 1424 | | voltage_dependence=None, dendritic_delay_fraction=1.0): |
| 1425 | | """ |
| 1426 | | Create a new specification for an STDP mechanism, by combining a |
| 1427 | | weight-dependence, a timing-dependence, and, optionally, a voltage- |
| 1428 | | dependence. |
| 1429 | | |
| 1430 | | For point neurons, the synaptic delay `d` can be interpreted either as |
| 1431 | | occurring purely in the pre-synaptic axon + synaptic cleft, in which |
| 1432 | | case the synaptic plasticity mechanism 'sees' the post-synaptic spike |
| 1433 | | immediately and the pre-synaptic spike after a delay `d` |
| 1434 | | (`dendritic_delay_fraction = 0`) or as occurring purely in the post- |
| 1435 | | synaptic dendrite, in which case the pre-synaptic spike is seen |
| 1436 | | immediately, and the post-synaptic spike after a delay `d` |
| 1437 | | (`dendritic_delay_fraction = 1`), or as having both pre- and post- |
| 1438 | | synaptic components (`dendritic_delay_fraction` between 0 and 1). |
| 1439 | | |
| 1440 | | In a future version of the API, we will allow the different |
| 1441 | | components of the synaptic delay to be specified separately in |
| 1442 | | milliseconds. |
| 1443 | | """ |
| 1444 | | self.timing_dependence = timing_dependence |
| 1445 | | self.weight_dependence = weight_dependence |
| 1446 | | self.voltage_dependence = voltage_dependence |
| 1447 | | self.dendritic_delay_fraction = dendritic_delay_fraction |
| 1448 | | |
| 1449 | | def describe(self): |
| 1450 | | """ |
| 1451 | | Return a human-readable description of the STDP mechanism. |
| 1452 | | """ |
| 1453 | | return "STDP mechanism (this description needs to be filled out)." |
| 1454 | | |
| 1455 | | |
| 1456 | | class STDPWeightDependence(StandardModelType): |
| 1457 | | """Abstract base class for models of STDP weight dependence.""" |
| 1458 | | |
| 1459 | | def __init__(self): |
| 1460 | | raise NotImplementedError |
| 1461 | | |
| 1462 | | |
| 1463 | | class STDPTimingDependence(StandardModelType): |
| 1464 | | """Abstract base class for models of STDP timing dependence (triplets, etc)""" |
| 1465 | | |
| 1466 | | def __init__(self): |
| 1467 | | raise NotImplementedError |
| 1468 | | |
| 1469 | | |
| 1470 | | # ============================================================================== |