rdkit.ML.DecTree.QuantTree

Package rdkit :: Package ML :: Package DecTree :: Module QuantTree |
[hide private]
[frames] | no frames]

# Source Code for Module rdkit.ML.DecTree.QuantTree

     1  # $Id: QuantTree.py 997 2009-02-25 06:12:43Z glandrum $
     2  #
     3  #  Copyright (C) 2001, 2003  greg Landrum and Rational Discovery LLC
     4  #   All Rights Reserved
     5  #
     6  """ Defines the class _QuantTreeNode_, used to represent decision trees with automatic
     7   quantization bounds
     8
     9    _QuantTreeNode_ is derived from _DecTree.DecTreeNode_
    10
    11  """
    12  from rdkit.ML.DecTree import DecTree,Tree
    13
    14 -class QuantTreeNode(DecTree.DecTreeNode):
    15    """
    16
    17    """
    18 -  def __init__(self,*args,**kwargs):
    19      apply(DecTree.DecTreeNode.__init__,(self,)+args,kwargs)
    20      self.qBounds = []
    21      self.nBounds = 0
    22 -  def ClassifyExample(self,example,appendExamples=0):
    23      """ Recursively classify an example by running it through the tree
    24
    25        **Arguments**
    26
    27          - example: the example to be classified
    28
    29          - appendExamples: if this is nonzero then this node (and all children)
    30            will store the example
    31
    32        **Returns**
    33
    34          the classification of _example_
    35
    36        **NOTE:**
    37          In the interest of speed, I don't use accessor functions
    38          here.  So if you subclass DecTreeNode for your own trees, you'll
    39          have to either include ClassifyExample or avoid changing the names
    40          of the instance variables this needs.
    41
    42      """
    43      if appendExamples:
    44        self.examples.append(example)
    45      if self.terminalNode:
    46        return self.label
    47      else:
    48        val = example[self.label]
    49        if not hasattr(self,'nBounds'): self.nBounds = len(self.qBounds)
    50        if self.nBounds:
    51          for i,bound in enumerate(self.qBounds):
    52            if val < bound:
    53              val = i
    54              break
    55          else:
    56            val = i+1
    57        else:
    58          val = int(val)
    59        return self.children[val].ClassifyExample(example,appendExamples=appendExamples)
    60
    61 -  def SetQuantBounds(self,qBounds):
    62      self.qBounds = qBounds[:]
    63      self.nBounds = len(self.qBounds)
    64 -  def GetQuantBounds(self):
    65      return self.qBounds
    66
    67 -  def __cmp__(self,other):
    68      return cmp(type(self),type(other)) or \
    69             cmp(self.qBounds,other.qBounds) or \
    70             Tree.TreeNode.__cmp__(self,other)
    71
    72 -  def __str__(self):
    73      """ returns a string representation of the tree
    74
    75        **Note**
    76
    77          this works recursively
    78
    79      """
    80      here = '%s%s %s\n'%('  '*self.level,self.name,str(self.qBounds))
    81      for child in self.children:
    82        here = here + str(child)
    83      return here
    84