MCPcopy
hub / github.com/dmlc/dgl / process

Method process

python/dgl/data/synthetic.py:641–701  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

639 )
640
641 def process(self):
642 if self.seed is not None:
643 np.random.seed(self.seed)
644
645 g = nx.balanced_tree(r=2, h=self.tree_height)
646 edges = list(g.edges())
647 src, dst = map(list, zip(*edges))
648 n = nx.number_of_nodes(g)
649
650 # Nodes in the base tree graph belong to class 0
651 node_labels = [0] * n
652 # The motifs will be evenly attached to the nodes in the base graph.
653 spacing = math.floor(n / self.num_motifs)
654
655 # Construct an n-by-n grid
656 motif_g = nx.grid_graph([self.grid_size, self.grid_size])
657 grid_size = nx.number_of_nodes(motif_g)
658 motif_g = nx.convert_node_labels_to_integers(motif_g, first_label=0)
659 motif_edges = list(motif_g.edges())
660 motif_src, motif_dst = map(list, zip(*motif_edges))
661 motif_src, motif_dst = np.array(motif_src), np.array(motif_dst)
662
663 for motif_id in range(self.num_motifs):
664 src.extend((motif_src + n).tolist())
665 dst.extend((motif_dst + n).tolist())
666
667 # Nodes in grids belong to class 1
668 node_labels.extend([1] * grid_size)
669
670 # Attach the motif to the base tree graph
671 src.append(n)
672 dst.append(int(motif_id * spacing))
673
674 n += grid_size
675
676 g = graph((src, dst), num_nodes=n)
677
678 # Perturb the graph by adding non-self-loop random edges
679 num_real_edges = g.num_edges()
680 max_ratio = (n * (n - 1) - num_real_edges) / num_real_edges
681 assert (
682 self.perturb_ratio <= max_ratio
683 ), "perturb_ratio cannot exceed {:.4f}".format(max_ratio)
684 num_random_edges = int(num_real_edges * self.perturb_ratio)
685
686 for _ in range(num_random_edges):
687 while True:
688 u = np.random.randint(0, n)
689 v = np.random.randint(0, n)
690 if (not g.has_edges_between(u, v)) and (u != v):
691 break
692 g.add_edges(u, v)
693
694 g.ndata["label"] = F.tensor(node_labels, F.int64)
695 g.ndata["feat"] = F.ones((n, 1), F.float32, F.cpu())
696 self._graph = reorder_graph(
697 g,
698 node_permute_algo="rcmk",

Callers

nothing calls this directly

Calls 11

graphFunction · 0.85
reorder_graphFunction · 0.85
extendMethod · 0.80
appendMethod · 0.80
formatMethod · 0.80
edgesMethod · 0.45
number_of_nodesMethod · 0.45
num_edgesMethod · 0.45
has_edges_betweenMethod · 0.45
add_edgesMethod · 0.45
cpuMethod · 0.45

Tested by

no test coverage detected