Pre-Processing


In [14]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import glob

In [28]:
def load_csvs(subject_id):
    temp_events = "study_data/s%d_events%s.csv"
    temp_gamestatus = "study_data/s%d_gamestatus%s.csv"
    
    events = []
    gamestatus = []
    split_counter = 0
    split = ""

    while True:
        if split_counter > 0:
            split = "_%d" % split_counter
        try:
            ev_tmp = pd.read_csv(temp_events % (subject_id, split), sep=';')
            gs_tmp = pd.read_csv(temp_gamestatus % (subject_id, split), sep=';')
            ev_tmp['SubjectId'] = subject_id
            gs_tmp['SubjectId'] = subject_id
            ev_tmp['SplitCount'] = split_counter
            gs_tmp['SplitCount'] = split_counter
            events.append(ev_tmp)
            gamestatus.append(gs_tmp)
            
        except FileNotFoundError:
            break
            
        split_counter += 1
        
    return pd.concat(events).reset_index(), pd.concat(gamestatus).reset_index()

In [68]:
def move_custom_data(re, rg):
    rg["CamContXPos"] = pd.Series(np.array(np.full(rg.size, 999), dtype='int16'))
    rg["CamContYPos"] = pd.Series(np.array(np.full(rg.size, 999), dtype='int16'))
    rg["CamContZPos"] = pd.Series(np.array(np.full(rg.size, 999), dtype='int16'))
        
    for index, re_row in re.copy().iterrows():
        if re_row.TaskType == 'Custom':
            rg_split_idx = rg.index[rg.SplitCount == re_row.SplitCount]
            cam_cont_pos = re_row.TaskPos.strip('() ').split(',')
            rg.loc[rg_split_idx, 'CamContXPos'] = cam_cont_pos[0]
            rg.loc[rg_split_idx, 'CamContYPos'] = cam_cont_pos[1]
            rg.loc[rg_split_idx, 'CamContZPos'] = cam_cont_pos[2]
            re.drop(index, inplace=True)
            continue
            
    return re, rg

In [59]:
def add_rc_count_to_events(re, rg):
    re['RepeatCount'] = pd.Series(np.array(np.zeros(re.size), dtype='uint8'))
    
    last_time = 0
    last_round_type = re.iloc[0].RoundType
    last_split_count = 0
    repeat_count = 0
    start_index = 0
    
    for index, re_row in re.iterrows():
        if (re_row.SplitCount != last_split_count) | (re_row.RoundType != last_round_type) | (index == re.index[-1]):
            re.loc[start_index : index, 'RepeatCount'] = repeat_count
            
            sc_rt_rg_mask = (rg.SplitCount == last_split_count) & (rg.RoundType == last_round_type)
            sc_rt_re_mask = (re.SplitCount == last_split_count) & (re.RoundType == last_round_type)

            last_rc_in_rg = rg.loc[(rg.Timestamp <= last_time) & sc_rt_rg_mask].RepeatCount.max()
            # print (last_round_type, "sc", last_split_count, "time", last_time, "rc", repeat_count, "lrc", last_rc_in_rg)

            if repeat_count < last_rc_in_rg:
                for rc in range(repeat_count+1, last_rc_in_rg+1):
                    rc_round = rg.loc[(rg.RepeatCount == rc) & sc_rt_rg_mask]
                    rc_start = rc_round.iloc[0].Timestamp
                    rc_end = rc_round.iloc[-1].Timestamp
                    
                    # print (last_round_type, repeat_count, last_rc_in_rg, rc_start, rc_end)
                    re.loc[(re.Timestamp >= rc_start) & (re.Timestamp <= rc_end) & sc_rt_re_mask, 'RepeatCount'] = rc
                 
            start_index = index
            repeat_count = 0
            last_round_type = re_row.RoundType
            last_split_count = re_row.SplitCount
            
        elif (re_row.Timestamp < last_time):
            re.loc[start_index : index, 'RepeatCount'] = repeat_count
            start_index = index
            repeat_count += 1
            
        last_time = re_row.Timestamp
        
    return re, rg

In [69]:
def create_rounds_df(re, rg):
    round_columns = ['SubjectId', 'Round', 'RoundType', 'Trial', 'RepeatCount', 'SplitCount', 'ValidTrial', 'StartTime', 'EndTime', 'Duration']
    round_data = []
    
    re["Trial"] = pd.Series(np.array(np.zeros(re.size), dtype='uint8'))
    re["ValidTrial"] = pd.Series([], dtype=bool)
    rg["Trial"] = pd.Series(np.array(np.empty(re.size), dtype='uint8'))
    rg["ValidTrial"] = pd.Series([], dtype=bool)
    
    # iterate over all available rounds
    for r in rg.Round.unique():
        trialNum = 0
        rg_round = rg.loc[rg.Round == r]
        round_type = rg_round.iloc[0].RoundType
        
        re_round_idx = re.index[(re.RoundType == round_type)]
        re.loc[re_round_idx, 'RoundType'] = round_type
        
        for sc in rg_round.SplitCount.unique():
            rg_split = rg_round.loc[rg_round.SplitCount == sc]
            
            for rc in rg_split.RepeatCount.unique():
                rg_trial = rg_split.loc[rg_split.RepeatCount == rc]
                
                trial_start = rg_trial.iloc[0].Timestamp
                trial_end = rg_trial.iloc[-1].Timestamp
                trial_dur = trial_end - trial_start
                
                rg_trial_idx = rg.index[(rg.Round == r) & (rg.SplitCount == sc) & (rg.RepeatCount == rc)]
                re_trial_idx = re.index[(re.RoundType == round_type) & (re.SplitCount == sc) & (re.RepeatCount == rc)]
                
                if trial_dur <= 15:
                    rg.drop(rg_trial_idx, inplace=True)
                    re.drop(re_trial_idx, inplace=True)
                    continue
                
                re_trial = re.loc[re_trial_idx]
                rings = re_trial.loc[(re_trial.TaskType == 'Ring') & (re_trial.TaskStatus != 'visible')].EventId.unique().size
                povs = re_trial.loc[(re_trial.TaskType == 'POV') & (re_trial.TaskStatus != 'visible')].EventId.unique().size
                
                valid_trial = False

                if (round_type == 'Training_Ring_Only') & (rings == 20):
                        valid_trial = True
                elif (round_type != 'Training_Ring_Only') & (povs == 9):
                        valid_trial = True
                
                round_data.append({
                     'SubjectId': rg_round.iloc[0].SubjectId, 'Round': r, 'RoundType': round_type,
                     'Trial': trialNum, 'RepeatCount': rc, 'SplitCount': sc,
                     'ValidTrial': valid_trial, 'Duration': trial_dur})
                
                rg.loc[rg_trial_idx, 'Trial'] = trialNum
                rg.loc[rg_trial_idx, 'ValidTrial'] = valid_trial
                
                re.loc[re_trial_idx, 'Trial'] = trialNum
                re.loc[re_trial_idx, 'ValidTrial'] = valid_trial
                
                trialNum += 1
                
    return pd.DataFrame(data=round_data, columns=round_columns), re, rg

In [52]:
def process_events(re, rg):
    re["EndTime"] = pd.Series([], dtype=float)
    re["Duration"] = pd.Series([], dtype=float)
    re["Round"] = pd.Series(np.array(np.zeros(re.size), dtype="uint8"))

    for index, re_row in re.copy().iterrows():
        if re_row.TaskStatus == 'visible':
            started = re_row.Timestamp
            rg_info = rg.loc[(rg.RoundType == re_row.RoundType) & (rg.Trial == re_row.Trial)].iloc[0]
            re.loc[index,'Round'] = rg_info.Round

            is_corresponding_event = (re.EventId == re_row.EventId) & (re.TaskStatus != 'visible')
            ce_idx = re.index[is_corresponding_event]
            
            if ce_idx.size > 0:
                corresponding_event = re.loc[ce_idx].iloc[0]
                finished = corresponding_event.Timestamp
                duration = finished - started
                status = corresponding_event.TaskStatus
                re.drop(ce_idx, inplace=True)
            else:
                print('unfinshed event')  
                finished = np.nan
                duration = np.nan
                status = 'unfinished'
            
            re.loc[index,'EndTime'] = finished
            re.loc[index, 'Duration'] = duration
            re.loc[index, 'TaskStatus'] = status
                
    re = re.rename(columns = {'Timestamp': 'StartTime'})

    return re, rg

In [92]:
def process_gamestatus(rg):
    # round pos and rot cols
    rcols = ['PlayerXPos', 'PlayerYPos', 'PlayerZPos', 'MainCamXPos', 'MainCamYPos', 'MainCamZPos', 'PlayerXRot', 'PlayerYRot', 'PlayerZRot', 'MainCamXRot', 'MainCamYRot', 'MainCamZRot']
    rg[rcols] = rg[rcols].round(3)
    
    rg['MainCamXRotRel'] = rg['MainCamXRot'] - rg['PlayerXRot']
    rg['MainCamYRotRel'] = rg['MainCamYRot'] - rg['PlayerYRot']
    rg['MainCamZRotRel'] = rg['MainCamZRot'] - rg['PlayerZRot']
    
    rg['MainCamXRotRelNorm'] = rg.apply(lambda row: ((row.MainCamXRotRel - 180) % 360 - 180), axis=1)
    rg['MainCamYRotRelNorm'] = rg.apply(lambda row: ((row.MainCamYRotRel - 180) % 360 - 180), axis=1)
    rg['MainCamZRotRelNorm'] = rg.apply(lambda row: ((row.MainCamZRotRel - 180) % 360 - 180), axis=1)
    
    return rg

In [7]:
def fix_timestamps(ev, ga, ro):
    total_dur = 0
    for r in range(ro.Round.max() + 1):
        curr_round = ro.loc[ro.Round == r]
        trials = curr_round.Trial.max() + 1
        
        for t in range(trials):
            trial_dur = curr_round.loc[curr_round.Trial == t].iloc[0].Duration
            trial_mask = (ro.Round == r) & (ro.Trial == t)
            ro.loc[trial_mask, 'StartTime'] = total_dur
            ro.loc[trial_mask, 'EndTime'] = total_dur + trial_dur
            
            ga_trial_mask = (ga.Round == r) & (ga.Trial == t)
            ev_trial_mask = (ev.Round == r) & (ev.Trial == t)
            
            ga_trial_start = ga.loc[ga_trial_mask].iloc[0].Timestamp
            trial_time_offset =  total_dur - ga_trial_start
            
            ga.loc[ga_trial_mask, 'Timestamp'] = ga.loc[ga_trial_mask, 'Timestamp'] + trial_time_offset
            ev.loc[ev_trial_mask, 'StartTime'] = ev.loc[ev_trial_mask, 'StartTime'] + trial_time_offset
            ev.loc[ev_trial_mask, 'EndTime'] = ev.loc[ev_trial_mask, 'EndTime'] + trial_time_offset
            
            total_dur += trial_dur + 0.001
    return ro, ev, ga

In [90]:
def preprocess(re, rg):
    re = re.rename(columns = {"EventInfo" : "TaskPos", "EventType": "TaskType", "EventStatus": 'TaskStatus'})
    
    re, rg = move_custom_data(re, rg)
    re, rg = add_rc_count_to_events(re, rg)
    ro, re, rg = create_rounds_df(re, rg)
    re, rg = process_events(re, rg)
    rg = process_gamestatus(rg)
    ro, re, rg = fix_timestamps(re, rg, ro)
    
    re = re[['SubjectId', 'EventId', 'Round', 'RoundType', 'Trial', 'ValidTrial',
             'TaskType', 'TaskStatus', 'TaskPos', 'Duration', 'StartTime', 'EndTime']]
    
    rg = rg[['SubjectId', 'Timestamp', 'Round', 'Trial',
             'PlayerXPos', 'PlayerYPos', 'PlayerZPos',
             'MainCamXPos', 'MainCamYPos', 'MainCamZPos',
             'CamContXPos', 'CamContYPos', 'CamContZPos',
             'PlayerXRot', 'PlayerYRot', 'PlayerZRot',
             'MainCamXRot', 'MainCamYRot', 'MainCamZRot',
             'MainCamXRotRel', 'MainCamYRotRel', 'MainCamZRotRel',
             'MainCamXRotRelNorm', 'MainCamYRotRelNorm', 'MainCamZRotRelNorm']]
    
    return ro, re, rg

In [86]:
def create_final_csvs(write_csvs=False):
    study_dict = { 'rounds': [], 'events': [], 'gamestatus': [] }
    
    for subject in range(4):
        # check if data for the subject available
        if len(glob.glob('study_data/s%d_*.csv' % subject)) < 2:
            continue
            
        print('Subject #%d' % (subject))
        
        # load all csvs and concatenate splits if available
        raw_events, raw_gamestatus = load_csvs(subject)
        
        # preprocess data
        ro, ev, ga = preprocess(raw_events, raw_gamestatus)
        
        # add to data dict
        study_dict['rounds'].append(ro)
        study_dict['events'].append(ev)
        study_dict['gamestatus'].append(ga)
        
    # clean index
    ro_total = pd.concat(study_dict['rounds']).reset_index()
    ev_total = pd.concat(study_dict['events']).reset_index()
    gs_total = pd.concat(study_dict['gamestatus']).reset_index()
    
    print(gs_total)
    
    if write_csvs:
        ro_total.to_excel('ro_all.xlsx')
        ev_total.to_excel('ev_all.xlsx')
        gs_total.to_excel('gs_all.xlsx')
        
    print('finished')

In [93]:
create_final_csvs(True)


Subject #3
unfinshed event
      index  SubjectId  Timestamp  Round  Trial  PlayerXPos  PlayerYPos  \
0         0          3      0.000      0      0       5.483       3.150   
1         1          3      0.133      0      0       8.283       3.150   
2         2          3      0.268      0      0      11.224       3.150   
3         3          3      0.401      0      0      14.231       3.150   
4         4          3      0.536      0      0      17.309       3.150   
5         5          3      0.669      0      0      20.390       3.150   
6         6          3      0.805      0      0      23.501       3.150   
7         7          3      0.950      0      0      26.817       3.150   
8         8          3      1.094      0      0      30.064       3.150   
9         9          3      1.228      0      0      33.015       3.150   
10       10          3      1.363      0      0      35.927       3.150   
11       11          3      1.496      0      0      38.746       3.150   
12       12          3      1.632      0      0      41.591       3.150   
13       13          3      1.764      0      0      44.335       3.150   
14       14          3      1.900      0      0      47.111       3.150   
15       15          3      2.033      0      0      49.778       3.150   
16       16          3      2.166      0      0      52.437       3.150   
17       17          3      2.314      0      0      55.378       3.150   
18       18          3      2.457      0      0      58.299       3.150   
19       19          3      2.593      0      0      61.139       3.150   
20       20          3      2.724      0      0      63.965       3.150   
21       21          3      2.859      0      0      66.896       3.150   
22       22          3      2.993      0      0      69.835       3.150   
23       23          3      3.127      0      0      72.788       3.150   
24       24          3      3.264      0      0      75.818       3.150   
25       25          3      3.397      0      0      78.766       3.150   
26       26          3      3.529      0      0      81.713       3.150   
27       27          3      3.643      0      0      84.258       3.150   
28       28          3      3.775      0      0      87.198       3.150   
29       29          3      3.908      0      0      90.168       3.150   
...     ...        ...        ...    ...    ...         ...         ...   
6917   6917          3    931.467      5      0    5844.582       3.174   
6918   6918          3    931.611      5      0    5842.312       3.174   
6919   6919          3    931.745      5      0    5840.187       3.174   
6920   6920          3    931.879      5      0    5838.049       3.174   
6921   6921          3    932.013      5      0    5835.883       3.174   
6922   6922          3    932.147      5      0    5833.688       3.174   
6923   6923          3    932.281      5      0    5831.468       3.174   
6924   6924          3    932.415      5      0    5829.216       3.174   
6925   6925          3    932.550      5      0    5826.915       3.174   
6926   6926          3    932.683      5      0    5824.612       3.174   
6927   6927          3    932.817      5      0    5822.273       3.174   
6928   6928          3    932.951      5      0    5819.901       3.174   
6929   6929          3    933.085      5      0    5817.506       3.174   
6930   6930          3    933.219      5      0    5815.087       3.174   
6931   6931          3    933.355      5      0    5812.617       3.174   
6932   6932          3    933.488      5      0    5810.140       3.174   
6933   6933          3    933.614      5      0    5807.786       3.174   
6934   6934          3    933.756      5      0    5805.102       3.174   
6935   6935          3    933.890      5      0    5802.544       3.174   
6936   6936          3    934.024      5      0    5799.967       3.174   
6937   6937          3    934.157      5      0    5797.360       3.174   
6938   6938          3    934.292      5      0    5794.719       3.174   
6939   6939          3    934.426      5      0    5792.047       3.174   
6940   6940          3    934.550      5      0    5789.562       3.174   
6941   6941          3    934.694      5      0    5786.646       3.174   
6942   6942          3    934.817      5      0    5784.132       3.174   
6943   6943          3    934.951      5      0    5781.359       3.174   
6944   6944          3    935.075      5      0    5778.790       3.174   
6945   6945          3    935.200      5      0    5776.145       3.174   
6946   6946          3    935.348      5      0    5773.058       3.174   

      PlayerZPos  MainCamXPos  MainCamYPos         ...          PlayerZRot  \
0         99.500        5.654        3.724         ...                 0.0   
1        101.334        8.462        3.724         ...                 0.0   
2        102.959       11.407        3.724         ...                -0.0   
3        104.409       14.417        3.724         ...                -0.0   
4        105.768       17.494        3.723         ...                -0.0   
5        107.072       20.573        3.723         ...                -0.0   
6        108.388       23.681        3.723         ...                 0.0   
7        109.849       26.991        3.722         ...                -0.0   
8        111.415       30.232        3.721         ...                -0.0   
9        113.000       33.178        3.721         ...                 0.0   
10       114.714       36.096        3.720         ...                 0.0   
11       116.476       38.930        3.716         ...                 0.0   
12       118.328       41.790        3.712         ...                 0.0   
13       120.181       44.542        3.708         ...                 0.0   
14       122.140       47.318        3.708         ...                 0.0   
15       124.111       49.982        3.710         ...                 0.0   
16       126.130       52.641        3.711         ...                 0.0   
17       128.337       55.589        3.712         ...                 0.0   
18       130.409       58.519        3.711         ...                 0.0   
19       132.270       61.364        3.711         ...                 0.0   
20       133.980       64.189        3.713         ...                 0.0   
21       135.645       67.115        3.714         ...                 0.0   
22       137.240       70.050        3.714         ...                 0.0   
23       138.807       72.997        3.715         ...                -0.0   
24       140.406       76.022        3.716         ...                 0.0   
25       141.945       78.968        3.717         ...                 0.0   
26       143.458       81.912        3.717         ...                 0.0   
27       144.748       84.456        3.718         ...                -0.0   
28       146.225       87.394        3.718         ...                 0.0   
29       147.727       90.361        3.718         ...                -0.0   
...          ...          ...          ...         ...                 ...   
6917    -663.633     5844.542        4.163         ...                 0.0   
6918    -666.433     5842.267        4.163         ...                 0.0   
6919    -669.027     5840.139        4.162         ...                 0.0   
6920    -671.593     5837.999        4.162         ...                 0.0   
6921    -674.145     5835.831        4.162         ...                 0.0   
6922    -676.684     5833.638        4.161         ...                 0.0   
6923    -679.193     5831.419        4.161         ...                 0.0   
6924    -681.671     5829.171        4.160         ...                 0.0   
6925    -684.139     5826.871        4.159         ...                 0.0   
6926    -686.549     5824.566        4.158         ...                 0.0   
6927    -688.941     5822.226        4.158         ...                 0.0   
6928    -691.316     5819.851        4.157         ...                 0.0   
6929    -693.662     5817.454        4.157         ...                 0.0   
6930    -695.976     5815.033        4.156         ...                 0.0   
6931    -698.280     5812.562        4.157         ...                 0.0   
6932    -700.531     5810.083        4.156         ...                 0.0   
6933    -702.619     5807.728        4.156         ...                 0.0   
6934    -704.943     5805.043        4.156         ...                 0.0   
6935    -707.104     5802.484        4.156         ...                 0.0   
6936    -709.230     5799.906        4.156         ...                 0.0   
6937    -711.328     5797.296        4.156         ...                 0.0   
6938    -713.396     5794.650        4.156         ...                 0.0   
6939    -715.431     5791.970        4.155         ...                 0.0   
6940    -717.278     5789.477        4.154         ...                 0.0   
6941    -719.386     5786.554        4.153         ...                 0.0   
6942    -721.150     5784.035        4.153         ...                 0.0   
6943    -723.038     5781.260        4.152         ...                 0.0   
6944    -724.743     5778.693        4.150         ...                 0.0   
6945    -726.465     5776.054        4.147         ...                 0.0   
6946    -728.452     5772.973        4.146         ...                 0.0   

     MainCamXRot MainCamYRot MainCamZRot  MainCamXRotRel  MainCamYRotRel  \
0        356.882      69.661     359.885         356.882          14.906   
1        356.571      72.828     359.870         356.571          13.383   
2        356.467      74.320     359.827         356.467          11.182   
3        356.658      74.779     359.626         356.658           9.214   
4        356.922      74.438     359.542         356.922           7.595   
5        357.014      73.222     359.701         357.014           6.023   
6        357.389      70.864     359.865         357.389           4.110   
7        358.036      67.340     359.890         358.036           2.101   
8        358.411      63.373       0.175         358.411           0.583   
9        358.500      60.856       0.428         358.500           0.483   
10       358.777      59.849       0.635         358.777           1.354   
11         0.441      59.512       0.296           0.441           2.164   
12         2.391      59.219       0.211           2.391           2.841   
13         4.031      58.216     359.896           4.031           2.887   
14         4.143      57.117     359.738           4.143           3.091   
15         3.631      56.950     359.632           3.631           3.996   
16         3.188      58.052     359.445           3.188           5.274   
17         2.892      60.626     359.524           2.892           6.786   
18         3.140      63.926     359.339           3.140           8.066   
19         3.006      66.259     358.995           3.006           8.227   
20         2.486      67.495     358.734           2.486           7.681   
21         2.129      67.655     359.136           2.129           6.513   
22         1.917      66.985     359.561           1.917           5.065   
23         1.637      66.817     359.794           1.637           4.684   
24         1.310      66.681     359.949           1.310           4.379   
25         0.910      67.216       0.067           0.910           4.571   
26         0.759      67.345       0.173           0.759           4.312   
27         0.372      67.400       0.190           0.372           4.122   
28         0.153      66.598       0.002           0.153           3.291   
29         0.133      66.836     359.821           0.133           3.893   
...          ...         ...         ...             ...             ...   
6917     355.577     226.070     358.637         355.577           7.038   
6918     356.058     228.374     358.505         356.058           9.233   
6919     356.754     229.342     358.757         356.754           9.756   
6920     357.090     229.695     359.166         357.090           9.595   
6921     357.465     229.434     359.966         357.465           8.823   
6922     357.814     227.861       1.559         357.814           6.664   
6923     358.069     226.228       2.761         358.069           4.290   
6924     358.590     224.722       4.087         358.590           2.018   
6925     358.810     224.563       4.811         358.810           1.139   
6926     359.031     225.597       4.998         359.031           1.511   
6927     359.320     226.904       5.432         359.320           2.193   
6928     359.710     228.047       5.736         359.710           2.719   
6929       0.118     228.861       5.904           0.118           2.888   
6930       0.242     229.644       5.940           0.242           2.962   
6931       0.177     230.498       5.946           0.177           3.068   
6932       0.107     231.050       5.982           0.107           2.896   
6933       0.196     231.496       6.150           0.196           2.699   
6934       0.161     231.810       6.267           0.161           2.287   
6935     359.706     232.360       6.255         359.706           2.157   
6936     359.650     233.022       6.359         359.650           2.137   
6937     359.701     233.839       6.446         359.701           2.225   
6938     359.496     237.636       6.829         359.496           5.260   
6939     359.823     246.514       7.749         359.823          13.382   
6940     359.922     253.160       8.462         359.922          19.372   
6941     359.752     258.419       8.861         359.752          23.783   
6942     359.726     259.509       8.589         359.726          24.094   
6943       0.653     255.099       7.522           0.653          18.915   
6944       2.432     245.570       6.173           2.432           8.820   
6945       4.226     237.167       5.117           4.226           0.039   
6946       6.180     231.854       4.755           6.180          -5.452   

      MainCamZRotRel  MainCamXRotRelNorm  MainCamYRotRelNorm  \
0            359.885              -3.118              14.906   
1            359.870              -3.429              13.383   
2            359.827              -3.533              11.182   
3            359.626              -3.342               9.214   
4            359.542              -3.078               7.595   
5            359.701              -2.986               6.023   
6            359.865              -2.611               4.110   
7            359.890              -1.964               2.101   
8              0.175              -1.589               0.583   
9              0.428              -1.500               0.483   
10             0.635              -1.223               1.354   
11             0.296               0.441               2.164   
12             0.211               2.391               2.841   
13           359.896               4.031               2.887   
14           359.738               4.143               3.091   
15           359.632               3.631               3.996   
16           359.445               3.188               5.274   
17           359.524               2.892               6.786   
18           359.339               3.140               8.066   
19           358.995               3.006               8.227   
20           358.734               2.486               7.681   
21           359.136               2.129               6.513   
22           359.561               1.917               5.065   
23           359.794               1.637               4.684   
24           359.949               1.310               4.379   
25             0.067               0.910               4.571   
26             0.173               0.759               4.312   
27             0.190               0.372               4.122   
28             0.002               0.153               3.291   
29           359.821               0.133               3.893   
...              ...                 ...                 ...   
6917         358.637              -4.423               7.038   
6918         358.505              -3.942               9.233   
6919         358.757              -3.246               9.756   
6920         359.166              -2.910               9.595   
6921         359.966              -2.535               8.823   
6922           1.559              -2.186               6.664   
6923           2.761              -1.931               4.290   
6924           4.087              -1.410               2.018   
6925           4.811              -1.190               1.139   
6926           4.998              -0.969               1.511   
6927           5.432              -0.680               2.193   
6928           5.736              -0.290               2.719   
6929           5.904               0.118               2.888   
6930           5.940               0.242               2.962   
6931           5.946               0.177               3.068   
6932           5.982               0.107               2.896   
6933           6.150               0.196               2.699   
6934           6.267               0.161               2.287   
6935           6.255              -0.294               2.157   
6936           6.359              -0.350               2.137   
6937           6.446              -0.299               2.225   
6938           6.829              -0.504               5.260   
6939           7.749              -0.177              13.382   
6940           8.462              -0.078              19.372   
6941           8.861              -0.248              23.783   
6942           8.589              -0.274              24.094   
6943           7.522               0.653              18.915   
6944           6.173               2.432               8.820   
6945           5.117               4.226               0.039   
6946           4.755               6.180              -5.452   

      MainCamZRotRelNorm  
0                 -0.115  
1                 -0.130  
2                 -0.173  
3                 -0.374  
4                 -0.458  
5                 -0.299  
6                 -0.135  
7                 -0.110  
8                  0.175  
9                  0.428  
10                 0.635  
11                 0.296  
12                 0.211  
13                -0.104  
14                -0.262  
15                -0.368  
16                -0.555  
17                -0.476  
18                -0.661  
19                -1.005  
20                -1.266  
21                -0.864  
22                -0.439  
23                -0.206  
24                -0.051  
25                 0.067  
26                 0.173  
27                 0.190  
28                 0.002  
29                -0.179  
...                  ...  
6917              -1.363  
6918              -1.495  
6919              -1.243  
6920              -0.834  
6921              -0.034  
6922               1.559  
6923               2.761  
6924               4.087  
6925               4.811  
6926               4.998  
6927               5.432  
6928               5.736  
6929               5.904  
6930               5.940  
6931               5.946  
6932               5.982  
6933               6.150  
6934               6.267  
6935               6.255  
6936               6.359  
6937               6.446  
6938               6.829  
6939               7.749  
6940               8.462  
6941               8.861  
6942               8.589  
6943               7.522  
6944               6.173  
6945               5.117  
6946               4.755  

[6947 rows x 26 columns]
finished

Data-Analysis


In [ ]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

ro_tot = pd.read_excel('rounds_total.xlsx')
ev_tot = pd.read_excel('events_total.xlsx')
gs_tot = pd.read_excel('gamestatus_total.xlsx')

In [85]:
def get_motion_data(gs, started, finished):
    gidx = gs.index[(gs.Timestamp >= started) & (gs.Timestamp <= finished)]
    md = gs.loc[gidx, ['PlayerYRot', 'PlayerXRot', 'MainCamYRotRelNorm', 'MainCamXRotRelNorm']]
    return md

Gewinnspiel Auswertung


In [ ]:
columns = ['SubjectId', 'RoundType', 'RoundScore', 'PovSucc', 'PovTotal', 'RingSucc', 'RingTotal']
data = []

def filter_ev(group):
    return ((group.Trial == group.Trial.max())
           & group.ValidTrial 
           & (~group.RoundType.isin(['Training_Ring_Only', 'Training_Complete']))).any()

def round_score(x):
    pov_succ = x[(x.TaskType == 'POV') & (x.TaskStatus == 'success')].EventId.size
    pov_tot = x[(x.TaskType == 'POV')].EventId.size
    ring_succ = x[(x.TaskType == 'Ring') & (x.TaskStatus == 'success')].EventId.size
    ring_tot = x[(x.TaskType == 'Ring')].EventId.size
    round_score = pov_succ * (ring_succ / ring_tot)
    data.append({'SubjectId': x.iloc[0].SubjectId, 'RoundType': x.iloc[0].RoundType, 
                 'RoundScore': round_score, 'PovSucc': pov_succ, 'PovTotal': pov_tot,
                 'RingSucc': ring_succ, 'RingTotal': ring_tot})
    return round_score

grouped = ev_tot.groupby(['SubjectId', 'RoundType', 'Trial'])
ev_red = grouped.filter(filter_ev)

#print(ev_red[(ev_red.SubjectId == 3) & (ev_red.TaskType == 'POV')])

round_score_group = ev_red.groupby(['SubjectId', 'RoundType']).apply(round_score)
total_score = round_score_group.groupby(['SubjectId']).agg({'Sum': 'sum'}).sort_values(by="SubjectId", ascending=True)
print(total_score)

#df = pd.DataFrame(data=data, columns=columns)
#df.to_excel('scores.xlsx')

#print(filtered_ev_tot.groupby(['SubjectId', 'RoundType', 'TaskType', 'TaskStatus'])['EventId'].agg({"Count": 'count'}).to_string())

POV Selection Times by Round Type


In [ ]:
pov = ev_tot[(ev_tot.RoundType != 'Training_Complete') &(ev_tot.TaskType == 'POV') & (ev_tot.TaskStatus == 'success')]
# Fehlerhafte EInträge entfernen
pov = pov.drop(pov[(pov.RoundType == 'Audio') & (pov.Duration > 10)].index)
plot = pov.boxplot(column=['Duration'], by='RoundType', figsize=(20,10))
fig = plot.get_figure()
fig.savefig("output.png")

Feedback Reaction Times


In [ ]:
for rt in ev_tot.RoundType.unique():
    round_ev = ev_tot[ev_tot.RoundType == rt]
    round_pov = events[events.TaskType == 'POV']
    #pov_succ = pov_events.loc[events.Status == 'success']
    
    #ring_events = events.loc[(eve nts.TaskType == 'Ring')]
    #ring_succ = ring_events.loc[events.Status == 'success'].shape[0]
    #ring_fail = ring_events.loc[events.Status == 'timeout'].shape[0]
    #pov_fail = pov_events.loc[events.Status == 'timeout'].shape[0]

    for pos in round_pov.Position.unique():
        print("Plot for %s, %s" % (rt, pos))
        round_pov_pos = round_pov[round_pov.Position == pos]
        round_pov_pos.plot(kind="scatter", x="MainCamYRot", y="MainCamXRot", xlim=(-360,360), ylim=(180,-180))
        plt.show()