Files

40 lines
2.5 KiB
Python
Raw Normal View History

"""Make one copy of the detectors per rule of visibility.py removed (pruning), each next to a link to the tools repository.
Usage: python3 prune.py <detectors folder> <tools folder> <work folder> [variant ...] (no variant: all)
Prints the copies made; a replacement not found is an error (the rule's text changed).
"""
import shutil
import sys
from pathlib import Path
VARIANTS = { # name -> [(old, new)] in scripts/visibility.py
"factor_high": [("if x > med + K_MAD * mad and x > FACTOR * med:", "if x > med + K_MAD * mad:")],
"factor_strong": [('if m["error_threshold"] and x > m["error_threshold"] and x > FACTOR * med:',
'if m["error_threshold"] and x > m["error_threshold"]:')],
"lone": [(" elif not few:\n continue\n", " else:\n continue\n")],
"salience": [("if n(t) >= BRIEF_MIN and n(t) >= SALIENCE * max(map(n, around), default=0):", "if n(t) >= BRIEF_MIN:")],
"brief_min": [("if n(t) >= BRIEF_MIN and n(t) >= SALIENCE", "if n(t) and n(t) >= SALIENCE")],
"two_sensors": [("zones = [zone for zone in zones if len(zone[2]) >= 2]\n", "")],
"not_briefs": [("if s in high.get(t, ()) and t not in briefs]", "if s in high.get(t, ())]")],
"diffuse": [(" if len(r) >= RUN_MIN and not any(", " if False and not any(")],
"diffuse_run": [(" if len(r) >= RUN_MIN and not any(", " if not any(")],
"diffuse_hosts": [("if len(r) >= RUN_MIN and not any(h[\"start\"] <= e[\"start\"] and e[\"end\"] <= h[\"end\"] for h in hosts):",
"if len(r) >= RUN_MIN:")],
"shimmer_share": [("if sum(a >= RATIO for a, _ in r.values()) >= SHARE * len(r)\n and statistics", "if statistics")],
"run_min": [(" if len(r) >= RUN_MIN]\n", " ]\n")],
"shimmer_error": [(" and statistics.median(e for _, e in r.values()) >= RATIO]", " ]")],
}
det, tools, work = map(Path, sys.argv[1:4])
for name in sys.argv[4:] or VARIANTS:
dst = work / name
shutil.rmtree(dst, ignore_errors=True)
shutil.copytree(det, dst / "aimsight-detectors", ignore=shutil.ignore_patterns(".git", "__pycache__"))
(dst / "aimsight-tools").symlink_to(tools.resolve())
p = dst / "aimsight-detectors" / "scripts" / "visibility.py"
s = p.read_text()
for old, new in VARIANTS[name]:
if old not in s:
sys.exit(f"{name}: not found: {old!r}")
s = s.replace(old, new, 1)
p.write_text(s)
print(dst)