Datasets:
Delete name_n4_slices.py
Browse files- name_n4_slices.py +0 -267
name_n4_slices.py
DELETED
|
@@ -1,267 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
name_n4_slices.py
|
| 4 |
-
-----------------
|
| 5 |
-
Append a unique prospect name to each N4 slice while retaining the
|
| 6 |
-
canonical naming convention:
|
| 7 |
-
|
| 8 |
-
n4_slice_000.csv → n4_slice_000_<ProspectName>.csv
|
| 9 |
-
n4_slice_001.csv → n4_slice_001_<ProspectName>.csv
|
| 10 |
-
…
|
| 11 |
-
|
| 12 |
-
Prospect names are deterministic, unique across the 253 (or N) slices,
|
| 13 |
-
and drawn from a Tau / Tav / RGC–themed vocabulary so the labels stay
|
| 14 |
-
meaningful inside the project.
|
| 15 |
-
|
| 16 |
-
Modes
|
| 17 |
-
-----
|
| 18 |
-
rename – rename files in place (default)
|
| 19 |
-
copy – copy to new names, leave originals intact
|
| 20 |
-
dry-run – print the mapping only, touch nothing
|
| 21 |
-
|
| 22 |
-
Usage
|
| 23 |
-
-----
|
| 24 |
-
python name_n4_slices.py ./n4_slices
|
| 25 |
-
python name_n4_slices.py ./n4_slices --mode copy --outdir ./n4_named
|
| 26 |
-
python name_n4_slices.py ./n4_slices --dry-run
|
| 27 |
-
python name_n4_slices.py ./n4_slices --manifest ./n4_slices/slice_manifest.csv
|
| 28 |
-
"""
|
| 29 |
-
|
| 30 |
-
from __future__ import annotations
|
| 31 |
-
|
| 32 |
-
import argparse
|
| 33 |
-
import csv
|
| 34 |
-
import re
|
| 35 |
-
import shutil
|
| 36 |
-
import sys
|
| 37 |
-
from pathlib import Path
|
| 38 |
-
from typing import Dict, List, Optional, Tuple
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# ---------------------------------------------------------------------------
|
| 42 |
-
# Prospect-name generator (deterministic, unique)
|
| 43 |
-
# ---------------------------------------------------------------------------
|
| 44 |
-
|
| 45 |
-
# Themed stem pools (Tau / Tav / RGC / lattice vocabulary)
|
| 46 |
-
_STEMS = [
|
| 47 |
-
"Tau", "Tav", "Berard", "Lattice", "Loll", "Wavefront", "Resonance",
|
| 48 |
-
"Core", "Ring", "Flux", "Phase", "Orbit", "Tongue", "Staircase",
|
| 49 |
-
"Kuramoto", "Josephson", "Photonic", "SQUID", "Genome", "Adapter",
|
| 50 |
-
"Mythos", "Etymos", "BoxMatch", "Ella", "Lilly", "Sharkovskii",
|
| 51 |
-
"Period3", "Mod7", "Heartbeat", "142857", "Radion", "Compact",
|
| 52 |
-
"Hierarchy", "Supermode", "Circulant", "Arnold", "Devil", "Splay",
|
| 53 |
-
"Chimera", "OrderR", "Coherence", "Attractor", "Handshake", "Async",
|
| 54 |
-
"Q123", "Arty", "MetaRing", "Sevenfold", "S1", "Cylinder",
|
| 55 |
-
]
|
| 56 |
-
|
| 57 |
-
_MODIFIERS = [
|
| 58 |
-
"Alpha", "Beta", "Gamma", "Delta", "Epsilon", "Zeta", "Eta",
|
| 59 |
-
"Prime", "Prime2", "Prime3", "Node", "Edge", "Face", "Cell",
|
| 60 |
-
"North", "East", "South", "West", "Inner", "Outer", "Upper", "Lower",
|
| 61 |
-
"Cold", "Warm", "Bright", "Deep", "High", "Low", "Fast", "Slow",
|
| 62 |
-
"Lock", "Drift", "Hold", "Sweep", "Scan", "Probe", "Trace", "Mark",
|
| 63 |
-
"Seed", "Root", "Leaf", "Branch", "Spine", "Crown", "Base", "Apex",
|
| 64 |
-
]
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
def prospect_name(index: int, n_slices: int = 253) -> str:
|
| 68 |
-
"""
|
| 69 |
-
Return a unique, human-readable prospect name for slice `index`.
|
| 70 |
-
|
| 71 |
-
Construction: <Stem><Modifier> or <Stem><Modifier><suffix>
|
| 72 |
-
Guaranteed unique for index in 0 .. n_slices-1 (and well beyond).
|
| 73 |
-
"""
|
| 74 |
-
n_stems = len(_STEMS)
|
| 75 |
-
n_mods = len(_MODIFIERS)
|
| 76 |
-
period = n_stems * n_mods
|
| 77 |
-
|
| 78 |
-
stem = _STEMS[index % n_stems]
|
| 79 |
-
mod = _MODIFIERS[(index // n_stems) % n_mods]
|
| 80 |
-
cycle = index // period
|
| 81 |
-
|
| 82 |
-
if cycle == 0:
|
| 83 |
-
return f"{stem}{mod}"
|
| 84 |
-
return f"{stem}{mod}{cycle + 1}"
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
def build_mapping(n_slices: int) -> Dict[int, str]:
|
| 88 |
-
"""index → prospect name for 0 .. n_slices-1."""
|
| 89 |
-
names = [prospect_name(i, n_slices) for i in range(n_slices)]
|
| 90 |
-
if len(set(names)) != n_slices:
|
| 91 |
-
# Extremely unlikely with the pools above; hard-fail if it happens
|
| 92 |
-
raise RuntimeError("Prospect-name collision detected — enlarge pools.")
|
| 93 |
-
return {i: names[i] for i in range(n_slices)}
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
# ---------------------------------------------------------------------------
|
| 97 |
-
# Discovery of existing slices
|
| 98 |
-
# ---------------------------------------------------------------------------
|
| 99 |
-
|
| 100 |
-
_SLICE_RE = re.compile(r"^n4_slice_(\d{3})(?:_.*)?\.csv$", re.IGNORECASE)
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
def discover_slices(directory: Path) -> List[Tuple[int, Path]]:
|
| 104 |
-
"""
|
| 105 |
-
Find files matching n4_slice_XXX.csv (or already-named variants)
|
| 106 |
-
and return sorted (index, path) pairs. Prefers the plain
|
| 107 |
-
n4_slice_XXX.csv form when both exist.
|
| 108 |
-
"""
|
| 109 |
-
found: Dict[int, Path] = {}
|
| 110 |
-
for p in directory.iterdir():
|
| 111 |
-
if not p.is_file():
|
| 112 |
-
continue
|
| 113 |
-
m = _SLICE_RE.match(p.name)
|
| 114 |
-
if not m:
|
| 115 |
-
continue
|
| 116 |
-
idx = int(m.group(1))
|
| 117 |
-
# Prefer the un-suffixed name if we see both
|
| 118 |
-
if idx not in found or "_" not in p.stem[len("n4_slice_XXX"):]:
|
| 119 |
-
# Keep the shortest name (plain slice) if multiple match
|
| 120 |
-
if idx not in found or len(p.name) < len(found[idx].name):
|
| 121 |
-
found[idx] = p
|
| 122 |
-
return sorted(found.items())
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
# ---------------------------------------------------------------------------
|
| 126 |
-
# Core rename / copy logic
|
| 127 |
-
# ---------------------------------------------------------------------------
|
| 128 |
-
|
| 129 |
-
def apply_names(
|
| 130 |
-
slices: List[Tuple[int, Path]],
|
| 131 |
-
mapping: Dict[int, str],
|
| 132 |
-
mode: str,
|
| 133 |
-
outdir: Optional[Path],
|
| 134 |
-
dry_run: bool,
|
| 135 |
-
) -> List[dict]:
|
| 136 |
-
"""
|
| 137 |
-
Rename or copy each slice, appending _<ProspectName>.
|
| 138 |
-
Returns a list of result rows for the new manifest.
|
| 139 |
-
"""
|
| 140 |
-
results = []
|
| 141 |
-
for idx, src in slices:
|
| 142 |
-
name = mapping.get(idx)
|
| 143 |
-
if name is None:
|
| 144 |
-
print(f" WARNING: no prospect name for index {idx}, skipping")
|
| 145 |
-
continue
|
| 146 |
-
|
| 147 |
-
new_name = f"n4_slice_{idx:03d}_{name}.csv"
|
| 148 |
-
if mode == "copy" and outdir is not None:
|
| 149 |
-
dest = outdir / new_name
|
| 150 |
-
else:
|
| 151 |
-
dest = src.parent / new_name
|
| 152 |
-
|
| 153 |
-
action = "DRY" if dry_run else mode.upper()
|
| 154 |
-
print(f" [{idx:03d}] {src.name} → {new_name} ({action})")
|
| 155 |
-
|
| 156 |
-
if not dry_run:
|
| 157 |
-
if mode == "rename":
|
| 158 |
-
if dest.resolve() == src.resolve():
|
| 159 |
-
pass # already correctly named
|
| 160 |
-
else:
|
| 161 |
-
src.rename(dest)
|
| 162 |
-
elif mode == "copy":
|
| 163 |
-
if outdir is not None:
|
| 164 |
-
outdir.mkdir(parents=True, exist_ok=True)
|
| 165 |
-
shutil.copy2(src, dest)
|
| 166 |
-
else:
|
| 167 |
-
raise ValueError(f"Unknown mode: {mode}")
|
| 168 |
-
|
| 169 |
-
results.append(
|
| 170 |
-
{
|
| 171 |
-
"slice_idx": idx,
|
| 172 |
-
"prospect": name,
|
| 173 |
-
"original": src.name,
|
| 174 |
-
"named_file": new_name,
|
| 175 |
-
}
|
| 176 |
-
)
|
| 177 |
-
return results
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
# ---------------------------------------------------------------------------
|
| 181 |
-
# CLI
|
| 182 |
-
# ---------------------------------------------------------------------------
|
| 183 |
-
|
| 184 |
-
def main() -> None:
|
| 185 |
-
p = argparse.ArgumentParser(
|
| 186 |
-
description="Append unique prospect names to N4 slice files."
|
| 187 |
-
)
|
| 188 |
-
p.add_argument(
|
| 189 |
-
"slicedir",
|
| 190 |
-
type=Path,
|
| 191 |
-
help="Directory containing n4_slice_XXX.csv files",
|
| 192 |
-
)
|
| 193 |
-
p.add_argument(
|
| 194 |
-
"--mode",
|
| 195 |
-
choices=("rename", "copy"),
|
| 196 |
-
default="rename",
|
| 197 |
-
help="rename in place (default) or copy to --outdir",
|
| 198 |
-
)
|
| 199 |
-
p.add_argument(
|
| 200 |
-
"--outdir",
|
| 201 |
-
type=Path,
|
| 202 |
-
default=None,
|
| 203 |
-
help="Destination directory when --mode copy (default: same as slicedir)",
|
| 204 |
-
)
|
| 205 |
-
p.add_argument(
|
| 206 |
-
"--dry-run",
|
| 207 |
-
action="store_true",
|
| 208 |
-
help="Print mapping only; do not rename or copy",
|
| 209 |
-
)
|
| 210 |
-
p.add_argument(
|
| 211 |
-
"--manifest",
|
| 212 |
-
type=Path,
|
| 213 |
-
default=None,
|
| 214 |
-
help="Optional path to existing slice_manifest.csv (for bookkeeping)",
|
| 215 |
-
)
|
| 216 |
-
p.add_argument(
|
| 217 |
-
"--n-slices",
|
| 218 |
-
type=int,
|
| 219 |
-
default=253,
|
| 220 |
-
help="Expected number of slices (default 253); used only for name pool sizing",
|
| 221 |
-
)
|
| 222 |
-
args = p.parse_args()
|
| 223 |
-
|
| 224 |
-
if not args.slicedir.is_dir():
|
| 225 |
-
sys.exit(f"ERROR: not a directory: {args.slicedir}")
|
| 226 |
-
|
| 227 |
-
slices = discover_slices(args.slicedir)
|
| 228 |
-
if not slices:
|
| 229 |
-
sys.exit(f"ERROR: no n4_slice_XXX.csv files found in {args.slicedir}")
|
| 230 |
-
|
| 231 |
-
n = max(args.n_slices, max(i for i, _ in slices) + 1)
|
| 232 |
-
mapping = build_mapping(n)
|
| 233 |
-
|
| 234 |
-
print(f"Found {len(slices)} slice(s) in {args.slicedir}")
|
| 235 |
-
print(f"Mode : {'DRY-RUN' if args.dry_run else args.mode}")
|
| 236 |
-
if args.mode == "copy":
|
| 237 |
-
dest = args.outdir or args.slicedir
|
| 238 |
-
print(f"Outdir : {dest}")
|
| 239 |
-
print()
|
| 240 |
-
|
| 241 |
-
results = apply_names(
|
| 242 |
-
slices=slices,
|
| 243 |
-
mapping=mapping,
|
| 244 |
-
mode=args.mode,
|
| 245 |
-
outdir=args.outdir,
|
| 246 |
-
dry_run=args.dry_run,
|
| 247 |
-
)
|
| 248 |
-
|
| 249 |
-
# Write / update a prospect manifest
|
| 250 |
-
man_path = (args.outdir or args.slicedir) / "prospect_manifest.csv"
|
| 251 |
-
if not args.dry_run:
|
| 252 |
-
man_path.parent.mkdir(parents=True, exist_ok=True)
|
| 253 |
-
with open(man_path, "w", encoding="utf-8", newline="") as f:
|
| 254 |
-
w = csv.DictWriter(
|
| 255 |
-
f,
|
| 256 |
-
fieldnames=["slice_idx", "prospect", "original", "named_file"],
|
| 257 |
-
)
|
| 258 |
-
w.writeheader()
|
| 259 |
-
w.writerows(results)
|
| 260 |
-
print()
|
| 261 |
-
print(f"Prospect manifest → {man_path}")
|
| 262 |
-
|
| 263 |
-
print(f"Done — {len(results)} slice(s) processed.")
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
if __name__ == "__main__":
|
| 267 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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