From 98b4aa0026a7208230d396bc9fb28c7a3b72be6d Mon Sep 17 00:00:00 2001 From: tlacombe Date: Tue, 7 Jan 2020 18:06:13 +0100 Subject: changing variable name from (p, q) to (q, internal_p). Must also be done in tests files and doc --- src/python/gudhi/wasserstein.py | 46 ++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/python/gudhi/wasserstein.py b/src/python/gudhi/wasserstein.py index d8a3104c..2acf93d6 100644 --- a/src/python/gudhi/wasserstein.py +++ b/src/python/gudhi/wasserstein.py @@ -23,26 +23,26 @@ def _proj_on_diag(X): return np.array([Z , Z]).T -def _build_dist_matrix(X, Y, p=2., q=2.): +def _build_dist_matrix(X, Y, q=2., internal_p=2.): ''' :param X: (n x 2) numpy.array encoding the (points of the) first diagram. :param Y: (m x 2) numpy.array encoding the second diagram. - :param q: Ground metric (i.e. norm l_q). - :param p: exponent for the Wasserstein metric. + :param internal_p: Ground metric (i.e. norm l_q). + :param q: exponent for the Wasserstein metric. :returns: (n+1) x (m+1) np.array encoding the cost matrix C. For 1 <= i <= n, 1 <= j <= m, C[i,j] encodes the distance between X[i] and Y[j], while C[i, m+1] (resp. C[n+1, j]) encodes the distance (to the p) between X[i] (resp Y[j]) and its orthogonal proj onto the diagonal. note also that C[n+1, m+1] = 0 (it costs nothing to move from the diagonal to the diagonal). ''' Xdiag = _proj_on_diag(X) Ydiag = _proj_on_diag(Y) - if np.isinf(q): - C = sc.cdist(X,Y, metric='chebyshev')**p - Cxd = np.linalg.norm(X - Xdiag, ord=q, axis=1)**p - Cdy = np.linalg.norm(Y - Ydiag, ord=q, axis=1)**p + if np.isinf(internal_p): + C = sc.cdist(X,Y, metric='chebyshev')**q + Cxd = np.linalg.norm(X - Xdiag, ord=internal_p, axis=1)**q + Cdy = np.linalg.norm(Y - Ydiag, ord=internal_p, axis=1)**q else: - C = sc.cdist(X,Y, metric='minkowski', p=q)**p - Cxd = np.linalg.norm(X - Xdiag, ord=q, axis=1)**p - Cdy = np.linalg.norm(Y - Ydiag, ord=q, axis=1)**p + C = sc.cdist(X,Y, metric='minkowski', p=internal_p)**q + Cxd = np.linalg.norm(X - Xdiag, ord=internal_p, axis=1)**q + Cdy = np.linalg.norm(Y - Ydiag, ord=internal_p, axis=1)**q Cf = np.hstack((C, Cxd[:,None])) Cdy = np.append(Cdy, 0) @@ -51,24 +51,24 @@ def _build_dist_matrix(X, Y, p=2., q=2.): return Cf -def _perstot(X, p, q): +def _perstot(X, q, internal_p): ''' :param X: (n x 2) numpy.array (points of a given diagram). - :param q: Ground metric on the (upper-half) plane (i.e. norm l_q in R^2); Default value is 2 (Euclidean norm). - :param p: exponent for Wasserstein; Default value is 2. + :param internal_p: Ground metric on the (upper-half) plane (i.e. norm l_p in R^2); Default value is 2 (Euclidean norm). + :param q: exponent for Wasserstein; Default value is 2. :returns: float, the total persistence of the diagram (that is, its distance to the empty diagram). ''' Xdiag = _proj_on_diag(X) - return (np.sum(np.linalg.norm(X - Xdiag, ord=q, axis=1)**p))**(1./p) + return (np.sum(np.linalg.norm(X - Xdiag, ord=internal_p, axis=1)**q))**(1./q) -def wasserstein_distance(X, Y, p=2., q=2.): +def wasserstein_distance(X, Y, q=2., internal_p=2.): ''' :param X: (n x 2) numpy.array encoding the (finite points of the) first diagram. Must not contain essential points (i.e. with infinite coordinate). :param Y: (m x 2) numpy.array encoding the second diagram. - :param q: Ground metric on the (upper-half) plane (i.e. norm l_q in R^2); Default value is 2 (euclidean norm). - :param p: exponent for Wasserstein; Default value is 2. - :returns: the p-Wasserstein distance (1 <= p < infinity) with respect to the q-norm as ground metric. + :param internal_p: Ground metric on the (upper-half) plane (i.e. norm l_p in R^2); Default value is 2 (euclidean norm). + :param q: exponent for Wasserstein; Default value is 2. + :returns: the q-Wasserstein distance (1 <= q < infinity) with respect to the internal_p-norm as ground metric. :rtype: float ''' n = len(X) @@ -79,20 +79,20 @@ def wasserstein_distance(X, Y, p=2., q=2.): if Y.size == 0: return 0. else: - return _perstot(Y, p, q) + return _perstot(Y, q, internal_p) elif Y.size == 0: - return _perstot(X, p, q) + return _perstot(X, q, internal_p) - M = _build_dist_matrix(X, Y, p=p, q=q) + M = _build_dist_matrix(X, Y, q=q, internal_p=internal_p) a = np.full(n+1, 1. / (n + m) ) # weight vector of the input diagram. Uniform here. a[-1] = a[-1] * m # normalized so that we have a probability measure, required by POT b = np.full(m+1, 1. / (n + m) ) # weight vector of the input diagram. Uniform here. b[-1] = b[-1] * n # so that we have a probability measure, required by POT # Comptuation of the otcost using the ot.emd2 library. - # Note: it is the squared Wasserstein distance. + # Note: it is the Wasserstein distance to the power q. # The default numItermax=100000 is not sufficient for some examples with 5000 points, what is a good value? ot_cost = (n+m) * ot.emd2(a, b, M, numItermax=2000000) - return ot_cost ** (1./p) + return ot_cost ** (1./q) -- cgit v1.2.3