Datasets:
Download items/ap-v21/cps0c18683407/make_gt.py from Sci-Index/Science-Index-Video: direct link, hf CLI and curl.
- Browser
- Download file 2.54 kB
-
https://huggingface.co/datasets/Sci-Index/Science-Index-Video/resolve/main/items/ap-v21/cps0c18683407/make_gt.py
- Command line
-
hf download hf://datasets/Sci-Index/Science-Index-Video/items/ap-v21/cps0c18683407/make_gt.py
-
curl -L -o make_gt.py https://huggingface.co/datasets/Sci-Index/Science-Index-Video/resolve/main/items/ap-v21/cps0c18683407/make_gt.py
2.54 kB
| import io, json, os, struct, sys, time, zipfile, zlib | |
| import urllib.request | |
| URL = "https://zenodo.org/api/records/14281479/files/andi2_benchmark_dataset.zip/content" | |
| SIZE = 749404326 | |
| B_IMM, B_FAST = 0.7, 12.0 | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| class R(io.RawIOBase): | |
| def __init__(s): | |
| s.p = 0 | |
| def seekable(s): | |
| return True | |
| def readable(s): | |
| return True | |
| def tell(s): | |
| return s.p | |
| def seek(s, o, w=0): | |
| s.p = o if w == 0 else s.p + o if w == 1 else SIZE + o | |
| return s.p | |
| def read(s, n=-1): | |
| n = SIZE - s.p if n is None or n < 0 else min(n, SIZE - s.p) | |
| if n <= 0: | |
| return b"" | |
| for k in range(5): | |
| try: | |
| q = urllib.request.Request(URL, headers={"Range": f"bytes={s.p}-{s.p + n - 1}"}) | |
| d = urllib.request.urlopen(q, timeout=120).read() | |
| if len(d) == n: | |
| s.p += n | |
| return d | |
| except Exception: | |
| time.sleep(5) | |
| raise IOError("range read failed") | |
| def readinto(s, b): | |
| d = s.read(len(b)); b[:len(d)] = d; return len(d) | |
| def labs_text(name): | |
| for root in [os.environ.get("APV21_CACHE", "raw_cache")]: | |
| p = os.path.join(root, name) | |
| if os.path.exists(p): | |
| return open(p).read() | |
| return zipfile.ZipFile(io.BufferedReader(R(), 1 << 16)).read(name).decode() | |
| def segments(line): | |
| x = [float(a) for a in line.split(",")] | |
| body = x[1:] | |
| segs, start = [], 0 | |
| D, a = body[0], body[1] | |
| rest = body[3:] | |
| while len(rest) >= 4: | |
| cp = int(round(rest[0])); segs.append((start, cp, D, a)); start = cp | |
| D, a = rest[1], rest[2]; rest = rest[4:] | |
| segs.append((start, int(round(rest[0])), D, a)) | |
| return int(x[0]), segs | |
| def label(D, a): | |
| m = 4 * D * 10 ** a | |
| return "immobile" if m < B_IMM else ("slow" if m < B_FAST else "fast") | |
| def main(): | |
| src = json.load(open(os.path.join(HERE, "gt", "src.json"))) | |
| txt = labs_text(f"track_1/exp_{src['exp']}/traj_labs_fov_{src['fov']}.txt") | |
| L = dict(segments(l) for l in txt.strip().splitlines()) | |
| out = {} | |
| for key, v in zip(src["keys"], src["traj_idx"]): | |
| segs = L[v] | |
| out[key] = {"changepoints": [s[0] for s in segs[1:]], "states": [label(s[2], s[3]) for s in segs]} | |
| ans = out["P"] if src["template_id"] == "cp_states" else {k: out[k]["changepoints"] for k in src["keys"]} | |
| print(json.dumps(ans)) | |
| return ans | |
| if __name__ == "__main__": | |
| main() | |