heter_graph.py 16.7 KB
Newer Older
Y
Yelrose 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (c) 2019 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.
"""
    This package implement Heterogeneous Graph structure for handling Heterogeneous graph data.
"""
W
Webbley 已提交
17
import os
L
liweibin 已提交
18
import time
Y
Yelrose 已提交
19 20 21 22
import numpy as np
import pickle as pkl
import time
import pgl.graph_kernel as graph_kernel
W
Webbley 已提交
23
from pgl.graph import Graph, MemmapGraph
Y
Yelrose 已提交
24

25
__all__ = ['HeterGraph', 'SubHeterGraph']
Y
Yelrose 已提交
26 27 28 29 30 31 32 33 34 35


def _hide_num_nodes(shape):
    """Set the first dimension as unknown
    """
    shape = list(shape)
    shape[0] = None
    return shape


L
liweibin 已提交
36 37
class HeterGraph(object):
    """Implementation of heterogeneous graph structure in pgl
Y
Yelrose 已提交
38

L
liweibin 已提交
39
    This is a simple implementation of heterogeneous graph structure in pgl.
Y
Yelrose 已提交
40

L
liweibin 已提交
41 42 43 44 45 46
    Args:
        num_nodes: number of nodes in a heterogeneous graph
        edges: dict, every element in dict is a list of (u, v) tuples.
        node_types (optional): list of (u, node_type) tuples to specify the node type of every node
        node_feat (optional): a dict of numpy array as node features
        edge_feat (optional): a dict of dict as edge features for every edge type
Y
Yelrose 已提交
47 48 49 50 51

    Examples:
        .. code-block:: python

            import numpy as np
L
liweibin 已提交
52 53 54 55 56
            num_nodes = 4
            node_types = [(0, 'user'), (1, 'item'), (2, 'item'), (3, 'user')]
            edges = {
                'edges_type1': [(0,1), (3,2)],
                'edges_type2': [(1,2), (3,1)],
Y
Yelrose 已提交
57
            }
L
liweibin 已提交
58 59 60 61
            node_feat = {'feature': np.random.randn(4, 16)}
            edges_feat = {
                'edges_type1': {'h': np.random.randn(2, 16)},
                'edges_type2': {'h': np.random.randn(2, 16)},
Y
Yelrose 已提交
62 63 64
            }

            g = heter_graph.HeterGraph(
L
liweibin 已提交
65 66 67 68 69
                            num_nodes=num_nodes,
                            edges=edges,
                            node_types=node_types,
                            node_feat=node_feat,
                            edge_feat=edges_feat)
Y
Yelrose 已提交
70 71 72
    """

    def __init__(self,
L
liweibin 已提交
73 74 75 76 77 78 79 80
                 num_nodes,
                 edges,
                 node_types=None,
                 node_feat=None,
                 edge_feat=None):
        self._num_nodes = num_nodes
        self._edges_dict = edges

81 82 83 84 85 86 87 88 89 90
        if isinstance(node_types, list):
            self._node_types = np.array(node_types, dtype=object)[:, 1]
        else:
            self._node_types = node_types

        self._nodes_type_dict = {}
        for n_type in np.unique(self._node_types):
            self._nodes_type_dict[n_type] = np.where(
                self._node_types == n_type)[0]

L
liweibin 已提交
91 92
        if node_feat is not None:
            self._node_feat = node_feat
Y
Yelrose 已提交
93 94 95
        else:
            self._node_feat = {}

L
liweibin 已提交
96 97
        if edge_feat is not None:
            self._edge_feat = edge_feat
Y
Yelrose 已提交
98 99 100 101
        else:
            self._edge_feat = {}

        self._multi_graph = {}
102

Y
Yelrose 已提交
103 104 105 106 107 108
        for key, value in self._edges_dict.items():
            if not self._edge_feat:
                edge_feat = None
            else:
                edge_feat = self._edge_feat[key]

109
            self._multi_graph[key] = Graph(
L
liweibin 已提交
110
                num_nodes=self._num_nodes,
Y
Yelrose 已提交
111
                edges=value,
L
liweibin 已提交
112
                node_feat=self._node_feat,
Y
Yelrose 已提交
113 114
                edge_feat=edge_feat)

115 116
        self._edge_types = self.edge_types_info()

W
Webbley 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    def dump(self, path, indegree=False, outdegree=False):

        if indegree:
            for e_type, g in self._multi_graph.items():
                g.indegree()

        if outdegree:
            for e_type, g in self._multi_graph.items():
                g.outdegree()

        if not os.path.exists(path):
            os.makedirs(path)

        np.save(os.path.join(path, "num_nodes.npy"), self._num_nodes)
        np.save(os.path.join(path, "node_types.npy"), self._node_types)
        with open(os.path.join(path, "edge_types.pkl"), 'wb') as f:
            pkl.dump(self._edge_types, f)
        with open(os.path.join(path, "nodes_type_dict.pkl"), 'wb') as f:
            pkl.dump(self._nodes_type_dict, f)

        for e_type, g in self._multi_graph.items():
            sub_path = os.path.join(path, e_type)
            g.dump(sub_path)

141 142 143 144 145 146
    @property
    def edge_types(self):
        """Return a list of edge types.
        """
        return self._edge_types

L
liweibin 已提交
147 148 149 150 151 152
    @property
    def num_nodes(self):
        """Return the number of nodes.
        """
        return self._num_nodes

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    @property
    def num_edges(self):
        """Return edges number of all edge types.
        """
        n_edges = {}
        for e_type in self._edge_types:
            n_edges[e_type] = self._multi_graph[e_type].num_edges
        return n_edges

    @property
    def node_types(self):
        """Return the node types.
        """
        return self._node_types

    @property
    def edge_feat(self, edge_type=None):
        """Return edge features of all edge types.
        """
        return self._edge_feat

    @property
    def node_feat(self):
        """Return a dictionary of node features.
        """
        return self._node_feat

    @property
    def nodes(self):
        """Return all nodes id from 0 to :code:`num_nodes - 1`
        """
        return np.arange(self._num_nodes, dtype='int64')

Y
Yelrose 已提交
186 187 188 189 190
    def __getitem__(self, edge_type):
        """__getitem__
        """
        return self._multi_graph[edge_type]

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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    def num_nodes_by_type(self, n_type=None):
        """Return the number of nodes with the specified node type.
        """
        if n_type not in self._nodes_type_dict:
            raise ("%s is not in valid node type" % n_type)
        else:
            return len(self._nodes_type_dict[n_type])

    def indegree(self, nodes=None, edge_type=None):
        """Return the indegree of the given nodes with the specified edge_type.

        Args:
            nodes: Return the indegree of given nodes.
                    if nodes is None, return indegree for all nodes.

            edge_types: Return the indegree with specified edge_type.
                    if edge_type is None, return the total indegree of the given nodes.

        Return:
            A numpy.ndarray as the given nodes' indegree.
        """
        if edge_type is None:
            indegrees = []
            for e_type in self._edge_types:
                indegrees.append(self._multi_graph[e_type].indegree(nodes))
            indegrees = np.sum(np.vstack(indegrees), axis=0)
            return indegrees
        else:
            return self._multi_graph[edge_type].indegree(nodes)

    def outdegree(self, nodes=None, edge_type=None):
        """Return the outdegree of the given nodes with the specified edge_type.

        Args:
            nodes: Return the outdegree of given nodes,
                   if nodes is None, return outdegree for all nodes

            edge_types: Return the outdegree with specified edge_type.
                    if edge_type is None, return the total outdegree of the given nodes.

        Return:
            A numpy.array as the given nodes' outdegree.
        """
        if edge_type is None:
            outdegrees = []
            for e_type in self._edge_types:
                outdegrees.append(self._multi_graph[e_type].outdegree(nodes))
            outdegrees = np.sum(np.vstack(outdegrees), axis=0)
            return outdegrees
        else:
            return self._multi_graph[edge_type].outdegree(nodes)

    def successor(self, edge_type, nodes=None, return_eids=False):
        """Find successor of given nodes with the specified edge_type.

        Args:
            nodes: Return the successor of given nodes,
                   if nodes is None, return successor for all nodes

            edge_types: Return the successor with specified edge_type.
                    if edge_type is None, return the total successor of the given nodes
                    and eids are invalid in this way.

            return_eids: If True return nodes together with corresponding eid
        """
        return self._multi_graph[edge_type].successor(nodes, return_eids)

    def sample_successor(self,
                         edge_type,
                         nodes,
                         max_degree,
                         return_eids=False,
                         shuffle=False):
        """Sample successors of given nodes with the specified edge_type.

        Args:
            edge_type: The specified edge_type.

            nodes: Given nodes whose successors will be sampled.

            max_degree: The max sampled successors for each nodes.

            return_eids: Whether to return the corresponding eids.

        Return:

            Return a list of numpy.ndarray and each numpy.ndarray represent a list
            of sampled successor ids for given nodes with specified edge type. 
            If :code:`return_eids=True`, there will be an additional list of 
            numpy.ndarray and each numpy.ndarray represent a list of eids that 
            connected nodes to their successors.
        """
        return self._multi_graph[edge_type].sample_successor(
            nodes=nodes,
            max_degree=max_degree,
            return_eids=return_eids,
            shuffle=shuffle)

    def predecessor(self, edge_type, nodes=None, return_eids=False):
        """Find predecessor of given nodes with the specified edge_type.

        Args:
            nodes: Return the predecessor of given nodes,
                   if nodes is None, return predecessor for all nodes

            edge_types: Return the predecessor with specified edge_type.

            return_eids: If True return nodes together with corresponding eid
        """
        return self._multi_graph[edge_type].predecessor(nodes, return_eids)

    def sample_predecessor(self,
                           edge_type,
                           nodes,
                           max_degree,
                           return_eids=False,
                           shuffle=False):
        """Sample predecessors of given nodes with the specified edge_type.

        Args:
            edge_type: The specified edge_type.

            nodes: Given nodes whose predecessors will be sampled.

            max_degree: The max sampled predecessors for each nodes.

            return_eids: Whether to return the corresponding eids.

        Return:

            Return a list of numpy.ndarray and each numpy.ndarray represent a list
            of sampled predecessor ids for given nodes with specified edge type. 
            If :code:`return_eids=True`, there will be an additional list of 
            numpy.ndarray and each numpy.ndarray represent a list of eids that 
            connected nodes to their predecessors.
        """
        return self._multi_graph[edge_type].sample_predecessor(
            nodes=nodes,
            max_degree=max_degree,
            return_eids=return_eids,
            shuffle=shuffle)

    def node_batch_iter(self, batch_size, shuffle=True, n_type=None):
        """Node batch iterator

        Iterate all nodes by batch with the specified node type.

        Args:
            batch_size: The batch size of each batch of nodes.

            shuffle: Whether shuffle the nodes.
            
            n_type: Iterate the nodes with the specified node type. If n_type is None, 
                    iterate all nodes by batch.

        Return:
            Batch iterator
        """
        if n_type is None:
            nodes = np.arange(self._num_nodes, dtype="int64")
        else:
            nodes = self._nodes_type_dict[n_type]

        if shuffle:
            np.random.shuffle(nodes)
        start = 0
        while start < len(nodes):
            yield nodes[start:start + batch_size]
            start += batch_size

    def sample_nodes(self, sample_num, n_type=None):
        """Sample nodes with the specified n_type from the graph

        This function helps to sample nodes with the specified n_type from the graph.
        If n_type is None, this function will sample nodes from all nodes.
        Nodes might be duplicated.

        Args:
            sample_num: The number of samples
            n_type: The nodes of type to be sampled

        Return:
            A list of nodes
        """
        if n_type is not None:
            return np.random.choice(
                self._nodes_type_dict[n_type], size=sample_num)
        else:
            return np.random.randint(
                low=0, high=self._num_nodes, size=sample_num)

Y
Yelrose 已提交
382 383 384 385 386 387 388
    def node_feat_info(self):
        """Return the information of node feature for HeterGraphWrapper.

        This function return the information of node features of all node types. And this
        function is used to help constructing HeterGraphWrapper

        Return:
L
liweibin 已提交
389
            A list of tuple (name, shape, dtype) for all given node feature.
Y
Yelrose 已提交
390 391

        """
L
liweibin 已提交
392 393 394 395
        node_feat_info = []
        for feat_name, feat in self._node_feat.items():
            node_feat_info.append(
                (feat_name, _hide_num_nodes(feat.shape), feat.dtype))
Y
Yelrose 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

        return node_feat_info

    def edge_feat_info(self):
        """Return the information of edge feature for HeterGraphWrapper.

        This function return the information of edge features of all edge types. And this
        function is used to help constructing HeterGraphWrapper

        Return:
            A dict of list of tuple (name, shape, dtype) for all given edge feature.

        """
        edge_feat_info = {}
        for edge_type_name, feat_dict in self._edge_feat.items():
            tmp_edge_feat_info = []
            for feat_name, feat in feat_dict.items():
                full_name = feat_name
                tmp_edge_feat_info.append(
                    (full_name, _hide_num_nodes(feat.shape), feat.dtype))
            edge_feat_info[edge_type_name] = tmp_edge_feat_info
        return edge_feat_info

    def edge_types_info(self):
        """Return the information of all edge types.
        
        Return:
L
liweibin 已提交
423
            A list of all edge types.
Y
Yelrose 已提交
424 425 426
        
        """
        edge_types_info = []
W
Webbley 已提交
427
        for key, _ in self._multi_graph.items():
Y
Yelrose 已提交
428 429 430
            edge_types_info.append(key)

        return edge_types_info
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487


class SubHeterGraph(HeterGraph):
    """Implementation of SubHeterGraph in pgl.

    SubHeterGraph is inherit from :code:`HeterGraph`. 

    Args:
        num_nodes: number of nodes in a heterogeneous graph
        edges: dict, every element in dict is a list of (u, v) tuples.
        node_types (optional): list of (u, node_type) tuples to specify the node type of every node
        node_feat (optional): a dict of numpy array as node features
        edge_feat (optional): a dict of dict as edge features for every edge type

        reindex: A dictionary that maps parent hetergraph node id to subhetergraph node id.
    """

    def __init__(self,
                 num_nodes,
                 edges,
                 node_types=None,
                 node_feat=None,
                 edge_feat=None,
                 reindex=None):
        super(SubHeterGraph, self).__init__(
            num_nodes=num_nodes,
            edges=edges,
            node_types=node_types,
            node_feat=node_feat,
            edge_feat=edge_feat)

        if reindex is None:
            reindex = {}
        self._from_reindex = reindex
        self._to_reindex = {u: v for v, u in reindex.items()}

    def reindex_from_parrent_nodes(self, nodes):
        """Map the given parent graph node id to subgraph id.

        Args:
            nodes: A list of nodes from parent graph.

        Return:
            A list of subgraph ids.
        """
        return graph_kernel.map_nodes(nodes, self._from_reindex)

    def reindex_to_parrent_nodes(self, nodes):
        """Map the given subgraph node id to parent graph id.

        Args:
            nodes: A list of nodes in this subgraph.

        Return:
            A list of node ids in parent graph.
        """
        return graph_kernel.map_nodes(nodes, self._to_reindex)
W
Webbley 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505


class MemmapHeterGraph(HeterGraph):
    def __init__(self, path):
        self._num_nodes = np.load(os.path.join(path, 'num_nodes.npy'))
        self._node_types = np.load(
            os.path.join(path, 'node_types.npy'), allow_pickle=True)

        with open(os.path.join(path, 'edge_types.pkl'), 'rb') as f:
            self._edge_types = pkl.load(f)

        with open(os.path.join(path, "nodes_type_dict.pkl"), 'rb') as f:
            self._nodes_type_dict = pkl.load(f)

        self._multi_graph = {}
        for e_type in self._edge_types:
            sub_path = os.path.join(path, e_type)
            self._multi_graph[e_type] = MemmapGraph(sub_path)