Download make_demo_data.py from princessamygdala/TreasureHuntForTwo: direct link, hf CLI and curl.
- Browser
- Download file 5.99 kB
-
https://huggingface.co/spaces/princessamygdala/TreasureHuntForTwo/resolve/main/make_demo_data.py
- Command line
-
hf download hf://spaces/princessamygdala/TreasureHuntForTwo/make_demo_data.py
-
curl -L -o make_demo_data.py https://huggingface.co/spaces/princessamygdala/TreasureHuntForTwo/resolve/main/make_demo_data.py
5.99 kB
| """Simulate a full playthrough and write a msgpack data file in the SAME format the | |
| live app produces, so we can validate export_csv.py without a browser.""" | |
| import os | |
| from datetime import datetime, timedelta | |
| import msgpack | |
| import numpy as np | |
| import jax | |
| import jax.tree_util as jtu | |
| import experiment_structure as E | |
| import nicewebrl | |
| os.makedirs("data", exist_ok=True) | |
| SEED = 999999 | |
| PID = "0007" # demo participant (group 0, number 7) | |
| PATH = f"data/user_data_{SEED}.msgpack" | |
| BASE = datetime(2026, 1, 1, 12, 0, 0) | |
| def write_record(f, rec): | |
| packed = msgpack.packb(rec) | |
| f.write(len(packed).to_bytes(4, "big")) | |
| f.write(packed) | |
| def env_record(name, timestep, action_idx, t): | |
| d = nicewebrl.make_serializable(E.custom_data_fn(timestep)) | |
| d.update( | |
| pid=PID, gid=int(PID[0]), # app.storage isn't available off-thread; inject here | |
| image_seen_time=(BASE + timedelta(seconds=t)).isoformat(), | |
| action_taken_time=(BASE + timedelta(seconds=t, milliseconds=450)).isoformat(), | |
| computer_interaction=E.action_keys[action_idx], | |
| action_name=E.action_to_name[action_idx], | |
| action_idx=action_idx, | |
| ) | |
| return dict(name=name, body="", stage_idx=0, session_id="demo", | |
| user_data=dict(user_id=SEED, age=27, sex="Female"), | |
| metadata=dict(type="EnvStage", task=name.lower()), data=d) | |
| def drive(f, params, name, t0): | |
| p = params | |
| ts = E.jax_web_env.reset(jax.random.PRNGKey(7), p) | |
| t = t0 | |
| def step(ts, a, sd): | |
| nxt = E.jax_web_env.next_steps(jax.random.PRNGKey(sd), ts, p) | |
| return jtu.tree_map(lambda x: x[a], nxt) | |
| def nearest(s): | |
| ar, ac = int(s.agent_pos[0]), int(s.agent_pos[1]) | |
| hp, act, co = np.asarray(s.house_pos), np.asarray(s.house_active), np.asarray(s.consumed) | |
| best, bd = None, 1e9 | |
| for i in range(E.N_PLACES): | |
| if act[i] and not co[i]: | |
| dd = abs(hp[i][0] - ar) + abs(hp[i][1] - ac) | |
| if dd < bd: | |
| best, bd = (int(hp[i][0]), int(hp[i][1])), dd | |
| return best | |
| rating_target = 0 | |
| for stp in range(40000): | |
| if bool(ts.last()): | |
| break | |
| s = ts.state | |
| m = int(s.mode) | |
| if m == E.M_OVERWORLD: | |
| tgt = nearest(s) | |
| if tgt is None: | |
| break | |
| ar, ac = int(s.agent_pos[0]), int(s.agent_pos[1]) | |
| tr, tc = tgt | |
| a = (E.A_UP if tr < ar else E.A_DOWN) if ar != tr else (E.A_LEFT if tc < ac else E.A_RIGHT) | |
| elif m == E.M_CHOICE: | |
| if int(s.awaiting_start): | |
| # right after entering a house, L/R is locked out until one SPACE is pressed | |
| a = E.A_SPACE | |
| else: | |
| a = E.A_LEFT if (stp % 2 == 0) else E.A_RIGHT # vary choices | |
| elif m == E.M_OUTCOME: | |
| a = E.A_SPACE | |
| else: # rating: nudge cursor then confirm | |
| cur = int(s.rating_cursor) | |
| a = E.A_RIGHT if cur < (1 + (int(s.current_place) % 5)) else E.A_SPACE | |
| write_record(f, env_record(name, ts, a, t)) | |
| ts = step(ts, a, stp + 1) | |
| t += 2 | |
| return t | |
| with open(PATH, "wb") as f: | |
| # quiz: one FeedbackStage record holding all responses (matches the live app) | |
| responses = [] | |
| for i, (st, co, fb) in enumerate(E.quiz_items("c")): | |
| ans = co if i != 1 else (not co) # get one wrong on purpose | |
| responses.append(dict(quizIndex=i, position=i, statement=st, | |
| answer=("TRUE" if ans else "FALSE"), | |
| correct_answer=("TRUE" if co else "FALSE"), | |
| is_correct=bool(ans == co), attempt=1)) | |
| write_record(f, dict(name="Quiz", body="", stage_idx=0, session_id="demo", | |
| user_data=dict(user_id=SEED, age=27, sex="Female"), | |
| metadata=dict(type="FeedbackStage"), | |
| data=dict(responses=responses))) | |
| # technical info + intro slide timing + a couple of clicks (as the live app logs them) | |
| def event(kind, **fields): | |
| write_record(f, dict(name="Event", body="", session_id="demo", | |
| user_data=dict(user_id=SEED), | |
| metadata=dict(type="Event", kind=kind), | |
| data=dict(kind=kind, pid=PID, gid=int(PID[0]), | |
| t=(BASE).isoformat(), **fields))) | |
| event("tech", game_version="treasure-hunt-for-two 1.0", git_commit="demo123", | |
| dev_mode=0, prolific_pid="", study_id="", session_id="", nicewebrl_version="0.0.0", | |
| framerate_fps=60, user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) " | |
| "Chrome/124.0 Safari/537.36", platform="MacIntel", language="en-US", | |
| timezone="Europe/Berlin", cpu_cores=8, screen_w=1920, screen_h=1080, | |
| device_pixel_ratio=2, viewport_w=1440, viewport_h=900) | |
| for sl in ["welcome", "cover", "howto", "explorer"]: | |
| event("slide", slide=sl, phase="enter") | |
| event("slide", slide=sl, phase="exit", dwell_ms=4200, action="next") | |
| event("click", label="Next", tag="BUTTON", x=720, y=560) | |
| t = drive(f, E.practice_params_default, "Practice", 100) | |
| drive(f, E.main_params_for("0007"), "Explore", t + 10) | |
| # two simulated breaks (start/end event pairs), as the live app would log them | |
| for (bs, dur) in [(600, 45), (900, 120)]: | |
| for phase, off in [("start", 0), ("end", dur)]: | |
| ev = dict(kind="break", pid=PID, gid=int(PID[0]), phase=phase, | |
| t=(BASE + timedelta(seconds=bs + off)).isoformat()) | |
| if phase == "end": | |
| ev["break_ms"] = dur * 1000 | |
| write_record(f, dict(name="Event", body="", session_id="demo", | |
| user_data=dict(user_id=SEED), | |
| metadata=dict(type="Event", kind="break"), data=ev)) | |
| print(f"wrote {PATH}") | |