Create and rate
***************

Create-and-rate trials for collecting and evaluating participant
creations.

The trial-maker mixin specializes chain selection: each chain head
first receives creator trials, waits for those trials to finalize, and
then receives rater trials. Selection therefore operates on chains,
while trial-class resolution remains node-based after the selected
chain is resolved to its head. Experiments may optionally assign fixed
participant roles; the mixin then uses the same role for candidate
eligibility and final trial-class validation. Creators only receive
heads that still need creators. Raters receive heads that are ready
for raters, and they wait (or finish the block) on heads whose creator
slots are filled but not yet finalized. Heads that still need creators
are not rater-eligible. Because roles follow each chain's creation
phase, create-and-rate supports only the dynamic ""balanced"" and
""random"" chain orders.

class psynet.trial.create_and_rate.CreateTrialMixin(experiment, node, participant, *args, **kwargs)

   Bases: "CreateAndRateTrialMixin"

   Mixin for creator trials. List it before a "ChainTrial" subclass.

   Example: "class CreateTrial(CreateTrialMixin,
   ImitationChainTrial)".

class psynet.trial.create_and_rate.RateOrSelectTrialMixin(experiment, node, participant, *args, **kwargs)

   Bases: "CreateAndRateTrialMixin"

   Base mixin for trials that evaluate creations; see "RateTrialMixin"
   and "SelectTrialMixin".

   On construction, the trial stores its evaluation targets in
   "targets". Candidates are the node's finalized, non-failed creator
   trials, plus the node itself when the trial maker sets
   "include_previous_iteration=True". Use "isinstance(trial,
   RateOrSelectTrialMixin)" to tell evaluation trials apart from
   creator trials.

class psynet.trial.create_and_rate.RateTrialMixin(experiment, node, participant, *args, **kwargs)

   Bases: "RateOrSelectTrialMixin"

   Mixin for trials where the participant gives numeric ratings; used
   with "rate_mode="rate"".

   With "target_selection_method="one"", each trial rates the target
   with the fewest ratings so far; with ""all"", it rates every target
   and the answer is a list of numbers.

class psynet.trial.create_and_rate.SelectTrialMixin(experiment, node, participant, *args, **kwargs)

   Bases: "RateOrSelectTrialMixin"

   Mixin for trials where the participant picks one target; used with
   "rate_mode="select"".

   The answer must be the string form of one of "targets".

class psynet.trial.create_and_rate.CreateAndRateNodeMixin(**kwargs)

   Bases: "object"

   Mixin that adds create-and-rate aggregation to a "ChainNode"
   subclass.

   List it before the node class, e.g. "class
   MyNode(CreateAndRateNodeMixin, ImitationChainNode)".
   "summarize_trials()" returns the winning target: the one with the
   highest mean rating ("rate_mode="rate"") or the most selections
   ("rate_mode="select"").

   summarize_trials(trials, experiment, participant)

      Return the winning target from this node's finalized rater or
      selector trials.

class psynet.trial.create_and_rate.CreateAndRateNode(*args, **kwargs)

   Bases: "CreateAndRateNodeMixin", "ChainNode"

   Ready-made create-and-rate node whose definition is its seed
   (default "{}").

   create_definition_from_seed(seed, experiment, participant)

      Creates a node definition from a seed. The seed comes from the
      previous node in the chain. In many cases (e.g. iterated
      reproduction) the definition will be trivially equal to the
      seed, but in some cases we may introduce some kind of stochastic
      alteration to produce the definition.

      Parameters:
         * **seed** (*object*) -- The seed, passed from the previous
           state in the chain.

         * **experiment** -- An instantiation of
           "psynet.experiment.Experiment", corresponding to the
           current experiment.

         * **participant** -- The participant who initiated the
           creation of the node.

      Returns:
         * *object* -- The derived definition. Should be suitable for
           serialisation to JSON.

   create_initial_seed(experiment, participant)

      Return the seed of a chain's first node when no definition was
      given.

   creation_time

      the time at which the Network was created.

   details

      a generic column for storing structured JSON data

   failed

      boolean indicating whether the Network has failed which prompts
      Dallinger to ignore it unless specified otherwise. Objects are
      usually failed to indicate something has gone wrong.

   failed_reason

      an optional reason the object was failed. If the object is
      failed as part of a cascading failure triggered from another
      object, the chain of objects will be captured in this field.

   id

      a unique number for every entry. 1, 2, 3 and so on...

   network

      the network the node is in

   network_id

      the id of the network that this node is a part of

   participant

      the participant the node is associated with

   participant_id

      the id of the participant whose node this is

   property1

      a generic column that can be used to store experiment-specific
      details in String form.

   property2

      a generic column that can be used to store experiment-specific
      details in String form.

   property3

      a generic column that can be used to store experiment-specific
      details in String form.

   property4

      a generic column that can be used to store experiment-specific
      details in String form.

   property5

      a generic column that can be used to store experiment-specific
      details in String form.

   time_of_death

      the time at which failing occurred

   type

      A String giving the name of the class. Defaults to "node". This
      allows subclassing.

   vars

class psynet.trial.create_and_rate.CreateAndRateTrialMakerMixin(**kwargs)

   Bases: "object"

   Mixin that turns a chain trial maker into a create-and-rate trial
   maker.

   List it before the trial maker class, e.g. "class
   MyTrialMaker(CreateAndRateTrialMakerMixin,
   ImitationChainTrialMaker)". Pass the arguments below alongside the
   usual trial maker arguments; "creator_class" is used as the trial
   maker's "trial_class".

   Parameters:
      * **n_creators** (*int*) -- Creator trials per node.

      * **n_raters** (*int*) -- Rater or selector trials per node.

      * **node_class** (*class*) -- Node class, which must include
        "CreateAndRateNodeMixin".

      * **creator_class** (*class*) -- Creator trial class, which must
        include "CreateTrialMixin".

      * **rater_class** (*class*) -- Evaluation trial class, which
        must include "RateTrialMixin" ("rate_mode="rate"") or
        "SelectTrialMixin" ("rate_mode="select"").

      * **start_nodes** (*list*) -- Initial nodes, one per chain.

      * **rate_mode** (*str*) -- ""rate"" or ""select"".

      * **include_previous_iteration** (*bool*) -- If "True", the
        current node is also a target; start nodes then need a non-
        empty seed.

      * **target_selection_method** (*str*) -- ""one"" (one target per
        rater trial) or ""all"" (every target per trial).

      * **randomize_target_order** (*bool*) -- Shuffle the order in
        which targets are presented.

      * **verbose** (*bool*) -- Log the ratings behind each target
        selection.

   get_creation_phases(nodes)

      Classify nodes using a constant number of database queries.

      Parameters:
         **nodes** -- Create-and-rate nodes to classify.

      Returns:
         A mapping from node ID to "NEEDS_CREATORS",
         "WAITING_FOR_CREATORS", or "READY_FOR_RATERS".

      Return type:
         dict

   get_finished_creations(node)

      Return the finalized, non-failed creator trials for a node.

   get_participant_role(participant, experiment)

      Return a fixed create-and-rate role, or "None" for phase-driven
      roles.

      Override this when participants must remain creators or raters.
      The returned role controls both chain eligibility and final
      trial-class validation. Creators are eligible only for
      "NEEDS_CREATORS" heads. Raters are eligible for
      "READY_FOR_RATERS" heads and for "WAITING_FOR_CREATORS" heads
      (so assignment can wait or exit until those heads become
      ratable). "NEEDS_CREATORS" heads are not rater-eligible.

      Returns:
         "CREATOR_ROLE", "RATER_ROLE", or "None".

      Return type:
         str or None

class psynet.trial.create_and_rate.CreateAndRateAssignmentPending(outcome)

   Bases: "Exception"

   Raised when a selected head cannot receive a trial yet.

   Parameters:
      **outcome** -- ""wait"" or ""exit"", matching
      "wait_for_networks".
