dataset_pgl.py 9.4 KB
Newer Older
L
liweibin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""NodePropPredDataset for pgl
"""
Z
Zhong Hui 已提交
16
import pgl
L
liweibin 已提交
17 18 19 20 21 22
import pandas as pd
import shutil, os
import os.path as osp
import numpy as np
from ogb.utils.url import decide_download, download_url, extract_zip
from ogb.nodeproppred import make_master_file  # create master.csv
Z
Zhong Hui 已提交
23 24
from pgl.contrib.ogb.io.read_graph_pgl import read_csv_graph_pgl, read_csv_heterograph_pgl
from ogb.io.read_graph_raw import read_node_label_hetero, read_nodesplitidx_split_hetero
L
liweibin 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57


def to_bool(value):
    """to_bool"""
    return np.array([value], dtype="bool")[0]


class PglNodePropPredDataset(object):
    """PglNodePropPredDataset
    """

    def __init__(self, name, root="dataset"):
        self.name = name  ## original name, e.g., ogbn-proteins
        self.dir_name = "_".join(
            name.split("-")
        ) + "_pgl"  ## replace hyphen with underline, e.g., ogbn_proteins_pgl

        self.original_root = root
        self.root = osp.join(root, self.dir_name)

        self.meta_info = make_master_file.df  #pd.read_csv(
        #os.path.join(os.path.dirname(__file__), "master.csv"), index_col=0)
        if not self.name in self.meta_info:
            error_mssg = "Invalid dataset name {}.\n".format(self.name)
            error_mssg += "Available datasets are as follows:\n"
            error_mssg += "\n".join(self.meta_info.keys())
            raise ValueError(error_mssg)

        self.download_name = self.meta_info[self.name][
            "download_name"]  ## name of downloaded file, e.g., tox21

        self.num_tasks = int(self.meta_info[self.name]["num tasks"])
        self.task_type = self.meta_info[self.name]["task type"]
Z
Zhong Hui 已提交
58
        self.eval_metric = self.meta_info[self.name]["eval metric"]
Z
refine  
Zhong Hui 已提交
59
        self.num_classes = int(self.meta_info[self.name]["num classes"])
Z
Zhong Hui 已提交
60
        self.is_hetero = self.meta_info[self.name]["is hetero"]
L
liweibin 已提交
61 62 63 64 65 66 67 68 69 70 71 72

        super(PglNodePropPredDataset, self).__init__()

        self.pre_process()

    def pre_process(self):
        """pre_process downlaoding data
        """
        processed_dir = osp.join(self.root, 'processed')
        pre_processed_file_path = osp.join(processed_dir, 'pgl_data_processed')

        if osp.exists(pre_processed_file_path):
Z
Zhong Hui 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85
            # TODO: Reload Preprocess files. DONE @ZHUI
            # TODO: add support for heterogenous graph.
            self.graph = []
            if os.path.isdir(pre_processed_file_path):
                for i in range(len(os.listdir(pre_processed_file_path))):
                    graph_path = os.path.join(pre_processed_file_path,
                                              "graph_{}".format(i))
                    if os.path.exists(graph_path):
                        self.graph.append(pgl.graph.Graph().load(graph_path))
            node_label = np.load(
                os.path.join(pre_processed_file_path, "node_label.npy"))
            label_dict = {"labels": node_label}
            self.labels = label_dict['labels']
L
liweibin 已提交
86 87
        else:
            ### check download
Z
Zhong Hui 已提交
88
            if not osp.exists(osp.join(self.root, "raw")):
L
liweibin 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
                url = self.meta_info[self.name]["url"]
                if decide_download(url):
                    path = download_url(url, self.original_root)
                    extract_zip(path, self.original_root)
                    os.unlink(path)
                    # delete folder if there exists
                    try:
                        shutil.rmtree(self.root)
                    except:
                        pass
                    shutil.move(
                        osp.join(self.original_root, self.download_name),
                        self.root)
                else:
                    print("Stop download.")
                    exit(-1)

            raw_dir = osp.join(self.root, "raw")
Z
Zhong Hui 已提交
107
            self.raw_dir = raw_dir
L
liweibin 已提交
108 109 110 111

            ### pre-process and save
            add_inverse_edge = to_bool(self.meta_info[self.name][
                "add_inverse_edge"])
Z
Zhong Hui 已提交
112 113
            add_inverse_edge = self.meta_info[self.name][
                "add_inverse_edge"] == "True"
L
liweibin 已提交
114

Z
Zhong Hui 已提交
115 116 117 118 119 120 121 122
            if self.meta_info[self.name]["additional node files"] == 'None':
                additional_node_files = []
            else:
                additional_node_files = self.meta_info[self.name][
                    "additional node files"].split(',')

            if self.meta_info[self.name]["additional edge files"] == 'None':
                additional_edge_files = []
L
liweibin 已提交
123
            else:
Z
Zhong Hui 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
                additional_edge_files = self.meta_info[self.name][
                    "additional edge files"].split(',')

            if self.is_hetero:
                self.graph = read_csv_heterograph_pgl(
                    self.raw_dir,
                    add_inverse_edge=add_inverse_edge,
                    additional_node_files=additional_node_files,
                    additional_edge_files=additional_edge_files)

                node_label_dict = read_node_label_hetero(self.raw_dir)
                y_dict = {}
                if "classification" in self.task_type:
                    for nodetype, node_label in node_label_dict.items():
                        # detect if there is any nan
                        if np.isnan(node_label).any():
                            y_dict[nodetype] = np.array(
                                node_label, dtype='float32')
                        else:
                            y_dict[nodetype] = np.array(
                                node_label, dtype='int64')
                else:
                    for nodetype, node_label in node_label_dict.items():
                        y_dict[nodetype] = np.array(
                            node_label, dtype='float32')
                self.labels = y_dict
L
liweibin 已提交
150

Z
Zhong Hui 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163
            else:
                self.graph = read_csv_graph_pgl(
                    raw_dir, add_inverse_edge=add_inverse_edge)

                ### adding prediction target
                node_label = pd.read_csv(
                    osp.join(raw_dir, 'node-label.csv.gz'),
                    compression="gzip",
                    header=None).values
                if "classification" in self.task_type:
                    node_label = np.array(node_label, dtype=np.int64)
                else:
                    node_label = np.array(node_label, dtype=np.float32)
L
liweibin 已提交
164

Z
Zhong Hui 已提交
165 166 167
                label_dict = {"labels": node_label}

                self.labels = label_dict['labels']
Z
Zhong Hui 已提交
168 169 170 171 172 173 174 175
                # TODO: SAVE preprocess graph, DONE @ZHUI
                for i in range(len(self.graph)):
                    self.graph[i].dump(
                        os.path.join(pre_processed_file_path,
                                     "graph_{}".format(i)))
                np.save(
                    os.path.join(pre_processed_file_path, "node_label.npy"),
                    node_label)
L
liweibin 已提交
176 177 178 179 180 181

    def get_idx_split(self):
        """Train/Validation/Test split
        """
        split_type = self.meta_info[self.name]["split"]
        path = osp.join(self.root, "split", split_type)
Z
Zhong Hui 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        if self.is_hetero:
            train_idx_dict, valid_idx_dict, test_idx_dict = read_nodesplitidx_split_hetero(
                path)
            for nodetype in train_idx_dict.keys():
                train_idx_dict[nodetype] = np.array(
                    train_idx_dict[nodetype], dtype='int64')
                valid_idx_dict[nodetype] = np.array(
                    valid_idx_dict[nodetype], dtype='int64')
                test_idx_dict[nodetype] = np.array(
                    test_idx_dict[nodetype], dtype='int64')
                # code refers dataset_pyg
                # TODO: check the code
                return {
                    "train": train_idx_dict,
                    "valid": valid_idx_dict,
                    "test": test_idx_dict
                }
        else:
            train_idx = pd.read_csv(
                osp.join(path, "train.csv.gz"),
                compression="gzip",
                header=None).values.T[0]
            valid_idx = pd.read_csv(
                osp.join(path, "valid.csv.gz"),
                compression="gzip",
                header=None).values.T[0]
            test_idx = pd.read_csv(
                osp.join(path, "test.csv.gz"), compression="gzip",
                header=None).values.T[0]

            return {
                "train": np.array(
                    train_idx, dtype="int64"),
                "valid": np.array(
                    valid_idx, dtype="int64"),
                "test": np.array(
                    test_idx, dtype="int64")
            }
L
liweibin 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232

    def __getitem__(self, idx):
        assert idx == 0, "This dataset has only one graph"
        return self.graph[idx], self.labels

    def __len__(self):
        return 1

    def __repr__(self):  # pragma: no cover
        return '{}({})'.format(self.__class__.__name__, len(self))


if __name__ == "__main__":
Z
Zhong Hui 已提交
233
    pgl_dataset = PglNodePropPredDataset(name="ogbn-mag")
L
liweibin 已提交
234 235 236
    splitted_index = pgl_dataset.get_idx_split()
    print(pgl_dataset[0])
    print(splitted_index)