/
_distance.py
1119 lines (1037 loc) · 35.7 KB
/
_distance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
__maintainer__ = []
from typing import Any, Callable, List, Optional, Tuple, TypedDict, Union
import numpy as np
from typing_extensions import Unpack
from aeon.distances._adtw import (
adtw_alignment_path,
adtw_cost_matrix,
adtw_distance,
adtw_pairwise_distance,
)
from aeon.distances._ddtw import (
ddtw_alignment_path,
ddtw_cost_matrix,
ddtw_distance,
ddtw_pairwise_distance,
)
from aeon.distances._dtw import (
dtw_alignment_path,
dtw_cost_matrix,
dtw_distance,
dtw_pairwise_distance,
)
from aeon.distances._edr import (
edr_alignment_path,
edr_cost_matrix,
edr_distance,
edr_pairwise_distance,
)
from aeon.distances._erp import (
erp_alignment_path,
erp_cost_matrix,
erp_distance,
erp_pairwise_distance,
)
from aeon.distances._euclidean import euclidean_distance, euclidean_pairwise_distance
from aeon.distances._lcss import (
lcss_alignment_path,
lcss_cost_matrix,
lcss_distance,
lcss_pairwise_distance,
)
from aeon.distances._manhattan import manhattan_distance, manhattan_pairwise_distance
from aeon.distances._minkowski import minkowski_distance, minkowski_pairwise_distance
from aeon.distances._msm import (
msm_alignment_path,
msm_cost_matrix,
msm_distance,
msm_pairwise_distance,
)
from aeon.distances._sbd import sbd_distance, sbd_pairwise_distance
from aeon.distances._shape_dtw import (
shape_dtw_alignment_path,
shape_dtw_cost_matrix,
shape_dtw_distance,
shape_dtw_pairwise_distance,
)
from aeon.distances._squared import squared_distance, squared_pairwise_distance
from aeon.distances._twe import (
twe_alignment_path,
twe_cost_matrix,
twe_distance,
twe_pairwise_distance,
)
from aeon.distances._utils import _convert_to_list, _is_multivariate
from aeon.distances._wddtw import (
wddtw_alignment_path,
wddtw_cost_matrix,
wddtw_distance,
wddtw_pairwise_distance,
)
from aeon.distances._wdtw import (
wdtw_alignment_path,
wdtw_cost_matrix,
wdtw_distance,
wdtw_pairwise_distance,
)
from aeon.distances.mpdist import mpdist
class DistanceKwargs(TypedDict, total=False):
window: Optional[float]
itakura_max_slope: Optional[float]
p: float
w: np.ndarray
g: float
descriptor: str
reach: int
epsilon: float
g_arr: np.ndarray
nu: float
lmbda: float
independent: bool
c: float
warp_penalty: float
standardize: bool
m: int
DistanceFunction = Callable[[np.ndarray, np.ndarray, Any], float]
AlignmentPathFunction = Callable[
[np.ndarray, np.ndarray, Any], Tuple[List[Tuple[int, int]], float]
]
CostMatrixFunction = Callable[[np.ndarray, np.ndarray, Any], np.ndarray]
PairwiseFunction = Callable[[np.ndarray, np.ndarray, Any], np.ndarray]
def distance(
x: np.ndarray,
y: np.ndarray,
metric: Union[str, DistanceFunction],
**kwargs: Unpack[DistanceKwargs],
) -> float:
"""Compute the distance between two time series.
Parameters
----------
x : np.ndarray
First time series, either univariate, shape ``(n_timepoints,)``, or
multivariate, shape ``(n_channels, n_timepoints)``.
y : np.ndarray
Second time series, either univariate, shape ``(n_timepoints,)``, or
multivariate, shape ``(n_channels, n_timepoints)``.
metric : str or Callable
The distance metric to use.
A list of valid distance metrics can be found in the documentation for
:func:`aeon.distances.get_distance_function` or by calling the function
:func:`aeon.distances.get_distance_function_names`.
kwargs : Any
Arguments for metric. Refer to each metrics documentation for a list of
possible arguments.
Returns
-------
float
Distance between the x and y.
Raises
------
ValueError
If x and y are not 1D, or 2D arrays.
If metric is not a valid string or callable.
Examples
--------
>>> import numpy as np
>>> from aeon.distances import distance
>>> x = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
>>> y = np.array([[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])
>>> distance(x, y, metric="dtw")
768.0
"""
if metric == "squared":
return squared_distance(x, y)
elif metric == "euclidean":
return euclidean_distance(x, y)
elif metric == "manhattan":
return manhattan_distance(x, y)
elif metric == "minkowski":
return minkowski_distance(x, y, kwargs.get("p", 2.0), kwargs.get("w", None))
elif metric == "dtw":
return dtw_distance(x, y, kwargs.get("window"), kwargs.get("itakura_max_slope"))
elif metric == "ddtw":
return ddtw_distance(
x, y, kwargs.get("window"), kwargs.get("itakura_max_slope")
)
elif metric == "wdtw":
return wdtw_distance(
x,
y,
kwargs.get("window"),
kwargs.get("g", 0.05),
kwargs.get("itakura_max_slope"),
)
elif metric == "shape_dtw":
return shape_dtw_distance(
x,
y,
window=kwargs.get("window"),
itakura_max_slope=kwargs.get("itakura_max_slope"),
descriptor=kwargs.get("descriptor", "identity"),
reach=kwargs.get("reach", 30),
transformation_precomputed=kwargs.get("transformation_precomputed", False),
transformed_x=kwargs.get("transformed_x", None),
transformed_y=kwargs.get("transformed_y", None),
)
elif metric == "wddtw":
return wddtw_distance(
x,
y,
kwargs.get("window"),
kwargs.get("g", 0.05),
kwargs.get("itakura_max_slope"),
)
elif metric == "lcss":
return lcss_distance(
x,
y,
kwargs.get("window"),
kwargs.get("epsilon", 1.0),
kwargs.get("itakura_max_slope"),
)
elif metric == "erp":
return erp_distance(
x,
y,
kwargs.get("window"),
kwargs.get("g", 0.0),
kwargs.get("g_arr", None),
kwargs.get("itakura_max_slope"),
)
elif metric == "edr":
return edr_distance(
x,
y,
kwargs.get("window"),
kwargs.get("epsilon"),
kwargs.get("itakura_max_slope"),
)
elif metric == "twe":
return twe_distance(
x,
y,
kwargs.get("window"),
kwargs.get("nu", 0.001),
kwargs.get("lmbda", 1.0),
kwargs.get("itakura_max_slope"),
)
elif metric == "msm":
return msm_distance(
x,
y,
kwargs.get("window"),
kwargs.get("independent", True),
kwargs.get("c", 1.0),
kwargs.get("itakura_max_slope"),
)
elif metric == "mpdist":
return mpdist(x, y, kwargs.get("m", 0))
elif metric == "adtw":
return adtw_distance(
x,
y,
itakura_max_slope=kwargs.get("itakura_max_slope"),
window=kwargs.get("window"),
warp_penalty=kwargs.get("warp_penalty", 1.0),
)
elif metric == "sbd":
return sbd_distance(x, y, kwargs.get("standardize", True))
else:
if isinstance(metric, Callable):
return metric(x, y, **kwargs)
raise ValueError("Metric must be one of the supported strings or a callable")
def pairwise_distance(
x: np.ndarray,
y: Optional[np.ndarray] = None,
metric: Union[str, DistanceFunction, None] = None,
**kwargs: Unpack[DistanceKwargs],
) -> np.ndarray:
"""Compute the pairwise distance matrix between two time series.
Parameters
----------
X : np.ndarray
A collection of time series instances of shape ``(n_cases, n_timepoints)``
or ``(n_cases, n_channels, n_timepoints)``.
y : np.ndarray or None, default=None
A single series or a collection of time series of shape ``(m_timepoints,)`` or
``(m_cases, m_timepoints)`` or ``(m_cases, m_channels, m_timepoints)``
metric : str or Callable
The distance metric to use.
A list of valid distance metrics can be found in the documentation for
:func:`aeon.distances.get_distance_function` or by calling the function
:func:`aeon.distances.get_distance_function_names`.
kwargs : Any
Extra arguments for metric. Refer to each metric documentation for a list of
possible arguments.
Returns
-------
np.ndarray (n_cases, n_cases)
pairwise matrix between the instances of X.
Raises
------
ValueError
If X is not 2D or 3D array when only passing X.
If X and y are not 1D, 2D or 3D arrays when passing both X and y.
If metric is not a valid string or callable.
Examples
--------
>>> import numpy as np
>>> from aeon.distances import pairwise_distance
>>> # Distance between each time series in a collection of time series
>>> X = np.array([[[1, 2, 3]],[[4, 5, 6]], [[7, 8, 9]]])
>>> pairwise_distance(X, metric='dtw')
array([[ 0., 26., 108.],
[ 26., 0., 26.],
[108., 26., 0.]])
>>> # Distance between two collections of time series
>>> X = np.array([[[1, 2, 3]],[[4, 5, 6]], [[7, 8, 9]]])
>>> y = np.array([[[11, 12, 13]],[[14, 15, 16]], [[17, 18, 19]]])
>>> pairwise_distance(X, y, metric='dtw')
array([[300., 507., 768.],
[147., 300., 507.],
[ 48., 147., 300.]])
>>> X = np.array([[[1, 2, 3]],[[4, 5, 6]], [[7, 8, 9]]])
>>> y_univariate = np.array([11, 12, 13])
>>> pairwise_distance(X, y_univariate, metric='dtw')
array([[300.],
[147.],
[ 48.]])
"""
if metric == "squared":
return squared_pairwise_distance(x, y)
elif metric == "euclidean":
return euclidean_pairwise_distance(x, y)
elif metric == "manhattan":
return manhattan_pairwise_distance(x, y)
elif metric == "minkowski":
return minkowski_pairwise_distance(
x, y, kwargs.get("p", 2.0), kwargs.get("w", None)
)
elif metric == "dtw":
return dtw_pairwise_distance(
x, y, kwargs.get("window"), kwargs.get("itakura_max_slope")
)
elif metric == "shape_dtw":
return shape_dtw_pairwise_distance(
x,
y,
window=kwargs.get("window"),
itakura_max_slope=kwargs.get("itakura_max_slope"),
descriptor=kwargs.get("descriptor", "identity"),
reach=kwargs.get("reach", 30),
transformation_precomputed=kwargs.get("transformation_precomputed", False),
transformed_x=kwargs.get("transformed_x", None),
transformed_y=kwargs.get("transformed_y", None),
)
elif metric == "ddtw":
return ddtw_pairwise_distance(
x, y, kwargs.get("window"), kwargs.get("itakura_max_slope")
)
elif metric == "wdtw":
return wdtw_pairwise_distance(
x,
y,
kwargs.get("window"),
kwargs.get("g", 0.05),
kwargs.get("itakura_max_slope"),
)
elif metric == "wddtw":
return wddtw_pairwise_distance(
x,
y,
kwargs.get("window"),
kwargs.get("g", 0.05),
kwargs.get("itakura_max_slope"),
)
elif metric == "lcss":
return lcss_pairwise_distance(
x,
y,
kwargs.get("window"),
kwargs.get("epsilon", 1.0),
kwargs.get("itakura_max_slope"),
)
elif metric == "erp":
return erp_pairwise_distance(
x,
y,
kwargs.get("window"),
kwargs.get("g", 0.0),
kwargs.get("g_arr", None),
kwargs.get("itakura_max_slope"),
)
elif metric == "edr":
return edr_pairwise_distance(
x,
y,
kwargs.get("window"),
kwargs.get("epsilon"),
kwargs.get("itakura_max_slope"),
)
elif metric == "twe":
return twe_pairwise_distance(
x,
y,
kwargs.get("window"),
kwargs.get("nu", 0.001),
kwargs.get("lmbda", 1.0),
kwargs.get("itakura_max_slope"),
)
elif metric == "msm":
return msm_pairwise_distance(
x,
y,
kwargs.get("window"),
kwargs.get("independent", True),
kwargs.get("c", 1.0),
kwargs.get("itakura_max_slope"),
)
elif metric == "mpdist":
return _custom_func_pairwise(x, y, mpdist, **kwargs)
elif metric == "adtw":
return adtw_pairwise_distance(
x,
y,
kwargs.get("window"),
kwargs.get("itakura_max_slope"),
kwargs.get("warp_penalty", 1.0),
)
elif metric == "sbd":
return sbd_pairwise_distance(x, y, kwargs.get("standardize", True))
else:
if isinstance(metric, Callable):
return _custom_func_pairwise(x, y, metric, **kwargs)
raise ValueError("Metric must be one of the supported strings or a callable")
def _custom_func_pairwise(
X: Optional[Union[np.ndarray, List[np.ndarray]]],
y: Optional[Union[np.ndarray, List[np.ndarray]]] = None,
dist_func: Union[DistanceFunction, None] = None,
**kwargs: Unpack[DistanceKwargs],
) -> np.ndarray:
if dist_func is None:
raise ValueError("dist_func must be a callable")
multivariate_conversion = _is_multivariate(X, y)
X, _ = _convert_to_list(X, "X", multivariate_conversion)
if y is None:
# To self
return _custom_pairwise_distance(X, dist_func, **kwargs)
y, _ = _convert_to_list(y, "y", multivariate_conversion)
return _custom_from_multiple_to_multiple_distance(X, y, dist_func, **kwargs)
def _custom_pairwise_distance(
X: Union[np.ndarray, List[np.ndarray]],
dist_func: DistanceFunction,
**kwargs: Unpack[DistanceKwargs],
) -> np.ndarray:
n_cases = len(X)
distances = np.zeros((n_cases, n_cases))
for i in range(n_cases):
for j in range(i + 1, n_cases):
distances[i, j] = dist_func(X[i], X[j], **kwargs)
distances[j, i] = distances[i, j]
return distances
def _custom_from_multiple_to_multiple_distance(
x: Union[np.ndarray, List[np.ndarray]],
y: Union[np.ndarray, List[np.ndarray]],
dist_func: DistanceFunction,
**kwargs: Unpack[DistanceKwargs],
) -> np.ndarray:
n_cases = len(x)
m_cases = len(y)
distances = np.zeros((n_cases, m_cases))
for i in range(n_cases):
for j in range(m_cases):
distances[i, j] = dist_func(x[i], y[j], **kwargs)
return distances
def alignment_path(
x: np.ndarray,
y: np.ndarray,
metric: str,
**kwargs: Unpack[DistanceKwargs],
) -> Tuple[List[Tuple[int, int]], float]:
"""Compute the alignment path and distance between two time series.
Parameters
----------
x : np.ndarray, of shape (n_channels, n_timepoints) or (n_timepoints,)
First time series.
y : np.ndarray, of shape (m_channels, m_timepoints) or (m_timepoints,)
Second time series.
metric : str
The distance metric to use.
A list of valid distance metrics can be found in the documentation for
:func:`aeon.distances.get_distance_function` or by calling the function
:func:`aeon.distances.get_distance_function_names`.
kwargs : any
Arguments for metric. Refer to each metrics documentation for a list of
possible arguments.
Returns
-------
List[Tuple[int, int]]
The alignment path between the two time series where each element is a tuple
of the index in x and the index in y that have the best alignment according
to the cost matrix.
float
The dtw distance betweeen the two time series.
Raises
------
ValueError
If x and y are not 1D, or 2D arrays.
If metric is not one of the supported strings or a callable.
Examples
--------
>>> import numpy as np
>>> from aeon.distances import alignment_path
>>> x = np.array([[1, 2, 3, 6]])
>>> y = np.array([[1, 2, 3, 4]])
>>> alignment_path(x, y, metric='dtw')
([(0, 0), (1, 1), (2, 2), (3, 3)], 4.0)
"""
if metric == "dtw":
return dtw_alignment_path(
x, y, kwargs.get("window"), kwargs.get("itakura_max_slope")
)
elif metric == "shape_dtw":
return shape_dtw_alignment_path(
x,
y,
window=kwargs.get("window"),
itakura_max_slope=kwargs.get("itakura_max_slope"),
descriptor=kwargs.get("descriptor", "identity"),
reach=kwargs.get("reach", 30),
transformation_precomputed=kwargs.get("transformation_precomputed", False),
transformed_x=kwargs.get("transformed_x", None),
transformed_y=kwargs.get("transformed_y", None),
)
elif metric == "ddtw":
return ddtw_alignment_path(
x, y, kwargs.get("window"), kwargs.get("itakura_max_slope")
)
elif metric == "wdtw":
return wdtw_alignment_path(
x,
y,
kwargs.get("window"),
kwargs.get("g", 0.05),
kwargs.get("itakura_max_slope"),
)
elif metric == "wddtw":
return wddtw_alignment_path(
x,
y,
kwargs.get("window"),
kwargs.get("g", 0.05),
kwargs.get("itakura_max_slope"),
)
elif metric == "lcss":
return lcss_alignment_path(
x,
y,
kwargs.get("window"),
kwargs.get("epsilon", 1.0),
kwargs.get("itakura_max_slope"),
)
elif metric == "erp":
return erp_alignment_path(
x,
y,
kwargs.get("window"),
kwargs.get("g", 0.0),
kwargs.get("g_arr", None),
kwargs.get("itakura_max_slope"),
)
elif metric == "edr":
return edr_alignment_path(
x,
y,
kwargs.get("window"),
kwargs.get("epsilon"),
kwargs.get("itakura_max_slope"),
)
elif metric == "twe":
return twe_alignment_path(
x,
y,
kwargs.get("window"),
kwargs.get("nu", 0.001),
kwargs.get("lmbda", 1.0),
kwargs.get("itakura_max_slope"),
)
elif metric == "msm":
return msm_alignment_path(
x,
y,
kwargs.get("window"),
kwargs.get("independent", True),
kwargs.get("c", 1.0),
kwargs.get("itakura_max_slope"),
)
elif metric == "adtw":
return adtw_alignment_path(
x,
y,
kwargs.get("window"),
kwargs.get("itakura_max_slope"),
kwargs.get("warp_penalty", 1.0),
)
else:
raise ValueError("Metric must be one of the supported strings")
def cost_matrix(
x: np.ndarray,
y: np.ndarray,
metric: str,
**kwargs: Unpack[DistanceKwargs],
) -> np.ndarray:
"""Compute the alignment path and distance between two time series.
Parameters
----------
x : np.ndarray, of shape (n_channels, n_timepoints) or (n_timepoints,)
First time series.
y : np.ndarray, of shape (m_channels, m_timepoints) or (m_timepoints,)
Second time series.
metric : str or Callable
The distance metric to use.
A list of valid distance metrics can be found in the documentation for
:func:`aeon.distances.get_distance_function` or by calling the function
:func:`aeon.distances.get_distance_function_names`.
kwargs : Any
Arguments for metric. Refer to each metrics documentation for a list of
possible arguments.
Returns
-------
np.ndarray (n_timepoints, m_timepoints)
cost matrix between x and y.
Raises
------
ValueError
If x and y are not 1D, or 2D arrays.
If metric is not one of the supported strings or a callable.
Examples
--------
>>> import numpy as np
>>> from aeon.distances import cost_matrix
>>> x = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
>>> y = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
>>> cost_matrix(x, y, metric="dtw")
array([[ 0., 1., 5., 14., 30., 55., 91., 140., 204., 285.],
[ 1., 0., 1., 5., 14., 30., 55., 91., 140., 204.],
[ 5., 1., 0., 1., 5., 14., 30., 55., 91., 140.],
[ 14., 5., 1., 0., 1., 5., 14., 30., 55., 91.],
[ 30., 14., 5., 1., 0., 1., 5., 14., 30., 55.],
[ 55., 30., 14., 5., 1., 0., 1., 5., 14., 30.],
[ 91., 55., 30., 14., 5., 1., 0., 1., 5., 14.],
[140., 91., 55., 30., 14., 5., 1., 0., 1., 5.],
[204., 140., 91., 55., 30., 14., 5., 1., 0., 1.],
[285., 204., 140., 91., 55., 30., 14., 5., 1., 0.]])
"""
if metric == "dtw":
return dtw_cost_matrix(
x, y, kwargs.get("window"), kwargs.get("itakura_max_slope")
)
elif metric == "shape_dtw":
return shape_dtw_cost_matrix(
x,
y,
window=kwargs.get("window"),
itakura_max_slope=kwargs.get("itakura_max_slope"),
descriptor=kwargs.get("descriptor", "identity"),
reach=kwargs.get("reach", 30),
transformation_precomputed=kwargs.get("transformation_precomputed", False),
transformed_x=kwargs.get("transformed_x", None),
transformed_y=kwargs.get("transformed_y", None),
)
elif metric == "ddtw":
return ddtw_cost_matrix(
x, y, kwargs.get("window"), kwargs.get("itakura_max_slope")
)
elif metric == "wdtw":
return wdtw_cost_matrix(
x,
y,
kwargs.get("window"),
kwargs.get("g", 0.05),
kwargs.get("itakura_max_slope"),
)
elif metric == "wddtw":
return wddtw_cost_matrix(
x,
y,
kwargs.get("window"),
kwargs.get("g", 0.05),
kwargs.get("itakura_max_slope"),
)
elif metric == "lcss":
return lcss_cost_matrix(
x,
y,
kwargs.get("window"),
kwargs.get("epsilon", 1.0),
kwargs.get("itakura_max_slope"),
)
elif metric == "erp":
return erp_cost_matrix(
x,
y,
kwargs.get("window"),
kwargs.get("g", 0.0),
kwargs.get("g_arr", None),
kwargs.get("itakura_max_slope"),
)
elif metric == "edr":
return edr_cost_matrix(
x,
y,
kwargs.get("window"),
kwargs.get("epsilon"),
kwargs.get("itakura_max_slope"),
)
elif metric == "twe":
return twe_cost_matrix(
x,
y,
kwargs.get("window"),
kwargs.get("nu", 0.001),
kwargs.get("lmbda", 1.0),
kwargs.get("itakura_max_slope"),
)
elif metric == "msm":
return msm_cost_matrix(
x,
y,
kwargs.get("window"),
kwargs.get("independent", True),
kwargs.get("c", 1.0),
kwargs.get("itakura_max_slope"),
)
elif metric == "adtw":
return adtw_cost_matrix(
x,
y,
kwargs.get("window"),
kwargs.get("itakura_max_slope"),
kwargs.get("warp_penalty", 1.0),
)
else:
raise ValueError("Metric must be one of the supported strings")
def get_distance_function_names() -> List[str]:
"""Get a list of distance function names in aeon.
All distance function names have two associated functions:
name_distance
name_pairwise_distance
Elastic distances have two additional functions associated with them:
name_alignment_path
name_cost_matrix
Returns
-------
List[str]
List of distance function names in aeon.
Examples
--------
>>> from aeon.distances import get_distance_function_names
>>> names = get_distance_function_names()
>>> names[0]
'adtw'
"""
return sorted(DISTANCES_DICT.keys())
def get_distance_function(metric: Union[str, DistanceFunction]) -> DistanceFunction:
"""Get the distance function for a given metric string or callable.
=============== ========================================
metric Distance Function
=============== ========================================
'dtw' distances.dtw_distance
'shape_dtw' distances.shape_dtw_distance
'ddtw' distances.ddtw_distance
'wdtw' distances.wdtw_distance
'wddtw' distances.wddtw_distance
'adtw' distances.adtw_distance
'erp' distances.erp_distance
'edr' distances.edr_distance
'msm' distances.msm_distance
'twe' distances.twe_distance
'lcss' distances.lcss_distance
'euclidean' distances.euclidean_distance
'squared' distances.squared_distance
'manhattan' distances.manhattan_distance
'minkowski' distances.minkowski_distance
'sbd' distances.sbd_distance
=============== ========================================
Parameters
----------
metric : str or Callable
The distance metric to use.
If string given then it will be resolved to a alignment path function.
If a callable is given, the value must be a function that accepts two
numpy arrays and **kwargs returns a float.
Returns
-------
Callable[[np.ndarray, np.ndarray, Any], float]
The distance function for the given metric.
Raises
------
ValueError
If metric is not one of the supported strings or a callable.
Examples
--------
>>> from aeon.distances import get_distance_function
>>> import numpy as np
>>> x = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
>>> y = np.array([[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])
>>> dtw_dist_func = get_distance_function("dtw")
>>> dtw_dist_func(x, y, window=0.2)
874.0
"""
return _resolve_key_from_distance(metric, "distance")
def get_pairwise_distance_function(
metric: Union[str, PairwiseFunction]
) -> PairwiseFunction:
"""Get the pairwise distance function for a given metric string or callable.
=============== ========================================
metric Distance Function
=============== ========================================
'dtw' distances.dtw_pairwise_distance
'shape_dtw' distances.shape_dtw_pairwise_distance
'ddtw' distances.ddtw_pairwise_distance
'wdtw' distances.wdtw_pairwise_distance
'wddtw' distances.wddtw_pairwise_distance
'adtw' distances.adtw_pairwise_distance
'erp' distances.erp_pairwise_distance
'edr' distances.edr_pairwise_distance
'msm' distances.msm_pairiwse_distance
'twe' distances.twe_pairwise_distance
'lcss' distances.lcss_pairwise_distance
'euclidean' distances.euclidean_pairwise_distance
'squared' distances.squared_pairwise_distance
'manhattan' distances.manhattan_pairwise_distance
'minkowski' distances.minkowski_pairwise_distance
'sbd' distances.sbd_pairwise_distance
=============== ========================================
Parameters
----------
metric : str or Callable
The metric string to resolve to a alignment path function.
If string given then it will be resolved to a alignment path function.
If a callable is given, the value must be a function that accepts two
numpy arrays and **kwargs returns a np.ndarray that is the pairwise distance
between each time series.
Returns
-------
Callable[[np.ndarray, np.ndarray, Any], np.ndarray]
The pairwise distance function for the given metric.
Raises
------
ValueError
If metric is not one of the supported strings or a callable.
Examples
--------
>>> from aeon.distances import get_pairwise_distance_function
>>> import numpy as np
>>> x = np.array([[[1, 2, 3]],[[4, 5, 6]], [[7, 8, 9]]])
>>> y = np.array([[[11, 12, 13]],[[14, 15, 16]], [[17, 18, 19]]])
>>> dtw_pairwise_dist_func = get_pairwise_distance_function("dtw")
>>> dtw_pairwise_dist_func(x, y, window=0.2)
array([[300., 507., 768.],
[147., 300., 507.],
[ 48., 147., 300.]])
"""
return _resolve_key_from_distance(metric, "pairwise_distance")
def get_alignment_path_function(metric: str) -> AlignmentPathFunction:
"""Get the alignment path function for a given metric string or callable.
=============== ========================================
metric Distance Function
=============== ========================================
'dtw' distances.dtw_alignment_path
'shape_dtw' distances.shape_dtw_alignment_path
'ddtw' distances.ddtw_alignment_path
'wdtw' distances.wdtw_alignment_path
'wddtw' distances.wddtw_alignment_path
'adtw' distances.adtw_alignment_path
'erp' distances.erp_alignment_path
'edr' distances.edr_alignment_path
'msm' distances.msm_alignment_path
'twe' distances.twe_alignment_path
'lcss' distances.lcss_alignment_path
=============== ========================================
Parameters
----------
metric : str or Callable
The metric string to resolve to a alignment path function.
Returns
-------
Callable[[np.ndarray, np.ndarray, Any], Tuple[List[Tuple[int, int]], float]]
The alignment path function for the given metric.
Raises
------
ValueError
If metric is not one of the supported strings or a callable.
If the metric doesn't have an alignment path function.
Examples
--------
>>> from aeon.distances import get_alignment_path_function
>>> import numpy as np
>>> x = np.array([[1, 2, 3, 4, 5]])
>>> y = np.array([[11, 12, 13, 14, 15]])
>>> dtw_alignment_path_func = get_alignment_path_function("dtw")
>>> dtw_alignment_path_func(x, y, window=0.2)
([(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)], 500.0)
"""
return _resolve_key_from_distance(metric, "alignment_path")
def get_cost_matrix_function(metric: str) -> CostMatrixFunction:
"""Get the cost matrix function for a given metric string or callable.
=============== ========================================
metric Distance Function
=============== ========================================
'dtw' distances.dtw_cost_matrix
'shape_dtw' distances.shape_dtw_cost_matrix
'ddtw' distances.ddtw_cost_matrix
'wdtw' distances.wdtw_cost_matrix
'wddtw' distances.wddtw_cost_matrix
'adtw' distances.adtw_cost_matrix
'erp' distances.erp_cost_matrix
'edr' distances.edr_cost_matrix
'msm' distances.msm_cost_matrix
'twe' distances.twe_cost_matrix
'lcss' distances.lcss_cost_matrix
=============== ========================================
Parameters
----------
metric : str or Callable
The metric string to resolve to a cost matrix function.
Returns
-------
Callable[[np.ndarray, np.ndarray, Any], np.ndarray]
The cost matrix function for the given metric.
Raises
------
ValueError
If metric is not one of the supported strings or a callable.
If the metric doesn't have a cost matrix function.
Examples
--------
>>> from aeon.distances import get_cost_matrix_function
>>> import numpy as np
>>> x = np.array([[1, 2, 3, 4, 5]])
>>> y = np.array([[11, 12, 13, 14, 15]])
>>> dtw_cost_matrix_func = get_cost_matrix_function("dtw")
>>> dtw_cost_matrix_func(x, y, window=0.2)
array([[100., 221., inf, inf, inf],
[181., 200., 321., inf, inf],
[ inf, 262., 300., 421., inf],
[ inf, inf, 343., 400., 521.],
[ inf, inf, inf, 424., 500.]])
"""
return _resolve_key_from_distance(metric, "cost_matrix")
def _resolve_key_from_distance(metric: Union[str, Callable], key: str) -> Any: