def test_pgso(): Xs = next(generate_mcca_test_data()) N = Xs[0].shape[0] ranks = [] corrs = [] for tol in [0, 0.1, 0.5, 1]: kmcca = KMCCA(kernel='rbf', pgso=True, tol=tol, n_components=2) scores = kmcca.fit(Xs).transform(Xs) # kmcca2 = KMCCA(kernel='rbf', pgso=True, tol=tol) # scores2 = kmcca2.fit(Xs).transform(Xs) for v in range(len(Xs)): assert len( set([ kmcca.pgso_Ls_[v].shape[1], kmcca.pgso_norms_[v].shape[0], kmcca.pgso_idxs_[v].shape[0], kmcca.pgso_Xs_[v].shape[0] ])) == 1 R = kmcca._get_kernel(Xs[v], v) - \ kmcca.pgso_Ls_[v] @ kmcca.pgso_Ls_[v].T R = R - kmcca.kernel_col_means_[v] - \ kmcca.kernel_col_means_[v].T + kmcca.kernel_mat_means_[v] assert np.trace(R) / N <= tol ranks.append(kmcca.pgso_ranks_) corrs.append(kmcca.canon_corrs(scores)) if tol == 0: assert np.all(ranks[-1] == [N for _ in Xs]) assert np.all(np.diff(corrs, axis=0) <= 1e-10), corrs assert np.all(np.diff(corrs, axis=1) <= 1e-10), corrs assert np.all(np.diff(ranks, axis=0) <= 1e-10), ranks
def test_kmcca_n_components(): Xs = next(generate_mcca_test_data()) n_features = sum([X.shape[1] for X in Xs]) kmcca = KMCCA(n_components=n_features + 1) with pytest.raises(AttributeError): kmcca.n_components_ with pytest.warns(None): kmcca.fit(Xs) assert kmcca.n_components_ == n_features for load in kmcca.dual_vars_: assert kmcca.n_components_ == load.shape[1]
def test_kmcca_params(kernel_args, diag_mode, signal_ranks, n_components, regs, sval_thresh, center, multiview_output): kernel, kernel_params = kernel_args kmcca = KMCCA( kernel=kernel, kernel_params=kernel_params, diag_mode=diag_mode, signal_ranks=signal_ranks, n_components=n_components, regs=regs, sval_thresh=sval_thresh, center=center, multiview_output=multiview_output) Xs = next(generate_mcca_test_data()) scores = kmcca.fit_transform(Xs) if multiview_output: assert len(scores) == len(Xs) canon_corrs = kmcca.canon_corrs(scores) assert len(canon_corrs) == scores.shape[2] else: assert scores.shape == (Xs[0].shape[0], kmcca.n_components_)
def test_kmcca(diag_mode): for Xs in generate_mcca_test_data(): for params in generate_mcca_test_settings(): if len(Xs) == 2 and params['n_components'] is None: # this setting raises some issues where are few # of the view scores are not equal. I do not think # this is an issue in practice so lets just skip # this scenario continue # Linear KMCCA should match MCCA n_features = [x.shape[1] for x in Xs] kmcca = KMCCA(kernel='linear', sval_thresh=0, signal_ranks=n_features, diag_mode=diag_mode, **params).fit(Xs) mcca = MCCA(**params).fit(Xs) # evals np.testing.assert_array_almost_equal(kmcca.evals_, mcca.evals_, decimal=2) # MCCA and linear KMCCA output should be the same for ks, ms in zip(kmcca.transform(Xs), mcca.transform(Xs)): np.testing.assert_array_almost_equal(ks, ms, decimal=2) # Linear KMCCA should match MCCA n_features = [x.shape[1] for x in Xs] kmcca = KMCCA(kernel='linear', sval_thresh=0, signal_ranks=n_features, diag_mode=diag_mode, multiview_output=False, **params).fit(Xs) mcca = MCCA(multiview_output=False, **params).fit(Xs) # evals np.testing.assert_array_almost_equal(kmcca.evals_, mcca.evals_, decimal=2) # common_scores_normed np.testing.assert_array_almost_equal(kmcca.transform(Xs), mcca.transform(Xs), decimal=2)