File: distance.py

package info (click to toggle)
python-biopython 1.42-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,584 kB
  • ctags: 12,272
  • sloc: python: 80,461; xml: 13,834; ansic: 7,902; cpp: 1,855; sql: 1,144; makefile: 203
file content (44 lines) | stat: -rw-r--r-- 1,251 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
This module provides code for various distance measures.

Functions:
euclidean       Euclidean distance between two points
euclidean_py    Pure Python implementation of euclidean.

"""
# XXX cosine distance
import math
try:
    from Numeric import *
except ImportError, x:
    raise ImportError, "This module requires NumPy"

from Bio import listfns

def euclidean(x, y):
    """euclidean(x, y) -> euclidean distance between x and y"""
    if len(x) != len(y):
        raise ValueError, "vectors must be same length"
    #return sqrt(sum((x-y)**2))
    # Optimization by John Corradi (JCorradi@msn.com)
    d = x-y
    return sqrt(dot(d, d))

def euclidean_py(x, y):
    """euclidean_py(x, y) -> euclidean distance between x and y"""
    # lightly modified from implementation by Thomas Sicheritz-Ponten.
    # This works faster than the Numeric implementation on shorter
    # vectors.
    if len(x) != len(y):
        raise ValueError, "vectors must be same length"
    sum = 0
    for i in range(len(x)):
        sum += (x[i]-y[i])**2
    return math.sqrt(sum)

# Try and load C implementations of functions.  If I can't,
# then just ignore and use the pure python implementations.
try:
    from cdistance import *
except ImportError:
    pass