Skip to content

Commit

Permalink
revise
Browse files Browse the repository at this point in the history
  • Loading branch information
acezen committed Jun 1, 2021
1 parent 39c0ce3 commit a54296c
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,9 @@ class ClosenessCentrality
distv = ctx.length[tid][v];
double edata = 1.0;
static_if<!std::is_same<edata_t, grape::EmptyType>{}>(
[&](auto& data, auto& use_edata, auto& e) {
if (use_edata) {
data = static_cast<double>(e.get_data());
}
})(edata, ctx.use_edata, e);
[&](auto& e, auto& data) {
data = static_cast<double>(e.get_data());
})(e, edata);
ndistv = distu + edata;
if (distv > ndistv) {
ctx.length[tid][v] = ndistv;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ class ClosenessCentralityContext
: grape::VertexDataContext<FRAG_T, double>(fragment),
centrality(this->data()) {}

void Init(grape::ParallelMessageManager& messages, bool use_edata_or_not,
bool wf) {
void Init(grape::ParallelMessageManager& messages, bool wf) {
auto& frag = this->fragment();
auto vertices = frag.Vertices();
wf_improve = wf;
use_edata = use_edata_or_not;
centrality.SetValue(0.0);
}

Expand All @@ -57,7 +55,6 @@ class ClosenessCentralityContext
}

bool wf_improve; // use Wasserman-Faust improved formula.
bool use_edata;
std::vector<typename FRAG_T::template vertex_array_t<double>> length;
typename FRAG_T::template vertex_array_t<double>& centrality;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,9 @@ class EigenvectorCentrality
for (auto& e : es) {
double edata = 1.0;
static_if<!std::is_same<edata_t, grape::EmptyType>{}>(
[&](auto& data, auto& use_edata, auto& e) {
if (use_edata) {
data = static_cast<double>(e.get_data());
}
})(edata, ctx.use_edata, e);
[&](auto& e, auto& data) {
data = static_cast<double>(e.get_data());
})(e, edata);
x[v] += x_last[e.get_neighbor()] * edata;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EigenvectorCentralityContext
x(this->data()) {}

void Init(grape::DefaultMessageManager& messages, double tolerance,
int max_round, bool use_edata_or_not) {
int max_round) {
auto& frag = this->fragment();
auto vertices = frag.Vertices();

Expand All @@ -43,7 +43,6 @@ class EigenvectorCentralityContext

this->tolerance = tolerance;
this->max_round = max_round;
this->use_edata = use_edata_or_not;
curr_round = 0;
}

Expand All @@ -62,7 +61,6 @@ class EigenvectorCentralityContext
double tolerance;
int max_round;
int curr_round;
bool use_edata;
};
} // namespace gs

Expand Down
8 changes: 3 additions & 5 deletions analytical_engine/apps/centrality/katz/katz_centrality.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,9 @@ class KatzCentrality : public AppBase<FRAG_T, KatzCentralityContext<FRAG_T>>,
// do the multiplication y^T = Alpha * x^T A - Beta
double edata = 1.0;
static_if<!std::is_same<edata_t, grape::EmptyType>{}>(
[&](auto& data, auto& use_edata, auto& e) {
if (use_edata) {
data = static_cast<double>(e.get_data());
}
})(edata, ctx.use_edata, e);
[&](auto& e, auto& data) {
data = static_cast<double>(e.get_data());
})(e, edata);
x[v] += x_last[e.get_neighbor()] * edata;
}
x[v] = x[v] * ctx.alpha + ctx.beta;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ class KatzCentralityContext : public grape::VertexDataContext<FRAG_T, double> {
x(this->data()) {}

void Init(grape::DefaultMessageManager& messages, double alpha, double beta,
double tolerance, int max_round, bool normalized,
bool use_edata_or_not) {
double tolerance, int max_round, bool normalized) {
auto& frag = this->fragment();
auto vertices = frag.Vertices();

Expand All @@ -46,7 +45,6 @@ class KatzCentralityContext : public grape::VertexDataContext<FRAG_T, double> {
this->tolerance = tolerance;
this->max_round = max_round;
this->normalized = normalized;
this->use_edata = use_edata_or_not;
curr_round = 0;
}

Expand All @@ -68,7 +66,6 @@ class KatzCentralityContext : public grape::VertexDataContext<FRAG_T, double> {
double total_sum;
int max_round;
bool normalized;
bool use_edata;
int curr_round;
};
} // namespace gs
Expand Down
13 changes: 4 additions & 9 deletions analytical_engine/apps/projected/sssp_projected.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ class SSSPProjectedContext : public grape::VertexDataContext<FRAG_T, double> {
: grape::VertexDataContext<FRAG_T, double>(fragment, true),
partial_result(this->data()) {}

void Init(grape::DefaultMessageManager& messages,
oid_t source_id_ bool use_edata_or_not) {
void Init(grape::DefaultMessageManager& messages, oid_t source_id_) {
auto& frag = this->fragment();
auto vertices = frag.Vertices();

source_id = source_id_;
use_edata = use_edata_or_not;
partial_result.SetValue(std::numeric_limits<double>::max());
modified.Init(vertices, false);
}
Expand All @@ -64,7 +62,6 @@ class SSSPProjectedContext : public grape::VertexDataContext<FRAG_T, double> {
typename FRAG_T::template vertex_array_t<double>& partial_result;
typename FRAG_T::template vertex_array_t<bool> modified;
oid_t source_id;
bool use_edata;
};

template <typename FRAG_T>
Expand Down Expand Up @@ -99,12 +96,10 @@ class SSSPProjected : public AppBase<FRAG_T, SSSPProjectedContext<FRAG_T>> {
v = e.neighbor();
distv = ctx.partial_result[v];
double edata = 1.0;
static_if<!std::is_same<edata_t, grape::EmptyType>{}>(
[&](auto& data, auto& use_edata, auto& e) {
if (use_edata) {
static_if<!std::is_same<edata_t, grape::EmptyType>{}>(
[&](auto& e, auto& data) {
data = static_cast<double>(e.get_data());
}
})(edata, ctx.use_edata, e);
})(e, edata);
ndistv = distu + edata;
if (distv > ndistv) {
ctx.partial_result[v] = ndistv;
Expand Down
13 changes: 11 additions & 2 deletions analytical_engine/apps/sssp/sssp_average_length.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class SSSPAverageLength
grape::LoadStrategy::kBothOutIn;
using vertex_t = typename fragment_t::vertex_t;
using vid_t = typename fragment_t::vid_t;
using edata_t = typename fragment_t::vid_t;
// vertex msg: [source, v, sssp_length]
// OR sum msg: [fid, fid, sssp_length_sum]
using tuple_t = typename std::tuple<vid_t, vid_t, double>;
Expand Down Expand Up @@ -194,7 +195,11 @@ class SSSPAverageLength
for (auto& e : oes) {
auto u = e.get_neighbor();
if (frag.IsOuterVertex(u)) {
double v_u = (ctx.weight) ? e.get_data() : 1;
double v_u = 1.0;
static_if<!std::is_same<edata_t, grape::EmptyType>{}>(
[&](auto& e, auto& data) {
data = static_cast<double>(e.get_data());
})(e, v_u);
double dist = ctx.path_distance[v][src_vid] + v_u;
vid_t u_vid = frag.Vertex2Gid(u);
messages.SendToFragment(
Expand Down Expand Up @@ -239,7 +244,11 @@ class SSSPAverageLength
for (auto& e : oes) {
auto u = e.get_neighbor();
if (frag.IsInnerVertex(u)) {
double v_u = (ctx.weight) ? e.get_data() : 1;
double v_u = 1.0;
static_if<!std::is_same<edata_t, grape::EmptyType>{}>(
[&](auto& e, auto& data) {
data = static_cast<double>(e.get_data());
})(e, v_u);
updateVertexState(u, src_vid, dist_v + v_u, ctx);
}
}
Expand Down
5 changes: 1 addition & 4 deletions analytical_engine/apps/sssp/sssp_average_length_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ class SSSPAverageLengthContext : public TensorContext<FRAG_T, double> {
explicit SSSPAverageLengthContext(const FRAG_T& fragment)
: TensorContext<FRAG_T, double>(fragment) {}

void Init(grape::DefaultMessageManager& messages, bool w) {
void Init(grape::DefaultMessageManager& messages) {
auto& frag = this->fragment();

weight = w;
inner_sum = 0.0;
path_distance.Init(frag.InnerVertices());
updated.Init(frag.InnerVertices());
Expand Down Expand Up @@ -72,8 +71,6 @@ class SSSPAverageLengthContext : public TensorContext<FRAG_T, double> {
#endif
}

bool weight;

// length sum of each fragment, only maintained by frag 0
std::map<fid_t, double> all_sums;

Expand Down
11 changes: 7 additions & 4 deletions analytical_engine/apps/sssp/sssp_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class SSSPPath : public AppBase<FRAG_T, SSSPPathContext<FRAG_T>>,
grape::LoadStrategy::kBothOutIn;
using vertex_t = typename fragment_t::vertex_t;
using vid_t = typename fragment_t::vid_t;
using edata_t = typename fragment_t::edata_t;
using pair_msg_t = typename std::pair<vid_t, double>;

void PEval(const fragment_t& frag, context_t& ctx,
Expand Down Expand Up @@ -149,10 +150,12 @@ class SSSPPath : public AppBase<FRAG_T, SSSPPathContext<FRAG_T>>,
for (auto& e : oes) {
auto u = e.get_neighbor();
double new_distu;
if (ctx.weight)
new_distu = ctx.path_distance[v] + e.get_data();
else
new_distu = ctx.path_distance[v] + 1;
double edata = 1.0;
static_if<!std::is_same<edata_t, grape::EmptyType>{}>(
[&](auto& e, auto& data) {
data = static_cast<double>(e.get_data());
})(e, edata);
new_distu = ctx.path_distance[v] + edata;
if (frag.IsOuterVertex(u)) {
messages.SyncStateOnOuterVertex<fragment_t, pair_msg_t>(
frag, u, std::make_pair(v_vid, new_distu));
Expand Down
4 changes: 1 addition & 3 deletions analytical_engine/apps/sssp/sssp_path_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ class SSSPPathContext : public TensorContext<FRAG_T, typename FRAG_T::oid_t> {
explicit SSSPPathContext(const FRAG_T& fragment)
: TensorContext<FRAG_T, typename FRAG_T::oid_t>(fragment) {}

void Init(grape::DefaultMessageManager& messages, oid_t source, bool w) {
void Init(grape::DefaultMessageManager& messages, oid_t source) {
auto& frag = this->fragment();

source_id = source;
weight = w;
predecessor.Init(frag.InnerVertices());
path_distance.Init(frag.InnerVertices(),
std::numeric_limits<double>::max());
Expand Down Expand Up @@ -68,7 +67,6 @@ class SSSPPathContext : public TensorContext<FRAG_T, typename FRAG_T::oid_t> {
}

oid_t source_id;
bool weight;

typename FRAG_T::template vertex_array_t<vertex_t> predecessor;
typename FRAG_T::template vertex_array_t<double> path_distance;
Expand Down
6 changes: 2 additions & 4 deletions python/graphscope/analytical/app/eigenvector_centrality.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

@project_to_simple
@not_compatible_for("arrow_property", "dynamic_property")
def eigenvector_centrality(graph, tolerance=1e-06, max_round=100, weight=True):
def eigenvector_centrality(graph, tolerance=1e-06, max_round=100):
"""Compute the eigenvector centrality for the `graph`.
See more about eigenvector centrality here:
https://networkx.org/documentation/networkx-1.10/reference/generated/networkx.algorithms.centrality.eigenvector_centrality.html
Expand All @@ -35,8 +35,6 @@ def eigenvector_centrality(graph, tolerance=1e-06, max_round=100, weight=True):
graph (:class:`Graph`): A projected simple graph.
tolerance (float, optional): Defaults to 1e-06.
max_round (int, optional): Defaults to 100.
weight (bool, optional): use edge data or not in, if False, all edge weights
are considered equal. Otherwise use the edge data.
Returns:
:class:`VertexDataContext`: A context with each vertex assigned with a gv-centrality.
Expand All @@ -55,4 +53,4 @@ def eigenvector_centrality(graph, tolerance=1e-06, max_round=100, weight=True):
"""
tolerance = float(tolerance)
max_round = int(max_round)
return AppAssets(algo="eigenvector_centrality")(graph, tolerance, max_round, weight)
return AppAssets(algo="eigenvector_centrality")(graph, tolerance, max_round)
4 changes: 0 additions & 4 deletions python/graphscope/analytical/app/katz_centrality.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def katz_centrality(
tolerance=1e-06,
max_round=100,
normalized=True,
weight=True,
):
"""Compute the Katz centrality.
Expand All @@ -47,8 +46,6 @@ def katz_centrality(
tolerance (float, optional): Error tolerance. Defaults to 1e-06.
max_round (int, optional): Maximun number of rounds. Defaults to 100.
normalized (bool, optional): Whether to normalize result values. Defaults to True.
weight (bool, optional): use edge data or not in, if False, all edge weights
are considered equal. Otherwise use the edge data.
Returns:
:class:`VertexDatacontext`: A context with each vertex assigned with the computed katz_centrality.
Expand Down Expand Up @@ -77,5 +74,4 @@ def katz_centrality(
tolerance,
max_round,
normalized,
weight,
)
1 change: 0 additions & 1 deletion python/graphscope/framework/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from io import BytesIO

import yaml
from decorator import decorator

import graphscope
from graphscope.framework.context import create_context
Expand Down
15 changes: 5 additions & 10 deletions python/graphscope/nx/algorithms/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ def wrapper(*args, **kwargs):
elif graph.graph_type == types_pb2.DYNAMIC_PROPERTY:
if "weight" in inspect.getargspec(func)[0]: # func has 'weight' argument
weight = kwargs.get("weight", None)
print(weight)
graph = graph._project_to_simple(e_prop=weight)
kwargs["weight"] = True if weight else False
else:
graph = graph._project_to_simple()
return func(graph, *args[1:], **kwargs)
Expand Down Expand Up @@ -251,9 +249,7 @@ def eigenvector_centrality(G, max_iter=100, tol=1e-06, weight=None):
eigenvector_centrality_numpy
hits
"""
ctx = graphscope.eigenvector_centrality(
G, tolerance=tol, max_round=max_iter, weight=weight
)
ctx = graphscope.eigenvector_centrality(G, tolerance=tol, max_round=max_iter)
return ctx.to_dataframe({"node": "v.id", "result": "r"})


Expand Down Expand Up @@ -342,7 +338,6 @@ def katz_centrality(
tolerance=tol,
max_round=max_iter,
normalized=normalized,
weight=weight,
)
return ctx.to_dataframe({"node": "v.id", "result": "r"})

Expand All @@ -367,7 +362,7 @@ def has_path(G, source, target):
@project_to_simple
@patch_docstring(nxa.shortest_path)
def shortest_path(G, source=None, target=None, weight=None):
return AppAssets(algo="sssp_path")(G, source, weight)
return AppAssets(algo="sssp_path")(G, source)


@project_to_simple
Expand Down Expand Up @@ -405,7 +400,7 @@ def single_source_dijkstra_path_length(G, source, weight=None):
Distances are calculated as sums of weighted edges traversed.
"""
ctx = AppAssets(algo="sssp_projected")(G, source, weight)
ctx = AppAssets(algo="sssp_projected")(G, source)
return ctx.to_dataframe({"node": "v.id", "result": "r"})


Expand Down Expand Up @@ -439,7 +434,7 @@ def average_shortest_path_length(G, weight=None):
2.0
"""
ctx = AppAssets(algo="sssp_average_length")(G, weight)
ctx = AppAssets(algo="sssp_average_length")(G)
return ctx.to_numpy("r", axis=0)[0]


Expand Down Expand Up @@ -564,7 +559,7 @@ def closeness_centrality(G, weight=None, wf_improved=True):
Social Network Analysis: Methods and Applications, 1994,
Cambridge University Press.
"""
ctx = AppAssets(algo="closeness_centrality")(G, weight, wf_improved)
ctx = AppAssets(algo="closeness_centrality")(G, wf_improved)
return ctx.to_dataframe({"node": "v.id", "result": "r"})


Expand Down

0 comments on commit a54296c

Please sign in to comment.