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

Source Code for Module ML.Composite.CompositeCOM

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