Custom Model Code
This example code shows how to write a custom model using Python. This example can be used to make in-house or third party models available to all StarDrop users because the model will appear within the StarDrop interface. StarDrop supports two types of custom model – continuous and category – both of which can be built using this example by following the comments in the code. This example will work without modification and installation instrucitons along with a copy of the code is provided in the following pages.
To install this model:
- Save the file ExampleModelCode.py and copy it onto the StarDrop IM Server (model server) onto the modelfiles directory. By default this will be in /usr/local/StarDrop/ims/models.
- Restart the model server (you will need to be logged in as root to do this) using the command “/etc/init.d/StarDrop_IMServer restart”.
- Start the StarDrop client and, assuming it is configured to use the model server, you will see this model appear in the list of available models.
Here is a copy of the code in the downloable file. Left as it is it will provide an example continuous model. however, following the comments below it can easily be changed into a categorical model instead.
from implugin import IMPlugin # this is required for all models
## Comment out the line below if this is a category model
from modeldescriptions import ContModelDescription
## Uncomment the line below if this is acategory model
# from modeldescriptions import CatModelDescription
class Plugin (IMPlugin):
## Comment out these 8 lines to make this a category model
model = ContModelDescription(‘MOD_CONT’,
0.0, # minimum possible value of prediction
1000.0, # maximum possible value of prediction
interactive = True,
shortName = ‘name’,
longName = ‘a continuous model’,
description = ‘This is an example’)
## Uncomment these 7 lines to make this a category model
# model = CatModelDescription(‘MOD_CAT’,
# [‘low’,’medium’,’high’], # possible result categories
# shortName=’3-category model’,
# longName=’a three category model’,
# description=’An example category model’)
# The predict function is where you need to add the custom code
# for calculation or to call out to other applications. The molecule
# is appsed to this function as a SMILES string
def predict(self, smiles, extra):
# implement model calculation here
## Comment out the lne below is this is a category model
return [7.2, 0.5] # [prediction, standard deviation]
## Uncomment the line below if this is a category model
# return [0.3,0.1,0.6] # probability of each category (listed above)
## For category models the “predicted category” is the one with the
## highest probability (in this case ‘high’)