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.minkowski. |
20
|
|
|
|
21
|
|
|
The distance.minkowski module implements Minkowski token-based distances: |
22
|
|
|
|
23
|
|
|
- Minkowski distance & similarity |
24
|
|
|
- Manhattan distance & similarity |
25
|
|
|
- Euclidean distance & similarity |
26
|
|
|
- Chebyshev distance |
27
|
|
|
""" |
28
|
|
|
|
29
|
1 |
|
from __future__ import division, unicode_literals |
30
|
|
|
|
31
|
1 |
|
from numbers import Number |
32
|
|
|
|
33
|
1 |
|
from ._distance import TokenDistance |
34
|
|
|
|
35
|
1 |
|
__all__ = [ |
36
|
|
|
'Chebyshev', |
37
|
|
|
'Euclidean', |
38
|
|
|
'Manhattan', |
39
|
|
|
'Minkowski', |
40
|
|
|
'chebyshev', |
41
|
|
|
'dist_euclidean', |
42
|
|
|
'dist_manhattan', |
43
|
|
|
'dist_minkowski', |
44
|
|
|
'euclidean', |
45
|
|
|
'manhattan', |
46
|
|
|
'minkowski', |
47
|
|
|
'sim_euclidean', |
48
|
|
|
'sim_manhattan', |
49
|
|
|
'sim_minkowski', |
50
|
|
|
] |
51
|
|
|
|
52
|
|
|
|
53
|
1 |
|
class Minkowski(TokenDistance): |
|
|
|
|
54
|
|
|
"""Minkowski distance. |
55
|
|
|
|
56
|
|
|
The Minkowski distance :cite:`Minkowski:1910` is a distance metric in |
57
|
|
|
:math:`L^p-space`. |
58
|
|
|
""" |
59
|
|
|
|
60
|
1 |
|
def dist_abs( |
|
|
|
|
61
|
|
|
self, src, tar, qval=2, pval=1, normalized=False, alphabet=None |
|
|
|
|
62
|
|
|
): |
63
|
|
|
"""Return the Minkowski distance (:math:`L^p-norm`) of two strings. |
64
|
|
|
|
65
|
|
|
Args: |
66
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
67
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
68
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
69
|
|
|
pval (int or float): The :math:`p`-value of the :math:`L^p`-space |
70
|
|
|
normalized (bool): Normalizes to [0, 1] if True |
71
|
|
|
alphabet (collection or int): The values or size of the alphabet |
72
|
|
|
|
73
|
|
|
Returns: |
74
|
|
|
float: The Minkowski distance |
75
|
|
|
|
76
|
|
|
Examples: |
77
|
|
|
>>> cmp = Minkowski() |
78
|
|
|
>>> cmp.dist_abs('cat', 'hat') |
79
|
|
|
4.0 |
80
|
|
|
>>> cmp.dist_abs('Niall', 'Neil') |
81
|
|
|
7.0 |
82
|
|
|
>>> cmp.dist_abs('Colin', 'Cuilen') |
83
|
|
|
9.0 |
84
|
|
|
>>> cmp.dist_abs('ATCG', 'TAGC') |
85
|
|
|
10.0 |
86
|
|
|
|
87
|
|
|
""" |
88
|
1 |
|
q_src, q_tar = self._get_qgrams(src, tar, qval) |
89
|
1 |
|
diffs = ((q_src - q_tar) + (q_tar - q_src)).values() |
90
|
|
|
|
91
|
1 |
|
normalizer = 1 |
92
|
1 |
|
if normalized: |
93
|
1 |
|
totals = (q_src + q_tar).values() |
94
|
1 |
|
if alphabet is not None: |
95
|
|
|
# noinspection PyTypeChecker |
96
|
1 |
|
normalizer = ( |
97
|
|
|
alphabet if isinstance(alphabet, Number) else len(alphabet) |
98
|
|
|
) |
99
|
1 |
|
elif pval == 0: |
100
|
1 |
|
normalizer = len(totals) |
101
|
|
|
else: |
102
|
1 |
|
normalizer = sum(_ ** pval for _ in totals) ** (1 / pval) |
103
|
|
|
|
104
|
1 |
|
if len(diffs) == 0: |
|
|
|
|
105
|
1 |
|
return 0.0 |
106
|
1 |
|
if pval == float('inf'): |
107
|
|
|
# Chebyshev distance |
108
|
1 |
|
return max(diffs) / normalizer |
109
|
1 |
|
if pval == 0: |
110
|
|
|
# This is the l_0 "norm" as developed by David Donoho |
111
|
1 |
|
return len(diffs) / normalizer |
112
|
1 |
|
return sum(_ ** pval for _ in diffs) ** (1 / pval) / normalizer |
113
|
|
|
|
114
|
1 |
|
def dist(self, src, tar, qval=2, pval=1, alphabet=None): |
|
|
|
|
115
|
|
|
"""Return normalized Minkowski distance of two strings. |
116
|
|
|
|
117
|
|
|
The normalized Minkowski distance :cite:`Minkowski:1910` is a distance |
118
|
|
|
metric in :math:`L^p-space`, normalized to [0, 1]. |
119
|
|
|
|
120
|
|
|
Args: |
121
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
122
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
123
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
124
|
|
|
pval (int or float): The :math:`p`-value of the :math:`L^p`-space |
125
|
|
|
alphabet (collection or int): The values or size of the alphabet |
126
|
|
|
|
127
|
|
|
Returns: |
128
|
|
|
float: The normalized Minkowski distance |
129
|
|
|
|
130
|
|
|
Examples: |
131
|
|
|
>>> cmp = Minkowski() |
132
|
|
|
>>> cmp.dist('cat', 'hat') |
133
|
|
|
0.5 |
134
|
|
|
>>> round(cmp.dist('Niall', 'Neil'), 12) |
135
|
|
|
0.636363636364 |
136
|
|
|
>>> round(cmp.dist('Colin', 'Cuilen'), 12) |
137
|
|
|
0.692307692308 |
138
|
|
|
>>> cmp.dist('ATCG', 'TAGC') |
139
|
|
|
1.0 |
140
|
|
|
|
141
|
|
|
""" |
142
|
1 |
|
return self.dist_abs(src, tar, qval, pval, True, alphabet) |
143
|
|
|
|
144
|
|
|
|
145
|
1 |
|
def minkowski(src, tar, qval=2, pval=1, normalized=False, alphabet=None): |
|
|
|
|
146
|
|
|
"""Return the Minkowski distance (:math:`L^p-norm`) of two strings. |
147
|
|
|
|
148
|
|
|
This is a wrapper for :py:meth:`Minkowski.dist_abs`. |
149
|
|
|
|
150
|
|
|
Args: |
151
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
152
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
153
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
154
|
|
|
pval (int or float): The :math:`p`-value of the :math:`L^p`-space |
155
|
|
|
normalized (bool): Normalizes to [0, 1] if True |
156
|
|
|
alphabet (collection or int): The values or size of the alphabet |
157
|
|
|
|
158
|
|
|
Returns: |
159
|
|
|
float: The Minkowski distance |
160
|
|
|
|
161
|
|
|
Examples: |
162
|
|
|
>>> minkowski('cat', 'hat') |
163
|
|
|
4.0 |
164
|
|
|
>>> minkowski('Niall', 'Neil') |
165
|
|
|
7.0 |
166
|
|
|
>>> minkowski('Colin', 'Cuilen') |
167
|
|
|
9.0 |
168
|
|
|
>>> minkowski('ATCG', 'TAGC') |
169
|
|
|
10.0 |
170
|
|
|
|
171
|
|
|
""" |
172
|
1 |
|
return Minkowski().dist_abs(src, tar, qval, pval, normalized, alphabet) |
173
|
|
|
|
174
|
|
|
|
175
|
1 |
|
def dist_minkowski(src, tar, qval=2, pval=1, alphabet=None): |
176
|
|
|
"""Return normalized Minkowski distance of two strings. |
177
|
|
|
|
178
|
|
|
This is a wrapper for :py:meth:`Minkowski.dist`. |
179
|
|
|
|
180
|
|
|
Args: |
181
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
182
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
183
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
184
|
|
|
pval (int or float): The :math:`p`-value of the :math:`L^p`-space |
185
|
|
|
alphabet (collection or int): The values or size of the alphabet |
186
|
|
|
|
187
|
|
|
Returns: |
188
|
|
|
float: The normalized Minkowski distance |
189
|
|
|
|
190
|
|
|
Examples: |
191
|
|
|
>>> dist_minkowski('cat', 'hat') |
192
|
|
|
0.5 |
193
|
|
|
>>> round(dist_minkowski('Niall', 'Neil'), 12) |
194
|
|
|
0.636363636364 |
195
|
|
|
>>> round(dist_minkowski('Colin', 'Cuilen'), 12) |
196
|
|
|
0.692307692308 |
197
|
|
|
>>> dist_minkowski('ATCG', 'TAGC') |
198
|
|
|
1.0 |
199
|
|
|
|
200
|
|
|
""" |
201
|
1 |
|
return Minkowski().dist(src, tar, qval, pval, alphabet) |
202
|
|
|
|
203
|
|
|
|
204
|
1 |
|
def sim_minkowski(src, tar, qval=2, pval=1, alphabet=None): |
205
|
|
|
"""Return normalized Minkowski similarity of two strings. |
206
|
|
|
|
207
|
|
|
This is a wrapper for :py:meth:`Minkowski.sim`. |
208
|
|
|
|
209
|
|
|
Args: |
210
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
211
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
212
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
213
|
|
|
pval (int or float): The :math:`p`-value of the :math:`L^p`-space |
214
|
|
|
alphabet (collection or int): The values or size of the alphabet |
215
|
|
|
|
216
|
|
|
Returns: |
217
|
|
|
float: The normalized Minkowski similarity |
218
|
|
|
|
219
|
|
|
Examples: |
220
|
|
|
>>> sim_minkowski('cat', 'hat') |
221
|
|
|
0.5 |
222
|
|
|
>>> round(sim_minkowski('Niall', 'Neil'), 12) |
223
|
|
|
0.363636363636 |
224
|
|
|
>>> round(sim_minkowski('Colin', 'Cuilen'), 12) |
225
|
|
|
0.307692307692 |
226
|
|
|
>>> sim_minkowski('ATCG', 'TAGC') |
227
|
|
|
0.0 |
228
|
|
|
|
229
|
|
|
""" |
230
|
1 |
|
return Minkowski().sim(src, tar, qval, pval, alphabet) |
231
|
|
|
|
232
|
|
|
|
233
|
1 |
|
class Manhattan(Minkowski): |
|
|
|
|
234
|
|
|
"""Manhattan distance. |
235
|
|
|
|
236
|
|
|
Manhattan distance is the city-block or taxi-cab distance, equivalent |
237
|
|
|
to Minkowski distance in :math:`L^1`-space. |
238
|
|
|
""" |
239
|
|
|
|
240
|
1 |
|
def dist_abs(self, src, tar, qval=2, normalized=False, alphabet=None): |
|
|
|
|
241
|
|
|
"""Return the Manhattan distance between two strings. |
242
|
|
|
|
243
|
|
|
Args: |
244
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
245
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
246
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
247
|
|
|
normalized (bool): Normalizes to [0, 1] if True |
248
|
|
|
alphabet (collection or int): The values or size of the alphabet |
249
|
|
|
|
250
|
|
|
Returns: |
251
|
|
|
float: The Manhattan distance |
252
|
|
|
|
253
|
|
|
Examples: |
254
|
|
|
>>> cmp = Manhattan() |
255
|
|
|
>>> cmp.dist_abs('cat', 'hat') |
256
|
|
|
4.0 |
257
|
|
|
>>> cmp.dist_abs('Niall', 'Neil') |
258
|
|
|
7.0 |
259
|
|
|
>>> cmp.dist_abs('Colin', 'Cuilen') |
260
|
|
|
9.0 |
261
|
|
|
>>> cmp.dist_abs('ATCG', 'TAGC') |
262
|
|
|
10.0 |
263
|
|
|
|
264
|
|
|
""" |
265
|
1 |
|
return super(self.__class__, self).dist_abs( |
|
|
|
|
266
|
|
|
src, tar, qval, 1, normalized, alphabet |
267
|
|
|
) |
268
|
|
|
|
269
|
1 |
|
def dist(self, src, tar, qval=2, alphabet=None): |
|
|
|
|
270
|
|
|
"""Return the normalized Manhattan distance between two strings. |
271
|
|
|
|
272
|
|
|
The normalized Manhattan distance is a distance metric in |
273
|
|
|
:math:`L^1-space`, normalized to [0, 1]. |
274
|
|
|
|
275
|
|
|
This is identical to Canberra distance. |
276
|
|
|
|
277
|
|
|
Args: |
278
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
279
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
280
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
281
|
|
|
alphabet (collection or int): The values or size of the alphabet |
282
|
|
|
|
283
|
|
|
Returns: |
284
|
|
|
float: The normalized Manhattan distance |
285
|
|
|
|
286
|
|
|
Examples: |
287
|
|
|
>>> cmp = Manhattan() |
288
|
|
|
>>> cmp.dist('cat', 'hat') |
289
|
|
|
0.5 |
290
|
|
|
>>> round(cmp.dist('Niall', 'Neil'), 12) |
291
|
|
|
0.636363636364 |
292
|
|
|
>>> round(cmp.dist('Colin', 'Cuilen'), 12) |
293
|
|
|
0.692307692308 |
294
|
|
|
>>> cmp.dist('ATCG', 'TAGC') |
295
|
|
|
1.0 |
296
|
|
|
|
297
|
|
|
""" |
298
|
1 |
|
return self.dist_abs(src, tar, qval, True, alphabet) |
299
|
|
|
|
300
|
|
|
|
301
|
1 |
|
def manhattan(src, tar, qval=2, normalized=False, alphabet=None): |
302
|
|
|
"""Return the Manhattan distance between two strings. |
303
|
|
|
|
304
|
|
|
This is a wrapper for :py:meth:`Manhattan.dist_abs`. |
305
|
|
|
|
306
|
|
|
Args: |
307
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
308
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
309
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
310
|
|
|
normalized (bool): Normalizes to [0, 1] if True |
311
|
|
|
alphabet (collection or int): The values or size of the alphabet |
312
|
|
|
|
313
|
|
|
Returns: |
314
|
|
|
float: The Manhattan distance |
315
|
|
|
|
316
|
|
|
Examples: |
317
|
|
|
>>> manhattan('cat', 'hat') |
318
|
|
|
4.0 |
319
|
|
|
>>> manhattan('Niall', 'Neil') |
320
|
|
|
7.0 |
321
|
|
|
>>> manhattan('Colin', 'Cuilen') |
322
|
|
|
9.0 |
323
|
|
|
>>> manhattan('ATCG', 'TAGC') |
324
|
|
|
10.0 |
325
|
|
|
|
326
|
|
|
""" |
327
|
1 |
|
return Manhattan().dist_abs(src, tar, qval, normalized, alphabet) |
328
|
|
|
|
329
|
|
|
|
330
|
1 |
|
def dist_manhattan(src, tar, qval=2, alphabet=None): |
331
|
|
|
"""Return the normalized Manhattan distance between two strings. |
332
|
|
|
|
333
|
|
|
This is a wrapper for :py:meth:`Manhattan.dist`. |
334
|
|
|
|
335
|
|
|
Args: |
336
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
337
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
338
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
339
|
|
|
alphabet (collection or int): The values or size of the alphabet |
340
|
|
|
|
341
|
|
|
Returns: |
342
|
|
|
float: The normalized Manhattan distance |
343
|
|
|
|
344
|
|
|
Examples: |
345
|
|
|
>>> dist_manhattan('cat', 'hat') |
346
|
|
|
0.5 |
347
|
|
|
>>> round(dist_manhattan('Niall', 'Neil'), 12) |
348
|
|
|
0.636363636364 |
349
|
|
|
>>> round(dist_manhattan('Colin', 'Cuilen'), 12) |
350
|
|
|
0.692307692308 |
351
|
|
|
>>> dist_manhattan('ATCG', 'TAGC') |
352
|
|
|
1.0 |
353
|
|
|
|
354
|
|
|
""" |
355
|
1 |
|
return Manhattan().dist(src, tar, qval, alphabet) |
356
|
|
|
|
357
|
|
|
|
358
|
1 |
|
def sim_manhattan(src, tar, qval=2, alphabet=None): |
359
|
|
|
"""Return the normalized Manhattan similarity of two strings. |
360
|
|
|
|
361
|
|
|
This is a wrapper for :py:meth:`Manhattan.sim`. |
362
|
|
|
|
363
|
|
|
Args: |
364
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
365
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
366
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
367
|
|
|
alphabet (collection or int): The values or size of the alphabet |
368
|
|
|
|
369
|
|
|
Returns: |
370
|
|
|
float: The normalized Manhattan similarity |
371
|
|
|
|
372
|
|
|
Examples: |
373
|
|
|
>>> sim_manhattan('cat', 'hat') |
374
|
|
|
0.5 |
375
|
|
|
>>> round(sim_manhattan('Niall', 'Neil'), 12) |
376
|
|
|
0.363636363636 |
377
|
|
|
>>> round(sim_manhattan('Colin', 'Cuilen'), 12) |
378
|
|
|
0.307692307692 |
379
|
|
|
>>> sim_manhattan('ATCG', 'TAGC') |
380
|
|
|
0.0 |
381
|
|
|
|
382
|
|
|
""" |
383
|
1 |
|
return Manhattan().sim(src, tar, qval, alphabet) |
384
|
|
|
|
385
|
|
|
|
386
|
1 |
|
class Euclidean(Minkowski): |
|
|
|
|
387
|
|
|
"""Euclidean distance. |
388
|
|
|
|
389
|
|
|
Euclidean distance is the straigh-line or as-the-crow-flies distance, |
390
|
|
|
equivalent to Minkowski distance in :math:`L^2`-space. |
391
|
|
|
""" |
392
|
|
|
|
393
|
1 |
|
def dist_abs(self, src, tar, qval=2, normalized=False, alphabet=None): |
|
|
|
|
394
|
|
|
"""Return the Euclidean distance between two strings. |
395
|
|
|
|
396
|
|
|
Args: |
397
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
398
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
399
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
400
|
|
|
normalized (bool): Normalizes to [0, 1] if True |
401
|
|
|
alphabet (collection or int): The values or size of the alphabet |
402
|
|
|
|
403
|
|
|
Returns: |
404
|
|
|
float: The Euclidean distance |
405
|
|
|
|
406
|
|
|
Examples: |
407
|
|
|
>>> cmp = Euclidean() |
408
|
|
|
>>> cmp.dist_abs('cat', 'hat') |
409
|
|
|
2.0 |
410
|
|
|
>>> round(cmp.dist_abs('Niall', 'Neil'), 12) |
411
|
|
|
2.645751311065 |
412
|
|
|
>>> cmp.dist_abs('Colin', 'Cuilen') |
413
|
|
|
3.0 |
414
|
|
|
>>> round(cmp.dist_abs('ATCG', 'TAGC'), 12) |
415
|
|
|
3.162277660168 |
416
|
|
|
|
417
|
|
|
""" |
418
|
1 |
|
return super(self.__class__, self).dist_abs( |
|
|
|
|
419
|
|
|
src, tar, qval, 2, normalized, alphabet |
420
|
|
|
) |
421
|
|
|
|
422
|
1 |
|
def dist(self, src, tar, qval=2, alphabet=None): |
|
|
|
|
423
|
|
|
"""Return the normalized Euclidean distance between two strings. |
424
|
|
|
|
425
|
|
|
The normalized Euclidean distance is a distance |
426
|
|
|
metric in :math:`L^2-space`, normalized to [0, 1]. |
427
|
|
|
|
428
|
|
|
Args: |
429
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
430
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
431
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
432
|
|
|
alphabet (collection or int): The values or size of the alphabet |
433
|
|
|
|
434
|
|
|
Returns: |
435
|
|
|
float: The normalized Euclidean distance |
436
|
|
|
|
437
|
|
|
Examples: |
438
|
|
|
>>> cmp = Euclidean() |
439
|
|
|
>>> round(cmp.dist('cat', 'hat'), 12) |
440
|
|
|
0.57735026919 |
441
|
|
|
>>> round(cmp.dist('Niall', 'Neil'), 12) |
442
|
|
|
0.683130051064 |
443
|
|
|
>>> round(cmp.dist('Colin', 'Cuilen'), 12) |
444
|
|
|
0.727606875109 |
445
|
|
|
>>> cmp.dist('ATCG', 'TAGC') |
446
|
|
|
1.0 |
447
|
|
|
|
448
|
|
|
""" |
449
|
1 |
|
return self.dist_abs(src, tar, qval, True, alphabet) |
450
|
|
|
|
451
|
|
|
|
452
|
1 |
|
def euclidean(src, tar, qval=2, normalized=False, alphabet=None): |
453
|
|
|
"""Return the Euclidean distance between two strings. |
454
|
|
|
|
455
|
|
|
This is a wrapper for :py:meth:`Euclidean.dist_abs`. |
456
|
|
|
|
457
|
|
|
Args: |
458
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
459
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
460
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
461
|
|
|
normalized (bool): Normalizes to [0, 1] if True |
462
|
|
|
alphabet (collection or int): The values or size of the alphabet |
463
|
|
|
|
464
|
|
|
Returns: |
465
|
|
|
float: The Euclidean distance |
466
|
|
|
|
467
|
|
|
Examples: |
468
|
|
|
>>> euclidean('cat', 'hat') |
469
|
|
|
2.0 |
470
|
|
|
>>> round(euclidean('Niall', 'Neil'), 12) |
471
|
|
|
2.645751311065 |
472
|
|
|
>>> euclidean('Colin', 'Cuilen') |
473
|
|
|
3.0 |
474
|
|
|
>>> round(euclidean('ATCG', 'TAGC'), 12) |
475
|
|
|
3.162277660168 |
476
|
|
|
|
477
|
|
|
""" |
478
|
1 |
|
return Euclidean().dist_abs(src, tar, qval, normalized, alphabet) |
479
|
|
|
|
480
|
|
|
|
481
|
1 |
|
def dist_euclidean(src, tar, qval=2, alphabet=None): |
482
|
|
|
"""Return the normalized Euclidean distance between two strings. |
483
|
|
|
|
484
|
|
|
This is a wrapper for :py:meth:`Euclidean.dist`. |
485
|
|
|
|
486
|
|
|
Args: |
487
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
488
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
489
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
490
|
|
|
alphabet (collection or int): The values or size of the alphabet |
491
|
|
|
|
492
|
|
|
Returns: |
493
|
|
|
float: The normalized Euclidean distance |
494
|
|
|
|
495
|
|
|
Examples: |
496
|
|
|
>>> round(dist_euclidean('cat', 'hat'), 12) |
497
|
|
|
0.57735026919 |
498
|
|
|
>>> round(dist_euclidean('Niall', 'Neil'), 12) |
499
|
|
|
0.683130051064 |
500
|
|
|
>>> round(dist_euclidean('Colin', 'Cuilen'), 12) |
501
|
|
|
0.727606875109 |
502
|
|
|
>>> dist_euclidean('ATCG', 'TAGC') |
503
|
|
|
1.0 |
504
|
|
|
|
505
|
|
|
""" |
506
|
1 |
|
return Euclidean().dist(src, tar, qval, alphabet) |
507
|
|
|
|
508
|
|
|
|
509
|
1 |
|
def sim_euclidean(src, tar, qval=2, alphabet=None): |
510
|
|
|
"""Return the normalized Euclidean similarity of two strings. |
511
|
|
|
|
512
|
|
|
This is a wrapper for :py:meth:`Euclidean.sim`. |
513
|
|
|
|
514
|
|
|
Args: |
515
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
516
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
517
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
518
|
|
|
alphabet (collection or int): The values or size of the alphabet |
519
|
|
|
|
520
|
|
|
Returns: |
521
|
|
|
float: The normalized Euclidean similarity |
522
|
|
|
|
523
|
|
|
Examples: |
524
|
|
|
>>> round(sim_euclidean('cat', 'hat'), 12) |
525
|
|
|
0.42264973081 |
526
|
|
|
>>> round(sim_euclidean('Niall', 'Neil'), 12) |
527
|
|
|
0.316869948936 |
528
|
|
|
>>> round(sim_euclidean('Colin', 'Cuilen'), 12) |
529
|
|
|
0.272393124891 |
530
|
|
|
>>> sim_euclidean('ATCG', 'TAGC') |
531
|
|
|
0.0 |
532
|
|
|
|
533
|
|
|
""" |
534
|
1 |
|
return Euclidean().sim(src, tar, qval, alphabet) |
535
|
|
|
|
536
|
|
|
|
537
|
1 |
|
class Chebyshev(Minkowski): |
|
|
|
|
538
|
|
|
r"""Chebyshev distance. |
539
|
|
|
|
540
|
|
|
Euclidean distance is the chessboard distance, |
541
|
|
|
equivalent to Minkowski distance in :math:`L^\infty-space`. |
542
|
|
|
""" |
543
|
|
|
|
544
|
1 |
|
def dist_abs(self, src, tar, qval=2, alphabet=None): |
|
|
|
|
545
|
|
|
r"""Return the Chebyshev distance between two strings. |
546
|
|
|
|
547
|
|
|
Args: |
548
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
549
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
550
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
551
|
|
|
alphabet (collection or int): The values or size of the alphabet |
552
|
|
|
|
553
|
|
|
Returns: |
554
|
|
|
float: The Chebyshev distance |
555
|
|
|
|
556
|
|
|
Examples: |
557
|
|
|
>>> cmp = Chebyshev() |
558
|
|
|
>>> cmp.dist_abs('cat', 'hat') |
559
|
|
|
1.0 |
560
|
|
|
>>> cmp.dist_abs('Niall', 'Neil') |
561
|
|
|
1.0 |
562
|
|
|
>>> cmp.dist_abs('Colin', 'Cuilen') |
563
|
|
|
1.0 |
564
|
|
|
>>> cmp.dist_abs('ATCG', 'TAGC') |
565
|
|
|
1.0 |
566
|
|
|
>>> cmp.dist_abs('ATCG', 'TAGC', qval=1) |
567
|
|
|
0.0 |
568
|
|
|
>>> cmp.dist_abs('ATCGATTCGGAATTTC', 'TAGCATAATCGCCG', qval=1) |
569
|
|
|
3.0 |
570
|
|
|
|
571
|
|
|
""" |
572
|
1 |
|
return super(self.__class__, self).dist_abs( |
|
|
|
|
573
|
|
|
src, tar, qval, float('inf'), False, alphabet |
574
|
|
|
) |
575
|
|
|
|
576
|
1 |
|
def sim(self, *args, **kwargs): |
|
|
|
|
577
|
|
|
"""Raise exception when called. |
578
|
|
|
|
579
|
|
|
Args: |
580
|
|
|
*args: Variable length argument list |
581
|
|
|
**kwargs: Arbitrary keyword arguments |
582
|
|
|
|
583
|
|
|
Raises: |
584
|
|
|
Exception: Method disabled for Chebyshev distance |
585
|
|
|
|
586
|
|
|
""" |
587
|
|
|
raise Exception('Method disabled for Chebyshev distance.') |
588
|
|
|
|
589
|
1 |
|
def dist(self, *args, **kwargs): |
|
|
|
|
590
|
|
|
"""Raise exception when called. |
591
|
|
|
|
592
|
|
|
Args: |
593
|
|
|
*args: Variable length argument list |
594
|
|
|
**kwargs: Arbitrary keyword arguments |
595
|
|
|
|
596
|
|
|
Raises: |
597
|
|
|
Exception: Method disabled for Chebyshev distance |
598
|
|
|
|
599
|
|
|
""" |
600
|
|
|
raise Exception('Method disabled for Chebyshev distance.') |
601
|
|
|
|
602
|
|
|
|
603
|
1 |
|
def chebyshev(src, tar, qval=2, alphabet=None): |
604
|
|
|
r"""Return the Chebyshev distance between two strings. |
605
|
|
|
|
606
|
|
|
This is a wrapper for the :py:meth:`Chebyshev.dist_abs`. |
607
|
|
|
|
608
|
|
|
Args: |
609
|
|
|
src (str): Source string (or QGrams/Counter objects) for comparison |
610
|
|
|
tar (str): Target string (or QGrams/Counter objects) for comparison |
611
|
|
|
qval (int): The length of each q-gram; 0 for non-q-gram version |
612
|
|
|
alphabet (collection or int): The values or size of the alphabet |
613
|
|
|
|
614
|
|
|
Returns: |
615
|
|
|
float: The Chebyshev distance |
616
|
|
|
|
617
|
|
|
Examples: |
618
|
|
|
>>> chebyshev('cat', 'hat') |
619
|
|
|
1.0 |
620
|
|
|
>>> chebyshev('Niall', 'Neil') |
621
|
|
|
1.0 |
622
|
|
|
>>> chebyshev('Colin', 'Cuilen') |
623
|
|
|
1.0 |
624
|
|
|
>>> chebyshev('ATCG', 'TAGC') |
625
|
|
|
1.0 |
626
|
|
|
>>> chebyshev('ATCG', 'TAGC', qval=1) |
627
|
|
|
0.0 |
628
|
|
|
>>> chebyshev('ATCGATTCGGAATTTC', 'TAGCATAATCGCCG', qval=1) |
629
|
|
|
3.0 |
630
|
|
|
|
631
|
|
|
""" |
632
|
1 |
|
return Chebyshev().dist_abs(src, tar, qval, alphabet) |
633
|
|
|
|
634
|
|
|
|
635
|
|
|
if __name__ == '__main__': |
636
|
|
|
import doctest |
637
|
|
|
|
638
|
|
|
doctest.testmod() |
639
|
|
|
|