Writing a timeline
******************

The timeline is the "timeline" attribute of the experiment class. Most
experiments build it in a "get_timeline" function:

   class Exp(psynet.experiment.Experiment):
       timeline = get_timeline()

Most examples on this page come from the "demos/features/timeline"
demo. Run it with "psynet debug local" from the demo directory to
click through them.


What a timeline is made of
==========================

The demo's timeline starts with four elements:

   ModularPage(
       "color",
       "What is your favorite color?",
       PushButtonControl(choices=["red", "green", "blue"]),
       time_estimate=10,
       save_answer="favorite_color",
   ),
   PageMaker(
       lambda participant: InfoPage(
           f"OK, your favorite color is {participant.var.favorite_color}."
       ),
       time_estimate=5,
   ),
   CodeBlock(
       lambda participant: participant.var.set(
           "random_number",
           random.randint(0, 100),
       )
   ),
   PageMaker(
       lambda participant: InfoPage(
           f"Your random number is {participant.var.random_number}",
       ),
       time_estimate=5,
   ),

* A page, here a "ModularPage", is constructed directly, with a
  "time_estimate" in seconds.

* A "PageMaker" wraps a function that returns a page, so that the page
  can depend on the participant. The function can take "participant"
  and "experiment" arguments.

* A "CodeBlock" wraps a function that runs on the server and returns
  nothing.

A slow function goes in an "AsyncCodeBlock", which runs it in a
background process. It needs a named function; lambdas raise an error.
With "wait=True", the default, the participant sees a waiting page
until the function finishes, and "expected_wait" in seconds is
required. With "wait=False", the participant carries on while the
function runs. The "demos/features/async_codeblock" demo shows both:

   def set_participant_var1(participant):
       time.sleep(1)
       participant.var.set("async1", "ASYNC 1")


   def set_participant_var2(participant):
       time.sleep(1)
       participant.var.set("async2", "ASYNC 2")



   timeline = Timeline(
       CodeBlock(lambda participant: participant.var.set("t1", time.time())),
       AsyncCodeBlock(
           set_participant_var1,
           wait=True,
           expected_wait=1.0,
       ),
       CodeBlock(lambda participant: participant.var.set("t2", time.time())),
       AsyncCodeBlock(
           set_participant_var2,
           wait=False,
       ),
       CodeBlock(lambda participant: participant.var.set("t3", time.time())),
   )


Code blocks from different participants can run at the same time. For
periodic work that isn't tied to a participant, decorate a static
method of the experiment class with "scheduled_task" from
"psynet.experiment", for example "@scheduled_task("interval",
seconds=10, max_instances=1)", as "demos/features/bot_2" does.
Scheduled tasks run in the clock process.


Remembering things about a participant
======================================

Participant variables live in "participant.var". Outside a lambda,
assign them directly; inside a lambda, use "set", because Python
doesn't allow assignments in lambdas:

   participant.var.color = "red"
   CodeBlock(lambda participant: participant.var.set("color", "red"))

Use "participant.var.get("score")" when a variable may not be set yet:
like "dict.get", it returns "None", or the default you pass, as in
"participant.var.get("score", default=0)". Attribute access, as in
"participant.var.score", raises "KeyError" for a variable that isn't
set.

A page's answer is stored in a variable when the page is given
"save_answer". In the demo above, the first page saves its answer as
"favorite_color", and the page maker after it reads
"participant.var.favorite_color". The most recent answer is also
available as "participant.answer".

Experiment variables are shared by all participants and live in
"experiment.var". Declare them, with their initial values, in the
experiment class's "variables" dictionary, as in
"demos/experiments/timeline":

   variables = {
       "new_variable": "some-value",
       "max_participant_payment": 10.0,
   }

The same dictionary overrides the defaults of PsyNet's built-in
variables, as "max_participant_payment" does here; see "Experiment"
for the list. Change an experiment variable during the experiment with
"set":

   CodeBlock(lambda experiment: experiment.var.set("difficulty", 2))


When code runs
==============

Everything at the top level of "get_timeline" runs when a server
process imports "experiment.py". Each web and worker process imports
it separately, so a random draw here is not tied to a participant and
may differ between processes:

   # Wrong: drawn at import time, not per participant
   InfoPage(f"Your number is {random.randint(0, 100)}", time_estimate=5)

Moving the draw into a page maker is also wrong, because page makers
run again when the page is refreshed. Draw in a code block and display
in a page maker, as the demo does:

   CodeBlock(
       lambda participant: participant.var.set(
           "random_number",
           random.randint(0, 100),
       )
   ),
   PageMaker(
       lambda participant: InfoPage(
           f"Your random number is {participant.var.random_number}",
       ),
       time_estimate=5,
   ),

More precisely, PsyNet calls a page maker's function again each time
the participant moves to the next element inside it, when the function
returns several elements, and whenever the page is refreshed. Keep the
function free of side effects. When it returns "cue()", only the first
call creates the trial, so later calls don't change it, but they still
repeat any work the function does. If that work is expensive, do it
once in a code block, store the result in "participant.var", and have
the page maker only read it.


Branching and repetition
========================

Each construct takes a label. Give each one a distinct, descriptive
label; nested loops must use different labels.

"switch()" takes a label, a function returning a key, and a dictionary
mapping keys to logic. Here the key is the answer to the preceding
page:

   switch(
       "choose_page",
       lambda participant: participant.var.choose_page,
       {
           "page_1": InfoPage("page_1", time_estimate=5),
           "page_2": InfoPage("page_2", time_estimate=5),
           "page_3": InfoPage("page_3", time_estimate=5),
       },
   ),

"while_loop()" takes a label, a "condition" and "logic", plus
"expected_repetitions" for time estimation. By default
("fix_time_credit=True"), every participant gets time credit for
"expected_repetitions" iterations, however many they actually do.
"max_loop_time" limits the time in the loop in seconds; participants
who reach it while the condition still holds are sent to the
unsuccessful end unless "fail_on_timeout=False". "extend_while" is
checked at the start of each pass through the loop; whenever it
returns "True", the "max_loop_time" clock restarts from zero, for
example while a participant's recording is still uploading.
"conditional()" takes a label, a "condition", and "logic_if_true" and
"logic_if_false". This loop repeats until the random score is above 5,
with feedback that depends on the score:

   while_loop(
       "my_loop",
       condition=lambda participant: participant.var.get("score", default=0) <= 5,
       logic=join(
           CodeBlock(
               lambda participant: participant.var.set(
                   "score", random.randint(1, 10)
               )
           ),
           conditional(
               "feedback",
               condition=lambda participant: participant.var.score <= 5,
               logic_if_true=PageMaker(
                   lambda participant: InfoPage(
                       f"You scored {participant.var.score}, bad luck."
                   ),
                   time_estimate=5,
               ),
               logic_if_false=PageMaker(
                   lambda participant: InfoPage(
                       f"You scored {participant.var.score}, well done!",
                   ),
                   time_estimate=5,
               ),
           ),
       ),
       expected_repetitions=2,
   ),

"for_loop()" takes keyword arguments only: "label", "iterate_over", a
function returning the list for this participant, and "logic", a
function from one item to timeline logic. It needs
"time_estimate_per_iteration" when "logic" is a function, and
"expected_repetitions" when "iterate_over" takes arguments such as
"participant":

   for_loop(
       label="counting",
       iterate_over=lambda participant: list(
           range(1, participant.var.target_number + 1)
       ),
       logic=lambda x: InfoPage(str(x), time_estimate=5),
       time_estimate_per_iteration=5,
       expected_repetitions=3,
   ),

To stop early, pass "stop_if", a function of "participant" and/or
"experiment". "for_loop" checks it before each iteration, and "join()"
checks it before each of its arguments; once it returns "True", the
participant skips the rest. Neither interrupts an iteration or
argument that has started, so wrap steps that belong together in their
own "join". To show something to the participants who stopped, follow
with a "conditional":

   def left_pair(participant):
       return not participant.in_sync_group("pair")

   join(
       for_loop(
           label="rounds",
           iterate_over=lambda: range(5),
           logic=lambda n: join(choice_page(n), outcome_page(n)),
           time_estimate_per_iteration=20,
           stop_if=left_pair,
       ),
       conditional("explain", left_pair, InfoPage("Your partner left.")),
   )


Organizing a long timeline
==========================

A "Module" groups a named section. Module names, like trial maker IDs,
must be unique within the timeline. From "demos/experiments/timeline":

   Module(
       "introduction",
       # You can set arbitrary variables with the participant object
       # inside code blocks. Here we set a variable called 'numpy_test',
       # and the value is an object from the numpy package (numpy.nan).
       CodeBlock(lambda participant: participant.var.set("numpy_test", numpy.nan)),
       PageMaker(
           lambda: InfoPage(
               f"The current time is {datetime.now().strftime('%H:%M:%S')}."
           ),
           time_estimate=5,
       ),
       ModularPage(
           "message",
           tags.p(
               "Write me a ",
               tags.span("message", style="color: red"),
               "!",
           ),
           control=TextControl(one_line=False),
           time_estimate=5,
           save_answer=True,
       ),
       PageMaker(
           lambda participant: InfoPage(f"Your message: {participant.answer}"),
           time_estimate=5,
       ),
   ),

"join()" combines elements and lists of elements into one sequence, so
sections can be defined separately and assembled into the full
timeline:

   instructions = join(
       InfoPage("In this experiment you will rate sounds.", time_estimate=5),
       InfoPage("Use headphones if you can.", time_estimate=5),
   )

   def get_timeline():
       return Timeline(consent, instructions, practice, main_task, questionnaire)


Ending the experiment early
===========================

"Timeline" appends a "SuccessfulEndPage" automatically. To end early,
place an "UnsuccessfulEndPage" in the timeline, typically inside a
conditional:

   conditional(
       "screening_result",
       condition=lambda participant: participant.var.passed_screening,
       logic_if_true=main_task,
       logic_if_false=UnsuccessfulEndPage(failure_tags=["screening"]),
   )

Successful, unsuccessful, and rejected-consent endings each have their
own branch of end logic, which you can replace with keyword arguments
to "Timeline", for example "Timeline(...,
unsuccessful_end=MyEndLogic())". See "SuccessfulEndLogic" and
"UnsuccessfulEndLogic".

When PsyNet ends a multiplayer session itself (a participant waited
too long at a barrier, was too slow to reach the next one, or their
group became too small), the unsuccessful end page says why, for
example "We waited too long for other participants, so we ended your
session." Other endings, such as failing a prescreen, get no such
line. To explain your own reasons, pass a function of the participant
that returns a line, or "None" to keep PsyNet's:

   def failure_message(participant):
       if "performance_check" in participant.failure_tags:
           return "Your answers suggest that your headphones aren't working."
       return None

   Timeline(..., unsuccessful_end=UnsuccessfulEndLogic(failure_message=failure_message))

You can also pass a plain string, but it replaces PsyNet's own lines
for every participant, including the multiplayer ones. Returning """"
shows no line at all.

To end without waiting for a click on Finish, for example in a
multiplayer game, pass "time_limit". The page then adds "This page
continues by itself in 3 s." and counts down:

   Timeline(
       ...,
       successful_end=SuccessfulEndLogic(time_limit=3),
       unsuccessful_end=UnsuccessfulEndLogic(time_limit=3),
   )

The rejected-consent page has no Finish button, so
"RejectedConsentLogic" does not take "time_limit".


Time limits
===========

Pass "time_limit" to any "Page" to allow only that many seconds on it.
The page shows "15 s left" at the top, where waiting messages also
appear, counting down, and when time runs out it submits
"time_limit_answer" for the participant:

   ModularPage(
       "offer",
       "How many coins do you offer?",
       PushButtonControl(choices=list(range(11))),
       time_limit=15,
       time_limit_answer="timeout",
       time_estimate=15,
   )

The server counts the time from when the participant reached the page,
so reloading the page does not restart the countdown. The time
includes any wait before the participant can answer, such as audio
that must finish before the Next button is enabled, so allow for it in
"time_limit". The server also treats an answer that arrives more than
5 seconds after the limit as timed out, so a tampered or stalled page
cannot take longer: it records "time_limit_answer", or with
"CURRENT_ANSWER" the late answer itself, unvalidated and with
"time_limit_reached" set. A timed page cannot have a "session_id". To
place the countdown yourself, put an element with the attribute "data-
psynet-seconds-left" in the page's content, for example "<p>Answer
within <span data-psynet-seconds-left></span> seconds.</p>".

Answers given because of the time limit skip validation, and their
"time_limit_reached" is "True". A later page maker or code block can
check the previous page with
"participant.response.time_limit_reached".

In a group experiment, a time limit covers only its page. A following
"GroupBarrier" with "late_after" waits for a member who has started a
timed page since the group's previous barrier until the page's time
limit has run out plus 10 seconds, which is enough for an answer sent
at the deadline to arrive. This also holds once they have answered and
moved on to untimed pages, so finishing early never makes a member
late. So give decision pages a "time_limit", and the barrier after
them "late_after" rather than a long "max_wait_time"; see Moving slow
players on for a worked example.

To keep what the participant has entered instead of a fixed answer,
pass "CURRENT_ANSWER":

   from psynet.timeline import CURRENT_ANSWER

   ModularPage(
       "story",
       "Write a short story.",
       TextControl(),
       time_limit=120,
       time_limit_answer=CURRENT_ANSWER,
       time_estimate=120,
   )

The page then submits its controls' current answer, for example a
half-written text, which is formatted as usual but not validated. If
the page has no answer to submit, "None" is recorded. Just before a
timed page submits itself, the browser dispatches a
"psynet:timeLimitReached" event on "document", so custom JavaScript
can stage its state first:

   document.addEventListener("psynet:timeLimitReached", () => {
       psynet.response.staged.rawAnswer = currentDrawing();
   });

With "CURRENT_ANSWER", a page with a built-in control submits that
control's answer; other pages submit "psynet.response.staged".


Time estimates
==============

* Pages and page makers take "time_estimate" in seconds.

* A trial maker's estimate is the trial class's "time_estimate"
  multiplied by the trial maker's "expected_trials_per_participant".

* "while_loop()" multiplies the estimate of its logic by
  "expected_repetitions".

* "for_loop()" multiplies "time_estimate_per_iteration" by
  "expected_repetitions".


Running code before the experiment launches
===========================================

A "PreDeployRoutine" runs a function once, on the machine that
launches the experiment, before the experiment starts. It takes a
label, the function, and a dictionary of keyword arguments for the
function. It can go anywhere in the timeline, any number of times, and
shows nothing to participants. This one configures an Amazon S3
bucket:

   from psynet.media import setup_bucket_for_presigned_urls
   from psynet.timeline import PreDeployRoutine

   PreDeployRoutine(
       "setup_bucket_for_presigned_urls",
       setup_bucket_for_presigned_urls,
       {"bucket_name": "recordings-s3-bucket", "public_read": True},
   )

The function can also take an "experiment" argument. Database changes
it makes are carried into the launched experiment, so it suits
database setup tasks. Assets it deposits count as prepared before
launch and are left out of "psynet export".

See also: Timeline in the API reference.
