rdkit.ML.KNN.DistFunctions

Package rdkit :: Package ML :: Package KNN :: Module DistFunctions |
[hide private]
[frames] | no frames]

# Source Code for Module rdkit.ML.KNN.DistFunctions

     1  # $Id: DistFunctions.py 997 2009-02-25 06:12:43Z glandrum $
     2  #
     3  #  Copyright (C) 2003 Rational Discovery LLC
     4  #      All Rights Reserved
     5  #
     6
     7  import math
     8
     9
    10 -def EuclideanDist(ex1, ex2, attrs) :
    11      """
    12      >>> v1 = [0,1,0,1]
    13      >>> v2 = [1,0,1,0]
    14      >>> EuclideanDist(v1,v2,range(4))
    15      2.0
    16      >>> EuclideanDist(v1,v1,range(4))
    17      0.0
    18      >>> v2 = [0,0,0,1]
    19      >>> EuclideanDist(v1,v2,range(4))
    20      1.0
    21      >>> v2 = [0,.5,0,.5]
    22      >>> abs(EuclideanDist(v1,v2,range(4))-1./math.sqrt(2))<1e-4
    23      1
    24
    25      """
    26      dist = 0.0
    27      for i in attrs:
    28          dist += (ex1[i] - ex2[i])**2
    29      dist = math.sqrt(dist)
    30      return dist
    31
    32 -def TanimotoDist(ex1, ex2, attrs) :
    33      """
    34      >>> v1 = [0,1,0,1]
    35      >>> v2 = [1,0,1,0]
    36      >>> TanimotoDist(v1,v2,range(4))
    37      1.0
    38      >>> v2 = [1,0,1,1]
    39      >>> TanimotoDist(v1,v2,range(4))
    40      0.75
    41      >>> TanimotoDist(v2,v2,range(4))
    42      0.0
    43
    44      # this tests Issue 122
    45      >>> v3 = [0,0,0,0]
    46      >>> TanimotoDist(v3,v3,range(4))
    47      1.0
    48
    49      """
    50      inter = 0.0
    51      unin = 0.0
    52      for i in attrs:
    53          if (ex1[i] or ex2[i]) :
    54              unin += 1
    55          if (ex1[i] and ex2[i]) :
    56              inter += 1
    57      if(unin != 0.0):
    58          return (1 - inter/unin)
    59      else:
    60          return 1.0
    61
    62  #------------------------------------
    63  #
    64  #  doctest boilerplate
    65  #
    66 -def _test():
    67    import doctest,sys
    68    return doctest.testmod(sys.modules["__main__"])
    69
    70  if __name__ == '__main__':
    71    import sys
    72    failed,tried = _test()
    73    sys.exit(failed)
    74