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