def test_classification_tikhonov(self): """Solve a classification problem.""" G = graphs.Logo() signal = np.zeros([G.n_vertices], dtype=int) signal[G.info['idx_s']] = 1 signal[G.info['idx_p']] = 2 # Make the input signal. rs = np.random.RandomState(seed=1) mask = rs.uniform(size=G.n_vertices) > 0.3 measures = signal.copy() measures[~mask] = -1 measures_bak = measures.copy() # Solve the classification problem. recovery = learning.classification_tikhonov(G, measures, mask, tau=0) recovery = np.argmax(recovery, axis=1) np.testing.assert_array_equal(recovery, signal) # Test the function with the simplex projection. recovery = learning.classification_tikhonov_simplex(G, measures, mask, tau=0.1) # Assert that the probabilities sums to 1 np.testing.assert_allclose(np.sum(recovery, axis=1), 1) # Check the quality of the solution. recovery = np.argmax(recovery, axis=1) np.testing.assert_allclose(signal, recovery) np.testing.assert_allclose(measures_bak, measures)
def test_fourier_basis(self): # Smallest eigenvalue close to zero. np.testing.assert_allclose(self._G.e[0], 0, atol=1e-12) # First eigenvector is constant. N = self._G.N np.testing.assert_allclose(self._G.U[:, 0], np.sqrt(N) / N) # Control eigenvector direction. # assert (self._G.U[0, :] > 0).all() # Spectrum bounded by [0, 2] for the normalized Laplacian. G = graphs.Logo(lap_type='normalized') n = G.N // 2 # check partial eigendecomposition G.compute_fourier_basis(n_eigenvectors=n) assert len(G.e) == n assert G.U.shape[1] == n assert G.e[-1] < 2 U = G.U e = G.e # check full eigendecomposition G.compute_fourier_basis() assert len(G.e) == G.N assert G.U.shape[1] == G.N assert G.e[-1] < 2 # eigsh might flip a sign np.testing.assert_allclose(np.abs(U), np.abs(G.U[:, :n]), atol=1e-12) np.testing.assert_allclose(e, G.e[:n])
def test_difference(self): for lap_type in ['combinatorial', 'normalized']: G = graphs.Logo(lap_type=lap_type) y = G.grad(self._signal) self.assertEqual(len(y), G.n_edges) z = G.div(y) self.assertEqual(len(z), G.n_vertices) np.testing.assert_allclose(z, G.L.dot(self._signal))
def setUpClass(cls): cls._G = graphs.Logo() cls._G.compute_fourier_basis() cls._rs = np.random.RandomState(42) cls._signal = cls._rs.uniform(size=cls._G.N) cls._img = img_as_float(data.camera()[::16, ::16])
def setUpClass(cls): cls._G = graphs.Logo() cls._G.compute_fourier_basis() cls._G.compute_differential_operator() cls._rng = np.random.default_rng(42) cls._signal = cls._rng.uniform(size=cls._G.N) cls._img = img_as_float(data.camera()[::16, ::16])
def test_fourier_basis(self): # Smallest eigenvalue close to zero. np.testing.assert_allclose(self._G.e[0], 0, atol=1e-12) # First eigenvector is constant. N = self._G.N np.testing.assert_allclose(self._G.U[:, 0], np.sqrt(N) / N) # Control eigenvector direction. # assert (self._G.U[0, :] > 0).all() # Spectrum bounded by [0, 2] for the normalized Laplacian. G = graphs.Logo(lap_type='normalized') G.compute_fourier_basis() assert G.e[-1] < 2
def test_graphtool_signal_export(self): g = graphs.Logo() rs = np.random.RandomState(42) s = rs.normal(size=g.N) s2 = rs.normal(size=g.N) g.set_signal(s, "signal1") g.set_signal(s2, "signal2") g_gt = g.to_graphtool() # Check the signals on all nodes for i, v in enumerate(g_gt.vertices()): self.assertEqual(g_gt.vertex_properties["signal1"][v], s[i]) self.assertEqual(g_gt.vertex_properties["signal2"][v], s2[i]) # invalid signal type graph = graphs.Path(3) graph.set_signal(np.array(['a', 'b', 'c']), 'sig') self.assertRaises(TypeError, graph.to_graphtool)
def test_eigendecompositions(self): G = graphs.Logo() U1, e1, V1 = scipy.linalg.svd(G.L.toarray()) U2, e2, V2 = np.linalg.svd(G.L.toarray()) e3, U3 = np.linalg.eig(G.L.toarray()) e4, U4 = scipy.linalg.eig(G.L.toarray()) e5, U5 = np.linalg.eigh(G.L.toarray()) e6, U6 = scipy.linalg.eigh(G.L.toarray()) def correct_sign(U): signs = np.sign(U[0, :]) signs[signs == 0] = 1 return U * signs U1 = correct_sign(U1) U2 = correct_sign(U2) U3 = correct_sign(U3) U4 = correct_sign(U4) U5 = correct_sign(U5) U6 = correct_sign(U6) V1 = correct_sign(V1.T) V2 = correct_sign(V2.T) inds3 = np.argsort(e3)[::-1] inds4 = np.argsort(e4)[::-1] np.testing.assert_allclose(e2, e1) np.testing.assert_allclose(e3[inds3], e1, atol=1e-12) np.testing.assert_allclose(e4[inds4], e1, atol=1e-12) np.testing.assert_allclose(e5[::-1], e1, atol=1e-12) np.testing.assert_allclose(e6[::-1], e1, atol=1e-12) np.testing.assert_allclose(U2, U1, atol=1e-12) np.testing.assert_allclose(V1, U1, atol=1e-12) np.testing.assert_allclose(V2, U1, atol=1e-12) np.testing.assert_allclose(U3[:, inds3], U1, atol=1e-10) np.testing.assert_allclose(U4[:, inds4], U1, atol=1e-10) np.testing.assert_allclose(U5[:, ::-1], U1, atol=1e-10) np.testing.assert_allclose(U6[:, ::-1], U1, atol=1e-10)
def setUpClass(cls): cls._G = graphs.Logo() cls._G.compute_fourier_basis() cls._rs = np.random.RandomState(42) cls._signal = cls._rs.uniform(size=cls._G.N)
def test_save(self): G = graphs.Logo() name = 'test_plot' G.plot(backend='matplotlib', save_as=name) os.remove(name + '.png') os.remove(name + '.pdf')
def test_logo(self): graphs.Logo()
def test_difference(self): for lap_type in ['combinatorial', 'normalized']: G = graphs.Logo(lap_type=lap_type) s_grad = G.grad(self._signal) Ls = G.div(s_grad) np.testing.assert_allclose(Ls, G.L.dot(self._signal))
def test_Logo(): G = graphs.Logo()
def test_filters(self): G = graphs.Logo() G.estimate_lmax() def fu(x): x / (1. + x) def test_default_filters(G, fu): g = filters.Filter(G) g1 = filters.Filter(G, filters=fu) def test_abspline(G): g = filters.Abspline(G, Nf=4) def test_expwin(G): g = filters.Expwin(G) def test_gabor(G, fu): g = filters.Gabor(G, fu) def test_halfcosine(G): g = filters.Halfcosine(G, Nf=4) def test_heat(G): g = filters.Heat(G) def test_held(G): g = filters.Held(G) g1 = filters.Held(G, a=0.25) def test_itersine(G): g = filters.itersine(G, Nf=4) def test_mexicanhat(G): g = filters.Mexicanhat(G, Nf=5) g1 = filters.Mexicanhat(G, Nf=4) def test_meyer(G): g = filters.Meyer(G, Nf=4) def test_papadakis(G): g = filters.Papadakis(G) g1 = filters.Papadakis(G, a=0.25) def test_regular(G): g = filters.Regular(G) g1 = filters.Regular(G, d=5) g2 = filters.Regular(G, d=0) def test_simoncelli(G): g = filters.Simoncelli(G) g1 = filters.Simoncelli(G, a=0.25) def test_simpletf(G): g = filters.Simpletf(G, Nf=4) # Warped translates are not implemented yet def test_warpedtranslates(G): pass # gw = filters.warpedtranslates(G, g)) """ Test of the different methods implemented for the filter analysis Checks if the 'exact', 'cheby' or 'lanczos' produce the same output of a Heat kernel on the Logo graph """ def test_analysis(G): # Using Kronecker signal at the node 8.3 S = zeros(G.N) vertex_delta = 83 S[vertex_delta] = 1 g = filters.Heat(G) c_exact = g.analysis(G, S, method='exact') c_cheby = g.analysis(G, S, method='cheby') # c_lancz = g.analysis(G, S, method='lanczos') self.assertAlmostEqual(c_exact, c_cheby)
from pygsp import graphs, filters G = graphs.Logo() G.estimate_lmax() g = filters.Heat(G, tau=100) import numpy as np DELTAS = [20, 30, 1090] s = np.zeros(G.N) s[DELTAS] = 1 s = g.filter(s) G.plot_signal(s, highlight=DELTAS, backend='matplotlib')
def test_Logo(): G = graphs.Logo() needed_attributes_testing(G)