Unverified Commit 4bd4d6e3 authored by Minjie Wang's avatar Minjie Wang Committed by GitHub
Browse files

[Lint] Pylint (#330)

* fix lint for graph_index.py

* pylint for base.py

* pylint for batched_graph.py

* pylint for frame.py; simplify and fix bugs in frame when index is slice type

* pylint for graph.py

* pylint for immutable_graph_index.py

* pylint for init.py

* pylint for rest files in root package

* pylint for _ffi package

* pylint for function package

* pylint for runtime package

* pylint for runtime.ir package

* add pylint to ci

* fix mx tests

* fix lint errors

* fix ci

* fix as requested

* fix lint
parent 1e50cd2e
"""Package for DGL's internal IR."""
from .executor import *
from .program import get_current_prog, prog
This diff is collapsed.
"""Module for program."""
from __future__ import absolute_import
from contextlib import contextmanager
......@@ -5,15 +6,26 @@ from contextlib import contextmanager
from .registry import IR_REGISTRY
class Prog(object):
"""The program."""
"""The program.
A program is simply a list of executors.
"""
def __init__(self):
self.execs = []
self.varcount = 0
def issue(self, exe):
"""Issue an executor to this program.
Parameters
----------
exe : Executor
The executor.
"""
self.execs.append(exe)
def pprint_exe(self, exe):
"""Internal function to pretty-print the executor."""
argstr = ', '.join([str(av) for av in exe.arg_vars()])
if exe.ret_var() is None:
# stmt
......@@ -28,21 +40,26 @@ class Prog(object):
argstr))
def pprint(self):
"""Pretty-print the program."""
for exe in self.execs:
self.pprint_exe(exe)
_current_prog = None
# current program
CURRENT_PROG = None
def get_current_prog():
global _current_prog
return _current_prog
"""Get the current program."""
global CURRENT_PROG
return CURRENT_PROG
def set_current_prog(prog):
global _current_prog
_current_prog = prog
def set_current_prog(program):
"""Set the current program."""
global CURRENT_PROG
CURRENT_PROG = program
@contextmanager
def prog():
"""A context manager to create a new program."""
set_current_prog(Prog())
yield get_current_prog()
set_current_prog(None)
"""Module for variables."""
# pylint: disable=invalid-name
from __future__ import absolute_import
from .program import get_current_prog
class VarType(object):
"""Variable types."""
# Types for symbolic objects (i.e, they might not be
# concretized before evaluation.
FEAT = 0
......@@ -23,47 +26,65 @@ VAR_TYPE_NAME_MAP = [
]
class Var(object):
"""Variable
"""Class for variables in IR.
Variables represent data in the IR. A variable can contain concrete values.
Otherwise, it can act as a "symbol", whose values are not materialized at the
moment, but later.
Parameters
----------
name : str
The variable name.
type : int
The type code.
data : any, default=None (not concretized)
The data.
"""
__slots__ = ['name', 'type', 'data']
def __init__(self, name, type, data):
__slots__ = ['name', 'typecode', 'data']
def __init__(self, name, typecode, data):
self.name = name
self.type = type
self.typecode = typecode
self.data = data
def __str__(self):
if self.type == VarType.STR:
if self.typecode == VarType.STR:
return '"%s"' % self.data
else:
return self.name
def typestr(self):
return VAR_TYPE_NAME_MAP[self.type]
"""Return the type string of this variable."""
return VAR_TYPE_NAME_MAP[self.typecode]
def new(type, data=None, name=None):
def new(typecode, data=None, name=None):
"""Create a new variable."""
if name is None:
cur_prog = get_current_prog()
name = '_z%d' % cur_prog.varcount
cur_prog.varcount += 1
return Var(name, type, data)
return Var(name, typecode, data)
def FEAT(data=None, name=None):
"""Create a variable for feature tensor."""
return new(VarType.FEAT, data, name)
def FEAT_DICT(data=None, name=None):
"""Create a variable for feature dict."""
return new(VarType.FEAT_DICT, data, name)
def SPMAT(data=None, name=None):
"""Create a variable for sparse matrix lambda."""
return new(VarType.SPMAT, data, name)
def IDX(data=None, name=None):
"""Create a variable for index."""
return new(VarType.IDX, data, name)
def STR(data=None, name=None):
"""Create a variable for string value."""
return new(VarType.STR, data, name)
def FUNC(data=None, name=None):
"""Create a variable for function."""
return new(VarType.FUNC, data, name)
"""DGL mini-runtime."""
class Runtime(object):
"""The mini runtime class."""
@staticmethod
def run(prog):
"""Run the given program."""
for exe in prog.execs:
#prog.pprint_exe(exe)
exe.run()
......@@ -3,27 +3,27 @@ from __future__ import absolute_import
from .. import utils
from .._ffi.function import _init_api
from ..base import ALL, DGLError, is_all
from ..base import DGLError
from .. import backend as F
from ..frame import frame_like, FrameRef
from ..function.base import BuiltinFunction, BundledFunction
from ..udf import EdgeBatch, NodeBatch
from . import ir
from .ir import var as var
from .ir import var
from . import degree_bucketing as db
from . import spmv
__all__ = [
"schedule_send",
"schedule_recv",
"schedule_update_all",
"schedule_snr",
"schedule_apply_nodes",
"schedule_apply_edges",
"schedule_push",
"schedule_pull"
]
"schedule_send",
"schedule_recv",
"schedule_update_all",
"schedule_snr",
"schedule_apply_nodes",
"schedule_apply_edges",
"schedule_push",
"schedule_pull"
]
def schedule_send(graph, u, v, eid, message_func):
"""get send schedule
......@@ -132,7 +132,6 @@ def schedule_snr(graph,
inplace: bool
If True, the update will be done in place
"""
call_type = 'send_and_recv'
u, v, eid = edge_tuples
recv_nodes, _ = F.sort_1d(F.unique(v.tousertensor()))
recv_nodes = utils.toindex(recv_nodes)
......@@ -143,13 +142,12 @@ def schedule_snr(graph,
var_eid = var.IDX(eid)
var_recv_nodes = var.IDX(recv_nodes, name='recv_nodes')
# generate send and reduce schedule
uv_getter = lambda : (var_u, var_v)
adj_creator = lambda : spmv.build_adj_matrix_uv(graph, (u, v), recv_nodes)
inc_creator = lambda : spmv.build_inc_matrix_dst(v, recv_nodes)
reduced_feat = _gen_send_reduce(
graph, message_func, reduce_func,
var_eid, var_recv_nodes,
uv_getter, adj_creator, inc_creator)
uv_getter = lambda: (var_u, var_v)
adj_creator = lambda: spmv.build_adj_matrix_uv(graph, (u, v), recv_nodes)
inc_creator = lambda: spmv.build_inc_matrix_dst(v, recv_nodes)
reduced_feat = _gen_send_reduce(graph, message_func, reduce_func,
var_eid, var_recv_nodes,
uv_getter, adj_creator, inc_creator)
# generate apply schedule
final_feat = _apply_with_accum(graph, var_recv_nodes, var_nf, reduced_feat, apply_func)
if inplace:
......@@ -180,7 +178,6 @@ def schedule_update_all(graph,
nodes = utils.toindex(slice(0, graph.number_of_nodes()))
schedule_apply_nodes(graph, nodes, apply_func, inplace=False)
else:
call_type = 'update_all'
eid = utils.toindex(slice(0, graph.number_of_edges())) # shortcut for ALL
recv_nodes = utils.toindex(slice(0, graph.number_of_nodes())) # shortcut for ALL
# create vars
......@@ -191,12 +188,11 @@ def schedule_update_all(graph,
def uv_getter():
src, dst, _ = graph._graph.edges()
return var.IDX(src), var.IDX(dst)
adj_creator = lambda : spmv.build_adj_matrix_graph(graph)
inc_creator = lambda : spmv.build_inc_matrix_graph(graph)
reduced_feat = _gen_send_reduce(
graph, message_func, reduce_func,
var_eid, var_recv_nodes,
uv_getter, adj_creator, inc_creator)
adj_creator = lambda: spmv.build_adj_matrix_graph(graph)
inc_creator = lambda: spmv.build_inc_matrix_graph(graph)
reduced_feat = _gen_send_reduce(graph, message_func, reduce_func,
var_eid, var_recv_nodes,
uv_getter, adj_creator, inc_creator)
# generate optional apply
final_feat = _apply_with_accum(graph, var_recv_nodes, var_nf, reduced_feat, apply_func)
ir.WRITE_DICT_(var_nf, final_feat)
......@@ -226,8 +222,8 @@ def schedule_apply_nodes(graph,
var_v = var.IDX(v)
v_nf = ir.READ_ROW(var_nf, var_v)
def _afunc_wrapper(node_data):
nb = NodeBatch(graph, v, node_data)
return apply_func(nb)
nbatch = NodeBatch(graph, v, node_data)
return apply_func(nbatch)
afunc = var.FUNC(_afunc_wrapper)
applied_feat = ir.NODE_UDF(afunc, v_nf)
if inplace:
......@@ -271,9 +267,8 @@ def schedule_apply_edges(graph,
fddst = ir.READ_ROW(var_nf, var_v)
fdedge = ir.READ_ROW(var_ef, var_eid)
def _efunc_wrapper(src_data, edge_data, dst_data):
eb = EdgeBatch(graph, (u, v, eid),
src_data, edge_data, dst_data)
return apply_func(eb)
ebatch = EdgeBatch(graph, (u, v, eid), src_data, edge_data, dst_data)
return apply_func(ebatch)
_efunc = var.FUNC(_efunc_wrapper)
new_fdedge = ir.EDGE_UDF(_efunc, fdsrc, fdedge, fddst)
if inplace:
......@@ -343,7 +338,6 @@ def schedule_pull(graph,
if apply_func is not None:
schedule_apply_nodes(graph, pull_nodes, apply_func, inplace)
else:
call_type = 'send_and_recv'
pull_nodes, _ = F.sort_1d(F.unique(pull_nodes.tousertensor()))
pull_nodes = utils.toindex(pull_nodes)
# create vars
......@@ -353,13 +347,12 @@ def schedule_pull(graph,
var_v = var.IDX(v)
var_eid = var.IDX(eid)
# generate send and reduce schedule
uv_getter = lambda : (var_u, var_v)
adj_creator = lambda : spmv.build_adj_matrix_uv(graph, (u, v), pull_nodes)
inc_creator = lambda : spmv.build_inc_matrix_dst(v, pull_nodes)
reduced_feat = _gen_send_reduce(
graph, message_func, reduce_func,
var_eid, var_pull_nodes,
uv_getter, adj_creator, inc_creator)
uv_getter = lambda: (var_u, var_v)
adj_creator = lambda: spmv.build_adj_matrix_uv(graph, (u, v), pull_nodes)
inc_creator = lambda: spmv.build_inc_matrix_dst(v, pull_nodes)
reduced_feat = _gen_send_reduce(graph, message_func, reduce_func,
var_eid, var_pull_nodes,
uv_getter, adj_creator, inc_creator)
# generate optional apply
final_feat = _apply_with_accum(graph, var_pull_nodes, var_nf, reduced_feat, apply_func)
if inplace:
......@@ -423,8 +416,8 @@ def _apply_with_accum(graph, var_nodes, var_nf, var_accum, apply_func):
v_nf = ir.READ_ROW(var_nf, var_nodes)
v_nf = ir.UPDATE_DICT(v_nf, var_accum)
def _afunc_wrapper(node_data):
nb = NodeBatch(graph, var_nodes.data, node_data)
return apply_func(nb)
nbatch = NodeBatch(graph, var_nodes.data, node_data)
return apply_func(nbatch)
afunc = var.FUNC(_afunc_wrapper)
applied_feat = ir.NODE_UDF(afunc, v_nf)
final_feat = ir.UPDATE_DICT(var_accum, applied_feat)
......@@ -439,7 +432,6 @@ def _gen_reduce(graph, reduce_func, edge_tuples, recv_nodes):
edge_tuples : tuple of utils.Index
recv_nodes : utils.Index
"""
call_type = "recv"
_, dst, eid = edge_tuples
rfunc = _standardize_func_usage(reduce_func, 'reduce')
rfunc_is_list = utils.is_iterable(rfunc)
......@@ -451,9 +443,9 @@ def _gen_reduce(graph, reduce_func, edge_tuples, recv_nodes):
tmpframe = FrameRef(frame_like(graph._node_frame._frame, len(recv_nodes)))
# vars
msg = var.FEAT_DICT(graph._msg_frame, 'msg')
nf = var.FEAT_DICT(graph._node_frame, 'nf')
out = var.FEAT_DICT(data=tmpframe)
var_msg = var.FEAT_DICT(graph._msg_frame, 'msg')
var_nf = var.FEAT_DICT(graph._node_frame, 'nf')
var_out = var.FEAT_DICT(data=tmpframe)
if rfunc_is_list:
# UDF message + builtin reducer
......@@ -461,19 +453,19 @@ def _gen_reduce(graph, reduce_func, edge_tuples, recv_nodes):
spmv_rfunc, rfunc = spmv.analyze_e2v_spmv(graph, rfunc)
inc = spmv.build_inc_matrix_eid(graph._msg_frame.num_rows, eid, dst,
recv_nodes)
spmv.gen_e2v_spmv_schedule(inc, spmv_rfunc, msg, out)
spmv.gen_e2v_spmv_schedule(inc, spmv_rfunc, var_msg, var_out)
if len(rfunc) == 0:
# All mfunc and rfunc has been processed.
return out
return var_out
# convert the remaining rfunc to UDFs
rfunc = BundledFunction(rfunc)
# gen degree bucketing schedule for UDF recv
db.gen_degree_bucketing_schedule(graph, rfunc, eid, dst,
recv_nodes, nf, msg, out)
return out
recv_nodes, var_nf, var_msg, var_out)
return var_out
def _gen_send_reduce(
graph,
......@@ -573,19 +565,19 @@ def _gen_send_reduce(
# gen degree bucketing schedule for UDF recv
mid = utils.toindex(slice(0, len(var_v.data))) # message id is from 0~|dst|
db.gen_degree_bucketing_schedule(graph, rfunc,
mid, var_v.data, reduce_nodes,
var_nf, var_mf, var_out)
db.gen_degree_bucketing_schedule(
graph, rfunc, mid, var_v.data, reduce_nodes, var_nf, var_mf, var_out)
return var_out
def _gen_send(graph, nf, ef, u, v, eid, mfunc):
fdsrc = ir.READ_ROW(nf, u)
fddst = ir.READ_ROW(nf, v)
fdedge = ir.READ_ROW(ef, eid)
def _gen_send(graph, nfr, efr, u, v, eid, mfunc):
"""Internal function to generate send schedule."""
fdsrc = ir.READ_ROW(nfr, u)
fddst = ir.READ_ROW(nfr, v)
fdedge = ir.READ_ROW(efr, eid)
def _mfunc_wrapper(src_data, edge_data, dst_data):
eb = EdgeBatch(graph, (u.data, v.data, eid.data),
src_data, edge_data, dst_data)
return mfunc(eb)
ebatch = EdgeBatch(graph, (u.data, v.data, eid.data),
src_data, edge_data, dst_data)
return mfunc(ebatch)
_mfunc_wrapper = var.FUNC(_mfunc_wrapper)
msg = ir.EDGE_UDF(_mfunc_wrapper, fdsrc, fdedge, fddst)
return msg
......
......@@ -6,7 +6,7 @@ from .. import backend as F
from .. import utils
from . import ir
from .ir import var as var
from .ir import var
def analyze_v2v_spmv(graph, mfunc, rfunc):
"""Analyze if SPMV from node space to node space can be applied.
......@@ -54,7 +54,7 @@ def analyze_v2v_spmv(graph, mfunc, rfunc):
return spmv_pairs, mfunc_left, rfunc_left
def analyze_e2v_spmv(graph, rfunc):
def analyze_e2v_spmv(graph, rfunc): # pylint: disable=unused-argument
"""Analyze if SPMV from edge space to node space can be applied.
Parameters
......@@ -80,16 +80,16 @@ def analyze_e2v_spmv(graph, rfunc):
rfunc_left.append(rfn)
return spmv_rfunc, rfunc_left
def gen_v2v_spmv_schedule(adj, spmv_pairs, nf, ef, eid, out):
def gen_v2v_spmv_schedule(adj, spmv_pairs, nft, eft, eid, out):
"""Generate v2v spmv schedule.
Parameters
----------
adj : tuple (sparse matrix, utils.Index)
spmv_pairs : list of pair
nf : var.Var
nft : var.Var
input node features
ef : var.Var
eft : var.Var
input edge features
eid : var.Var
eid index
......@@ -103,16 +103,16 @@ def gen_v2v_spmv_schedule(adj, spmv_pairs, nf, ef, eid, out):
eid = var.IDX(new_eid)
for mfn, rfn in spmv_pairs:
if mfn.use_edge_feature:
ftedge = ir.READ(ef, eid, var.STR(mfn.edge_field))
ftsrc = ir.READ_COL(nf, var.STR(mfn.src_field))
ftedge = ir.READ(eft, eid, var.STR(mfn.edge_field))
ftsrc = ir.READ_COL(nft, var.STR(mfn.src_field))
ftdst = ir.SPMV_WITH_DATA(adj_var, ftedge, ftsrc)
else:
ftsrc = ir.READ_COL(nf, var.STR(mfn.src_field))
ftsrc = ir.READ_COL(nft, var.STR(mfn.src_field))
ftdst = ir.SPMV(adj_var, ftsrc)
# save for merge
ir.WRITE_COL_(out, var.STR(rfn.out_field), ftdst)
def gen_e2v_spmv_schedule(inc, spmv_rfunc, mf, out):
def gen_e2v_spmv_schedule(inc, spmv_rfunc, mfr, out):
"""Generate e2v SPMV schedule.
Parameters
......@@ -127,7 +127,7 @@ def gen_e2v_spmv_schedule(inc, spmv_rfunc, mf, out):
incmat, _ = inc
inc_var = var.SPMAT(incmat)
for rfn in spmv_rfunc:
ftmsg = ir.READ_COL(mf, var.STR(rfn.msg_field))
ftmsg = ir.READ_COL(mfr, var.STR(rfn.msg_field))
ftdst = ir.SPMV(inc_var, ftmsg)
ir.WRITE_COL_(out, var.STR(rfn.out_field), ftdst)
......@@ -147,9 +147,9 @@ def build_adj_matrix_graph(graph):
A index for data shuffling due to sparse format change. Return None
if shuffle is not required.
"""
gi = graph._graph
_, shuffle_idx = gi.adjacency_matrix(False, F.cpu())
return lambda ctx : gi.adjacency_matrix(False, ctx)[0], shuffle_idx
gidx = graph._graph
_, shuffle_idx = gidx.adjacency_matrix(False, F.cpu())
return lambda ctx: gidx.adjacency_matrix(False, ctx)[0], shuffle_idx
def _build_adj_matrix_index_uv(graph, edges, reduce_nodes):
"""Build adj matrix index and shape using the given (u, v) edges.
......@@ -180,7 +180,7 @@ def _build_adj_matrix_index_uv(graph, edges, reduce_nodes):
The dense shape.
"""
# TODO(minjie): add node frontier for this
new2old, old2new = utils.build_relabel_map(reduce_nodes, sorted=True)
_, old2new = utils.build_relabel_map(reduce_nodes, is_sorted=True)
u, v = edges
u = u.tousertensor()
v = v.tousertensor()
......@@ -218,13 +218,13 @@ def build_adj_matrix_uv(graph, edges, reduce_nodes):
if shuffle is not required.
"""
sp_idx, shape = _build_adj_matrix_index_uv(graph, edges, reduce_nodes)
u, v = edges
u, _ = edges
nnz = len(u)
# FIXME(minjie): data type
dat = F.ones((nnz,), dtype=F.float32, ctx=F.cpu())
mat, shuffle_idx = F.sparse_matrix(dat, sp_idx, shape)
shuffle_idx = utils.toindex(shuffle_idx) if shuffle_idx is not None else None
return utils.CtxCachedObject(lambda ctx : F.copy_to(mat, ctx)), shuffle_idx
return utils.CtxCachedObject(lambda ctx: F.copy_to(mat, ctx)), shuffle_idx
def build_inc_matrix_graph(graph):
"""Build incidence matrix.
......@@ -242,16 +242,16 @@ def build_inc_matrix_graph(graph):
A index for data shuffling due to sparse format change. Return None
if shuffle is not required.
"""
gi = graph._graph
gidx = graph._graph
# inc mat will not use data tensor so conversion index is not needed
return lambda ctx : gi.incidence_matrix('in', ctx)[0], None
return lambda ctx: gidx.incidence_matrix('in', ctx)[0], None
def build_inc_matrix_eid(m, eid, dst, reduce_nodes):
"""Build incidence matrix using edge id and edge dst nodes.
The incidence matrix is of shape (n, m), where n=len(reduce_nodes).
The nnz is equal to len(eid).
Invariant: len(eid) == len(dst)
The dst nodes will be sorted in the *unique-ascending* order of
......@@ -296,7 +296,7 @@ def build_inc_matrix_eid(m, eid, dst, reduce_nodes):
A index for data shuffling due to sparse format change. Return None
if shuffle is not required.
"""
new2old, old2new = utils.build_relabel_map(reduce_nodes, sorted=True)
_, old2new = utils.build_relabel_map(reduce_nodes, is_sorted=True)
dst = dst.tousertensor()
eid = eid.tousertensor()
# relabel edges dsts
......@@ -311,7 +311,7 @@ def build_inc_matrix_eid(m, eid, dst, reduce_nodes):
dat = F.ones((nnz,), dtype=F.float32, ctx=F.cpu())
mat, _ = F.sparse_matrix(dat, ('coo', idx), (n, m))
# inc mat will not use data tensor so conversion index is not needed
return utils.CtxCachedObject(lambda ctx : F.copy_to(mat, ctx)), None
return utils.CtxCachedObject(lambda ctx: F.copy_to(mat, ctx)), None
def build_inc_matrix_dst(dst, reduce_nodes):
"""Build incidence matrix using only edge destinations.
......@@ -332,7 +332,7 @@ def build_inc_matrix_dst(dst, reduce_nodes):
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 1]], shape=(5, 5))
Parameters
----------
dst : utils.Index
......
"""Class for subgraph data structure."""
from __future__ import absolute_import
import networkx as nx
from . import backend as F
from .frame import Frame, FrameRef
from .graph import DGLGraph
from . import utils
......@@ -47,22 +44,24 @@ class DGLSubGraph(DGLGraph):
def __init__(self, parent, parent_nid, parent_eid, graph_idx, shared=False):
super(DGLSubGraph, self).__init__(graph_data=graph_idx,
readonly=graph_idx.is_readonly())
if shared:
raise DGLError('Shared mode is not yet supported.')
self._parent = parent
self._parent_nid = parent_nid
self._parent_eid = parent_eid
# override APIs
def add_nodes(self, num, reprs=None):
def add_nodes(self, num, data=None):
"""Add nodes. Disabled because BatchedDGLGraph is read-only."""
raise RuntimeError('Readonly graph. Mutation is not allowed.')
raise DGLError('Readonly graph. Mutation is not allowed.')
def add_edge(self, u, v, reprs=None):
def add_edge(self, u, v, data=None):
"""Add one edge. Disabled because BatchedDGLGraph is read-only."""
raise RuntimeError('Readonly graph. Mutation is not allowed.')
raise DGLError('Readonly graph. Mutation is not allowed.')
def add_edges(self, u, v, reprs=None):
def add_edges(self, u, v, data=None):
"""Add many edges. Disabled because BatchedDGLGraph is read-only."""
raise RuntimeError('Readonly graph. Mutation is not allowed.')
raise DGLError('Readonly graph. Mutation is not allowed.')
@property
def parent_nid(self):
......@@ -110,10 +109,10 @@ class DGLSubGraph(DGLGraph):
If true, use inplace write (no gradient but faster)
"""
self._parent._node_frame.update_rows(
self._parent_nid, self._node_frame, inplace=inplace)
self._parent_nid, self._node_frame, inplace=inplace)
if self._parent._edge_frame.num_rows != 0:
self._parent._edge_frame.update_rows(
self._get_parent_eid(), self._edge_frame, inplace=inplace)
self._get_parent_eid(), self._edge_frame, inplace=inplace)
def copy_from_parent(self):
"""Copy node/edge features from the parent graph.
......
......@@ -9,7 +9,7 @@ __all__ = ['bfs_nodes_generator', 'bfs_edges_generator',
'topological_nodes_generator',
'dfs_edges_generator', 'dfs_labeled_edges_generator',]
def bfs_nodes_generator(graph, source, reversed=False):
def bfs_nodes_generator(graph, source, reverse=False):
"""Node frontiers generator using breadth-first search.
Parameters
......@@ -18,7 +18,7 @@ def bfs_nodes_generator(graph, source, reversed=False):
The graph object.
source : list, tensor of nodes
Source nodes.
reversed : bool, default False
reverse : bool, default False
If True, traverse following the in-edge direction.
Returns
......@@ -41,14 +41,14 @@ def bfs_nodes_generator(graph, source, reversed=False):
"""
ghandle = graph._graph._handle
source = utils.toindex(source)
ret = _CAPI_DGLBFSNodes(ghandle, source.todgltensor(), reversed)
ret = _CAPI_DGLBFSNodes(ghandle, source.todgltensor(), reverse)
all_nodes = utils.toindex(ret(0)).tousertensor()
# TODO(minjie): how to support directly creating python list
sections = utils.toindex(ret(1)).tonumpy().tolist()
node_frontiers = F.split(all_nodes, sections, dim=0)
return node_frontiers
def bfs_edges_generator(graph, source, reversed=False):
def bfs_edges_generator(graph, source, reverse=False):
"""Edges frontiers generator using breadth-first search.
Parameters
......@@ -57,7 +57,7 @@ def bfs_edges_generator(graph, source, reversed=False):
The graph object.
source : list, tensor of nodes
Source nodes.
reversed : bool, default False
reverse : bool, default False
If True, traverse following the in-edge direction.
Returns
......@@ -81,21 +81,21 @@ def bfs_edges_generator(graph, source, reversed=False):
"""
ghandle = graph._graph._handle
source = utils.toindex(source)
ret = _CAPI_DGLBFSEdges(ghandle, source.todgltensor(), reversed)
ret = _CAPI_DGLBFSEdges(ghandle, source.todgltensor(), reverse)
all_edges = utils.toindex(ret(0)).tousertensor()
# TODO(minjie): how to support directly creating python list
sections = utils.toindex(ret(1)).tonumpy().tolist()
edge_frontiers = F.split(all_edges, sections, dim=0)
return edge_frontiers
def topological_nodes_generator(graph, reversed=False):
def topological_nodes_generator(graph, reverse=False):
"""Node frontiers generator using topological traversal.
Parameters
----------
graph : DGLGraph
The graph object.
reversed : bool, optional
reverse : bool, optional
If True, traverse following the in-edge direction.
Returns
......@@ -117,13 +117,13 @@ def topological_nodes_generator(graph, reversed=False):
[tensor([0]), tensor([1]), tensor([2]), tensor([3, 4]), tensor([5])]
"""
ghandle = graph._graph._handle
ret = _CAPI_DGLTopologicalNodes(ghandle, reversed)
ret = _CAPI_DGLTopologicalNodes(ghandle, reverse)
all_nodes = utils.toindex(ret(0)).tousertensor()
# TODO(minjie): how to support directly creating python list
sections = utils.toindex(ret(1)).tonumpy().tolist()
return F.split(all_nodes, sections, dim=0)
def dfs_edges_generator(graph, source, reversed=False):
def dfs_edges_generator(graph, source, reverse=False):
"""Edge frontiers generator using depth-first-search (DFS).
Multiple source nodes can be specified to start the DFS traversal. One
......@@ -137,7 +137,7 @@ def dfs_edges_generator(graph, source, reversed=False):
The graph object.
source : list, tensor of nodes
Source nodes.
reversed : bool, optional
reverse : bool, optional
If True, traverse following the in-edge direction.
Returns
......@@ -162,7 +162,7 @@ def dfs_edges_generator(graph, source, reversed=False):
"""
ghandle = graph._graph._handle
source = utils.toindex(source)
ret = _CAPI_DGLDFSEdges(ghandle, source.todgltensor(), reversed)
ret = _CAPI_DGLDFSEdges(ghandle, source.todgltensor(), reverse)
all_edges = utils.toindex(ret(0)).tousertensor()
# TODO(minjie): how to support directly creating python list
sections = utils.toindex(ret(1)).tonumpy().tolist()
......@@ -171,7 +171,7 @@ def dfs_edges_generator(graph, source, reversed=False):
def dfs_labeled_edges_generator(
graph,
source,
reversed=False,
reverse=False,
has_reverse_edge=False,
has_nontree_edge=False,
return_labels=True):
......@@ -199,7 +199,7 @@ def dfs_labeled_edges_generator(
The graph object.
source : list, tensor of nodes
Source nodes.
reversed : bool, optional
reverse : bool, optional
If true, traverse following the in-edge direction.
has_reverse_edge : bool, optional
True to include reverse edges.
......@@ -234,12 +234,12 @@ def dfs_labeled_edges_generator(
ghandle = graph._graph._handle
source = utils.toindex(source)
ret = _CAPI_DGLDFSLabeledEdges(
ghandle,
source.todgltensor(),
reversed,
has_reverse_edge,
has_nontree_edge,
return_labels)
ghandle,
source.todgltensor(),
reverse,
has_reverse_edge,
has_nontree_edge,
return_labels)
all_edges = utils.toindex(ret(0)).tousertensor()
# TODO(minjie): how to support directly creating python list
if return_labels:
......
"""User-defined function related data structures."""
from __future__ import absolute_import
from .base import ALL, is_all
from .base import is_all
from . import backend as F
from . import utils
......
"""Utility module."""
from __future__ import absolute_import, division
from collections import Mapping, Iterable
from collections.abc import Mapping, Iterable
from functools import wraps
import numpy as np
......@@ -43,7 +43,7 @@ class Index(object):
def _dispatch(self, data):
"""Store data based on its type."""
if F.is_tensor(data):
if not (F.dtype(data) == F.int64):
if F.dtype(data) != F.int64:
raise DGLError('Index data must be an int64 vector, but got: %s' % str(data))
if len(F.shape(data)) > 1:
raise DGLError('Index data must be 1D int64 vector, but got: %s' % str(data))
......@@ -63,19 +63,17 @@ class Index(object):
self._slice_data = slice(data.start, data.stop)
else:
try:
self._pydata = np.array([int(data)]).astype(np.int64)
except:
try:
data = np.array(data).astype(np.int64)
if data.ndim != 1:
raise DGLError('Index data must be 1D int64 vector,'
' but got: %s' % str(data))
self._pydata = data
except:
raise DGLError('Error index data: %s' % str(data))
data = np.array(data).astype(np.int64)
except Exception: # pylint: disable=broad-except
raise DGLError('Error index data: %s' % str(data))
if data.ndim == 0: # scalar array
data = np.expand_dims(data, 0)
elif data.ndim != 1:
raise DGLError('Index data must be 1D int64 vector,'
' but got: %s' % str(data))
self._pydata = data
self._user_tensor_data[F.cpu()] = F.zerocopy_from_numpy(self._pydata)
def tonumpy(self):
"""Convert to a numpy ndarray."""
if self._pydata is None:
......@@ -96,8 +94,8 @@ class Index(object):
if len(self._user_tensor_data) == 0:
if self._dgl_tensor_data is not None:
# zero copy from dgl tensor
dl = self._dgl_tensor_data.to_dlpack()
self._user_tensor_data[F.cpu()] = F.zerocopy_from_dlpack(dl)
dlpack = self._dgl_tensor_data.to_dlpack()
self._user_tensor_data[F.cpu()] = F.zerocopy_from_dlpack(dlpack)
else:
# zero copy from numpy array
self._user_tensor_data[F.cpu()] = F.zerocopy_from_numpy(self.tonumpy())
......@@ -112,10 +110,17 @@ class Index(object):
if self._dgl_tensor_data is None:
# zero copy from user tensor
tsor = self.tousertensor()
dl = F.zerocopy_to_dlpack(tsor)
self._dgl_tensor_data = nd.from_dlpack(dl)
dlpack = F.zerocopy_to_dlpack(tsor)
self._dgl_tensor_data = nd.from_dlpack(dlpack)
return self._dgl_tensor_data
def slice_data(self):
"""Return the internal slice data.
If this index is not initialized from slice, the return will be None.
"""
return self._slice_data
def is_slice(self, start, stop):
"""Check if Index wraps a slice data with given start and stop"""
return self._slice_data == slice(start, stop)
......@@ -136,20 +141,26 @@ class Index(object):
Returns
-------
utils.Index
The values at the given position.
"""
if index._slice_data is None:
if self._slice_data is not None and self._slice_data.start == 0:
# short-cut for identical mapping
# NOTE: we don't check for out-of-bound error
return index
elif index._slice_data is None:
# the provided index is not a slice
tensor = self.tousertensor()
index = index.tousertensor()
return Index(F.gather_row(tensor, index))
elif self._slice_data is None:
# the current index is not a slice but the provided is a slice
tensor = self.tousertensor()
index = index._slice_data
return Index(F.narrow_row(tensor, index.start, index.stop))
else:
# both self and index wrap a slice object, then return another
# Index wrapping a slice
start = self._slicedata.start
start = self._slice_data.start
index = index._slice_data
return Index(slice(start + index.start, start + index.stop))
......@@ -168,7 +179,7 @@ class Index(object):
Returns
-------
utils.Index
The new values.
"""
tensor = self.tousertensor()
index = index.tousertensor()
......@@ -207,8 +218,24 @@ class Index(object):
tensor = self.tousertensor()
return F.sum(tensor, 0) > 0
def toindex(x):
return x if isinstance(x, Index) else Index(x)
def toindex(data):
"""Convert the given data to Index object.
Parameters
----------
data : index data
Data to create the index.
Returns
-------
Index
The index object.
See Also
--------
Index
"""
return data if isinstance(data, Index) else Index(data)
def zero_index(size):
"""Create a index with provided size initialized to zero
......@@ -244,21 +271,22 @@ class LazyDict(Mapping):
class HybridDict(Mapping):
"""A readonly dictonary that merges several dict-like (python dict, LazyDict).
If there are duplicate keys, early keys have priority over latter ones
If there are duplicate keys, early keys have priority over latter ones.
"""
def __init__(self, *dict_like_list):
self._dict_like_list = dict_like_list
self._keys = set()
for d in dict_like_list:
self._keys.update(d.keys())
for obj in dict_like_list:
self._keys.update(obj.keys())
def keys(self):
return self._keys
def __getitem__(self, key):
for d in self._dict_like_list:
if key in d:
return d[key]
for obj in self._dict_like_list:
if key in obj:
return obj[key]
raise KeyError(key)
def __contains__(self, key):
......@@ -290,7 +318,7 @@ class ReadOnlyDict(Mapping):
def __len__(self):
return len(self._dict_like)
def build_relabel_map(x, sorted=False):
def build_relabel_map(x, is_sorted=False):
"""Relabel the input ids to continuous ids that starts from zero.
Ids are assigned new ids according to their ascending order.
......@@ -310,7 +338,7 @@ def build_relabel_map(x, sorted=False):
----------
x : Index
The input ids.
sorted : bool, default=False
is_sorted : bool, default=False
Whether the input has already been unique and sorted.
Returns
......@@ -323,7 +351,7 @@ def build_relabel_map(x, sorted=False):
new id tensor: new_id = old_to_new[old_id]
"""
x = x.tousertensor()
if not sorted:
if not is_sorted:
unique_x, _ = F.sort_1d(F.unique(x))
else:
unique_x = x
......@@ -397,6 +425,7 @@ def cached_member(cache, prefix):
return _creator
def is_dict_like(obj):
"""Return true if the object can be treated as a dictionary."""
return isinstance(obj, Mapping)
def reorder(dict_like, index):
......
"""Views of DGLGraph."""
from __future__ import absolute_import
from collections import MutableMapping, namedtuple
from collections import namedtuple
from collections.abc import MutableMapping
from .base import ALL, is_all, DGLError
from . import backend as F
from . import utils
NodeSpace = namedtuple('NodeSpace', ['data'])
......@@ -41,6 +41,12 @@ class NodeView(object):
return F.arange(0, len(self))
class NodeDataView(MutableMapping):
"""The data view class when G.nodes[...].data is called.
See Also
--------
dgl.DGLGraph.nodes
"""
__slots__ = ['_graph', '_nodes']
def __init__(self, graph, nodes):
......@@ -103,6 +109,12 @@ class EdgeView(object):
return self._graph.all_edges(*args, **kwargs)
class EdgeDataView(MutableMapping):
"""The data view class when G.edges[...].data is called.
See Also
--------
dgl.DGLGraph.edges
"""
__slots__ = ['_graph', '_edges']
def __init__(self, graph, edges):
......
......@@ -145,10 +145,11 @@ def test_create_from_elist():
for i, (u, v) in enumerate(elist):
assert g.edge_id(u, v)[0] == i
# immutable graph
g = create_graph_index(elist, readonly=True)
for i, (u, v) in enumerate(elist):
print(u, v, g.edge_id(u, v)[0])
assert g.edge_id(u, v)[0] == i
# TODO: disabled due to torch support
#g = create_graph_index(elist, readonly=True)
#for i, (u, v) in enumerate(elist):
# print(u, v, g.edge_id(u, v)[0])
# assert g.edge_id(u, v)[0] == i
if __name__ == '__main__':
test_edge_id()
......
[MASTER]
# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code.
extension-pkg-whitelist=
# Add files or directories to the blacklist. They should be base names, not
# paths.
ignore=CVS,_cy2,_cy3,backend,data,nn,contrib
# Add files or directories matching the regex patterns to the blacklist. The
# regex matches against base names, not paths.
ignore-patterns=
# Python code to execute, usually for sys.path manipulation such as
# pygtk.require().
#init-hook=
# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the
# number of processors available to use.
jobs=4
# Control the amount of potential inferred values when inferring a single
# object. This can help the performance when dealing with large functions or
# complex, nested conditions.
limit-inference-results=100
# List of plugins (as comma separated values of python modules names) to load,
# usually to register additional checkers.
load-plugins=
# Pickle collected data for later comparisons.
persistent=yes
# Specify a configuration file.
#rcfile=
# When enabled, pylint would attempt to guess common misconfiguration and emit
# user-friendly hints instead of false-positive error messages.
suggestion-mode=yes
# Allow loading of arbitrary C extensions. Extensions are imported into the
# active Python interpreter and may run arbitrary code.
unsafe-load-any-extension=no
[MESSAGES CONTROL]
# Only show warnings with the listed confidence levels. Leave empty to show
# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED.
confidence=
# Disable the message, report, category or checker with the given id(s). You
# can either give multiple identifiers separated by comma (,) or put this
# option multiple times (only on the command line, not in the configuration
# file where it should appear only once). You can also use "--disable=all" to
# disable everything first and then reenable specific checks. For example, if
# you want to run only the similarities checker, you can use "--disable=all
# --enable=similarities". If you want to run only the classes checker, but have
# no Warning level messages displayed, use "--disable=all --enable=classes
# --disable=W".
disable=design,
similarities,
no-self-use,
attribute-defined-outside-init,
locally-disabled,
star-args,
pointless-except,
bad-option-value,
global-statement,
fixme,
suppressed-message,
useless-suppression,
locally-enabled,
import-error,
unsubscriptable-object,
unbalanced-tuple-unpacking,
protected-access,
useless-object-inheritance,
no-else-return,
len-as-condition,
cyclic-import, # disabled due to the inevitable dgl.graph -> dgl.subgraph loop
undefined-variable, # disabled due to C extension (should enable)
# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
# multiple time (only on the command line, not in the configuration file where
# it should appear only once). See also the "--disable" option for examples.
enable=c-extension-no-member
[REPORTS]
# Python expression which should return a note less than 10 (10 is the highest
# note). You have access to the variables errors warning, statement which
# respectively contain the number of errors / warnings messages and the total
# number of statements analyzed. This is used by the global evaluation report
# (RP0004).
evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10)
# Template used to display messages. This is a python new-style format string
# used to format the message information. See doc for all details.
#msg-template=
# Set the output format. Available formats are text, parseable, colorized, json
# and msvs (visual studio). You can also give a reporter class, e.g.
# mypackage.mymodule.MyReporterClass.
output-format=text
# Tells whether to display a full report or only the messages.
reports=no
# Activate the evaluation score.
score=yes
[REFACTORING]
# Maximum number of nested blocks for function / method body
max-nested-blocks=5
# Complete name of functions that never returns. When checking for
# inconsistent-return-statements if a never returning function is called then
# it will be considered as an explicit return statement and no message will be
# printed.
never-returning-functions=sys.exit
[MISCELLANEOUS]
# List of note tags to take in consideration, separated by a comma.
notes=FIXME,
XXX,
TODO
[BASIC]
# Naming style matching correct argument names.
argument-naming-style=snake_case
# Regular expression matching correct argument names. Overrides argument-
# naming-style.
#argument-rgx=
# Naming style matching correct attribute names.
attr-naming-style=snake_case
# Regular expression matching correct attribute names. Overrides attr-naming-
# style.
#attr-rgx=
# Bad variable names which should always be refused, separated by a comma.
bad-names=foo,
bar,
baz,
toto,
tutu,
tata
# Naming style matching correct class attribute names.
class-attribute-naming-style=any
# Regular expression matching correct class attribute names. Overrides class-
# attribute-naming-style.
#class-attribute-rgx=
# Naming style matching correct class names.
class-naming-style=PascalCase
# Regular expression matching correct class names. Overrides class-naming-
# style.
#class-rgx=
# Naming style matching correct constant names.
const-naming-style=UPPER_CASE
# Regular expression matching correct constant names. Overrides const-naming-
# style.
#const-rgx=
# Minimum line length for functions/classes that require docstrings, shorter
# ones are exempt.
docstring-min-length=-1
# Naming style matching correct function names.
function-naming-style=snake_case
# Regular expression matching correct function names. Overrides function-
# naming-style.
#function-rgx=
# Good variable names which should always be accepted, separated by a comma.
good-names=i,j,k,u,v,e,n,m,w,x,y,g,fn,ex,Run,_
# Include a hint for the correct naming format with invalid-name.
include-naming-hint=no
# Naming style matching correct inline iteration names.
inlinevar-naming-style=any
# Regular expression matching correct inline iteration names. Overrides
# inlinevar-naming-style.
#inlinevar-rgx=
# Naming style matching correct method names.
method-naming-style=snake_case
# Regular expression matching correct method names. Overrides method-naming-
# style.
#method-rgx=
# Naming style matching correct module names.
module-naming-style=snake_case
# Regular expression matching correct module names. Overrides module-naming-
# style.
#module-rgx=
# Colon-delimited sets of names that determine each other's naming style when
# the name regexes allow several styles.
name-group=
# Regular expression which should only match function or class names that do
# not require a docstring.
no-docstring-rgx=^_
# List of decorators that produce properties, such as abc.abstractproperty. Add
# to this list to register other decorators that produce valid properties.
# These decorators are taken in consideration only for invalid-name.
property-classes=abc.abstractproperty
# Naming style matching correct variable names.
variable-naming-style=snake_case
# Regular expression matching correct variable names. Overrides variable-
# naming-style.
#variable-rgx=
[VARIABLES]
# List of additional names supposed to be defined in builtins. Remember that
# you should avoid defining new builtins when possible.
additional-builtins=
# Tells whether unused global variables should be treated as a violation.
allow-global-unused-variables=yes
# List of strings which can identify a callback function by name. A callback
# name must start or end with one of those strings.
callbacks=cb_,
_cb
# A regular expression matching the name of dummy variables (i.e. expected to
# not be used).
dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_
# Argument names that match this expression will be ignored. Default to name
# with leading underscore.
ignored-argument-names=_.*|^ignored_|^unused_
# Tells whether we should check for unused import in __init__ files.
init-import=no
# List of qualified module names which can have objects that can redefine
# builtins.
redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io
[SPELLING]
# Limits count of emitted suggestions for spelling mistakes.
max-spelling-suggestions=4
# Spelling dictionary name. Available dictionaries: none. To make it working
# install python-enchant package..
spelling-dict=
# List of comma separated words that should not be checked.
spelling-ignore-words=
# A path to a file that contains private dictionary; one word per line.
spelling-private-dict-file=
# Tells whether to store unknown words to indicated private dictionary in
# --spelling-private-dict-file option instead of raising a message.
spelling-store-unknown-words=no
[LOGGING]
# Format style used to check logging format string. `old` means using %
# formatting, while `new` is for `{}` formatting.
logging-format-style=old
# Logging modules to check that the string format arguments are in logging
# function parameter format.
logging-modules=logging
[FORMAT]
# Expected format of line ending, e.g. empty (any line ending), LF or CRLF.
expected-line-ending-format=
# Regexp for a line that is allowed to be longer than the limit.
ignore-long-lines=^\s*(# )?<?https?://\S+>?$
# Number of spaces of indent required inside a hanging or continued line.
indent-after-paren=4
# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1
# tab).
indent-string=' '
# Maximum number of characters on a single line.
max-line-length=100
# Maximum number of lines in a module.
max-module-lines=4000
# List of optional constructs for which whitespace checking is disabled. `dict-
# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}.
# `trailing-comma` allows a space between comma and closing bracket: (a, ).
# `empty-line` allows space-only lines.
no-space-check=trailing-comma,
dict-separator
# Allow the body of a class to be on the same line as the declaration if body
# contains single statement.
single-line-class-stmt=no
# Allow the body of an if to be on the same line as the test if there is no
# else.
single-line-if-stmt=no
[SIMILARITIES]
# Ignore comments when computing similarities.
ignore-comments=yes
# Ignore docstrings when computing similarities.
ignore-docstrings=yes
# Ignore imports when computing similarities.
ignore-imports=no
# Minimum lines number of a similarity.
min-similarity-lines=4
[TYPECHECK]
# List of decorators that produce context managers, such as
# contextlib.contextmanager. Add to this list to register other decorators that
# produce valid context managers.
contextmanager-decorators=contextlib.contextmanager
# List of members which are set dynamically and missed by pylint inference
# system, and so shouldn't trigger E1101 when accessed. Python regular
# expressions are accepted.
generated-members=
# Tells whether missing members accessed in mixin class should be ignored. A
# mixin class is detected if its name ends with "mixin" (case insensitive).
ignore-mixin-members=yes
# Tells whether to warn about missing members when the owner of the attribute
# is inferred to be None.
ignore-none=yes
# This flag controls whether pylint should warn about no-member and similar
# checks whenever an opaque object is returned when inferring. The inference
# can return multiple potential results while evaluating a Python object, but
# some branches might not be evaluated, which results in partial inference. In
# that case, it might be useful to still emit no-member and other checks for
# the rest of the inferred objects.
ignore-on-opaque-inference=yes
# List of class names for which member attributes should not be checked (useful
# for classes with dynamically set attributes). This supports the use of
# qualified names.
ignored-classes=optparse.Values,thread._local,_thread._local
# List of module names for which member attributes should not be checked
# (useful for modules/projects where namespaces are manipulated during runtime
# and thus existing member attributes cannot be deduced by static analysis. It
# supports qualified module names, as well as Unix pattern matching.
ignored-modules=dgl.backend,dgl._api_internal
# Show a hint with possible names when a member name was not found. The aspect
# of finding the hint is based on edit distance.
missing-member-hint=yes
# The minimum edit distance a name should have in order to be considered a
# similar match for a missing member name.
missing-member-hint-distance=1
# The total number of similar names that should be taken in consideration when
# showing a hint for a missing member.
missing-member-max-choices=1
[IMPORTS]
# Allow wildcard imports from modules that define __all__.
allow-wildcard-with-all=yes
# Analyse import fallback blocks. This can be used to support both Python 2 and
# 3 compatible code, which means that the block might have code that exists
# only in one or another interpreter, leading to false positives when analysed.
analyse-fallback-blocks=no
# Deprecated modules which should not be used, separated by a comma.
deprecated-modules=optparse,tkinter.tix
# Create a graph of external dependencies in the given file (report RP0402 must
# not be disabled).
ext-import-graph=
# Create a graph of every (i.e. internal and external) dependencies in the
# given file (report RP0402 must not be disabled).
import-graph=
# Create a graph of internal dependencies in the given file (report RP0402 must
# not be disabled).
int-import-graph=
# Force import order to recognize a module as part of the standard
# compatibility libraries.
known-standard-library=
# Force import order to recognize a module as part of a third party library.
known-third-party=enchant
[DESIGN]
# Maximum number of arguments for function / method.
max-args=5
# Maximum number of attributes for a class (see R0902).
max-attributes=7
# Maximum number of boolean expressions in an if statement.
max-bool-expr=5
# Maximum number of branch for function / method body.
max-branches=12
# Maximum number of locals for function / method body.
max-locals=15
# Maximum number of parents for a class (see R0901).
max-parents=7
# Maximum number of public methods for a class (see R0904).
max-public-methods=20
# Maximum number of return / yield for function / method body.
max-returns=6
# Maximum number of statements in function / method body.
max-statements=50
# Minimum number of public methods for a class (see R0903).
min-public-methods=2
[CLASSES]
# List of method names used to declare (i.e. assign) instance attributes.
defining-attr-methods=__init__,
__new__,
setUp
# List of member names, which should be excluded from the protected access
# warning.
exclude-protected=_asdict,
_fields,
_replace,
_source,
_make
# List of valid names for the first argument in a class method.
valid-classmethod-first-arg=cls
# List of valid names for the first argument in a metaclass class method.
valid-metaclass-classmethod-first-arg=cls
[EXCEPTIONS]
# Exceptions that will emit a warning when being caught. Defaults to
# "Exception".
overgeneral-exceptions=Exception
......@@ -4,8 +4,7 @@ import mxnet as mx
import numpy as np
import scipy as sp
import dgl
from dgl.graph import GraphIndex, create_graph_index
from dgl.graph_index import map_to_subgraph_nid
from dgl.graph_index import map_to_subgraph_nid, GraphIndex, create_graph_index
from dgl import utils
def generate_rand_graph(n):
......
......@@ -596,11 +596,12 @@ def test_repr():
G.add_nodes(10)
G.add_edge(0, 1)
repr_string = G.__repr__()
print(repr_string)
G.ndata['x'] = th.zeros((10, 5))
G.add_edges([0, 1], 2)
G.edata['y'] = th.zeros((3, 4))
repr_string = G.__repr__()
print(repr_string)
if __name__ == '__main__':
test_nx_conversion()
......
......@@ -61,7 +61,7 @@ def test_column1():
def test_column2():
# Test frameref column getter/setter
data = Frame(create_test_data())
f = FrameRef(data, [3, 4, 5, 6, 7])
f = FrameRef(data, toindex([3, 4, 5, 6, 7]))
assert f.num_rows == 5
assert len(f) == 3
assert U.allclose(f['a1'], data['a1'].data[3:8])
......@@ -111,7 +111,7 @@ def test_append2():
assert not f.is_span_whole_column()
assert f.num_rows == 3 * N
new_idx = list(range(N)) + list(range(2*N, 4*N))
assert th.all(f.index().tousertensor() == th.tensor(new_idx, dtype=th.int64))
assert th.all(f._index.tousertensor() == th.tensor(new_idx, dtype=th.int64))
assert data.num_rows == 4 * N
def test_append3():
......@@ -233,8 +233,8 @@ def test_row4():
def test_sharing():
data = Frame(create_test_data())
f1 = FrameRef(data, index=[0, 1, 2, 3])
f2 = FrameRef(data, index=[2, 3, 4, 5, 6])
f1 = FrameRef(data, index=toindex([0, 1, 2, 3]))
f2 = FrameRef(data, index=toindex([2, 3, 4, 5, 6]))
# test read
for k, v in f1.items():
assert U.allclose(data[k].data[0:4], v)
......@@ -260,8 +260,8 @@ def test_sharing():
def test_slicing():
data = Frame(create_test_data(grad=True))
f1 = FrameRef(data, index=slice(1, 5))
f2 = FrameRef(data, index=slice(3, 8))
f1 = FrameRef(data, index=toindex(slice(1, 5)))
f2 = FrameRef(data, index=toindex(slice(3, 8)))
# test read
for k, v in f1.items():
assert U.allclose(data[k].data[1:5], v)
......@@ -279,15 +279,15 @@ def test_slicing():
'a2': th.ones([2, D]),
'a3': th.ones([2, D]),
}
f2_a1[0:2] = 1
f2_a1[toindex(slice(0,2))] = 1
assert U.allclose(f2['a1'], f2_a1)
f1[2:4] = {
f1[toindex(slice(2,4))] = {
'a1': th.zeros([2, D]),
'a2': th.zeros([2, D]),
'a3': th.zeros([2, D]),
}
f2_a1[0:2] = 0
f2_a1[toindex(slice(0,2))] = 0
assert U.allclose(f2['a1'], f2_a1)
def test_add_rows():
......@@ -299,12 +299,48 @@ def test_add_rows():
ans = th.cat([x, th.zeros(3, 4)])
assert U.allclose(f1['x'], ans)
f1.add_rows(4)
f1[4:8] = {'x': th.ones(4, 4), 'y': th.ones(4, 5)}
f1[toindex(slice(4,8))] = {'x': th.ones(4, 4), 'y': th.ones(4, 5)}
ans = th.cat([ans, th.ones(4, 4)])
assert U.allclose(f1['x'], ans)
ans = th.cat([th.zeros(4, 5), th.ones(4, 5)])
assert U.allclose(f1['y'], ans)
def test_inplace():
f = FrameRef(Frame(create_test_data()))
print(f.schemes)
a1addr = f['a1'].data.data_ptr()
a2addr = f['a2'].data.data_ptr()
a3addr = f['a3'].data.data_ptr()
# column updates are always out-of-place
f['a1'] = th.ones((N, D))
newa1addr = f['a1'].data.data_ptr()
assert a1addr != newa1addr
a1addr = newa1addr
# full row update that becomes column update
f[toindex(slice(0, N))] = {'a1' : th.ones((N, D))}
assert f['a1'].data.data_ptr() != a1addr
# row update (outplace) w/ slice
f[toindex(slice(1, 4))] = {'a2' : th.ones((3, D))}
newa2addr = f['a2'].data.data_ptr()
assert a2addr != newa2addr
a2addr = newa2addr
# row update (outplace) w/ list
f[toindex([1, 3, 5])] = {'a2' : th.ones((3, D))}
newa2addr = f['a2'].data.data_ptr()
assert a2addr != newa2addr
a2addr = newa2addr
# row update (inplace) w/ slice
f.update_data(toindex(slice(1, 4)), {'a2' : th.ones((3, D))}, True)
newa2addr = f['a2'].data.data_ptr()
assert a2addr == newa2addr
# row update (inplace) w/ list
f.update_data(toindex([1, 3, 5]), {'a2' : th.ones((3, D))}, True)
newa2addr = f['a2'].data.data_ptr()
assert a2addr == newa2addr
if __name__ == '__main__':
test_create()
test_column1()
......@@ -319,3 +355,4 @@ if __name__ == '__main__':
test_sharing()
test_slicing()
test_add_rows()
test_inplace()
......@@ -33,9 +33,10 @@ def test_create_from_elist():
for i, (u, v) in enumerate(elist):
assert g.edge_id(u, v) == i
# immutable graph
g = dgl.DGLGraph(elist, readonly=True)
for i, (u, v) in enumerate(elist):
assert g.edge_id(u, v) == i
# XXX: not enabled for pytorch
#g = dgl.DGLGraph(elist, readonly=True)
#for i, (u, v) in enumerate(elist):
# assert g.edge_id(u, v) == i
def test_adjmat_cache():
n = 1000
......@@ -109,7 +110,7 @@ def test_incmat_cache():
assert dur2 < dur1
assert id(inc1) == id(inc2)
# different arg should result in different cache
inc3 = g.incidence_matrix(type="both")
inc3 = g.incidence_matrix("both")
assert id(inc3) != id(inc2)
# manually clear the cache
g.clear_cache()
......
......@@ -112,7 +112,7 @@ def test_pickling_graph():
assert new_g._message_func == _global_message_func
assert isinstance(new_g._reduce_func, type(reduce_func))
assert new_g._reduce_func._name == 'sum'
assert new_g._reduce_func.op == backend.sum
assert new_g._reduce_func.reduce_op == backend.sum
assert new_g._reduce_func.msg_field == 'x'
assert new_g._reduce_func.out_field == 'x'
......
......@@ -3,3 +3,7 @@
# cpplint
echo 'Checking code style of C++ codes...'
python3 third_party/dmlc-core/scripts/lint.py dgl cpp include src
# pylint
echo 'Checking code style of python codes...'
python3 -m pylint --reports=y -v --rcfile=tests/lint/pylintrc python/dgl
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment