| 1 | from __future__ import division |
| 2 | from functools import wraps |
| 3 | |
| 4 | |
| 5 | def cached(fun): |
| 6 | """ |
| 7 | memoizing decorator for linkage functions. |
| 8 | |
| 9 | Parameters have been hardcoded (no ``*args``, ``**kwargs`` magic), because, |
| 10 | the way this is coded (interchangingly using sets and frozensets) is true |
| 11 | for this specific case. For other cases that is not necessarily guaranteed. |
| 12 | """ |
| 13 | |
| 14 | _cache = {} |
| 15 | |
| 16 | @wraps(fun) |
| 17 | def newfun(a, b, distance_function): |
| 18 | frozen_a = frozenset(a) |
| 19 | frozen_b = frozenset(b) |
| 20 | if (frozen_a, frozen_b) not in _cache: |
| 21 | result = fun(a, b, distance_function) |
| 22 | _cache[(frozen_a, frozen_b)] = result |
| 23 | return _cache[(frozen_a, frozen_b)] |
| 24 | return newfun |
| 25 | |
| 26 | |
| 27 | @cached |
| 28 | def single(a, b, distance_function): |
| 29 | """ |
| 30 | Given two collections ``a`` and ``b``, this will return the distance of the |
| 31 | points which are closest together. ``distance_function`` is used to |
| 32 | determine the distance between two elements. |
| 33 | |
| 34 | Example:: |
| 35 | |
| 36 | >>> single([1, 2], [3, 4], lambda x, y: abs(x-y)) |
| 37 | 1 # (distance between 2 and 3) |
| 38 | """ |
| 39 | left_a, right_a = min(a), max(a) |
| 40 | left_b, right_b = min(b), max(b) |
| 41 | result = min(distance_function(left_a, right_b), |
| 42 | distance_function(left_b, right_a)) |
| 43 | return result |
| 44 | |
| 45 | |
| 46 | @cached |
| 47 | def complete(a, b, distance_function): |
| 48 | """ |
| 49 | Given two collections ``a`` and ``b``, this will return the distance of the |
| 50 | points which are farthest apart. ``distance_function`` is used to determine |
| 51 | the distance between two elements. |
| 52 | |
| 53 | Example:: |
| 54 | |
| 55 | >>> single([1, 2], [3, 4], lambda x, y: abs(x-y)) |
| 56 | 3 # (distance between 1 and 4) |
| 57 | """ |
| 58 | left_a, right_a = min(a), max(a) |
| 59 | left_b, right_b = min(b), max(b) |
| 60 | result = max(distance_function(left_a, right_b), |
| 61 | distance_function(left_b, right_a)) |
| 62 | return result |
| 63 | |
| 64 | |
| 65 | @cached |
| 66 | def average(a, b, distance_function): |
| 67 | """ |
| 68 | Given two collections ``a`` and ``b``, this will return the mean of all |
| 69 | distances. ``distance_function`` is used to determine the distance between |
| 70 | two elements. |
| 71 | |
| 72 | Example:: |
| 73 | |
| 74 | >>> single([1, 2], [3, 100], lambda x, y: abs(x-y)) |
| 75 | 26 |
| 76 | """ |
| 77 | distances = [distance_function(x, y) |
| 78 | for x in a for y in b] |
| 79 | return sum(distances) / len(distances) |
| 80 | |
| 81 | |
| 82 | @cached |
| 83 | def uclus(a, b, distance_function): |
| 84 | """ |
| 85 | Given two collections ``a`` and ``b``, this will return the *median* of all |
| 86 | distances. ``distance_function`` is used to determine the distance between |
| 87 | two elements. |
| 88 | |
| 89 | Example:: |
| 90 | |
| 91 | >>> single([1, 2], [3, 100], lambda x, y: abs(x-y)) |
| 92 | 2.5 |
| 93 | """ |
| 94 | distances = sorted([distance_function(x, y) |
| 95 | for x in a for y in b]) |
| 96 | midpoint, rest = len(distances) // 2, len(distances) % 2 |
| 97 | if not rest: |
| 98 | return sum(distances[midpoint-1:midpoint+1]) / 2 |
| 99 | else: |
| 100 | return distances[midpoint] |