File size: 5,986 Bytes
bd2c7cd 81f9092 bd2c7cd 81f9092 bd2c7cd cb32d35 bd2c7cd 9f9579e cb32d35 bd2c7cd 3421e60 81f9092 9f9579e f137ac4 4895777 f137ac4 356440a c7a3196 bd2c7cd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | """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}")
|