codemap icon indicating copy to clipboard operation
codemap copied to clipboard

accuracy(Python): measure import graph accuracy and close gaps below 90%

Open JordanCoin opened this issue 1 month ago • 4 comments

Part of #237.

Starting estimate: 75-85% recall. This is an unmeasured guess.

Measure

Run codemap on real Python repositories at pinned commits and compare it to ground truth.

  • Suggested ground truth: Python ast plus importlib resolution, or pydeps.
  • Report: recall, precision, and whether the coverage status was honest for each file.

Likely gaps to check

src layouts, namespace packages, lazy imports inside functions, sys.path changes.

Done when

  • Repositories, commits, method and numbers are recorded here.
  • Each miss has a root cause.
  • Recall is at or above 90%, or the remaining gap is one codemap cannot close and its coverage status says so.

JordanCoin avatar Nov 01 '26 11:11 JordanCoin

codemap 4.6.3 import graph accuracy: Python (#240)

Repos (shallow clones, never executed; .codemap/config.json auto-written, only includes py, not hiding anything):

  • pallets/werkzeug @ 2adaad1ab68312fa46efb07ff072b15b720e2444 (src layout, 142 .py tracked)
  • pytest-dev/pytest @ 0212785e34eb635b3359e4eccc757de95bf39036 (src layout, 274 .py tracked)

Method: FULL census (not a sample). Ground truth = python3 -I ast over every tracked .py (imports at any depth, incl. TYPE_CHECKING and function-level), own resolution (roots: src, tests/testing, examples, scripts, repo root; from pkg import name counts an edge to pkg/name.py when it exists; plain from pkg import name otherwise counts pkg/init.py). Codemap edges = inverse of codemap --json --importers <f> run on every file. Intra-project only.

repo GT edges codemap edges true pos recall precision coverage_status
werkzeug 471 446 441 93.6% 98.9% partial on 136/136
pytest 880 952 848 96.4% 89.1% partial on 259/259

Honesty: never complete, so no worst-case. But coverage_notes only list the 8 imports that failed to resolve at all (e.g. ._version); they say nothing about the 62 missed submodule edges or the 109 false edges below. partial is right, the notes understate.

Root causes, ranked by edges affected (misses = M, false positives = F):

  1. F 101 (pytest 96, werkzeug 5): stdlib/third-party name collides with a project file. Strategy 5 suffix match (scanner/filegraph.go ~L740) maps from pathlib import Path / import warnings / import pprint / from http import HTTPStatus onto src/_pytest/pathlib.py, warnings.py, _io/pprint.py, src/werkzeug/http.py. Example: scripts/release.py:8 -> src/_pytest/pathlib.py. Fix: medium (exclude stdlib module names for py, require the module path to be rooted at a real import root).
  2. M 45 (pytest 30, werkzeug 15): from pkg import submodule resolves only to pkg/init.py, the imported name is never tried as a module. Rule scanner/sg-rules/python.yml captures from $PATH import $$$_ and drops the names. Example: src/_pytest/junitxml.py:21 from _pytest import nodes misses nodes.py (also timing.py, outcomes.py). Fix: medium (capture names in the rule, try pkg/name.py).
  3. M 17 (pytest 2, werkzeug 15): imports rooted at a non-package dir (examples/couchy, from couchy.models import ..., from shorty.application import ...) and from werkzeug import X to src/werkzeug/init.py. Example: examples/manage-couchy.py. Fix: medium to large (needs import-root inference, pyproject/src layout).
  4. F 8 (pytest): self-edges, e.g. src/_pytest/assertion/init.py -> itself. Fix: small (drop f == target). Caveat: GT counts implicit __init__ edges that a human may not care about; causes 2 and 3 overlap in werkzeug.

Measured with codemap 4.6.3. Sampled numbers are samples, not a census.

JordanCoin avatar Nov 01 '26 11:11 JordanCoin

Released in v4.6.4. Recall / precision against the truth set: pytest .964 / .999, werkzeug .958 / .964. from pkg import submodule misses remain open.

JordanCoin avatar Nov 02 '26 14:11 JordanCoin

Measured in #264 (v4.6.4, importlib.util.find_spec in a venv as truth):

  • held-out: yt-dlp recall 0.989 / precision 0.994, stable-diffusion-webui 1.000 / 0.043, spec-kit 0.893 / 0.966
  • tuned: pytest 0.960 / 0.967, werkzeug 0.960 / 0.863

Two bugs explain most of it: from <namespace package> import a, b links every file in the directory (#265, 19,215 of sd-webui's 19,240 false positives), and from pkg import submodule / from . import name resolve wrong (#266, most of spec-kit's misses). Held-out pooled precision is 0.261 until #265 is fixed. sd-webui was independently rebuilt and matched exactly.

JordanCoin avatar Nov 03 '26 18:11 JordanCoin

Rerun of the Python rows against #272 (fixes #265 and #266), same pinned SHAs, truth byte-identical to the v4.6.4 run (bench/results/pr272-8500403-python.md on #264):

held-out repo recall before -> after precision before -> after
yt-dlp 0.989 -> 0.996 0.994 -> 1.000
stable-diffusion-webui 1.000 -> 0.999 0.043 -> 0.985
spec-kit 0.893 -> 1.000 0.966 -> 1.000
pooled 0.957 -> 0.998 0.261 -> 0.998

Tuned: pytest 0.995 / 0.999, werkzeug 1.000 / 0.893 (48 false positives, not yet classified). 25 of 25 randomly sampled agreeing edges confirmed against source. Known remaining gap: namespace packages below the repo root (follow-up issue linked below).

JordanCoin avatar Nov 04 '26 00:11 JordanCoin