def translate(cls, parameters): if 'spike_times' in parameters: try: parameters['spike_times'] = numpy.array( parameters['spike_times'], float) except ValueError: raise common.InvalidParameterValueError( "spike times must be floats") return super(SpikeSourceArray, cls).translate(parameters)
def sanitize_spike_times(spike_times): """ PCSIM has a bug that the SpikingInputNeuron sometimes stops emitting spikes I think this happens when two spikes fall in the same time step. This workaround removes any spikes after the first within a given time step. """ time_step = common.get_time_step() try: spike_times = numpy.array(spike_times, float) except ValueError, e: raise common.InvalidParameterValueError( "Spike times must be floats. %s")
def set_native_parameters(self, parameters): """Set parameters of the NEST cell model from a dictionary.""" if 'v_init' in parameters: self._v_init = parameters.pop('v_init') parameters[ 'V_m'] = self._v_init # not correct, since could set v_init in the middle of a simulation, but until we add a proper reset mechanism, this will do. try: nest.SetStatus([self], [parameters]) except: # I can't seem to catch the NESTError that is raised, hence this roundabout way of doing it. exc_type, exc_value, traceback = sys.exc_info() if exc_type == 'NESTError' and "Unsupported Numpy array type" in exc_value: raise common.InvalidParameterValueError() else: raise
def set(self, param, val=None): """ Set one or more parameters for every cell in the population. param can be a dict, in which case val should not be supplied, or a string giving the parameter name, in which case val is the parameter value. val can be a numeric value, or list of such (e.g. for setting spike times). e.g. p.set("tau_m",20.0). p.set({'tau_m':20,'v_rest':-65}) """ if isinstance(param, str): if isinstance(val, (str, float, int)): param_dict = {param: float(val)} else: raise common.InvalidParameterValueError elif isinstance(param, dict): param_dict = param else: raise common.InvalidParameterValueError # The default implementation in common is is not very efficient for # simple and scaled parameters. # Should call nest.SetStatus(self.local_cells,...) for the parameters in # self.celltype.__class__.simple_parameters() and .scaled_parameters() # and keep the loop below just for the computed parameters. Even in this # case, it may be quicker to test whether the parameters participating # in the computation vary between cells, since if this is not the case # we can do the computation here and use nest.SetStatus. for key, value in param_dict.items(): if not isinstance(self.celltype, str): # Here we check the consistency of the given parameters try: self.celltype.default_parameters[key] except Exception: raise common.NonExistentParameterError( key, self.celltype.__class__) if type(value) != type(self.celltype.default_parameters[key]): if isinstance(value, int) and isinstance( self.celltype.default_parameters[key], float): value = float(value) else: raise common.InvalidParameterValueError( "The parameter %s should be a %s, you supplied a %s" % (key, type(self.celltype.default_parameters[key]), type(value))) # Then we do the call to SetStatus if key == 'v_init': for cell in self.local_cells: cell._v_init = value nest.SetStatus( self.local_cells.tolist(), "V_m", val ) # not correct, since could set v_init in the middle of a simulation elif key in self.celltype.scaled_parameters(): translation = self.celltype.translations[key] value = eval(translation['forward_transform'], globals(), {key: value}) nest.SetStatus(self.local_cells.tolist(), translation['translated_name'], value) elif key in self.celltype.simple_parameters(): translation = self.celltype.translations[key] nest.SetStatus(self.local_cells.tolist(), translation['translated_name'], value) else: for cell in self.local_cells: cell.set_parameters(**{key: value}) else: try: nest.SetStatus(self.local_cells, key, value) except Exception: raise common.InvalidParameterValueError
def _set_spike_times(self, spike_times): try: self._spike_times = h.Vector(spike_times) except RuntimeError: raise common.InvalidParameterValueError("spike_times must be an array of floats") self.play(self._spike_times)