def _draw_subject_specific_and_estimate(cov_u, dim, vnum, outliers_num, b, show_corrs=False, repeat_one=0, label=None): covars = [] noise_covars = [] for i in range(0, vnum-outliers_num): # ---construct covariance of subject# LambdaMatrix = _draw_eigenvalues(dim=args.dim, b=args.b, distribution="unif", overlap=args.overlap_eigenvalues) rotMatrix = _draw_rotation_matrix(args.dim) noise_cov = rotMatrix.dot(LambdaMatrix.dot(rotMatrix.transpose())) cov = np.add(cov_u, noise_cov) noise_covars.append(noise_cov) covars.append(cov) for i in range(repeat_one): covars.append(cov) # --------------------------------------# print("snr is :" + str(snr(np.asarray(noise_covars), cov_u))) if(show_corrs): factor =0.03 + (not args.overlap_eigenvalues)*dim*0.03 f, axarr = plt.subplots(3, 3, figsize=(11, 5)) idxs = np.random.choice(range(len(covars)), size=(3, 3)) for i in range(idxs.shape[0]): for j in range(idxs.shape[1]): im = axarr[i, j].pcolormesh(np.flipud((connectivity_matrices.cov_to_corr(covars[idxs[i, j]])).T), cmap=plt.cm.gist_earth_r, edgecolors='k', vmin=-factor*b, vmax=factor*b, **{'linewidth': 0.1}) axarr[i, j].set_axis_off() axarr[i, j].set_aspect('equal', 'box') if(j == 2): divider = make_axes_locatable(axarr[i, j]) cax = divider.append_axes("right", size="5%", pad=0.05) cb = f.colorbar(im, cax=cax) tick_locations = (-factor*b, 0, factor*b) tick_labels = ('{:{width}.{prec}f}'.format(-factor*b, width=1, prec=1), '{:{width}.{prec}f}'.format(0, width=1, prec=1), '{:{width}.{prec}f}'.format(factor*b, width=1, prec=1)) cb.locator = ticker.FixedLocator(tick_locations) cb.formatter = ticker.FixedFormatter(tick_labels) cb.update_ticks() if label is None: plt.savefig('./subjects_covars.eps', format='eps', dpi=1000) else: plt.savefig('./subjects_covars_' + str(label) + '.eps', format='eps', dpi=1000) # construct outliers w, v = LA.eig(cov_u) for i in range(0, outliers_num): LambdaMatrix = _draw_eigenvalues(dim=args.dim, b=args.b, distribution="unif", overlap=args.overlap_eigenvalues) rotMatrix = _draw_rotation_matrix(dim=3) cov = rotMatrix.dot(LambdaMatrix.dot(rotMatrix.transpose())) covars.append(cov) our_est = est_common_cov(covars, outliers=args.outliers) eucl_mean = mean_euclid(np.array(covars)) riemann_mean = mean_covariance(np.array(covars), metric='riemann', sample_weight=None) return our_est, eucl_mean, riemann_mean
def test_distance_wrapper_random(met, gfunc, get_covmats): n_trials, n_channels = 2, 5 covmats = get_covmats(n_trials, n_channels) A, B = covmats[0], covmats[1] if gfunc is geodesic_euclid: Ctrue = mean_euclid(covmats) elif gfunc is geodesic_logeuclid: Ctrue = mean_logeuclid(covmats) elif gfunc is geodesic_riemann: Ctrue = mean_riemann(covmats) assert geodesic(A, B, 0.5, metric=met) == approx(Ctrue)
def test_random_mat(self, geodesic_func, get_covmats): n_trials, n_channels = 2, 5 covmats = get_covmats(n_trials, n_channels) A, B = covmats[0], covmats[1] if geodesic_func is geodesic_euclid: Ctrue = mean_euclid(covmats) elif geodesic_func is geodesic_logeuclid: Ctrue = mean_logeuclid(covmats) elif geodesic_func is geodesic_riemann: Ctrue = mean_riemann(covmats) self.geodesic_0(geodesic_func, A, B) self.geodesic_1(geodesic_func, A, B) self.geodesic_middle(geodesic_func, A, B, Ctrue)
def test_power_mean(get_covmats): """Test the power mean""" n_matrices, n_channels = 3, 3 covmats = get_covmats(n_matrices, n_channels) C_power_1 = mean_power(covmats, 1) C_power_0 = mean_power(covmats, 0) C_power_m1 = mean_power(covmats, -1) C_arithm = mean_euclid(covmats) C_geom = mean_riemann(covmats) C_harm = mean_harmonic(covmats) assert C_power_1 == approx(C_arithm) assert C_power_0 == approx(C_geom) assert C_power_m1 == approx(C_harm)
def test_euclid_mean(): """Test the euclidean mean""" covmats, diags, A = generate_cov(100, 3) C = mean_euclid(covmats) assert_array_almost_equal(C, covmats.mean(axis=0))
def test_mean_covariance_euclid(): """Test mean_covariance for euclidean metric""" covmats, diags, A = generate_cov(100, 3) C = mean_covariance(covmats, metric='euclid') Ctrue = mean_euclid(covmats) assert_array_equal(C, Ctrue)
def test_mean_covariance_euclid(): """Test mean_covariance for euclidean metric""" covmats = generate_cov(100, 3) C = mean_covariance(covmats, metric='euclid') Ctrue = mean_euclid(covmats) assert_array_equal(C, Ctrue)
def test_euclid_mean(): """Test the euclidean mean""" covmats = generate_cov(100, 3) C = mean_euclid(covmats) assert_array_almost_equal(C, covmats.mean(axis=0))
def test_euclid_mean(get_covmats): """Test the euclidean mean""" n_matrices, n_channels = 100, 3 covmats = get_covmats(n_matrices, n_channels) C = mean_euclid(covmats) assert C == approx(covmats.mean(axis=0))
C = np.dot(Q, np.dot(D, Q.T)) theta = np.pi / 2.0 R = np.array([[cos(theta), -sin(theta)], [sin(theta), cos(theta)]]) Coutlier = np.dot(R, np.dot(C, R.T)) covs = [C, Coutlier] for _ in range(5): theta = 1 * np.pi / 20 * np.random.randn() R = np.array([[cos(theta), -sin(theta)], [sin(theta), cos(theta)]]) Cr = np.dot(R, np.dot(C, R.T)) covs.append(Cr) for cov in covs: e = gen_ellipse(cov) ax.add_artist(e) covs = np.stack(covs) Crmn = mean_riemann(covs) Ceuc = mean_euclid(covs) e = gen_ellipse(Crmn, ec='blue', lw=2.0) ax.add_artist(e) e = gen_ellipse(Ceuc, ec='red', lw=2.0) ax.add_artist(e) ax.set_xlim(-1.2, +1.2) ax.set_ylim(-1.2, +1.2) plt.show()