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$
     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
    33 -def TanimotoDist(ex1, ex2, attrs):
    34    """
    35      >>> v1 = [0,1,0,1]
    36      >>> v2 = [1,0,1,0]
    37      >>> TanimotoDist(v1,v2,range(4))
    38      1.0
    39      >>> v2 = [1,0,1,1]
    40      >>> TanimotoDist(v1,v2,range(4))
    41      0.75
    42      >>> TanimotoDist(v2,v2,range(4))
    43      0.0
    44
    45      # this tests Issue 122
    46      >>> v3 = [0,0,0,0]
    47      >>> TanimotoDist(v3,v3,range(4))
    48      1.0
    49
    50      """
    51    inter = 0.0
    52    unin = 0.0
    53    for i in attrs:
    54      if (ex1[i] or ex2[i]):
    55        unin += 1
    56        if (ex1[i] and ex2[i]):
    57          inter += 1
    58    if (unin != 0.0):
    59      return (1 - inter / unin)
    60    else:
    61      return 1.0
    62
    63
    64  # ------------------------------------
    65  #
    66  #  doctest boilerplate
    67  #
    68 -def _runDoctests(verbose=None):  # pragma: nocover
    69    import sys
    70    import doctest
    71    failed, _ = doctest.testmod(optionflags=doctest.ELLIPSIS, verbose=verbose)
    72    sys.exit(failed)
    73
    74
    75  if __name__ == '__main__':  # pragma: nocover
    76    _runDoctests()
    77