Mercurial > repos > sagun98 > micropita_v2
comparison src/breadcrumbs/src/MLPYDistanceAdaptor.py @ 0:0de566f21448 draft default tip
v2
author | sagun98 |
---|---|
date | Thu, 03 Jun 2021 18:13:32 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0de566f21448 |
---|---|
1 """ | |
2 Author: Timothy Tickle | |
3 Description: Allows KMedoids on a custom metric space. | |
4 """ | |
5 | |
6 ##################################################################################### | |
7 #Copyright (C) <2012> | |
8 # | |
9 #Permission is hereby granted, free of charge, to any person obtaining a copy of | |
10 #this software and associated documentation files (the "Software"), to deal in the | |
11 #Software without restriction, including without limitation the rights to use, copy, | |
12 #modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
13 #and to permit persons to whom the Software is furnished to do so, subject to | |
14 #the following conditions: | |
15 # | |
16 #The above copyright notice and this permission notice shall be included in all copies | |
17 #or substantial portions of the Software. | |
18 # | |
19 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
20 #INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
21 #PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
22 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
23 #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
24 #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
25 ##################################################################################### | |
26 | |
27 __author__ = "Timothy Tickle" | |
28 __copyright__ = "Copyright 2012" | |
29 __credits__ = ["Timothy Tickle"] | |
30 __license__ = "MIT" | |
31 __maintainer__ = "Timothy Tickle" | |
32 __email__ = "ttickle@sph.harvard.edu" | |
33 __status__ = "Development" | |
34 | |
35 #External libraries | |
36 from scipy.spatial.distance import squareform | |
37 | |
38 class MLPYDistanceAdaptor: | |
39 """ | |
40 Allows one to use custom distance metrics with KMedoids in the MLPY package. | |
41 """ | |
42 | |
43 npaMatrix = None | |
44 """ | |
45 Distance matrix to reference. | |
46 """ | |
47 | |
48 def __init__(self, npaDistanceMatrix, fIsCondensedMatrix): | |
49 """ | |
50 Constructor requires a matrix of distances, could be condensed or square matrices | |
51 | |
52 :param npaDistanceMatrix: The distance matrix to be used | |
53 :type Numpy array | |
54 :param fIsCondensedMatrix: Indicator of the matrix being square (true = condensed; false = square) | |
55 :type Boolean | |
56 """ | |
57 | |
58 if fIsCondensedMatrix: | |
59 self.npaMatrix = squareform(npaDistanceMatrix) | |
60 else: | |
61 self.npaMatrix = npaDistanceMatrix | |
62 | |
63 def compute(self,x,y): | |
64 """ | |
65 This is the only method required in the interface to MLPY to be a distance metric. | |
66 Does NOT want values but positions, the positions will be used for accessing the distance matrix already provided. | |
67 | |
68 :param x: X position as a array of 1 number | |
69 :type Numpy array | |
70 :param y: Y position as a array of 1 number | |
71 :type Boolean | |
72 """ | |
73 | |
74 if(self.npaMatrix == None): | |
75 raise Exception("".join(["MLPYDistanceAdaptor. Attempted to compute distance with out a distance matrix passed in during construction."])) | |
76 return self.npaMatrix[x[0],y[0]] |