def display_solution(name, caption, R, S): D = euclidean_distances(S) sec = pub.section(name) sec.text('info', caption) with sec.plot('S') as pl: pl.plot(S[0, :], S[1, :], 'k.') if S.shape[0] == 3: import mpl_toolkits.mplot3d.axes3d as p3 for angle in [0, 45, 90]: with sec.plot('S3d%s' % angle) as pl: fig = pl.gcf() ax = p3.Axes3D(fig) ax.view_init(30, angle) col = S[2, :] ax.scatter(S[0, :], S[1, :], S[2, :], c=col) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') fig.add_axes(ax) if False: # TODO: use cheaper solution for point cloud as in # calib with sec.plot('D_vs_R') as pl: pl.plot(D.flat, R.flat, 'k.') pl.xlabel('D') pl.ylabel('R')
def euclidean_distances_test(): n = 5 P = np.random.rand(3, n) D = euclidean_distances(P) assert D.shape == (n, n) for i, j in itertools.product(range(n), range(n)): d = np.linalg.norm(P[:, i] - P[:, j]) assert_allclose(d, D[i, j])
def mds_test(): for n in [10, 100]: for k in [3, 4, 5]: P = np.random.rand(k, n) D = euclidean_distances(P) P2 = mds(D, ndim=k) error = evaluate_error(P, P2) assert_allclose(0, error, atol=1e-7)
def rank_test(): ''' Check that the double-centered matrix has small rank. ''' for n in range(5, 50, 5): for k in range(1, 5): P = np.random.rand(k, n) D = euclidean_distances(P) B = double_center(D * D) w, v = eigh(B) #@UnusedVariable w = w[::-1] # descending # normalize wn = w / w[0] small = np.abs(wn[k]) assert_allclose(0, small, atol=1e-7)
def mds_fast_test(): for n in [10, 100]: for k in [2, 3]: P = np.random.rand(k, n) D = euclidean_distances(P) for algo in [mds, mds_randomized]: # t0 = time.clock() P2 = algo(D, ndim=k) # t1 = time.clock() #t_mds = t1 - t0 # D2 = euclidean_distances(P2) error = evaluate_error(P, P2) assert_allclose(0, error, atol=1e-7)
def evaluate_error(P1, P2): D1 = euclidean_distances(P1) D2 = euclidean_distances(P2) return np.abs(D1 - D2).mean()