def _check_graph(x, a, e, y): g = Graph() # Empty graph g = Graph(x=x) # Only node features g = Graph(a=a) # Only adjacency g = Graph(x=x, a=a, e=e, y=y, extra=1) # Complete graph with extra attribute # numpy g_np = g.numpy() g_gt_names = ["x", "a", "e", "y"] g_gt = [x, a, e, y] for i in range(len(g_gt)): assert np.all(g_np[i] == g_gt[i]) # __getitem__ for i in range(len(g_gt)): assert np.all(g.__getitem__(g_gt_names[i]) == g_gt[i]) # __repr__ print(g) # Properties assert g.n_nodes == n_nodes assert g.n_node_features == n_node_features assert g.n_edge_features == n_edge_features assert g.n_labels == n_out assert g.n_edges == np.count_nonzero(a)
def read(self): return [ Graph(x=np.random.rand(n, f), a=sp.csr_matrix(np.random.randint(0, 2, (n, n))), e=np.random.rand(n, n, s), y=np.ones((n, 2))) for n in ns ]
def read(self): return [ Graph(x=np.random.rand(n, f), a=sp.csr_matrix(np.random.randint(0, 2, (n, n))), e=np.random.rand(n, n, s), y=np.array([0., 1.])) for n in ns ]
def read(self): return [ Graph(x=np.random.rand(n, f), a=np.random.randint(0, 2, (n, n)), e=np.random.rand(n, n, s), y=np.array([0., 1.])) for n in Ns ]
def read(self): n = 10 return [ Graph(x=np.random.rand(n, f), a=sp.csr_matrix(np.random.randint(0, 2, (n, n))), e=np.random.rand(n, n, s), y=np.array(n * [[0., 1.]])) ]
def read(self): n = np.random.randint(3, 8) self.a = sp.csr_matrix(np.random.randint(0, 2, (n, n))) return [ Graph( x=np.random.rand(n, f), e=np.random.rand(n, n, s), y=np.array([0.0, 1.0]), ) for _ in range(n_graphs) ]
def test_dataset(): class TestDataset(Dataset): def read(self): return [ Graph(x=np.random.rand(n, f), a=np.random.randint(0, 2, (n, n)), e=np.random.rand(n, n, s), y=np.array([0., 1.])) for n in Ns ] d = TestDataset() assert d.n_node_features == f assert d.n_edge_features == s assert d.n_labels == 2 # signature for k in ['x', 'a', 'e', 'y']: assert k in d.signature # __getitem__ assert isinstance(d[0], Graph) assert isinstance(d[:3], Dataset) assert isinstance(d[[1, 3, 4]], Dataset) # __setitem__ n = 100 g = Graph(x=np.random.rand(n, f), a=np.random.randint(0, 2, (n, n)), e=np.random.rand(n, n, s), y=np.array([0., 1.])) # single assignment d[0] = g assert d[0].n_nodes == n and all([d_.n_nodes != n for d_ in d[1:]]) # Slice assignment d[1:3] = [g] * 2 assert d[1].n_nodes == n and d[2].n_nodes == n and all( [d_.n_nodes != n for d_ in d[3:]]) # List assignment d[[3, 4]] = [g] * 2 assert d[3].n_nodes == n and d[4].n_nodes == n and all( [d_.n_nodes != n for d_ in d[5:]]) # __len__ assert d.__len__() == n_graphs # __repr__ print(d) # Test that shuffling doesn't crash np.random.shuffle(d)
def _check_graph(x, a, e, y): g = Graph() g = Graph(x=x) g = Graph(a=a) g = Graph(x=x, a=a, e=e, y=y) # numpy g_np = g.numpy() g_gt_names = ["x", "a", "e", "y"] g_gt = [x, a, e, y] for i in range(len(g_gt)): assert np.all(g_np[i] == g_gt[i]) # __getitem__ for i in range(len(g_gt)): assert np.all(g.__getitem__(g_gt_names[i]) == g_gt[i]) # __repr__ print(g)
def load_off(filename): """ Reads an .off file into a Graph object. Node attributes are the 3d coordinates of the points, all faces are converted to edges. :param filename: path to the .off file. :return: a Graph """ lines = open(filename, "r").read().lstrip("OF\n").splitlines() x, faces = _parse_off(lines) n = x.shape[0] row, col = np.vstack((faces[:, :2], faces[:, 1:], faces[:, ::2])).T adj = sp.csr_matrix((np.ones_like(row), (row, col)), shape=(n, n)).tolil() adj[col, row] = adj[row, col] adj = adj.T.tocsr() adj.data = np.clip(adj.data, 0, 1) return Graph(x=x, adj=adj)
def test_dataset(): class TestDataset(Dataset): def read(self): return [ Graph( x=np.random.rand(n, f), a=np.random.randint(0, 2, (n, n)), e=np.random.rand(n, n, s), y=np.array([0.0, 1.0]), ) for n in Ns ] d = TestDataset() assert d.n_node_features == f assert d.n_edge_features == s assert d.n_labels == 2 # signature for k in ["x", "a", "e", "y"]: assert k in d.signature # __getitem__ assert isinstance(d[0], Graph) assert isinstance(d[:3], Dataset) assert isinstance(d[[1, 3, 4]], Dataset) # __setitem__ n = 100 g = Graph( x=np.random.rand(n, f), a=np.random.randint(0, 2, (n, n)), e=np.random.rand(n, n, s), y=np.array([0.0, 1.0]), ) # single assignment d[0] = g assert d[0].n_nodes == n and all([d_.n_nodes != n for d_ in d[1:]]) # Slice assignment d[1:3] = [g] * 2 assert (d[1].n_nodes == n and d[2].n_nodes == n and all([d_.n_nodes != n for d_ in d[3:]])) # List assignment d[[3, 4]] = [g] * 2 assert (d[3].n_nodes == n and d[4].n_nodes == n and all([d_.n_nodes != n for d_ in d[5:]])) # __len__ assert d.__len__() == n_graphs # __repr__ print(d) # Test that shuffling doesn't crash np.random.shuffle(d) # Test apply() t = transforms.NormalizeSphere() d.apply(t) # Test filter d.filter(lambda g: g.n_nodes >= 100) # Test map t = lambda g: g.n_nodes d.map(t, reduce=max)