1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
# Copyright 2018 by Christopher C. Little. |
4
|
|
|
# This file is part of Abydos. |
5
|
|
|
# |
6
|
|
|
# Abydos is free software: you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# Abydos is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with Abydos. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
1 |
|
"""abydos.distance._chebyshev. |
20
|
|
|
|
21
|
|
|
Chebyshev distance |
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
from __future__ import ( |
25
|
|
|
absolute_import, |
26
|
|
|
division, |
27
|
|
|
print_function, |
28
|
|
|
unicode_literals, |
29
|
|
|
) |
30
|
|
|
|
31
|
1 |
|
from ._minkowski import Minkowski |
32
|
|
|
|
33
|
1 |
|
__all__ = ['Chebyshev', 'chebyshev'] |
34
|
|
|
|
35
|
|
|
|
36
|
1 |
|
class Chebyshev(Minkowski): |
|
|
|
|
37
|
|
|
r"""Chebyshev distance. |
38
|
|
|
|
39
|
|
|
Euclidean distance is the chessboard distance, |
40
|
|
|
equivalent to Minkowski distance in :math:`L^\infty-space`. |
41
|
|
|
""" |
42
|
|
|
|
43
|
1 |
|
def dist_abs(self, src, tar, qval=2, alphabet=None): |
|
|
|
|
44
|
|
|
r"""Return the Chebyshev distance between two strings. |
45
|
|
|
|
46
|
|
|
Args: |
47
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
48
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
49
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
50
|
|
|
alphabet (collection or int): The values or size of the alphabet |
51
|
|
|
|
52
|
|
|
Returns: |
53
|
|
|
float: The Chebyshev distance |
54
|
|
|
|
55
|
|
|
Examples: |
56
|
|
|
>>> cmp = Chebyshev() |
57
|
|
|
>>> cmp.dist_abs('cat', 'hat') |
58
|
|
|
1.0 |
59
|
|
|
>>> cmp.dist_abs('Niall', 'Neil') |
60
|
|
|
1.0 |
61
|
|
|
>>> cmp.dist_abs('Colin', 'Cuilen') |
62
|
|
|
1.0 |
63
|
|
|
>>> cmp.dist_abs('ATCG', 'TAGC') |
64
|
|
|
1.0 |
65
|
|
|
>>> cmp.dist_abs('ATCG', 'TAGC', qval=1) |
66
|
|
|
0.0 |
67
|
|
|
>>> cmp.dist_abs('ATCGATTCGGAATTTC', 'TAGCATAATCGCCG', qval=1) |
68
|
|
|
3.0 |
69
|
|
|
|
70
|
|
|
""" |
71
|
1 |
|
return super(self.__class__, self).dist_abs( |
|
|
|
|
72
|
|
|
src, tar, qval, float('inf'), False, alphabet |
73
|
|
|
) |
74
|
|
|
|
75
|
1 |
|
def sim(self, *args, **kwargs): |
|
|
|
|
76
|
|
|
"""Raise exception when called. |
77
|
|
|
|
78
|
|
|
Args: |
79
|
|
|
*args: Variable length argument list |
80
|
|
|
**kwargs: Arbitrary keyword arguments |
81
|
|
|
|
82
|
|
|
Raises: |
83
|
|
|
Exception: Method disabled for Chebyshev distance |
84
|
|
|
|
85
|
|
|
""" |
86
|
|
|
raise Exception('Method disabled for Chebyshev distance.') |
87
|
|
|
|
88
|
1 |
|
def dist(self, *args, **kwargs): |
|
|
|
|
89
|
|
|
"""Raise exception when called. |
90
|
|
|
|
91
|
|
|
Args: |
92
|
|
|
*args: Variable length argument list |
93
|
|
|
**kwargs: Arbitrary keyword arguments |
94
|
|
|
|
95
|
|
|
Raises: |
96
|
|
|
Exception: Method disabled for Chebyshev distance |
97
|
|
|
|
98
|
|
|
""" |
99
|
|
|
raise Exception('Method disabled for Chebyshev distance.') |
100
|
|
|
|
101
|
|
|
|
102
|
1 |
|
def chebyshev(src, tar, qval=2, alphabet=None): |
103
|
|
|
r"""Return the Chebyshev distance between two strings. |
104
|
|
|
|
105
|
|
|
This is a wrapper for the :py:meth:`Chebyshev.dist_abs`. |
106
|
|
|
|
107
|
|
|
Args: |
108
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
109
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
110
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
111
|
|
|
alphabet (collection or int): The values or size of the alphabet |
112
|
|
|
|
113
|
|
|
Returns: |
114
|
|
|
float: The Chebyshev distance |
115
|
|
|
|
116
|
|
|
Examples: |
117
|
|
|
>>> chebyshev('cat', 'hat') |
118
|
|
|
1.0 |
119
|
|
|
>>> chebyshev('Niall', 'Neil') |
120
|
|
|
1.0 |
121
|
|
|
>>> chebyshev('Colin', 'Cuilen') |
122
|
|
|
1.0 |
123
|
|
|
>>> chebyshev('ATCG', 'TAGC') |
124
|
|
|
1.0 |
125
|
|
|
>>> chebyshev('ATCG', 'TAGC', qval=1) |
126
|
|
|
0.0 |
127
|
|
|
>>> chebyshev('ATCGATTCGGAATTTC', 'TAGCATAATCGCCG', qval=1) |
128
|
|
|
3.0 |
129
|
|
|
|
130
|
|
|
""" |
131
|
1 |
|
return Chebyshev().dist_abs(src, tar, qval, alphabet) |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
if __name__ == '__main__': |
135
|
|
|
import doctest |
136
|
|
|
|
137
|
|
|
doctest.testmod() |
138
|
|
|
|