docstr: Call this dispatchable function with a backend; for use with testing.
docstr_tok: ["Call","this","dispatchable","function","with","a","backend",";","for","use","with","testing","."]
code: def _convert_and_call_for_tests(
      self, backend_name, args, kwargs, *, fallback_to_nx=False
  ):
      """Call this dispatchable function with a backend; for use with testing."""
      backend = _load_backend(backend_name)
      if not self._can_backend_run(backend_name, *args, **kwargs):
          if fallback_to_nx or not self.graphs:
              return self.orig_func(*args, **kwargs)
  
          import pytest
  
          msg = f"'{self.name}' not implemented by {backend_name}"
          if hasattr(backend, self.name):
              msg += " with the given arguments"
          pytest.xfail(msg)
  
      from collections.abc import Iterator
      from copy import copy
      from io import BufferedReader, BytesIO
      from itertools import tee
      from random import Random
  
      # We sometimes compare the backend result to the original result,
      # so we need two sets of arguments. We tee iterators and copy
      # random state so that they may be used twice.
      if not args:
          args1 = args2 = args
      else:
          args1, args2 = zip(
              *(
                  (arg, copy(arg))
                  if isinstance(arg, Random | BytesIO)
                  else tee(arg)
                  if isinstance(arg, Iterator)
                  and not isinstance(arg, BufferedReader)
                  else (arg, arg)
                  for arg in args
              )
          )
      if not kwargs:
          kwargs1 = kwargs2 = kwargs
      else:
          kwargs1, kwargs2 = zip(
              *(
                  ((k, v), (k, copy(v)))
                  if isinstance(v, Random | BytesIO)
                  else ((k, (teed := tee(v))[0]), (k, teed[1]))
                  if isinstance(v, Iterator)
                  and not isinstance(v, BufferedReader)
                  else ((k, v), (k, v))
                  for k, v in kwargs.items()
              )
          )
          kwargs1 = dict(kwargs1)
          kwargs2 = dict(kwargs2)
      try:
          converted_args, converted_kwargs = self._convert_arguments(
              backend_name, args1, kwargs1
          )
          result = getattr(backend, self.name)(
              *converted_args, **converted_kwargs
          )
      except (NotImplementedError, NetworkXNotImplemented) as exc:
          if fallback_to_nx:
              return self.orig_func(*args2, **kwargs2)
          import pytest
  
          pytest.xfail(
              exc.args[0]
              if exc.args
              else f"{self.name} raised {type(exc).__name__}"
          )
  
      if self.name in {
          "edmonds_karp_core",
          "barycenter",
          "contracted_edge",
          "contracted_nodes",
          "stochastic_graph",
          "relabel_nodes",
      }:
          # Special-case algorithms that mutate input graphs
          bound = self.__signature__.bind(
              *converted_args, **converted_kwargs
          )
          bound.apply_defaults()
          bound2 = self.__signature__.bind(*args2, **kwargs2)
          bound2.apply_defaults()
          if self.name == "edmonds_karp_core":
              R1 = backend.convert_to_nx(bound.arguments["R"])
              R2 = bound2.arguments["R"]
              for k, v in R1.edges.items():
                  R2.edges[k]["flow"] = v["flow"]
          elif (
              self.name == "barycenter"
              and bound.arguments["attr"] is not None
          ):
              G1 = backend.convert_to_nx(bound.arguments["G"])
              G2 = bound2.arguments["G"]
              attr = bound.arguments["attr"]
              for k, v in G1.nodes.items():
                  G2.nodes[k][attr] = v[attr]
          elif (
              self.name in {"contracted_nodes", "contracted_edge"}
              and not bound.arguments["copy"]
          ):
              # Edges and nodes changed; node "contraction" and edge "weight" attrs
              G1 = backend.convert_to_nx(bound.arguments["G"])
              G2 = bound2.arguments["G"]
              G2.__dict__.update(G1.__dict__)
          elif (
              self.name == "stochastic_graph"
              and not bound.arguments["copy"]
          ):
              G1 = backend.convert_to_nx(bound.arguments["G"])
              G2 = bound2.arguments["G"]
              for k, v in G1.edges.items():
                  G2.edges[k]["weight"] = v["weight"]
          elif (
              self.name == "relabel_nodes"
              and not bound.arguments["copy"]
          ):
              G1 = backend.convert_to_nx(bound.arguments["G"])
              G2 = bound2.arguments["G"]
              if G1 is G2:
                  return G2
              G2._node.clear()
              G2._node.update(G1._node)
              G2._adj.clear()
              G2._adj.update(G1._adj)
              if hasattr(G1, "_pred") and hasattr(G2, "_pred"):
                  G2._pred.clear()
                  G2._pred.update(G1._pred)
              if hasattr(G1, "_succ") and hasattr(G2, "_succ"):
                  G2._succ.clear()
                  G2._succ.update(G1._succ)
              return G2
          return backend.convert_to_nx(result)
  
      converted_result = backend.convert_to_nx(result)
      if isinstance(converted_result, nx.Graph) and self.name not in {
          "boykov_kolmogorov",
          "preflow_push",
          "quotient_graph",
          "shortest_augmenting_path",
          "spectral_graph_forge",
          # We don't handle tempfile.NamedTemporaryFile arguments
          "read_gml",
          "read_graph6",
          "read_sparse6",
          # We don't handle io.BufferedReader arguments
          "bipartite_read_edgelist",
          "read_adjlist",
          "read_edgelist",
          "read_graphml",
          "read_multiline_adjlist",
          "read_pajek",
          # graph comparison fails b/c of nan values
          "read_gexf",
      }:
          # For graph return types (e.g. generators), we compare that results are
          # the same between the backend and networkx, then return the original
          # networkx result so the iteration order will be consistent in tests.
          G = self.orig_func(*args2, **kwargs2)
          if not nx.utils.graphs_equal(G, converted_result):
              assert (
                  G.number_of_nodes()
                  == converted_result.number_of_nodes()
              )
              assert (
                  G.number_of_edges()
                  == converted_result.number_of_edges()
              )
              assert G.graph == converted_result.graph
              assert G.nodes == converted_result.nodes
              assert G.adj == converted_result.adj
              assert type(G) is type(converted_result)
              raise AssertionError("Graphs are not equal")
          return G
      return converted_result
  
imports: import inspect
  import os
  import sys
  import warnings
  from functools import partial
  from importlib.metadata import entry_points
  import networkx
  from ..exception import NetworkXNotImplemented
label_desc: Use image node_id 9 for calling the _dispatchable obj's underlying member method code with example usage: obj._convert_and_call_for_tests(backend_name, args, kwargs) and returns: converted_result, backend, G, self, self, G2, G2