Changeset 711 for trunk/src/common.py
- Timestamp:
- 02/16/10 10:59:22 (2 years ago)
- Files:
-
- 1 modified
-
trunk/src/common.py (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/common.py
r702 r711 7 7 consistent with good performance (optimisations may require overriding some of 8 8 the default definitions given here). 9 10 Exceptions:11 InvalidParameterValueError12 NonExistentParameterError13 InvalidDimensionsError14 ConnectionError15 InvalidModelError16 RoundingWarning17 NothingToWriteError18 InvalidWeightError19 NotLocalError20 RecordingError21 9 22 10 Utility functions and classes: … … 27 15 check_delay() 28 16 distance() 29 distances()30 next_n()31 ConstIter32 17 33 18 Accessing individual neurons: … … 61 46 Population 62 47 Projection 63 Connector64 48 65 49 4. Specification of synaptic plasticity: … … 78 62 from math import * 79 63 import operator 80 from pyNN import random, utility, recording 64 from pyNN import random, utility, recording, errors 81 65 from string import Template 82 66 … … 89 73 logger = logging.getLogger("PyNN") 90 74 91 class InvalidParameterValueError(ValueError):92 """Inappropriate parameter value"""93 pass94 95 class NonExistentParameterError(Exception):96 """97 Model parameter does not exist.98 """99 100 def __init__(self, parameter_name, model):101 self.parameter_name = parameter_name102 if isinstance(model, type):103 if issubclass(model, StandardModelType):104 self.model_name = model.__name__105 self.valid_parameter_names = model.default_parameters.keys()106 self.valid_parameter_names.sort()107 elif isinstance(model, basestring):108 self.model_name = model109 self.valid_parameter_names = ['unknown']110 else:111 raise Exception("When raising a NonExistentParameterError, model must be a class or a string, not a %s" % type(model))112 113 def __str__(self):114 return "%s (valid parameters for %s are: %s)" % (self.parameter_name,115 self.model_name,116 ", ".join(self.valid_parameter_names))117 118 class InvalidDimensionsError(Exception):119 """Argument has inappropriate shape/dimensions."""120 pass121 122 class ConnectionError(Exception):123 """Attempt to create an invalid connection or access a non-existent connection."""124 pass125 126 class InvalidModelError(Exception):127 """Attempt to use a non-existent model type."""128 pass129 130 class NoModelAvailableError(Exception):131 """The simulator does not implement the requested model."""132 pass133 134 class RoundingWarning(Warning):135 """The argument has been rounded to a lower level of precision by the simulator."""136 pass137 138 class NothingToWriteError(Exception):139 """There is no data available to write."""140 pass # subclass IOError?141 142 class InvalidWeightError(Exception): # subclass ValueError? or InvalidParameterValueError?143 """Invalid value for the synaptic weight."""144 pass145 146 class NotLocalError(Exception):147 """Attempt to access a cell or connection that does not exist on this node (but exists elsewhere)."""148 pass149 150 class RecordingError(Exception): # subclass AttributeError?151 """Attempt to record a variable that does not exist for this cell type."""152 153 def __init__(self, variable, cell_type):154 self.variable = variable155 self.cell_type = cell_type156 157 def __str__(self):158 return "Cannot record %s from cell type %s" % (self.variable, self.cell_type.__class__.__name__)159 75 160 76 … … 194 110 all_positive = (filtered_weight>=0).all() 195 111 if not (all_negative or all_positive): 196 raise InvalidWeightError("Weights must be either all positive or all negative")112 raise errors.InvalidWeightError("Weights must be either all positive or all negative") 197 113 elif is_number(weight): 198 114 all_positive = weight >= 0 … … 202 118 if is_conductance or synapse_type == 'excitatory': 203 119 if not all_positive: 204 raise InvalidWeightError("Weights must be positive for conductance-based and/or excitatory synapses")120 raise errors.InvalidWeightError("Weights must be positive for conductance-based and/or excitatory synapses") 205 121 elif is_conductance==False and synapse_type == 'inhibitory': 206 122 if not all_negative: 207 raise InvalidWeightError("Weights must be negative for current-based, inhibitory synapses")123 raise errors.InvalidWeightError("Weights must be negative for current-based, inhibitory synapses") 208 124 else: # is_conductance is None. This happens if the cell does not exist on the current node. 209 125 logger.debug("Can't check weight, conductance status unknown.") … … 215 131 # If the delay is too small , we have to throw an error 216 132 if delay < get_min_delay() or delay > get_max_delay(): 217 raise ConnectionError("delay (%s) is out of range [%s,%s]" % (delay, get_min_delay(), get_max_delay()))133 raise errors.ConnectionError("delay (%s) is out of range [%s,%s]" % (delay, get_min_delay(), get_max_delay())) 218 134 return delay 219 135 … … 323 239 val = self.get_parameters()[name] 324 240 except KeyError: 325 raise NonExistentParameterError(name, self.cellclass) 241 raise errors.NonExistentParameterError(name, self.cellclass.__name__, 242 self.cellclass.default_parameters.keys()) 326 243 return val 327 244 … … 492 409 parameters[k] = float(supplied_parameters[k]) 493 410 except (ValueError, TypeError): 494 raise InvalidParameterValueError(err_msg)411 raise errors.InvalidParameterValueError(err_msg) 495 412 # list and something that can be transformed to a list 496 413 elif type(default_parameters[k]) == types.ListType: … … 498 415 parameters[k] = list(supplied_parameters[k]) 499 416 except TypeError: 500 raise InvalidParameterValueError(err_msg)417 raise errors.InvalidParameterValueError(err_msg) 501 418 else: 502 raise InvalidParameterValueError(err_msg)419 raise errors.InvalidParameterValueError(err_msg) 503 420 else: 504 raise NonExistentParameterError(k, cls)421 raise errors.NonExistentParameterError(k, cls) 505 422 return parameters 506 423 … … 683 600 for tgt in target: 684 601 #if not isinstance(tgt, IDMixin): 685 # raise ConnectionError("target is not a cell, actually %s" % type(tgt))602 # raise errors.ConnectionError("target is not a cell, actually %s" % type(tgt)) 686 603 sources = numpy.array(source, dtype=type(source)) 687 604 if p < 1: … … 794 711 id = self.all_cells[addr] 795 712 else: 796 raise InvalidDimensionsError, "Population has %d dimensions. Address was %s" % (self.ndim, str(addr))713 raise errors.InvalidDimensionsError, "Population has %d dimensions. Address was %s" % (self.ndim, str(addr)) 797 714 return id 798 715 … … 844 761 coords = (idx,) 845 762 else: 846 raise common.InvalidDimensionsError763 raise errors.InvalidDimensionsError 847 764 return coords 848 765 … … 933 850 param_dict = {param: val} 934 851 else: 935 raise InvalidParameterValueError852 raise errors.InvalidParameterValueError 936 853 elif isinstance(param, dict): 937 854 param_dict = param 938 855 else: 939 raise InvalidParameterValueError856 raise errors.InvalidParameterValueError 940 857 logger.debug("%s.set(%s)", self.label, param_dict) 941 858 for cell in self: … … 953 870 local_values = value_array[self._mask_local] # not sure this works 954 871 else: 955 raise InvalidDimensionsError, "Population: %s, value_array: %s" % (str(self.dim),872 raise errors.InvalidDimensionsError, "Population: %s, value_array: %s" % (str(self.dim), 956 873 str(value_array.shape)) 957 874 assert local_values.shape[0] == self.local_cells.size, "%d != %d" % (local_values.size, self.local_cells.size) … … 1017 934 """ 1018 935 if not self.can_record(variable): 1019 raise RecordingError(variable, self.celltype)936 raise errors.RecordingError(variable, self.celltype) 1020 937 if isinstance(record_from, list): #record from the fixed list specified by user 1021 938 pass
