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
20 changes: 10 additions & 10 deletions _nx_graphblas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,14 @@ def get_info():
"center": '`usebounds` bounds the eccentricities on an undirected graph whose lengths are integers;\nelsewhere every one is computed. The null graph is left to NetworkX.\nLengths are Bellman-Ford\'s, so a negative edge without a negative cycle is answered where\nNetworkX\'s Dijkstra raises "Contradictory paths found".\nRaises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.',
"closeness_centrality": 'Lengths are Bellman-Ford\'s, so a negative edge without a negative cycle is answered where\nNetworkX\'s Dijkstra raises "Contradictory paths found".',
"common_neighbor_centrality": "An `alpha` other than a Python int or float, unless it equals 1, is left to NetworkX.",
"compose": "Edge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), bools mixed with numbers as\nnumbers (True as 1 or 1.0), and NumPy scalars as Python numbers. A mix that would\nchange a value, such as an int past 2**53 beside a float, is left to NetworkX.",
"compose": "Edge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), and NumPy scalars as Python\nnumbers. A mix that would change a value, such as an int past 2**53 beside a float, is\nleft to NetworkX, and so is any bool weight, as for every name.",
"cut_size": "Raises OverflowError rather than add integer weights whose absolute values sum to 2**63\nor more.",
"diameter": '`usebounds` bounds the eccentricities on an undirected graph whose lengths are integers;\nelsewhere every one is computed. The null graph is left to NetworkX.\nLengths are Bellman-Ford\'s, so a negative edge without a negative cycle is answered where\nNetworkX\'s Dijkstra raises "Contradictory paths found".\nRaises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.',
"disjoint_union": "Edge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), bools mixed with numbers as\nnumbers (True as 1 or 1.0), and NumPy scalars as Python numbers. A mix that would\nchange a value, such as an int past 2**53 beside a float, is left to NetworkX.",
"disjoint_union": "Edge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), and NumPy scalars as Python\nnumbers. A mix that would change a value, such as an int past 2**53 beside a float, is\nleft to NetworkX, and so is any bool weight, as for every name.",
"eccentricity": 'A precomputed `sp`, and the null graph, are left to NetworkX.\nLengths are Bellman-Ford\'s, so a negative edge without a negative cycle is answered where\nNetworkX\'s Dijkstra raises "Contradictory paths found".\nRaises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.',
"edge_betweenness_centrality": "Unweighted: a call with `weight`, or with `k` short of every node, is left to NetworkX.\nThe edges come by their first ends in node order, then by their second ends in node\norder, which may differ from the order of `G.edges()`.",
"edge_betweenness_centrality_subset": "Unweighted: a call with `weight` is left to NetworkX. The edges come by their first ends\nin node order, then by their second ends in node order, which may differ from the order\nof `G.edges()`.",
"ego_graph": 'With `distance`, lengths are Bellman-Ford\'s, so a negative edge without a negative\ncycle is answered where NetworkX\'s Dijkstra raises "Contradictory paths found", and the\ncall raises OverflowError rather than add integer weights where the number of nodes\ntimes the heaviest absolute weight reaches 2**62, as a length could then pass 2**63.\nEdge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), bools mixed with numbers as\nnumbers (True as 1 or 1.0), and NumPy scalars as Python numbers. A mix that would\nchange a value, such as an int past 2**53 beside a float, is left to NetworkX.',
"ego_graph": 'With `distance`, lengths are Bellman-Ford\'s, so a negative edge without a negative\ncycle is answered where NetworkX\'s Dijkstra raises "Contradictory paths found", and the\ncall raises OverflowError rather than add integer weights where the number of nodes\ntimes the heaviest absolute weight reaches 2**62, as a length could then pass 2**63.\nEdge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), and NumPy scalars as Python\nnumbers. A mix that would change a value, such as an int past 2**53 beside a float, is\nleft to NetworkX, and so is any bool weight, as for every name.',
"floyd_warshall": "Raises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.",
"floyd_warshall_predecessor_and_distance": "Raises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.",
"group_closeness_centrality": "Unweighted: a call with `weight` is left to NetworkX.",
Expand All @@ -217,19 +217,19 @@ def get_info():
"harmonic_diameter": 'A precomputed `sp` is left to NetworkX.\nLengths are Bellman-Ford\'s, so a negative edge without a negative cycle is answered where\nNetworkX\'s Dijkstra raises "Contradictory paths found".',
"hits": 'A graph with nodes and no edges gets 0.0 for every node, where NetworkX\'s power\niteration does not converge. `method="svd"` is left to NetworkX.',
"hyper_wiener_index": "Raises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, or where the sum over one node's targets could\nreach 2**63.",
"k_core": "A precomputed `core_number`, or a `k` that is not a real number, is left to NetworkX.\nEdge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), bools mixed with numbers as\nnumbers (True as 1 or 1.0), and NumPy scalars as Python numbers. A mix that would\nchange a value, such as an int past 2**53 beside a float, is left to NetworkX.",
"k_corona": "A precomputed `core_number`, or a `k` that is not a real number, is left to NetworkX.\nEdge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), bools mixed with numbers as\nnumbers (True as 1 or 1.0), and NumPy scalars as Python numbers. A mix that would\nchange a value, such as an int past 2**53 beside a float, is left to NetworkX.",
"k_crust": "A precomputed `core_number`, or a `k` that is not a real number, is left to NetworkX.\nEdge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), bools mixed with numbers as\nnumbers (True as 1 or 1.0), and NumPy scalars as Python numbers. A mix that would\nchange a value, such as an int past 2**53 beside a float, is left to NetworkX.",
"k_shell": "A precomputed `core_number`, or a `k` that is not a real number, is left to NetworkX.\nEdge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), bools mixed with numbers as\nnumbers (True as 1 or 1.0), and NumPy scalars as Python numbers. A mix that would\nchange a value, such as an int past 2**53 beside a float, is left to NetworkX.",
"k_truss": "Edge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), bools mixed with numbers as\nnumbers (True as 1 or 1.0), and NumPy scalars as Python numbers. A mix that would\nchange a value, such as an int past 2**53 beside a float, is left to NetworkX.",
"k_core": "A precomputed `core_number`, or a `k` that is not a real number, is left to NetworkX.\nEdge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), and NumPy scalars as Python\nnumbers. A mix that would change a value, such as an int past 2**53 beside a float, is\nleft to NetworkX, and so is any bool weight, as for every name.",
"k_corona": "A precomputed `core_number`, or a `k` that is not a real number, is left to NetworkX.\nEdge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), and NumPy scalars as Python\nnumbers. A mix that would change a value, such as an int past 2**53 beside a float, is\nleft to NetworkX, and so is any bool weight, as for every name.",
"k_crust": "A precomputed `core_number`, or a `k` that is not a real number, is left to NetworkX.\nEdge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), and NumPy scalars as Python\nnumbers. A mix that would change a value, such as an int past 2**53 beside a float, is\nleft to NetworkX, and so is any bool weight, as for every name.",
"k_shell": "A precomputed `core_number`, or a `k` that is not a real number, is left to NetworkX.\nEdge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), and NumPy scalars as Python\nnumbers. A mix that would change a value, such as an int past 2**53 beside a float, is\nleft to NetworkX, and so is any bool weight, as for every name.",
"k_truss": "Edge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), and NumPy scalars as Python\nnumbers. A mix that would change a value, such as an int past 2**53 beside a float, is\nleft to NetworkX, and so is any bool weight, as for every name.",
"local_efficiency": "The cost is the breadth-first levels times the chunks of walks, so a node whose\nneighbors induce long paths (the hub of a wheel) is no faster than in NetworkX; the\nmemory stays bounded.",
"negative_edge_cycle": "Raises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.",
"periphery": '`usebounds` bounds the eccentricities on an undirected graph whose lengths are integers;\nelsewhere every one is computed. The null graph is left to NetworkX.\nLengths are Bellman-Ford\'s, so a negative edge without a negative cycle is answered where\nNetworkX\'s Dijkstra raises "Contradictory paths found".\nRaises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.',
"radius": '`usebounds` bounds the eccentricities on an undirected graph whose lengths are integers;\nelsewhere every one is computed. The null graph is left to NetworkX.\nLengths are Bellman-Ford\'s, so a negative edge without a negative cycle is answered where\nNetworkX\'s Dijkstra raises "Contradictory paths found".\nRaises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.',
"reverse": "Edge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), bools mixed with numbers as\nnumbers (True as 1 or 1.0), and NumPy scalars as Python numbers. A mix that would\nchange a value, such as an int past 2**53 beside a float, is left to NetworkX.",
"reverse": "Edge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), and NumPy scalars as Python\nnumbers. A mix that would change a value, such as an int past 2**53 beside a float, is\nleft to NetworkX, and so is any bool weight, as for every name.",
"schultz_index": 'A graph with a node named "weight" is left to NetworkX.\nRaises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, or where the sum over one node\'s targets could\nreach 2**63.',
"single_source_bellman_ford_path_length": "Raises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, as a length could then pass 2**63.",
"union": "Edge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), bools mixed with numbers as\nnumbers (True as 1 or 1.0), and NumPy scalars as Python numbers. A mix that would\nchange a value, such as an int past 2**53 beside a float, is left to NetworkX.",
"union": "Edge weights come back in the one type their matrix holds: mixed ints and floats, or\n`np.uint64` beside signed ints, as floats (1 as 1.0), and NumPy scalars as Python\nnumbers. A mix that would change a value, such as an int past 2**53 beside a float, is\nleft to NetworkX, and so is any bool weight, as for every name.",
"volume": "Raises OverflowError rather than add integer weights whose absolute values sum to 2**63\nor more, or to 2**63 over the most times `S` names one node.",
"wiener_index": 'Lengths are Bellman-Ford\'s, so a negative edge without a negative cycle is answered where\nNetworkX\'s Dijkstra raises "Contradictory paths found".\nRaises OverflowError rather than add integer weights where the number of nodes times the\nheaviest absolute weight reaches 2**62, or where the sum over one node\'s targets could\nreach 2**63.',
# END: additional_docs
Expand Down
6 changes: 3 additions & 3 deletions _nx_graphblas/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@
# full_join rebuilds its inputs too, but leaves a weighted graph to NetworkX.
_ONE_WEIGHT_TYPE = (
"Edge weights come back in the one type their matrix holds: mixed ints and floats, or\n"
"`np.uint64` beside signed ints, as floats (1 as 1.0), bools mixed with numbers as\n"
"numbers (True as 1 or 1.0), and NumPy scalars as Python numbers. A mix that would\n"
"change a value, such as an int past 2**53 beside a float, is left to NetworkX."
"`np.uint64` beside signed ints, as floats (1 as 1.0), and NumPy scalars as Python\n"
"numbers. A mix that would change a value, such as an int past 2**53 beside a float, is\n"
"left to NetworkX, and so is any bool weight, as for every name."
)
for _name in (
"compose",
Expand Down
51 changes: 50 additions & 1 deletion graphblas_algorithms/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def _edge_value_problem(graph, attr):
numpy infers for them all, so where that is a float or complex, an int past 2**53 can
come back as another number (2**53 + 1 beside a float as 9007199254740992.0, a large
`np.uint64` beside a negative int the same way): refused as well. A change of type alone
is not refused: 1.0 for 1 beside a float, 1 for True beside an int. A numpy time and a
is not refused: 1.0 for 1 beside a float. A bool is refused once the graph is converted,
whatever it sits beside (`_has_bool_values`). A numpy time and a
long double are refused whatever they sit beside: GraphBLAS has neither type, and
`int()` of a `timedelta64`, which subclasses `np.integer`, raises TypeError.
"""
Expand Down Expand Up @@ -147,6 +148,49 @@ def _edge_value_problem(graph, attr):
return None


def _has_bool_values(graph, attr, G):
"""Whether some edge's `attr` is a bool, Python's or NumPy's; `G` is the converted graph.

NetworkX adds Python's bools as the ints 1 and 0 and NumPy's two at a time as a logical or
(`np.True_ + np.True_` is True, where `mutual_weight` adds two), and some of its answers
hold a weight as it was given (`floyd_warshall`'s distance along one edge, `ego_graph`'s
edges, `adjacency_matrix`). A matrix of bools has no plus, and one of ints or floats has
lost which values were bools, so a bool anywhere is left to NetworkX.

A matrix of bools says so at once. In any other, a bool is held as 1 or 0 (1.0 or 0.0
beside floats, and an undirected self-loop's True as 1 once NetworkX's conversion has
negated and added it), so only those entries are looked up in `graph`: one at a time when
they are few, as most weights from data are not exactly 1 or 0, and else all the values
at once, their types gathered at C speed.
"""
import itertools
import operator

import numpy as np
from graphblas import binary, dtypes, select

A = G._A
if A.dtype == dtypes.BOOL:
return True
bools = (bool, np.bool_)
ones_and_zeros = binary.any(select.valueeq(A, 1).new() | select.valueeq(A, 0).new()).new()
if ones_and_zeros.nvals == 0:
return False
if 4 * ones_and_zeros.nvals < A.nvals:
keys = list(G._key_to_id) # by id
adj = graph._adj
rows, cols, _values = ones_and_zeros.to_coo(values=False)
return any(
type(adj[keys[row]][keys[col]].get(attr)) in bools
for row, col in zip(rows.tolist(), cols.tolist(), strict=True)
)
datadicts = itertools.chain.from_iterable(
map(operator.methodcaller("values"), graph._adj.values())
)
types = set(map(type, map(operator.methodcaller("get", attr), datadicts)))
return not types.isdisjoint(bools)


def _unrebuildable(graph, weight, preserve_edge_attrs, *, keeps_node_data=True):
"""Why a graph rebuilt from `graph` would not carry what NetworkX's own result does.

Expand Down Expand Up @@ -522,6 +566,11 @@ def convert_from_nx(
raise NotImplementedError(
f"Edge attribute {weight!r} has values a GraphBLAS matrix cannot hold: {exc}"
) from exc
if weight is not None and _has_bool_values(graph, weight, G):
raise NotImplementedError(
f"Edge attribute {weight!r} has bool values, which NetworkX adds as ints (two "
"of NumPy's as a logical or) and returns as bools; networkx serves the call"
)
# A rebuilt graph keeps the graph attributes too, which NetworkX before 3.7 does not
# ask for on `reverse` when its conversion cache is off.
if preserve_graph_attrs or declared.rebuilds_input:
Expand Down
6 changes: 6 additions & 0 deletions graphblas_algorithms/nxapi/tests/test_community.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ def test_modularity_follows_networkx(name, weight, resolution):
G, partition = _partitioned()[name]
expected = quality.modularity(G, partition, weight=weight, resolution=resolution)
for H in (G, backend(G, weight="weight")):
if H is G and name == "boolean weights" and weight == "weight":
# NetworkX's bools are left to NetworkX as they are converted (test_bool_weights.py);
# a backend graph that holds bools already is served, adding them as numbers.
with pytest.raises(NotImplementedError):
quality.modularity(H, partition, weight, resolution, backend="graphblas")
continue
rv = quality.modularity(H, partition, weight, resolution, backend="graphblas")
assert rv == pytest.approx(expected, rel=1e-12, abs=1e-15)

Expand Down
Loading
Loading