Writing a chain experiment
**************************

The examples on this page come from
"demos/experiments/chain_trial_maker", a serial-reproduction task in
which each participant retells the previous participant's story. To
run it from a PsyNet source checkout:

   cd demos/experiments/chain_trial_maker
   psynet debug local


How a chain grows
=================

Start nodes are built in a function, like the nodes of a static
experiment:

   def get_start_nodes():
       return [
           CustomChainNode(
               definition={
                   "story": story,
               }
           )
           for story in STORIES
       ]

The rule for making the next node is the node class's
"make_next_definition()" method. PsyNet calls it on the current node
once that node has "trials_per_node" usable trials, and uses the
returned dictionary as the next node's definition. In the demo, the
next story is the previous participant's retelling:

   class CustomChainNode(ChainNode):
       def make_next_definition(self, experiment, participant):
           return {"story": self.completed_and_processed_trials[0].answer}

Inside the method:

* "self.definition" is the current node's definition;

* "self.completed_and_processed_trials" lists the trials to build on:
  those that are complete, finalized (so any recording analysis has
  finished), and not failed, excluding repeat trials;

* "self.degree" is the current node's position in the chain, starting
  at 0; the new node will have degree "self.degree + 1";

* "participant" is the participant whose trial completed the node.

With several trials per node, combine their answers, and carry forward
any fields the next node still needs:

   import statistics

   class RatingNode(ChainNode):
       def make_next_definition(self, experiment, participant):
           return {
               "question": self.definition["question"],
               "mean_rating": statistics.mean(
                   float(t.answer) for t in self.completed_and_processed_trials
               ),
           }

The trial is a subclass of "ChainTrial" and works like a static trial:

   class CustomTrial(ChainTrial):
       time_estimate = 60

       def show_trial(self, experiment, participant):
           return join(
               InfoPage(
                   f"""
                   Read the following story carefully:

                   {self.definition["story"]}
                   """,
               ),
               ModularPage(
                   "recall_story",
                   "Now recall the story in your own words.",
                   TextControl(),
               ),
           )

If a node needs new media, for example a sound synthesized from the
previous participant's answer, override the node's "async_on_deploy"
method and call "self.add_assets" there. It runs in the background
after the node is created, and participants cannot visit the node
until it finishes.


Within and across participants
==============================

The "ChainTrialMaker" goes in the timeline:

   def get_timeline():
       return Timeline(
           ChainTrialMaker(
               id_="stories",
               trial_class=CustomTrial,
               node_class=CustomChainNode,
               chain_type="across",
               start_nodes=get_start_nodes,
               expected_trials_per_participant="n_start_nodes",
               max_nodes_per_chain=10,
               recruit_mode="n_trials",
           )
       )

"chain_type" is ""across"" or ""within"". For within-participant
chains, "start_nodes" must be a function that creates fresh nodes; it
may take a "participant" argument. Without "start_nodes", set
"chains_per_experiment" (across) or "chains_per_participant" (within).


Choosing the next chain
=======================

* "allow_revisiting_networks_in_across_chains" (default "False") lets
  participants return to a chain they have already contributed to.

* "block_order" and "chain_order" set the order of blocks and of
  chains within a block. They work like "block_order" and "node_order"
  in Trial order, except that "chain_order" defaults to ""random"",
  ""balanced"" favours the shortest chains and then the heads with the
  fewest trials, and ""listed"" follows the order of the start nodes.
  In within-participant chains, where participants can have different
  blocks, a "block_order" list skips blocks the participant does not
  have. A "chain_order" function takes any of "participant",
  "experiment", "block" and "chains", by name. Each chain is a
  "ChainNetwork" in start-node order; its start node's "context" is
  "chain.context" and its current node is "chain.head". For example,
  to run each chain to the end, easiest first (see "interleave_chains"
  below):

     chain_order=lambda chains: sorted(chains, key=lambda chain: chain.context["difficulty"]),
     interleave_chains=False,

  With a planned order, the participant takes the planned chains in
  turn, one trial each, skipping any that are busy, until none can
  give them another trial. If every remaining planned chain is busy at
  once, the participant waits with "wait_for_networks=True"; otherwise
  the block ends. Create-and-rate trial makers support only
  ""balanced"" and ""random"".

* "interleave_chains" (default "True"; "False" for
  "GeometricStaircaseTrialMaker"). With "False", the participant stays
  on one chain until it can give them no more trials, then moves to
  the next chain in "chain_order". While their chain is busy they
  wait, and like any wait for a trial this fails them after
  "max_time_waiting_for_trial" seconds (default 60). It needs
  "chain_type="within"" or
  "allow_revisiting_networks_in_across_chains=True".

* "wait_for_networks" (default "False") decides what happens when
  every chain the participant could take in the current block is busy,
  either waiting on asynchronous processing or on other participants'
  unfinished trials. With "True" the participant waits; with "False"
  they finish the block early and move to the next one, or leave the
  trial maker after the last block.

* Participant groups work as in Writing a static trial maker: set
  "participant_group" on the start nodes and pass
  "choose_participant_group".

* To choose the chain yourself, override "custom_chain_filter()",
  which removes chains the participant must not receive, and
  "select_chain()", which picks one of the rest. They follow the same
  rules as the static trial maker's node hooks (see choosing nodes
  yourself); PsyNet then gives the participant the selected chain's
  current node. With many chains, filter and rank them in the database
  with "filter_chains_query()" and "chain_priority()" instead (see
  Keeping selection fast).


Chain length and trials per node
================================

* "trials_per_node" (default "1"): responses a node needs before the
  next node is made. Unlike a static "target_trials_per_node", this is
  a hard limit: once a node's trials, including those in progress,
  reach it, no other participant is given that node, even if they ask
  at the same moment.

* "max_nodes_per_chain": the chain is full after this many nodes.

* "expected_trials_per_participant" and "max_trials_per_participant":
  an integer, or ""n_start_nodes"".

* "recruit_mode": ""n_participants"" with "target_n_participants"
  recruits until that many participants finish; ""n_trials"" recruits
  until every chain is full. The default, "None", leaves recruitment
  to the rest of the experiment.


Built-in paradigms
==================

* "ImitationChainTrialMaker", with "AudioImitationChainTrialMaker" and
  "CameraImitationChainTrialMaker" for recorded reproductions;

* "GibbsTrialMaker" and "AudioGibbsTrialMaker" (with image, HTML, and
  video variants);

* "MCMCPTrialMaker";

* "GeometricStaircaseTrialMaker";

* the create-and-rate mixins in "psynet.trial.create_and_rate" (see
  Create and rate);

* "GraphChainTrialMaker".

The "demos/experiments" folder has a demo for each, for example
"imitation_chain", "gibbs", "mcmcp", "staircase_pitch_discrimination",
"create_and_rate", and "graph".

A "GeometricStaircaseTrialMaker" runs within-participant staircases.
Subclass "GeometricStaircaseNode" to set "k" (the number of
consecutive correct answers needed before the task gets harder;
default "2") and define the "increase_difficulty" and
"decrease_difficulty" methods; the "staircase_pitch_discrimination"
demo multiplies or divides the parameter by a fixed "step". The trial
maker takes "max_nodes_per_chain" and optionally
"max_reversals_per_chain", where a reversal is a node at which the
difficulty changes direction. At the end, it scores each staircase as
the mean parameter at its reversals and passes or fails the
participant against "min_passing_score" and "max_passing_score" if you
set them.


Writing a Gibbs Sampling with People experiment
-----------------------------------------------

A Gibbs experiment subclasses three classes from "psynet.trial.gibbs":

1. A "GibbsNode" subclass sets "vector_length", the number of stimulus
   dimensions, and defines "random_sample(i)", which returns a random
   value for dimension "i":

      class CustomNode(GibbsNode):
          vector_length = 3

          def random_sample(self, i):
              return random.randint(0, 255)

   Start nodes can carry a "context", which stays fixed within a
   chain, such as the target word, and a "participant_group". For
   within-participant chains, "start_nodes" is a function that creates
   fresh nodes.

2. A "GibbsTrial" subclass defines "show_trial". The page shows the
   stimulus for "self.initial_vector", lets the participant change
   dimension "self.active_index", and returns the new value of that
   dimension as the answer. "self.context" holds the chain's fixed
   parameters:

      def show_trial(self, experiment, participant):
          target = self.context["target"]
          prompt = Markup(
              "<h3 id='participant-group'>Participant group = {}</h3>"
              "<p>Adjust the slider to match the following word as well as possible: "
              "<strong>{}</strong></p>"
          ).format(participant.module_state.participant_group, target)
          page = ColorSliderPage(
              "color_trial",
              prompt,
              starting_values=self.initial_vector,
              selected_idx=self.active_index,
              reverse_scale=self.reverse_scale,
              directional=False,
              time_estimate=5,
          )
          return [
              page,
              # You can also include code blocks within a trial.
              # This one doesn't do anything useful, it's just there for demonstration purposes.
              CodeBlock(lambda participant: participant.var.set("test_variable", 123)),
          ]

   "show_trial" can return a list of pages and code blocks; the answer
   then comes from the last page.

3. A "GibbsTrialMaker" combines the node and trial classes and goes in
   the timeline.


When a trial fails
==================

When a participant leaves early, PsyNet fails their incomplete trials
and keeps their completed ones.
"fail_trials_on_participant_performance_check" defaults to "False" for
chains, so completed trials are also kept when a participant fails a
check. "propagate_failure" (default "True") fails the nodes that a
failed, finalized trial helped to create. See Handling failed
participants and trials.


Where the data goes
===================

Nodes, trials, and chains are database rows:

   CustomChainNode.query.filter_by(trial_maker_id="stories").order_by(CustomChainNode.degree)
   node.network      # the chain
   node.child        # the next node, if any
   node.all_trials

See also:

  Chain in the API reference.

  The Make an experiment adaptive skill describes how to plan,
  simulate and check an adaptive chain.
