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

Source Code for Module 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  import exceptions 
10 -class DescriptorCalculationError(exceptions.Exception):
11 """ used to signal problems generating descriptor values """ 12 pass
13 -class ClassificationError(exceptions.Exception):
14 """ used to signal problems generating predictions """ 15 pass
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 - def __init__(self,descCalc=None,model=None,dataSet=None,notes=''):
24 self._descCalc = descCalc 25 self._model = model 26 self._notes = notes 27 self._dataSet = dataSet 28 self._initialized = 0 29 self._supplementalData = []
30
31 - def SetCalculator(self,calc):
32 self._descCalc = calc
33 - def GetCalculator(self):
34 return self._descCalc
35
36 - def SetModel(self,model):
37 self._model = model
38 - def GetModel(self):
39 return self._model
40
41 - def SetDataset(self,data):
42 self._dataSet = data
43 - def GetDataset(self):
44 return self._dataSet
45
46 - def SetNotes(self,notes):
47 self._notes = notes
48 - def GetNotes(self):
49 return self._notes
50
51 - def SetSupplementalData(self,suppD):
52 self._supplementalData = suppD
53 - def GetSupplementalData(self):
54 if not hasattr(self,'_supplementalData'): 55 self._supplementalData = [] 56 return self._supplementalData
57 - def AddSupplementalData(self,data):
58 if not hasattr(self,'_supplementalData'): 59 self._supplementalData = [] 60 self._supplementalData.append(data)
61
62 - def Classify(self,obj,label='',threshold=0):
63 if not self._initialized: 64 self.Init() 65 try: 66 descs = self._descCalc.CalcDescriptors(obj) 67 except: 68 raise DescriptorCalculationError,'problems encountered generating descriptors' 69 70 argVect = [label]+list(descs)+[0] 71 try: 72 res = self._model.ClassifyExample(argVect,threshold=threshold,appendExample=0) 73 except: 74 import traceback 75 traceback.print_exc() 76 raise ClassificationError,'problems encountered generating prediction' 77 78 return res
79
80 - def Init(self):
81 if self._model is None or self._descCalc is None: 82 return 83 84 nms = self._model.GetDescriptorNames() 85 lbl = nms[0] 86 act = nms[-1] 87 descs = self._descCalc.GetDescriptorNames() 88 order = [lbl] + list(descs) + [act] 89 self._model.SetInputOrder(order) 90 91 self._initialized = 1
92