Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions graphblas_algorithms/algorithms/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ def single_clustering(G, node, *, weighted=False):
return 0
index = G._key_to_id[node]
# TODO: it would be nice if we could clean this up, but still be fast
if "degrees-" in G._cache:
# The cache keeps the degrees under the names their aliases resolve to.
if G._cache_aliases.get("degrees-", "degrees-") in G._cache:
degrees = G.get_property("degrees-").get(index)
elif "degrees+" in G._cache:
elif G._cache_aliases.get("degrees+", "degrees+") in G._cache:
degrees = G.get_property("degrees+").get(index)
if G.get_property("has_self_edges") and G._A.get(index, index) is not None:
degrees -= 1
Expand Down
24 changes: 13 additions & 11 deletions graphblas_algorithms/classes/_caching.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from graphblas import Scalar, dtypes, op
from graphblas import Scalar, dtypes, monoid, op
from graphblas.core import operator

NONNEGATIVE_DTYPES = {dtypes.BOOL, dtypes.UINT8, dtypes.UINT16, dtypes.UINT32, dtypes.UINT64}
Expand Down Expand Up @@ -99,6 +99,8 @@ def get_reduction(G, mask=None):
def get_reduce_to_scalar(key, opname):
op_ = op.from_string(opname)
op_, opclass = operator.find_opclass(op_)
# The reduction of the whole from those of the rows (or columns): counts add up.
parts_op = monoid.plus if opname == "count" else op_
keybase = key[:-1]
if key[-1] == "-":

Expand All @@ -108,13 +110,13 @@ def get_reduction(G, mask=None):
if cache.get("has_self_edges") is False and f"{keybase}+" in cache:
cache[key] = cache[f"{keybase}+"]
elif f"{opname}_rowwise-" in cache:
cache[key] = cache[f"{opname}_rowwise-"].reduce(op_).new(name=key)
cache[key] = cache[f"{opname}_rowwise-"].reduce(parts_op).new(name=key)
elif f"{opname}_columnwise-" in cache:
cache[key] = cache[f"{opname}_columnwise-"].reduce(op_).new(name=key)
cache[key] = cache[f"{opname}_columnwise-"].reduce(parts_op).new(name=key)
elif cache.get("has_self_edges") is False and f"{opname}_rowwise+" in cache:
cache[key] = cache[f"{opname}_rowwise+"].reduce(op_).new(name=key)
cache[key] = cache[f"{opname}_rowwise+"].reduce(parts_op).new(name=key)
elif cache.get("has_self_edges") is False and f"{opname}_columnwise+" in cache:
cache[key] = cache[f"{opname}_columnwise+"].reduce(op_).new(name=key)
cache[key] = cache[f"{opname}_columnwise+"].reduce(parts_op).new(name=key)
elif "offdiag" in cache:
cache[key] = cache["offdiag"].reduce_scalar(op_).new(name=key)
elif (
Expand All @@ -124,8 +126,8 @@ def get_reduction(G, mask=None):
and G.get_property("has_self_edges")
):
return op_(
cache["L-"].reduce(op_)._as_vector() | cache["U-"].reduce(op_)._as_vector()
)[0].new(name=key)
cache["L-"].reduce_scalar(op_).new() | cache["U-"].reduce_scalar(op_).new()
).new(name=key)
elif not G.get_property("has_self_edges"):
cache[key] = G.get_property(f"{keybase}+")
else:
Expand All @@ -149,13 +151,13 @@ def get_reduction(G, mask=None):
if cache.get("has_self_edges") is False and f"{keybase}-" in cache:
cache[key] = cache[f"{keybase}-"]
elif f"{opname}_rowwise+" in cache:
cache[key] = cache[f"{opname}_rowwise+"].reduce(op_).new(name=key)
cache[key] = cache[f"{opname}_rowwise+"].reduce(parts_op).new(name=key)
elif f"{opname}_columnwise+" in cache:
cache[key] = cache[f"{opname}_columnwise+"].reduce(op_).new(name=key)
cache[key] = cache[f"{opname}_columnwise+"].reduce(parts_op).new(name=key)
elif cache.get("has_self_edges") is False and f"{opname}_rowwise-" in cache:
cache[key] = cache[f"{opname}_rowwise-"].reduce(op_).new(name=key)
cache[key] = cache[f"{opname}_rowwise-"].reduce(parts_op).new(name=key)
elif cache.get("has_self_edges") is False and f"{opname}_columnwise-" in cache:
cache[key] = cache[f"{opname}_columnwise-"].reduce(op_).new(name=key)
cache[key] = cache[f"{opname}_columnwise-"].reduce(parts_op).new(name=key)
else:
cache[key] = A.reduce_scalar(op_).new(name=key)
if (
Expand Down
12 changes: 6 additions & 6 deletions graphblas_algorithms/classes/digraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def get_recip_degreesp(G, mask=None):
cache["recip_degrees+"] = cache["recip_degrees-"]
return cache["recip_degrees-"].dup(mask=mask)
if "recip_degrees-" in cache and "diag" in cache:
return (unary.one(cache["diag"]) + cache["recip_degrees-"]).new(
return (unary.one[int](cache["diag"]) + cache["recip_degrees-"]).new(
mask=mask, name="recip_degrees+"
)
if "recip_degrees-" in cache and not G.get_property("has_self_edges"):
Expand All @@ -138,7 +138,7 @@ def get_recip_degreesp(G, mask=None):
if cache.get("has_self_edges") is False and "recip_degrees-" in cache:
cache["recip_degrees+"] = cache["recip_degrees-"]
elif "recip_degrees-" in cache and "diag" in cache:
cache["recip_degrees+"] = (unary.one(cache["diag"]) + cache["recip_degrees-"]).new(
cache["recip_degrees+"] = (unary.one[int](cache["diag"]) + cache["recip_degrees-"]).new(
name="recip_degrees+"
)
elif "recip_degrees-" in cache and not G.get_property("has_self_edges"):
Expand Down Expand Up @@ -175,7 +175,7 @@ def get_recip_degreesm(G, mask=None):
cache["recip_degrees-"] = cache["recip_degrees+"]
return cache["recip_degrees-"].dup(mask=mask)
if "recip_degrees+" in cache and "diag" in cache:
rv = binary.minus(cache["recip_degrees+"] | unary.one(cache["diag"])).new(
rv = binary.minus(cache["recip_degrees+"] | unary.one[int](cache["diag"])).new(
mask=mask, name="recip_degrees-"
)
rv(rv.V, replace) << rv # drop 0s
Expand All @@ -195,14 +195,14 @@ def get_recip_degreesm(G, mask=None):
).new(name="recip_degrees-")
diag = G.get_property("diag", mask=mask)
overlap = binary.pair(A & AT).reduce_rowwise().new(mask=mask)
rv = binary.minus(overlap | unary.one(diag)).new(name="recip_degrees-")
rv = binary.minus(overlap | unary.one[int](diag)).new(name="recip_degrees-")
rv(rv.V, replace) << rv # drop 0s
return rv
if "recip_degrees-" not in cache:
if cache.get("has_self_edges") is False and "recip_degrees+" in cache:
cache["recip_degrees-"] = cache["recip_degrees+"]
elif "recip_degrees+" in cache and "diag" in cache:
rv = binary.minus(cache["recip_degrees+"] | unary.one(cache["diag"])).new(
rv = binary.minus(cache["recip_degrees+"] | unary.one[int](cache["diag"])).new(
name="recip_degrees-"
)
rv(rv.V, replace) << rv # drop 0s
Expand All @@ -221,7 +221,7 @@ def get_recip_degreesm(G, mask=None):
else:
diag = G.get_property("diag")
overlap = binary.pair(A & AT).reduce_rowwise().new()
rv = binary.minus(overlap | unary.one(diag)).new(name="recip_degrees-")
rv = binary.minus(overlap | unary.one[int](diag)).new(name="recip_degrees-")
rv(rv.V, replace) << rv # drop 0s
cache["recip_degrees-"] = rv
if (
Expand Down
10 changes: 7 additions & 3 deletions graphblas_algorithms/classes/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,16 @@ def has_negative_diagonal(G, mask=None):
if A.dtype in NONNEGATIVE_DTYPES or A.dtype._is_udt or cache.get("has_self_edges") is False:
cache["has_negative_diagonal"] = False
elif (
# A negative weight, and none off the diagonal; a missing fact tells nothing.
cache.get("has_negative_edges+") is True
and cache.get("has_negative_edges-") is False
or cache.get("has_negative_edges+") is True
and cache.get("min_element-", 0) >= 0
or cache.get("min_element+", 0) < 0
and cache.get("min_element+", 0) < cache.get("min_element-", 0)
and "min_element-" in cache
and cache["min_element-"] >= 0
or "min_element+" in cache
and "min_element-" in cache
and cache["min_element+"] < 0
and cache["min_element+"] < cache["min_element-"]
):
cache["has_negative_diagonal"] = True
else:
Expand Down
Loading
Loading