Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/Cython/Coverage.py +439 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/__pycache__/covering.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/__pycache__/extendability.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/__pycache__/generators.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/__pycache__/spectral.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/centrality.py +290 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/tests/test_edgelist.py +229 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/tests/test_spectral_bipartivity.py +80 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/flow/tests/wlm3.gpickle.bz2 +3 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/tests/__pycache__/test_communicability.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/tests/__pycache__/test_core.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/tests/__pycache__/test_d_separation.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/tests/__pycache__/test_non_randomness.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/tests/__pycache__/test_planar_drawing.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/tests/__pycache__/test_smallworld.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/drawing/__pycache__/nx_pydot.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/classic.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/cographs.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/community.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/degree_seq.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/ego.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/expanders.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/internet_as_graphs.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/intersection.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/interval_graph.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/lattice.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/line.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/random_clustered.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/random_graphs.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/small.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/stochastic.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/sudoku.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/time_series.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/trees.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/__init__.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_community.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_directed.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_duplication.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_ego.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_expanders.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_internet_as_graphs.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_intersection.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_joint_degree_seq.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_line.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_mycielski.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_stochastic.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_sudoku.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_time_series.cpython-311.pyc +0 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/test_atlas.py +75 -0
- tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/test_classic.py +622 -0
tuning-competition-baseline/.venv/lib/python3.11/site-packages/Cython/Coverage.py
ADDED
|
@@ -0,0 +1,439 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
A Cython plugin for coverage.py
|
| 3 |
+
|
| 4 |
+
Requires the coverage package at least in version 4.0 (which added the plugin API).
|
| 5 |
+
|
| 6 |
+
This plugin requires the generated C sources to be available, next to the extension module.
|
| 7 |
+
It parses the C file and reads the original source files from it, which are stored in C comments.
|
| 8 |
+
It then reports a source file to coverage.py when it hits one of its lines during line tracing.
|
| 9 |
+
|
| 10 |
+
Basically, Cython can (on request) emit explicit trace calls into the C code that it generates,
|
| 11 |
+
and as a general human debugging helper, it always copies the current source code line
|
| 12 |
+
(and its surrounding context) into the C files before it generates code for that line, e.g.
|
| 13 |
+
|
| 14 |
+
::
|
| 15 |
+
|
| 16 |
+
/* "line_trace.pyx":147
|
| 17 |
+
* def cy_add_with_nogil(a,b):
|
| 18 |
+
* cdef int z, x=a, y=b # 1
|
| 19 |
+
* with nogil: # 2 # <<<<<<<<<<<<<<
|
| 20 |
+
* z = 0 # 3
|
| 21 |
+
* z += cy_add_nogil(x, y) # 4
|
| 22 |
+
*/
|
| 23 |
+
__Pyx_TraceLine(147,1,__PYX_ERR(0, 147, __pyx_L4_error))
|
| 24 |
+
[C code generated for file line_trace.pyx, line 147, follows here]
|
| 25 |
+
|
| 26 |
+
The crux is that multiple source files can contribute code to a single C (or C++) file
|
| 27 |
+
(and thus, to a single extension module) besides the main module source file (.py/.pyx),
|
| 28 |
+
usually shared declaration files (.pxd) but also literally included files (.pxi).
|
| 29 |
+
|
| 30 |
+
Therefore, the coverage plugin doesn't actually try to look at the file that happened
|
| 31 |
+
to contribute the current source line for the trace call, but simply looks up the single
|
| 32 |
+
.c file from which the extension was compiled (which usually lies right next to it after
|
| 33 |
+
the build, having the same name), and parses the code copy comments from that .c file
|
| 34 |
+
to recover the original source files and their code as a line-to-file mapping.
|
| 35 |
+
|
| 36 |
+
That mapping is then used to report the ``__Pyx_TraceLine()`` calls to the coverage tool.
|
| 37 |
+
The plugin also reports the line of source code that it found in the C file to the coverage
|
| 38 |
+
tool to support annotated source representations. For this, again, it does not look at the
|
| 39 |
+
actual source files but only reports the source code that it found in the C code comments.
|
| 40 |
+
|
| 41 |
+
Apart from simplicity (read one file instead of finding and parsing many), part of the
|
| 42 |
+
reasoning here is that any line in the original sources for which there is no comment line
|
| 43 |
+
(and trace call) in the generated C code cannot count as executed, really, so the C code
|
| 44 |
+
comments are a very good source for coverage reporting. They already filter out purely
|
| 45 |
+
declarative code lines that do not contribute executable code, and such (missing) lines
|
| 46 |
+
can then be marked as excluded from coverage analysis.
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
from __future__ import absolute_import
|
| 50 |
+
|
| 51 |
+
import re
|
| 52 |
+
import os.path
|
| 53 |
+
import sys
|
| 54 |
+
from collections import defaultdict
|
| 55 |
+
|
| 56 |
+
from coverage.plugin import CoveragePlugin, FileTracer, FileReporter # requires coverage.py 4.0+
|
| 57 |
+
from coverage.files import canonical_filename
|
| 58 |
+
|
| 59 |
+
from .Utils import find_root_package_dir, is_package_dir, is_cython_generated_file, open_source_file
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
from . import __version__
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
C_FILE_EXTENSIONS = ['.c', '.cpp', '.cc', '.cxx']
|
| 66 |
+
MODULE_FILE_EXTENSIONS = set(['.py', '.pyx', '.pxd'] + C_FILE_EXTENSIONS)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def _find_c_source(base_path):
|
| 70 |
+
file_exists = os.path.exists
|
| 71 |
+
for ext in C_FILE_EXTENSIONS:
|
| 72 |
+
file_name = base_path + ext
|
| 73 |
+
if file_exists(file_name):
|
| 74 |
+
return file_name
|
| 75 |
+
return None
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def _find_dep_file_path(main_file, file_path, relative_path_search=False):
|
| 79 |
+
abs_path = os.path.abspath(file_path)
|
| 80 |
+
if not os.path.exists(abs_path) and (file_path.endswith('.pxi') or
|
| 81 |
+
relative_path_search):
|
| 82 |
+
# files are looked up relative to the main source file
|
| 83 |
+
rel_file_path = os.path.join(os.path.dirname(main_file), file_path)
|
| 84 |
+
if os.path.exists(rel_file_path):
|
| 85 |
+
abs_path = os.path.abspath(rel_file_path)
|
| 86 |
+
|
| 87 |
+
abs_no_ext = os.path.splitext(abs_path)[0]
|
| 88 |
+
file_no_ext, extension = os.path.splitext(file_path)
|
| 89 |
+
# We check if the paths match by matching the directories in reverse order.
|
| 90 |
+
# pkg/module.pyx /long/absolute_path/bla/bla/site-packages/pkg/module.c should match.
|
| 91 |
+
# this will match the pairs: module-module and pkg-pkg. After which there is nothing left to zip.
|
| 92 |
+
abs_no_ext = os.path.normpath(abs_no_ext)
|
| 93 |
+
file_no_ext = os.path.normpath(file_no_ext)
|
| 94 |
+
matching_paths = zip(reversed(abs_no_ext.split(os.sep)), reversed(file_no_ext.split(os.sep)))
|
| 95 |
+
for one, other in matching_paths:
|
| 96 |
+
if one != other:
|
| 97 |
+
break
|
| 98 |
+
else: # No mismatches detected
|
| 99 |
+
matching_abs_path = os.path.splitext(main_file)[0] + extension
|
| 100 |
+
if os.path.exists(matching_abs_path):
|
| 101 |
+
return canonical_filename(matching_abs_path)
|
| 102 |
+
|
| 103 |
+
# search sys.path for external locations if a valid file hasn't been found
|
| 104 |
+
if not os.path.exists(abs_path):
|
| 105 |
+
for sys_path in sys.path:
|
| 106 |
+
test_path = os.path.realpath(os.path.join(sys_path, file_path))
|
| 107 |
+
if os.path.exists(test_path):
|
| 108 |
+
return canonical_filename(test_path)
|
| 109 |
+
return canonical_filename(abs_path)
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
class Plugin(CoveragePlugin):
|
| 113 |
+
# map from traced file paths to absolute file paths
|
| 114 |
+
_file_path_map = None
|
| 115 |
+
# map from traced file paths to corresponding C files
|
| 116 |
+
_c_files_map = None
|
| 117 |
+
# map from parsed C files to their content
|
| 118 |
+
_parsed_c_files = None
|
| 119 |
+
# map from traced files to lines that are excluded from coverage
|
| 120 |
+
_excluded_lines_map = None
|
| 121 |
+
# list of regex patterns for lines to exclude
|
| 122 |
+
_excluded_line_patterns = ()
|
| 123 |
+
|
| 124 |
+
def sys_info(self):
|
| 125 |
+
return [('Cython version', __version__)]
|
| 126 |
+
|
| 127 |
+
def configure(self, config):
|
| 128 |
+
# Entry point for coverage "configurer".
|
| 129 |
+
# Read the regular expressions from the coverage config that match lines to be excluded from coverage.
|
| 130 |
+
self._excluded_line_patterns = config.get_option("report:exclude_lines")
|
| 131 |
+
|
| 132 |
+
def file_tracer(self, filename):
|
| 133 |
+
"""
|
| 134 |
+
Try to find a C source file for a file path found by the tracer.
|
| 135 |
+
"""
|
| 136 |
+
if filename.startswith('<') or filename.startswith('memory:'):
|
| 137 |
+
return None
|
| 138 |
+
c_file = py_file = None
|
| 139 |
+
filename = canonical_filename(os.path.abspath(filename))
|
| 140 |
+
if self._c_files_map and filename in self._c_files_map:
|
| 141 |
+
c_file = self._c_files_map[filename][0]
|
| 142 |
+
|
| 143 |
+
if c_file is None:
|
| 144 |
+
c_file, py_file = self._find_source_files(filename)
|
| 145 |
+
if not c_file:
|
| 146 |
+
return None # unknown file
|
| 147 |
+
|
| 148 |
+
# parse all source file paths and lines from C file
|
| 149 |
+
# to learn about all relevant source files right away (pyx/pxi/pxd)
|
| 150 |
+
# FIXME: this might already be too late if the first executed line
|
| 151 |
+
# is not from the main .pyx file but a file with a different
|
| 152 |
+
# name than the .c file (which prevents us from finding the
|
| 153 |
+
# .c file)
|
| 154 |
+
_, code = self._read_source_lines(c_file, filename)
|
| 155 |
+
if code is None:
|
| 156 |
+
return None # no source found
|
| 157 |
+
|
| 158 |
+
if self._file_path_map is None:
|
| 159 |
+
self._file_path_map = {}
|
| 160 |
+
return CythonModuleTracer(filename, py_file, c_file, self._c_files_map, self._file_path_map)
|
| 161 |
+
|
| 162 |
+
def file_reporter(self, filename):
|
| 163 |
+
# TODO: let coverage.py handle .py files itself
|
| 164 |
+
#ext = os.path.splitext(filename)[1].lower()
|
| 165 |
+
#if ext == '.py':
|
| 166 |
+
# from coverage.python import PythonFileReporter
|
| 167 |
+
# return PythonFileReporter(filename)
|
| 168 |
+
|
| 169 |
+
filename = canonical_filename(os.path.abspath(filename))
|
| 170 |
+
if self._c_files_map and filename in self._c_files_map:
|
| 171 |
+
c_file, rel_file_path, code = self._c_files_map[filename]
|
| 172 |
+
else:
|
| 173 |
+
c_file, _ = self._find_source_files(filename)
|
| 174 |
+
if not c_file:
|
| 175 |
+
return None # unknown file
|
| 176 |
+
rel_file_path, code = self._read_source_lines(c_file, filename)
|
| 177 |
+
if code is None:
|
| 178 |
+
return None # no source found
|
| 179 |
+
return CythonModuleReporter(
|
| 180 |
+
c_file,
|
| 181 |
+
filename,
|
| 182 |
+
rel_file_path,
|
| 183 |
+
code,
|
| 184 |
+
self._excluded_lines_map.get(rel_file_path, frozenset())
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
def _find_source_files(self, filename):
|
| 188 |
+
basename, ext = os.path.splitext(filename)
|
| 189 |
+
ext = ext.lower()
|
| 190 |
+
if ext in MODULE_FILE_EXTENSIONS:
|
| 191 |
+
pass
|
| 192 |
+
elif ext == '.pyd':
|
| 193 |
+
# Windows extension module
|
| 194 |
+
platform_suffix = re.search(r'[.]cp[0-9]+-win[_a-z0-9]*$', basename, re.I)
|
| 195 |
+
if platform_suffix:
|
| 196 |
+
basename = basename[:platform_suffix.start()]
|
| 197 |
+
elif ext == '.so':
|
| 198 |
+
# Linux/Unix/Mac extension module
|
| 199 |
+
platform_suffix = re.search(r'[.](?:cpython|pypy)-[0-9]+[-_a-z0-9]*$', basename, re.I)
|
| 200 |
+
if platform_suffix:
|
| 201 |
+
basename = basename[:platform_suffix.start()]
|
| 202 |
+
elif ext == '.pxi':
|
| 203 |
+
# if we get here, it means that the first traced line of a Cython module was
|
| 204 |
+
# not in the main module but in an include file, so try a little harder to
|
| 205 |
+
# find the main source file
|
| 206 |
+
self._find_c_source_files(os.path.dirname(filename), filename)
|
| 207 |
+
if filename in self._c_files_map:
|
| 208 |
+
return self._c_files_map[filename][0], None
|
| 209 |
+
else:
|
| 210 |
+
# none of our business
|
| 211 |
+
return None, None
|
| 212 |
+
|
| 213 |
+
c_file = filename if ext in C_FILE_EXTENSIONS else _find_c_source(basename)
|
| 214 |
+
if c_file is None:
|
| 215 |
+
# a module "pkg/mod.so" can have a source file "pkg/pkg.mod.c"
|
| 216 |
+
package_root = find_root_package_dir.uncached(filename)
|
| 217 |
+
package_path = os.path.relpath(basename, package_root).split(os.path.sep)
|
| 218 |
+
if len(package_path) > 1:
|
| 219 |
+
test_basepath = os.path.join(os.path.dirname(filename), '.'.join(package_path))
|
| 220 |
+
c_file = _find_c_source(test_basepath)
|
| 221 |
+
|
| 222 |
+
py_source_file = None
|
| 223 |
+
if c_file:
|
| 224 |
+
py_source_file = os.path.splitext(c_file)[0] + '.py'
|
| 225 |
+
if not os.path.exists(py_source_file):
|
| 226 |
+
py_source_file = None
|
| 227 |
+
if not is_cython_generated_file(c_file, if_not_found=False):
|
| 228 |
+
if py_source_file and os.path.exists(c_file):
|
| 229 |
+
# if we did not generate the C file,
|
| 230 |
+
# then we probably also shouldn't care about the .py file.
|
| 231 |
+
py_source_file = None
|
| 232 |
+
c_file = None
|
| 233 |
+
|
| 234 |
+
return c_file, py_source_file
|
| 235 |
+
|
| 236 |
+
def _find_c_source_files(self, dir_path, source_file):
|
| 237 |
+
"""
|
| 238 |
+
Desperately parse all C files in the directory or its package parents
|
| 239 |
+
(not re-descending) to find the (included) source file in one of them.
|
| 240 |
+
"""
|
| 241 |
+
if not os.path.isdir(dir_path):
|
| 242 |
+
return
|
| 243 |
+
splitext = os.path.splitext
|
| 244 |
+
for filename in os.listdir(dir_path):
|
| 245 |
+
ext = splitext(filename)[1].lower()
|
| 246 |
+
if ext in C_FILE_EXTENSIONS:
|
| 247 |
+
self._read_source_lines(os.path.join(dir_path, filename), source_file)
|
| 248 |
+
if source_file in self._c_files_map:
|
| 249 |
+
return
|
| 250 |
+
# not found? then try one package up
|
| 251 |
+
if is_package_dir(dir_path):
|
| 252 |
+
self._find_c_source_files(os.path.dirname(dir_path), source_file)
|
| 253 |
+
|
| 254 |
+
def _read_source_lines(self, c_file, sourcefile):
|
| 255 |
+
"""
|
| 256 |
+
Parse a Cython generated C/C++ source file and find the executable lines.
|
| 257 |
+
Each executable line starts with a comment header that states source file
|
| 258 |
+
and line number, as well as the surrounding range of source code lines.
|
| 259 |
+
"""
|
| 260 |
+
if self._parsed_c_files is None:
|
| 261 |
+
self._parsed_c_files = {}
|
| 262 |
+
if c_file in self._parsed_c_files:
|
| 263 |
+
code_lines = self._parsed_c_files[c_file]
|
| 264 |
+
else:
|
| 265 |
+
code_lines = self._parse_cfile_lines(c_file)
|
| 266 |
+
self._parsed_c_files[c_file] = code_lines
|
| 267 |
+
|
| 268 |
+
if self._c_files_map is None:
|
| 269 |
+
self._c_files_map = {}
|
| 270 |
+
|
| 271 |
+
for filename, code in code_lines.items():
|
| 272 |
+
abs_path = _find_dep_file_path(c_file, filename,
|
| 273 |
+
relative_path_search=True)
|
| 274 |
+
self._c_files_map[abs_path] = (c_file, filename, code)
|
| 275 |
+
|
| 276 |
+
if sourcefile not in self._c_files_map:
|
| 277 |
+
return (None,) * 2 # e.g. shared library file
|
| 278 |
+
return self._c_files_map[sourcefile][1:]
|
| 279 |
+
|
| 280 |
+
def _parse_cfile_lines(self, c_file):
|
| 281 |
+
"""
|
| 282 |
+
Parse a C file and extract all source file lines that generated executable code.
|
| 283 |
+
"""
|
| 284 |
+
match_source_path_line = re.compile(r' */[*] +"(.*)":([0-9]+)$').match
|
| 285 |
+
match_current_code_line = re.compile(r' *[*] (.*) # <<<<<<+$').match
|
| 286 |
+
match_comment_end = re.compile(r' *[*]/$').match
|
| 287 |
+
match_trace_line = re.compile(r' *__Pyx_TraceLine\(([0-9]+),').match
|
| 288 |
+
not_executable = re.compile(
|
| 289 |
+
r'\s*c(?:type)?def\s+'
|
| 290 |
+
r'(?:(?:public|external)\s+)?'
|
| 291 |
+
r'(?:struct|union|enum|class)'
|
| 292 |
+
r'(\s+[^:]+|)\s*:'
|
| 293 |
+
).match
|
| 294 |
+
if self._excluded_line_patterns:
|
| 295 |
+
line_is_excluded = re.compile("|".join(["(?:%s)" % regex for regex in self._excluded_line_patterns])).search
|
| 296 |
+
else:
|
| 297 |
+
line_is_excluded = lambda line: False
|
| 298 |
+
|
| 299 |
+
code_lines = defaultdict(dict)
|
| 300 |
+
executable_lines = defaultdict(set)
|
| 301 |
+
current_filename = None
|
| 302 |
+
if self._excluded_lines_map is None:
|
| 303 |
+
self._excluded_lines_map = defaultdict(set)
|
| 304 |
+
|
| 305 |
+
with open(c_file) as lines:
|
| 306 |
+
lines = iter(lines)
|
| 307 |
+
for line in lines:
|
| 308 |
+
match = match_source_path_line(line)
|
| 309 |
+
if not match:
|
| 310 |
+
if '__Pyx_TraceLine(' in line and current_filename is not None:
|
| 311 |
+
trace_line = match_trace_line(line)
|
| 312 |
+
if trace_line:
|
| 313 |
+
executable_lines[current_filename].add(int(trace_line.group(1)))
|
| 314 |
+
continue
|
| 315 |
+
filename, lineno = match.groups()
|
| 316 |
+
current_filename = filename
|
| 317 |
+
lineno = int(lineno)
|
| 318 |
+
for comment_line in lines:
|
| 319 |
+
match = match_current_code_line(comment_line)
|
| 320 |
+
if match:
|
| 321 |
+
code_line = match.group(1).rstrip()
|
| 322 |
+
if not_executable(code_line):
|
| 323 |
+
break
|
| 324 |
+
if line_is_excluded(code_line):
|
| 325 |
+
self._excluded_lines_map[filename].add(lineno)
|
| 326 |
+
break
|
| 327 |
+
code_lines[filename][lineno] = code_line
|
| 328 |
+
break
|
| 329 |
+
elif match_comment_end(comment_line):
|
| 330 |
+
# unexpected comment format - false positive?
|
| 331 |
+
break
|
| 332 |
+
|
| 333 |
+
# Remove lines that generated code but are not traceable.
|
| 334 |
+
for filename, lines in code_lines.items():
|
| 335 |
+
dead_lines = set(lines).difference(executable_lines.get(filename, ()))
|
| 336 |
+
for lineno in dead_lines:
|
| 337 |
+
del lines[lineno]
|
| 338 |
+
return code_lines
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
class CythonModuleTracer(FileTracer):
|
| 342 |
+
"""
|
| 343 |
+
Find the Python/Cython source file for a Cython module.
|
| 344 |
+
"""
|
| 345 |
+
def __init__(self, module_file, py_file, c_file, c_files_map, file_path_map):
|
| 346 |
+
super(CythonModuleTracer, self).__init__()
|
| 347 |
+
self.module_file = module_file
|
| 348 |
+
self.py_file = py_file
|
| 349 |
+
self.c_file = c_file
|
| 350 |
+
self._c_files_map = c_files_map
|
| 351 |
+
self._file_path_map = file_path_map
|
| 352 |
+
|
| 353 |
+
def has_dynamic_source_filename(self):
|
| 354 |
+
return True
|
| 355 |
+
|
| 356 |
+
def dynamic_source_filename(self, filename, frame):
|
| 357 |
+
"""
|
| 358 |
+
Determine source file path. Called by the function call tracer.
|
| 359 |
+
"""
|
| 360 |
+
source_file = frame.f_code.co_filename
|
| 361 |
+
try:
|
| 362 |
+
return self._file_path_map[source_file]
|
| 363 |
+
except KeyError:
|
| 364 |
+
pass
|
| 365 |
+
abs_path = _find_dep_file_path(filename, source_file)
|
| 366 |
+
|
| 367 |
+
if self.py_file and source_file[-3:].lower() == '.py':
|
| 368 |
+
# always let coverage.py handle this case itself
|
| 369 |
+
self._file_path_map[source_file] = self.py_file
|
| 370 |
+
return self.py_file
|
| 371 |
+
|
| 372 |
+
assert self._c_files_map is not None
|
| 373 |
+
if abs_path not in self._c_files_map:
|
| 374 |
+
self._c_files_map[abs_path] = (self.c_file, source_file, None)
|
| 375 |
+
self._file_path_map[source_file] = abs_path
|
| 376 |
+
return abs_path
|
| 377 |
+
|
| 378 |
+
|
| 379 |
+
class CythonModuleReporter(FileReporter):
|
| 380 |
+
"""
|
| 381 |
+
Provide detailed trace information for one source file to coverage.py.
|
| 382 |
+
"""
|
| 383 |
+
def __init__(self, c_file, source_file, rel_file_path, code, excluded_lines):
|
| 384 |
+
super(CythonModuleReporter, self).__init__(source_file)
|
| 385 |
+
self.name = rel_file_path
|
| 386 |
+
self.c_file = c_file
|
| 387 |
+
self._code = code
|
| 388 |
+
self._excluded_lines = excluded_lines
|
| 389 |
+
|
| 390 |
+
def lines(self):
|
| 391 |
+
"""
|
| 392 |
+
Return set of line numbers that are possibly executable.
|
| 393 |
+
"""
|
| 394 |
+
return set(self._code)
|
| 395 |
+
|
| 396 |
+
def excluded_lines(self):
|
| 397 |
+
"""
|
| 398 |
+
Return set of line numbers that are excluded from coverage.
|
| 399 |
+
"""
|
| 400 |
+
return self._excluded_lines
|
| 401 |
+
|
| 402 |
+
def _iter_source_tokens(self):
|
| 403 |
+
current_line = 1
|
| 404 |
+
for line_no, code_line in sorted(self._code.items()):
|
| 405 |
+
while line_no > current_line:
|
| 406 |
+
yield []
|
| 407 |
+
current_line += 1
|
| 408 |
+
yield [('txt', code_line)]
|
| 409 |
+
current_line += 1
|
| 410 |
+
|
| 411 |
+
def source(self):
|
| 412 |
+
"""
|
| 413 |
+
Return the source code of the file as a string.
|
| 414 |
+
"""
|
| 415 |
+
if os.path.exists(self.filename):
|
| 416 |
+
with open_source_file(self.filename) as f:
|
| 417 |
+
return f.read()
|
| 418 |
+
else:
|
| 419 |
+
return '\n'.join(
|
| 420 |
+
(tokens[0][1] if tokens else '')
|
| 421 |
+
for tokens in self._iter_source_tokens())
|
| 422 |
+
|
| 423 |
+
def source_token_lines(self):
|
| 424 |
+
"""
|
| 425 |
+
Iterate over the source code tokens.
|
| 426 |
+
"""
|
| 427 |
+
if os.path.exists(self.filename):
|
| 428 |
+
with open_source_file(self.filename) as f:
|
| 429 |
+
for line in f:
|
| 430 |
+
yield [('txt', line.rstrip('\n'))]
|
| 431 |
+
else:
|
| 432 |
+
for line in self._iter_source_tokens():
|
| 433 |
+
yield [('txt', line)]
|
| 434 |
+
|
| 435 |
+
|
| 436 |
+
def coverage_init(reg, options):
|
| 437 |
+
plugin = Plugin()
|
| 438 |
+
reg.add_configurer(plugin)
|
| 439 |
+
reg.add_file_tracer(plugin)
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/__pycache__/covering.cpython-311.pyc
ADDED
|
Binary file (2.7 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/__pycache__/extendability.cpython-311.pyc
ADDED
|
Binary file (5.53 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/__pycache__/generators.cpython-311.pyc
ADDED
|
Binary file (29.2 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/__pycache__/spectral.cpython-311.pyc
ADDED
|
Binary file (2.69 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/centrality.py
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import networkx as nx
|
| 2 |
+
|
| 3 |
+
__all__ = ["degree_centrality", "betweenness_centrality", "closeness_centrality"]
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
@nx._dispatch(name="bipartite_degree_centrality")
|
| 7 |
+
def degree_centrality(G, nodes):
|
| 8 |
+
r"""Compute the degree centrality for nodes in a bipartite network.
|
| 9 |
+
|
| 10 |
+
The degree centrality for a node `v` is the fraction of nodes
|
| 11 |
+
connected to it.
|
| 12 |
+
|
| 13 |
+
Parameters
|
| 14 |
+
----------
|
| 15 |
+
G : graph
|
| 16 |
+
A bipartite network
|
| 17 |
+
|
| 18 |
+
nodes : list or container
|
| 19 |
+
Container with all nodes in one bipartite node set.
|
| 20 |
+
|
| 21 |
+
Returns
|
| 22 |
+
-------
|
| 23 |
+
centrality : dictionary
|
| 24 |
+
Dictionary keyed by node with bipartite degree centrality as the value.
|
| 25 |
+
|
| 26 |
+
Examples
|
| 27 |
+
--------
|
| 28 |
+
>>> G = nx.wheel_graph(5)
|
| 29 |
+
>>> top_nodes = {0, 1, 2}
|
| 30 |
+
>>> nx.bipartite.degree_centrality(G, nodes=top_nodes)
|
| 31 |
+
{0: 2.0, 1: 1.5, 2: 1.5, 3: 1.0, 4: 1.0}
|
| 32 |
+
|
| 33 |
+
See Also
|
| 34 |
+
--------
|
| 35 |
+
betweenness_centrality
|
| 36 |
+
closeness_centrality
|
| 37 |
+
:func:`~networkx.algorithms.bipartite.basic.sets`
|
| 38 |
+
:func:`~networkx.algorithms.bipartite.basic.is_bipartite`
|
| 39 |
+
|
| 40 |
+
Notes
|
| 41 |
+
-----
|
| 42 |
+
The nodes input parameter must contain all nodes in one bipartite node set,
|
| 43 |
+
but the dictionary returned contains all nodes from both bipartite node
|
| 44 |
+
sets. See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
|
| 45 |
+
for further details on how bipartite graphs are handled in NetworkX.
|
| 46 |
+
|
| 47 |
+
For unipartite networks, the degree centrality values are
|
| 48 |
+
normalized by dividing by the maximum possible degree (which is
|
| 49 |
+
`n-1` where `n` is the number of nodes in G).
|
| 50 |
+
|
| 51 |
+
In the bipartite case, the maximum possible degree of a node in a
|
| 52 |
+
bipartite node set is the number of nodes in the opposite node set
|
| 53 |
+
[1]_. The degree centrality for a node `v` in the bipartite
|
| 54 |
+
sets `U` with `n` nodes and `V` with `m` nodes is
|
| 55 |
+
|
| 56 |
+
.. math::
|
| 57 |
+
|
| 58 |
+
d_{v} = \frac{deg(v)}{m}, \mbox{for} v \in U ,
|
| 59 |
+
|
| 60 |
+
d_{v} = \frac{deg(v)}{n}, \mbox{for} v \in V ,
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
where `deg(v)` is the degree of node `v`.
|
| 64 |
+
|
| 65 |
+
References
|
| 66 |
+
----------
|
| 67 |
+
.. [1] Borgatti, S.P. and Halgin, D. In press. "Analyzing Affiliation
|
| 68 |
+
Networks". In Carrington, P. and Scott, J. (eds) The Sage Handbook
|
| 69 |
+
of Social Network Analysis. Sage Publications.
|
| 70 |
+
https://dx.doi.org/10.4135/9781446294413.n28
|
| 71 |
+
"""
|
| 72 |
+
top = set(nodes)
|
| 73 |
+
bottom = set(G) - top
|
| 74 |
+
s = 1.0 / len(bottom)
|
| 75 |
+
centrality = {n: d * s for n, d in G.degree(top)}
|
| 76 |
+
s = 1.0 / len(top)
|
| 77 |
+
centrality.update({n: d * s for n, d in G.degree(bottom)})
|
| 78 |
+
return centrality
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@nx._dispatch(name="bipartite_betweenness_centrality")
|
| 82 |
+
def betweenness_centrality(G, nodes):
|
| 83 |
+
r"""Compute betweenness centrality for nodes in a bipartite network.
|
| 84 |
+
|
| 85 |
+
Betweenness centrality of a node `v` is the sum of the
|
| 86 |
+
fraction of all-pairs shortest paths that pass through `v`.
|
| 87 |
+
|
| 88 |
+
Values of betweenness are normalized by the maximum possible
|
| 89 |
+
value which for bipartite graphs is limited by the relative size
|
| 90 |
+
of the two node sets [1]_.
|
| 91 |
+
|
| 92 |
+
Let `n` be the number of nodes in the node set `U` and
|
| 93 |
+
`m` be the number of nodes in the node set `V`, then
|
| 94 |
+
nodes in `U` are normalized by dividing by
|
| 95 |
+
|
| 96 |
+
.. math::
|
| 97 |
+
|
| 98 |
+
\frac{1}{2} [m^2 (s + 1)^2 + m (s + 1)(2t - s - 1) - t (2s - t + 3)] ,
|
| 99 |
+
|
| 100 |
+
where
|
| 101 |
+
|
| 102 |
+
.. math::
|
| 103 |
+
|
| 104 |
+
s = (n - 1) \div m , t = (n - 1) \mod m ,
|
| 105 |
+
|
| 106 |
+
and nodes in `V` are normalized by dividing by
|
| 107 |
+
|
| 108 |
+
.. math::
|
| 109 |
+
|
| 110 |
+
\frac{1}{2} [n^2 (p + 1)^2 + n (p + 1)(2r - p - 1) - r (2p - r + 3)] ,
|
| 111 |
+
|
| 112 |
+
where,
|
| 113 |
+
|
| 114 |
+
.. math::
|
| 115 |
+
|
| 116 |
+
p = (m - 1) \div n , r = (m - 1) \mod n .
|
| 117 |
+
|
| 118 |
+
Parameters
|
| 119 |
+
----------
|
| 120 |
+
G : graph
|
| 121 |
+
A bipartite graph
|
| 122 |
+
|
| 123 |
+
nodes : list or container
|
| 124 |
+
Container with all nodes in one bipartite node set.
|
| 125 |
+
|
| 126 |
+
Returns
|
| 127 |
+
-------
|
| 128 |
+
betweenness : dictionary
|
| 129 |
+
Dictionary keyed by node with bipartite betweenness centrality
|
| 130 |
+
as the value.
|
| 131 |
+
|
| 132 |
+
Examples
|
| 133 |
+
--------
|
| 134 |
+
>>> G = nx.cycle_graph(4)
|
| 135 |
+
>>> top_nodes = {1, 2}
|
| 136 |
+
>>> nx.bipartite.betweenness_centrality(G, nodes=top_nodes)
|
| 137 |
+
{0: 0.25, 1: 0.25, 2: 0.25, 3: 0.25}
|
| 138 |
+
|
| 139 |
+
See Also
|
| 140 |
+
--------
|
| 141 |
+
degree_centrality
|
| 142 |
+
closeness_centrality
|
| 143 |
+
:func:`~networkx.algorithms.bipartite.basic.sets`
|
| 144 |
+
:func:`~networkx.algorithms.bipartite.basic.is_bipartite`
|
| 145 |
+
|
| 146 |
+
Notes
|
| 147 |
+
-----
|
| 148 |
+
The nodes input parameter must contain all nodes in one bipartite node set,
|
| 149 |
+
but the dictionary returned contains all nodes from both node sets.
|
| 150 |
+
See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
|
| 151 |
+
for further details on how bipartite graphs are handled in NetworkX.
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
References
|
| 155 |
+
----------
|
| 156 |
+
.. [1] Borgatti, S.P. and Halgin, D. In press. "Analyzing Affiliation
|
| 157 |
+
Networks". In Carrington, P. and Scott, J. (eds) The Sage Handbook
|
| 158 |
+
of Social Network Analysis. Sage Publications.
|
| 159 |
+
https://dx.doi.org/10.4135/9781446294413.n28
|
| 160 |
+
"""
|
| 161 |
+
top = set(nodes)
|
| 162 |
+
bottom = set(G) - top
|
| 163 |
+
n = len(top)
|
| 164 |
+
m = len(bottom)
|
| 165 |
+
s, t = divmod(n - 1, m)
|
| 166 |
+
bet_max_top = (
|
| 167 |
+
((m**2) * ((s + 1) ** 2))
|
| 168 |
+
+ (m * (s + 1) * (2 * t - s - 1))
|
| 169 |
+
- (t * ((2 * s) - t + 3))
|
| 170 |
+
) / 2.0
|
| 171 |
+
p, r = divmod(m - 1, n)
|
| 172 |
+
bet_max_bot = (
|
| 173 |
+
((n**2) * ((p + 1) ** 2))
|
| 174 |
+
+ (n * (p + 1) * (2 * r - p - 1))
|
| 175 |
+
- (r * ((2 * p) - r + 3))
|
| 176 |
+
) / 2.0
|
| 177 |
+
betweenness = nx.betweenness_centrality(G, normalized=False, weight=None)
|
| 178 |
+
for node in top:
|
| 179 |
+
betweenness[node] /= bet_max_top
|
| 180 |
+
for node in bottom:
|
| 181 |
+
betweenness[node] /= bet_max_bot
|
| 182 |
+
return betweenness
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
@nx._dispatch(name="bipartite_closeness_centrality")
|
| 186 |
+
def closeness_centrality(G, nodes, normalized=True):
|
| 187 |
+
r"""Compute the closeness centrality for nodes in a bipartite network.
|
| 188 |
+
|
| 189 |
+
The closeness of a node is the distance to all other nodes in the
|
| 190 |
+
graph or in the case that the graph is not connected to all other nodes
|
| 191 |
+
in the connected component containing that node.
|
| 192 |
+
|
| 193 |
+
Parameters
|
| 194 |
+
----------
|
| 195 |
+
G : graph
|
| 196 |
+
A bipartite network
|
| 197 |
+
|
| 198 |
+
nodes : list or container
|
| 199 |
+
Container with all nodes in one bipartite node set.
|
| 200 |
+
|
| 201 |
+
normalized : bool, optional
|
| 202 |
+
If True (default) normalize by connected component size.
|
| 203 |
+
|
| 204 |
+
Returns
|
| 205 |
+
-------
|
| 206 |
+
closeness : dictionary
|
| 207 |
+
Dictionary keyed by node with bipartite closeness centrality
|
| 208 |
+
as the value.
|
| 209 |
+
|
| 210 |
+
Examples
|
| 211 |
+
--------
|
| 212 |
+
>>> G = nx.wheel_graph(5)
|
| 213 |
+
>>> top_nodes = {0, 1, 2}
|
| 214 |
+
>>> nx.bipartite.closeness_centrality(G, nodes=top_nodes)
|
| 215 |
+
{0: 1.5, 1: 1.2, 2: 1.2, 3: 1.0, 4: 1.0}
|
| 216 |
+
|
| 217 |
+
See Also
|
| 218 |
+
--------
|
| 219 |
+
betweenness_centrality
|
| 220 |
+
degree_centrality
|
| 221 |
+
:func:`~networkx.algorithms.bipartite.basic.sets`
|
| 222 |
+
:func:`~networkx.algorithms.bipartite.basic.is_bipartite`
|
| 223 |
+
|
| 224 |
+
Notes
|
| 225 |
+
-----
|
| 226 |
+
The nodes input parameter must contain all nodes in one bipartite node set,
|
| 227 |
+
but the dictionary returned contains all nodes from both node sets.
|
| 228 |
+
See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
|
| 229 |
+
for further details on how bipartite graphs are handled in NetworkX.
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
Closeness centrality is normalized by the minimum distance possible.
|
| 233 |
+
In the bipartite case the minimum distance for a node in one bipartite
|
| 234 |
+
node set is 1 from all nodes in the other node set and 2 from all
|
| 235 |
+
other nodes in its own set [1]_. Thus the closeness centrality
|
| 236 |
+
for node `v` in the two bipartite sets `U` with
|
| 237 |
+
`n` nodes and `V` with `m` nodes is
|
| 238 |
+
|
| 239 |
+
.. math::
|
| 240 |
+
|
| 241 |
+
c_{v} = \frac{m + 2(n - 1)}{d}, \mbox{for} v \in U,
|
| 242 |
+
|
| 243 |
+
c_{v} = \frac{n + 2(m - 1)}{d}, \mbox{for} v \in V,
|
| 244 |
+
|
| 245 |
+
where `d` is the sum of the distances from `v` to all
|
| 246 |
+
other nodes.
|
| 247 |
+
|
| 248 |
+
Higher values of closeness indicate higher centrality.
|
| 249 |
+
|
| 250 |
+
As in the unipartite case, setting normalized=True causes the
|
| 251 |
+
values to normalized further to n-1 / size(G)-1 where n is the
|
| 252 |
+
number of nodes in the connected part of graph containing the
|
| 253 |
+
node. If the graph is not completely connected, this algorithm
|
| 254 |
+
computes the closeness centrality for each connected part
|
| 255 |
+
separately.
|
| 256 |
+
|
| 257 |
+
References
|
| 258 |
+
----------
|
| 259 |
+
.. [1] Borgatti, S.P. and Halgin, D. In press. "Analyzing Affiliation
|
| 260 |
+
Networks". In Carrington, P. and Scott, J. (eds) The Sage Handbook
|
| 261 |
+
of Social Network Analysis. Sage Publications.
|
| 262 |
+
https://dx.doi.org/10.4135/9781446294413.n28
|
| 263 |
+
"""
|
| 264 |
+
closeness = {}
|
| 265 |
+
path_length = nx.single_source_shortest_path_length
|
| 266 |
+
top = set(nodes)
|
| 267 |
+
bottom = set(G) - top
|
| 268 |
+
n = len(top)
|
| 269 |
+
m = len(bottom)
|
| 270 |
+
for node in top:
|
| 271 |
+
sp = dict(path_length(G, node))
|
| 272 |
+
totsp = sum(sp.values())
|
| 273 |
+
if totsp > 0.0 and len(G) > 1:
|
| 274 |
+
closeness[node] = (m + 2 * (n - 1)) / totsp
|
| 275 |
+
if normalized:
|
| 276 |
+
s = (len(sp) - 1) / (len(G) - 1)
|
| 277 |
+
closeness[node] *= s
|
| 278 |
+
else:
|
| 279 |
+
closeness[node] = 0.0
|
| 280 |
+
for node in bottom:
|
| 281 |
+
sp = dict(path_length(G, node))
|
| 282 |
+
totsp = sum(sp.values())
|
| 283 |
+
if totsp > 0.0 and len(G) > 1:
|
| 284 |
+
closeness[node] = (n + 2 * (m - 1)) / totsp
|
| 285 |
+
if normalized:
|
| 286 |
+
s = (len(sp) - 1) / (len(G) - 1)
|
| 287 |
+
closeness[node] *= s
|
| 288 |
+
else:
|
| 289 |
+
closeness[node] = 0.0
|
| 290 |
+
return closeness
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/tests/test_edgelist.py
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Unit tests for bipartite edgelists.
|
| 3 |
+
"""
|
| 4 |
+
import io
|
| 5 |
+
import os
|
| 6 |
+
import tempfile
|
| 7 |
+
|
| 8 |
+
import pytest
|
| 9 |
+
|
| 10 |
+
import networkx as nx
|
| 11 |
+
from networkx.algorithms import bipartite
|
| 12 |
+
from networkx.utils import edges_equal, graphs_equal, nodes_equal
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class TestEdgelist:
|
| 16 |
+
@classmethod
|
| 17 |
+
def setup_class(cls):
|
| 18 |
+
cls.G = nx.Graph(name="test")
|
| 19 |
+
e = [("a", "b"), ("b", "c"), ("c", "d"), ("d", "e"), ("e", "f"), ("a", "f")]
|
| 20 |
+
cls.G.add_edges_from(e)
|
| 21 |
+
cls.G.add_nodes_from(["a", "c", "e"], bipartite=0)
|
| 22 |
+
cls.G.add_nodes_from(["b", "d", "f"], bipartite=1)
|
| 23 |
+
cls.G.add_node("g", bipartite=0)
|
| 24 |
+
cls.DG = nx.DiGraph(cls.G)
|
| 25 |
+
cls.MG = nx.MultiGraph()
|
| 26 |
+
cls.MG.add_edges_from([(1, 2), (1, 2), (1, 2)])
|
| 27 |
+
cls.MG.add_node(1, bipartite=0)
|
| 28 |
+
cls.MG.add_node(2, bipartite=1)
|
| 29 |
+
|
| 30 |
+
def test_read_edgelist_1(self):
|
| 31 |
+
s = b"""\
|
| 32 |
+
# comment line
|
| 33 |
+
1 2
|
| 34 |
+
# comment line
|
| 35 |
+
2 3
|
| 36 |
+
"""
|
| 37 |
+
bytesIO = io.BytesIO(s)
|
| 38 |
+
G = bipartite.read_edgelist(bytesIO, nodetype=int)
|
| 39 |
+
assert edges_equal(G.edges(), [(1, 2), (2, 3)])
|
| 40 |
+
|
| 41 |
+
def test_read_edgelist_3(self):
|
| 42 |
+
s = b"""\
|
| 43 |
+
# comment line
|
| 44 |
+
1 2 {'weight':2.0}
|
| 45 |
+
# comment line
|
| 46 |
+
2 3 {'weight':3.0}
|
| 47 |
+
"""
|
| 48 |
+
bytesIO = io.BytesIO(s)
|
| 49 |
+
G = bipartite.read_edgelist(bytesIO, nodetype=int, data=False)
|
| 50 |
+
assert edges_equal(G.edges(), [(1, 2), (2, 3)])
|
| 51 |
+
|
| 52 |
+
bytesIO = io.BytesIO(s)
|
| 53 |
+
G = bipartite.read_edgelist(bytesIO, nodetype=int, data=True)
|
| 54 |
+
assert edges_equal(
|
| 55 |
+
G.edges(data=True), [(1, 2, {"weight": 2.0}), (2, 3, {"weight": 3.0})]
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
def test_write_edgelist_1(self):
|
| 59 |
+
fh = io.BytesIO()
|
| 60 |
+
G = nx.Graph()
|
| 61 |
+
G.add_edges_from([(1, 2), (2, 3)])
|
| 62 |
+
G.add_node(1, bipartite=0)
|
| 63 |
+
G.add_node(2, bipartite=1)
|
| 64 |
+
G.add_node(3, bipartite=0)
|
| 65 |
+
bipartite.write_edgelist(G, fh, data=False)
|
| 66 |
+
fh.seek(0)
|
| 67 |
+
assert fh.read() == b"1 2\n3 2\n"
|
| 68 |
+
|
| 69 |
+
def test_write_edgelist_2(self):
|
| 70 |
+
fh = io.BytesIO()
|
| 71 |
+
G = nx.Graph()
|
| 72 |
+
G.add_edges_from([(1, 2), (2, 3)])
|
| 73 |
+
G.add_node(1, bipartite=0)
|
| 74 |
+
G.add_node(2, bipartite=1)
|
| 75 |
+
G.add_node(3, bipartite=0)
|
| 76 |
+
bipartite.write_edgelist(G, fh, data=True)
|
| 77 |
+
fh.seek(0)
|
| 78 |
+
assert fh.read() == b"1 2 {}\n3 2 {}\n"
|
| 79 |
+
|
| 80 |
+
def test_write_edgelist_3(self):
|
| 81 |
+
fh = io.BytesIO()
|
| 82 |
+
G = nx.Graph()
|
| 83 |
+
G.add_edge(1, 2, weight=2.0)
|
| 84 |
+
G.add_edge(2, 3, weight=3.0)
|
| 85 |
+
G.add_node(1, bipartite=0)
|
| 86 |
+
G.add_node(2, bipartite=1)
|
| 87 |
+
G.add_node(3, bipartite=0)
|
| 88 |
+
bipartite.write_edgelist(G, fh, data=True)
|
| 89 |
+
fh.seek(0)
|
| 90 |
+
assert fh.read() == b"1 2 {'weight': 2.0}\n3 2 {'weight': 3.0}\n"
|
| 91 |
+
|
| 92 |
+
def test_write_edgelist_4(self):
|
| 93 |
+
fh = io.BytesIO()
|
| 94 |
+
G = nx.Graph()
|
| 95 |
+
G.add_edge(1, 2, weight=2.0)
|
| 96 |
+
G.add_edge(2, 3, weight=3.0)
|
| 97 |
+
G.add_node(1, bipartite=0)
|
| 98 |
+
G.add_node(2, bipartite=1)
|
| 99 |
+
G.add_node(3, bipartite=0)
|
| 100 |
+
bipartite.write_edgelist(G, fh, data=[("weight")])
|
| 101 |
+
fh.seek(0)
|
| 102 |
+
assert fh.read() == b"1 2 2.0\n3 2 3.0\n"
|
| 103 |
+
|
| 104 |
+
def test_unicode(self):
|
| 105 |
+
G = nx.Graph()
|
| 106 |
+
name1 = chr(2344) + chr(123) + chr(6543)
|
| 107 |
+
name2 = chr(5543) + chr(1543) + chr(324)
|
| 108 |
+
G.add_edge(name1, "Radiohead", **{name2: 3})
|
| 109 |
+
G.add_node(name1, bipartite=0)
|
| 110 |
+
G.add_node("Radiohead", bipartite=1)
|
| 111 |
+
fd, fname = tempfile.mkstemp()
|
| 112 |
+
bipartite.write_edgelist(G, fname)
|
| 113 |
+
H = bipartite.read_edgelist(fname)
|
| 114 |
+
assert graphs_equal(G, H)
|
| 115 |
+
os.close(fd)
|
| 116 |
+
os.unlink(fname)
|
| 117 |
+
|
| 118 |
+
def test_latin1_issue(self):
|
| 119 |
+
G = nx.Graph()
|
| 120 |
+
name1 = chr(2344) + chr(123) + chr(6543)
|
| 121 |
+
name2 = chr(5543) + chr(1543) + chr(324)
|
| 122 |
+
G.add_edge(name1, "Radiohead", **{name2: 3})
|
| 123 |
+
G.add_node(name1, bipartite=0)
|
| 124 |
+
G.add_node("Radiohead", bipartite=1)
|
| 125 |
+
fd, fname = tempfile.mkstemp()
|
| 126 |
+
pytest.raises(
|
| 127 |
+
UnicodeEncodeError, bipartite.write_edgelist, G, fname, encoding="latin-1"
|
| 128 |
+
)
|
| 129 |
+
os.close(fd)
|
| 130 |
+
os.unlink(fname)
|
| 131 |
+
|
| 132 |
+
def test_latin1(self):
|
| 133 |
+
G = nx.Graph()
|
| 134 |
+
name1 = "Bj" + chr(246) + "rk"
|
| 135 |
+
name2 = chr(220) + "ber"
|
| 136 |
+
G.add_edge(name1, "Radiohead", **{name2: 3})
|
| 137 |
+
G.add_node(name1, bipartite=0)
|
| 138 |
+
G.add_node("Radiohead", bipartite=1)
|
| 139 |
+
fd, fname = tempfile.mkstemp()
|
| 140 |
+
bipartite.write_edgelist(G, fname, encoding="latin-1")
|
| 141 |
+
H = bipartite.read_edgelist(fname, encoding="latin-1")
|
| 142 |
+
assert graphs_equal(G, H)
|
| 143 |
+
os.close(fd)
|
| 144 |
+
os.unlink(fname)
|
| 145 |
+
|
| 146 |
+
def test_edgelist_graph(self):
|
| 147 |
+
G = self.G
|
| 148 |
+
(fd, fname) = tempfile.mkstemp()
|
| 149 |
+
bipartite.write_edgelist(G, fname)
|
| 150 |
+
H = bipartite.read_edgelist(fname)
|
| 151 |
+
H2 = bipartite.read_edgelist(fname)
|
| 152 |
+
assert H is not H2 # they should be different graphs
|
| 153 |
+
G.remove_node("g") # isolated nodes are not written in edgelist
|
| 154 |
+
assert nodes_equal(list(H), list(G))
|
| 155 |
+
assert edges_equal(list(H.edges()), list(G.edges()))
|
| 156 |
+
os.close(fd)
|
| 157 |
+
os.unlink(fname)
|
| 158 |
+
|
| 159 |
+
def test_edgelist_integers(self):
|
| 160 |
+
G = nx.convert_node_labels_to_integers(self.G)
|
| 161 |
+
(fd, fname) = tempfile.mkstemp()
|
| 162 |
+
bipartite.write_edgelist(G, fname)
|
| 163 |
+
H = bipartite.read_edgelist(fname, nodetype=int)
|
| 164 |
+
# isolated nodes are not written in edgelist
|
| 165 |
+
G.remove_nodes_from(list(nx.isolates(G)))
|
| 166 |
+
assert nodes_equal(list(H), list(G))
|
| 167 |
+
assert edges_equal(list(H.edges()), list(G.edges()))
|
| 168 |
+
os.close(fd)
|
| 169 |
+
os.unlink(fname)
|
| 170 |
+
|
| 171 |
+
def test_edgelist_multigraph(self):
|
| 172 |
+
G = self.MG
|
| 173 |
+
(fd, fname) = tempfile.mkstemp()
|
| 174 |
+
bipartite.write_edgelist(G, fname)
|
| 175 |
+
H = bipartite.read_edgelist(fname, nodetype=int, create_using=nx.MultiGraph())
|
| 176 |
+
H2 = bipartite.read_edgelist(fname, nodetype=int, create_using=nx.MultiGraph())
|
| 177 |
+
assert H is not H2 # they should be different graphs
|
| 178 |
+
assert nodes_equal(list(H), list(G))
|
| 179 |
+
assert edges_equal(list(H.edges()), list(G.edges()))
|
| 180 |
+
os.close(fd)
|
| 181 |
+
os.unlink(fname)
|
| 182 |
+
|
| 183 |
+
def test_empty_digraph(self):
|
| 184 |
+
with pytest.raises(nx.NetworkXNotImplemented):
|
| 185 |
+
bytesIO = io.BytesIO()
|
| 186 |
+
bipartite.write_edgelist(nx.DiGraph(), bytesIO)
|
| 187 |
+
|
| 188 |
+
def test_raise_attribute(self):
|
| 189 |
+
with pytest.raises(AttributeError):
|
| 190 |
+
G = nx.path_graph(4)
|
| 191 |
+
bytesIO = io.BytesIO()
|
| 192 |
+
bipartite.write_edgelist(G, bytesIO)
|
| 193 |
+
|
| 194 |
+
def test_parse_edgelist(self):
|
| 195 |
+
"""Tests for conditions specific to
|
| 196 |
+
parse_edge_list method"""
|
| 197 |
+
|
| 198 |
+
# ignore strings of length less than 2
|
| 199 |
+
lines = ["1 2", "2 3", "3 1", "4", " "]
|
| 200 |
+
G = bipartite.parse_edgelist(lines, nodetype=int)
|
| 201 |
+
assert list(G.nodes) == [1, 2, 3]
|
| 202 |
+
|
| 203 |
+
# Exception raised when node is not convertible
|
| 204 |
+
# to specified data type
|
| 205 |
+
with pytest.raises(TypeError, match=".*Failed to convert nodes"):
|
| 206 |
+
lines = ["a b", "b c", "c a"]
|
| 207 |
+
G = bipartite.parse_edgelist(lines, nodetype=int)
|
| 208 |
+
|
| 209 |
+
# Exception raised when format of data is not
|
| 210 |
+
# convertible to dictionary object
|
| 211 |
+
with pytest.raises(TypeError, match=".*Failed to convert edge data"):
|
| 212 |
+
lines = ["1 2 3", "2 3 4", "3 1 2"]
|
| 213 |
+
G = bipartite.parse_edgelist(lines, nodetype=int)
|
| 214 |
+
|
| 215 |
+
# Exception raised when edge data and data
|
| 216 |
+
# keys are not of same length
|
| 217 |
+
with pytest.raises(IndexError):
|
| 218 |
+
lines = ["1 2 3 4", "2 3 4"]
|
| 219 |
+
G = bipartite.parse_edgelist(
|
| 220 |
+
lines, nodetype=int, data=[("weight", int), ("key", int)]
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
# Exception raised when edge data is not
|
| 224 |
+
# convertible to specified data type
|
| 225 |
+
with pytest.raises(TypeError, match=".*Failed to convert key data"):
|
| 226 |
+
lines = ["1 2 3 a", "2 3 4 b"]
|
| 227 |
+
G = bipartite.parse_edgelist(
|
| 228 |
+
lines, nodetype=int, data=[("weight", int), ("key", int)]
|
| 229 |
+
)
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/bipartite/tests/test_spectral_bipartivity.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
pytest.importorskip("scipy")
|
| 4 |
+
|
| 5 |
+
import networkx as nx
|
| 6 |
+
from networkx.algorithms.bipartite import spectral_bipartivity as sb
|
| 7 |
+
|
| 8 |
+
# Examples from Figure 1
|
| 9 |
+
# E. Estrada and J. A. Rodríguez-Velázquez, "Spectral measures of
|
| 10 |
+
# bipartivity in complex networks", PhysRev E 72, 046105 (2005)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class TestSpectralBipartivity:
|
| 14 |
+
def test_star_like(self):
|
| 15 |
+
# star-like
|
| 16 |
+
|
| 17 |
+
G = nx.star_graph(2)
|
| 18 |
+
G.add_edge(1, 2)
|
| 19 |
+
assert sb(G) == pytest.approx(0.843, abs=1e-3)
|
| 20 |
+
|
| 21 |
+
G = nx.star_graph(3)
|
| 22 |
+
G.add_edge(1, 2)
|
| 23 |
+
assert sb(G) == pytest.approx(0.871, abs=1e-3)
|
| 24 |
+
|
| 25 |
+
G = nx.star_graph(4)
|
| 26 |
+
G.add_edge(1, 2)
|
| 27 |
+
assert sb(G) == pytest.approx(0.890, abs=1e-3)
|
| 28 |
+
|
| 29 |
+
def test_k23_like(self):
|
| 30 |
+
# K2,3-like
|
| 31 |
+
G = nx.complete_bipartite_graph(2, 3)
|
| 32 |
+
G.add_edge(0, 1)
|
| 33 |
+
assert sb(G) == pytest.approx(0.769, abs=1e-3)
|
| 34 |
+
|
| 35 |
+
G = nx.complete_bipartite_graph(2, 3)
|
| 36 |
+
G.add_edge(2, 4)
|
| 37 |
+
assert sb(G) == pytest.approx(0.829, abs=1e-3)
|
| 38 |
+
|
| 39 |
+
G = nx.complete_bipartite_graph(2, 3)
|
| 40 |
+
G.add_edge(2, 4)
|
| 41 |
+
G.add_edge(3, 4)
|
| 42 |
+
assert sb(G) == pytest.approx(0.731, abs=1e-3)
|
| 43 |
+
|
| 44 |
+
G = nx.complete_bipartite_graph(2, 3)
|
| 45 |
+
G.add_edge(0, 1)
|
| 46 |
+
G.add_edge(2, 4)
|
| 47 |
+
assert sb(G) == pytest.approx(0.692, abs=1e-3)
|
| 48 |
+
|
| 49 |
+
G = nx.complete_bipartite_graph(2, 3)
|
| 50 |
+
G.add_edge(2, 4)
|
| 51 |
+
G.add_edge(3, 4)
|
| 52 |
+
G.add_edge(0, 1)
|
| 53 |
+
assert sb(G) == pytest.approx(0.645, abs=1e-3)
|
| 54 |
+
|
| 55 |
+
G = nx.complete_bipartite_graph(2, 3)
|
| 56 |
+
G.add_edge(2, 4)
|
| 57 |
+
G.add_edge(3, 4)
|
| 58 |
+
G.add_edge(2, 3)
|
| 59 |
+
assert sb(G) == pytest.approx(0.645, abs=1e-3)
|
| 60 |
+
|
| 61 |
+
G = nx.complete_bipartite_graph(2, 3)
|
| 62 |
+
G.add_edge(2, 4)
|
| 63 |
+
G.add_edge(3, 4)
|
| 64 |
+
G.add_edge(2, 3)
|
| 65 |
+
G.add_edge(0, 1)
|
| 66 |
+
assert sb(G) == pytest.approx(0.597, abs=1e-3)
|
| 67 |
+
|
| 68 |
+
def test_single_nodes(self):
|
| 69 |
+
# single nodes
|
| 70 |
+
G = nx.complete_bipartite_graph(2, 3)
|
| 71 |
+
G.add_edge(2, 4)
|
| 72 |
+
sbn = sb(G, nodes=[1, 2])
|
| 73 |
+
assert sbn[1] == pytest.approx(0.85, abs=1e-2)
|
| 74 |
+
assert sbn[2] == pytest.approx(0.77, abs=1e-2)
|
| 75 |
+
|
| 76 |
+
G = nx.complete_bipartite_graph(2, 3)
|
| 77 |
+
G.add_edge(0, 1)
|
| 78 |
+
sbn = sb(G, nodes=[1, 2])
|
| 79 |
+
assert sbn[1] == pytest.approx(0.73, abs=1e-2)
|
| 80 |
+
assert sbn[2] == pytest.approx(0.82, abs=1e-2)
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/flow/tests/wlm3.gpickle.bz2
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ccacba1e0fbfb30bec361f0e48ec88c999d3474fcda5ddf93bd444ace17cfa0e
|
| 3 |
+
size 88132
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/tests/__pycache__/test_communicability.cpython-311.pyc
ADDED
|
Binary file (3.63 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/tests/__pycache__/test_core.cpython-311.pyc
ADDED
|
Binary file (14.8 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/tests/__pycache__/test_d_separation.cpython-311.pyc
ADDED
|
Binary file (12.5 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/tests/__pycache__/test_non_randomness.cpython-311.pyc
ADDED
|
Binary file (2.42 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/tests/__pycache__/test_planar_drawing.cpython-311.pyc
ADDED
|
Binary file (13.2 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/algorithms/tests/__pycache__/test_smallworld.cpython-311.pyc
ADDED
|
Binary file (5.5 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/drawing/__pycache__/nx_pydot.cpython-311.pyc
ADDED
|
Binary file (18.7 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/classic.cpython-311.pyc
ADDED
|
Binary file (36.2 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/cographs.cpython-311.pyc
ADDED
|
Binary file (2.82 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/community.cpython-311.pyc
ADDED
|
Binary file (42.4 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/degree_seq.cpython-311.pyc
ADDED
|
Binary file (37.3 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/ego.cpython-311.pyc
ADDED
|
Binary file (2.48 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/expanders.cpython-311.pyc
ADDED
|
Binary file (7.17 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/internet_as_graphs.cpython-311.pyc
ADDED
|
Binary file (20.3 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/intersection.cpython-311.pyc
ADDED
|
Binary file (5.68 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/interval_graph.cpython-311.pyc
ADDED
|
Binary file (3.09 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/lattice.cpython-311.pyc
ADDED
|
Binary file (22.4 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/line.cpython-311.pyc
ADDED
|
Binary file (21.7 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/random_clustered.cpython-311.pyc
ADDED
|
Binary file (5.41 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/random_graphs.cpython-311.pyc
ADDED
|
Binary file (52.8 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/small.cpython-311.pyc
ADDED
|
Binary file (32.6 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/stochastic.cpython-311.pyc
ADDED
|
Binary file (2.53 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/sudoku.cpython-311.pyc
ADDED
|
Binary file (5.08 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/time_series.cpython-311.pyc
ADDED
|
Binary file (3.38 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/__pycache__/trees.cpython-311.pyc
ADDED
|
Binary file (47.5 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (226 Bytes). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_community.cpython-311.pyc
ADDED
|
Binary file (23.2 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_directed.cpython-311.pyc
ADDED
|
Binary file (13.3 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_duplication.cpython-311.pyc
ADDED
|
Binary file (4.54 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_ego.cpython-311.pyc
ADDED
|
Binary file (3.39 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_expanders.cpython-311.pyc
ADDED
|
Binary file (6.14 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_internet_as_graphs.cpython-311.pyc
ADDED
|
Binary file (10.8 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_intersection.cpython-311.pyc
ADDED
|
Binary file (2.26 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_joint_degree_seq.cpython-311.pyc
ADDED
|
Binary file (4.63 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_line.cpython-311.pyc
ADDED
|
Binary file (21.3 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_mycielski.cpython-311.pyc
ADDED
|
Binary file (2.49 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_stochastic.cpython-311.pyc
ADDED
|
Binary file (5.36 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_sudoku.cpython-311.pyc
ADDED
|
Binary file (2.92 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/__pycache__/test_time_series.cpython-311.pyc
ADDED
|
Binary file (4.63 kB). View file
|
|
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/test_atlas.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from itertools import groupby
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
import networkx as nx
|
| 6 |
+
from networkx import graph_atlas, graph_atlas_g
|
| 7 |
+
from networkx.generators.atlas import NUM_GRAPHS
|
| 8 |
+
from networkx.utils import edges_equal, nodes_equal, pairwise
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class TestAtlasGraph:
|
| 12 |
+
"""Unit tests for the :func:`~networkx.graph_atlas` function."""
|
| 13 |
+
|
| 14 |
+
def test_index_too_small(self):
|
| 15 |
+
with pytest.raises(ValueError):
|
| 16 |
+
graph_atlas(-1)
|
| 17 |
+
|
| 18 |
+
def test_index_too_large(self):
|
| 19 |
+
with pytest.raises(ValueError):
|
| 20 |
+
graph_atlas(NUM_GRAPHS)
|
| 21 |
+
|
| 22 |
+
def test_graph(self):
|
| 23 |
+
G = graph_atlas(6)
|
| 24 |
+
assert nodes_equal(G.nodes(), range(3))
|
| 25 |
+
assert edges_equal(G.edges(), [(0, 1), (0, 2)])
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class TestAtlasGraphG:
|
| 29 |
+
"""Unit tests for the :func:`~networkx.graph_atlas_g` function."""
|
| 30 |
+
|
| 31 |
+
@classmethod
|
| 32 |
+
def setup_class(cls):
|
| 33 |
+
cls.GAG = graph_atlas_g()
|
| 34 |
+
|
| 35 |
+
def test_sizes(self):
|
| 36 |
+
G = self.GAG[0]
|
| 37 |
+
assert G.number_of_nodes() == 0
|
| 38 |
+
assert G.number_of_edges() == 0
|
| 39 |
+
|
| 40 |
+
G = self.GAG[7]
|
| 41 |
+
assert G.number_of_nodes() == 3
|
| 42 |
+
assert G.number_of_edges() == 3
|
| 43 |
+
|
| 44 |
+
def test_names(self):
|
| 45 |
+
for i, G in enumerate(self.GAG):
|
| 46 |
+
assert int(G.name[1:]) == i
|
| 47 |
+
|
| 48 |
+
def test_nondecreasing_nodes(self):
|
| 49 |
+
# check for nondecreasing number of nodes
|
| 50 |
+
for n1, n2 in pairwise(map(len, self.GAG)):
|
| 51 |
+
assert n2 <= n1 + 1
|
| 52 |
+
|
| 53 |
+
def test_nondecreasing_edges(self):
|
| 54 |
+
# check for nondecreasing number of edges (for fixed number of
|
| 55 |
+
# nodes)
|
| 56 |
+
for n, group in groupby(self.GAG, key=nx.number_of_nodes):
|
| 57 |
+
for m1, m2 in pairwise(map(nx.number_of_edges, group)):
|
| 58 |
+
assert m2 <= m1 + 1
|
| 59 |
+
|
| 60 |
+
def test_nondecreasing_degree_sequence(self):
|
| 61 |
+
# Check for lexicographically nondecreasing degree sequences
|
| 62 |
+
# (for fixed number of nodes and edges).
|
| 63 |
+
#
|
| 64 |
+
# There are three exceptions to this rule in the order given in
|
| 65 |
+
# the "Atlas of Graphs" book, so we need to manually exclude
|
| 66 |
+
# those.
|
| 67 |
+
exceptions = [("G55", "G56"), ("G1007", "G1008"), ("G1012", "G1013")]
|
| 68 |
+
for n, group in groupby(self.GAG, key=nx.number_of_nodes):
|
| 69 |
+
for m, group in groupby(group, key=nx.number_of_edges):
|
| 70 |
+
for G1, G2 in pairwise(group):
|
| 71 |
+
if (G1.name, G2.name) in exceptions:
|
| 72 |
+
continue
|
| 73 |
+
d1 = sorted(d for v, d in G1.degree())
|
| 74 |
+
d2 = sorted(d for v, d in G2.degree())
|
| 75 |
+
assert d1 <= d2
|
tuning-competition-baseline/.venv/lib/python3.11/site-packages/networkx/generators/tests/test_classic.py
ADDED
|
@@ -0,0 +1,622 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
====================
|
| 3 |
+
Generators - Classic
|
| 4 |
+
====================
|
| 5 |
+
|
| 6 |
+
Unit tests for various classic graph generators in generators/classic.py
|
| 7 |
+
"""
|
| 8 |
+
import itertools
|
| 9 |
+
import typing
|
| 10 |
+
|
| 11 |
+
import pytest
|
| 12 |
+
|
| 13 |
+
import networkx as nx
|
| 14 |
+
from networkx.algorithms.isomorphism.isomorph import graph_could_be_isomorphic
|
| 15 |
+
from networkx.utils import edges_equal, nodes_equal
|
| 16 |
+
|
| 17 |
+
is_isomorphic = graph_could_be_isomorphic
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class TestGeneratorClassic:
|
| 21 |
+
def test_balanced_tree(self):
|
| 22 |
+
# balanced_tree(r,h) is a tree with (r**(h+1)-1)/(r-1) edges
|
| 23 |
+
for r, h in [(2, 2), (3, 3), (6, 2)]:
|
| 24 |
+
t = nx.balanced_tree(r, h)
|
| 25 |
+
order = t.order()
|
| 26 |
+
assert order == (r ** (h + 1) - 1) / (r - 1)
|
| 27 |
+
assert nx.is_connected(t)
|
| 28 |
+
assert t.size() == order - 1
|
| 29 |
+
dh = nx.degree_histogram(t)
|
| 30 |
+
assert dh[0] == 0 # no nodes of 0
|
| 31 |
+
assert dh[1] == r**h # nodes of degree 1 are leaves
|
| 32 |
+
assert dh[r] == 1 # root is degree r
|
| 33 |
+
assert dh[r + 1] == order - r**h - 1 # everyone else is degree r+1
|
| 34 |
+
assert len(dh) == r + 2
|
| 35 |
+
|
| 36 |
+
def test_balanced_tree_star(self):
|
| 37 |
+
# balanced_tree(r,1) is the r-star
|
| 38 |
+
t = nx.balanced_tree(r=2, h=1)
|
| 39 |
+
assert is_isomorphic(t, nx.star_graph(2))
|
| 40 |
+
t = nx.balanced_tree(r=5, h=1)
|
| 41 |
+
assert is_isomorphic(t, nx.star_graph(5))
|
| 42 |
+
t = nx.balanced_tree(r=10, h=1)
|
| 43 |
+
assert is_isomorphic(t, nx.star_graph(10))
|
| 44 |
+
|
| 45 |
+
def test_balanced_tree_path(self):
|
| 46 |
+
"""Tests that the balanced tree with branching factor one is the
|
| 47 |
+
path graph.
|
| 48 |
+
|
| 49 |
+
"""
|
| 50 |
+
# A tree of height four has five levels.
|
| 51 |
+
T = nx.balanced_tree(1, 4)
|
| 52 |
+
P = nx.path_graph(5)
|
| 53 |
+
assert is_isomorphic(T, P)
|
| 54 |
+
|
| 55 |
+
def test_full_rary_tree(self):
|
| 56 |
+
r = 2
|
| 57 |
+
n = 9
|
| 58 |
+
t = nx.full_rary_tree(r, n)
|
| 59 |
+
assert t.order() == n
|
| 60 |
+
assert nx.is_connected(t)
|
| 61 |
+
dh = nx.degree_histogram(t)
|
| 62 |
+
assert dh[0] == 0 # no nodes of 0
|
| 63 |
+
assert dh[1] == 5 # nodes of degree 1 are leaves
|
| 64 |
+
assert dh[r] == 1 # root is degree r
|
| 65 |
+
assert dh[r + 1] == 9 - 5 - 1 # everyone else is degree r+1
|
| 66 |
+
assert len(dh) == r + 2
|
| 67 |
+
|
| 68 |
+
def test_full_rary_tree_balanced(self):
|
| 69 |
+
t = nx.full_rary_tree(2, 15)
|
| 70 |
+
th = nx.balanced_tree(2, 3)
|
| 71 |
+
assert is_isomorphic(t, th)
|
| 72 |
+
|
| 73 |
+
def test_full_rary_tree_path(self):
|
| 74 |
+
t = nx.full_rary_tree(1, 10)
|
| 75 |
+
assert is_isomorphic(t, nx.path_graph(10))
|
| 76 |
+
|
| 77 |
+
def test_full_rary_tree_empty(self):
|
| 78 |
+
t = nx.full_rary_tree(0, 10)
|
| 79 |
+
assert is_isomorphic(t, nx.empty_graph(10))
|
| 80 |
+
t = nx.full_rary_tree(3, 0)
|
| 81 |
+
assert is_isomorphic(t, nx.empty_graph(0))
|
| 82 |
+
|
| 83 |
+
def test_full_rary_tree_3_20(self):
|
| 84 |
+
t = nx.full_rary_tree(3, 20)
|
| 85 |
+
assert t.order() == 20
|
| 86 |
+
|
| 87 |
+
def test_barbell_graph(self):
|
| 88 |
+
# number of nodes = 2*m1 + m2 (2 m1-complete graphs + m2-path + 2 edges)
|
| 89 |
+
# number of edges = 2*(nx.number_of_edges(m1-complete graph) + m2 + 1
|
| 90 |
+
m1 = 3
|
| 91 |
+
m2 = 5
|
| 92 |
+
b = nx.barbell_graph(m1, m2)
|
| 93 |
+
assert nx.number_of_nodes(b) == 2 * m1 + m2
|
| 94 |
+
assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
|
| 95 |
+
|
| 96 |
+
m1 = 4
|
| 97 |
+
m2 = 10
|
| 98 |
+
b = nx.barbell_graph(m1, m2)
|
| 99 |
+
assert nx.number_of_nodes(b) == 2 * m1 + m2
|
| 100 |
+
assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
|
| 101 |
+
|
| 102 |
+
m1 = 3
|
| 103 |
+
m2 = 20
|
| 104 |
+
b = nx.barbell_graph(m1, m2)
|
| 105 |
+
assert nx.number_of_nodes(b) == 2 * m1 + m2
|
| 106 |
+
assert nx.number_of_edges(b) == m1 * (m1 - 1) + m2 + 1
|
| 107 |
+
|
| 108 |
+
# Raise NetworkXError if m1<2
|
| 109 |
+
m1 = 1
|
| 110 |
+
m2 = 20
|
| 111 |
+
pytest.raises(nx.NetworkXError, nx.barbell_graph, m1, m2)
|
| 112 |
+
|
| 113 |
+
# Raise NetworkXError if m2<0
|
| 114 |
+
m1 = 5
|
| 115 |
+
m2 = -2
|
| 116 |
+
pytest.raises(nx.NetworkXError, nx.barbell_graph, m1, m2)
|
| 117 |
+
|
| 118 |
+
# nx.barbell_graph(2,m) = nx.path_graph(m+4)
|
| 119 |
+
m1 = 2
|
| 120 |
+
m2 = 5
|
| 121 |
+
b = nx.barbell_graph(m1, m2)
|
| 122 |
+
assert is_isomorphic(b, nx.path_graph(m2 + 4))
|
| 123 |
+
|
| 124 |
+
m1 = 2
|
| 125 |
+
m2 = 10
|
| 126 |
+
b = nx.barbell_graph(m1, m2)
|
| 127 |
+
assert is_isomorphic(b, nx.path_graph(m2 + 4))
|
| 128 |
+
|
| 129 |
+
m1 = 2
|
| 130 |
+
m2 = 20
|
| 131 |
+
b = nx.barbell_graph(m1, m2)
|
| 132 |
+
assert is_isomorphic(b, nx.path_graph(m2 + 4))
|
| 133 |
+
|
| 134 |
+
pytest.raises(
|
| 135 |
+
nx.NetworkXError, nx.barbell_graph, m1, m2, create_using=nx.DiGraph()
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
mb = nx.barbell_graph(m1, m2, create_using=nx.MultiGraph())
|
| 139 |
+
assert edges_equal(mb.edges(), b.edges())
|
| 140 |
+
|
| 141 |
+
def test_binomial_tree(self):
|
| 142 |
+
graphs = (None, nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph)
|
| 143 |
+
for create_using in graphs:
|
| 144 |
+
for n in range(4):
|
| 145 |
+
b = nx.binomial_tree(n, create_using)
|
| 146 |
+
assert nx.number_of_nodes(b) == 2**n
|
| 147 |
+
assert nx.number_of_edges(b) == (2**n - 1)
|
| 148 |
+
|
| 149 |
+
def test_complete_graph(self):
|
| 150 |
+
# complete_graph(m) is a connected graph with
|
| 151 |
+
# m nodes and m*(m+1)/2 edges
|
| 152 |
+
for m in [0, 1, 3, 5]:
|
| 153 |
+
g = nx.complete_graph(m)
|
| 154 |
+
assert nx.number_of_nodes(g) == m
|
| 155 |
+
assert nx.number_of_edges(g) == m * (m - 1) // 2
|
| 156 |
+
|
| 157 |
+
mg = nx.complete_graph(m, create_using=nx.MultiGraph)
|
| 158 |
+
assert edges_equal(mg.edges(), g.edges())
|
| 159 |
+
|
| 160 |
+
g = nx.complete_graph("abc")
|
| 161 |
+
assert nodes_equal(g.nodes(), ["a", "b", "c"])
|
| 162 |
+
assert g.size() == 3
|
| 163 |
+
|
| 164 |
+
# creates a self-loop... should it? <backward compatible says yes>
|
| 165 |
+
g = nx.complete_graph("abcb")
|
| 166 |
+
assert nodes_equal(g.nodes(), ["a", "b", "c"])
|
| 167 |
+
assert g.size() == 4
|
| 168 |
+
|
| 169 |
+
g = nx.complete_graph("abcb", create_using=nx.MultiGraph)
|
| 170 |
+
assert nodes_equal(g.nodes(), ["a", "b", "c"])
|
| 171 |
+
assert g.size() == 6
|
| 172 |
+
|
| 173 |
+
def test_complete_digraph(self):
|
| 174 |
+
# complete_graph(m) is a connected graph with
|
| 175 |
+
# m nodes and m*(m+1)/2 edges
|
| 176 |
+
for m in [0, 1, 3, 5]:
|
| 177 |
+
g = nx.complete_graph(m, create_using=nx.DiGraph)
|
| 178 |
+
assert nx.number_of_nodes(g) == m
|
| 179 |
+
assert nx.number_of_edges(g) == m * (m - 1)
|
| 180 |
+
|
| 181 |
+
g = nx.complete_graph("abc", create_using=nx.DiGraph)
|
| 182 |
+
assert len(g) == 3
|
| 183 |
+
assert g.size() == 6
|
| 184 |
+
assert g.is_directed()
|
| 185 |
+
|
| 186 |
+
def test_circular_ladder_graph(self):
|
| 187 |
+
G = nx.circular_ladder_graph(5)
|
| 188 |
+
pytest.raises(
|
| 189 |
+
nx.NetworkXError, nx.circular_ladder_graph, 5, create_using=nx.DiGraph
|
| 190 |
+
)
|
| 191 |
+
mG = nx.circular_ladder_graph(5, create_using=nx.MultiGraph)
|
| 192 |
+
assert edges_equal(mG.edges(), G.edges())
|
| 193 |
+
|
| 194 |
+
def test_circulant_graph(self):
|
| 195 |
+
# Ci_n(1) is the cycle graph for all n
|
| 196 |
+
Ci6_1 = nx.circulant_graph(6, [1])
|
| 197 |
+
C6 = nx.cycle_graph(6)
|
| 198 |
+
assert edges_equal(Ci6_1.edges(), C6.edges())
|
| 199 |
+
|
| 200 |
+
# Ci_n(1, 2, ..., n div 2) is the complete graph for all n
|
| 201 |
+
Ci7 = nx.circulant_graph(7, [1, 2, 3])
|
| 202 |
+
K7 = nx.complete_graph(7)
|
| 203 |
+
assert edges_equal(Ci7.edges(), K7.edges())
|
| 204 |
+
|
| 205 |
+
# Ci_6(1, 3) is K_3,3 i.e. the utility graph
|
| 206 |
+
Ci6_1_3 = nx.circulant_graph(6, [1, 3])
|
| 207 |
+
K3_3 = nx.complete_bipartite_graph(3, 3)
|
| 208 |
+
assert is_isomorphic(Ci6_1_3, K3_3)
|
| 209 |
+
|
| 210 |
+
def test_cycle_graph(self):
|
| 211 |
+
G = nx.cycle_graph(4)
|
| 212 |
+
assert edges_equal(G.edges(), [(0, 1), (0, 3), (1, 2), (2, 3)])
|
| 213 |
+
mG = nx.cycle_graph(4, create_using=nx.MultiGraph)
|
| 214 |
+
assert edges_equal(mG.edges(), [(0, 1), (0, 3), (1, 2), (2, 3)])
|
| 215 |
+
G = nx.cycle_graph(4, create_using=nx.DiGraph)
|
| 216 |
+
assert not G.has_edge(2, 1)
|
| 217 |
+
assert G.has_edge(1, 2)
|
| 218 |
+
assert G.is_directed()
|
| 219 |
+
|
| 220 |
+
G = nx.cycle_graph("abc")
|
| 221 |
+
assert len(G) == 3
|
| 222 |
+
assert G.size() == 3
|
| 223 |
+
G = nx.cycle_graph("abcb")
|
| 224 |
+
assert len(G) == 3
|
| 225 |
+
assert G.size() == 2
|
| 226 |
+
g = nx.cycle_graph("abc", nx.DiGraph)
|
| 227 |
+
assert len(g) == 3
|
| 228 |
+
assert g.size() == 3
|
| 229 |
+
assert g.is_directed()
|
| 230 |
+
g = nx.cycle_graph("abcb", nx.DiGraph)
|
| 231 |
+
assert len(g) == 3
|
| 232 |
+
assert g.size() == 4
|
| 233 |
+
|
| 234 |
+
def test_dorogovtsev_goltsev_mendes_graph(self):
|
| 235 |
+
G = nx.dorogovtsev_goltsev_mendes_graph(0)
|
| 236 |
+
assert edges_equal(G.edges(), [(0, 1)])
|
| 237 |
+
assert nodes_equal(list(G), [0, 1])
|
| 238 |
+
G = nx.dorogovtsev_goltsev_mendes_graph(1)
|
| 239 |
+
assert edges_equal(G.edges(), [(0, 1), (0, 2), (1, 2)])
|
| 240 |
+
assert nx.average_clustering(G) == 1.0
|
| 241 |
+
assert sorted(nx.triangles(G).values()) == [1, 1, 1]
|
| 242 |
+
G = nx.dorogovtsev_goltsev_mendes_graph(10)
|
| 243 |
+
assert nx.number_of_nodes(G) == 29526
|
| 244 |
+
assert nx.number_of_edges(G) == 59049
|
| 245 |
+
assert G.degree(0) == 1024
|
| 246 |
+
assert G.degree(1) == 1024
|
| 247 |
+
assert G.degree(2) == 1024
|
| 248 |
+
|
| 249 |
+
pytest.raises(
|
| 250 |
+
nx.NetworkXError,
|
| 251 |
+
nx.dorogovtsev_goltsev_mendes_graph,
|
| 252 |
+
7,
|
| 253 |
+
create_using=nx.DiGraph,
|
| 254 |
+
)
|
| 255 |
+
pytest.raises(
|
| 256 |
+
nx.NetworkXError,
|
| 257 |
+
nx.dorogovtsev_goltsev_mendes_graph,
|
| 258 |
+
7,
|
| 259 |
+
create_using=nx.MultiGraph,
|
| 260 |
+
)
|
| 261 |
+
|
| 262 |
+
def test_create_using(self):
|
| 263 |
+
G = nx.empty_graph()
|
| 264 |
+
assert isinstance(G, nx.Graph)
|
| 265 |
+
pytest.raises(TypeError, nx.empty_graph, create_using=0.0)
|
| 266 |
+
pytest.raises(TypeError, nx.empty_graph, create_using="Graph")
|
| 267 |
+
|
| 268 |
+
G = nx.empty_graph(create_using=nx.MultiGraph)
|
| 269 |
+
assert isinstance(G, nx.MultiGraph)
|
| 270 |
+
G = nx.empty_graph(create_using=nx.DiGraph)
|
| 271 |
+
assert isinstance(G, nx.DiGraph)
|
| 272 |
+
|
| 273 |
+
G = nx.empty_graph(create_using=nx.DiGraph, default=nx.MultiGraph)
|
| 274 |
+
assert isinstance(G, nx.DiGraph)
|
| 275 |
+
G = nx.empty_graph(create_using=None, default=nx.MultiGraph)
|
| 276 |
+
assert isinstance(G, nx.MultiGraph)
|
| 277 |
+
G = nx.empty_graph(default=nx.MultiGraph)
|
| 278 |
+
assert isinstance(G, nx.MultiGraph)
|
| 279 |
+
|
| 280 |
+
G = nx.path_graph(5)
|
| 281 |
+
H = nx.empty_graph(create_using=G)
|
| 282 |
+
assert not H.is_multigraph()
|
| 283 |
+
assert not H.is_directed()
|
| 284 |
+
assert len(H) == 0
|
| 285 |
+
assert G is H
|
| 286 |
+
|
| 287 |
+
H = nx.empty_graph(create_using=nx.MultiGraph())
|
| 288 |
+
assert H.is_multigraph()
|
| 289 |
+
assert not H.is_directed()
|
| 290 |
+
assert G is not H
|
| 291 |
+
|
| 292 |
+
# test for subclasses that also use typing.Protocol. See gh-6243
|
| 293 |
+
class Mixin(typing.Protocol):
|
| 294 |
+
pass
|
| 295 |
+
|
| 296 |
+
class MyGraph(Mixin, nx.DiGraph):
|
| 297 |
+
pass
|
| 298 |
+
|
| 299 |
+
G = nx.empty_graph(create_using=MyGraph)
|
| 300 |
+
|
| 301 |
+
def test_empty_graph(self):
|
| 302 |
+
G = nx.empty_graph()
|
| 303 |
+
assert nx.number_of_nodes(G) == 0
|
| 304 |
+
G = nx.empty_graph(42)
|
| 305 |
+
assert nx.number_of_nodes(G) == 42
|
| 306 |
+
assert nx.number_of_edges(G) == 0
|
| 307 |
+
|
| 308 |
+
G = nx.empty_graph("abc")
|
| 309 |
+
assert len(G) == 3
|
| 310 |
+
assert G.size() == 0
|
| 311 |
+
|
| 312 |
+
# create empty digraph
|
| 313 |
+
G = nx.empty_graph(42, create_using=nx.DiGraph(name="duh"))
|
| 314 |
+
assert nx.number_of_nodes(G) == 42
|
| 315 |
+
assert nx.number_of_edges(G) == 0
|
| 316 |
+
assert isinstance(G, nx.DiGraph)
|
| 317 |
+
|
| 318 |
+
# create empty multigraph
|
| 319 |
+
G = nx.empty_graph(42, create_using=nx.MultiGraph(name="duh"))
|
| 320 |
+
assert nx.number_of_nodes(G) == 42
|
| 321 |
+
assert nx.number_of_edges(G) == 0
|
| 322 |
+
assert isinstance(G, nx.MultiGraph)
|
| 323 |
+
|
| 324 |
+
# create empty graph from another
|
| 325 |
+
pete = nx.petersen_graph()
|
| 326 |
+
G = nx.empty_graph(42, create_using=pete)
|
| 327 |
+
assert nx.number_of_nodes(G) == 42
|
| 328 |
+
assert nx.number_of_edges(G) == 0
|
| 329 |
+
assert isinstance(G, nx.Graph)
|
| 330 |
+
|
| 331 |
+
def test_ladder_graph(self):
|
| 332 |
+
for i, G in [
|
| 333 |
+
(0, nx.empty_graph(0)),
|
| 334 |
+
(1, nx.path_graph(2)),
|
| 335 |
+
(2, nx.hypercube_graph(2)),
|
| 336 |
+
(10, nx.grid_graph([2, 10])),
|
| 337 |
+
]:
|
| 338 |
+
assert is_isomorphic(nx.ladder_graph(i), G)
|
| 339 |
+
|
| 340 |
+
pytest.raises(nx.NetworkXError, nx.ladder_graph, 2, create_using=nx.DiGraph)
|
| 341 |
+
|
| 342 |
+
g = nx.ladder_graph(2)
|
| 343 |
+
mg = nx.ladder_graph(2, create_using=nx.MultiGraph)
|
| 344 |
+
assert edges_equal(mg.edges(), g.edges())
|
| 345 |
+
|
| 346 |
+
def test_lollipop_graph_right_sizes(self):
|
| 347 |
+
# number of nodes = m1 + m2
|
| 348 |
+
# number of edges = nx.number_of_edges(nx.complete_graph(m1)) + m2
|
| 349 |
+
for m1, m2 in [(3, 5), (4, 10), (3, 20)]:
|
| 350 |
+
G = nx.lollipop_graph(m1, m2)
|
| 351 |
+
assert nx.number_of_nodes(G) == m1 + m2
|
| 352 |
+
assert nx.number_of_edges(G) == m1 * (m1 - 1) / 2 + m2
|
| 353 |
+
for first, second in [("ab", ""), ("abc", "defg")]:
|
| 354 |
+
m1, m2 = len(first), len(second)
|
| 355 |
+
G = nx.lollipop_graph(first, second)
|
| 356 |
+
assert nx.number_of_nodes(G) == m1 + m2
|
| 357 |
+
assert nx.number_of_edges(G) == m1 * (m1 - 1) / 2 + m2
|
| 358 |
+
|
| 359 |
+
def test_lollipop_graph_exceptions(self):
|
| 360 |
+
# Raise NetworkXError if m<2
|
| 361 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, -1, 2)
|
| 362 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, 1, 20)
|
| 363 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, "", 20)
|
| 364 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, "a", 20)
|
| 365 |
+
|
| 366 |
+
# Raise NetworkXError if n<0
|
| 367 |
+
pytest.raises(nx.NetworkXError, nx.lollipop_graph, 5, -2)
|
| 368 |
+
|
| 369 |
+
# raise NetworkXError if create_using is directed
|
| 370 |
+
with pytest.raises(nx.NetworkXError):
|
| 371 |
+
nx.lollipop_graph(2, 20, create_using=nx.DiGraph)
|
| 372 |
+
with pytest.raises(nx.NetworkXError):
|
| 373 |
+
nx.lollipop_graph(2, 20, create_using=nx.MultiDiGraph)
|
| 374 |
+
|
| 375 |
+
def test_lollipop_graph_same_as_path_when_m1_is_2(self):
|
| 376 |
+
# lollipop_graph(2,m) = path_graph(m+2)
|
| 377 |
+
for m1, m2 in [(2, 0), (2, 5), (2, 10), ("ab", 20)]:
|
| 378 |
+
G = nx.lollipop_graph(m1, m2)
|
| 379 |
+
assert is_isomorphic(G, nx.path_graph(m2 + 2))
|
| 380 |
+
|
| 381 |
+
def test_lollipop_graph_for_multigraph(self):
|
| 382 |
+
G = nx.lollipop_graph(5, 20)
|
| 383 |
+
MG = nx.lollipop_graph(5, 20, create_using=nx.MultiGraph)
|
| 384 |
+
assert edges_equal(MG.edges(), G.edges())
|
| 385 |
+
|
| 386 |
+
def test_lollipop_graph_mixing_input_types(self):
|
| 387 |
+
cases = [(4, "abc"), ("abcd", 3), ([1, 2, 3, 4], "abc"), ("abcd", [1, 2, 3])]
|
| 388 |
+
for m1, m2 in cases:
|
| 389 |
+
G = nx.lollipop_graph(m1, m2)
|
| 390 |
+
assert len(G) == 7
|
| 391 |
+
assert G.size() == 9
|
| 392 |
+
|
| 393 |
+
def test_lollipop_graph_not_int_integer_inputs(self):
|
| 394 |
+
# test non-int integers
|
| 395 |
+
np = pytest.importorskip("numpy")
|
| 396 |
+
G = nx.lollipop_graph(np.int32(4), np.int64(3))
|
| 397 |
+
assert len(G) == 7
|
| 398 |
+
assert G.size() == 9
|
| 399 |
+
|
| 400 |
+
def test_null_graph(self):
|
| 401 |
+
assert nx.number_of_nodes(nx.null_graph()) == 0
|
| 402 |
+
|
| 403 |
+
def test_path_graph(self):
|
| 404 |
+
p = nx.path_graph(0)
|
| 405 |
+
assert is_isomorphic(p, nx.null_graph())
|
| 406 |
+
|
| 407 |
+
p = nx.path_graph(1)
|
| 408 |
+
assert is_isomorphic(p, nx.empty_graph(1))
|
| 409 |
+
|
| 410 |
+
p = nx.path_graph(10)
|
| 411 |
+
assert nx.is_connected(p)
|
| 412 |
+
assert sorted(d for n, d in p.degree()) == [1, 1, 2, 2, 2, 2, 2, 2, 2, 2]
|
| 413 |
+
assert p.order() - 1 == p.size()
|
| 414 |
+
|
| 415 |
+
dp = nx.path_graph(3, create_using=nx.DiGraph)
|
| 416 |
+
assert dp.has_edge(0, 1)
|
| 417 |
+
assert not dp.has_edge(1, 0)
|
| 418 |
+
|
| 419 |
+
mp = nx.path_graph(10, create_using=nx.MultiGraph)
|
| 420 |
+
assert edges_equal(mp.edges(), p.edges())
|
| 421 |
+
|
| 422 |
+
G = nx.path_graph("abc")
|
| 423 |
+
assert len(G) == 3
|
| 424 |
+
assert G.size() == 2
|
| 425 |
+
G = nx.path_graph("abcb")
|
| 426 |
+
assert len(G) == 3
|
| 427 |
+
assert G.size() == 2
|
| 428 |
+
g = nx.path_graph("abc", nx.DiGraph)
|
| 429 |
+
assert len(g) == 3
|
| 430 |
+
assert g.size() == 2
|
| 431 |
+
assert g.is_directed()
|
| 432 |
+
g = nx.path_graph("abcb", nx.DiGraph)
|
| 433 |
+
assert len(g) == 3
|
| 434 |
+
assert g.size() == 3
|
| 435 |
+
|
| 436 |
+
G = nx.path_graph((1, 2, 3, 2, 4))
|
| 437 |
+
assert G.has_edge(2, 4)
|
| 438 |
+
|
| 439 |
+
def test_star_graph(self):
|
| 440 |
+
assert is_isomorphic(nx.star_graph(""), nx.empty_graph(0))
|
| 441 |
+
assert is_isomorphic(nx.star_graph([]), nx.empty_graph(0))
|
| 442 |
+
assert is_isomorphic(nx.star_graph(0), nx.empty_graph(1))
|
| 443 |
+
assert is_isomorphic(nx.star_graph(1), nx.path_graph(2))
|
| 444 |
+
assert is_isomorphic(nx.star_graph(2), nx.path_graph(3))
|
| 445 |
+
assert is_isomorphic(nx.star_graph(5), nx.complete_bipartite_graph(1, 5))
|
| 446 |
+
|
| 447 |
+
s = nx.star_graph(10)
|
| 448 |
+
assert sorted(d for n, d in s.degree()) == [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10]
|
| 449 |
+
|
| 450 |
+
pytest.raises(nx.NetworkXError, nx.star_graph, 10, create_using=nx.DiGraph)
|
| 451 |
+
|
| 452 |
+
ms = nx.star_graph(10, create_using=nx.MultiGraph)
|
| 453 |
+
assert edges_equal(ms.edges(), s.edges())
|
| 454 |
+
|
| 455 |
+
G = nx.star_graph("abc")
|
| 456 |
+
assert len(G) == 3
|
| 457 |
+
assert G.size() == 2
|
| 458 |
+
|
| 459 |
+
G = nx.star_graph("abcb")
|
| 460 |
+
assert len(G) == 3
|
| 461 |
+
assert G.size() == 2
|
| 462 |
+
G = nx.star_graph("abcb", create_using=nx.MultiGraph)
|
| 463 |
+
assert len(G) == 3
|
| 464 |
+
assert G.size() == 3
|
| 465 |
+
|
| 466 |
+
G = nx.star_graph("abcdefg")
|
| 467 |
+
assert len(G) == 7
|
| 468 |
+
assert G.size() == 6
|
| 469 |
+
|
| 470 |
+
def test_non_int_integers_for_star_graph(self):
|
| 471 |
+
np = pytest.importorskip("numpy")
|
| 472 |
+
G = nx.star_graph(np.int32(3))
|
| 473 |
+
assert len(G) == 4
|
| 474 |
+
assert G.size() == 3
|
| 475 |
+
|
| 476 |
+
def test_tadpole_graph_right_sizes(self):
|
| 477 |
+
# number of nodes = m1 + m2
|
| 478 |
+
# number of edges = m1 + m2 - (m1 == 2)
|
| 479 |
+
for m1, m2 in [(3, 0), (3, 5), (4, 10), (3, 20)]:
|
| 480 |
+
G = nx.tadpole_graph(m1, m2)
|
| 481 |
+
assert nx.number_of_nodes(G) == m1 + m2
|
| 482 |
+
assert nx.number_of_edges(G) == m1 + m2 - (m1 == 2)
|
| 483 |
+
for first, second in [("ab", ""), ("ab", "c"), ("abc", "defg")]:
|
| 484 |
+
m1, m2 = len(first), len(second)
|
| 485 |
+
print(first, second)
|
| 486 |
+
G = nx.tadpole_graph(first, second)
|
| 487 |
+
print(G.edges())
|
| 488 |
+
assert nx.number_of_nodes(G) == m1 + m2
|
| 489 |
+
assert nx.number_of_edges(G) == m1 + m2 - (m1 == 2)
|
| 490 |
+
|
| 491 |
+
def test_tadpole_graph_exceptions(self):
|
| 492 |
+
# Raise NetworkXError if m<2
|
| 493 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, -1, 3)
|
| 494 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, 0, 3)
|
| 495 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, 1, 3)
|
| 496 |
+
|
| 497 |
+
# Raise NetworkXError if n<0
|
| 498 |
+
pytest.raises(nx.NetworkXError, nx.tadpole_graph, 5, -2)
|
| 499 |
+
|
| 500 |
+
# Raise NetworkXError for digraphs
|
| 501 |
+
with pytest.raises(nx.NetworkXError):
|
| 502 |
+
nx.tadpole_graph(2, 20, create_using=nx.DiGraph)
|
| 503 |
+
with pytest.raises(nx.NetworkXError):
|
| 504 |
+
nx.tadpole_graph(2, 20, create_using=nx.MultiDiGraph)
|
| 505 |
+
|
| 506 |
+
def test_tadpole_graph_same_as_path_when_m1_is_2_or_0(self):
|
| 507 |
+
# tadpole_graph(2,m) = path_graph(m+2)
|
| 508 |
+
for m1, m2 in [(2, 0), (2, 5), (2, 10), ("ab", 20)]:
|
| 509 |
+
G = nx.tadpole_graph(m1, m2)
|
| 510 |
+
assert is_isomorphic(G, nx.path_graph(m2 + 2))
|
| 511 |
+
|
| 512 |
+
def test_tadpole_graph_same_as_cycle_when_m2_is_0(self):
|
| 513 |
+
# tadpole_graph(m,0) = cycle_(m)
|
| 514 |
+
for m1, m2 in [(4, 0), (7, 0)]:
|
| 515 |
+
G = nx.tadpole_graph(m1, m2)
|
| 516 |
+
assert is_isomorphic(G, nx.cycle_graph(m1))
|
| 517 |
+
|
| 518 |
+
def test_tadpole_graph_for_multigraph(self):
|
| 519 |
+
G = nx.tadpole_graph(5, 20)
|
| 520 |
+
MG = nx.tadpole_graph(5, 20, create_using=nx.MultiGraph)
|
| 521 |
+
assert edges_equal(MG.edges(), G.edges())
|
| 522 |
+
|
| 523 |
+
def test_tadpole_graph_mixing_input_types(self):
|
| 524 |
+
cases = [(4, "abc"), ("abcd", 3), ([1, 2, 3, 4], "abc"), ("abcd", [1, 2, 3])]
|
| 525 |
+
for m1, m2 in cases:
|
| 526 |
+
G = nx.tadpole_graph(m1, m2)
|
| 527 |
+
assert len(G) == 7
|
| 528 |
+
assert G.size() == 7
|
| 529 |
+
|
| 530 |
+
def test_tadpole_graph_not_int_integer_inputs(self):
|
| 531 |
+
# test non-int integers
|
| 532 |
+
np = pytest.importorskip("numpy")
|
| 533 |
+
G = nx.tadpole_graph(np.int32(4), np.int64(3))
|
| 534 |
+
assert len(G) == 7
|
| 535 |
+
assert G.size() == 7
|
| 536 |
+
|
| 537 |
+
def test_trivial_graph(self):
|
| 538 |
+
assert nx.number_of_nodes(nx.trivial_graph()) == 1
|
| 539 |
+
|
| 540 |
+
def test_turan_graph(self):
|
| 541 |
+
assert nx.number_of_edges(nx.turan_graph(13, 4)) == 63
|
| 542 |
+
assert is_isomorphic(
|
| 543 |
+
nx.turan_graph(13, 4), nx.complete_multipartite_graph(3, 4, 3, 3)
|
| 544 |
+
)
|
| 545 |
+
|
| 546 |
+
def test_wheel_graph(self):
|
| 547 |
+
for n, G in [
|
| 548 |
+
("", nx.null_graph()),
|
| 549 |
+
(0, nx.null_graph()),
|
| 550 |
+
(1, nx.empty_graph(1)),
|
| 551 |
+
(2, nx.path_graph(2)),
|
| 552 |
+
(3, nx.complete_graph(3)),
|
| 553 |
+
(4, nx.complete_graph(4)),
|
| 554 |
+
]:
|
| 555 |
+
g = nx.wheel_graph(n)
|
| 556 |
+
assert is_isomorphic(g, G)
|
| 557 |
+
|
| 558 |
+
g = nx.wheel_graph(10)
|
| 559 |
+
assert sorted(d for n, d in g.degree()) == [3, 3, 3, 3, 3, 3, 3, 3, 3, 9]
|
| 560 |
+
|
| 561 |
+
pytest.raises(nx.NetworkXError, nx.wheel_graph, 10, create_using=nx.DiGraph)
|
| 562 |
+
|
| 563 |
+
mg = nx.wheel_graph(10, create_using=nx.MultiGraph())
|
| 564 |
+
assert edges_equal(mg.edges(), g.edges())
|
| 565 |
+
|
| 566 |
+
G = nx.wheel_graph("abc")
|
| 567 |
+
assert len(G) == 3
|
| 568 |
+
assert G.size() == 3
|
| 569 |
+
|
| 570 |
+
G = nx.wheel_graph("abcb")
|
| 571 |
+
assert len(G) == 3
|
| 572 |
+
assert G.size() == 4
|
| 573 |
+
G = nx.wheel_graph("abcb", nx.MultiGraph)
|
| 574 |
+
assert len(G) == 3
|
| 575 |
+
assert G.size() == 6
|
| 576 |
+
|
| 577 |
+
def test_non_int_integers_for_wheel_graph(self):
|
| 578 |
+
np = pytest.importorskip("numpy")
|
| 579 |
+
G = nx.wheel_graph(np.int32(3))
|
| 580 |
+
assert len(G) == 3
|
| 581 |
+
assert G.size() == 3
|
| 582 |
+
|
| 583 |
+
def test_complete_0_partite_graph(self):
|
| 584 |
+
"""Tests that the complete 0-partite graph is the null graph."""
|
| 585 |
+
G = nx.complete_multipartite_graph()
|
| 586 |
+
H = nx.null_graph()
|
| 587 |
+
assert nodes_equal(G, H)
|
| 588 |
+
assert edges_equal(G.edges(), H.edges())
|
| 589 |
+
|
| 590 |
+
def test_complete_1_partite_graph(self):
|
| 591 |
+
"""Tests that the complete 1-partite graph is the empty graph."""
|
| 592 |
+
G = nx.complete_multipartite_graph(3)
|
| 593 |
+
H = nx.empty_graph(3)
|
| 594 |
+
assert nodes_equal(G, H)
|
| 595 |
+
assert edges_equal(G.edges(), H.edges())
|
| 596 |
+
|
| 597 |
+
def test_complete_2_partite_graph(self):
|
| 598 |
+
"""Tests that the complete 2-partite graph is the complete bipartite
|
| 599 |
+
graph.
|
| 600 |
+
|
| 601 |
+
"""
|
| 602 |
+
G = nx.complete_multipartite_graph(2, 3)
|
| 603 |
+
H = nx.complete_bipartite_graph(2, 3)
|
| 604 |
+
assert nodes_equal(G, H)
|
| 605 |
+
assert edges_equal(G.edges(), H.edges())
|
| 606 |
+
|
| 607 |
+
def test_complete_multipartite_graph(self):
|
| 608 |
+
"""Tests for generating the complete multipartite graph."""
|
| 609 |
+
G = nx.complete_multipartite_graph(2, 3, 4)
|
| 610 |
+
blocks = [(0, 1), (2, 3, 4), (5, 6, 7, 8)]
|
| 611 |
+
# Within each block, no two vertices should be adjacent.
|
| 612 |
+
for block in blocks:
|
| 613 |
+
for u, v in itertools.combinations_with_replacement(block, 2):
|
| 614 |
+
assert v not in G[u]
|
| 615 |
+
assert G.nodes[u] == G.nodes[v]
|
| 616 |
+
# Across blocks, all vertices should be adjacent.
|
| 617 |
+
for block1, block2 in itertools.combinations(blocks, 2):
|
| 618 |
+
for u, v in itertools.product(block1, block2):
|
| 619 |
+
assert v in G[u]
|
| 620 |
+
assert G.nodes[u] != G.nodes[v]
|
| 621 |
+
with pytest.raises(nx.NetworkXError, match="Negative number of nodes"):
|
| 622 |
+
nx.complete_multipartite_graph(2, -3, 4)
|