| 40 | |
| 41 | |
| 42 | class BlogcatalogDataset(Dataset): |
| 43 | |
| 44 | def __init__(self, input_path, train_ratio): |
| 45 | Dataset.__init__(self, input_path) |
| 46 | self.input_path = input_path |
| 47 | self.train_ratio = train_ratio |
| 48 | self.mat_data = None |
| 49 | |
| 50 | def load(self): |
| 51 | self.mat_data = scipy_io.loadmat(self.input_path) |
| 52 | |
| 53 | def to_nx_graph(self): |
| 54 | assert "group" in self.mat_data |
| 55 | node_labels = self.mat_data["group"].toarray() |
| 56 | node_ids = list(range(len(node_labels))) |
| 57 | train_node_ids = set( |
| 58 | random.sample(node_ids, int(len(node_ids) * self.train_ratio))) |
| 59 | |
| 60 | nx_graph = nx.Graph() |
| 61 | for node_id in node_ids: |
| 62 | stage = "train" if node_id in train_node_ids else "test" |
| 63 | nx_graph.add_node(node_id, stage=stage, label=node_labels[node_id]) |
| 64 | |
| 65 | assert "network" in self.mat_data |
| 66 | edges = self.mat_data["network"].nonzero() |
| 67 | for src_id, dst_id in zip(edges[0], edges[1]): |
| 68 | if src_id in train_node_ids and dst_id in train_node_ids: |
| 69 | stage = "train" |
| 70 | else: |
| 71 | stage = "test" |
| 72 | nx_graph.add_edge(src_id, dst_id, stage=stage, weight=1.0) |
| 73 | return nx_graph |
| 74 | |
| 75 | |
| 76 | class CoraDataset(Dataset): |