backends.py 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  1. # Notes about NetworkX namespace objects set up here:
  2. #
  3. # nx.utils.backends.backends:
  4. # dict keyed by backend name to the backend entry point object.
  5. # Filled using ``_get_backends("networkx.backends")`` during import of this module.
  6. #
  7. # nx.utils.backends.backend_info:
  8. # dict keyed by backend name to the metadata returned by the function indicated
  9. # by the "networkx.backend_info" entry point.
  10. # Created as an empty dict while importing this module, but later filled using
  11. # ``_set_configs_from_environment()`` at end of importing ``networkx/__init__.py``.
  12. #
  13. # nx.config:
  14. # Config object for NetworkX config setting. Created using
  15. # ``_set_configs_from_environment()`` at end of importing ``networkx/__init__.py``.
  16. #
  17. # private dicts:
  18. # nx.utils.backends._loaded_backends:
  19. # dict used to memoize loaded backends. Keyed by backend name to loaded backends.
  20. #
  21. # nx.utils.backends._registered_algorithms:
  22. # dict of all the dispatchable functions in networkx, keyed by _dispatchable
  23. # function name to the wrapped function object.
  24. import inspect
  25. import itertools
  26. import logging
  27. import os
  28. import typing
  29. import warnings
  30. from functools import partial
  31. from importlib.metadata import entry_points
  32. import networkx as nx
  33. from .configs import BackendPriorities, Config, NetworkXConfig
  34. from .decorators import argmap
  35. __all__ = ["_dispatchable"]
  36. _logger = logging.getLogger(__name__)
  37. FAILED_TO_CONVERT = "FAILED_TO_CONVERT"
  38. def _get_backends(group, *, load_and_call=False):
  39. """
  40. Retrieve NetworkX ``backends`` and ``backend_info`` from the entry points.
  41. Parameters
  42. -----------
  43. group : str
  44. The entry_point to be retrieved.
  45. load_and_call : bool, optional
  46. If True, load and call the backend. Defaults to False.
  47. Returns
  48. --------
  49. dict
  50. A dictionary mapping backend names to their respective backend objects.
  51. Notes
  52. ------
  53. If a backend is defined more than once, a warning is issued.
  54. If a backend name is not a valid Python identifier, the backend is
  55. ignored and a warning is issued.
  56. The "nx_loopback" backend is removed if it exists, as it is only available during testing.
  57. A warning is displayed if an error occurs while loading a backend.
  58. """
  59. items = entry_points(group=group)
  60. rv = {}
  61. for ep in items:
  62. if not ep.name.isidentifier():
  63. warnings.warn(
  64. f"networkx backend name is not a valid identifier: {ep.name!r}. Ignoring.",
  65. RuntimeWarning,
  66. stacklevel=2,
  67. )
  68. elif ep.name in rv:
  69. warnings.warn(
  70. f"networkx backend defined more than once: {ep.name}",
  71. RuntimeWarning,
  72. stacklevel=2,
  73. )
  74. elif load_and_call:
  75. try:
  76. rv[ep.name] = ep.load()()
  77. except Exception as exc:
  78. warnings.warn(
  79. f"Error encountered when loading info for backend {ep.name}: {exc}",
  80. RuntimeWarning,
  81. stacklevel=2,
  82. )
  83. else:
  84. rv[ep.name] = ep
  85. rv.pop("nx_loopback", None)
  86. return rv
  87. # Note: "networkx" is in `backend_info` but ignored in `backends` and `config.backends`.
  88. # It is valid to use "networkx" as a backend argument and in `config.backend_priority`.
  89. # If we make "networkx" a "proper" backend, put it in `backends` and `config.backends`.
  90. backends = _get_backends("networkx.backends")
  91. # Use _set_configs_from_environment() below to fill backend_info dict as
  92. # the last step in importing networkx
  93. backend_info = {}
  94. # Load and cache backends on-demand
  95. _loaded_backends = {} # type: ignore[var-annotated]
  96. _registered_algorithms = {}
  97. # Get default configuration from environment variables at import time
  98. def _comma_sep_to_list(string):
  99. return [x_strip for x in string.strip().split(",") if (x_strip := x.strip())]
  100. def _set_configs_from_environment():
  101. """Initialize ``config.backend_priority``, load backend_info and config.
  102. This gets default values from environment variables (see ``nx.config`` for details).
  103. This function is run at the very end of importing networkx. It is run at this time
  104. to avoid loading backend_info before the rest of networkx is imported in case a
  105. backend uses networkx for its backend_info (e.g. subclassing the Config class.)
  106. """
  107. # backend_info is defined above as empty dict. Fill it after import finishes.
  108. backend_info.update(_get_backends("networkx.backend_info", load_and_call=True))
  109. backend_info.update(
  110. (backend, {}) for backend in backends.keys() - backend_info.keys()
  111. )
  112. # set up config based on backend_info and environment
  113. backend_config = {}
  114. for backend, info in backend_info.items():
  115. if "default_config" not in info:
  116. cfg = Config()
  117. else:
  118. cfg = info["default_config"]
  119. if not isinstance(cfg, Config):
  120. cfg = Config(**cfg)
  121. backend_config[backend] = cfg
  122. backend_config = Config(**backend_config)
  123. # Setting doc of backends_config type is not setting doc of Config
  124. # Config has __new__ method that returns instance with a unique type!
  125. type(backend_config).__doc__ = "All installed NetworkX backends and their configs."
  126. backend_priority = BackendPriorities(algos=[], generators=[], classes=[])
  127. config = NetworkXConfig(
  128. backend_priority=backend_priority,
  129. backends=backend_config,
  130. cache_converted_graphs=bool(
  131. os.environ.get("NETWORKX_CACHE_CONVERTED_GRAPHS", True)
  132. ),
  133. fallback_to_nx=bool(os.environ.get("NETWORKX_FALLBACK_TO_NX", False)),
  134. warnings_to_ignore=set(
  135. _comma_sep_to_list(os.environ.get("NETWORKX_WARNINGS_TO_IGNORE", ""))
  136. ),
  137. )
  138. # Add "networkx" item to backend_info now b/c backend_config is set up
  139. backend_info["networkx"] = {}
  140. # NETWORKX_BACKEND_PRIORITY is the same as NETWORKX_BACKEND_PRIORITY_ALGOS
  141. priorities = {
  142. key[26:].lower(): val
  143. for key, val in os.environ.items()
  144. if key.startswith("NETWORKX_BACKEND_PRIORITY_")
  145. }
  146. backend_priority = config.backend_priority
  147. backend_priority.algos = (
  148. _comma_sep_to_list(priorities.pop("algos"))
  149. if "algos" in priorities
  150. else _comma_sep_to_list(
  151. os.environ.get(
  152. "NETWORKX_BACKEND_PRIORITY",
  153. os.environ.get("NETWORKX_AUTOMATIC_BACKENDS", ""),
  154. )
  155. )
  156. )
  157. backend_priority.generators = _comma_sep_to_list(priorities.pop("generators", ""))
  158. for key in sorted(priorities):
  159. backend_priority[key] = _comma_sep_to_list(priorities[key])
  160. return config
  161. def _do_nothing():
  162. """This does nothing at all, yet it helps turn ``_dispatchable`` into functions.
  163. Use this with the ``argmap`` decorator to turn ``self`` into a function. It results
  164. in some small additional overhead compared to calling ``_dispatchable`` directly,
  165. but ``argmap`` has the property that it can stack with other ``argmap``
  166. decorators "for free". Being a function is better for REPRs and type-checkers.
  167. """
  168. def _always_run(name, args, kwargs):
  169. return True
  170. def _load_backend(backend_name):
  171. if backend_name in _loaded_backends:
  172. return _loaded_backends[backend_name]
  173. if backend_name not in backends:
  174. raise ImportError(f"'{backend_name}' backend is not installed")
  175. rv = _loaded_backends[backend_name] = backends[backend_name].load()
  176. if not hasattr(rv, "can_run"):
  177. rv.can_run = _always_run
  178. if not hasattr(rv, "should_run"):
  179. rv.should_run = _always_run
  180. return rv
  181. class _dispatchable:
  182. _is_testing = False
  183. def __new__(
  184. cls,
  185. func=None,
  186. *,
  187. name=None,
  188. graphs="G",
  189. edge_attrs=None,
  190. node_attrs=None,
  191. preserve_edge_attrs=False,
  192. preserve_node_attrs=False,
  193. preserve_graph_attrs=False,
  194. preserve_all_attrs=False,
  195. mutates_input=False,
  196. returns_graph=False,
  197. implemented_by_nx=True,
  198. ):
  199. """A decorator function that is used to redirect the execution of ``func``
  200. function to its backend implementation.
  201. This decorator allows the function to dispatch to different backend
  202. implementations based on the input graph types, and also manages the
  203. extra keywords ``backend`` and ``**backend_kwargs``.
  204. Usage can be any of the following decorator forms:
  205. - ``@_dispatchable``
  206. - ``@_dispatchable()``
  207. - ``@_dispatchable(name="override_name")``
  208. - ``@_dispatchable(graphs="graph_var_name")``
  209. - ``@_dispatchable(edge_attrs="weight")``
  210. - ``@_dispatchable(graphs={"G": 0, "H": 1}, edge_attrs={"weight": "default"})``
  211. with 0 and 1 giving the position in the signature function for graph
  212. objects. When ``edge_attrs`` is a dict, keys are keyword names and values
  213. are defaults.
  214. Parameters
  215. ----------
  216. func : callable, optional (default: None)
  217. The function to be decorated. If None, ``_dispatchable`` returns a
  218. partial object that can be used to decorate a function later. If ``func``
  219. is a callable, returns a new callable object that dispatches to a backend
  220. function based on input graph types.
  221. name : str, optional (default: name of `func`)
  222. The dispatch name for the function. It defaults to the name of `func`,
  223. but can be set manually to avoid conflicts in the global dispatch
  224. namespace. A common pattern is to prefix the function name with its
  225. module or submodule to make it unique. For example:
  226. - ``@_dispatchable(name="tournament_is_strongly_connected")``
  227. resolves conflict between ``nx.tournament.is_strongly_connected``
  228. and ``nx.is_strongly_connected``.
  229. - ``@_dispatchable(name="approximate_node_connectivity")``
  230. resolves conflict between ``nx.approximation.node_connectivity``
  231. and ``nx.connectivity.node_connectivity``.
  232. graphs : str or dict or None, optional (default: "G")
  233. If a string, the parameter name of the graph, which must be the first
  234. argument of the wrapped function. If more than one graph is required
  235. for the function (or if the graph is not the first argument), provide
  236. a dict keyed by graph parameter name to the value parameter position.
  237. A question mark in the name indicates an optional argument.
  238. For example, ``@_dispatchable(graphs={"G": 0, "auxiliary?": 4})``
  239. indicates the 0th parameter ``G`` of the function is a required graph,
  240. and the 4th parameter ``auxiliary?`` is an optional graph.
  241. To indicate that an argument is a list of graphs, do ``"[graphs]"``.
  242. Use ``graphs=None``, if *no* arguments are NetworkX graphs such as for
  243. graph generators, readers, and conversion functions.
  244. edge_attrs : str or dict, optional (default: None)
  245. ``edge_attrs`` holds information about edge attribute arguments
  246. and default values for those edge attributes.
  247. If a string, ``edge_attrs`` holds the function argument name that
  248. indicates a single edge attribute to include in the converted graph.
  249. The default value for this attribute is 1. To indicate that an argument
  250. is a list of attributes (all with default value 1), use e.g. ``"[attrs]"``.
  251. If a dict, ``edge_attrs`` holds a dict keyed by argument names, with
  252. values that are either the default value or, if a string, the argument
  253. name that indicates the default value.
  254. If None, function does not use edge attributes.
  255. node_attrs : str or dict, optional
  256. Like ``edge_attrs``, but for node attributes.
  257. preserve_edge_attrs : bool or str or dict, optional (default: False)
  258. If bool, whether to preserve all edge attributes.
  259. If a string, the parameter name that may indicate (with ``True`` or a
  260. callable argument) whether all edge attributes should be preserved
  261. when converting graphs to a backend graph type.
  262. If a dict of form ``{graph_name: {attr: default}}``, indicate
  263. pre-determined edge attributes (and defaults) to preserve for the
  264. indicated input graph.
  265. preserve_node_attrs : bool or str or dict, optional (default: False)
  266. Like ``preserve_edge_attrs``, but for node attributes.
  267. preserve_graph_attrs : bool or set, optional (default: False)
  268. If bool, whether to preserve all graph attributes.
  269. If set, which input graph arguments to preserve graph attributes.
  270. preserve_all_attrs : bool, optional (default: False)
  271. Whether to preserve all edge, node and graph attributes.
  272. If True, this overrides all the other preserve_*_attrs.
  273. mutates_input : bool or dict, optional (default: False)
  274. If bool, whether the function mutates an input graph argument.
  275. If dict of ``{arg_name: arg_pos}``, name and position of bool arguments
  276. that indicate whether an input graph will be mutated, and ``arg_name``
  277. may begin with ``"not "`` to negate the logic (for example, ``"not copy"``
  278. means we mutate the input graph when the ``copy`` argument is False).
  279. By default, dispatching doesn't convert input graphs to a different
  280. backend for functions that mutate input graphs.
  281. returns_graph : bool, optional (default: False)
  282. Whether the function can return or yield a graph object. By default,
  283. dispatching doesn't convert input graphs to a different backend for
  284. functions that return graphs.
  285. implemented_by_nx : bool, optional (default: True)
  286. Whether the function is implemented by NetworkX. If it is not, then the
  287. function is included in NetworkX only as an API to dispatch to backends.
  288. Default is True.
  289. """
  290. if func is None:
  291. return partial(
  292. _dispatchable,
  293. name=name,
  294. graphs=graphs,
  295. edge_attrs=edge_attrs,
  296. node_attrs=node_attrs,
  297. preserve_edge_attrs=preserve_edge_attrs,
  298. preserve_node_attrs=preserve_node_attrs,
  299. preserve_graph_attrs=preserve_graph_attrs,
  300. preserve_all_attrs=preserve_all_attrs,
  301. mutates_input=mutates_input,
  302. returns_graph=returns_graph,
  303. implemented_by_nx=implemented_by_nx,
  304. )
  305. if isinstance(func, str):
  306. raise TypeError("'name' and 'graphs' must be passed by keyword") from None
  307. # If name not provided, use the name of the function
  308. if name is None:
  309. name = func.__name__
  310. self = object.__new__(cls)
  311. # standard function-wrapping stuff
  312. # __annotations__ not used
  313. self.__name__ = func.__name__
  314. # self.__doc__ = func.__doc__ # __doc__ handled as cached property
  315. self.__defaults__ = func.__defaults__
  316. # Add `backend=` keyword argument to allow backend choice at call-time
  317. if func.__kwdefaults__:
  318. self.__kwdefaults__ = {**func.__kwdefaults__, "backend": None}
  319. else:
  320. self.__kwdefaults__ = {"backend": None}
  321. self.__module__ = func.__module__
  322. self.__qualname__ = func.__qualname__
  323. self.__dict__.update(func.__dict__)
  324. self.__wrapped__ = func
  325. # Supplement docstring with backend info; compute and cache when needed
  326. self._orig_doc = func.__doc__
  327. self._cached_doc = None
  328. self.orig_func = func
  329. self.name = name
  330. self.edge_attrs = edge_attrs
  331. self.node_attrs = node_attrs
  332. self.preserve_edge_attrs = preserve_edge_attrs or preserve_all_attrs
  333. self.preserve_node_attrs = preserve_node_attrs or preserve_all_attrs
  334. self.preserve_graph_attrs = preserve_graph_attrs or preserve_all_attrs
  335. self.mutates_input = mutates_input
  336. # Keep `returns_graph` private for now, b/c we may extend info on return types
  337. self._returns_graph = returns_graph
  338. if edge_attrs is not None and not isinstance(edge_attrs, str | dict):
  339. raise TypeError(
  340. f"Bad type for edge_attrs: {type(edge_attrs)}. Expected str or dict."
  341. ) from None
  342. if node_attrs is not None and not isinstance(node_attrs, str | dict):
  343. raise TypeError(
  344. f"Bad type for node_attrs: {type(node_attrs)}. Expected str or dict."
  345. ) from None
  346. if not isinstance(self.preserve_edge_attrs, bool | str | dict):
  347. raise TypeError(
  348. f"Bad type for preserve_edge_attrs: {type(self.preserve_edge_attrs)}."
  349. " Expected bool, str, or dict."
  350. ) from None
  351. if not isinstance(self.preserve_node_attrs, bool | str | dict):
  352. raise TypeError(
  353. f"Bad type for preserve_node_attrs: {type(self.preserve_node_attrs)}."
  354. " Expected bool, str, or dict."
  355. ) from None
  356. if not isinstance(self.preserve_graph_attrs, bool | set):
  357. raise TypeError(
  358. f"Bad type for preserve_graph_attrs: {type(self.preserve_graph_attrs)}."
  359. " Expected bool or set."
  360. ) from None
  361. if not isinstance(self.mutates_input, bool | dict):
  362. raise TypeError(
  363. f"Bad type for mutates_input: {type(self.mutates_input)}."
  364. " Expected bool or dict."
  365. ) from None
  366. if not isinstance(self._returns_graph, bool):
  367. raise TypeError(
  368. f"Bad type for returns_graph: {type(self._returns_graph)}."
  369. " Expected bool."
  370. ) from None
  371. if isinstance(graphs, str):
  372. graphs = {graphs: 0}
  373. elif graphs is None:
  374. pass
  375. elif not isinstance(graphs, dict):
  376. raise TypeError(
  377. f"Bad type for graphs: {type(graphs)}. Expected str or dict."
  378. ) from None
  379. elif len(graphs) == 0:
  380. raise KeyError("'graphs' must contain at least one variable name") from None
  381. # This dict comprehension is complicated for better performance; equivalent shown below.
  382. self.optional_graphs = set()
  383. self.list_graphs = set()
  384. if graphs is None:
  385. self.graphs = {}
  386. else:
  387. self.graphs = {
  388. self.optional_graphs.add(val := k[:-1]) or val
  389. if (last := k[-1]) == "?"
  390. else self.list_graphs.add(val := k[1:-1]) or val
  391. if last == "]"
  392. else k: v
  393. for k, v in graphs.items()
  394. }
  395. # The above is equivalent to:
  396. # self.optional_graphs = {k[:-1] for k in graphs if k[-1] == "?"}
  397. # self.list_graphs = {k[1:-1] for k in graphs if k[-1] == "]"}
  398. # self.graphs = {k[:-1] if k[-1] == "?" else k: v for k, v in graphs.items()}
  399. # Compute and cache the signature on-demand
  400. self._sig = None
  401. # Which backends implement this function?
  402. self.backends = {
  403. backend
  404. for backend, info in backend_info.items()
  405. if "functions" in info and name in info["functions"]
  406. }
  407. if implemented_by_nx:
  408. self.backends.add("networkx")
  409. if name in _registered_algorithms:
  410. raise KeyError(
  411. f"Algorithm already exists in dispatch namespace: {name}. "
  412. "Fix by assigning a unique `name=` in the `@_dispatchable` decorator."
  413. ) from None
  414. # Use the `argmap` decorator to turn `self` into a function. This does result
  415. # in small additional overhead compared to calling `_dispatchable` directly,
  416. # but `argmap` has the property that it can stack with other `argmap`
  417. # decorators "for free". Being a function is better for REPRs and type-checkers.
  418. # It also allows `_dispatchable` to be used on class methods, since functions
  419. # define `__get__`. Without using `argmap`, we would need to define `__get__`.
  420. self = argmap(_do_nothing)(self)
  421. _registered_algorithms[name] = self
  422. return self
  423. @property
  424. def __doc__(self):
  425. """If the cached documentation exists, it is returned.
  426. Otherwise, the documentation is generated using _make_doc() method,
  427. cached, and then returned."""
  428. rv = self._cached_doc
  429. if rv is None:
  430. rv = self._cached_doc = self._make_doc()
  431. return rv
  432. @__doc__.setter
  433. def __doc__(self, val):
  434. """Sets the original documentation to the given value and resets the
  435. cached documentation."""
  436. self._orig_doc = val
  437. self._cached_doc = None
  438. @property
  439. def __signature__(self):
  440. """Return the signature of the original function, with the addition of
  441. the `backend` and `backend_kwargs` parameters."""
  442. if self._sig is None:
  443. sig = inspect.signature(self.orig_func)
  444. # `backend` is now a reserved argument used by dispatching.
  445. # assert "backend" not in sig.parameters
  446. if not any(
  447. p.kind == inspect.Parameter.VAR_KEYWORD for p in sig.parameters.values()
  448. ):
  449. sig = sig.replace(
  450. parameters=[
  451. *sig.parameters.values(),
  452. inspect.Parameter(
  453. "backend", inspect.Parameter.KEYWORD_ONLY, default=None
  454. ),
  455. inspect.Parameter(
  456. "backend_kwargs", inspect.Parameter.VAR_KEYWORD
  457. ),
  458. ]
  459. )
  460. else:
  461. *parameters, var_keyword = sig.parameters.values()
  462. sig = sig.replace(
  463. parameters=[
  464. *parameters,
  465. inspect.Parameter(
  466. "backend", inspect.Parameter.KEYWORD_ONLY, default=None
  467. ),
  468. var_keyword,
  469. ]
  470. )
  471. self._sig = sig
  472. return self._sig
  473. # Fast, simple path if no backends are installed
  474. def _call_if_no_backends_installed(self, /, *args, backend=None, **kwargs):
  475. """Returns the result of the original function (no backends installed)."""
  476. if backend is not None and backend != "networkx":
  477. raise ImportError(f"'{backend}' backend is not installed")
  478. if "networkx" not in self.backends:
  479. raise NotImplementedError(
  480. f"'{self.name}' is not implemented by 'networkx' backend. "
  481. "This function is included in NetworkX as an API to dispatch to "
  482. "other backends."
  483. )
  484. return self.orig_func(*args, **kwargs)
  485. # Dispatch to backends based on inputs, `backend=` arg, or configuration
  486. def _call_if_any_backends_installed(self, /, *args, backend=None, **kwargs):
  487. """Returns the result of the original function, or the backend function if
  488. the backend is specified and that backend implements `func`."""
  489. # Use `backend_name` in this function instead of `backend`.
  490. # This is purely for aesthetics and to make it easier to search for this
  491. # variable since "backend" is used in many comments and log/error messages.
  492. backend_name = backend
  493. if backend_name is not None and backend_name not in backend_info:
  494. raise ImportError(f"'{backend_name}' backend is not installed")
  495. graphs_resolved = {}
  496. for gname, pos in self.graphs.items():
  497. if pos < len(args):
  498. if gname in kwargs:
  499. raise TypeError(f"{self.name}() got multiple values for {gname!r}")
  500. graph = args[pos]
  501. elif gname in kwargs:
  502. graph = kwargs[gname]
  503. elif gname not in self.optional_graphs:
  504. raise TypeError(
  505. f"{self.name}() missing required graph argument: {gname}"
  506. )
  507. else:
  508. continue
  509. if graph is None:
  510. if gname not in self.optional_graphs:
  511. raise TypeError(
  512. f"{self.name}() required graph argument {gname!r} is None; must be a graph"
  513. )
  514. else:
  515. graphs_resolved[gname] = graph
  516. # Alternative to the above that does not check duplicated args or missing required graphs.
  517. # graphs_resolved = {
  518. # gname: graph
  519. # for gname, pos in self.graphs.items()
  520. # if (graph := args[pos] if pos < len(args) else kwargs.get(gname)) is not None
  521. # }
  522. # Check if any graph comes from a backend
  523. if self.list_graphs:
  524. # Make sure we don't lose values by consuming an iterator
  525. args = list(args)
  526. for gname in self.list_graphs & graphs_resolved.keys():
  527. list_of_graphs = list(graphs_resolved[gname])
  528. graphs_resolved[gname] = list_of_graphs
  529. if gname in kwargs:
  530. kwargs[gname] = list_of_graphs
  531. else:
  532. args[self.graphs[gname]] = list_of_graphs
  533. graph_backend_names = {
  534. getattr(g, "__networkx_backend__", None)
  535. for gname, g in graphs_resolved.items()
  536. if gname not in self.list_graphs
  537. }
  538. for gname in self.list_graphs & graphs_resolved.keys():
  539. graph_backend_names.update(
  540. getattr(g, "__networkx_backend__", None)
  541. for g in graphs_resolved[gname]
  542. )
  543. else:
  544. graph_backend_names = {
  545. getattr(g, "__networkx_backend__", None)
  546. for g in graphs_resolved.values()
  547. }
  548. backend_priority = nx.config.backend_priority.get(
  549. self.name,
  550. nx.config.backend_priority.classes
  551. if self.name.endswith("__new__")
  552. else nx.config.backend_priority.generators
  553. if self._returns_graph
  554. else nx.config.backend_priority.algos,
  555. )
  556. fallback_to_nx = nx.config.fallback_to_nx and "networkx" in self.backends
  557. if self._is_testing and backend_priority and backend_name is None:
  558. # Special path if we are running networkx tests with a backend.
  559. # This even runs for (and handles) functions that mutate input graphs.
  560. return self._convert_and_call_for_tests(
  561. backend_priority[0],
  562. args,
  563. kwargs,
  564. fallback_to_nx=fallback_to_nx,
  565. )
  566. graph_backend_names.discard(None)
  567. if backend_name is not None:
  568. # Must run with the given backend.
  569. # `can_run` only used for better log and error messages.
  570. # Check `mutates_input` for logging, not behavior.
  571. backend_kwarg_msg = (
  572. "No other backends will be attempted, because the backend was "
  573. f"specified with the `backend='{backend_name}'` keyword argument."
  574. )
  575. extra_message = (
  576. f"'{backend_name}' backend raised NotImplementedError when calling "
  577. f"'{self.name}'. {backend_kwarg_msg}"
  578. )
  579. if not graph_backend_names or graph_backend_names == {backend_name}:
  580. # All graphs are backend graphs--no need to convert!
  581. if self._can_backend_run(backend_name, args, kwargs):
  582. return self._call_with_backend(
  583. backend_name, args, kwargs, extra_message=extra_message
  584. )
  585. if self._does_backend_have(backend_name):
  586. extra = " for the given arguments"
  587. else:
  588. extra = ""
  589. raise NotImplementedError(
  590. f"'{self.name}' is not implemented by '{backend_name}' backend"
  591. f"{extra}. {backend_kwarg_msg}"
  592. )
  593. if self._can_convert(backend_name, graph_backend_names):
  594. if self._can_backend_run(backend_name, args, kwargs):
  595. if self._will_call_mutate_input(args, kwargs):
  596. _logger.debug(
  597. "'%s' will mutate an input graph. This prevents automatic conversion "
  598. "to, and use of, backends listed in `nx.config.backend_priority`. "
  599. "Using backend specified by the "
  600. "`backend='%s'` keyword argument. This may change behavior by not "
  601. "mutating inputs.",
  602. self.name,
  603. backend_name,
  604. )
  605. mutations = []
  606. else:
  607. mutations = None
  608. rv = self._convert_and_call(
  609. backend_name,
  610. graph_backend_names,
  611. args,
  612. kwargs,
  613. extra_message=extra_message,
  614. mutations=mutations,
  615. )
  616. if mutations:
  617. for cache, key in mutations:
  618. # If the call mutates inputs, then remove all inputs gotten
  619. # from cache. We do this after all conversions (and call) so
  620. # that a graph can be gotten from a cache multiple times.
  621. cache.pop(key, None)
  622. return rv
  623. if self._does_backend_have(backend_name):
  624. extra = " for the given arguments"
  625. else:
  626. extra = ""
  627. raise NotImplementedError(
  628. f"'{self.name}' is not implemented by '{backend_name}' backend"
  629. f"{extra}. {backend_kwarg_msg}"
  630. )
  631. if len(graph_backend_names) == 1:
  632. maybe_s = ""
  633. graph_backend_names = f"'{next(iter(graph_backend_names))}'"
  634. else:
  635. maybe_s = "s"
  636. raise TypeError(
  637. f"'{self.name}' is unable to convert graph from backend{maybe_s} "
  638. f"{graph_backend_names} to '{backend_name}' backend, which was "
  639. f"specified with the `backend='{backend_name}'` keyword argument. "
  640. f"{backend_kwarg_msg}"
  641. )
  642. if self._will_call_mutate_input(args, kwargs):
  643. # The current behavior for functions that mutate input graphs:
  644. #
  645. # 1. If backend is specified by `backend=` keyword, use it (done above).
  646. # 2. If inputs are from one backend, try to use it.
  647. # 3. If all input graphs are instances of `nx.Graph`, then run with the
  648. # default "networkx" implementation.
  649. #
  650. # Do not automatically convert if a call will mutate inputs, because doing
  651. # so would change behavior. Hence, we should fail if there are multiple input
  652. # backends or if the input backend does not implement the function. However,
  653. # we offer a way for backends to circumvent this if they do not implement
  654. # this function: we will fall back to the default "networkx" implementation
  655. # without using conversions if all input graphs are subclasses of `nx.Graph`.
  656. mutate_msg = (
  657. "conversions between backends (if configured) will not be attempted "
  658. "because the original input graph would not be mutated. Using the "
  659. "backend keyword e.g. `backend='some_backend'` will force conversions "
  660. "and not mutate the original input graph."
  661. )
  662. fallback_msg = (
  663. "This call will mutate inputs, so fall back to 'networkx' "
  664. "backend (without converting) since all input graphs are "
  665. "instances of nx.Graph and are hopefully compatible."
  666. )
  667. if len(graph_backend_names) == 1:
  668. [backend_name] = graph_backend_names
  669. msg_template = (
  670. f"Backend '{backend_name}' does not implement '{self.name}'%s. "
  671. f"This call will mutate an input, so automatic {mutate_msg}"
  672. )
  673. # `can_run` is only used for better log and error messages
  674. try:
  675. if self._can_backend_run(backend_name, args, kwargs):
  676. return self._call_with_backend(
  677. backend_name,
  678. args,
  679. kwargs,
  680. extra_message=msg_template % " with these arguments",
  681. )
  682. except NotImplementedError as exc:
  683. if all(isinstance(g, nx.Graph) for g in graphs_resolved.values()):
  684. _logger.debug(
  685. "Backend '%s' raised when calling '%s': %s. %s",
  686. backend_name,
  687. self.name,
  688. exc,
  689. fallback_msg,
  690. )
  691. else:
  692. raise
  693. else:
  694. if fallback_to_nx and all(
  695. # Consider dropping the `isinstance` check here to allow
  696. # duck-type graphs, but let's wait for a backend to ask us.
  697. isinstance(g, nx.Graph)
  698. for g in graphs_resolved.values()
  699. ):
  700. # Log that we are falling back to networkx
  701. _logger.debug(
  702. "Backend '%s' can't run '%s'. %s",
  703. backend_name,
  704. self.name,
  705. fallback_msg,
  706. )
  707. else:
  708. if self._does_backend_have(backend_name):
  709. extra = " with these arguments"
  710. else:
  711. extra = ""
  712. raise NotImplementedError(msg_template % extra)
  713. elif fallback_to_nx and all(
  714. # Consider dropping the `isinstance` check here to allow
  715. # duck-type graphs, but let's wait for a backend to ask us.
  716. isinstance(g, nx.Graph)
  717. for g in graphs_resolved.values()
  718. ):
  719. # Log that we are falling back to networkx
  720. _logger.debug(
  721. "'%s' was called with inputs from multiple backends: %s. %s",
  722. self.name,
  723. graph_backend_names,
  724. fallback_msg,
  725. )
  726. else:
  727. raise RuntimeError(
  728. f"'{self.name}' will mutate an input, but it was called with "
  729. f"inputs from multiple backends: {graph_backend_names}. "
  730. f"Automatic {mutate_msg}"
  731. )
  732. # At this point, no backends are available to handle the call with
  733. # the input graph types, but if the input graphs are compatible
  734. # nx.Graph instances, fall back to networkx without converting.
  735. return self.orig_func(*args, **kwargs)
  736. # We may generalize fallback configuration as e.g. `nx.config.backend_fallback`
  737. if fallback_to_nx or not graph_backend_names:
  738. # Use "networkx" by default if there are no inputs from backends.
  739. # For example, graph generators should probably return NetworkX graphs
  740. # instead of raising NotImplementedError.
  741. backend_fallback = ["networkx"]
  742. else:
  743. backend_fallback = []
  744. # ##########################
  745. # # How this behaves today #
  746. # ##########################
  747. #
  748. # The prose below describes the implementation and a *possible* way to
  749. # generalize "networkx" as "just another backend". The code is structured
  750. # to perhaps someday support backend-to-backend conversions (including
  751. # simply passing objects from one backend directly to another backend;
  752. # the dispatch machinery does not necessarily need to perform conversions),
  753. # but since backend-to-backend matching is not yet supported, the following
  754. # code is merely a convenient way to implement dispatch behaviors that have
  755. # been carefully developed since NetworkX 3.0 and to include falling back
  756. # to the default NetworkX implementation.
  757. #
  758. # The current behavior for functions that don't mutate input graphs:
  759. #
  760. # 1. If backend is specified by `backend=` keyword, use it (done above).
  761. # 2. If input is from a backend other than "networkx", try to use it.
  762. # - Note: if present, "networkx" graphs will be converted to the backend.
  763. # 3. If input is from "networkx" (or no backend), try to use backends from
  764. # `backend_priority` before running with the default "networkx" implementation.
  765. # 4. If configured, "fall back" and run with the default "networkx" implementation.
  766. #
  767. # ################################################
  768. # # How this is implemented and may work someday #
  769. # ################################################
  770. #
  771. # Let's determine the order of backends we should try according
  772. # to `backend_priority`, `backend_fallback`, and input backends.
  773. # There are two† dimensions of priorities to consider:
  774. # backend_priority > unspecified > backend_fallback
  775. # and
  776. # backend of an input > not a backend of an input
  777. # These are combined to form five groups of priorities as such:
  778. #
  779. # input ~input
  780. # +-------+-------+
  781. # backend_priority | 1 | 2 |
  782. # unspecified | 3 | N/A | (if only 1)
  783. # backend_fallback | 4 | 5 |
  784. # +-------+-------+
  785. #
  786. # This matches the behaviors we developed in versions 3.0 to 3.2, it
  787. # ought to cover virtually all use cases we expect, and I (@eriknw) don't
  788. # think it can be done any simpler (although it can be generalized further
  789. # and made to be more complicated to capture 100% of *possible* use cases).
  790. # Some observations:
  791. #
  792. # 1. If an input is in `backend_priority`, it will be used before trying a
  793. # backend that is higher priority in `backend_priority` and not an input.
  794. # 2. To prioritize converting from one backend to another even if both implement
  795. # a function, list one in `backend_priority` and one in `backend_fallback`.
  796. # 3. To disable conversions, set `backend_priority` and `backend_fallback` to [].
  797. #
  798. # †: There is actually a third dimension of priorities:
  799. # should_run == True > should_run == False
  800. # Backends with `can_run == True` and `should_run == False` are tried last.
  801. #
  802. seen = set()
  803. group1 = [] # In backend_priority, and an input
  804. group2 = [] # In backend_priority, but not an input
  805. for name in backend_priority:
  806. if name in seen:
  807. continue
  808. seen.add(name)
  809. if name in graph_backend_names:
  810. group1.append(name)
  811. else:
  812. group2.append(name)
  813. group4 = [] # In backend_fallback, and an input
  814. group5 = [] # In backend_fallback, but not an input
  815. for name in backend_fallback:
  816. if name in seen:
  817. continue
  818. seen.add(name)
  819. if name in graph_backend_names:
  820. group4.append(name)
  821. else:
  822. group5.append(name)
  823. # An input, but not in backend_priority or backend_fallback.
  824. group3 = graph_backend_names - seen
  825. if len(group3) > 1:
  826. # `group3` backends are not configured for automatic conversion or fallback.
  827. # There are at least two issues if this group contains multiple backends:
  828. #
  829. # 1. How should we prioritize them? We have no good way to break ties.
  830. # Although we could arbitrarily choose alphabetical or left-most,
  831. # let's follow the Zen of Python and refuse the temptation to guess.
  832. # 2. We probably shouldn't automatically convert to these backends,
  833. # because we are not configured to do so.
  834. #
  835. # (2) is important to allow disabling all conversions by setting both
  836. # `nx.config.backend_priority` and `nx.config.backend_fallback` to [].
  837. #
  838. # If there is a single backend in `group3`, then giving it priority over
  839. # the fallback backends is what is generally expected. For example, this
  840. # allows input graphs of `backend_fallback` backends (such as "networkx")
  841. # to be converted to, and run with, the unspecified backend.
  842. _logger.debug(
  843. "Call to '%s' has inputs from multiple backends, %s, that "
  844. "have no priority set in `nx.config.backend_priority`, "
  845. "so automatic conversions to "
  846. "these backends will not be attempted.",
  847. self.name,
  848. group3,
  849. )
  850. group3 = ()
  851. try_order = list(itertools.chain(group1, group2, group3, group4, group5))
  852. if len(try_order) > 1:
  853. # Should we consider adding an option for more verbose logging?
  854. # For example, we could explain the order of `try_order` in detail.
  855. _logger.debug(
  856. "Call to '%s' has inputs from %s backends, and will try to use "
  857. "backends in the following order: %s",
  858. self.name,
  859. graph_backend_names or "no",
  860. try_order,
  861. )
  862. backends_to_try_again = []
  863. for is_not_first, backend_name in enumerate(try_order):
  864. if is_not_first:
  865. _logger.debug("Trying next backend: '%s'", backend_name)
  866. try:
  867. if not graph_backend_names or graph_backend_names == {backend_name}:
  868. if self._can_backend_run(backend_name, args, kwargs):
  869. return self._call_with_backend(backend_name, args, kwargs)
  870. elif self._can_convert(
  871. backend_name, graph_backend_names
  872. ) and self._can_backend_run(backend_name, args, kwargs):
  873. if self._should_backend_run(backend_name, args, kwargs):
  874. rv = self._convert_and_call(
  875. backend_name, graph_backend_names, args, kwargs
  876. )
  877. if (
  878. self._returns_graph
  879. and graph_backend_names
  880. and backend_name not in graph_backend_names
  881. ):
  882. # If the function has graph inputs and graph output, we try
  883. # to make it so the backend of the return type will match the
  884. # backend of the input types. In case this is not possible,
  885. # let's tell the user that the backend of the return graph
  886. # has changed. Perhaps we could try to convert back, but
  887. # "fallback" backends for graph generators should typically
  888. # be compatible with NetworkX graphs.
  889. _logger.debug(
  890. "Call to '%s' is returning a graph from a different "
  891. "backend! It has inputs from %s backends, but ran with "
  892. "'%s' backend and is returning graph from '%s' backend",
  893. self.name,
  894. graph_backend_names,
  895. backend_name,
  896. backend_name,
  897. )
  898. return rv
  899. # `should_run` is False, but `can_run` is True, so try again later
  900. backends_to_try_again.append(backend_name)
  901. except NotImplementedError as exc:
  902. _logger.debug(
  903. "Backend '%s' raised when calling '%s': %s",
  904. backend_name,
  905. self.name,
  906. exc,
  907. )
  908. # We are about to fail. Let's try backends with can_run=True and should_run=False.
  909. # This is unlikely to help today since we try to run with "networkx" before this.
  910. for backend_name in backends_to_try_again:
  911. _logger.debug(
  912. "Trying backend: '%s' (ignoring `should_run=False`)", backend_name
  913. )
  914. try:
  915. rv = self._convert_and_call(
  916. backend_name, graph_backend_names, args, kwargs
  917. )
  918. if (
  919. self._returns_graph
  920. and graph_backend_names
  921. and backend_name not in graph_backend_names
  922. ):
  923. _logger.debug(
  924. "Call to '%s' is returning a graph from a different "
  925. "backend! It has inputs from %s backends, but ran with "
  926. "'%s' backend and is returning graph from '%s' backend",
  927. self.name,
  928. graph_backend_names,
  929. backend_name,
  930. backend_name,
  931. )
  932. return rv
  933. except NotImplementedError as exc:
  934. _logger.debug(
  935. "Backend '%s' raised when calling '%s': %s",
  936. backend_name,
  937. self.name,
  938. exc,
  939. )
  940. # As a final effort, we could try to convert and run with `group3` backends
  941. # that we discarded when `len(group3) > 1`, but let's not consider doing
  942. # so until there is a reasonable request for it.
  943. if len(unspecified_backends := graph_backend_names - seen) > 1:
  944. raise TypeError(
  945. f"Unable to convert inputs from {graph_backend_names} backends and "
  946. f"run '{self.name}'. NetworkX is configured to automatically convert "
  947. f"to {try_order} backends. To remedy this, you may enable automatic "
  948. f"conversion to {unspecified_backends} backends by adding them to "
  949. "`nx.config.backend_priority`, or you "
  950. "may specify a backend to use with the `backend=` keyword argument."
  951. )
  952. if "networkx" not in self.backends:
  953. extra = (
  954. " This function is included in NetworkX as an API to dispatch to "
  955. "other backends."
  956. )
  957. else:
  958. extra = ""
  959. raise NotImplementedError(
  960. f"'{self.name}' is not implemented by {try_order} backends. To remedy "
  961. "this, you may enable automatic conversion to more backends (including "
  962. "'networkx') by adding them to `nx.config.backend_priority`, "
  963. "or you may specify a backend to use with "
  964. f"the `backend=` keyword argument.{extra}"
  965. )
  966. # Dispatch only if there exist any installed backend(s)
  967. __call__: typing.Callable = (
  968. _call_if_any_backends_installed if backends else _call_if_no_backends_installed
  969. )
  970. def _will_call_mutate_input(self, args, kwargs):
  971. # Fairly few nx functions mutate the input graph. Most that do, always do.
  972. # So a boolean input indicates "always" or "never".
  973. if isinstance((mutates_input := self.mutates_input), bool):
  974. return mutates_input
  975. # The ~10 other nx functions either use "copy=True" to control mutation or
  976. # an arg naming an edge/node attribute to mutate (None means no mutation).
  977. # Now `mutates_input` is a dict keyed by arg_name to its func-sig position.
  978. # The `copy=` args are keyed as "not copy" to mean "negate the copy argument".
  979. # Keys w/o "not " mean the call mutates only when the arg value `is not None`.
  980. #
  981. # This section might need different code if new functions mutate in new ways.
  982. #
  983. # NetworkX doesn't have any `mutates_input` dicts with more than 1 item.
  984. # But we treat it like it might have more than 1 item for generality.
  985. n = len(args)
  986. return any(
  987. (args[arg_pos] if n > arg_pos else kwargs.get(arg_name)) is not None
  988. if not arg_name.startswith("not ")
  989. # This assumes that e.g. `copy=True` is the default
  990. else not (args[arg_pos] if n > arg_pos else kwargs.get(arg_name[4:], True))
  991. for arg_name, arg_pos in mutates_input.items()
  992. )
  993. def _can_convert(self, backend_name, graph_backend_names):
  994. # Backend-to-backend conversion not supported yet.
  995. # We can only convert to and from networkx.
  996. rv = backend_name == "networkx" or graph_backend_names.issubset(
  997. {"networkx", backend_name}
  998. )
  999. if not rv:
  1000. _logger.debug(
  1001. "Unable to convert from %s backends to '%s' backend",
  1002. graph_backend_names,
  1003. backend_name,
  1004. )
  1005. return rv
  1006. def _does_backend_have(self, backend_name):
  1007. """Does the specified backend have this algorithm?"""
  1008. if backend_name == "networkx":
  1009. return "networkx" in self.backends
  1010. # Inspect the backend; don't trust metadata used to create `self.backends`
  1011. backend = _load_backend(backend_name)
  1012. return hasattr(backend, self.name)
  1013. def _can_backend_run(self, backend_name, args, kwargs):
  1014. """Can the specified backend run this algorithm with these arguments?"""
  1015. if backend_name == "networkx":
  1016. return "networkx" in self.backends
  1017. backend = _load_backend(backend_name)
  1018. # `backend.can_run` and `backend.should_run` may return strings that describe
  1019. # why they can't or shouldn't be run.
  1020. if not hasattr(backend, self.name):
  1021. _logger.debug(
  1022. "Backend '%s' does not implement '%s'", backend_name, self.name
  1023. )
  1024. return False
  1025. can_run = backend.can_run(self.name, args, kwargs)
  1026. if isinstance(can_run, str) or not can_run:
  1027. reason = f", because: {can_run}" if isinstance(can_run, str) else ""
  1028. _logger.debug(
  1029. "Backend '%s' can't run `%s` with arguments: %s%s",
  1030. backend_name,
  1031. self.name,
  1032. _LazyArgsRepr(self, args, kwargs),
  1033. reason,
  1034. )
  1035. return False
  1036. return True
  1037. def _should_backend_run(self, backend_name, args, kwargs):
  1038. """Should the specified backend run this algorithm with these arguments?
  1039. Note that this does not check ``backend.can_run``.
  1040. """
  1041. # `backend.can_run` and `backend.should_run` may return strings that describe
  1042. # why they can't or shouldn't be run.
  1043. # `_should_backend_run` may assume that `_can_backend_run` returned True.
  1044. if backend_name == "networkx":
  1045. return True
  1046. backend = _load_backend(backend_name)
  1047. should_run = backend.should_run(self.name, args, kwargs)
  1048. if isinstance(should_run, str) or not should_run:
  1049. reason = f", because: {should_run}" if isinstance(should_run, str) else ""
  1050. _logger.debug(
  1051. "Backend '%s' shouldn't run `%s` with arguments: %s%s",
  1052. backend_name,
  1053. self.name,
  1054. _LazyArgsRepr(self, args, kwargs),
  1055. reason,
  1056. )
  1057. return False
  1058. return True
  1059. def _convert_arguments(self, backend_name, args, kwargs, *, use_cache, mutations):
  1060. """Convert graph arguments to the specified backend.
  1061. Returns
  1062. -------
  1063. args tuple and kwargs dict
  1064. """
  1065. bound = self.__signature__.bind(*args, **kwargs)
  1066. bound.apply_defaults()
  1067. if not self.graphs:
  1068. bound_kwargs = bound.kwargs
  1069. del bound_kwargs["backend"]
  1070. return bound.args, bound_kwargs
  1071. if backend_name == "networkx":
  1072. # `backend_interface.convert_from_nx` preserves everything
  1073. preserve_edge_attrs = preserve_node_attrs = preserve_graph_attrs = True
  1074. else:
  1075. preserve_edge_attrs = self.preserve_edge_attrs
  1076. preserve_node_attrs = self.preserve_node_attrs
  1077. preserve_graph_attrs = self.preserve_graph_attrs
  1078. edge_attrs = self.edge_attrs
  1079. node_attrs = self.node_attrs
  1080. # Convert graphs into backend graph-like object
  1081. # Include the edge and/or node labels if provided to the algorithm
  1082. if preserve_edge_attrs is False:
  1083. # e.g. `preserve_edge_attrs=False`
  1084. pass
  1085. elif preserve_edge_attrs is True:
  1086. # e.g. `preserve_edge_attrs=True`
  1087. edge_attrs = None
  1088. elif isinstance(preserve_edge_attrs, str):
  1089. if bound.arguments[preserve_edge_attrs] is True or callable(
  1090. bound.arguments[preserve_edge_attrs]
  1091. ):
  1092. # e.g. `preserve_edge_attrs="attr"` and `func(attr=True)`
  1093. # e.g. `preserve_edge_attrs="attr"` and `func(attr=myfunc)`
  1094. preserve_edge_attrs = True
  1095. edge_attrs = None
  1096. elif bound.arguments[preserve_edge_attrs] is False and (
  1097. isinstance(edge_attrs, str)
  1098. and edge_attrs == preserve_edge_attrs
  1099. or isinstance(edge_attrs, dict)
  1100. and preserve_edge_attrs in edge_attrs
  1101. ):
  1102. # e.g. `preserve_edge_attrs="attr"` and `func(attr=False)`
  1103. # Treat `False` argument as meaning "preserve_edge_data=False"
  1104. # and not `False` as the edge attribute to use.
  1105. preserve_edge_attrs = False
  1106. edge_attrs = None
  1107. else:
  1108. # e.g. `preserve_edge_attrs="attr"` and `func(attr="weight")`
  1109. preserve_edge_attrs = False
  1110. # Else: e.g. `preserve_edge_attrs={"G": {"weight": 1}}`
  1111. if edge_attrs is None:
  1112. # May have been set to None above b/c all attributes are preserved
  1113. pass
  1114. elif isinstance(edge_attrs, str):
  1115. if edge_attrs[0] == "[":
  1116. # e.g. `edge_attrs="[edge_attributes]"` (argument of list of attributes)
  1117. # e.g. `func(edge_attributes=["foo", "bar"])`
  1118. edge_attrs = {
  1119. edge_attr: 1 for edge_attr in bound.arguments[edge_attrs[1:-1]]
  1120. }
  1121. elif callable(bound.arguments[edge_attrs]):
  1122. # e.g. `edge_attrs="weight"` and `func(weight=myfunc)`
  1123. preserve_edge_attrs = True
  1124. edge_attrs = None
  1125. elif bound.arguments[edge_attrs] is not None:
  1126. # e.g. `edge_attrs="weight"` and `func(weight="foo")` (default of 1)
  1127. edge_attrs = {bound.arguments[edge_attrs]: 1}
  1128. elif self.name == "to_numpy_array" and hasattr(
  1129. bound.arguments["dtype"], "names"
  1130. ):
  1131. # Custom handling: attributes may be obtained from `dtype`
  1132. edge_attrs = {
  1133. edge_attr: 1 for edge_attr in bound.arguments["dtype"].names
  1134. }
  1135. else:
  1136. # e.g. `edge_attrs="weight"` and `func(weight=None)`
  1137. edge_attrs = None
  1138. else:
  1139. # e.g. `edge_attrs={"attr": "default"}` and `func(attr="foo", default=7)`
  1140. # e.g. `edge_attrs={"attr": 0}` and `func(attr="foo")`
  1141. edge_attrs = {
  1142. edge_attr: bound.arguments.get(val, 1) if isinstance(val, str) else val
  1143. for key, val in edge_attrs.items()
  1144. if (edge_attr := bound.arguments[key]) is not None
  1145. }
  1146. if preserve_node_attrs is False:
  1147. # e.g. `preserve_node_attrs=False`
  1148. pass
  1149. elif preserve_node_attrs is True:
  1150. # e.g. `preserve_node_attrs=True`
  1151. node_attrs = None
  1152. elif isinstance(preserve_node_attrs, str):
  1153. if bound.arguments[preserve_node_attrs] is True or callable(
  1154. bound.arguments[preserve_node_attrs]
  1155. ):
  1156. # e.g. `preserve_node_attrs="attr"` and `func(attr=True)`
  1157. # e.g. `preserve_node_attrs="attr"` and `func(attr=myfunc)`
  1158. preserve_node_attrs = True
  1159. node_attrs = None
  1160. elif bound.arguments[preserve_node_attrs] is False and (
  1161. isinstance(node_attrs, str)
  1162. and node_attrs == preserve_node_attrs
  1163. or isinstance(node_attrs, dict)
  1164. and preserve_node_attrs in node_attrs
  1165. ):
  1166. # e.g. `preserve_node_attrs="attr"` and `func(attr=False)`
  1167. # Treat `False` argument as meaning "preserve_node_data=False"
  1168. # and not `False` as the node attribute to use. Is this used?
  1169. preserve_node_attrs = False
  1170. node_attrs = None
  1171. else:
  1172. # e.g. `preserve_node_attrs="attr"` and `func(attr="weight")`
  1173. preserve_node_attrs = False
  1174. # Else: e.g. `preserve_node_attrs={"G": {"pos": None}}`
  1175. if node_attrs is None:
  1176. # May have been set to None above b/c all attributes are preserved
  1177. pass
  1178. elif isinstance(node_attrs, str):
  1179. if node_attrs[0] == "[":
  1180. # e.g. `node_attrs="[node_attributes]"` (argument of list of attributes)
  1181. # e.g. `func(node_attributes=["foo", "bar"])`
  1182. node_attrs = {
  1183. node_attr: None for node_attr in bound.arguments[node_attrs[1:-1]]
  1184. }
  1185. elif callable(bound.arguments[node_attrs]):
  1186. # e.g. `node_attrs="weight"` and `func(weight=myfunc)`
  1187. preserve_node_attrs = True
  1188. node_attrs = None
  1189. elif bound.arguments[node_attrs] is not None:
  1190. # e.g. `node_attrs="weight"` and `func(weight="foo")`
  1191. node_attrs = {bound.arguments[node_attrs]: None}
  1192. else:
  1193. # e.g. `node_attrs="weight"` and `func(weight=None)`
  1194. node_attrs = None
  1195. else:
  1196. # e.g. `node_attrs={"attr": "default"}` and `func(attr="foo", default=7)`
  1197. # e.g. `node_attrs={"attr": 0}` and `func(attr="foo")`
  1198. node_attrs = {
  1199. node_attr: bound.arguments.get(val) if isinstance(val, str) else val
  1200. for key, val in node_attrs.items()
  1201. if (node_attr := bound.arguments[key]) is not None
  1202. }
  1203. # It should be safe to assume that we either have networkx graphs or backend graphs.
  1204. # Future work: allow conversions between backends.
  1205. for gname in self.graphs:
  1206. if gname in self.list_graphs:
  1207. bound.arguments[gname] = [
  1208. self._convert_graph(
  1209. backend_name,
  1210. g,
  1211. edge_attrs=edge_attrs,
  1212. node_attrs=node_attrs,
  1213. preserve_edge_attrs=preserve_edge_attrs,
  1214. preserve_node_attrs=preserve_node_attrs,
  1215. preserve_graph_attrs=preserve_graph_attrs,
  1216. graph_name=gname,
  1217. use_cache=use_cache,
  1218. mutations=mutations,
  1219. )
  1220. if getattr(g, "__networkx_backend__", "networkx") != backend_name
  1221. else g
  1222. for g in bound.arguments[gname]
  1223. ]
  1224. else:
  1225. graph = bound.arguments[gname]
  1226. if graph is None:
  1227. if gname in self.optional_graphs:
  1228. continue
  1229. raise TypeError(
  1230. f"Missing required graph argument `{gname}` in {self.name} function"
  1231. )
  1232. if isinstance(preserve_edge_attrs, dict):
  1233. preserve_edges = False
  1234. edges = preserve_edge_attrs.get(gname, edge_attrs)
  1235. else:
  1236. preserve_edges = preserve_edge_attrs
  1237. edges = edge_attrs
  1238. if isinstance(preserve_node_attrs, dict):
  1239. preserve_nodes = False
  1240. nodes = preserve_node_attrs.get(gname, node_attrs)
  1241. else:
  1242. preserve_nodes = preserve_node_attrs
  1243. nodes = node_attrs
  1244. if isinstance(preserve_graph_attrs, set):
  1245. preserve_graph = gname in preserve_graph_attrs
  1246. else:
  1247. preserve_graph = preserve_graph_attrs
  1248. if getattr(graph, "__networkx_backend__", "networkx") != backend_name:
  1249. bound.arguments[gname] = self._convert_graph(
  1250. backend_name,
  1251. graph,
  1252. edge_attrs=edges,
  1253. node_attrs=nodes,
  1254. preserve_edge_attrs=preserve_edges,
  1255. preserve_node_attrs=preserve_nodes,
  1256. preserve_graph_attrs=preserve_graph,
  1257. graph_name=gname,
  1258. use_cache=use_cache,
  1259. mutations=mutations,
  1260. )
  1261. bound_kwargs = bound.kwargs
  1262. del bound_kwargs["backend"]
  1263. return bound.args, bound_kwargs
  1264. def _convert_graph(
  1265. self,
  1266. backend_name,
  1267. graph,
  1268. *,
  1269. edge_attrs,
  1270. node_attrs,
  1271. preserve_edge_attrs,
  1272. preserve_node_attrs,
  1273. preserve_graph_attrs,
  1274. graph_name,
  1275. use_cache,
  1276. mutations,
  1277. ):
  1278. nx_cache = getattr(graph, "__networkx_cache__", None) if use_cache else None
  1279. if nx_cache is not None:
  1280. cache = nx_cache.setdefault("backends", {}).setdefault(backend_name, {})
  1281. key = _get_cache_key(
  1282. edge_attrs=edge_attrs,
  1283. node_attrs=node_attrs,
  1284. preserve_edge_attrs=preserve_edge_attrs,
  1285. preserve_node_attrs=preserve_node_attrs,
  1286. preserve_graph_attrs=preserve_graph_attrs,
  1287. )
  1288. compat_key, rv = _get_from_cache(cache, key, mutations=mutations)
  1289. if rv is not None:
  1290. if "cache" not in nx.config.warnings_to_ignore:
  1291. warnings.warn(
  1292. "Note: conversions to backend graphs are saved to cache "
  1293. "(`G.__networkx_cache__` on the original graph) by default."
  1294. "\n\nThis warning means the cached graph is being used "
  1295. f"for the {backend_name!r} backend in the "
  1296. f"call to {self.name}.\n\nFor the cache to be consistent "
  1297. "(i.e., correct), the input graph must not have been "
  1298. "manually mutated since the cached graph was created. "
  1299. "Examples of manually mutating the graph data structures "
  1300. "resulting in an inconsistent cache include:\n\n"
  1301. " >>> G[u][v][key] = val\n\n"
  1302. "and\n\n"
  1303. " >>> for u, v, d in G.edges(data=True):\n"
  1304. " ... d[key] = val\n\n"
  1305. "Using methods such as `G.add_edge(u, v, weight=val)` "
  1306. "will correctly clear the cache to keep it consistent. "
  1307. "You may also use `G.__networkx_cache__.clear()` to "
  1308. "manually clear the cache, or set `G.__networkx_cache__` "
  1309. "to None to disable caching for G. Enable or disable caching "
  1310. "globally via `nx.config.cache_converted_graphs` config.\n\n"
  1311. "To disable this warning:\n\n"
  1312. ' >>> nx.config.warnings_to_ignore.add("cache")\n'
  1313. )
  1314. if rv == FAILED_TO_CONVERT:
  1315. # NotImplementedError is reasonable to use since the backend doesn't
  1316. # implement this conversion. However, this will be different than
  1317. # the original exception that the backend raised when it failed.
  1318. # Using NotImplementedError allows the next backend to be attempted.
  1319. raise NotImplementedError(
  1320. "Graph conversion aborted: unable to convert graph to "
  1321. f"'{backend_name}' backend in call to `{self.name}', "
  1322. "because this conversion has previously failed."
  1323. )
  1324. _logger.debug(
  1325. "Using cached converted graph (from '%s' to '%s' backend) "
  1326. "in call to '%s' for '%s' argument",
  1327. getattr(graph, "__networkx_backend__", None),
  1328. backend_name,
  1329. self.name,
  1330. graph_name,
  1331. )
  1332. return rv
  1333. if backend_name == "networkx":
  1334. # Perhaps we should check that "__networkx_backend__" attribute exists
  1335. # and return the original object if not.
  1336. if not hasattr(graph, "__networkx_backend__"):
  1337. _logger.debug(
  1338. "Unable to convert input to 'networkx' backend in call to '%s' for "
  1339. "'%s argument, because it is not from a backend (i.e., it does not "
  1340. "have `G.__networkx_backend__` attribute). Using the original "
  1341. "object: %s",
  1342. self.name,
  1343. graph_name,
  1344. graph,
  1345. )
  1346. # This may fail, but let it fail in the networkx function
  1347. return graph
  1348. backend = _load_backend(graph.__networkx_backend__)
  1349. try:
  1350. rv = backend.convert_to_nx(graph)
  1351. except Exception:
  1352. if nx_cache is not None:
  1353. _set_to_cache(cache, key, FAILED_TO_CONVERT)
  1354. raise
  1355. else:
  1356. backend = _load_backend(backend_name)
  1357. try:
  1358. rv = backend.convert_from_nx(
  1359. graph,
  1360. edge_attrs=edge_attrs,
  1361. node_attrs=node_attrs,
  1362. preserve_edge_attrs=preserve_edge_attrs,
  1363. preserve_node_attrs=preserve_node_attrs,
  1364. # Always preserve graph attrs when we are caching b/c this should be
  1365. # cheap and may help prevent extra (unnecessary) conversions. Because
  1366. # we do this, we don't need `preserve_graph_attrs` in the cache key.
  1367. preserve_graph_attrs=preserve_graph_attrs or nx_cache is not None,
  1368. name=self.name,
  1369. graph_name=graph_name,
  1370. )
  1371. except Exception:
  1372. if nx_cache is not None:
  1373. _set_to_cache(cache, key, FAILED_TO_CONVERT)
  1374. raise
  1375. if nx_cache is not None:
  1376. _set_to_cache(cache, key, rv)
  1377. _logger.debug(
  1378. "Caching converted graph (from '%s' to '%s' backend) "
  1379. "in call to '%s' for '%s' argument",
  1380. getattr(graph, "__networkx_backend__", None),
  1381. backend_name,
  1382. self.name,
  1383. graph_name,
  1384. )
  1385. return rv
  1386. def _call_with_backend(self, backend_name, args, kwargs, *, extra_message=None):
  1387. """Call this dispatchable function with a backend without converting inputs."""
  1388. if backend_name == "networkx":
  1389. return self.orig_func(*args, **kwargs)
  1390. backend = _load_backend(backend_name)
  1391. _logger.debug(
  1392. "Using backend '%s' for call to '%s' with arguments: %s",
  1393. backend_name,
  1394. self.name,
  1395. _LazyArgsRepr(self, args, kwargs),
  1396. )
  1397. try:
  1398. return getattr(backend, self.name)(*args, **kwargs)
  1399. except NotImplementedError as exc:
  1400. if extra_message is not None:
  1401. _logger.debug(
  1402. "Backend '%s' raised when calling '%s': %s",
  1403. backend_name,
  1404. self.name,
  1405. exc,
  1406. )
  1407. raise NotImplementedError(extra_message) from exc
  1408. raise
  1409. def _convert_and_call(
  1410. self,
  1411. backend_name,
  1412. input_backend_names,
  1413. args,
  1414. kwargs,
  1415. *,
  1416. extra_message=None,
  1417. mutations=None,
  1418. ):
  1419. """Call this dispatchable function with a backend after converting inputs.
  1420. Parameters
  1421. ----------
  1422. backend_name : str
  1423. input_backend_names : set[str]
  1424. args : arguments tuple
  1425. kwargs : keywords dict
  1426. extra_message : str, optional
  1427. Additional message to log if NotImplementedError is raised by backend.
  1428. mutations : list, optional
  1429. Used to clear objects gotten from cache if inputs will be mutated.
  1430. """
  1431. if backend_name == "networkx":
  1432. func = self.orig_func
  1433. else:
  1434. backend = _load_backend(backend_name)
  1435. func = getattr(backend, self.name)
  1436. other_backend_names = input_backend_names - {backend_name}
  1437. _logger.debug(
  1438. "Converting input graphs from %s backend%s to '%s' backend for call to '%s'",
  1439. other_backend_names
  1440. if len(other_backend_names) > 1
  1441. else f"'{next(iter(other_backend_names))}'",
  1442. "s" if len(other_backend_names) > 1 else "",
  1443. backend_name,
  1444. self.name,
  1445. )
  1446. try:
  1447. converted_args, converted_kwargs = self._convert_arguments(
  1448. backend_name,
  1449. args,
  1450. kwargs,
  1451. use_cache=nx.config.cache_converted_graphs,
  1452. mutations=mutations,
  1453. )
  1454. except NotImplementedError as exc:
  1455. # Only log the exception if we are adding an extra message
  1456. # because we don't want to lose any information.
  1457. _logger.debug(
  1458. "Failed to convert graphs from %s to '%s' backend for call to '%s'"
  1459. + ("" if extra_message is None else ": %s"),
  1460. input_backend_names,
  1461. backend_name,
  1462. self.name,
  1463. *(() if extra_message is None else (exc,)),
  1464. )
  1465. if extra_message is not None:
  1466. raise NotImplementedError(extra_message) from exc
  1467. raise
  1468. if backend_name != "networkx":
  1469. _logger.debug(
  1470. "Using backend '%s' for call to '%s' with arguments: %s",
  1471. backend_name,
  1472. self.name,
  1473. _LazyArgsRepr(self, converted_args, converted_kwargs),
  1474. )
  1475. try:
  1476. return func(*converted_args, **converted_kwargs)
  1477. except NotImplementedError as exc:
  1478. if extra_message is not None:
  1479. _logger.debug(
  1480. "Backend '%s' raised when calling '%s': %s",
  1481. backend_name,
  1482. self.name,
  1483. exc,
  1484. )
  1485. raise NotImplementedError(extra_message) from exc
  1486. raise
  1487. def _convert_and_call_for_tests(
  1488. self, backend_name, args, kwargs, *, fallback_to_nx=False
  1489. ):
  1490. """Call this dispatchable function with a backend; for use with testing."""
  1491. backend = _load_backend(backend_name)
  1492. if not self._can_backend_run(backend_name, args, kwargs):
  1493. if fallback_to_nx or not self.graphs:
  1494. if fallback_to_nx:
  1495. _logger.debug(
  1496. "Falling back to use 'networkx' instead of '%s' backend "
  1497. "for call to '%s' with arguments: %s",
  1498. backend_name,
  1499. self.name,
  1500. _LazyArgsRepr(self, args, kwargs),
  1501. )
  1502. return self.orig_func(*args, **kwargs)
  1503. import pytest
  1504. msg = f"'{self.name}' not implemented by {backend_name}"
  1505. if hasattr(backend, self.name):
  1506. msg += " with the given arguments"
  1507. pytest.xfail(msg)
  1508. from collections.abc import Iterable, Iterator, Mapping
  1509. from copy import copy, deepcopy
  1510. from io import BufferedReader, BytesIO, StringIO, TextIOWrapper
  1511. from itertools import tee
  1512. from random import Random
  1513. import numpy as np
  1514. from numpy.random import Generator, RandomState
  1515. from scipy.sparse import sparray
  1516. # We sometimes compare the backend result (or input graphs) to the
  1517. # original result (or input graphs), so we need two sets of arguments.
  1518. compare_result_to_nx = (
  1519. self._returns_graph
  1520. and "networkx" in self.backends
  1521. and self.name
  1522. not in {
  1523. # Has graphs as node values (unable to compare)
  1524. "quotient_graph",
  1525. # We don't handle tempfile.NamedTemporaryFile arguments
  1526. "read_gml",
  1527. "read_graph6",
  1528. "read_sparse6",
  1529. # We don't handle io.BufferedReader or io.TextIOWrapper arguments
  1530. "bipartite_read_edgelist",
  1531. "read_adjlist",
  1532. "read_edgelist",
  1533. "read_graphml",
  1534. "read_multiline_adjlist",
  1535. "read_pajek",
  1536. "from_pydot",
  1537. "pydot_read_dot",
  1538. "agraph_read_dot",
  1539. # graph comparison fails b/c of nan values
  1540. "read_gexf",
  1541. }
  1542. )
  1543. compare_inputs_to_nx = (
  1544. "networkx" in self.backends and self._will_call_mutate_input(args, kwargs)
  1545. )
  1546. # Tee iterators and copy random state so that they may be used twice.
  1547. if not args or not compare_result_to_nx and not compare_inputs_to_nx:
  1548. args_to_convert = args_nx = args
  1549. else:
  1550. args_to_convert, args_nx = zip(
  1551. *(
  1552. (arg, deepcopy(arg))
  1553. if isinstance(arg, RandomState)
  1554. else (arg, copy(arg))
  1555. if isinstance(arg, BytesIO | StringIO | Random | Generator)
  1556. else tee(arg)
  1557. if isinstance(arg, Iterator)
  1558. and not isinstance(arg, BufferedReader | TextIOWrapper)
  1559. else (arg, arg)
  1560. for arg in args
  1561. )
  1562. )
  1563. if not kwargs or not compare_result_to_nx and not compare_inputs_to_nx:
  1564. kwargs_to_convert = kwargs_nx = kwargs
  1565. else:
  1566. kwargs_to_convert, kwargs_nx = zip(
  1567. *(
  1568. ((k, v), (k, deepcopy(v)))
  1569. if isinstance(v, RandomState)
  1570. else ((k, v), (k, copy(v)))
  1571. if isinstance(v, BytesIO | StringIO | Random | Generator)
  1572. else ((k, (teed := tee(v))[0]), (k, teed[1]))
  1573. if isinstance(v, Iterator)
  1574. and not isinstance(v, BufferedReader | TextIOWrapper)
  1575. else ((k, v), (k, v))
  1576. for k, v in kwargs.items()
  1577. )
  1578. )
  1579. kwargs_to_convert = dict(kwargs_to_convert)
  1580. kwargs_nx = dict(kwargs_nx)
  1581. try:
  1582. converted_args, converted_kwargs = self._convert_arguments(
  1583. backend_name,
  1584. args_to_convert,
  1585. kwargs_to_convert,
  1586. use_cache=False,
  1587. mutations=None,
  1588. )
  1589. except NotImplementedError as exc:
  1590. if fallback_to_nx:
  1591. _logger.debug(
  1592. "Graph conversion failed; falling back to use 'networkx' instead "
  1593. "of '%s' backend for call to '%s'",
  1594. backend_name,
  1595. self.name,
  1596. )
  1597. return self.orig_func(*args_nx, **kwargs_nx)
  1598. import pytest
  1599. pytest.xfail(
  1600. exc.args[0] if exc.args else f"{self.name} raised {type(exc).__name__}"
  1601. )
  1602. if compare_inputs_to_nx:
  1603. # Ensure input graphs are different if the function mutates an input graph.
  1604. bound_backend = self.__signature__.bind(*converted_args, **converted_kwargs)
  1605. bound_backend.apply_defaults()
  1606. bound_nx = self.__signature__.bind(*args_nx, **kwargs_nx)
  1607. bound_nx.apply_defaults()
  1608. for gname in self.graphs:
  1609. graph_nx = bound_nx.arguments[gname]
  1610. if bound_backend.arguments[gname] is graph_nx is not None:
  1611. bound_nx.arguments[gname] = graph_nx.copy()
  1612. args_nx = bound_nx.args
  1613. kwargs_nx = bound_nx.kwargs
  1614. kwargs_nx.pop("backend", None)
  1615. _logger.debug(
  1616. "Using backend '%s' for call to '%s' with arguments: %s",
  1617. backend_name,
  1618. self.name,
  1619. _LazyArgsRepr(self, converted_args, converted_kwargs),
  1620. )
  1621. try:
  1622. result = getattr(backend, self.name)(*converted_args, **converted_kwargs)
  1623. except NotImplementedError as exc:
  1624. if fallback_to_nx:
  1625. _logger.debug(
  1626. "Backend '%s' raised when calling '%s': %s; "
  1627. "falling back to use 'networkx' instead.",
  1628. backend_name,
  1629. self.name,
  1630. exc,
  1631. )
  1632. return self.orig_func(*args_nx, **kwargs_nx)
  1633. import pytest
  1634. pytest.xfail(
  1635. exc.args[0] if exc.args else f"{self.name} raised {type(exc).__name__}"
  1636. )
  1637. # Verify that `self._returns_graph` is correct. This compares the return type
  1638. # to the type expected from `self._returns_graph`. This handles tuple and list
  1639. # return types, but *does not* catch functions that yield graphs.
  1640. if (
  1641. self._returns_graph
  1642. != (
  1643. isinstance(result, nx.Graph)
  1644. or hasattr(result, "__networkx_backend__")
  1645. or isinstance(result, tuple | list)
  1646. and any(
  1647. isinstance(x, nx.Graph) or hasattr(x, "__networkx_backend__")
  1648. for x in result
  1649. )
  1650. )
  1651. and not (
  1652. # May return Graph or None
  1653. self.name in {"check_planarity", "check_planarity_recursive"}
  1654. and any(x is None for x in result)
  1655. )
  1656. and not (
  1657. # May return Graph or dict
  1658. self.name in {"held_karp_ascent"}
  1659. and any(isinstance(x, dict) for x in result)
  1660. )
  1661. and self.name
  1662. not in {
  1663. # yields graphs
  1664. "all_triads",
  1665. "general_k_edge_subgraphs",
  1666. # yields graphs or arrays
  1667. "nonisomorphic_trees",
  1668. }
  1669. ):
  1670. raise RuntimeError(f"`returns_graph` is incorrect for {self.name}")
  1671. def check_result(val, depth=0):
  1672. if isinstance(val, np.number):
  1673. raise RuntimeError(
  1674. f"{self.name} returned a numpy scalar {val} ({type(val)}, depth={depth})"
  1675. )
  1676. if isinstance(val, np.ndarray | sparray):
  1677. return
  1678. if isinstance(val, nx.Graph):
  1679. check_result(val._node, depth=depth + 1)
  1680. check_result(val._adj, depth=depth + 1)
  1681. return
  1682. if isinstance(val, Iterator):
  1683. raise NotImplementedError
  1684. if isinstance(val, Iterable) and not isinstance(val, str):
  1685. for x in val:
  1686. check_result(x, depth=depth + 1)
  1687. if isinstance(val, Mapping):
  1688. for x in val.values():
  1689. check_result(x, depth=depth + 1)
  1690. def check_iterator(it):
  1691. for val in it:
  1692. try:
  1693. check_result(val)
  1694. except RuntimeError as exc:
  1695. raise RuntimeError(
  1696. f"{self.name} returned a numpy scalar {val} ({type(val)})"
  1697. ) from exc
  1698. yield val
  1699. if self.name in {"from_edgelist"}:
  1700. # numpy scalars are explicitly given as values in some tests
  1701. pass
  1702. elif isinstance(result, Iterator):
  1703. result = check_iterator(result)
  1704. else:
  1705. try:
  1706. check_result(result)
  1707. except RuntimeError as exc:
  1708. raise RuntimeError(
  1709. f"{self.name} returned a numpy scalar {result} ({type(result)})"
  1710. ) from exc
  1711. check_result(result)
  1712. if self.name.endswith("__new__"):
  1713. # Graph is not yet done initializing; no sense doing more here
  1714. return result
  1715. def assert_graphs_equal(G1, G2, strict=True):
  1716. assert G1.number_of_nodes() == G2.number_of_nodes()
  1717. assert G1.number_of_edges() == G2.number_of_edges()
  1718. assert G1.is_directed() is G2.is_directed()
  1719. assert G1.is_multigraph() is G2.is_multigraph()
  1720. if strict:
  1721. assert G1.graph == G2.graph
  1722. assert G1._node == G2._node
  1723. assert G1._adj == G2._adj
  1724. else:
  1725. assert set(G1) == set(G2)
  1726. assert set(G1.edges) == set(G2.edges)
  1727. if compare_inputs_to_nx:
  1728. # Special-case algorithms that mutate input graphs
  1729. result_nx = self.orig_func(*args_nx, **kwargs_nx)
  1730. for gname in self.graphs:
  1731. G0 = bound_backend.arguments[gname]
  1732. G1 = bound_nx.arguments[gname]
  1733. if G0 is not None or G1 is not None:
  1734. G1 = backend.convert_to_nx(G1)
  1735. assert_graphs_equal(G0, G1, strict=False)
  1736. converted_result = backend.convert_to_nx(result)
  1737. if compare_result_to_nx and isinstance(converted_result, nx.Graph):
  1738. # For graph return types (e.g. generators), we compare that results are
  1739. # the same between the backend and networkx, then return the original
  1740. # networkx result so the iteration order will be consistent in tests.
  1741. if compare_inputs_to_nx:
  1742. G = result_nx
  1743. else:
  1744. G = self.orig_func(*args_nx, **kwargs_nx)
  1745. assert_graphs_equal(G, converted_result)
  1746. return G
  1747. return converted_result
  1748. def _make_doc(self):
  1749. """Generate the backends section at the end for functions having an alternate
  1750. backend implementation(s) using the `backend_info` entry-point."""
  1751. if self.backends == {"networkx"}:
  1752. return self._orig_doc
  1753. # Add "Backends" section to the bottom of the docstring (if there are backends)
  1754. lines = [
  1755. "Backends",
  1756. "--------",
  1757. ]
  1758. for backend in sorted(self.backends - {"networkx"}):
  1759. info = backend_info[backend]
  1760. if "short_summary" in info:
  1761. lines.append(f"{backend} : {info['short_summary']}")
  1762. else:
  1763. lines.append(backend)
  1764. if "functions" not in info or self.name not in info["functions"]:
  1765. lines.append("")
  1766. continue
  1767. func_info = info["functions"][self.name]
  1768. # Renaming extra_docstring to additional_docs
  1769. if func_docs := (
  1770. func_info.get("additional_docs") or func_info.get("extra_docstring")
  1771. ):
  1772. lines.extend(
  1773. f" {line}" if line else line for line in func_docs.split("\n")
  1774. )
  1775. add_gap = True
  1776. else:
  1777. add_gap = False
  1778. # Renaming extra_parameters to additional_parameters
  1779. if extra_parameters := (
  1780. func_info.get("extra_parameters")
  1781. or func_info.get("additional_parameters")
  1782. ):
  1783. if add_gap:
  1784. lines.append("")
  1785. lines.append(" Additional parameters:")
  1786. for param in sorted(extra_parameters):
  1787. lines.append(f" {param}")
  1788. if desc := extra_parameters[param]:
  1789. lines.append(f" {desc}")
  1790. lines.append("")
  1791. else:
  1792. lines.append("")
  1793. if func_url := func_info.get("url"):
  1794. lines.append(f"[`Source <{func_url}>`_]")
  1795. lines.append("")
  1796. # We assume the docstrings are indented by four spaces (true for now)
  1797. new_doc = self._orig_doc or ""
  1798. if not new_doc.rstrip():
  1799. new_doc = f"The original docstring for {self.name} was empty."
  1800. if self.backends:
  1801. lines.pop() # Remove last empty line
  1802. to_add = "\n ".join(lines)
  1803. new_doc = f"{new_doc.rstrip()}\n\n {to_add}"
  1804. # For backend-only funcs, add "Attention" admonishment after the one line summary
  1805. if "networkx" not in self.backends:
  1806. lines = new_doc.split("\n")
  1807. index = 0
  1808. while not lines[index].strip():
  1809. index += 1
  1810. while index < len(lines) and lines[index].strip():
  1811. index += 1
  1812. backends = sorted(self.backends)
  1813. if len(backends) == 0:
  1814. example = ""
  1815. elif len(backends) == 1:
  1816. example = f' such as "{backends[0]}"'
  1817. elif len(backends) == 2:
  1818. example = f' such as "{backends[0]} or "{backends[1]}"'
  1819. else:
  1820. example = (
  1821. " such as "
  1822. + ", ".join(f'"{x}"' for x in backends[:-1])
  1823. + f', or "{backends[-1]}"' # Oxford comma
  1824. )
  1825. to_add = (
  1826. "\n .. attention:: This function does not have a default NetworkX implementation.\n"
  1827. " It may only be run with an installable :doc:`backend </backends>` that\n"
  1828. f" supports it{example}.\n\n"
  1829. " Hint: use ``backend=...`` keyword argument to specify a backend or add\n"
  1830. " backends to ``nx.config.backend_priority``."
  1831. )
  1832. lines.insert(index, to_add)
  1833. new_doc = "\n".join(lines)
  1834. return new_doc
  1835. def __reduce__(self):
  1836. """Allow this object to be serialized with pickle.
  1837. This uses the global registry `_registered_algorithms` to deserialize.
  1838. """
  1839. return _restore_dispatchable, (self.name,)
  1840. def _restore_dispatchable(name):
  1841. return _registered_algorithms[name].__wrapped__
  1842. def _get_cache_key(
  1843. *,
  1844. edge_attrs,
  1845. node_attrs,
  1846. preserve_edge_attrs,
  1847. preserve_node_attrs,
  1848. preserve_graph_attrs,
  1849. ):
  1850. """Return key used by networkx caching given arguments for ``convert_from_nx``."""
  1851. # edge_attrs: dict | None
  1852. # node_attrs: dict | None
  1853. # preserve_edge_attrs: bool (False if edge_attrs is not None)
  1854. # preserve_node_attrs: bool (False if node_attrs is not None)
  1855. return (
  1856. frozenset(edge_attrs.items())
  1857. if edge_attrs is not None
  1858. else preserve_edge_attrs,
  1859. frozenset(node_attrs.items())
  1860. if node_attrs is not None
  1861. else preserve_node_attrs,
  1862. )
  1863. def _get_from_cache(cache, key, *, backend_name=None, mutations=None):
  1864. """Search the networkx cache for a graph that is compatible with ``key``.
  1865. Parameters
  1866. ----------
  1867. cache : dict
  1868. If ``backend_name`` is given, then this is treated as ``G.__networkx_cache__``,
  1869. but if ``backend_name`` is None, then this is treated as the resolved inner
  1870. cache such as ``G.__networkx_cache__["backends"][backend_name]``.
  1871. key : tuple
  1872. Cache key from ``_get_cache_key``.
  1873. backend_name : str, optional
  1874. Name of the backend to control how ``cache`` is interpreted.
  1875. mutations : list, optional
  1876. Used internally to clear objects gotten from cache if inputs will be mutated.
  1877. Returns
  1878. -------
  1879. tuple or None
  1880. The key of the compatible graph found in the cache.
  1881. graph or "FAILED_TO_CONVERT" or None
  1882. A compatible graph if possible. "FAILED_TO_CONVERT" indicates that a previous
  1883. conversion attempt failed for this cache key.
  1884. """
  1885. if backend_name is not None:
  1886. cache = cache.get("backends", {}).get(backend_name, {})
  1887. if not cache:
  1888. return None, None
  1889. # Do a simple search for a cached graph with compatible data.
  1890. # For example, if we need a single attribute, then it's okay
  1891. # to use a cached graph that preserved all attributes.
  1892. # This looks for an exact match first.
  1893. edge_key, node_key = key
  1894. for compat_key in itertools.product(
  1895. (edge_key, True) if edge_key is not True else (True,),
  1896. (node_key, True) if node_key is not True else (True,),
  1897. ):
  1898. if (rv := cache.get(compat_key)) is not None and (
  1899. rv != FAILED_TO_CONVERT or key == compat_key
  1900. ):
  1901. if mutations is not None:
  1902. # Remove this item from the cache (after all conversions) if
  1903. # the call to this dispatchable function will mutate an input.
  1904. mutations.append((cache, compat_key))
  1905. return compat_key, rv
  1906. # Iterate over the items in `cache` to see if any are compatible.
  1907. # For example, if no edge attributes are needed, then a graph
  1908. # with any edge attribute will suffice. We use the same logic
  1909. # below (but switched) to clear unnecessary items from the cache.
  1910. # Use `list(cache.items())` to be thread-safe.
  1911. for (ekey, nkey), graph in list(cache.items()):
  1912. if graph == FAILED_TO_CONVERT:
  1913. # Return FAILED_TO_CONVERT if any cache key that requires a subset
  1914. # of the edge/node attributes of the given cache key has previously
  1915. # failed to convert. This logic is similar to `_set_to_cache`.
  1916. if ekey is False or edge_key is True:
  1917. pass
  1918. elif ekey is True or edge_key is False or not ekey.issubset(edge_key):
  1919. continue
  1920. if nkey is False or node_key is True: # or nkey == node_key:
  1921. pass
  1922. elif nkey is True or node_key is False or not nkey.issubset(node_key):
  1923. continue
  1924. # Save to cache for faster subsequent lookups
  1925. cache[key] = FAILED_TO_CONVERT
  1926. elif edge_key is False or ekey is True:
  1927. pass # Cache works for edge data!
  1928. elif edge_key is True or ekey is False or not edge_key.issubset(ekey):
  1929. continue # Cache missing required edge data; does not work
  1930. if node_key is False or nkey is True:
  1931. pass # Cache works for node data!
  1932. elif node_key is True or nkey is False or not node_key.issubset(nkey):
  1933. continue # Cache missing required node data; does not work
  1934. if mutations is not None:
  1935. # Remove this item from the cache (after all conversions) if
  1936. # the call to this dispatchable function will mutate an input.
  1937. mutations.append((cache, (ekey, nkey)))
  1938. return (ekey, nkey), graph
  1939. return None, None
  1940. def _set_to_cache(cache, key, graph, *, backend_name=None):
  1941. """Set a backend graph to the cache, and remove unnecessary cached items.
  1942. Parameters
  1943. ----------
  1944. cache : dict
  1945. If ``backend_name`` is given, then this is treated as ``G.__networkx_cache__``,
  1946. but if ``backend_name`` is None, then this is treated as the resolved inner
  1947. cache such as ``G.__networkx_cache__["backends"][backend_name]``.
  1948. key : tuple
  1949. Cache key from ``_get_cache_key``.
  1950. graph : graph or "FAILED_TO_CONVERT"
  1951. Setting value to "FAILED_TO_CONVERT" prevents this conversion from being
  1952. attempted in future calls.
  1953. backend_name : str, optional
  1954. Name of the backend to control how ``cache`` is interpreted.
  1955. Returns
  1956. -------
  1957. dict
  1958. The items that were removed from the cache.
  1959. """
  1960. if backend_name is not None:
  1961. cache = cache.setdefault("backends", {}).setdefault(backend_name, {})
  1962. # Remove old cached items that are no longer necessary since they
  1963. # are dominated/subsumed/outdated by what was just calculated.
  1964. # This uses the same logic as above, but with keys switched.
  1965. # Also, don't update the cache here if the call will mutate an input.
  1966. removed = {}
  1967. edge_key, node_key = key
  1968. cache[key] = graph # Set at beginning to be thread-safe
  1969. if graph == FAILED_TO_CONVERT:
  1970. return removed
  1971. for cur_key in list(cache):
  1972. if cur_key == key:
  1973. continue
  1974. ekey, nkey = cur_key
  1975. if ekey is False or edge_key is True:
  1976. pass
  1977. elif ekey is True or edge_key is False or not ekey.issubset(edge_key):
  1978. continue
  1979. if nkey is False or node_key is True:
  1980. pass
  1981. elif nkey is True or node_key is False or not nkey.issubset(node_key):
  1982. continue
  1983. # Use pop instead of del to try to be thread-safe
  1984. if (graph := cache.pop(cur_key, None)) is not None:
  1985. removed[cur_key] = graph
  1986. return removed
  1987. class _LazyArgsRepr:
  1988. """Simple wrapper to display arguments of dispatchable functions in logging calls."""
  1989. def __init__(self, func, args, kwargs):
  1990. self.func = func
  1991. self.args = args
  1992. self.kwargs = kwargs
  1993. self.value = None
  1994. def __repr__(self):
  1995. if self.value is None:
  1996. bound = self.func.__signature__.bind_partial(*self.args, **self.kwargs)
  1997. inner = ", ".join(f"{key}={val!r}" for key, val in bound.arguments.items())
  1998. self.value = f"({inner})"
  1999. return self.value
  2000. if os.environ.get("_NETWORKX_BUILDING_DOCS_"):
  2001. # When building docs with Sphinx, use the original function with the
  2002. # dispatched __doc__, b/c Sphinx renders normal Python functions better.
  2003. # This doesn't show e.g. `*, backend=None, **backend_kwargs` in the
  2004. # signatures, which is probably okay. It does allow the docstring to be
  2005. # updated based on the installed backends.
  2006. _orig_dispatchable = _dispatchable
  2007. def _dispatchable(func=None, **kwargs): # type: ignore[no-redef]
  2008. if func is None:
  2009. return partial(_dispatchable, **kwargs)
  2010. dispatched_func = _orig_dispatchable(func, **kwargs)
  2011. func.__doc__ = dispatched_func.__doc__
  2012. return func
  2013. _dispatchable.__doc__ = _orig_dispatchable.__new__.__doc__ # type: ignore[method-assign,assignment]
  2014. _sig = inspect.signature(_orig_dispatchable.__new__)
  2015. _dispatchable.__signature__ = _sig.replace( # type: ignore[method-assign,assignment]
  2016. parameters=[v for k, v in _sig.parameters.items() if k != "cls"]
  2017. )