rdkit.ML.ModelPackage.Packager

Package rdkit :: Package ML :: Package ModelPackage :: Module Packager |
[hide private]
[frames] | no frames]

# Source Code for Module rdkit.ML.ModelPackage.Packager

      1  #
      2  # Copyright (C) 2002-2008 Greg Landrum and Rational Discovery LLC
      3  # All rights are reserved.
      4  #
      5
      6
      7 -class DescriptorCalculationError(Exception):
      8    """ used to signal problems generating descriptor values """
      9    pass
     10
     11
     12 -class ClassificationError(Exception):
     13    """ used to signal problems generating predictions """
     14    pass
     15
     16
     17 -class ModelPackage(object):
     18    """ a container class to package a composite model with a descriptor
     19    calculator so that objects needing predictions (compounds, molecules, etc.)
     20    can be passed directly in without worrying about generating descriptors
     21
     22    """
     23
     24 -  def __init__(self, descCalc=None, model=None, dataSet=None, notes=''):
     25      self._descCalc = descCalc
     26      self._model = model
     27      self._notes = notes
     28      self._dataSet = dataSet
     29      self._initialized = 0
     30      self._supplementalData = []
     31
     32 -  def SetCalculator(self, calc):
     33      self._descCalc = calc
     34
     35 -  def GetCalculator(self):
     36      return self._descCalc
     37
     38 -  def SetModel(self, model):
     39      self._model = model
     40
     41 -  def GetModel(self):
     42      return self._model
     43
     44 -  def SetDataset(self, data):
     45      self._dataSet = data
     46
     47 -  def GetDataset(self):
     48      return self._dataSet
     49
     50 -  def SetNotes(self, notes):
     51      self._notes = notes
     52
     53 -  def GetNotes(self):
     54      return self._notes
     55
     56 -  def SetSupplementalData(self, suppD):
     57      self._supplementalData = suppD
     58
     59 -  def GetSupplementalData(self):
     60      if not hasattr(self, '_supplementalData'):
     61        self.SetSupplementalData([])
     62      return self._supplementalData
     63
     64 -  def AddSupplementalData(self, data):
     65      if not hasattr(self, '_supplementalData'):
     66        self.SetSupplementalData([])
     67      self._supplementalData.append(data)
     68
     69 -  def Classify(self, obj, label='', threshold=0):
     70      if not self._initialized:
     71        self.Init()
     72      try:
     73        descs = self._descCalc.CalcDescriptors(obj)
     74      except Exception:
     75        raise DescriptorCalculationError('problems encountered generating descriptors')
     76
     77      argVect = [label] + list(descs) + [0]
     78      try:
     79        res = self._model.ClassifyExample(argVect, threshold=threshold, appendExample=0)
     80      except Exception:
     81        import traceback
     82        traceback.print_exc()
     83        raise ClassificationError('problems encountered generating prediction')
     84
     85      return res
     86
     87 -  def Init(self):
     88      if self._model is None or self._descCalc is None:
     89        return
     90
     91      nms = self._model.GetDescriptorNames()
     92      lbl = nms[0]
     93      act = nms[-1]
     94      descs = self._descCalc.GetDescriptorNames()
     95      order = [lbl] + list(descs) + [act]
     96      self._model.SetInputOrder(order)
     97
     98      self._initialized = 1
     99