def test_weighted_graph_label(self): g = nx.Graph() edges = [(1, 2), (2, 3), (3, 4), (4, 1)] g.add_edges_from(edges) g[1][2]["w"] = 1 g[2][3]["w"] = 2 g[3][4]["w"] = 3 g[4][1]["w"] = 4 g = StellarGraph(g, edge_weight_label="w") nodes = list(g.nodes()) n = 1 length = 1 seed = None p = 1.0 q = 1.0 biasedrw = BiasedRandomWalk(g) assert (len( biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=True)) == 4) g = nx.Graph() edges = [(1, 2), (2, 3), (3, 4), (4, 1)] g.add_edges_from(edges) g[1][2]["wt"] = 1 g[2][3]["wt"] = 2 g[3][4]["wt"] = 3 g[4][1]["wt"] = 4 # Deliberately use wrong name for edge weight!? g = StellarGraph(g, edge_weight_label="w") nodes = list(g.nodes()) n = 1 length = 1 seed = None p = 1.0 q = 1.0 biasedrw = BiasedRandomWalk(g) with pytest.raises(ValueError): biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=True)
def test_identity_unweighted_weighted_1_walks(self): # graph with all edge weights = 1 g = nx.Graph() edges = [(1, 2, 1), (2, 3, 1), (3, 4, 1), (4, 1, 1)] g.add_weighted_edges_from(edges) g = StellarGraph(g) nodes = g.nodes() n = 4 length = 4 seed = 42 p = 1.0 q = 1.0 biasedrw = BiasedRandomWalk(g) assert biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=True) == biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=False)
def test_UnsupervisedSampler_parameter(self): g = create_test_graph() # rw = UniformRandomWalk(StellarGraph(g)) # if no graph is provided with pytest.raises(ValueError): UnsupervisedSampler(G=None) # graph has to be a Stellargraph object with pytest.raises(ValueError): UnsupervisedSampler(G=g) g = StellarGraph(g) """ # only Uniform random walk is supported at the moment with pytest.raises(TypeError): UnsupervisedSampler(G=g, walker="any random walker") # if no walker is provided, default to Uniform Random Walk sampler = UnsupervisedSampler(G=g) assert isinstance(sampler.walker, UniformRandomWalk) """ # walk must have length strictly greater than 1 with pytest.raises(ValueError): UnsupervisedSampler(G=g, length=1) # at least 1 walk from each root node with pytest.raises(ValueError): UnsupervisedSampler(G=g, number_of_walks=0) # nodes nodes parameter should be an iterable of node IDs with pytest.raises(ValueError): UnsupervisedSampler(G=g, nodes=1) # if no root nodes are provided for sampling defaulting to using all nodes as root nodes sampler = UnsupervisedSampler(G=g, nodes=None) assert sampler.nodes == list(g.nodes()) # if the seed value is provided check # that the random choices is reproducable sampler = UnsupervisedSampler(G=g, seed=1) assert sampler.random.choices(range(100), k=10) == [ 13, 84, 76, 25, 49, 44, 65, 78, 9, 2, ]
def test_weighted_walks(self): # all positive walks g = nx.Graph() edges = [(1, 2, 1), (2, 3, 2), (3, 4, 3), (4, 1, 4)] g.add_weighted_edges_from(edges) g = StellarGraph(g) nodes = list(g.nodes()) n = 1 length = 1 seed = None p = 1.0 q = 1.0 biasedrw = BiasedRandomWalk(g) assert (len( biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=True)) == 4) # negative edge g = nx.Graph() edges = [(1, 2, 1), (2, 3, -2), (3, 4, 3), (4, 1, 4)] g.add_weighted_edges_from(edges) g = StellarGraph(g) biasedrw = BiasedRandomWalk(g) with pytest.raises(ValueError): biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=True) # edge with weight infinity g = nx.Graph() edges = [(1, 2, 1), (2, 3, np.inf), (3, 4, 3), (4, 1, 4)] g.add_weighted_edges_from(edges) g = StellarGraph(g) biasedrw = BiasedRandomWalk(g) with pytest.raises(ValueError): biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=True) # missing edges g = nx.Graph() edges = [(1, 2, 1), (2, 3, None), (3, 4, 3), (4, 1, 4)] g.add_weighted_edges_from(edges) g = StellarGraph(g) biasedrw = BiasedRandomWalk(g) with pytest.raises(ValueError): biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=True) # edges with NaN g = nx.Graph() edges = [(1, 2, 1), (2, 3, np.NaN), (3, 4, 3), (4, 1, 4)] g.add_weighted_edges_from(edges) g = StellarGraph(g) biasedrw = BiasedRandomWalk(g) with pytest.raises(ValueError): biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=True)