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