N4_slice / scripts /logs_for_window.py
parti-wave's picture
Upload logs_for_window.py
15a43a5 verified
Raw History Blame Contribute Delete
3.24 kB
"""
Find which session-log slices cover a given data slice's `n` window.
Prospect names pair the two streams only nominally -- data slice k and log
slice k do not cover the same part of the run (median IoU 0.242). Join on `n`
instead. This module reads `slice_n_index.csv`, which lists the [n_first,
n_last] range of every slice on both streams.
from logs_for_window import logs_for
meta, covering = logs_for(126)
Every data slice is fully covered by some set of log slices (verified for all
253), typically 3-4 of them.
"""
from pathlib import Path
import pandas as pd
# Look for the index next to this file, then in ../manifests/, then in the CWD,
# so the helper works whether it is run from scripts/, from the repo root, or
# downloaded on its own alongside the index.
_CANDIDATES = [
Path(__file__).resolve().parent / "slice_n_index.csv",
Path(__file__).resolve().parent.parent / "manifests" / "slice_n_index.csv",
Path("slice_n_index.csv"),
Path("manifests/slice_n_index.csv"),
]
def load_index(path=None) -> pd.DataFrame:
"""Load slice_n_index.csv from an explicit path or the usual locations."""
if path is not None:
return pd.read_csv(path)
for c in _CANDIDATES:
if c.is_file():
return pd.read_csv(c)
raise FileNotFoundError(
"slice_n_index.csv not found. Pass an explicit path, or fetch it with:\n"
" huggingface_hub.hf_hub_download('parti-wave/N4_slice',\n"
" 'manifests/slice_n_index.csv', repo_type='dataset')"
)
def logs_for(data_slice_idx: int, index: pd.DataFrame | None = None):
"""
Return (data_slice_meta, covering_log_slices).
`covering_log_slices` is sorted by how much of the data window each log
slice covers, with a `frac_of_data_window` column so thin host context is
visible rather than silent.
"""
idx = load_index() if index is None else index
sel = idx[(idx.stream == "data") & (idx.slice_idx == data_slice_idx)]
if sel.empty:
raise KeyError(f"no data slice with index {data_slice_idx} (expected 0-252)")
d = sel.iloc[0]
L = idx[idx.stream == "log"].copy()
lo = L.n_first.clip(lower=d.n_first)
hi = L.n_last.clip(upper=d.n_last)
L["covered"] = (hi - lo + 1).clip(lower=0)
L["frac_of_data_window"] = L.covered / d.n_span
out = L[L.covered > 0].sort_values("covered", ascending=False)
return d, out[["slice_idx", "prospect", "file", "n_first", "n_last",
"covered", "frac_of_data_window"]].reset_index(drop=True)
if __name__ == "__main__":
import sys
k = int(sys.argv[1]) if len(sys.argv) > 1 else 126
d, out = logs_for(k)
print(f"data slice {k:03d} {d.prospect} "
f"n[{d.n_first:,} .. {d.n_last:,}] ({d.n_span:,} frames)")
same = out.loc[out.slice_idx == k, "frac_of_data_window"]
print(f" same-name log slice {k:03d} alone covers "
f"{(same.iloc[0] if len(same) else 0.0):.1%} of it")
print(f" covering set ({len(out)} slices):")
for _, r in out.iterrows():
print(f" log {int(r.slice_idx):03d} {r.prospect:<16} "
f"n[{int(r.n_first):>10,} .. {int(r.n_last):>10,}]"
f" covers {r.frac_of_data_window:6.1%}")