"""First-look analysis for the three research-kit studies.

    python analyze.py comfort-ride  summary.csv [trials.csv]
    python analyze.py distance      summary.csv trials.csv
    python analyze.py procedure     summary.csv [trials.csv]

Make the CSV files with tools/merge.html (it leaves out test-mode and withdrawn sessions). Needs pandas; uses scipy and
statsmodels when installed (pip install pandas scipy statsmodels). This is a starting point that matches the
preregistration templates in research/<study>/README.md, not a substitute for the analysis plan: decide the tests,
exclusions and sample size before collecting data.
"""
import sys

import pandas as pd

try:
    from scipy import stats
except ImportError:  # descriptive statistics still work
    stats = None
try:
    import statsmodels.formula.api as smf
except ImportError:
    smf = None


def describe(df, by, cols):
    cols = [c for c in cols if c in df.columns]
    print(df.groupby(by)[cols].agg(['count', 'mean', 'std', 'median']).round(2).to_string(), '\n')


def compare(df, by, col, label):
    groups = [g[col].dropna() for _, g in df.groupby(by)]
    groups = [g for g in groups if len(g) > 1]
    if stats is None or len(groups) < 2:
        return
    if len(groups) == 2:
        u = stats.mannwhitneyu(*groups, alternative='two-sided')
        print(f'{label}: Mann-Whitney U = {u.statistic:.1f}, p = {u.pvalue:.4f}')
    else:
        h = stats.kruskal(*groups)
        print(f'{label}: Kruskal-Wallis H = {h.statistic:.2f}, p = {h.pvalue:.4f}')


def comfort_ride(s, t):
    print('== Comfort Ride: cybersickness by comfort aid ==\n')
    s = s[s['phase'] == 'main']
    s['ssq_change'] = s.get('ssq_post_score_total') - s.get('ssq_pre_score_total')
    describe(s, 'condition', ['ssq_pre_score_total', 'ssq_post_score_total', 'ssq_change', 'fms_max', 'fms_mean', 'ride_seconds', 'ipq_score_sp', 'perf_fps_mean'])
    compare(s, 'condition', 'ssq_change', 'SSQ total change')
    compare(s, 'condition', 'fms_max', 'FMS maximum')
    print('\nStopped before the end, by condition:')
    print(pd.crosstab(s['condition'], s['ride_completed']).to_string(), '\n')
    if stats is not None and s['ride_completed'].nunique() > 1:
        chi = stats.chi2_contingency(pd.crosstab(s['condition'], s['ride_completed']))
        print(f'Chi-square for completion: {chi[0]:.2f}, p = {chi[1]:.4f} (use a survival analysis of ride_seconds for the paper)')
    if 'mode' in s:
        print('\nBy device mode (VR vs screen is a covariate here, not a manipulation):')
        describe(s, ['mode', 'condition'], ['ssq_change', 'fms_max'])
    if t is not None and len(t):
        f = t[(t['kind'] == 'fms') & (t['missed'].astype(str).str.lower() != 'true')]
        if len(f):
            f = f.assign(minute=(f['ride_s'].astype(float) // 60 + 1).astype(int))
            print('Mean FMS by minute and condition:')
            print(f.pivot_table(index='minute', columns='condition', values='value', aggfunc='mean').round(2).to_string())


def distance(s, t):
    print('== How Far: distance estimates ==\n')
    t = t[t['kind'] == 'trial'].copy()
    t['shadow'] = t['shadow'].astype(str).str.lower() == 'true'
    print('Mean ratio (estimate / true distance) by mode, ground and shadow (1.0 = accurate, < 1 = underestimate):')
    print(t.pivot_table(index=['mode', 'condition'], columns='shadow', values='ratio', aggfunc='mean').round(3).to_string(), '\n')
    print('By nominal distance:')
    print(t.pivot_table(index='distance_nominal', columns='mode', values='ratio', aggfunc='mean').round(3).to_string(), '\n')
    if smf is not None and t['pid'].nunique() > 2:
        try:
            m = smf.mixedlm('ratio ~ C(shadow) * C(mode) + distance_nominal + C(condition)', t, groups=t['pid']).fit()
            print(m.summary())
        except Exception as e:  # small or unbalanced pilot data
            print('Mixed model not fitted:', e)
    moved = t.groupby('pid')['head_moved_m'].max()
    print(f'\nParticipants whose head moved more than 0.5 m in a trial (check the instructions were followed): {int((moved > 0.5).sum())}')


def procedure(s, t):
    print('== Pump Start-up: guidance during training and recall ==\n')
    m = s[s['phase'] == 'main']
    describe(m, 'condition', ['training_errors', 'training_ms', 'test_steps_in_order', 'test_errors', 'test_ms', 'tlx_score_raw_tlx', 'sus_score_sus', 'ipq_score_sp'])
    compare(m, 'condition', 'training_errors', 'Training errors')
    compare(m, 'condition', 'test_steps_in_order', 'Test: steps done in order')
    compare(m, 'condition', 'test_errors', 'Test errors')
    r = s[s['phase'] == 'retention']
    if len(r):
        print('\nDelayed retention:')
        describe(r, 'condition', ['test_steps_in_order', 'test_errors', 'test_ms'])
        compare(r, 'condition', 'test_steps_in_order', 'Retention: steps in order')


if __name__ == '__main__':
    if len(sys.argv) < 3:
        print(__doc__)
        sys.exit(1)
    study, summary = sys.argv[1], pd.read_csv(sys.argv[2])
    trials = None
    if len(sys.argv) > 3:
        try:
            trials = pd.read_csv(sys.argv[3])
        except pd.errors.EmptyDataError:  # a study run with no trial rows
            trials = None
    for col in ('test_mode', 'withdrawn'):
        if col in summary:
            summary = summary[summary[col].astype(str).str.lower() != 'true']
    {'comfort-ride': comfort_ride, 'distance': distance, 'procedure': procedure}[study](summary, trials)
