def test_sensor(self): graphs.Sensor(3000) graphs.Sensor(N=100, distributed=True) self.assertRaises(ValueError, graphs.Sensor, N=101, distributed=True) graphs.Sensor(N=101, distributed=False) graphs.Sensor(seed=10) graphs.Sensor(k=20)
def test_gabor(self): f = filters.Rectangular(self._G, None, 0.1) f = filters.Gabor(self._G, f) self._test_methods(f, tight=False, check=False) self.assertRaises(ValueError, filters.Gabor, graphs.Sensor(), f) f = filters.Regular(self._G) self.assertRaises(ValueError, filters.Gabor, self._G, f)
def test_save_load(self): # TODO: test with multiple graphs and signals # * dtypes (float, int, bool) of adjacency and signals # * empty graph / isolated nodes G1 = graphs.Sensor(seed=42) W = G1.W.toarray() sig = np.random.RandomState(42).normal(size=G1.N) G1.set_signal(sig, 's') for fmt in ['graphml', 'gml', 'gexf']: for backend in ['networkx', 'graph-tool']: if fmt == 'gexf' and backend == 'graph-tool': self.assertRaises(ValueError, G1.save, 'g', fmt, backend) self.assertRaises(ValueError, graphs.Graph.load, 'g', fmt, backend) os.remove('g') continue atol = 1e-5 if fmt == 'gml' and backend == 'graph-tool' else 0 for filename, fmt in [('graph.' + fmt, None), ('graph', fmt)]: G1.save(filename, fmt, backend) G2 = graphs.Graph.load(filename, fmt, backend) np.testing.assert_allclose(G2.W.toarray(), W, atol=atol) np.testing.assert_allclose(G2.signals['s'], sig, atol=atol) os.remove(filename) self.assertRaises(ValueError, graphs.Graph.load, 'g.gml', fmt='?') self.assertRaises(ValueError, graphs.Graph.load, 'g.gml', backend='?') self.assertRaises(ValueError, G1.save, 'g.gml', fmt='?') self.assertRaises(ValueError, G1.save, 'g.gml', backend='?')
def test_import_errors(self): from unittest.mock import patch graph = graphs.Sensor() filename = 'graph.gml' with patch.dict(sys.modules, {'networkx': None}): self.assertRaises(ImportError, graph.to_networkx) self.assertRaises(ImportError, graphs.Graph.from_networkx, None) self.assertRaises(ImportError, graph.save, filename, backend='networkx') self.assertRaises(ImportError, graphs.Graph.load, filename, backend='networkx') graph.save(filename) graphs.Graph.load(filename) with patch.dict(sys.modules, {'graph_tool': None}): self.assertRaises(ImportError, graph.to_graphtool) self.assertRaises(ImportError, graphs.Graph.from_graphtool, None) self.assertRaises(ImportError, graph.save, filename, backend='graph-tool') self.assertRaises(ImportError, graphs.Graph.load, filename, backend='graph-tool') graph.save(filename) graphs.Graph.load(filename) with patch.dict(sys.modules, {'networkx': None, 'graph_tool': None}): self.assertRaises(ImportError, graph.save, filename) self.assertRaises(ImportError, graphs.Graph.load, filename) os.remove(filename)
def test_signals(self): """Test the different kind of signals that can be plotted.""" G = graphs.Sensor() G.plot() rng = np.random.default_rng(42) def test_color(param, length): for value in ['r', 4*(.5,), length*(2,), np.ones([1, length]), rng.random(length), np.ones([length, 3]), ["red"] * length, rng.random([length, 4])]: params = {param: value} G.plot(**params) for value in [10, (0.5, 0.5), np.ones([length, 2]), np.ones([2, length, 3]), np.ones([length, 3]) * 1.1]: params = {param: value} self.assertRaises(ValueError, G.plot, **params) for value in ['r', 4*(.5)]: params = {param: value, 'backend': 'pyqtgraph'} self.assertRaises(ValueError, G.plot, **params) test_color('vertex_color', G.n_vertices) test_color('edge_color', G.n_edges) def test_size(param, length): for value in [15, length*(2,), np.ones([1, length]), rng.random(length)]: params = {param: value} G.plot(**params) for value in [(2, 3, 4, 5), np.ones([2, length]), np.ones([2, length, 3])]: params = {param: value} self.assertRaises(ValueError, G.plot, **params) test_size('vertex_size', G.n_vertices) test_size('edge_width', G.n_edges)
def test_plot_graphs(self): r""" Plot all graphs which have coordinates. With and without signal. With both backends. """ # Graphs who are not embedded, i.e., have no coordinates. COORDS_NO = { 'Graph', 'BarabasiAlbert', 'ErdosRenyi', 'FullConnected', 'RandomRegular', 'StochasticBlockModel', } # Coordinates are not in 2D or 3D. COORDS_WRONG_DIM = {'ImgPatches'} Gs = [] for classname in set(graphs.__all__) - COORDS_NO - COORDS_WRONG_DIM: Graph = getattr(graphs, classname) # Classes who require parameters. if classname == 'NNGraph': Xin = np.arange(90).reshape(30, 3) Gs.append(Graph(Xin)) elif classname in ['ImgPatches', 'Grid2dImgPatches']: Gs.append(Graph(img=self._img, patch_shape=(3, 3))) elif classname == 'LineGraph': Gs.append(Graph(graphs.Sensor(20, seed=42))) else: Gs.append(Graph()) # Add more test cases. if classname == 'TwoMoons': Gs.append(Graph(moontype='standard')) Gs.append(Graph(moontype='synthesized')) elif classname == 'Cube': Gs.append(Graph(nb_dim=2)) Gs.append(Graph(nb_dim=3)) elif classname == 'DavidSensorNet': Gs.append(Graph(N=64)) Gs.append(Graph(N=500)) Gs.append(Graph(N=128)) for G in Gs: self.assertTrue(hasattr(G, 'coords')) self.assertEqual(G.N, G.coords.shape[0]) signal = np.arange(G.N) + 0.3 G.plot(backend='pyqtgraph') G.plot(backend='matplotlib') G.plot(signal, backend='pyqtgraph') G.plot(signal, backend='matplotlib') plotting.close_all()
def test_eigenvalues(self): """Plot with and without showing the eigenvalues.""" graph = graphs.Sensor(20, seed=42) graph.estimate_lmax() filters.Heat(graph).plot() filters.Heat(graph).plot(eigenvalues=False) graph.compute_fourier_basis() filters.Heat(graph).plot() filters.Heat(graph).plot(eigenvalues=True) filters.Heat(graph).plot(eigenvalues=False)
def test_modulation(self): f = filters.Rectangular(self._G, None, 0.1) # TODO: synthesis doesn't work yet. # f = filters.Modulation(self._G, f, modulation_first=False) # self._test_methods(f, tight=False, check=False) f = filters.Modulation(self._G, f, modulation_first=True) self._test_methods(f, tight=False, check=False) self.assertRaises(ValueError, filters.Modulation, graphs.Sensor(), f) f = filters.Regular(self._G) self.assertRaises(ValueError, filters.Modulation, self._G, f)
def test_coords(self): G = graphs.Sensor() del G.coords self.assertRaises(AttributeError, G.plot) G.coords = None self.assertRaises(AttributeError, G.plot) G.coords = np.ones((G.N, 4)) self.assertRaises(AttributeError, G.plot) G.coords = np.ones((G.N, 3, 1)) self.assertRaises(AttributeError, G.plot) G.coords = np.ones((G.N//2, 3)) self.assertRaises(AttributeError, G.plot)
def test_sensor(self): graphs.Sensor(regular=True) graphs.Sensor(regular=False) graphs.Sensor(distributed=True) graphs.Sensor(distributed=False) graphs.Sensor(connected=True) graphs.Sensor(connected=False)
def test_break_join_signals(self): """Multi-dim signals are broken on export and joined on import.""" graph1 = graphs.Sensor(20, seed=42) graph1.set_signal(graph1.coords, 'coords') # networkx graph2 = graph1.to_networkx() graph2 = graphs.Graph.from_networkx(graph2) np.testing.assert_allclose(graph2.signals['coords'], graph1.coords) # graph-tool graph2 = graph1.to_graphtool() graph2 = graphs.Graph.from_graphtool(graph2) np.testing.assert_allclose(graph2.signals['coords'], graph1.coords) # save and load (need ordered dicts) filename = 'graph.graphml' graph1.save(filename) graph2 = graphs.Graph.load(filename) np.testing.assert_allclose(graph2.signals['coords'], graph1.coords) os.remove(filename)
def test_regression_tikhonov_2(self): """Solve a regression problem with a constraint.""" G = graphs.Sensor(100) G.estimate_lmax() # Create a smooth signal. filt = filters.Filter(G, lambda x: 1 / (1 + 10 * x)) rs = np.random.RandomState(1) signal = filt.analyze(rs.normal(size=(G.n_vertices, 5))) # Make the input signal. mask = rs.uniform(0, 1, [G.n_vertices]) > 0.5 measures = signal.copy() measures[~mask] = np.nan measures_bak = measures.copy() # Solve the problem. recovery0 = learning.regression_tikhonov(G, measures, mask, tau=0) np.testing.assert_allclose(measures_bak, measures) recovery1 = np.zeros_like(recovery0) for i in range(recovery0.shape[1]): recovery1[:, i] = learning.regression_tikhonov(G, measures[:, i], mask, tau=0) np.testing.assert_allclose(measures_bak, measures) G = graphs.Graph(G.W.toarray()) recovery2 = learning.regression_tikhonov(G, measures, mask, tau=0) recovery3 = np.zeros_like(recovery0) for i in range(recovery0.shape[1]): recovery3[:, i] = learning.regression_tikhonov(G, measures[:, i], mask, tau=0) np.testing.assert_allclose(recovery1, recovery0) np.testing.assert_allclose(recovery2, recovery0) np.testing.assert_allclose(recovery3, recovery0) np.testing.assert_allclose(measures_bak, measures)
def test_regression_tikhonov_3(self, tau=3.5): """Solve a relaxed regression problem.""" G = graphs.Sensor(100) G.estimate_lmax() # Create a smooth signal. filt = filters.Filter(G, lambda x: 1 / (1 + 10 * x)) rs = np.random.RandomState(1) signal = filt.analyze(rs.normal(size=(G.n_vertices, 6))) # Make the input signal. mask = rs.uniform(0, 1, G.n_vertices) > 0.5 measures = signal.copy() measures[~mask] = 18 measures_bak = measures.copy() L = G.L.toarray() recovery = np.matmul(np.linalg.inv(np.diag(1 * mask) + tau * L), (mask * measures.T).T) # Solve the problem. recovery0 = learning.regression_tikhonov(G, measures, mask, tau=tau) np.testing.assert_allclose(measures_bak, measures) recovery1 = np.zeros_like(recovery0) for i in range(recovery0.shape[1]): recovery1[:, i] = learning.regression_tikhonov( G, measures[:, i], mask, tau) np.testing.assert_allclose(measures_bak, measures) G = graphs.Graph(G.W.toarray()) recovery2 = learning.regression_tikhonov(G, measures, mask, tau) recovery3 = np.zeros_like(recovery0) for i in range(recovery0.shape[1]): recovery3[:, i] = learning.regression_tikhonov( G, measures[:, i], mask, tau) np.testing.assert_allclose(recovery0, recovery, atol=1e-5) np.testing.assert_allclose(recovery1, recovery, atol=1e-5) np.testing.assert_allclose(recovery2, recovery, atol=1e-5) np.testing.assert_allclose(recovery3, recovery, atol=1e-5) np.testing.assert_allclose(measures_bak, measures)
def test_estimate_lmax(self): graph = graphs.Sensor() self.assertRaises(ValueError, graph.estimate_lmax, method='unk') def check_lmax(graph, lmax): graph.estimate_lmax(method='bounds') np.testing.assert_allclose(graph.lmax, lmax) graph.estimate_lmax(method='lanczos') np.testing.assert_allclose(graph.lmax, lmax * 1.01) graph.compute_fourier_basis() np.testing.assert_allclose(graph.lmax, lmax) # Full graph (bound is tight). n_nodes, value = 10, 2 adjacency = np.full((n_nodes, n_nodes), value) graph = graphs.Graph(adjacency, lap_type='combinatorial') check_lmax(graph, lmax=value * n_nodes) # Regular bipartite graph (bound is tight). adjacency = [ [0, 0, 1, 1], [0, 0, 1, 1], [1, 1, 0, 0], [1, 1, 0, 0], ] graph = graphs.Graph(adjacency, lap_type='combinatorial') check_lmax(graph, lmax=4) # Bipartite graph (bound is tight). adjacency = [ [0, 0, 1, 1], [0, 0, 1, 0], [1, 1, 0, 0], [1, 0, 0, 0], ] graph = graphs.Graph(adjacency, lap_type='normalized') check_lmax(graph, lmax=2)
def setUpClass(cls): cls._G = graphs.Sensor(123, seed=42) cls._G.compute_fourier_basis() cls._rng = np.random.default_rng(42) cls._signal = cls._rng.uniform(size=cls._G.N)
def test_Sensor(): G = graphs.Sensor()
import numpy as np from pygsp import graphs, plotting, learning import matplotlib.pyplot as plt from matplotlib import gridspec G = graphs.Sensor(N=256,distributed=True,seed=42) G.compute_fourier_basis() label_signal = np.copysign(np.ones(G.N),G.U[:,9]) #label_signal = np.random.randn(G.N) #fig,axes=plt.subplot2grid(2,3,figsize=(12,4)) #gs = gridspec.GridSpec(2,) ax1 = plt.subplot2grid((3,3),(0,0),colspan=1) ax2 = plt.subplot2grid((3,3),(0,1),colspan=1) ax3 = plt.subplot2grid((3,3),(0,2),colspan=1) ax4 = plt.subplot2grid((3,3),(1,0),colspan=3) ax5 = plt.subplot2grid((3,3),(2,0),colspan=3) G.plot(label_signal,ax=ax1) ax1.set_title('Original') rs = np.random.RandomState(42) M = rs.rand(G.N) M = (M > 0.6).astype(float) sigma = 0.1 sub_samp = M * (label_signal + sigma* rs.standard_normal(G.N)) G.plot(sub_samp,ax=ax2) ax2.set_title('gemessene Signale') import pyunlocbox from SCA.stela import stela_lasso,soft_thresholding gamma = 3
from utils import * from pygsp import graphs, plotting, filters import pyunlocbox import networkx as nx from gl_algorithms import * from sklearn.datasets import make_moons, make_circles, make_classification, make_blobs from sklearn.preprocessing import StandardScaler path = 'C:/Kaige_Research/Graph Learning/graph_learning_code/results/' timeRun = datetime.datetime.now().strftime('_%m_%d_%H_%M_%S') n_samples = 256 noise = 0.1 iteration = 100 G = graphs.Sensor(N=n_samples, distributed=True, seed=42) G.compute_fourier_basis() label_signal = np.copysign(np.ones(G.N), G.U[:, 3]) #G.plot_signal(label_signal) rs = np.random.RandomState(42) M = rs.rand(G.N) M = (M > 0.1).astype(float) sigma = 0.1 subsampled_noisy_label_signal = M * (label_signal + sigma * rs.standard_normal(G.N)) #subsampled_noisy_label_signal=label_signal+np.random.normal(size=(n_samples)) #G.plot_signal(subsampled_noisy_label_signal) x, y = G.coords, label_signal y = subsampled_noisy_label_signal #x,y=make_blobs(n_samples=n_samples, n_features=2, centers=4, cluster_std=0.1, center_box=(0,5),shuffle=False, random_state=0)
G = graphs.SwissRoll(N=1000, seed=42) levels = 5 Gs = multiresolution(G, levels, sparsify=True) fig = plt.figure(figsize=(10, 2.5)) for i in range(4): ax = fig.add_subplot(1, 4, i + 1, projection='3d') plotting.plot_graph(Gs[i + 1], ax=ax) _ = ax.set_title( 'Pyramid Level: {} \n Number of nodes: {} \n Number of edges: {}'. format(i + 1, Gs[i + 1].N, Gs[i + 1].Ne)) ax.set_axis_off() fig.tight_layout() plt.show() G = graphs.Sensor(1200, distribute=True) levels = 5 Gs = multiresolution(G, levels, sparsify=True) fig = plt.figure(figsize=(10, 2.5)) for i in range(4): ax = fig.add_subplot(1, 4, i + 1) # , projection='3d' plotting.plot_graph(Gs[i + 1], ax=ax) _ = ax.set_title( 'Pyramid Level: {} \n Number of nodes: {} \n Number of edges: {}'. format(i + 1, Gs[i + 1].N, Gs[i + 1].Ne)) ax.set_axis_off() fig.tight_layout() plt.show()
def test_show_close(self): G = graphs.Sensor() G.plot() plotting.show(block=False) # Don't block or the test will halt. plotting.close() plotting.close_all()
def setUpClass(cls): cls._G = graphs.Sensor(123, seed=42) cls._G.compute_fourier_basis() cls._rs = np.random.RandomState(42) cls._signal = cls._rs.uniform(size=cls._G.N)
def test_unknown_backend(self): G = graphs.Sensor() self.assertRaises(ValueError, G.plot, backend='abc')
def setUpClass(cls): cls._graph = graphs.Sensor(20, seed=42) cls._graph.compute_fourier_basis()
def test_Sensor(): G = graphs.Sensor() needed_attributes_testing(G)