def transform_rct2str(source, target_train, target_test): covs_source = source['covs'] covs_target_train = target_train['covs'] covs_target_test = target_test['covs'] source_pow = {} source_pow['covs'] = source['covs'] source_pow['labels'] = source['labels'] n = covs_source.shape[1] disp_source = np.sum( [distance_riemann(covi, np.eye(n))**2 for covi in covs_source]) / len(covs_source) disp_target = np.sum( [distance_riemann(covi, np.eye(n))**2 for covi in covs_target_train]) / len(covs_target_train) p = np.sqrt(disp_target / disp_source) target_pow_train = {} target_pow_train['covs'] = np.stack( [powm(covi, 1.0 / p) for covi in covs_target_train]) target_pow_train['labels'] = target_train['labels'] target_pow_test = {} target_pow_test['covs'] = np.stack( [powm(covi, 1.0 / p) for covi in covs_target_test]) target_pow_test['labels'] = target_test['labels'] return source_pow, target_pow_train, target_pow_test
def dim_reduction_nrmesup(X, P, labels, params): K = X.shape[0] nc = X.shape[1] Sw = np.zeros((nc, nc)) Sb = np.zeros((nc, nc)) for i in range(K): ci = labels[i] for j in range(K): Ci, Cj = X[i, :, :], X[j, :, :] Sij = np.dot(invsqrtm(Ci), np.dot(Cj, invsqrtm(Ci))) if (i != j) & (labels[j] == ci): Sw = Sw + powm(logm(Sij), 2) if (i != j) & (labels[j] != ci): Sb = Sb + powm(logm(Sij), 2) M = np.dot(np.linalg.inv(Sw), Sb) g, U = np.linalg.eig(M) idx = g.argsort()[::-1] g = g[idx] U = U[:, idx] B, p = sp.linalg.polar(U) W = B[:, :P] return W
def power_means(C, p): phi = 0.375/np.abs(p) K = len(C) n = C[0].shape[0] w = np.ones(K) w = w/(1.0*len(w)) G = np.sum([wk*powm(Ck, p) for (wk,Ck) in zip(w,C)], axis=0) if p > 0: X = invsqrtm(G) else: X = sqrtm(G) zeta = 10e-10 test = 10*zeta while test > zeta: H = np.sum([wk*powm(np.dot(X, np.dot(powm(Ck, np.sign(p)), X.T)), np.abs(p)) for (wk,Ck) in zip(w,C)], axis=0) X = np.dot(powm(H, -phi), X) test = 1.0/np.sqrt(n) * np.linalg.norm(H - np.eye(n)) if p > 0: P = np.dot(np.linalg.inv(X), np.linalg.inv(X.T)) else: P = np.dot(X.T, X) return P
def dim_reduction_nrmelandmark(X, P, labels, params): K = X.shape[0] nc = X.shape[1] S = np.zeros((nc, nc)) M = mean_riemann(X) for i in range(K): Ci = X[i, :, :] Sij = np.dot(invsqrtm(Ci), np.dot(M, invsqrtm(Ci))) S = S + powm(logm(Sij), 2) l, v = np.linalg.eig(S) idx = l.argsort()[::-1] l = l[idx] v = v[:, idx] W = v[:, :P] return W
def dim_reduction_nrmeuns(X, P, labels, params): K = X.shape[0] nc = X.shape[1] S = np.zeros((nc, nc)) for i in range(K): for j in range(K): if i != j: Ci, Cj = X[i, :, :], X[j, :, :] Sij = np.dot(invsqrtm(Ci), np.dot(Cj, invsqrtm(Ci))) S = S + powm(logm(Sij), 2) l, v = np.linalg.eig(S) idx = l.argsort()[::-1] l = l[idx] v = v[:, idx] W = v[:, :P] return W
def test_powm(): """Test matrix power""" C = 2 * np.eye(3) Ctrue = (2**0.5) * np.eye(3) assert_array_almost_equal(powm(C, 0.5), Ctrue)
def test_powm(): """Test matrix power""" C = 2*np.eye(3) Ctrue = (2**0.5)*np.eye(3) assert_array_almost_equal(powm(C,0.5),Ctrue)