Download clevrer_lite/events.py from jimchen2/clevrer-lite-example: direct link, hf CLI and curl.
- Browser
- Download file 2.5 kB
-
https://huggingface.co/datasets/jimchen2/clevrer-lite-example/resolve/main/clevrer_lite/events.py
- Command line
-
hf download hf://datasets/jimchen2/clevrer-lite-example/clevrer_lite/events.py
-
curl -L -o events.py https://huggingface.co/datasets/jimchen2/clevrer-lite-example/resolve/main/clevrer_lite/events.py
2.5 kB
| """Extract causal events (collisions, starts/stops of motion) from a trajectory. | |
| This is what makes question generation *grounded*: every question/answer is | |
| derived from events that verifiably happened in the recorded physics rollout. | |
| """ | |
| import numpy as np | |
| from . import config | |
| def extract_events(states, collisions, wall_hits, n): | |
| """Args: | |
| states: (T, n, 4) per-frame (x, y, vx, vy) | |
| collisions: [(t, i, j)] substep-level contact log | |
| wall_hits: [(t, i, axis, side)] | |
| n: number of objects | |
| Returns dict with: | |
| collisions: [{frame, objects:[i,j]}] grouped collision events | |
| starts: {obj_idx: [frames]} times each object starts moving | |
| stops: {obj_idx: [frames]} | |
| initially_moving: [obj_idx] | |
| moving: (T, n) bool mask | |
| speeds: (T, n) | |
| """ | |
| T = states.shape[0] | |
| speeds = np.linalg.norm(states[:, :, 2:], axis=-1) # (T, n) | |
| moving = speeds > config.MOVE_THRESH | |
| # ---- group the contact log into collision events ---- | |
| grouped = [] | |
| for (t, i, j) in sorted(collisions, key=lambda c: c[0]): | |
| if grouped and grouped[-1]["objects"] == [i, j] and \ | |
| t - grouped[-1]["end"] <= config.MIN_EVENT_GAP_FRAMES: | |
| grouped[-1]["end"] = t | |
| else: | |
| grouped.append({"frame": t, "end": t, "objects": [i, j]}) | |
| collision_events = [{"frame": g["frame"], "objects": g["objects"]} for g in grouped] | |
| # ---- start / stop of motion per object ---- | |
| starts, stops, initially_moving = {}, {}, [] | |
| min_run = config.MIN_RUN_FRAMES | |
| for o in range(n): | |
| m = moving[:, o] | |
| sts, sps = [], [] | |
| t = 0 | |
| while t < T: | |
| if m[t]: | |
| t2 = t | |
| while t2 < T and m[t2]: | |
| t2 += 1 | |
| if t2 - t >= min_run: | |
| if t == 0: | |
| initially_moving.append(o) | |
| else: | |
| sts.append(t) | |
| sps.append(t2) # frame where it (first) stops | |
| t = t2 | |
| else: | |
| t += 1 | |
| starts[o] = sts | |
| stops[o] = [s for s in sps if s < T - 1] # ignore end-of-video "stop" | |
| return { | |
| "collisions": collision_events, | |
| "starts": starts, | |
| "stops": stops, | |
| "initially_moving": sorted(initially_moving), | |
| "moving": moving, | |
| "speeds": speeds, | |
| "wall_hits": wall_hits, | |
| } | |