示例#1
0
 def adj_dst_index(self):
     """Return an EdgeIndex object for dst.
     """
     if self._adj_dst_index is None:
         v = self._edges[:, 0]
         u = self._edges[:, 1]
         self._adj_dst_index = EdgeIndex.from_edges(
             u=u, v=v, num_nodes=self._num_nodes)
     return self._adj_dst_index
示例#2
0
    def load(cls, path, mmap_mode="r"):
        """Load Graph from path and return a Graph in numpy. 

        Args:

            path: The directory path of the stored Graph.

            mmap_mode: Default :code:`mmap_mode="r"`. If not None, memory-map the graph.  
        """

        num_nodes = np.load(os.path.join(path, 'num_nodes.npy'),
                            mmap_mode=mmap_mode)
        edges = np.load(os.path.join(path, 'edges.npy'), mmap_mode=mmap_mode)
        num_graph = np.load(os.path.join(path, 'num_graph.npy'),
                            mmap_mode=mmap_mode)
        if os.path.exists(os.path.join(path, 'graph_node_index.npy')):
            graph_node_index = np.load(os.path.join(path,
                                                    'graph_node_index.npy'),
                                       mmap_mode=mmap_mode)
        else:
            graph_node_index = None

        if os.path.exists(os.path.join(path, 'graph_edge_index.npy')):
            graph_edge_index = np.load(os.path.join(path,
                                                    'graph_edge_index.npy'),
                                       mmap_mode=mmap_mode)
        else:
            graph_edge_index = None

        if os.path.isdir(os.path.join(path, 'adj_src')):
            adj_src_index = EdgeIndex.load(os.path.join(path, 'adj_src'),
                                           mmap_mode=mmap_mode)
        else:
            adj_src_index = None

        if os.path.isdir(os.path.join(path, 'adj_dst')):
            adj_dst_index = EdgeIndex.load(os.path.join(path, 'adj_dst'),
                                           mmap_mode=mmap_mode)
        else:
            adj_dst_index = None

        def _load_feat(feat_path):
            """Load features from .npy file.
            """
            feat = {}
            if os.path.isdir(feat_path):
                for feat_name in os.listdir(feat_path):
                    feat[os.path.splitext(feat_name)[0]] = np.load(
                        os.path.join(feat_path, feat_name),
                        mmap_mode=mmap_mode)
            return feat

        node_feat = _load_feat(os.path.join(path, 'node_feat'))
        edge_feat = _load_feat(os.path.join(path, 'edge_feat'))
        return cls(edges=edges,
                   num_nodes=num_nodes,
                   node_feat=node_feat,
                   edge_feat=edge_feat,
                   adj_src_index=adj_src_index,
                   adj_dst_index=adj_dst_index,
                   _num_graph=num_graph,
                   _graph_node_index=graph_node_index,
                   _graph_edge_index=graph_edge_index)