Datasets:
Delete slice_n4_run.py
Browse files- slice_n4_run.py +0 -311
slice_n4_run.py
DELETED
|
@@ -1,311 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
slice_n4_run.py
|
| 4 |
-
---------------
|
| 5 |
-
Slice the canonical RGC N4 logistic CSV into 253 roughly equal pieces
|
| 6 |
-
with small tail overlaps on every interior slice.
|
| 7 |
-
|
| 8 |
-
Design goals
|
| 9 |
-
------------
|
| 10 |
-
* Stream the source file — never load the full multi-GB CSV into RAM.
|
| 11 |
-
* Keep every slice self-contained (identical header row).
|
| 12 |
-
* Prefer cuts that fall on frame boundaries (multiples of 49 data rows)
|
| 13 |
-
so a 49-core lattice snapshot is never split mid-frame.
|
| 14 |
-
* Add a configurable overlap (default 1.5 MB ≈ a few thousand frames)
|
| 15 |
-
at the *start* of every slice except the first, and at the *end* of
|
| 16 |
-
every slice except the last. This gives contiguous analysis windows
|
| 17 |
-
a small shared context for ACF / period-3 / order-parameter continuity.
|
| 18 |
-
* Produce deterministic, numbered output files:
|
| 19 |
-
n4_slice_000.csv … n4_slice_252.csv
|
| 20 |
-
|
| 21 |
-
Usage
|
| 22 |
-
-----
|
| 23 |
-
python slice_n4_run.py /path/to/logistic_20260606_145947.csv \\
|
| 24 |
-
[--outdir ./n4_slices] \\
|
| 25 |
-
[--n-slices 253] \\
|
| 26 |
-
[--overlap-mb 1.5] \\
|
| 27 |
-
[--cores 49]
|
| 28 |
-
|
| 29 |
-
The script only needs read access to the source and write access to
|
| 30 |
-
the output directory. It prints a progress summary and a final
|
| 31 |
-
manifest (slice index, byte range, row estimate, file size).
|
| 32 |
-
"""
|
| 33 |
-
|
| 34 |
-
from __future__ import annotations
|
| 35 |
-
|
| 36 |
-
import argparse
|
| 37 |
-
import os
|
| 38 |
-
import sys
|
| 39 |
-
from pathlib import Path
|
| 40 |
-
from typing import List, Tuple
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# ---------------------------------------------------------------------------
|
| 44 |
-
# Helpers
|
| 45 |
-
# ---------------------------------------------------------------------------
|
| 46 |
-
|
| 47 |
-
def human(n: float) -> str:
|
| 48 |
-
"""Pretty-print a byte count."""
|
| 49 |
-
for unit in ("B", "KB", "MB", "GB", "TB"):
|
| 50 |
-
if abs(n) < 1024.0:
|
| 51 |
-
return f"{n:6.1f} {unit}"
|
| 52 |
-
n /= 1024.0
|
| 53 |
-
return f"{n:.1f} PB"
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def find_frame_aligned_offset(
|
| 57 |
-
fh,
|
| 58 |
-
target_byte: int,
|
| 59 |
-
cores: int,
|
| 60 |
-
header_len: int,
|
| 61 |
-
search_window: int = 512 * 1024,
|
| 62 |
-
) -> int:
|
| 63 |
-
"""
|
| 64 |
-
Return a byte offset >= target_byte that sits on a clean frame boundary
|
| 65 |
-
(i.e. after a complete set of `cores` data rows).
|
| 66 |
-
|
| 67 |
-
Strategy: jump near the target, read a modest window, count newlines,
|
| 68 |
-
and advance until (data_rows_seen % cores) == 0.
|
| 69 |
-
"""
|
| 70 |
-
file_size = fh.seek(0, os.SEEK_END)
|
| 71 |
-
if target_byte >= file_size:
|
| 72 |
-
return file_size
|
| 73 |
-
|
| 74 |
-
# Start a little before the target so we have context
|
| 75 |
-
start = max(header_len, target_byte - search_window // 4)
|
| 76 |
-
fh.seek(start)
|
| 77 |
-
buf = fh.read(search_window)
|
| 78 |
-
if not buf:
|
| 79 |
-
return file_size
|
| 80 |
-
|
| 81 |
-
# If we started mid-line, discard the partial first line
|
| 82 |
-
if start > header_len and buf[0:1] != b"\n":
|
| 83 |
-
nl = buf.find(b"\n")
|
| 84 |
-
if nl < 0:
|
| 85 |
-
return min(file_size, start + len(buf))
|
| 86 |
-
buf = buf[nl + 1 :]
|
| 87 |
-
start += nl + 1
|
| 88 |
-
|
| 89 |
-
# Walk line-by-line until we are past target_byte *and* on a frame edge
|
| 90 |
-
pos = start
|
| 91 |
-
data_rows = 0
|
| 92 |
-
for line in buf.splitlines(keepends=True):
|
| 93 |
-
pos += len(line)
|
| 94 |
-
data_rows += 1
|
| 95 |
-
if pos >= target_byte and (data_rows % cores) == 0:
|
| 96 |
-
return min(pos, file_size)
|
| 97 |
-
|
| 98 |
-
# Fallback: just return the end of the window (still better than mid-line)
|
| 99 |
-
return min(pos, file_size)
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
def compute_slice_plan(
|
| 103 |
-
file_size: int,
|
| 104 |
-
header_len: int,
|
| 105 |
-
n_slices: int,
|
| 106 |
-
overlap_bytes: int,
|
| 107 |
-
cores: int,
|
| 108 |
-
fh,
|
| 109 |
-
) -> List[Tuple[int, int, int, int]]:
|
| 110 |
-
"""
|
| 111 |
-
Return a list of (slice_idx, start_byte, end_byte, pure_end_byte).
|
| 112 |
-
|
| 113 |
-
start_byte – inclusive, may include leading overlap
|
| 114 |
-
end_byte – exclusive, may include trailing overlap
|
| 115 |
-
pure_end_byte – the nominal boundary *without* trailing overlap
|
| 116 |
-
(used so the next slice knows where its leading
|
| 117 |
-
overlap should begin)
|
| 118 |
-
"""
|
| 119 |
-
body_size = file_size - header_len
|
| 120 |
-
if body_size <= 0:
|
| 121 |
-
raise ValueError("File contains only a header (or is empty).")
|
| 122 |
-
|
| 123 |
-
nominal_chunk = body_size / n_slices
|
| 124 |
-
plan = []
|
| 125 |
-
|
| 126 |
-
# First pass: pure (non-overlapping) boundaries, frame-aligned
|
| 127 |
-
pure_bounds = [header_len]
|
| 128 |
-
for i in range(1, n_slices):
|
| 129 |
-
target = header_len + int(i * nominal_chunk)
|
| 130 |
-
aligned = find_frame_aligned_offset(fh, target, cores, header_len)
|
| 131 |
-
pure_bounds.append(aligned)
|
| 132 |
-
pure_bounds.append(file_size)
|
| 133 |
-
|
| 134 |
-
# Second pass: expand each interior slice by overlap_bytes on both sides
|
| 135 |
-
for i in range(n_slices):
|
| 136 |
-
pure_start = pure_bounds[i]
|
| 137 |
-
pure_end = pure_bounds[i + 1]
|
| 138 |
-
|
| 139 |
-
if i == 0:
|
| 140 |
-
start = pure_start # no leading overlap on first slice
|
| 141 |
-
else:
|
| 142 |
-
# Walk backward from pure_start by ~overlap_bytes, stay frame-aligned
|
| 143 |
-
target = max(header_len, pure_start - overlap_bytes)
|
| 144 |
-
start = find_frame_aligned_offset(fh, target, cores, header_len)
|
| 145 |
-
# Guarantee we do not go past the previous pure boundary in a
|
| 146 |
-
# way that would create negative-length regions
|
| 147 |
-
start = min(start, pure_start)
|
| 148 |
-
|
| 149 |
-
if i == n_slices - 1:
|
| 150 |
-
end = pure_end # no trailing overlap on last slice
|
| 151 |
-
else:
|
| 152 |
-
target = min(file_size, pure_end + overlap_bytes)
|
| 153 |
-
end = find_frame_aligned_offset(fh, target, cores, header_len)
|
| 154 |
-
|
| 155 |
-
plan.append((i, start, end, pure_end))
|
| 156 |
-
|
| 157 |
-
return plan
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
# ---------------------------------------------------------------------------
|
| 161 |
-
# Main slicing routine
|
| 162 |
-
# ---------------------------------------------------------------------------
|
| 163 |
-
|
| 164 |
-
def slice_file(
|
| 165 |
-
src: Path,
|
| 166 |
-
outdir: Path,
|
| 167 |
-
n_slices: int = 253,
|
| 168 |
-
overlap_mb: float = 1.5,
|
| 169 |
-
cores: int = 49,
|
| 170 |
-
bufsize: int = 8 * 1024 * 1024,
|
| 171 |
-
) -> None:
|
| 172 |
-
if not src.is_file():
|
| 173 |
-
sys.exit(f"ERROR: source file not found: {src}")
|
| 174 |
-
|
| 175 |
-
outdir.mkdir(parents=True, exist_ok=True)
|
| 176 |
-
overlap_bytes = int(overlap_mb * 1024 * 1024)
|
| 177 |
-
file_size = src.stat().st_size
|
| 178 |
-
|
| 179 |
-
print(f"Source : {src}")
|
| 180 |
-
print(f"Size : {human(file_size)}")
|
| 181 |
-
print(f"Slices : {n_slices}")
|
| 182 |
-
print(f"Overlap : {overlap_mb} MB ({human(overlap_bytes)})")
|
| 183 |
-
print(f"Cores/frame : {cores}")
|
| 184 |
-
print(f"Output dir : {outdir}")
|
| 185 |
-
print()
|
| 186 |
-
|
| 187 |
-
with open(src, "rb") as fh:
|
| 188 |
-
# Read header (first line)
|
| 189 |
-
header = fh.readline()
|
| 190 |
-
if not header:
|
| 191 |
-
sys.exit("ERROR: empty file")
|
| 192 |
-
header_len = len(header)
|
| 193 |
-
print(f"Header ({header_len} bytes): {header.decode('utf-8', errors='replace').rstrip()}")
|
| 194 |
-
print()
|
| 195 |
-
|
| 196 |
-
plan = compute_slice_plan(
|
| 197 |
-
file_size, header_len, n_slices, overlap_bytes, cores, fh
|
| 198 |
-
)
|
| 199 |
-
|
| 200 |
-
manifest = []
|
| 201 |
-
for idx, start, end, pure_end in plan:
|
| 202 |
-
out_path = outdir / f"n4_slice_{idx:03d}.csv"
|
| 203 |
-
length = end - start
|
| 204 |
-
# Leading / trailing overlap sizes for the log
|
| 205 |
-
lead = max(0, pure_end - start) if idx > 0 else 0
|
| 206 |
-
# Actually lead = pure_start - start; we approximate via plan
|
| 207 |
-
pure_start = plan[idx][1] if idx == 0 else plan[idx - 1][3]
|
| 208 |
-
# Recompute cleanly
|
| 209 |
-
pure_start_i = header_len if idx == 0 else plan[idx - 1][3]
|
| 210 |
-
lead_ov = max(0, pure_start_i - start) if idx > 0 else 0
|
| 211 |
-
trail_ov = max(0, end - pure_end) if idx < n_slices - 1 else 0
|
| 212 |
-
|
| 213 |
-
with open(out_path, "wb") as out:
|
| 214 |
-
out.write(header) # every slice is self-describing
|
| 215 |
-
fh.seek(start)
|
| 216 |
-
remaining = length
|
| 217 |
-
while remaining > 0:
|
| 218 |
-
chunk = fh.read(min(bufsize, remaining))
|
| 219 |
-
if not chunk:
|
| 220 |
-
break
|
| 221 |
-
out.write(chunk)
|
| 222 |
-
remaining -= len(chunk)
|
| 223 |
-
|
| 224 |
-
final_size = out_path.stat().st_size
|
| 225 |
-
manifest.append(
|
| 226 |
-
{
|
| 227 |
-
"idx": idx,
|
| 228 |
-
"file": out_path.name,
|
| 229 |
-
"start": start,
|
| 230 |
-
"end": end,
|
| 231 |
-
"bytes": final_size,
|
| 232 |
-
"lead_ov": lead_ov,
|
| 233 |
-
"trail_ov": trail_ov,
|
| 234 |
-
}
|
| 235 |
-
)
|
| 236 |
-
print(
|
| 237 |
-
f" [{idx:03d}] {out_path.name} "
|
| 238 |
-
f"{human(final_size)} "
|
| 239 |
-
f"bytes {start:,}–{end:,} "
|
| 240 |
-
f"lead_ov={human(lead_ov)} trail_ov={human(trail_ov)}"
|
| 241 |
-
)
|
| 242 |
-
|
| 243 |
-
# Write a small manifest CSV for later bookkeeping
|
| 244 |
-
man_path = outdir / "slice_manifest.csv"
|
| 245 |
-
with open(man_path, "w", encoding="utf-8") as m:
|
| 246 |
-
m.write("slice_idx,filename,start_byte,end_byte,file_bytes,lead_overlap_bytes,trail_overlap_bytes\n")
|
| 247 |
-
for row in manifest:
|
| 248 |
-
m.write(
|
| 249 |
-
f"{row['idx']},{row['file']},{row['start']},{row['end']},"
|
| 250 |
-
f"{row['bytes']},{row['lead_ov']},{row['trail_ov']}\n"
|
| 251 |
-
)
|
| 252 |
-
print()
|
| 253 |
-
print(f"Manifest written to {man_path}")
|
| 254 |
-
print(f"Done — {n_slices} slices.")
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
# ---------------------------------------------------------------------------
|
| 258 |
-
# CLI
|
| 259 |
-
# ---------------------------------------------------------------------------
|
| 260 |
-
|
| 261 |
-
def main() -> None:
|
| 262 |
-
p = argparse.ArgumentParser(
|
| 263 |
-
description="Slice canonical RGC N4 logistic CSV into overlapping pieces."
|
| 264 |
-
)
|
| 265 |
-
p.add_argument(
|
| 266 |
-
"source",
|
| 267 |
-
type=Path,
|
| 268 |
-
help="Path to the full logistic_*.csv (canonical N4 run)",
|
| 269 |
-
)
|
| 270 |
-
p.add_argument(
|
| 271 |
-
"--outdir",
|
| 272 |
-
type=Path,
|
| 273 |
-
default=Path("./n4_slices"),
|
| 274 |
-
help="Directory for the slice files (default: ./n4_slices)",
|
| 275 |
-
)
|
| 276 |
-
p.add_argument(
|
| 277 |
-
"--n-slices",
|
| 278 |
-
type=int,
|
| 279 |
-
default=253,
|
| 280 |
-
help="Number of slices (default: 253)",
|
| 281 |
-
)
|
| 282 |
-
p.add_argument(
|
| 283 |
-
"--overlap-mb",
|
| 284 |
-
type=float,
|
| 285 |
-
default=1.5,
|
| 286 |
-
help="Overlap size in MiB at each interior boundary (default: 1.5)",
|
| 287 |
-
)
|
| 288 |
-
p.add_argument(
|
| 289 |
-
"--cores",
|
| 290 |
-
type=int,
|
| 291 |
-
default=49,
|
| 292 |
-
help="Cores per lattice frame (default: 49)",
|
| 293 |
-
)
|
| 294 |
-
args = p.parse_args()
|
| 295 |
-
|
| 296 |
-
if args.n_slices < 1:
|
| 297 |
-
sys.exit("--n-slices must be >= 1")
|
| 298 |
-
if args.overlap_mb < 0:
|
| 299 |
-
sys.exit("--overlap-mb must be >= 0")
|
| 300 |
-
|
| 301 |
-
slice_file(
|
| 302 |
-
src=args.source,
|
| 303 |
-
outdir=args.outdir,
|
| 304 |
-
n_slices=args.n_slices,
|
| 305 |
-
overlap_mb=args.overlap_mb,
|
| 306 |
-
cores=args.cores,
|
| 307 |
-
)
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
if __name__ == "__main__":
|
| 311 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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