Package Bio :: Module distance
[hide private]
[frames] | no frames]

Source Code for Module Bio.distance

 1  """ 
 2  This module provides code for various distance measures. 
 3   
 4  Functions: 
 5  euclidean       Euclidean distance between two points 
 6  euclidean_py    Pure Python implementation of euclidean. 
 7   
 8  """ 
 9  # XXX cosine distance 
10  import math 
11  try: 
12      from Numeric import * 
13  except ImportError, x: 
14      raise ImportError, "This module requires Numeric (precursor to NumPy)" 
15   
16  from Bio import listfns 
17   
18 -def euclidean(x, y):
19 """euclidean(x, y) -> euclidean distance between x and y""" 20 if len(x) != len(y): 21 raise ValueError, "vectors must be same length" 22 #return sqrt(sum((x-y)**2)) 23 # Optimization by John Corradi (JCorradi@msn.com) 24 d = x-y 25 return sqrt(dot(d, d))
26
27 -def euclidean_py(x, y):
28 """euclidean_py(x, y) -> euclidean distance between x and y""" 29 # lightly modified from implementation by Thomas Sicheritz-Ponten. 30 # This works faster than the Numeric implementation on shorter 31 # vectors. 32 if len(x) != len(y): 33 raise ValueError, "vectors must be same length" 34 sum = 0 35 for i in range(len(x)): 36 sum += (x[i]-y[i])**2 37 return math.sqrt(sum)
38 39 # Try and load C implementations of functions. If I can't, 40 # then just ignore and use the pure python implementations. 41 try: 42 from cdistance import * 43 except ImportError: 44 pass 45