pulse2percept.models.base

Classes

BaseModel(**kwargs) Base model

Exceptions

NotBuiltError Exception class used to raise if model is used before building
class pulse2percept.models.base.BaseModel(**kwargs)[source]

Base model

The BaseModel class defines which methods and attributes a model must have. You can create your own model by adding a class that derives from BaseModel:

class MyModel(BaseModel):

The constructor is the only place where you can add new variables (i.e., class attributes). The signature of your own constructor should look like this:

def __init__(self, **kwargs):

meaning that all arguments are passed as keyword arguments. Also, make sure to call the BaseModel constructor first thing. So a complete example of a constructor could look like this:

class MyModel(BaseModel):

    def __init__(self, **kwargs):
        super(MyModel, self).__init__(self, **kwargs)
        self.newvar = 0

This is the only place where you can add new class attributes. Trying to set self.someothervar outside the constructor will raise an AttributeError. Of course, you can always set self.newvar = None in the constructor to make sure the variable exists, and then assign a new value in other class methods.

Note

Please note: If self.newvar already exists in the BaseModel class, the last line of the above code snippet would overwrite it.

To make the model complete (and compile), you will also need to fill in all methods marked with @abc.abstractmethod below. These include build and predict_percept. Have a look at the ScoreboardModel or AxonMapModel classes below to get an idea of how to write a complete model.

build(**build_params)[source]

Build the model

Every model must have a `build method, which is meant to perform all expensive one-time calculations. You must call build before calling predict_percept.

You can override build in your own model (for a good example, see the AxonMapModel). You will want to make sure that:

  • all build_params take effect,
  • the flag _is_built is set,
  • the method returns self.

One way to do this is to call the BaseModel’s build method from within your own model:

class MyModel(BaseModel):

def build(self, **build_params):
super(MyModel, self).build(self, **build_params) # Add your own code here…
Parameters:**build_params (Additional build parameters) – Additional build parameters passed as keyword arguments (e.g., model.build(engine='joblib')). Note that these must be listed in get_params; i.e., you can’t add any new parameters outside the constructor.
get_params()[source]

Get a dictionary of all model parameters (don’t override!)

get_tissue_coords(xdva, ydva)[source]

Convert dva into tissue coordinates

predict_percept(implant, t=None)[source]

Predict a percept

Parameters:
  • implant (ProsthesisSystem) – Stimulus can be passed via stim.
  • fps (int, double) – Frames per second at which the percept should be rendered.
  • n_frames (int) – If None, will simulate for the duration of the stimulus plus one frame (rounding up).
exception pulse2percept.models.base.NotBuiltError[source]

Exception class used to raise if model is used before building

This class inherits from both ValueError and AttributeError to help with exception handling and backward compatibility.

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.