ML.Cluster.Resemblance

Package ML :: Package Cluster :: Module Resemblance |
[hide private]
[frames] | no frames]

# Source Code for Module ML.Cluster.Resemblance

      1  # $Id: Resemblance.py 2 2006-05-06 22:54:39Z glandrum $
      2  #
      3  # Copyright (C) 2001-2006  greg Landrum
      4  #
      5  #   @@ All Rights Reserved  @@
      6  #
      7  """ code for dealing with resemblance (metric) matrices
      8
      9      Here's how the matrices are stored:
     10
     11       '[(0,1),(0,2),(1,2),(0,3),(1,3),(2,3)...]  (row,col), col>row'
     12
     13       or, alternatively the matrix can be drawn, with indices as:
     14
     15         || - || 0 || 1 || 3
     16         || - || - || 2 || 4
     17         || - || - || - || 5
     18         || - || - || - || -
     19
     20       the index of a given (row,col) pair is:
     21         '(col*(col-1))/2 + row'
     22
     23  """
     24  from Numeric import *
     25
     26
     27 -def EuclideanDistance(inData):
     28    """returns the euclidean metricMat between the points in _inData_
     29
     30      **Arguments**
     31
     32       - inData: a Numeric array of data points
     33
     34      **Returns**
     35
     36         a Numeric array with the metric matrix.  See the module documentation
     37         for the format.
     38
     39
     40    """
     41    nObjs = len(inData)
     42    res = zeros((nObjs*(nObjs-1)/2),Float)
     43    nSoFar = 0
     44    for col in xrange(1,nObjs):
     45      for row in xrange(col):
     46        t = inData[row]-inData[col]
     47        res[nSoFar] = sum(t*t)
     48        nSoFar += 1
     49    return sqrt(res)
     50
     51 -def CalcMetricMatrix(inData,metricFunc):
     52    """ generates a metric matrix
     53
     54      **Arguments**
     55       - inData is assumed to be a list of clusters (or anything with
     56         a GetPosition() method)
     57
     58       - metricFunc is the function to be used to generate the matrix
     59
     60
     61      **Returns**
     62
     63        the metric matrix as a Numeric array
     64
     65    """
     66    nObjs = len(inData)
     67    res = []
     68    inData = map(lambda x:x.GetPosition(),inData)
     69    return metricFunc(inData)
     70
     71 -def FindMinValInList(mat,nObjs,minIdx=None):
     72    """ finds the minimum value in a metricMatrix and returns it and its indices
     73
     74      **Arguments**
     75
     76       - mat: the metric matrix
     77
     78       - nObjs: the number of objects to be considered
     79
     80       - minIdx: the index of the minimum value (value, row and column still need
     81         to be calculated
     82
     83      **Returns**
     84
     85        a 3-tuple containing:
     86
     87          1) the row
     88          2) the column
     89          3) the minimum value itself
     90
     91      **Notes**
     92
     93        -this probably ain't the speediest thing on earth
     94
     95    """
     96    assert len(mat) == nObjs*(nObjs-1)/2, 'bad matrix length in FindMinValInList'
     97    if minIdx is None:
     98      minIdx = argmin(mat)
     99
    100    nSoFar = 0
    101    col = 0
    102    while nSoFar <= minIdx:
    103      col = col + 1
    104      nSoFar += col
    105
    106    row = minIdx - nSoFar + col
    107    return row,col,mat[minIdx]
    108
    109 -def ShowMetricMat(metricMat,nObjs):
    110    """ displays a metric matrix
    111
    112     **Arguments**
    113
    114      - metricMat: the matrix to be displayed
    115
    116      - nObjs: the number of objects to display
    117
    118    """
    119    assert len(metricMat) == nObjs*(nObjs-1)/2, 'bad matrix length in FindMinValInList'
    120    for row in xrange(nObjs):
    121      for col in xrange(nObjs):
    122        if col <= row:
    123          print '   ---    ',
    124        else:
    125          print '%10.6f'%metricMat[(col*(col-1))/2+row],
    126      print
    127
    128
    129
    130  methods = [
    131    ("Euclidean",EuclideanDistance,"Euclidean Distance"),
    132    ]
    133
    134
    135
    136  if __name__ == '__main__':
    137    m = [.1,.2,.3,.4,.5,.6,.7,.8,.9,1.0]
    138    nObjs = 5
    139    for i in range(10):
    140      print i, FindMinValInList(m,nObjs,i)
    141