rdkit.ML.Composite.CompositeCOM

Package rdkit :: Package ML :: Package Composite :: Module CompositeCOM |
[hide private]
[frames] | no frames]

# Source Code for Module rdkit.ML.Composite.CompositeCOM

      1  #
      2  #  Copyright (C) 2001 greg Landrum
      3  #
      4  """ the COM server for Composite models
      5
      6  """
      7
      8  from rdkit import RDConfig
      9  import string
     10  from rdkit.ML.Composite import Composite
     11  import cPickle
     12  import sys
     13  import winerror
     14  from win32com.server import exception
     15
     16 -class CompositeServer(object):
     17    """ exposes an interface for composite COM servers
     18
     19      This interface does not support modifying the composite, only
     20       classifying new examples
     21
     22      **Public Methods**
     23
     24       - LoadComposite
     25
     26       - ClassifyExample
     27
     28       - ShowComposite
     29
     30       - GetVoteDetails
     31
     32       - SetInputOrder
     33
     34       - GetDescriptorNames
     35
     36       - Close
     37
     38      **Public Attributes**
     39
     40        None
     41
     42      **ProgID**
     43
     44        RD.Composite
     45
     46    """
     47    _public_methods_ = ['LoadComposite','ClassifyExample','ShowComposite',
     48                        'GetVoteDetails','SetInputOrder','GetDescriptorNames',
     49                        'Close']
     50    _public_attrs_ = []
     51    _reg_clsid_ = '{9F62358E-9043-4BF9-93C7-47BED8BE522F}'
     52    _reg_progid_ = "RD.Composite"
     53
     54 -  def Close(self):
     55      """ Blows out the local composite
     56
     57        **Note**
     58
     59          _LoadComposite()_ must be called after this for further use of the
     60          composite
     61
     62      """
     63      self._composite = None
     64
     65 -  def LoadComposite(self,fileName):
     66      """ Loads a (pickled) composite from a file
     67
     68       **Arguments**
     69         - fileName: the name of the file to load
     70
     71      """
     72      try:
     73        f = open(str(fileName),'rb')
     74      except:
     75        raise exception.COMException('The file %s could not be opened'%(fileName),
     76                                     winerror.ERROR_FILE_NOT_FOUND)
     77      try:
     78        self._composite = cPickle.load(f)
     79      except:
     80        raise exception.COMException('The composite could not be loaded',
     81                                     winerror.ERROR_INVALID_HANDLE)
     82
     83      return fileName
     84
     85 -  def ShowComposite(self):
     86      """ returns a string representation of the composite
     87
     88      """
     89      try:
     90        c = self._composite
     91      except AttributeError:
     92        raise exception.COMException('The composite has not yet been loaded',
     93                                     winerror.ERROR_INVALID_HANDLE)
     94      else:
     95        return str(c)
     96
     97 -  def GetVoteDetails(self):
     98      """ returns a list of the results of the last vote
     99
    100      """
    101      try:
    102        c = self._composite
    103      except AttributeError:
    104        raise exception.COMException('The composite has not yet been loaded',
    105                                     winerror.ERROR_FILE_NOT_FOUND)
    106      else:
    107        try:
    108          return c.GetVoteDetails()
    109        except:
    110          raise exception.COMException('VoteDetails unavaliable',winerror.ERROR_INVALID_HANDLE)
    111
    112 -  def SetInputOrder(self,colNames):
    113      """ Sets the input order for the composite
    114
    115        this is used so that the composite can remap inputs which are not
    116        in an order it expects
    117
    118        **Arguments**
    119
    120          - colNames: a list of the names of the columns in the data which will be
    121             passed in to the composite
    122
    123        **Notes**
    124
    125          - there must be a column name which matches each of the composite's
    126            descriptors (accessible via _GetDescriptorNames()_)
    127
    128      """
    129      try:
    130        c = self._composite
    131      except AttributeError:
    132        raise exception.COMException('The composite has not yet been loaded',
    133                                     winerror.ERROR_INVALID_HANDLE)
    134
    135      try:
    136        colNames = map(str,colNames)
    137        c.SetInputOrder(colNames)
    138      except:
    139        raise exception.COMException('SetInputOrder failed.',winerror.ERROR_INVALID_HANDLE)
    140      return 0
    141
    142 -  def GetDescriptorNames(self):
    143      """ returns a list of the descriptor names the composite expects
    144
    145      """
    146      try:
    147        c = self._composite
    148      except AttributeError:
    149        raise exception.COMException('The composite has not yet been loaded',
    150                                     winerror.ERROR_INVALID_HANDLE)
    151      else:
    152        try:
    153          return c.GetDescriptorNames()
    154        except:
    155          raise exception.COMException('GetDescriptorNames failed.',winerror.ERROR_INVALID_HANDLE)
    156
    157 -  def ClassifyExample(self,example,threshold=0):
    158      """ classifies a new example
    159
    160        **Arguments**
    161
    162          - example: a list containing the example to be classified
    163
    164          - threshold: the threshold to be used for high-confidence predictions
    165
    166        **Returns**
    167
    168          a list two elements long:
    169
    170             1) the classification (this will be -1 if the confidence of the prediction
    171                was below _threshold_
    172
    173             2) the confidence
    174
    175      """
    176      try:
    177        c = self._composite
    178      except AttributeError:
    179        raise exception.COMException('The composite has not yet been loaded',
    180                                     winerror.ERROR_INVALID_HANDLE)
    181      if 0:
    182        try:
    183          res = c.ClassifyExample(example,threshold=threshold)
    184        except:
    185          import traceback,sys
    186          outF = open(RDConfig.RDCodeDir+'/ml/composite/log.txt','a+')
    187          outF.write('#------------------------------\n')
    188          outF.write('ex: %s\n'%str(example))
    189          traceback.print_tb(sys.exc_info()[2],file=outF)
    190          outF.close()
    191          res = []
    192      else:
    193        try:
    194          res = c.ClassifyExample(example,threshold=threshold)
    195        except:
    196          raise exception.COMException('ClassifyExample failed.',winerror.ERROR_INVALID_HANDLE)
    197      return list(res)
    198
    199  if __name__=='__main__':
    200      from win32com.server import register
    201      register.UseCommandLine(CompositeServer)
    202