Datasets:
Download scripts/slice_n4_log.py from parti-wave/N4_slice: direct link, hf CLI and curl.
- Browser
- Download file 4.85 kB
-
https://huggingface.co/datasets/parti-wave/N4_slice/resolve/main/scripts/slice_n4_log.py
- Command line
-
hf download hf://datasets/parti-wave/N4_slice/scripts/slice_n4_log.py
-
curl -L -o slice_n4_log.py https://huggingface.co/datasets/parti-wave/N4_slice/resolve/main/scripts/slice_n4_log.py
4.85 kB
| #!/usr/bin/env python3 | |
| """Slice RGC session log into 253 overlapping windows (match N4 data slices).""" | |
| from __future__ import annotations | |
| import argparse, os, sys | |
| from pathlib import Path | |
| from typing import List, Tuple | |
| def human(n: float) -> str: | |
| for unit in ("B", "KB", "MB", "GB", "TB"): | |
| if abs(n) < 1024.0: | |
| return f"{n:6.1f} {unit}" | |
| n /= 1024.0 | |
| return f"{n:.1f} PB" | |
| def find_line_aligned_offset(fh, target_byte: int, search_window: int = 512 * 1024) -> int: | |
| file_size = fh.seek(0, os.SEEK_END) | |
| if target_byte <= 0: | |
| return 0 | |
| if target_byte >= file_size: | |
| return file_size | |
| start = max(0, target_byte - search_window // 4) | |
| fh.seek(start) | |
| buf = fh.read(min(search_window, file_size - start)) | |
| if not buf: | |
| return file_size | |
| if start > 0: | |
| nl = buf.find(b"\n") | |
| if nl < 0: | |
| return min(file_size, start + len(buf)) | |
| buf = buf[nl + 1 :] | |
| start += nl + 1 | |
| pos = start | |
| for line in buf.splitlines(keepends=True): | |
| pos += len(line) | |
| if pos >= target_byte: | |
| return min(pos, file_size) | |
| return min(pos, file_size) | |
| def compute_slice_plan(file_size: int, n_slices: int, overlap_bytes: int, fh) -> List[Tuple[int, int, int, int]]: | |
| if file_size <= 0: | |
| raise ValueError("Empty log file.") | |
| nominal = file_size / n_slices | |
| pure_bounds = [0] | |
| for i in range(1, n_slices): | |
| pure_bounds.append(find_line_aligned_offset(fh, int(i * nominal))) | |
| pure_bounds.append(file_size) | |
| plan = [] | |
| for i in range(n_slices): | |
| pure_start, pure_end = pure_bounds[i], pure_bounds[i + 1] | |
| if i == 0: | |
| start = pure_start | |
| else: | |
| start = min(find_line_aligned_offset(fh, max(0, pure_start - overlap_bytes)), pure_start) | |
| if i == n_slices - 1: | |
| end = pure_end | |
| else: | |
| end = find_line_aligned_offset(fh, min(file_size, pure_end + overlap_bytes)) | |
| plan.append((i, start, end, pure_end)) | |
| return plan | |
| def slice_log(src: Path, outdir: Path, n_slices: int = 253, overlap_mb: float = 1.5, bufsize: int = 8 * 1024 * 1024) -> None: | |
| if not src.is_file(): | |
| sys.exit(f"ERROR: source file not found: {src}") | |
| outdir.mkdir(parents=True, exist_ok=True) | |
| overlap_bytes = int(overlap_mb * 1024 * 1024) | |
| file_size = src.stat().st_size | |
| print(f"Source : {src}") | |
| print(f"Size : {human(file_size)}") | |
| print(f"Slices : {n_slices}") | |
| print(f"Overlap : {overlap_mb} MB ({human(overlap_bytes)})") | |
| print(f"Output dir : {outdir}\n") | |
| with open(src, "rb") as fh: | |
| plan = compute_slice_plan(file_size, n_slices, overlap_bytes, fh) | |
| manifest = [] | |
| for idx, start, end, pure_end in plan: | |
| out_path = outdir / f"n4_log_{idx:03d}.log" | |
| length = end - start | |
| pure_start_i = 0 if idx == 0 else plan[idx - 1][3] | |
| lead_ov = max(0, pure_start_i - start) if idx > 0 else 0 | |
| trail_ov = max(0, end - pure_end) if idx < n_slices - 1 else 0 | |
| with open(out_path, "wb") as out: | |
| fh.seek(start) | |
| remaining = length | |
| while remaining > 0: | |
| chunk = fh.read(min(bufsize, remaining)) | |
| if not chunk: | |
| break | |
| out.write(chunk) | |
| remaining -= len(chunk) | |
| final_size = out_path.stat().st_size | |
| manifest.append({"idx": idx, "file": out_path.name, "start": start, "end": end, "bytes": final_size, "lead_ov": lead_ov, "trail_ov": trail_ov}) | |
| print(f" [{idx:03d}] {out_path.name} {human(final_size)} bytes {start:,}–{end:,} lead_ov={human(lead_ov)} trail_ov={human(trail_ov)}") | |
| man_path = outdir / "log_slice_manifest.csv" | |
| with open(man_path, "w", encoding="utf-8") as m: | |
| m.write("slice_idx,filename,start_byte,end_byte,file_bytes,lead_overlap_bytes,trail_overlap_bytes\n") | |
| for row in manifest: | |
| m.write(f"{row['idx']},{row['file']},{row['start']},{row['end']},{row['bytes']},{row['lead_ov']},{row['trail_ov']}\n") | |
| print(f"\nManifest written to {man_path}") | |
| print(f"Done — {n_slices} log slices.") | |
| def main() -> None: | |
| p = argparse.ArgumentParser(description="Slice RGC session log into overlapping windows.") | |
| p.add_argument("source", type=Path) | |
| p.add_argument("--outdir", type=Path, default=Path("./n4_log_slices")) | |
| p.add_argument("--n-slices", type=int, default=253) | |
| p.add_argument("--overlap-mb", type=float, default=1.5) | |
| args = p.parse_args() | |
| if args.n_slices < 1 or args.overlap_mb < 0: | |
| sys.exit("Invalid n-slices or overlap-mb") | |
| slice_log(args.source, args.outdir, args.n_slices, args.overlap_mb) | |
| if __name__ == "__main__": | |
| main() | |