Analyzing data
**************


In the dashboard
================

The dashboard's **Database** tab shows each type of database object in
its own table. You can act on selected objects there, for example
marking them as failed. The **Basic data** tab previews the
experiment's basic data (see Basic data).


Checking data during collection
===============================

Keep an analysis script (for example "export.py") in the experiment
directory and run it on every export. It should:

* check that participants progress through the whole experiment and
  complete the expected number of trials, using assertions so that
  problems stop the script;

* check the time estimates, for example with a histogram of trial
  durations;

* extract the demographic information you need, such as age and
  gender;

* convert the raw tables into the format you analyze, for example a
  CSV for R or MATLAB;

* plot the main results, so that unexpected effects show up early.

Running it after the first batch of participants lets you stop a
broken experiment early. For quick checks without a full export, read
basic data from the dashboard or the "/basic_data" endpoint.


Reading the tables
==================

"psynet.export" has helpers for reading the exported tables:

   from psynet.export import (
       load_export_table,
       merge_participant_identifiers,
       unpack_json_column,
   )

   trials = load_export_table("exports/latest", "trial")
   # Or the zip downloaded from the dashboard: load_export_table("export.zip", "trial")
   trials = unpack_json_column(trials, "definition", prefix="definition_")
   participants = load_export_table("exports/latest", "participant")
   participants = merge_participant_identifiers(
       participants.rename(columns={"id": "participant_id"}),
       "exports/latest/participant_identifiers.csv",
   )

"psynet export" leaves out tables with no rows, and
"load_export_table" raises "KeyError" for a table whose file is
missing. Tables that only some experiments fill, such as the group
tables below, may therefore be absent; "manifest.json" lists the row
count of each table in "table_row_counts".

An unfinished trial, abandoned or still running when the export was
made, can have an "answer": the answer given so far, before
"format_answer" has run. So filter trials on "complete" as well as
"failed" before analysing their answers:

   finished = trials[trials["complete"] & ~trials["failed"]]

In group experiments, each synchronized trial records its group and
round in "sync_group_id" and "sync_group_round" (see Group
experiments). This example pairs each trial with the trials of the
other group members in the same round:

   trials = load_export_table("exports/latest", "trial")
   trials = trials[
       trials["sync_group_id"].notna() & trials["complete"] & ~trials["failed"]
   ]

   round_ = ["sync_group_id", "sync_group_round"]
   partners = trials[round_ + ["participant_id", "answer"]].rename(
       columns={"participant_id": "partner_id", "answer": "partner_answer"}
   )
   player_rounds = trials.merge(partners, on=round_)
   player_rounds = player_rounds[
       player_rounds["participant_id"] != player_rounds["partner_id"]
   ]

A player who timed out has no answer of their own: in the
"rock_paper_scissors" demo their "answer" is "None". The move actually
played, including the random one given to a player who timed out, is
in each trial's vars ("action"), as the demo's basic data shows (see
Group experiments).

Group details such as "group_type" are in the "sync_group" table, and
each member's role and removal reason in
"participant_link_sync_group". The group's results for each round are
in the "sync_group_round" table: each trial's "sync_group_round_id"
points to its row, whose "sync_group_id" and "number" match the
trial's "sync_group_id" and "sync_group_round".


Loading an export into a local database
=======================================

"psynet load exports/latest" replaces the local database with an
export, so you can query it directly. It takes the folder that "psynet
export" writes or a zip, such as the one the dashboard's **Export**
tab downloads ("psynet load export.zip"). Stop "psynet debug" and any
other client using the same database role first: PsyNet refuses to
drop tables while another client using that role is connected.
