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
|
|
|
Parameters |
47
|
|
|
---------- |
48
|
|
|
src : str |
49
|
|
|
Source string (or QGrams/Counter objects) for comparison |
50
|
|
|
tar : str |
51
|
|
|
Target string (or QGrams/Counter objects) for comparison |
52
|
|
|
qval : int |
53
|
|
|
The length of each q-gram; 0 for non-q-gram version alphabet |
54
|
|
|
alphabet : collection or int |
55
|
|
|
The values or size of the alphabet |
56
|
|
|
|
57
|
|
|
Returns |
58
|
|
|
------- |
59
|
|
|
float |
60
|
|
|
The Chebyshev distance |
61
|
|
|
|
62
|
|
|
Examples |
63
|
|
|
-------- |
64
|
|
|
>>> cmp = Chebyshev() |
65
|
|
|
>>> cmp.dist_abs('cat', 'hat') |
66
|
|
|
1.0 |
67
|
|
|
>>> cmp.dist_abs('Niall', 'Neil') |
68
|
|
|
1.0 |
69
|
|
|
>>> cmp.dist_abs('Colin', 'Cuilen') |
70
|
|
|
1.0 |
71
|
|
|
>>> cmp.dist_abs('ATCG', 'TAGC') |
72
|
|
|
1.0 |
73
|
|
|
>>> cmp.dist_abs('ATCG', 'TAGC', qval=1) |
74
|
|
|
0.0 |
75
|
|
|
>>> cmp.dist_abs('ATCGATTCGGAATTTC', 'TAGCATAATCGCCG', qval=1) |
76
|
|
|
3.0 |
77
|
|
|
|
78
|
|
|
""" |
79
|
1 |
|
return super(self.__class__, self).dist_abs( |
|
|
|
|
80
|
|
|
src, tar, qval, float('inf'), False, alphabet |
81
|
|
|
) |
82
|
|
|
|
83
|
1 |
|
def sim(self, *args, **kwargs): |
|
|
|
|
84
|
|
|
"""Raise exception when called. |
85
|
|
|
|
86
|
|
|
Parameters |
87
|
|
|
---------- |
88
|
|
|
*args |
89
|
|
|
Variable length argument list |
90
|
|
|
**kwargs |
91
|
|
|
Arbitrary keyword arguments |
92
|
|
|
|
93
|
|
|
Raises |
94
|
|
|
------ |
95
|
|
|
NotImplementedError |
96
|
|
|
Method disabled for Chebyshev distance |
97
|
|
|
|
98
|
|
|
""" |
99
|
|
|
raise NotImplementedError('Method disabled for Chebyshev distance.') |
100
|
|
|
|
101
|
1 |
|
def dist(self, *args, **kwargs): |
|
|
|
|
102
|
|
|
"""Raise exception when called. |
103
|
|
|
|
104
|
|
|
Parameters |
105
|
|
|
---------- |
106
|
|
|
*args |
107
|
|
|
Variable length argument list |
108
|
|
|
**kwargs |
109
|
|
|
Arbitrary keyword arguments |
110
|
|
|
|
111
|
|
|
Raises |
112
|
|
|
------ |
113
|
|
|
NotImplementedError |
114
|
|
|
Method disabled for Chebyshev distance |
115
|
|
|
|
116
|
|
|
""" |
117
|
|
|
raise NotImplementedError('Method disabled for Chebyshev distance.') |
118
|
|
|
|
119
|
|
|
|
120
|
1 |
|
def chebyshev(src, tar, qval=2, alphabet=None): |
121
|
|
|
r"""Return the Chebyshev distance between two strings. |
122
|
|
|
|
123
|
|
|
This is a wrapper for the :py:meth:`Chebyshev.dist_abs`. |
124
|
|
|
|
125
|
|
|
Parameters |
126
|
|
|
---------- |
127
|
|
|
src : str |
128
|
|
|
Source string (or QGrams/Counter objects) for comparison |
129
|
|
|
tar : str |
130
|
|
|
Target string (or QGrams/Counter objects) for comparison |
131
|
|
|
qval : int |
132
|
|
|
The length of each q-gram; 0 for non-q-gram version alphabet |
133
|
|
|
alphabet : collection or int |
134
|
|
|
The values or size of the alphabet |
135
|
|
|
|
136
|
|
|
Returns |
137
|
|
|
------- |
138
|
|
|
float |
139
|
|
|
The Chebyshev distance |
140
|
|
|
|
141
|
|
|
Examples |
142
|
|
|
-------- |
143
|
|
|
>>> chebyshev('cat', 'hat') |
144
|
|
|
1.0 |
145
|
|
|
>>> chebyshev('Niall', 'Neil') |
146
|
|
|
1.0 |
147
|
|
|
>>> chebyshev('Colin', 'Cuilen') |
148
|
|
|
1.0 |
149
|
|
|
>>> chebyshev('ATCG', 'TAGC') |
150
|
|
|
1.0 |
151
|
|
|
>>> chebyshev('ATCG', 'TAGC', qval=1) |
152
|
|
|
0.0 |
153
|
|
|
>>> chebyshev('ATCGATTCGGAATTTC', 'TAGCATAATCGCCG', qval=1) |
154
|
|
|
3.0 |
155
|
|
|
|
156
|
|
|
""" |
157
|
1 |
|
return Chebyshev().dist_abs(src, tar, qval, alphabet) |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
if __name__ == '__main__': |
161
|
|
|
import doctest |
162
|
|
|
|
163
|
|
|
doctest.testmod() |
164
|
|
|
|