279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375 | def compile_trial(
spec: TrialSpec,
) -> HtmlKeyboardResponse | HtmlChoice | HtmlSliderResponse:
"""Compile one declarative trial spec into a SweetBean stimulus object."""
scene_html, rendered = _build_scene_html(spec)
if not spec.responses:
return HtmlKeyboardResponse(
duration=_max_duration_ms(spec),
stimulus=scene_html,
choices=[],
correct_key="",
)
if len(spec.responses) != 1:
raise ValueError(
"Current renderer supports exactly one response spec per trial."
)
response = spec.responses[0]
if response.kind == "keyboard_press":
if response.max_responses is not None and response.max_responses > 1:
raise ValueError(
"Keyboard response: max_responses > 1 is not supported by the "
"renderer yet; use 1 or null (unlimited)."
)
correct_key = ""
if response.correct_keys:
if len(response.correct_keys) > 1:
raise ValueError(
"Keyboard response currently supports at most one `correct_keys` "
"entry for built-in correctness tracking."
)
correct_key = response.correct_keys[0]
keys = list(response.allowed_keys)
choices: list[str] | _JsPsychAllKeys = (
keys if keys else _JS_PSYCH_ALL_KEYS
)
return HtmlKeyboardResponse(
duration=_max_duration_ms(spec),
stimulus=scene_html,
choices=choices,
correct_key=correct_key,
)
if response.kind == "mouse_click":
if response.max_responses is not None and response.max_responses > 1:
raise ValueError(
"Mouse click response: max_responses > 1 is not supported by the "
"renderer yet; use 1 or null (unlimited)."
)
target_set = set(response.target_ids)
html_array: list[str] = []
values: list[str] = []
for stim_id, stim_html in rendered:
if target_set and stim_id not in target_set:
continue
html_array.append(stim_html)
values.append(stim_id)
if not html_array:
raise ValueError(
"Mouse click response has no clickable targets after filtering."
)
return HtmlChoice(
duration=_max_duration_ms(spec),
html_array=html_array,
values=values,
correct_values=list(response.correct_target_ids or ()),
time_after_response=0,
)
if response.kind == "slider":
if response.max_responses is not None and response.max_responses > 1:
raise ValueError(
"Slider response: max_responses > 1 is not supported by the "
"renderer yet; use 1 or null (unlimited)."
)
start = (
response.start_value
if response.start_value is not None
else (response.min_value + response.max_value) / 2.0
)
return HtmlSliderResponse(
duration=_max_duration_ms(spec),
stimulus=scene_html,
min=response.min_value,
max=response.max_value,
slider_start=start,
step=response.step,
labels=list(response.tick_labels),
button_label=response.button_label,
require_movement=response.require_movement,
response_ends_trial=response.response_ends_trial,
correct_values=list(response.correct_values or ()),
correct_min=response.correct_min_value,
correct_max=response.correct_max_value,
)
raise ValueError(f"Unsupported response kind: {response.kind}")
|