Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not_implemented_for decorator for is_regular and related functions #7182

Merged
merged 5 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions networkx/algorithms/approximation/maxcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
__all__ = ["randomized_partitioning", "one_exchange"]


@not_implemented_for("directed", "multigraph")
@not_implemented_for("directed")
@not_implemented_for("multigraph")
@py_random_state(1)
@nx._dispatch(edge_attrs="weight")
def randomized_partitioning(G, seed=None, p=0.5, weight=None):
Expand Down Expand Up @@ -49,7 +50,8 @@ def _swap_node_partition(cut, node):
return cut - {node} if node in cut else cut.union({node})


@not_implemented_for("directed", "multigraph")
@not_implemented_for("directed")
@not_implemented_for("multigraph")
@py_random_state(2)
@nx._dispatch(edge_attrs="weight")
def one_exchange(G, initial_cut=None, seed=None, weight=None):
Expand Down
12 changes: 12 additions & 0 deletions networkx/algorithms/approximation/tests/test_maxcut.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import random

import pytest

import networkx as nx
from networkx.algorithms.approximation import maxcut


@pytest.mark.parametrize(
"f", (nx.approximation.randomized_partitioning, nx.approximation.one_exchange)
)
@pytest.mark.parametrize("graph_constructor", (nx.DiGraph, nx.MultiGraph))
def test_raises_on_directed_and_multigraphs(f, graph_constructor):
G = graph_constructor([(0, 1), (1, 2)])
with pytest.raises(nx.NetworkXNotImplemented):
f(G)


def _is_valid_cut(G, set1, set2):
union = set1.union(set2)
assert union == set(G.nodes)
Expand Down
3 changes: 2 additions & 1 deletion networkx/algorithms/community/asyn_fluid.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
__all__ = ["asyn_fluidc"]


@not_implemented_for("directed", "multigraph")
@not_implemented_for("directed")
@not_implemented_for("multigraph")
@py_random_state(3)
@nx._dispatch
def asyn_fluidc(G, k, max_iter=100, seed=None):
Expand Down
7 changes: 7 additions & 0 deletions networkx/algorithms/community/tests/test_asyn_fluid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
from networkx.algorithms.community import asyn_fluidc


@pytest.mark.parametrize("graph_constructor", (nx.DiGraph, nx.MultiGraph))
def test_raises_on_directed_and_multigraphs(graph_constructor):
G = graph_constructor([(0, 1), (1, 2)])
with pytest.raises(nx.NetworkXNotImplemented):
nx.community.asyn_fluidc(G, 1)


def test_exceptions():
test = Graph()
test.add_node("a")
Expand Down
6 changes: 4 additions & 2 deletions networkx/algorithms/distance_regular.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ def global_parameters(b, c):
return ((y, b[0] - x - y, x) for x, y in zip(b + [0], [0] + c))


@not_implemented_for("directed", "multigraph")
@not_implemented_for("directed")
@not_implemented_for("multigraph")
@nx._dispatch
def intersection_array(G):
"""Returns the intersection array of a distance-regular graph.
Expand Down Expand Up @@ -181,7 +182,8 @@ def intersection_array(G):


# TODO There is a definition for directed strongly regular graphs.
@not_implemented_for("directed", "multigraph")
@not_implemented_for("directed")
@not_implemented_for("multigraph")
@nx._dispatch
def is_strongly_regular(G):
"""Returns True if and only if the given graph is strongly
Expand Down
10 changes: 10 additions & 0 deletions networkx/algorithms/isomorphism/tests/test_tree_isomorphism.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import random
import time

import pytest

import networkx as nx
from networkx.algorithms.isomorphism.tree_isomorphism import (
rooted_tree_isomorphism,
Expand All @@ -9,6 +11,14 @@
from networkx.classes.function import is_directed


@pytest.mark.parametrize("graph_constructor", (nx.DiGraph, nx.MultiGraph))
def test_tree_isomorphism_raises_on_directed_and_multigraphs(graph_constructor):
t1 = graph_constructor([(0, 1)])
t2 = graph_constructor([(1, 2)])
with pytest.raises(nx.NetworkXNotImplemented):
nx.isomorphism.tree_isomorphism(t1, t2)


# have this work for graph
# given two trees (either the directed or undirected)
# transform t2 according to the isomorphism
Expand Down
3 changes: 2 additions & 1 deletion networkx/algorithms/isomorphism/tree_isomorphism.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ def rooted_tree_isomorphism(t1, root1, t2, root2):
return isomorphism


@not_implemented_for("directed", "multigraph")
@not_implemented_for("directed")
@not_implemented_for("multigraph")
@nx._dispatch(graphs={"t1": 0, "t2": 1})
def tree_isomorphism(t1, t2):
"""
Expand Down
10 changes: 10 additions & 0 deletions networkx/algorithms/tests/test_distance_regular.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
from networkx import is_strongly_regular


@pytest.mark.parametrize(
"f", (nx.is_distance_regular, nx.intersection_array, nx.is_strongly_regular)
)
@pytest.mark.parametrize("graph_constructor", (nx.DiGraph, nx.MultiGraph))
def test_raises_on_directed_and_multigraphs(f, graph_constructor):
G = graph_constructor([(0, 1), (1, 2)])
with pytest.raises(nx.NetworkXNotImplemented):
f(G)


class TestDistanceRegular:
def test_is_distance_regular(self):
assert nx.is_distance_regular(nx.icosahedral_graph())
Expand Down