@oriro/orirocli 0.1.9 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -18
- package/dist/cli.js +4776 -2964
- package/package.json +2 -2
- package/skills/craft/ai-engineering/SKILL.md +2 -2
- package/skills/graphify/SKILL.md +0 -619
- package/skills/graphify/__init__.py +0 -28
- package/skills/graphify/__main__.py +0 -4582
- package/skills/graphify/affected.py +0 -154
- package/skills/graphify/always_on/agents-md.md +0 -12
- package/skills/graphify/always_on/antigravity-rules.md +0 -14
- package/skills/graphify/always_on/claude-md.md +0 -9
- package/skills/graphify/always_on/gemini-md.md +0 -9
- package/skills/graphify/always_on/kiro-steering.md +0 -5
- package/skills/graphify/always_on/vscode-instructions.md +0 -17
- package/skills/graphify/analyze.py +0 -724
- package/skills/graphify/benchmark.py +0 -155
- package/skills/graphify/build.py +0 -487
- package/skills/graphify/cache.py +0 -417
- package/skills/graphify/callflow_html.py +0 -2020
- package/skills/graphify/cluster.py +0 -272
- package/skills/graphify/command-kilo.md +0 -15
- package/skills/graphify/dedup.py +0 -429
- package/skills/graphify/detect.py +0 -1379
- package/skills/graphify/diagnostics.py +0 -390
- package/skills/graphify/export.py +0 -1408
- package/skills/graphify/extract.py +0 -11570
- package/skills/graphify/global_graph.py +0 -159
- package/skills/graphify/google_workspace.py +0 -223
- package/skills/graphify/hooks.py +0 -457
- package/skills/graphify/ingest.py +0 -331
- package/skills/graphify/llm.py +0 -1896
- package/skills/graphify/manifest.py +0 -4
- package/skills/graphify/mcp_ingest.py +0 -392
- package/skills/graphify/multigraph_compat.py +0 -212
- package/skills/graphify/pg_introspect.py +0 -142
- package/skills/graphify/prs.py +0 -748
- package/skills/graphify/querylog.py +0 -70
- package/skills/graphify/report.py +0 -218
- package/skills/graphify/scip_ingest.py +0 -363
- package/skills/graphify/security.py +0 -336
- package/skills/graphify/semantic_cleanup.py +0 -319
- package/skills/graphify/serve.py +0 -1309
- package/skills/graphify/skill-aider.md +0 -1246
- package/skills/graphify/skill-amp.md +0 -613
- package/skills/graphify/skill-claw.md +0 -616
- package/skills/graphify/skill-codex.md +0 -613
- package/skills/graphify/skill-copilot.md +0 -616
- package/skills/graphify/skill-devin.md +0 -1372
- package/skills/graphify/skill-droid.md +0 -613
- package/skills/graphify/skill-kilo.md +0 -625
- package/skills/graphify/skill-kiro.md +0 -615
- package/skills/graphify/skill-opencode.md +0 -608
- package/skills/graphify/skill-pi.md +0 -615
- package/skills/graphify/skill-trae.md +0 -614
- package/skills/graphify/skill-vscode.md +0 -612
- package/skills/graphify/skill-windows.md +0 -651
- package/skills/graphify/skills/amp/references/add-watch.md +0 -56
- package/skills/graphify/skills/amp/references/exports.md +0 -71
- package/skills/graphify/skills/amp/references/extraction-spec.md +0 -68
- package/skills/graphify/skills/amp/references/github-and-merge.md +0 -46
- package/skills/graphify/skills/amp/references/hooks.md +0 -33
- package/skills/graphify/skills/amp/references/query.md +0 -249
- package/skills/graphify/skills/amp/references/transcribe.md +0 -48
- package/skills/graphify/skills/amp/references/update.md +0 -179
- package/skills/graphify/skills/claude/references/add-watch.md +0 -56
- package/skills/graphify/skills/claude/references/exports.md +0 -71
- package/skills/graphify/skills/claude/references/extraction-spec.md +0 -68
- package/skills/graphify/skills/claude/references/github-and-merge.md +0 -46
- package/skills/graphify/skills/claude/references/hooks.md +0 -33
- package/skills/graphify/skills/claude/references/query.md +0 -103
- package/skills/graphify/skills/claude/references/transcribe.md +0 -48
- package/skills/graphify/skills/claude/references/update.md +0 -179
- package/skills/graphify/skills/claw/references/add-watch.md +0 -56
- package/skills/graphify/skills/claw/references/exports.md +0 -71
- package/skills/graphify/skills/claw/references/extraction-spec.md +0 -29
- package/skills/graphify/skills/claw/references/github-and-merge.md +0 -46
- package/skills/graphify/skills/claw/references/hooks.md +0 -33
- package/skills/graphify/skills/claw/references/query.md +0 -249
- package/skills/graphify/skills/claw/references/transcribe.md +0 -48
- package/skills/graphify/skills/claw/references/update.md +0 -179
- package/skills/graphify/skills/codex/references/add-watch.md +0 -56
- package/skills/graphify/skills/codex/references/exports.md +0 -71
- package/skills/graphify/skills/codex/references/extraction-spec.md +0 -29
- package/skills/graphify/skills/codex/references/github-and-merge.md +0 -46
- package/skills/graphify/skills/codex/references/hooks.md +0 -33
- package/skills/graphify/skills/codex/references/query.md +0 -249
- package/skills/graphify/skills/codex/references/transcribe.md +0 -48
- package/skills/graphify/skills/codex/references/update.md +0 -179
- package/skills/graphify/skills/copilot/references/add-watch.md +0 -56
- package/skills/graphify/skills/copilot/references/exports.md +0 -71
- package/skills/graphify/skills/copilot/references/extraction-spec.md +0 -68
- package/skills/graphify/skills/copilot/references/github-and-merge.md +0 -46
- package/skills/graphify/skills/copilot/references/hooks.md +0 -33
- package/skills/graphify/skills/copilot/references/query.md +0 -249
- package/skills/graphify/skills/copilot/references/transcribe.md +0 -48
- package/skills/graphify/skills/copilot/references/update.md +0 -179
- package/skills/graphify/skills/droid/references/add-watch.md +0 -56
- package/skills/graphify/skills/droid/references/exports.md +0 -71
- package/skills/graphify/skills/droid/references/extraction-spec.md +0 -68
- package/skills/graphify/skills/droid/references/github-and-merge.md +0 -46
- package/skills/graphify/skills/droid/references/hooks.md +0 -33
- package/skills/graphify/skills/droid/references/query.md +0 -249
- package/skills/graphify/skills/droid/references/transcribe.md +0 -48
- package/skills/graphify/skills/droid/references/update.md +0 -179
- package/skills/graphify/skills/kilo/references/add-watch.md +0 -56
- package/skills/graphify/skills/kilo/references/exports.md +0 -71
- package/skills/graphify/skills/kilo/references/extraction-spec.md +0 -68
- package/skills/graphify/skills/kilo/references/github-and-merge.md +0 -46
- package/skills/graphify/skills/kilo/references/hooks.md +0 -33
- package/skills/graphify/skills/kilo/references/query.md +0 -249
- package/skills/graphify/skills/kilo/references/transcribe.md +0 -48
- package/skills/graphify/skills/kilo/references/update.md +0 -179
- package/skills/graphify/skills/kiro/references/add-watch.md +0 -56
- package/skills/graphify/skills/kiro/references/exports.md +0 -71
- package/skills/graphify/skills/kiro/references/extraction-spec.md +0 -29
- package/skills/graphify/skills/kiro/references/github-and-merge.md +0 -46
- package/skills/graphify/skills/kiro/references/hooks.md +0 -33
- package/skills/graphify/skills/kiro/references/query.md +0 -249
- package/skills/graphify/skills/kiro/references/transcribe.md +0 -48
- package/skills/graphify/skills/kiro/references/update.md +0 -179
- package/skills/graphify/skills/opencode/references/add-watch.md +0 -56
- package/skills/graphify/skills/opencode/references/exports.md +0 -71
- package/skills/graphify/skills/opencode/references/extraction-spec.md +0 -68
- package/skills/graphify/skills/opencode/references/github-and-merge.md +0 -46
- package/skills/graphify/skills/opencode/references/hooks.md +0 -33
- package/skills/graphify/skills/opencode/references/query.md +0 -249
- package/skills/graphify/skills/opencode/references/transcribe.md +0 -48
- package/skills/graphify/skills/opencode/references/update.md +0 -179
- package/skills/graphify/skills/pi/references/add-watch.md +0 -56
- package/skills/graphify/skills/pi/references/exports.md +0 -71
- package/skills/graphify/skills/pi/references/extraction-spec.md +0 -29
- package/skills/graphify/skills/pi/references/github-and-merge.md +0 -46
- package/skills/graphify/skills/pi/references/hooks.md +0 -33
- package/skills/graphify/skills/pi/references/query.md +0 -249
- package/skills/graphify/skills/pi/references/transcribe.md +0 -48
- package/skills/graphify/skills/pi/references/update.md +0 -179
- package/skills/graphify/skills/trae/references/add-watch.md +0 -56
- package/skills/graphify/skills/trae/references/exports.md +0 -71
- package/skills/graphify/skills/trae/references/extraction-spec.md +0 -68
- package/skills/graphify/skills/trae/references/github-and-merge.md +0 -46
- package/skills/graphify/skills/trae/references/hooks.md +0 -35
- package/skills/graphify/skills/trae/references/query.md +0 -249
- package/skills/graphify/skills/trae/references/transcribe.md +0 -48
- package/skills/graphify/skills/trae/references/update.md +0 -179
- package/skills/graphify/skills/vscode/references/add-watch.md +0 -56
- package/skills/graphify/skills/vscode/references/exports.md +0 -71
- package/skills/graphify/skills/vscode/references/extraction-spec.md +0 -68
- package/skills/graphify/skills/vscode/references/github-and-merge.md +0 -46
- package/skills/graphify/skills/vscode/references/hooks.md +0 -33
- package/skills/graphify/skills/vscode/references/query.md +0 -249
- package/skills/graphify/skills/vscode/references/transcribe.md +0 -48
- package/skills/graphify/skills/vscode/references/update.md +0 -179
- package/skills/graphify/skills/windows/references/add-watch.md +0 -56
- package/skills/graphify/skills/windows/references/exports.md +0 -71
- package/skills/graphify/skills/windows/references/extraction-spec.md +0 -68
- package/skills/graphify/skills/windows/references/github-and-merge.md +0 -46
- package/skills/graphify/skills/windows/references/hooks.md +0 -33
- package/skills/graphify/skills/windows/references/query.md +0 -249
- package/skills/graphify/skills/windows/references/transcribe.md +0 -48
- package/skills/graphify/skills/windows/references/update.md +0 -179
- package/skills/graphify/symbol_resolution.py +0 -538
- package/skills/graphify/transcribe.py +0 -184
- package/skills/graphify/tree_html.py +0 -582
- package/skills/graphify/validate.py +0 -72
- package/skills/graphify/watch.py +0 -898
- package/skills/graphify/wiki.py +0 -282
|
@@ -1,1408 +0,0 @@
|
|
|
1
|
-
# write graph to HTML, JSON, SVG, GraphML, Obsidian vault, and Neo4j Cypher
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
import hashlib
|
|
4
|
-
import html as _html
|
|
5
|
-
import json
|
|
6
|
-
import math
|
|
7
|
-
import os
|
|
8
|
-
import re
|
|
9
|
-
import shutil
|
|
10
|
-
from collections import Counter
|
|
11
|
-
from datetime import date
|
|
12
|
-
from pathlib import Path
|
|
13
|
-
import networkx as nx
|
|
14
|
-
from networkx.readwrite import json_graph
|
|
15
|
-
from graphify.security import sanitize_label
|
|
16
|
-
from graphify.analyze import _node_community_map
|
|
17
|
-
from graphify.build import edge_data
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
# Artifacts worth preserving across rebuilds (non-regenerable without LLM or curation).
|
|
21
|
-
_BACKUP_ARTIFACTS = [
|
|
22
|
-
"graph.json",
|
|
23
|
-
"GRAPH_REPORT.md",
|
|
24
|
-
".graphify_labels.json",
|
|
25
|
-
".graphify_analysis.json",
|
|
26
|
-
"manifest.json",
|
|
27
|
-
".graphify_semantic_marker",
|
|
28
|
-
"cost.json",
|
|
29
|
-
]
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
def backup_if_protected(out_dir: Path) -> "Path | None":
|
|
33
|
-
"""Snapshot graph artifacts to a dated subfolder before an overwrite.
|
|
34
|
-
|
|
35
|
-
Triggers when graph.json exists AND either:
|
|
36
|
-
- .graphify_semantic_marker is present (graph cost real LLM tokens), or
|
|
37
|
-
- .graphify_labels.json contains at least one non-default community label
|
|
38
|
-
(graph has been curated by a human or skill).
|
|
39
|
-
|
|
40
|
-
Returns the backup folder path, or None if no backup was taken.
|
|
41
|
-
Never raises — backup failure prints a warning but never blocks the write.
|
|
42
|
-
Set GRAPHIFY_NO_BACKUP=1 to disable.
|
|
43
|
-
"""
|
|
44
|
-
if os.environ.get("GRAPHIFY_NO_BACKUP"):
|
|
45
|
-
return None
|
|
46
|
-
out = Path(out_dir)
|
|
47
|
-
if not (out / "graph.json").exists():
|
|
48
|
-
return None
|
|
49
|
-
|
|
50
|
-
is_semantic = (out / ".graphify_semantic_marker").exists()
|
|
51
|
-
is_curated = False
|
|
52
|
-
labels_file = out / ".graphify_labels.json"
|
|
53
|
-
if labels_file.exists():
|
|
54
|
-
try:
|
|
55
|
-
labels = json.loads(labels_file.read_text(encoding="utf-8"))
|
|
56
|
-
is_curated = any(v != f"Community {k}" for k, v in labels.items())
|
|
57
|
-
except Exception:
|
|
58
|
-
pass
|
|
59
|
-
|
|
60
|
-
if not is_semantic and not is_curated:
|
|
61
|
-
return None
|
|
62
|
-
|
|
63
|
-
reason = "+".join(filter(None, ["semantic" if is_semantic else "", "curated" if is_curated else ""]))
|
|
64
|
-
today = date.today().isoformat()
|
|
65
|
-
backup_dir = out / today
|
|
66
|
-
graph_src = out / "graph.json"
|
|
67
|
-
|
|
68
|
-
# Skip re-copying if today's backup already has identical graph.json content.
|
|
69
|
-
# If content differs (graph changed since the last backup today), overwrite
|
|
70
|
-
# the backup in place — one folder per day, always the latest pre-overwrite state.
|
|
71
|
-
if backup_dir.exists() and (backup_dir / "graph.json").exists():
|
|
72
|
-
src_hash = hashlib.sha256(graph_src.read_bytes()).hexdigest()
|
|
73
|
-
bak_hash = hashlib.sha256((backup_dir / "graph.json").read_bytes()).hexdigest()
|
|
74
|
-
if src_hash == bak_hash:
|
|
75
|
-
return backup_dir # identical content, nothing to do
|
|
76
|
-
|
|
77
|
-
try:
|
|
78
|
-
backup_dir.mkdir(parents=True, exist_ok=True)
|
|
79
|
-
copied = 0
|
|
80
|
-
for name in _BACKUP_ARTIFACTS:
|
|
81
|
-
src = out / name
|
|
82
|
-
if src.exists():
|
|
83
|
-
try:
|
|
84
|
-
shutil.copy2(src, backup_dir / name)
|
|
85
|
-
copied += 1
|
|
86
|
-
except Exception:
|
|
87
|
-
pass
|
|
88
|
-
if copied:
|
|
89
|
-
print(f"[graphify] backed up {reason} graph ({copied} files) -> {backup_dir.name}/")
|
|
90
|
-
return backup_dir
|
|
91
|
-
except Exception as exc:
|
|
92
|
-
import sys
|
|
93
|
-
print(f"[graphify] warning: backup failed ({exc}) - continuing with overwrite", file=sys.stderr)
|
|
94
|
-
return None
|
|
95
|
-
|
|
96
|
-
def _obsidian_tag(name: str) -> str:
|
|
97
|
-
"""Sanitize a community name for use as an Obsidian tag.
|
|
98
|
-
|
|
99
|
-
Obsidian tags only allow alphanumerics, hyphens, underscores, and slashes.
|
|
100
|
-
Spaces become underscores; everything else is stripped.
|
|
101
|
-
"""
|
|
102
|
-
return re.sub(r"[^a-zA-Z0-9_\-/]", "", name.replace(" ", "_"))
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
def _strip_diacritics(text: str) -> str:
|
|
106
|
-
import unicodedata
|
|
107
|
-
nfkd = unicodedata.normalize("NFKD", text)
|
|
108
|
-
return "".join(c for c in nfkd if not unicodedata.combining(c))
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
def _yaml_str(s: str) -> str:
|
|
112
|
-
"""Escape a value for safe embedding in a YAML double-quoted scalar (F-009).
|
|
113
|
-
|
|
114
|
-
See `graphify.ingest._yaml_str` for the full rationale; duplicated here to
|
|
115
|
-
avoid pulling the URL-fetching `ingest` module into export's dependency
|
|
116
|
-
graph. Handles backslash, double-quote, all line breaks (\\n, \\r,
|
|
117
|
-
U+2028, U+2029), tab, NUL, and other C0/DEL control characters that
|
|
118
|
-
would otherwise let a hostile `source_file` / `community` / etc. break
|
|
119
|
-
out of the YAML scalar and inject sibling keys.
|
|
120
|
-
"""
|
|
121
|
-
if s is None:
|
|
122
|
-
return ""
|
|
123
|
-
out: list[str] = []
|
|
124
|
-
for ch in str(s):
|
|
125
|
-
cp = ord(ch)
|
|
126
|
-
if ch == "\\":
|
|
127
|
-
out.append("\\\\")
|
|
128
|
-
elif ch == '"':
|
|
129
|
-
out.append('\\"')
|
|
130
|
-
elif ch == "\n":
|
|
131
|
-
out.append("\\n")
|
|
132
|
-
elif ch == "\r":
|
|
133
|
-
out.append("\\r")
|
|
134
|
-
elif ch == "\t":
|
|
135
|
-
out.append("\\t")
|
|
136
|
-
elif ch == "\0":
|
|
137
|
-
out.append("\\0")
|
|
138
|
-
elif cp == 0x2028:
|
|
139
|
-
out.append("\\L")
|
|
140
|
-
elif cp == 0x2029:
|
|
141
|
-
out.append("\\P")
|
|
142
|
-
elif cp < 0x20 or cp == 0x7F:
|
|
143
|
-
out.append(f"\\x{cp:02x}")
|
|
144
|
-
else:
|
|
145
|
-
out.append(ch)
|
|
146
|
-
return "".join(out)
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
COMMUNITY_COLORS = [
|
|
150
|
-
"#4E79A7", "#F28E2B", "#E15759", "#76B7B2", "#59A14F",
|
|
151
|
-
"#EDC948", "#B07AA1", "#FF9DA7", "#9C755F", "#BAB0AC",
|
|
152
|
-
]
|
|
153
|
-
|
|
154
|
-
MAX_NODES_FOR_VIZ = 5_000
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
def _viz_node_limit() -> int:
|
|
158
|
-
"""Return the effective viz node limit, honoring GRAPHIFY_VIZ_NODE_LIMIT env var.
|
|
159
|
-
|
|
160
|
-
Falls back to MAX_NODES_FOR_VIZ when the env var is unset, empty, or non-integer.
|
|
161
|
-
Set to 0 to disable HTML viz unconditionally (useful for CI runners).
|
|
162
|
-
"""
|
|
163
|
-
import os
|
|
164
|
-
raw = os.environ.get("GRAPHIFY_VIZ_NODE_LIMIT")
|
|
165
|
-
if raw is None or not raw.strip():
|
|
166
|
-
return MAX_NODES_FOR_VIZ
|
|
167
|
-
try:
|
|
168
|
-
return int(raw)
|
|
169
|
-
except ValueError:
|
|
170
|
-
return MAX_NODES_FOR_VIZ
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
def _html_styles() -> str:
|
|
174
|
-
return """<style>
|
|
175
|
-
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
176
|
-
body { background: #0f0f1a; color: #e0e0e0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; display: flex; height: 100vh; overflow: hidden; }
|
|
177
|
-
#graph { flex: 1; }
|
|
178
|
-
#sidebar { width: 280px; background: #1a1a2e; border-left: 1px solid #2a2a4e; display: flex; flex-direction: column; overflow: hidden; }
|
|
179
|
-
#search-wrap { padding: 12px; border-bottom: 1px solid #2a2a4e; }
|
|
180
|
-
#search { width: 100%; background: #0f0f1a; border: 1px solid #3a3a5e; color: #e0e0e0; padding: 7px 10px; border-radius: 6px; font-size: 13px; outline: none; }
|
|
181
|
-
#search:focus { border-color: #4E79A7; }
|
|
182
|
-
#search-results { max-height: 140px; overflow-y: auto; padding: 4px 12px; border-bottom: 1px solid #2a2a4e; display: none; }
|
|
183
|
-
.search-item { padding: 4px 6px; cursor: pointer; border-radius: 4px; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
184
|
-
.search-item:hover { background: #2a2a4e; }
|
|
185
|
-
#info-panel { padding: 14px; border-bottom: 1px solid #2a2a4e; min-height: 140px; }
|
|
186
|
-
#info-panel h3 { font-size: 13px; color: #aaa; margin-bottom: 8px; text-transform: uppercase; letter-spacing: 0.05em; }
|
|
187
|
-
#info-content { font-size: 13px; color: #ccc; line-height: 1.6; }
|
|
188
|
-
#info-content .field { margin-bottom: 5px; }
|
|
189
|
-
#info-content .field b { color: #e0e0e0; }
|
|
190
|
-
#info-content .empty { color: #555; font-style: italic; }
|
|
191
|
-
.neighbor-link { display: block; padding: 2px 6px; margin: 2px 0; border-radius: 3px; cursor: pointer; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; border-left: 3px solid #333; }
|
|
192
|
-
.neighbor-link:hover { background: #2a2a4e; }
|
|
193
|
-
#neighbors-list { max-height: 160px; overflow-y: auto; margin-top: 4px; }
|
|
194
|
-
#legend-wrap { flex: 1; overflow-y: auto; padding: 12px; }
|
|
195
|
-
#legend-wrap h3 { font-size: 13px; color: #aaa; margin-bottom: 10px; text-transform: uppercase; letter-spacing: 0.05em; }
|
|
196
|
-
.legend-item { display: flex; align-items: center; gap: 8px; padding: 4px 0; cursor: pointer; border-radius: 4px; font-size: 12px; }
|
|
197
|
-
.legend-item:hover { background: #2a2a4e; padding-left: 4px; }
|
|
198
|
-
.legend-item.dimmed { opacity: 0.35; }
|
|
199
|
-
.legend-dot { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; }
|
|
200
|
-
.legend-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
201
|
-
.legend-count { color: #666; font-size: 11px; }
|
|
202
|
-
#stats { padding: 10px 14px; border-top: 1px solid #2a2a4e; font-size: 11px; color: #555; }
|
|
203
|
-
#legend-controls { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; padding: 4px 0; }
|
|
204
|
-
#legend-controls label { display: flex; align-items: center; gap: 6px; cursor: pointer; font-size: 12px; color: #aaa; user-select: none; }
|
|
205
|
-
#legend-controls label:hover { color: #e0e0e0; }
|
|
206
|
-
.legend-cb, #select-all-cb { appearance: none; -webkit-appearance: none; width: 14px; height: 14px; border: 1.5px solid #3a3a5e; border-radius: 3px; background: #0f0f1a; cursor: pointer; position: relative; flex-shrink: 0; }
|
|
207
|
-
.legend-cb:checked, #select-all-cb:checked { background: #4E79A7; border-color: #4E79A7; }
|
|
208
|
-
.legend-cb:checked::after, #select-all-cb:checked::after { content: ''; position: absolute; left: 3.5px; top: 1px; width: 4px; height: 7px; border: solid #fff; border-width: 0 2px 2px 0; transform: rotate(45deg); }
|
|
209
|
-
#select-all-cb:indeterminate { background: #4E79A7; border-color: #4E79A7; }
|
|
210
|
-
#select-all-cb:indeterminate::after { content: ''; position: absolute; left: 2px; top: 5px; width: 8px; height: 2px; background: #fff; border: none; transform: none; }
|
|
211
|
-
</style>"""
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
def _hyperedge_script(hyperedges_json: str) -> str:
|
|
215
|
-
return f"""<script>
|
|
216
|
-
// Render hyperedges as shaded regions
|
|
217
|
-
const hyperedges = {hyperedges_json};
|
|
218
|
-
// afterDrawing passes ctx already transformed to network coordinate space.
|
|
219
|
-
// Draw node positions raw — no manual pan/zoom/DPR math needed.
|
|
220
|
-
network.on('afterDrawing', function(ctx) {{
|
|
221
|
-
hyperedges.forEach(h => {{
|
|
222
|
-
const positions = h.nodes
|
|
223
|
-
.map(nid => network.getPositions([nid])[nid])
|
|
224
|
-
.filter(p => p !== undefined);
|
|
225
|
-
if (positions.length < 2) return;
|
|
226
|
-
ctx.save();
|
|
227
|
-
ctx.globalAlpha = 0.12;
|
|
228
|
-
ctx.fillStyle = '#6366f1';
|
|
229
|
-
ctx.strokeStyle = '#6366f1';
|
|
230
|
-
ctx.lineWidth = 2;
|
|
231
|
-
ctx.beginPath();
|
|
232
|
-
// Centroid and expanded hull in network coordinates
|
|
233
|
-
const cx = positions.reduce((s, p) => s + p.x, 0) / positions.length;
|
|
234
|
-
const cy = positions.reduce((s, p) => s + p.y, 0) / positions.length;
|
|
235
|
-
const expanded = positions.map(p => ({{
|
|
236
|
-
x: cx + (p.x - cx) * 1.15,
|
|
237
|
-
y: cy + (p.y - cy) * 1.15
|
|
238
|
-
}}));
|
|
239
|
-
ctx.moveTo(expanded[0].x, expanded[0].y);
|
|
240
|
-
expanded.slice(1).forEach(p => ctx.lineTo(p.x, p.y));
|
|
241
|
-
ctx.closePath();
|
|
242
|
-
ctx.fill();
|
|
243
|
-
ctx.globalAlpha = 0.4;
|
|
244
|
-
ctx.stroke();
|
|
245
|
-
// Label
|
|
246
|
-
ctx.globalAlpha = 0.8;
|
|
247
|
-
ctx.fillStyle = '#4f46e5';
|
|
248
|
-
ctx.font = 'bold 11px sans-serif';
|
|
249
|
-
ctx.textAlign = 'center';
|
|
250
|
-
ctx.fillText(h.label, cx, cy - 5);
|
|
251
|
-
ctx.restore();
|
|
252
|
-
}});
|
|
253
|
-
}});
|
|
254
|
-
</script>"""
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
def _html_script(nodes_json: str, edges_json: str, legend_json: str) -> str:
|
|
258
|
-
return f"""<script>
|
|
259
|
-
const RAW_NODES = {nodes_json};
|
|
260
|
-
const RAW_EDGES = {edges_json};
|
|
261
|
-
const LEGEND = {legend_json};
|
|
262
|
-
|
|
263
|
-
// HTML-escape helper — prevents XSS when injecting graph data into innerHTML
|
|
264
|
-
function esc(s) {{
|
|
265
|
-
return String(s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/'/g,''');
|
|
266
|
-
}}
|
|
267
|
-
|
|
268
|
-
// Build vis datasets
|
|
269
|
-
const nodesDS = new vis.DataSet(RAW_NODES.map(n => ({{
|
|
270
|
-
id: n.id, label: n.label, color: n.color, size: n.size,
|
|
271
|
-
font: n.font, title: n.title,
|
|
272
|
-
_community: n.community, _community_name: n.community_name,
|
|
273
|
-
_source_file: n.source_file, _file_type: n.file_type, _degree: n.degree,
|
|
274
|
-
}})));
|
|
275
|
-
|
|
276
|
-
const edgesDS = new vis.DataSet(RAW_EDGES.map((e, i) => ({{
|
|
277
|
-
id: i, from: e.from, to: e.to,
|
|
278
|
-
label: '',
|
|
279
|
-
title: e.title,
|
|
280
|
-
dashes: e.dashes,
|
|
281
|
-
width: e.width,
|
|
282
|
-
color: e.color,
|
|
283
|
-
arrows: {{ to: {{ enabled: true, scaleFactor: 0.5 }} }},
|
|
284
|
-
}})));
|
|
285
|
-
|
|
286
|
-
const container = document.getElementById('graph');
|
|
287
|
-
const network = new vis.Network(container, {{ nodes: nodesDS, edges: edgesDS }}, {{
|
|
288
|
-
physics: {{
|
|
289
|
-
enabled: true,
|
|
290
|
-
solver: 'forceAtlas2Based',
|
|
291
|
-
forceAtlas2Based: {{
|
|
292
|
-
gravitationalConstant: -60,
|
|
293
|
-
centralGravity: 0.005,
|
|
294
|
-
springLength: 120,
|
|
295
|
-
springConstant: 0.08,
|
|
296
|
-
damping: 0.4,
|
|
297
|
-
avoidOverlap: 0.8,
|
|
298
|
-
}},
|
|
299
|
-
stabilization: {{ iterations: 200, fit: true }},
|
|
300
|
-
}},
|
|
301
|
-
interaction: {{
|
|
302
|
-
hover: true,
|
|
303
|
-
tooltipDelay: 100,
|
|
304
|
-
hideEdgesOnDrag: true,
|
|
305
|
-
navigationButtons: false,
|
|
306
|
-
keyboard: false,
|
|
307
|
-
}},
|
|
308
|
-
nodes: {{ shape: 'dot', borderWidth: 1.5 }},
|
|
309
|
-
edges: {{ smooth: {{ type: 'continuous', roundness: 0.2 }}, selectionWidth: 3 }},
|
|
310
|
-
}});
|
|
311
|
-
|
|
312
|
-
network.once('stabilizationIterationsDone', () => {{
|
|
313
|
-
network.setOptions({{ physics: {{ enabled: false }} }});
|
|
314
|
-
}});
|
|
315
|
-
|
|
316
|
-
function showInfo(nodeId) {{
|
|
317
|
-
const n = nodesDS.get(nodeId);
|
|
318
|
-
if (!n) return;
|
|
319
|
-
const neighborIds = network.getConnectedNodes(nodeId);
|
|
320
|
-
const neighborItems = neighborIds.map(nid => {{
|
|
321
|
-
const nb = nodesDS.get(nid);
|
|
322
|
-
const color = nb ? nb.color.background : '#555';
|
|
323
|
-
return `<span class="neighbor-link" style="border-left-color:${{esc(color)}}" onclick="focusNode(${{JSON.stringify(nid)}})">${{esc(nb ? nb.label : nid)}}</span>`;
|
|
324
|
-
}}).join('');
|
|
325
|
-
document.getElementById('info-content').innerHTML = `
|
|
326
|
-
<div class="field"><b>${{esc(n.label)}}</b></div>
|
|
327
|
-
<div class="field">Type: ${{esc(n._file_type || 'unknown')}}</div>
|
|
328
|
-
<div class="field">Community: ${{esc(n._community_name)}}</div>
|
|
329
|
-
<div class="field">Source: ${{esc(n._source_file || '-')}}</div>
|
|
330
|
-
<div class="field">Degree: ${{n._degree}}</div>
|
|
331
|
-
${{neighborIds.length ? `<div class="field" style="margin-top:8px;color:#aaa;font-size:11px">Neighbors (${{neighborIds.length}})</div><div id="neighbors-list">${{neighborItems}}</div>` : ''}}
|
|
332
|
-
`;
|
|
333
|
-
}}
|
|
334
|
-
|
|
335
|
-
function focusNode(nodeId) {{
|
|
336
|
-
network.focus(nodeId, {{ scale: 1.4, animation: true }});
|
|
337
|
-
network.selectNodes([nodeId]);
|
|
338
|
-
showInfo(nodeId);
|
|
339
|
-
}}
|
|
340
|
-
|
|
341
|
-
// Track hovered node — hover detection is more reliable than click params
|
|
342
|
-
let hoveredNodeId = null;
|
|
343
|
-
network.on('hoverNode', params => {{
|
|
344
|
-
hoveredNodeId = params.node;
|
|
345
|
-
container.style.cursor = 'pointer';
|
|
346
|
-
}});
|
|
347
|
-
network.on('blurNode', () => {{
|
|
348
|
-
hoveredNodeId = null;
|
|
349
|
-
container.style.cursor = 'default';
|
|
350
|
-
}});
|
|
351
|
-
container.addEventListener('click', () => {{
|
|
352
|
-
if (hoveredNodeId !== null) {{
|
|
353
|
-
showInfo(hoveredNodeId);
|
|
354
|
-
network.selectNodes([hoveredNodeId]);
|
|
355
|
-
}}
|
|
356
|
-
}});
|
|
357
|
-
network.on('click', params => {{
|
|
358
|
-
if (params.nodes.length > 0) {{
|
|
359
|
-
showInfo(params.nodes[0]);
|
|
360
|
-
}} else if (hoveredNodeId === null) {{
|
|
361
|
-
document.getElementById('info-content').innerHTML = '<span class="empty">Click a node to inspect it</span>';
|
|
362
|
-
}}
|
|
363
|
-
}});
|
|
364
|
-
|
|
365
|
-
const searchInput = document.getElementById('search');
|
|
366
|
-
const searchResults = document.getElementById('search-results');
|
|
367
|
-
searchInput.addEventListener('input', () => {{
|
|
368
|
-
const q = searchInput.value.toLowerCase().trim();
|
|
369
|
-
searchResults.innerHTML = '';
|
|
370
|
-
if (!q) {{ searchResults.style.display = 'none'; return; }}
|
|
371
|
-
const matches = RAW_NODES.filter(n => n.label.toLowerCase().includes(q)).slice(0, 20);
|
|
372
|
-
if (!matches.length) {{ searchResults.style.display = 'none'; return; }}
|
|
373
|
-
searchResults.style.display = 'block';
|
|
374
|
-
matches.forEach(n => {{
|
|
375
|
-
const el = document.createElement('div');
|
|
376
|
-
el.className = 'search-item';
|
|
377
|
-
el.textContent = n.label;
|
|
378
|
-
el.style.borderLeft = `3px solid ${{n.color.background}}`;
|
|
379
|
-
el.style.paddingLeft = '8px';
|
|
380
|
-
el.onclick = () => {{
|
|
381
|
-
network.focus(n.id, {{ scale: 1.5, animation: true }});
|
|
382
|
-
network.selectNodes([n.id]);
|
|
383
|
-
showInfo(n.id);
|
|
384
|
-
searchResults.style.display = 'none';
|
|
385
|
-
searchInput.value = '';
|
|
386
|
-
}};
|
|
387
|
-
searchResults.appendChild(el);
|
|
388
|
-
}});
|
|
389
|
-
}});
|
|
390
|
-
document.addEventListener('click', e => {{
|
|
391
|
-
if (!searchResults.contains(e.target) && e.target !== searchInput)
|
|
392
|
-
searchResults.style.display = 'none';
|
|
393
|
-
}});
|
|
394
|
-
|
|
395
|
-
const hiddenCommunities = new Set();
|
|
396
|
-
|
|
397
|
-
const selectAllCb = document.getElementById('select-all-cb');
|
|
398
|
-
|
|
399
|
-
function updateSelectAllState() {{
|
|
400
|
-
const total = LEGEND.length;
|
|
401
|
-
const hidden = hiddenCommunities.size;
|
|
402
|
-
selectAllCb.checked = hidden === 0;
|
|
403
|
-
selectAllCb.indeterminate = hidden > 0 && hidden < total;
|
|
404
|
-
}}
|
|
405
|
-
|
|
406
|
-
function toggleAllCommunities(hide) {{
|
|
407
|
-
document.querySelectorAll('.legend-item').forEach(item => {{
|
|
408
|
-
hide ? item.classList.add('dimmed') : item.classList.remove('dimmed');
|
|
409
|
-
}});
|
|
410
|
-
document.querySelectorAll('.legend-cb').forEach(cb => {{
|
|
411
|
-
cb.checked = !hide;
|
|
412
|
-
}});
|
|
413
|
-
LEGEND.forEach(c => {{
|
|
414
|
-
if (hide) hiddenCommunities.add(c.cid); else hiddenCommunities.delete(c.cid);
|
|
415
|
-
}});
|
|
416
|
-
const updates = RAW_NODES.map(n => ({{ id: n.id, hidden: hide }}));
|
|
417
|
-
nodesDS.update(updates);
|
|
418
|
-
updateSelectAllState();
|
|
419
|
-
}}
|
|
420
|
-
|
|
421
|
-
const legendEl = document.getElementById('legend');
|
|
422
|
-
LEGEND.forEach(c => {{
|
|
423
|
-
const item = document.createElement('div');
|
|
424
|
-
item.className = 'legend-item';
|
|
425
|
-
const cb = document.createElement('input');
|
|
426
|
-
cb.type = 'checkbox';
|
|
427
|
-
cb.className = 'legend-cb';
|
|
428
|
-
cb.checked = true;
|
|
429
|
-
cb.addEventListener('change', (e) => {{
|
|
430
|
-
e.stopPropagation();
|
|
431
|
-
if (cb.checked) {{
|
|
432
|
-
hiddenCommunities.delete(c.cid);
|
|
433
|
-
item.classList.remove('dimmed');
|
|
434
|
-
}} else {{
|
|
435
|
-
hiddenCommunities.add(c.cid);
|
|
436
|
-
item.classList.add('dimmed');
|
|
437
|
-
}}
|
|
438
|
-
const updates = RAW_NODES
|
|
439
|
-
.filter(n => n.community === c.cid)
|
|
440
|
-
.map(n => ({{ id: n.id, hidden: !cb.checked }}));
|
|
441
|
-
nodesDS.update(updates);
|
|
442
|
-
updateSelectAllState();
|
|
443
|
-
}});
|
|
444
|
-
item.innerHTML = `<div class="legend-dot" style="background:${{c.color}}"></div>
|
|
445
|
-
<span class="legend-label">${{c.label}}</span>
|
|
446
|
-
<span class="legend-count">${{c.count}}</span>`;
|
|
447
|
-
item.prepend(cb);
|
|
448
|
-
item.onclick = (e) => {{
|
|
449
|
-
if (e.target === cb) return;
|
|
450
|
-
cb.checked = !cb.checked;
|
|
451
|
-
cb.dispatchEvent(new Event('change'));
|
|
452
|
-
}};
|
|
453
|
-
legendEl.appendChild(item);
|
|
454
|
-
}});
|
|
455
|
-
</script>"""
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
_CONFIDENCE_SCORE_DEFAULTS = {"EXTRACTED": 1.0, "INFERRED": 0.5, "AMBIGUOUS": 0.2}
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
def attach_hyperedges(G: nx.Graph, hyperedges: list) -> None:
|
|
462
|
-
"""Store hyperedges in the graph's metadata dict."""
|
|
463
|
-
existing = G.graph.get("hyperedges", [])
|
|
464
|
-
seen_ids = {h["id"] for h in existing}
|
|
465
|
-
for h in hyperedges:
|
|
466
|
-
if h.get("id") and h["id"] not in seen_ids:
|
|
467
|
-
existing.append(h)
|
|
468
|
-
seen_ids.add(h["id"])
|
|
469
|
-
G.graph["hyperedges"] = existing
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
def _git_head() -> str | None:
|
|
473
|
-
"""Return the current git HEAD commit hash, or None if not in a git repo."""
|
|
474
|
-
import subprocess as _sp
|
|
475
|
-
try:
|
|
476
|
-
r = _sp.run(["git", "rev-parse", "HEAD"], capture_output=True, text=True, timeout=3)
|
|
477
|
-
return r.stdout.strip() if r.returncode == 0 else None
|
|
478
|
-
except Exception:
|
|
479
|
-
return None
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
def to_json(G: nx.Graph, communities: dict[int, list[str]], output_path: str, *, force: bool = False, built_at_commit: str | None = None) -> bool:
|
|
483
|
-
# Safety check: refuse to silently shrink an existing graph (#479)
|
|
484
|
-
existing_path = Path(output_path)
|
|
485
|
-
if not force and existing_path.exists():
|
|
486
|
-
try:
|
|
487
|
-
from graphify.security import check_graph_file_size_cap
|
|
488
|
-
check_graph_file_size_cap(existing_path)
|
|
489
|
-
existing_data = json.loads(existing_path.read_text(encoding="utf-8"))
|
|
490
|
-
existing_n = len(existing_data.get("nodes", []))
|
|
491
|
-
new_n = G.number_of_nodes()
|
|
492
|
-
if new_n < existing_n:
|
|
493
|
-
import sys as _sys
|
|
494
|
-
print(
|
|
495
|
-
f"[graphify] WARNING: new graph has {new_n} nodes but existing "
|
|
496
|
-
f"graph.json has {existing_n} (net -{existing_n - new_n}). "
|
|
497
|
-
f"Refusing to overwrite. Possible causes: missing chunk files from "
|
|
498
|
-
f"a previous session, or fuzzy dedup collapsed same-named symbols "
|
|
499
|
-
f"across files during an --update on an already-current graph. "
|
|
500
|
-
f"Run a full rebuild (/graphify .) to be safe, or pass force=True "
|
|
501
|
-
f"only if you have verified the reduction is legitimate.",
|
|
502
|
-
file=_sys.stderr,
|
|
503
|
-
)
|
|
504
|
-
return False
|
|
505
|
-
except Exception:
|
|
506
|
-
pass # unreadable existing file — proceed with write
|
|
507
|
-
|
|
508
|
-
node_community = _node_community_map(communities)
|
|
509
|
-
try:
|
|
510
|
-
data = json_graph.node_link_data(G, edges="links")
|
|
511
|
-
except TypeError:
|
|
512
|
-
data = json_graph.node_link_data(G)
|
|
513
|
-
for node in data["nodes"]:
|
|
514
|
-
node["community"] = node_community.get(node["id"])
|
|
515
|
-
node["norm_label"] = _strip_diacritics(node.get("label", "")).lower()
|
|
516
|
-
for link in data["links"]:
|
|
517
|
-
if "confidence_score" not in link:
|
|
518
|
-
conf = link.get("confidence", "EXTRACTED")
|
|
519
|
-
link["confidence_score"] = _CONFIDENCE_SCORE_DEFAULTS.get(conf, 1.0)
|
|
520
|
-
# Restore original edge direction. Undirected NetworkX storage may
|
|
521
|
-
# canonicalize endpoint order, flipping `calls` and other directional
|
|
522
|
-
# edges in graph.json. The build path stashes the true endpoints in
|
|
523
|
-
# _src/_tgt for exactly this purpose (#563).
|
|
524
|
-
true_src = link.pop("_src", None)
|
|
525
|
-
true_tgt = link.pop("_tgt", None)
|
|
526
|
-
if true_src is not None and true_tgt is not None:
|
|
527
|
-
link["source"] = true_src
|
|
528
|
-
link["target"] = true_tgt
|
|
529
|
-
data["hyperedges"] = getattr(G, "graph", {}).get("hyperedges", [])
|
|
530
|
-
commit = built_at_commit if built_at_commit is not None else _git_head()
|
|
531
|
-
if commit:
|
|
532
|
-
data["built_at_commit"] = commit
|
|
533
|
-
with open(output_path, "w", encoding="utf-8") as f: # nosec
|
|
534
|
-
json.dump(data, f, indent=2)
|
|
535
|
-
return True
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
def prune_dangling_edges(graph_data: dict) -> tuple[dict, int]:
|
|
539
|
-
"""Remove edges whose source or target node is not in the node set.
|
|
540
|
-
|
|
541
|
-
Returns the cleaned graph_data dict and the number of pruned edges.
|
|
542
|
-
"""
|
|
543
|
-
node_ids = {n["id"] for n in graph_data["nodes"]}
|
|
544
|
-
links_key = "links" if "links" in graph_data else "edges"
|
|
545
|
-
before = len(graph_data[links_key])
|
|
546
|
-
graph_data[links_key] = [
|
|
547
|
-
e for e in graph_data[links_key]
|
|
548
|
-
if e["source"] in node_ids and e["target"] in node_ids
|
|
549
|
-
]
|
|
550
|
-
return graph_data, before - len(graph_data[links_key])
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
def _cypher_escape(s: str) -> str:
|
|
554
|
-
"""Escape a string for safe embedding in a Cypher single-quoted literal.
|
|
555
|
-
|
|
556
|
-
Handles all characters that could prematurely terminate the literal or
|
|
557
|
-
inject control sequences:
|
|
558
|
-
- `\\` and `'` (literal terminators)
|
|
559
|
-
- newlines/CRs (would break the per-line statement framing)
|
|
560
|
-
- NUL/control bytes (defensive — Neo4j errors on raw NULs)
|
|
561
|
-
|
|
562
|
-
Also strips any leading/trailing whitespace that would let an attacker
|
|
563
|
-
break the `;`-terminated statement boundary used by `cypher-shell`.
|
|
564
|
-
Closing `}` and `)` are NOT special inside a single-quoted Cypher string,
|
|
565
|
-
so escaping the quote and backslash correctly is sufficient (a `}` inside
|
|
566
|
-
a properly-closed `'...'` literal is just a character) — but we previously
|
|
567
|
-
missed `\\n` / `\\r` which DO let a payload break out of the statement
|
|
568
|
-
line and inject a fresh MATCH/DELETE on the following line. See F-008.
|
|
569
|
-
"""
|
|
570
|
-
# First normalise: drop NUL and other C0 control chars except tab.
|
|
571
|
-
s = "".join(ch for ch in s if ch >= " " or ch == "\t")
|
|
572
|
-
return (
|
|
573
|
-
s.replace("\\", "\\\\")
|
|
574
|
-
.replace("'", "\\'")
|
|
575
|
-
.replace("\n", "\\n")
|
|
576
|
-
.replace("\r", "\\r")
|
|
577
|
-
)
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
# Restrict identifier-position values (labels and relationship types are NOT
|
|
581
|
-
# quoted in Cypher and so cannot be safely escaped — they must be allowlisted).
|
|
582
|
-
_CYPHER_IDENT_RE = re.compile(r"[^A-Za-z0-9_]")
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
def _cypher_label(raw: str, fallback: str) -> str:
|
|
586
|
-
"""Sanitise a value used in identifier position (node label / rel type).
|
|
587
|
-
|
|
588
|
-
Cypher does not provide a way to escape `:Foo` label syntax, so we must
|
|
589
|
-
strip everything except `[A-Za-z0-9_]` and require the result to start
|
|
590
|
-
with a letter; otherwise we fall back to a safe constant.
|
|
591
|
-
"""
|
|
592
|
-
cleaned = _CYPHER_IDENT_RE.sub("", raw or "")
|
|
593
|
-
if not cleaned or not cleaned[0].isalpha():
|
|
594
|
-
return fallback
|
|
595
|
-
return cleaned
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
def to_cypher(G: nx.Graph, output_path: str) -> None:
|
|
599
|
-
lines = ["// Neo4j Cypher import - generated by /graphify", ""]
|
|
600
|
-
for node_id, data in G.nodes(data=True):
|
|
601
|
-
label = _cypher_escape(data.get("label", node_id))
|
|
602
|
-
node_id_esc = _cypher_escape(node_id)
|
|
603
|
-
ftype = _cypher_label(
|
|
604
|
-
(data.get("file_type", "unknown") or "unknown").capitalize(),
|
|
605
|
-
"Entity",
|
|
606
|
-
)
|
|
607
|
-
lines.append(f"MERGE (n:{ftype} {{id: '{node_id_esc}', label: '{label}'}});")
|
|
608
|
-
lines.append("")
|
|
609
|
-
for u, v, data in G.edges(data=True):
|
|
610
|
-
rel = _cypher_label(
|
|
611
|
-
(data.get("relation", "RELATES_TO") or "RELATES_TO").upper(),
|
|
612
|
-
"RELATES_TO",
|
|
613
|
-
)
|
|
614
|
-
conf = _cypher_escape(data.get("confidence", "EXTRACTED"))
|
|
615
|
-
u_esc = _cypher_escape(u)
|
|
616
|
-
v_esc = _cypher_escape(v)
|
|
617
|
-
lines.append(
|
|
618
|
-
f"MATCH (a {{id: '{u_esc}'}}), (b {{id: '{v_esc}'}}) "
|
|
619
|
-
f"MERGE (a)-[:{rel} {{confidence: '{conf}'}}]->(b);"
|
|
620
|
-
)
|
|
621
|
-
with open(output_path, "w", encoding="utf-8") as f: # nosec
|
|
622
|
-
f.write("\n".join(lines))
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
def to_html(
|
|
626
|
-
G: nx.Graph,
|
|
627
|
-
communities: dict[int, list[str]],
|
|
628
|
-
output_path: str,
|
|
629
|
-
community_labels: dict[int, str] | None = None,
|
|
630
|
-
member_counts: dict[int, int] | None = None,
|
|
631
|
-
node_limit: int | None = None,
|
|
632
|
-
) -> None:
|
|
633
|
-
"""Generate an interactive vis.js HTML visualization of the graph.
|
|
634
|
-
|
|
635
|
-
Features: node size by degree, click-to-inspect panel, search box,
|
|
636
|
-
community filter, physics clustering by community, confidence-styled edges.
|
|
637
|
-
Raises ValueError if graph exceeds MAX_NODES_FOR_VIZ.
|
|
638
|
-
|
|
639
|
-
If member_counts is provided (aggregated community view), node sizes are
|
|
640
|
-
based on community member counts rather than graph degree.
|
|
641
|
-
|
|
642
|
-
If node_limit is set and the graph exceeds it, automatically builds an
|
|
643
|
-
aggregated community-level meta-graph instead of raising ValueError.
|
|
644
|
-
"""
|
|
645
|
-
limit = node_limit if node_limit is not None else _viz_node_limit()
|
|
646
|
-
if G.number_of_nodes() > limit:
|
|
647
|
-
if node_limit is not None:
|
|
648
|
-
# Build aggregated community meta-graph
|
|
649
|
-
from collections import Counter as _Counter
|
|
650
|
-
import networkx as _nx
|
|
651
|
-
print(f"Graph has {G.number_of_nodes()} nodes (above {limit} limit). Building aggregated community view...")
|
|
652
|
-
node_to_community = {nid: cid for cid, members in communities.items() for nid in members}
|
|
653
|
-
meta = _nx.Graph()
|
|
654
|
-
for cid, members in communities.items():
|
|
655
|
-
meta.add_node(str(cid), label=(community_labels or {}).get(cid, f"Community {cid}"))
|
|
656
|
-
edge_counts = _Counter()
|
|
657
|
-
for u, v in G.edges():
|
|
658
|
-
cu, cv = node_to_community.get(u), node_to_community.get(v)
|
|
659
|
-
if cu is not None and cv is not None and cu != cv:
|
|
660
|
-
edge_counts[(min(cu, cv), max(cu, cv))] += 1
|
|
661
|
-
for (cu, cv), w in edge_counts.items():
|
|
662
|
-
meta.add_edge(str(cu), str(cv), weight=w,
|
|
663
|
-
relation=f"{w} cross-community edges", confidence="AGGREGATED")
|
|
664
|
-
if meta.number_of_nodes() <= 1:
|
|
665
|
-
print("Single community - aggregated view not useful. Skipping graph.html.")
|
|
666
|
-
return
|
|
667
|
-
meta_communities = {cid: [str(cid)] for cid in communities}
|
|
668
|
-
mc = {cid: len(members) for cid, members in communities.items()}
|
|
669
|
-
# Remap hyperedges from semantic node IDs to community IDs
|
|
670
|
-
raw_hyperedges = G.graph.get("hyperedges", [])
|
|
671
|
-
if raw_hyperedges:
|
|
672
|
-
remapped = []
|
|
673
|
-
for he in raw_hyperedges:
|
|
674
|
-
he_members = he.get("nodes") or he.get("members") or []
|
|
675
|
-
comm_ids, seen = [], set()
|
|
676
|
-
for nid in he_members:
|
|
677
|
-
c = node_to_community.get(nid)
|
|
678
|
-
if c is None:
|
|
679
|
-
continue
|
|
680
|
-
s = str(c)
|
|
681
|
-
if s in seen:
|
|
682
|
-
continue
|
|
683
|
-
seen.add(s)
|
|
684
|
-
comm_ids.append(s)
|
|
685
|
-
if len(comm_ids) < 2:
|
|
686
|
-
continue
|
|
687
|
-
remapped.append({
|
|
688
|
-
"id": he.get("id", ""),
|
|
689
|
-
"label": he.get("label") or he.get("relation", "").replace("_", " "),
|
|
690
|
-
"nodes": comm_ids,
|
|
691
|
-
})
|
|
692
|
-
meta.graph["hyperedges"] = remapped
|
|
693
|
-
to_html(meta, meta_communities, output_path,
|
|
694
|
-
community_labels=community_labels, member_counts=mc)
|
|
695
|
-
print(f"graph.html written (aggregated: {meta.number_of_nodes()} community nodes, {meta.number_of_edges()} cross-community edges)")
|
|
696
|
-
print("Tip: run with --obsidian for full node-level detail.")
|
|
697
|
-
return
|
|
698
|
-
raise ValueError(
|
|
699
|
-
f"Graph has {G.number_of_nodes()} nodes - too large for HTML viz "
|
|
700
|
-
f"(limit: {limit}). Use --no-viz, raise GRAPHIFY_VIZ_NODE_LIMIT, "
|
|
701
|
-
f"or reduce input size."
|
|
702
|
-
)
|
|
703
|
-
|
|
704
|
-
node_community = _node_community_map(communities)
|
|
705
|
-
degree = dict(G.degree())
|
|
706
|
-
max_deg = max(degree.values(), default=1) or 1
|
|
707
|
-
max_mc = (max(member_counts.values(), default=1) or 1) if member_counts else 1
|
|
708
|
-
|
|
709
|
-
# Build nodes list for vis.js
|
|
710
|
-
vis_nodes = []
|
|
711
|
-
for node_id, data in G.nodes(data=True):
|
|
712
|
-
cid = node_community.get(node_id, 0)
|
|
713
|
-
color = COMMUNITY_COLORS[cid % len(COMMUNITY_COLORS)]
|
|
714
|
-
label = sanitize_label(data.get("label", node_id))
|
|
715
|
-
deg = degree.get(node_id, 1)
|
|
716
|
-
if member_counts:
|
|
717
|
-
mc = member_counts.get(cid, 1)
|
|
718
|
-
size = 10 + 30 * (mc / max_mc)
|
|
719
|
-
font_size = 12
|
|
720
|
-
else:
|
|
721
|
-
size = 10 + 30 * (deg / max_deg)
|
|
722
|
-
# Only show label for high-degree nodes by default; others show on hover
|
|
723
|
-
font_size = 12 if deg >= max_deg * 0.15 else 0
|
|
724
|
-
vis_nodes.append({
|
|
725
|
-
"id": node_id,
|
|
726
|
-
"label": label,
|
|
727
|
-
"color": {"background": color, "border": color, "highlight": {"background": "#ffffff", "border": color}},
|
|
728
|
-
"size": round(size, 1),
|
|
729
|
-
"font": {"size": font_size, "color": "#ffffff"},
|
|
730
|
-
"title": _html.escape(label),
|
|
731
|
-
"community": cid,
|
|
732
|
-
"community_name": sanitize_label((community_labels or {}).get(cid, f"Community {cid}")),
|
|
733
|
-
"source_file": sanitize_label(str(data.get("source_file") or "")),
|
|
734
|
-
"file_type": data.get("file_type", ""),
|
|
735
|
-
"degree": deg,
|
|
736
|
-
})
|
|
737
|
-
|
|
738
|
-
# Build edges list. Restore original edge direction from _src/_tgt
|
|
739
|
-
# (stashed by build.py for exactly this reason): undirected NetworkX
|
|
740
|
-
# canonicalizes endpoint order, which would otherwise flip the arrow
|
|
741
|
-
# for `calls` and `rationale_for` in the rendered graph (#563).
|
|
742
|
-
vis_edges = []
|
|
743
|
-
for u, v, data in G.edges(data=True):
|
|
744
|
-
confidence = data.get("confidence", "EXTRACTED")
|
|
745
|
-
relation = data.get("relation", "")
|
|
746
|
-
true_src = data.get("_src", u)
|
|
747
|
-
true_tgt = data.get("_tgt", v)
|
|
748
|
-
vis_edges.append({
|
|
749
|
-
"from": true_src,
|
|
750
|
-
"to": true_tgt,
|
|
751
|
-
"label": relation,
|
|
752
|
-
"title": _html.escape(f"{relation} [{confidence}]"),
|
|
753
|
-
"dashes": confidence != "EXTRACTED",
|
|
754
|
-
"width": 2 if confidence == "EXTRACTED" else 1,
|
|
755
|
-
"color": {"opacity": 0.7 if confidence == "EXTRACTED" else 0.35},
|
|
756
|
-
"confidence": confidence,
|
|
757
|
-
})
|
|
758
|
-
|
|
759
|
-
# Build community legend data
|
|
760
|
-
legend_data = []
|
|
761
|
-
for cid in sorted((community_labels or {}).keys()):
|
|
762
|
-
color = COMMUNITY_COLORS[cid % len(COMMUNITY_COLORS)]
|
|
763
|
-
lbl = _html.escape(sanitize_label((community_labels or {}).get(cid, f"Community {cid}")))
|
|
764
|
-
n = member_counts.get(cid, len(communities.get(cid, []))) if member_counts else len(communities.get(cid, []))
|
|
765
|
-
legend_data.append({"cid": cid, "color": color, "label": lbl, "count": n})
|
|
766
|
-
|
|
767
|
-
# Escape </script> sequences so embedded JSON cannot break out of the script tag
|
|
768
|
-
def _js_safe(obj) -> str:
|
|
769
|
-
return json.dumps(obj).replace("</", "<\\/")
|
|
770
|
-
|
|
771
|
-
nodes_json = _js_safe(vis_nodes)
|
|
772
|
-
edges_json = _js_safe(vis_edges)
|
|
773
|
-
legend_json = _js_safe(legend_data)
|
|
774
|
-
hyperedges_json = _js_safe(getattr(G, "graph", {}).get("hyperedges", []))
|
|
775
|
-
title = _html.escape(sanitize_label(str(output_path)))
|
|
776
|
-
stats = f"{G.number_of_nodes()} nodes · {G.number_of_edges()} edges · {len(communities)} communities"
|
|
777
|
-
|
|
778
|
-
html = f"""<!DOCTYPE html>
|
|
779
|
-
<html lang="en">
|
|
780
|
-
<head>
|
|
781
|
-
<meta charset="UTF-8">
|
|
782
|
-
<title>graphify - {title}</title>
|
|
783
|
-
<script src="https://unpkg.com/vis-network@9.1.6/standalone/umd/vis-network.min.js"
|
|
784
|
-
integrity="sha384-Ux6phic9PEHJ38YtrijhkzyJ8yQlH8i/+buBR8s3mAZOJrP1gwyvAcIYl3GWtpX1"
|
|
785
|
-
crossorigin="anonymous"></script>
|
|
786
|
-
{_html_styles()}
|
|
787
|
-
</head>
|
|
788
|
-
<body>
|
|
789
|
-
<div id="graph"></div>
|
|
790
|
-
<div id="sidebar">
|
|
791
|
-
<div id="search-wrap">
|
|
792
|
-
<input id="search" type="text" placeholder="Search nodes..." autocomplete="off">
|
|
793
|
-
<div id="search-results"></div>
|
|
794
|
-
</div>
|
|
795
|
-
<div id="info-panel">
|
|
796
|
-
<h3>Node Info</h3>
|
|
797
|
-
<div id="info-content"><span class="empty">Click a node to inspect it</span></div>
|
|
798
|
-
</div>
|
|
799
|
-
<div id="legend-wrap">
|
|
800
|
-
<h3>Communities</h3>
|
|
801
|
-
<div id="legend-controls">
|
|
802
|
-
<label><input type="checkbox" id="select-all-cb" checked onchange="toggleAllCommunities(!this.checked)">Select All</label>
|
|
803
|
-
</div>
|
|
804
|
-
<div id="legend"></div>
|
|
805
|
-
</div>
|
|
806
|
-
<div id="stats">{stats}</div>
|
|
807
|
-
</div>
|
|
808
|
-
{_html_script(nodes_json, edges_json, legend_json)}
|
|
809
|
-
{_hyperedge_script(hyperedges_json)}
|
|
810
|
-
</body>
|
|
811
|
-
</html>"""
|
|
812
|
-
|
|
813
|
-
Path(output_path).write_text(html, encoding="utf-8") # nosec
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
# Keep backward-compatible alias - skill.md calls generate_html
|
|
817
|
-
generate_html = to_html
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
def _cap_filename(s: str, limit: int = 200) -> str:
|
|
821
|
-
"""Cap a filename stem to ``limit`` UTF-8 bytes so it stays under the 255-byte
|
|
822
|
-
filesystem limit even after the ``.md`` extension and dedup suffix are added
|
|
823
|
-
(#1094). The cap is on BYTES, not chars, because a label of multibyte
|
|
824
|
-
characters (CJK, accented) can exceed 255 bytes well under 255 chars. When
|
|
825
|
-
truncation happens, an 8-char hash of the full label is appended so two
|
|
826
|
-
distinct labels sharing a long prefix produce distinct, deterministic
|
|
827
|
-
filenames instead of colliding."""
|
|
828
|
-
b = s.encode("utf-8")
|
|
829
|
-
if len(b) <= limit:
|
|
830
|
-
return s
|
|
831
|
-
digest = hashlib.sha1(s.encode("utf-8")).hexdigest()[:8] # nosec - not security
|
|
832
|
-
keep = limit - 9 # "_" + 8 hex chars
|
|
833
|
-
truncated = b[:keep].decode("utf-8", "ignore") # "ignore" drops a split trailing char
|
|
834
|
-
return f"{truncated}_{digest}"
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
def to_obsidian(
|
|
838
|
-
G: nx.Graph,
|
|
839
|
-
communities: dict[int, list[str]],
|
|
840
|
-
output_dir: str,
|
|
841
|
-
community_labels: dict[int, str] | None = None,
|
|
842
|
-
cohesion: dict[int, float] | None = None,
|
|
843
|
-
) -> int:
|
|
844
|
-
"""Export graph as an Obsidian vault - one .md file per node with [[wikilinks]],
|
|
845
|
-
plus one _COMMUNITY_name.md overview note per community (sorted to top by underscore prefix).
|
|
846
|
-
|
|
847
|
-
Open the output directory as a vault in Obsidian to get an interactive
|
|
848
|
-
graph view with community colors and full-text search over node metadata.
|
|
849
|
-
|
|
850
|
-
Returns the number of node notes + community notes written.
|
|
851
|
-
"""
|
|
852
|
-
out = Path(output_dir)
|
|
853
|
-
out.mkdir(parents=True, exist_ok=True)
|
|
854
|
-
|
|
855
|
-
node_community = _node_community_map(communities)
|
|
856
|
-
|
|
857
|
-
# Map node_id → safe filename so wikilinks stay consistent.
|
|
858
|
-
# Deduplicate: if two nodes produce the same filename, append a numeric suffix.
|
|
859
|
-
def safe_name(label: str) -> str:
|
|
860
|
-
cleaned = re.sub(r'[\\/*?:"<>|#^[\]]', "", label.replace("\r\n", " ").replace("\r", " ").replace("\n", " ")).strip()
|
|
861
|
-
# Strip trailing .md/.mdx/.markdown so "CLAUDE.md" doesn't become "CLAUDE.md.md"
|
|
862
|
-
cleaned = re.sub(r"\.(md|mdx|qmd|markdown)$", "", cleaned, flags=re.IGNORECASE)
|
|
863
|
-
return _cap_filename(cleaned) if cleaned else "unnamed"
|
|
864
|
-
|
|
865
|
-
node_filename: dict[str, str] = {}
|
|
866
|
-
seen_names: dict[str, int] = {}
|
|
867
|
-
for node_id, data in G.nodes(data=True):
|
|
868
|
-
base = safe_name(data.get("label", node_id))
|
|
869
|
-
if base in seen_names:
|
|
870
|
-
seen_names[base] += 1
|
|
871
|
-
node_filename[node_id] = f"{base}_{seen_names[base]}"
|
|
872
|
-
else:
|
|
873
|
-
seen_names[base] = 0
|
|
874
|
-
node_filename[node_id] = base
|
|
875
|
-
|
|
876
|
-
# Helper: compute dominant confidence for a node across all its edges
|
|
877
|
-
def _dominant_confidence(node_id: str) -> str:
|
|
878
|
-
confs = []
|
|
879
|
-
for u, v, edata in G.edges(node_id, data=True):
|
|
880
|
-
confs.append(edata.get("confidence", "EXTRACTED"))
|
|
881
|
-
if not confs:
|
|
882
|
-
return "EXTRACTED"
|
|
883
|
-
return Counter(confs).most_common(1)[0][0]
|
|
884
|
-
|
|
885
|
-
# Map file_type → graphify tag
|
|
886
|
-
_FTYPE_TAG = {
|
|
887
|
-
"code": "graphify/code",
|
|
888
|
-
"document": "graphify/document",
|
|
889
|
-
"paper": "graphify/paper",
|
|
890
|
-
"image": "graphify/image",
|
|
891
|
-
}
|
|
892
|
-
|
|
893
|
-
# Write one .md file per node
|
|
894
|
-
for node_id, data in G.nodes(data=True):
|
|
895
|
-
label = data.get("label", node_id)
|
|
896
|
-
cid = node_community.get(node_id)
|
|
897
|
-
community_name = (
|
|
898
|
-
community_labels.get(cid, f"Community {cid}")
|
|
899
|
-
if community_labels and cid is not None
|
|
900
|
-
else f"Community {cid}"
|
|
901
|
-
)
|
|
902
|
-
|
|
903
|
-
# Build tags for this node
|
|
904
|
-
ftype = data.get("file_type", "")
|
|
905
|
-
ftype_tag = _FTYPE_TAG.get(ftype, f"graphify/{ftype}" if ftype else "graphify/document")
|
|
906
|
-
dom_conf = _dominant_confidence(node_id)
|
|
907
|
-
conf_tag = f"graphify/{dom_conf}"
|
|
908
|
-
comm_tag = f"community/{_obsidian_tag(community_name)}"
|
|
909
|
-
node_tags = [ftype_tag, conf_tag, comm_tag]
|
|
910
|
-
|
|
911
|
-
lines: list[str] = []
|
|
912
|
-
|
|
913
|
-
# YAML frontmatter - readable in Obsidian's properties panel.
|
|
914
|
-
# All scalars pass through _yaml_str so a hostile source_file or
|
|
915
|
-
# community label cannot break out and inject sibling keys (F-009).
|
|
916
|
-
lines += [
|
|
917
|
-
"---",
|
|
918
|
-
f'source_file: "{_yaml_str(data.get("source_file", ""))}"',
|
|
919
|
-
f'type: "{_yaml_str(ftype)}"',
|
|
920
|
-
f'community: "{_yaml_str(community_name)}"',
|
|
921
|
-
]
|
|
922
|
-
if data.get("source_location"):
|
|
923
|
-
lines.append(f'location: "{_yaml_str(str(data["source_location"]))}"')
|
|
924
|
-
# Add tags list to frontmatter
|
|
925
|
-
lines.append("tags:")
|
|
926
|
-
for tag in node_tags:
|
|
927
|
-
lines.append(f" - {tag}")
|
|
928
|
-
lines += ["---", "", f"# {label}", ""]
|
|
929
|
-
|
|
930
|
-
# Outgoing edges as wikilinks
|
|
931
|
-
neighbors = list(G.neighbors(node_id))
|
|
932
|
-
if neighbors:
|
|
933
|
-
lines.append("## Connections")
|
|
934
|
-
for neighbor in sorted(neighbors, key=lambda n: G.nodes[n].get("label", n)):
|
|
935
|
-
edata = edge_data(G, node_id, neighbor)
|
|
936
|
-
neighbor_label = node_filename[neighbor]
|
|
937
|
-
relation = edata.get("relation", "")
|
|
938
|
-
confidence = edata.get("confidence", "EXTRACTED")
|
|
939
|
-
lines.append(f"- [[{neighbor_label}]] - `{relation}` [{confidence}]")
|
|
940
|
-
lines.append("")
|
|
941
|
-
|
|
942
|
-
# Inline tags at bottom of note body (for Obsidian tag panel)
|
|
943
|
-
inline_tags = " ".join(f"#{t}" for t in node_tags)
|
|
944
|
-
lines.append(inline_tags)
|
|
945
|
-
|
|
946
|
-
fname = node_filename[node_id] + ".md"
|
|
947
|
-
(out / fname).write_text("\n".join(lines), encoding="utf-8") # nosec
|
|
948
|
-
|
|
949
|
-
# Write one _COMMUNITY_name.md overview note per community
|
|
950
|
-
# Build inter-community edge counts for "Connections to other communities"
|
|
951
|
-
inter_community_edges: dict[int, dict[int, int]] = {}
|
|
952
|
-
for cid in communities:
|
|
953
|
-
inter_community_edges[cid] = {}
|
|
954
|
-
for u, v in G.edges():
|
|
955
|
-
cu = node_community.get(u)
|
|
956
|
-
cv = node_community.get(v)
|
|
957
|
-
if cu is not None and cv is not None and cu != cv:
|
|
958
|
-
inter_community_edges.setdefault(cu, {})
|
|
959
|
-
inter_community_edges.setdefault(cv, {})
|
|
960
|
-
inter_community_edges[cu][cv] = inter_community_edges[cu].get(cv, 0) + 1
|
|
961
|
-
inter_community_edges[cv][cu] = inter_community_edges[cv].get(cu, 0) + 1
|
|
962
|
-
|
|
963
|
-
# Precompute per-node community reach (number of distinct communities a node connects to)
|
|
964
|
-
def _community_reach(node_id: str) -> int:
|
|
965
|
-
neighbor_cids = {
|
|
966
|
-
node_community[nb]
|
|
967
|
-
for nb in G.neighbors(node_id)
|
|
968
|
-
if nb in node_community and node_community[nb] != node_community.get(node_id)
|
|
969
|
-
}
|
|
970
|
-
return len(neighbor_cids)
|
|
971
|
-
|
|
972
|
-
community_notes_written = 0
|
|
973
|
-
for cid, members in communities.items():
|
|
974
|
-
community_name = (
|
|
975
|
-
community_labels.get(cid, f"Community {cid}")
|
|
976
|
-
if community_labels and cid is not None
|
|
977
|
-
else f"Community {cid}"
|
|
978
|
-
)
|
|
979
|
-
n_members = len(members)
|
|
980
|
-
coh_value = cohesion.get(cid) if cohesion else None
|
|
981
|
-
|
|
982
|
-
lines: list[str] = []
|
|
983
|
-
|
|
984
|
-
# YAML frontmatter
|
|
985
|
-
lines.append("---")
|
|
986
|
-
lines.append("type: community")
|
|
987
|
-
if coh_value is not None:
|
|
988
|
-
lines.append(f"cohesion: {coh_value:.2f}")
|
|
989
|
-
lines.append(f"members: {n_members}")
|
|
990
|
-
lines.append("---")
|
|
991
|
-
lines.append("")
|
|
992
|
-
lines.append(f"# {community_name}")
|
|
993
|
-
lines.append("")
|
|
994
|
-
|
|
995
|
-
# Cohesion + member count summary
|
|
996
|
-
if coh_value is not None:
|
|
997
|
-
cohesion_desc = (
|
|
998
|
-
"tightly connected" if coh_value >= 0.7
|
|
999
|
-
else "moderately connected" if coh_value >= 0.4
|
|
1000
|
-
else "loosely connected"
|
|
1001
|
-
)
|
|
1002
|
-
lines.append(f"**Cohesion:** {coh_value:.2f} - {cohesion_desc}")
|
|
1003
|
-
lines.append(f"**Members:** {n_members} nodes")
|
|
1004
|
-
lines.append("")
|
|
1005
|
-
|
|
1006
|
-
# Members section
|
|
1007
|
-
lines.append("## Members")
|
|
1008
|
-
for node_id in sorted(members, key=lambda n: G.nodes[n].get("label", n)):
|
|
1009
|
-
data = G.nodes[node_id]
|
|
1010
|
-
node_label = node_filename[node_id]
|
|
1011
|
-
ftype = data.get("file_type", "")
|
|
1012
|
-
source = data.get("source_file", "")
|
|
1013
|
-
entry = f"- [[{node_label}]]"
|
|
1014
|
-
if ftype:
|
|
1015
|
-
entry += f" - {ftype}"
|
|
1016
|
-
if source:
|
|
1017
|
-
entry += f" - {source}"
|
|
1018
|
-
lines.append(entry)
|
|
1019
|
-
lines.append("")
|
|
1020
|
-
|
|
1021
|
-
# Dataview live query (improvement 2)
|
|
1022
|
-
comm_tag_name = _obsidian_tag(community_name)
|
|
1023
|
-
lines.append("## Live Query (requires Dataview plugin)")
|
|
1024
|
-
lines.append("")
|
|
1025
|
-
lines.append("```dataview")
|
|
1026
|
-
lines.append(f"TABLE source_file, type FROM #community/{comm_tag_name}")
|
|
1027
|
-
lines.append("SORT file.name ASC")
|
|
1028
|
-
lines.append("```")
|
|
1029
|
-
lines.append("")
|
|
1030
|
-
|
|
1031
|
-
# Connections to other communities
|
|
1032
|
-
cross = inter_community_edges.get(cid, {})
|
|
1033
|
-
if cross:
|
|
1034
|
-
lines.append("## Connections to other communities")
|
|
1035
|
-
for other_cid, edge_count in sorted(cross.items(), key=lambda x: -x[1]):
|
|
1036
|
-
other_name = (
|
|
1037
|
-
community_labels.get(other_cid, f"Community {other_cid}")
|
|
1038
|
-
if community_labels and other_cid is not None
|
|
1039
|
-
else f"Community {other_cid}"
|
|
1040
|
-
)
|
|
1041
|
-
other_safe = safe_name(other_name)
|
|
1042
|
-
lines.append(f"- {edge_count} edge{'s' if edge_count != 1 else ''} to [[_COMMUNITY_{other_safe}]]")
|
|
1043
|
-
lines.append("")
|
|
1044
|
-
|
|
1045
|
-
# Top bridge nodes - highest degree nodes that connect to other communities
|
|
1046
|
-
bridge_nodes = [
|
|
1047
|
-
(node_id, G.degree(node_id), _community_reach(node_id))
|
|
1048
|
-
for node_id in members
|
|
1049
|
-
if _community_reach(node_id) > 0
|
|
1050
|
-
]
|
|
1051
|
-
bridge_nodes.sort(key=lambda x: (-x[2], -x[1]))
|
|
1052
|
-
top_bridges = bridge_nodes[:5]
|
|
1053
|
-
if top_bridges:
|
|
1054
|
-
lines.append("## Top bridge nodes")
|
|
1055
|
-
for node_id, degree, reach in top_bridges:
|
|
1056
|
-
node_label = node_filename[node_id]
|
|
1057
|
-
lines.append(
|
|
1058
|
-
f"- [[{node_label}]] - degree {degree}, connects to {reach} "
|
|
1059
|
-
f"{'community' if reach == 1 else 'communities'}"
|
|
1060
|
-
)
|
|
1061
|
-
|
|
1062
|
-
community_safe = safe_name(community_name)
|
|
1063
|
-
fname = f"_COMMUNITY_{community_safe}.md"
|
|
1064
|
-
(out / fname).write_text("\n".join(lines), encoding="utf-8") # nosec
|
|
1065
|
-
community_notes_written += 1
|
|
1066
|
-
|
|
1067
|
-
# Improvement 4: write .obsidian/graph.json to color nodes by community in graph view
|
|
1068
|
-
obsidian_dir = out / ".obsidian"
|
|
1069
|
-
obsidian_dir.mkdir(exist_ok=True)
|
|
1070
|
-
graph_config = {
|
|
1071
|
-
"colorGroups": [
|
|
1072
|
-
{
|
|
1073
|
-
"query": f"tag:#community/{label.replace(' ', '_')}",
|
|
1074
|
-
"color": {"a": 1, "rgb": int(COMMUNITY_COLORS[cid % len(COMMUNITY_COLORS)].lstrip('#'), 16)}
|
|
1075
|
-
}
|
|
1076
|
-
for cid, label in sorted((community_labels or {}).items())
|
|
1077
|
-
]
|
|
1078
|
-
}
|
|
1079
|
-
(obsidian_dir / "graph.json").write_text(json.dumps(graph_config, indent=2), encoding="utf-8") # nosec
|
|
1080
|
-
|
|
1081
|
-
return G.number_of_nodes() + community_notes_written
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
def to_canvas(
|
|
1085
|
-
G: nx.Graph,
|
|
1086
|
-
communities: dict[int, list[str]],
|
|
1087
|
-
output_path: str,
|
|
1088
|
-
community_labels: dict[int, str] | None = None,
|
|
1089
|
-
node_filenames: dict[str, str] | None = None,
|
|
1090
|
-
) -> None:
|
|
1091
|
-
"""Export graph as an Obsidian Canvas file - communities as groups, nodes as cards.
|
|
1092
|
-
|
|
1093
|
-
Generates a structured layout: communities arranged in a grid, nodes within
|
|
1094
|
-
each community arranged in rows. Edges shown between connected nodes.
|
|
1095
|
-
Opens in Obsidian as an infinite canvas with community groupings visible.
|
|
1096
|
-
"""
|
|
1097
|
-
# Obsidian canvas color codes (cycle through for communities)
|
|
1098
|
-
CANVAS_COLORS = ["1", "2", "3", "4", "5", "6"] # red, orange, yellow, green, cyan, purple
|
|
1099
|
-
|
|
1100
|
-
def safe_name(label: str) -> str:
|
|
1101
|
-
cleaned = re.sub(r'[\\/*?:"<>|#^[\]]', "", label.replace("\r\n", " ").replace("\r", " ").replace("\n", " ")).strip()
|
|
1102
|
-
cleaned = re.sub(r"\.(md|mdx|qmd|markdown)$", "", cleaned, flags=re.IGNORECASE)
|
|
1103
|
-
return _cap_filename(cleaned) if cleaned else "unnamed"
|
|
1104
|
-
|
|
1105
|
-
# Build node_filenames if not provided (same dedup logic as to_obsidian)
|
|
1106
|
-
if node_filenames is None:
|
|
1107
|
-
node_filenames = {}
|
|
1108
|
-
seen_names: dict[str, int] = {}
|
|
1109
|
-
for node_id, data in G.nodes(data=True):
|
|
1110
|
-
base = safe_name(data.get("label", node_id))
|
|
1111
|
-
if base in seen_names:
|
|
1112
|
-
seen_names[base] += 1
|
|
1113
|
-
node_filenames[node_id] = f"{base}_{seen_names[base]}"
|
|
1114
|
-
else:
|
|
1115
|
-
seen_names[base] = 0
|
|
1116
|
-
node_filenames[node_id] = base
|
|
1117
|
-
|
|
1118
|
-
num_communities = len(communities)
|
|
1119
|
-
cols = math.ceil(math.sqrt(num_communities)) if num_communities > 0 else 1
|
|
1120
|
-
rows = math.ceil(num_communities / cols) if num_communities > 0 else 1
|
|
1121
|
-
|
|
1122
|
-
canvas_nodes: list[dict] = []
|
|
1123
|
-
canvas_edges: list[dict] = []
|
|
1124
|
-
|
|
1125
|
-
# Lay out communities in a grid
|
|
1126
|
-
gap = 80
|
|
1127
|
-
group_x_offsets: list[int] = []
|
|
1128
|
-
group_y_offsets: list[int] = []
|
|
1129
|
-
|
|
1130
|
-
# Precompute group sizes so we can calculate offsets
|
|
1131
|
-
sorted_cids = sorted(communities.keys())
|
|
1132
|
-
group_sizes: dict[int, tuple[int, int]] = {}
|
|
1133
|
-
for cid in sorted_cids:
|
|
1134
|
-
members = communities[cid]
|
|
1135
|
-
n = len(members)
|
|
1136
|
-
w = max(600, 220 * math.ceil(math.sqrt(n)) if n > 0 else 600)
|
|
1137
|
-
h = max(400, 100 * math.ceil(n / 3) + 120 if n > 0 else 400)
|
|
1138
|
-
group_sizes[cid] = (w, h)
|
|
1139
|
-
|
|
1140
|
-
# Compute cumulative row heights and col widths for grid placement
|
|
1141
|
-
# Each grid cell uses the max width/height in its col/row
|
|
1142
|
-
col_widths: list[int] = []
|
|
1143
|
-
row_heights: list[int] = []
|
|
1144
|
-
for col_idx in range(cols):
|
|
1145
|
-
max_w = 0
|
|
1146
|
-
for row_idx in range(rows):
|
|
1147
|
-
linear = row_idx * cols + col_idx
|
|
1148
|
-
if linear < len(sorted_cids):
|
|
1149
|
-
cid = sorted_cids[linear]
|
|
1150
|
-
w, _ = group_sizes[cid]
|
|
1151
|
-
max_w = max(max_w, w)
|
|
1152
|
-
col_widths.append(max_w)
|
|
1153
|
-
|
|
1154
|
-
for row_idx in range(rows):
|
|
1155
|
-
max_h = 0
|
|
1156
|
-
for col_idx in range(cols):
|
|
1157
|
-
linear = row_idx * cols + col_idx
|
|
1158
|
-
if linear < len(sorted_cids):
|
|
1159
|
-
cid = sorted_cids[linear]
|
|
1160
|
-
_, h = group_sizes[cid]
|
|
1161
|
-
max_h = max(max_h, h)
|
|
1162
|
-
row_heights.append(max_h)
|
|
1163
|
-
|
|
1164
|
-
# Map from cid → (group_x, group_y, group_w, group_h)
|
|
1165
|
-
group_layout: dict[int, tuple[int, int, int, int]] = {}
|
|
1166
|
-
for idx, cid in enumerate(sorted_cids):
|
|
1167
|
-
col_idx = idx % cols
|
|
1168
|
-
row_idx = idx // cols
|
|
1169
|
-
gx = sum(col_widths[:col_idx]) + col_idx * gap
|
|
1170
|
-
gy = sum(row_heights[:row_idx]) + row_idx * gap
|
|
1171
|
-
gw, gh = group_sizes[cid]
|
|
1172
|
-
group_layout[cid] = (gx, gy, gw, gh)
|
|
1173
|
-
|
|
1174
|
-
# Build set of all node_ids in canvas for edge filtering
|
|
1175
|
-
all_canvas_nodes: set[str] = set()
|
|
1176
|
-
for members in communities.values():
|
|
1177
|
-
all_canvas_nodes.update(members)
|
|
1178
|
-
|
|
1179
|
-
# Generate group and node canvas entries
|
|
1180
|
-
for idx, cid in enumerate(sorted_cids):
|
|
1181
|
-
members = communities[cid]
|
|
1182
|
-
community_name = (
|
|
1183
|
-
community_labels.get(cid, f"Community {cid}")
|
|
1184
|
-
if community_labels and cid is not None
|
|
1185
|
-
else f"Community {cid}"
|
|
1186
|
-
)
|
|
1187
|
-
gx, gy, gw, gh = group_layout[cid]
|
|
1188
|
-
canvas_color = CANVAS_COLORS[idx % len(CANVAS_COLORS)]
|
|
1189
|
-
|
|
1190
|
-
# Group node
|
|
1191
|
-
canvas_nodes.append({
|
|
1192
|
-
"id": f"g{cid}",
|
|
1193
|
-
"type": "group",
|
|
1194
|
-
"label": community_name,
|
|
1195
|
-
"x": gx,
|
|
1196
|
-
"y": gy,
|
|
1197
|
-
"width": gw,
|
|
1198
|
-
"height": gh,
|
|
1199
|
-
"color": canvas_color,
|
|
1200
|
-
})
|
|
1201
|
-
|
|
1202
|
-
# Node cards inside the group - rows of 3
|
|
1203
|
-
sorted_members = sorted(members, key=lambda n: G.nodes[n].get("label", n))
|
|
1204
|
-
for m_idx, node_id in enumerate(sorted_members):
|
|
1205
|
-
col = m_idx % 3
|
|
1206
|
-
row = m_idx // 3
|
|
1207
|
-
nx_x = gx + 20 + col * (180 + 20)
|
|
1208
|
-
nx_y = gy + 80 + row * (60 + 20)
|
|
1209
|
-
fname = node_filenames.get(node_id, safe_name(G.nodes[node_id].get("label", node_id)))
|
|
1210
|
-
canvas_nodes.append({
|
|
1211
|
-
"id": f"n_{node_id}",
|
|
1212
|
-
"type": "file",
|
|
1213
|
-
"file": f"{fname}.md",
|
|
1214
|
-
"x": nx_x,
|
|
1215
|
-
"y": nx_y,
|
|
1216
|
-
"width": 180,
|
|
1217
|
-
"height": 60,
|
|
1218
|
-
})
|
|
1219
|
-
|
|
1220
|
-
# Generate edges - only between nodes both in canvas, cap at 200 highest-weight
|
|
1221
|
-
all_edges_weighted: list[tuple[float, str, str, str]] = []
|
|
1222
|
-
for u, v, edata in G.edges(data=True):
|
|
1223
|
-
if u in all_canvas_nodes and v in all_canvas_nodes:
|
|
1224
|
-
weight = edata.get("weight", 1.0)
|
|
1225
|
-
relation = edata.get("relation", "")
|
|
1226
|
-
conf = edata.get("confidence", "EXTRACTED")
|
|
1227
|
-
label = f"{relation} [{conf}]" if relation else f"[{conf}]"
|
|
1228
|
-
all_edges_weighted.append((weight, u, v, label))
|
|
1229
|
-
|
|
1230
|
-
all_edges_weighted.sort(key=lambda x: -x[0])
|
|
1231
|
-
for weight, u, v, label in all_edges_weighted[:200]:
|
|
1232
|
-
canvas_edges.append({
|
|
1233
|
-
"id": f"e_{u}_{v}",
|
|
1234
|
-
"fromNode": f"n_{u}",
|
|
1235
|
-
"toNode": f"n_{v}",
|
|
1236
|
-
"label": label,
|
|
1237
|
-
})
|
|
1238
|
-
|
|
1239
|
-
canvas_data = {"nodes": canvas_nodes, "edges": canvas_edges}
|
|
1240
|
-
Path(output_path).write_text(json.dumps(canvas_data, indent=2), encoding="utf-8") # nosec
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
def push_to_neo4j(
|
|
1244
|
-
G: nx.Graph,
|
|
1245
|
-
uri: str,
|
|
1246
|
-
user: str,
|
|
1247
|
-
password: str,
|
|
1248
|
-
communities: dict[int, list[str]] | None = None,
|
|
1249
|
-
) -> dict[str, int]:
|
|
1250
|
-
"""Push graph directly to a running Neo4j instance via the Python driver.
|
|
1251
|
-
|
|
1252
|
-
Requires: pip install neo4j
|
|
1253
|
-
|
|
1254
|
-
Uses MERGE so re-running is safe - nodes and edges are upserted, not duplicated.
|
|
1255
|
-
Returns a dict with counts of nodes and edges pushed.
|
|
1256
|
-
"""
|
|
1257
|
-
try:
|
|
1258
|
-
from neo4j import GraphDatabase
|
|
1259
|
-
except ImportError as e:
|
|
1260
|
-
raise ImportError(
|
|
1261
|
-
"neo4j driver not installed. Run: pip install neo4j"
|
|
1262
|
-
) from e
|
|
1263
|
-
|
|
1264
|
-
node_community = _node_community_map(communities) if communities else {}
|
|
1265
|
-
|
|
1266
|
-
def _safe_rel(relation: str) -> str:
|
|
1267
|
-
return re.sub(r"[^A-Z0-9_]", "_", relation.upper().replace(" ", "_").replace("-", "_")) or "RELATED_TO"
|
|
1268
|
-
|
|
1269
|
-
def _safe_label(label: str) -> str:
|
|
1270
|
-
"""Sanitize a Neo4j node label to prevent Cypher injection."""
|
|
1271
|
-
sanitized = re.sub(r"[^A-Za-z0-9_]", "", label)
|
|
1272
|
-
return sanitized if sanitized else "Entity"
|
|
1273
|
-
|
|
1274
|
-
driver = GraphDatabase.driver(uri, auth=(user, password))
|
|
1275
|
-
nodes_pushed = 0
|
|
1276
|
-
edges_pushed = 0
|
|
1277
|
-
|
|
1278
|
-
with driver.session() as session:
|
|
1279
|
-
for node_id, data in G.nodes(data=True):
|
|
1280
|
-
props = {
|
|
1281
|
-
k: v for k, v in data.items()
|
|
1282
|
-
if isinstance(v, (str, int, float, bool)) and not k.startswith("_")
|
|
1283
|
-
}
|
|
1284
|
-
props["id"] = node_id
|
|
1285
|
-
cid = node_community.get(node_id)
|
|
1286
|
-
if cid is not None:
|
|
1287
|
-
props["community"] = cid
|
|
1288
|
-
ftype = _safe_label(data.get("file_type", "Entity").capitalize())
|
|
1289
|
-
session.run(
|
|
1290
|
-
f"MERGE (n:{ftype} {{id: $id}}) SET n += $props",
|
|
1291
|
-
id=node_id,
|
|
1292
|
-
props=props,
|
|
1293
|
-
)
|
|
1294
|
-
nodes_pushed += 1
|
|
1295
|
-
|
|
1296
|
-
for u, v, data in G.edges(data=True):
|
|
1297
|
-
rel = _safe_rel(data.get("relation", "RELATED_TO"))
|
|
1298
|
-
props = {
|
|
1299
|
-
k: v for k, v in data.items()
|
|
1300
|
-
if isinstance(v, (str, int, float, bool)) and not k.startswith("_")
|
|
1301
|
-
}
|
|
1302
|
-
session.run(
|
|
1303
|
-
f"MATCH (a {{id: $src}}), (b {{id: $tgt}}) "
|
|
1304
|
-
f"MERGE (a)-[r:{rel}]->(b) SET r += $props",
|
|
1305
|
-
src=u,
|
|
1306
|
-
tgt=v,
|
|
1307
|
-
props=props,
|
|
1308
|
-
)
|
|
1309
|
-
edges_pushed += 1
|
|
1310
|
-
|
|
1311
|
-
driver.close()
|
|
1312
|
-
return {"nodes": nodes_pushed, "edges": edges_pushed}
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
def to_graphml(
|
|
1316
|
-
G: nx.Graph,
|
|
1317
|
-
communities: dict[int, list[str]],
|
|
1318
|
-
output_path: str,
|
|
1319
|
-
) -> None:
|
|
1320
|
-
"""Export graph as GraphML - opens in Gephi, yEd, and any GraphML-compatible tool.
|
|
1321
|
-
|
|
1322
|
-
Community IDs are written as a node attribute so Gephi can colour by community.
|
|
1323
|
-
Edge confidence (EXTRACTED/INFERRED/AMBIGUOUS) is preserved as an edge attribute.
|
|
1324
|
-
"""
|
|
1325
|
-
H = G.copy()
|
|
1326
|
-
node_community = _node_community_map(communities)
|
|
1327
|
-
for node_id in H.nodes():
|
|
1328
|
-
H.nodes[node_id]["community"] = node_community.get(node_id, -1)
|
|
1329
|
-
# Drop internal markers (e.g. the AST-provenance "_origin" tag, #1116, and
|
|
1330
|
-
# the "_src"/"_tgt" direction markers) — they are persistence/runtime details,
|
|
1331
|
-
# not graph data, and should not leak into the exported file.
|
|
1332
|
-
for _, attrs in H.nodes(data=True):
|
|
1333
|
-
for k in [k for k in attrs if k.startswith("_")]:
|
|
1334
|
-
del attrs[k]
|
|
1335
|
-
for _, _, attrs in H.edges(data=True):
|
|
1336
|
-
for k in [k for k in attrs if k.startswith("_")]:
|
|
1337
|
-
del attrs[k]
|
|
1338
|
-
nx.write_graphml(H, output_path)
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
def to_svg(
|
|
1342
|
-
G: nx.Graph,
|
|
1343
|
-
communities: dict[int, list[str]],
|
|
1344
|
-
output_path: str,
|
|
1345
|
-
community_labels: dict[int, str] | None = None,
|
|
1346
|
-
figsize: tuple[int, int] = (20, 14),
|
|
1347
|
-
) -> None:
|
|
1348
|
-
"""Export graph as an SVG file using matplotlib + spring layout.
|
|
1349
|
-
|
|
1350
|
-
Lightweight and embeddable - works in Obsidian notes, Notion, GitHub READMEs,
|
|
1351
|
-
and any markdown renderer. No JavaScript required.
|
|
1352
|
-
|
|
1353
|
-
Node size scales with degree. Community colors match the HTML output.
|
|
1354
|
-
"""
|
|
1355
|
-
try:
|
|
1356
|
-
import matplotlib
|
|
1357
|
-
matplotlib.use("Agg")
|
|
1358
|
-
import matplotlib.pyplot as plt
|
|
1359
|
-
import matplotlib.patches as mpatches
|
|
1360
|
-
except ImportError as e:
|
|
1361
|
-
raise ImportError("matplotlib not installed. Run: pip install matplotlib") from e
|
|
1362
|
-
|
|
1363
|
-
node_community = _node_community_map(communities)
|
|
1364
|
-
|
|
1365
|
-
fig, ax = plt.subplots(figsize=figsize, facecolor="#1a1a2e")
|
|
1366
|
-
ax.set_facecolor("#1a1a2e")
|
|
1367
|
-
ax.axis("off")
|
|
1368
|
-
|
|
1369
|
-
pos = nx.spring_layout(G, seed=42, k=2.0 / (G.number_of_nodes() ** 0.5 + 1))
|
|
1370
|
-
|
|
1371
|
-
degree = dict(G.degree())
|
|
1372
|
-
max_deg = max(degree.values(), default=1) or 1
|
|
1373
|
-
|
|
1374
|
-
node_colors = [COMMUNITY_COLORS[node_community.get(n, 0) % len(COMMUNITY_COLORS)] for n in G.nodes()]
|
|
1375
|
-
node_sizes = [300 + 1200 * (degree.get(n, 1) / max_deg) for n in G.nodes()]
|
|
1376
|
-
|
|
1377
|
-
# Draw edges - dashed for non-EXTRACTED
|
|
1378
|
-
for u, v, data in G.edges(data=True):
|
|
1379
|
-
conf = data.get("confidence", "EXTRACTED")
|
|
1380
|
-
style = "solid" if conf == "EXTRACTED" else "dashed"
|
|
1381
|
-
alpha = 0.6 if conf == "EXTRACTED" else 0.3
|
|
1382
|
-
x0, y0 = pos[u]
|
|
1383
|
-
x1, y1 = pos[v]
|
|
1384
|
-
ax.plot([x0, x1], [y0, y1], color="#aaaaaa", linewidth=0.8,
|
|
1385
|
-
linestyle=style, alpha=alpha, zorder=1)
|
|
1386
|
-
|
|
1387
|
-
nx.draw_networkx_nodes(G, pos, ax=ax, node_color=node_colors,
|
|
1388
|
-
node_size=node_sizes, alpha=0.9)
|
|
1389
|
-
nx.draw_networkx_labels(G, pos, ax=ax,
|
|
1390
|
-
labels={n: G.nodes[n].get("label", n) for n in G.nodes()},
|
|
1391
|
-
font_size=7, font_color="white")
|
|
1392
|
-
|
|
1393
|
-
# Legend
|
|
1394
|
-
if community_labels:
|
|
1395
|
-
patches = [
|
|
1396
|
-
mpatches.Patch(
|
|
1397
|
-
color=COMMUNITY_COLORS[cid % len(COMMUNITY_COLORS)],
|
|
1398
|
-
label=f"{label} ({len(communities.get(cid, []))})",
|
|
1399
|
-
)
|
|
1400
|
-
for cid, label in sorted(community_labels.items())
|
|
1401
|
-
]
|
|
1402
|
-
ax.legend(handles=patches, loc="upper left", framealpha=0.7,
|
|
1403
|
-
facecolor="#2a2a4e", labelcolor="white", fontsize=8)
|
|
1404
|
-
|
|
1405
|
-
plt.tight_layout()
|
|
1406
|
-
plt.savefig(output_path, format="svg", bbox_inches="tight",
|
|
1407
|
-
facecolor=fig.get_facecolor())
|
|
1408
|
-
plt.close(fig)
|