Plots

Created on Sat May 1 15:12:38 2019
@author: ‘Semeon Risom’
@purpose: Hub for creating plots.

##### imports

[ ]:
#----local
from imhr import plot, processing, raw, redcap
import imhr.model as model
import imhr.settings as settings

#----check if required libraries are available
is_library = False
if is_library:
    settings.library()
pass

#----core
from pdb import set_trace as breakpoint
import pandas as pd
import gc, glob, string, pytz
from datetime import datetime

##### initalize

[ ]:
console = settings.console

##### single subject bokeh coordinates (all trials)

[ ]:
##### single subject
'''
Resources:
 - https://media.springernature.com/original/springer-static/image/art%3A10.3758%2Fs13428-017-0913-7/MediaObjects/13428_2017_913_Figa_HTML.gif
'''
print(console['red'] + 'Step: bokeh_trial()' + console['ENDC'])
subject = 31
session = 0
#data
path_sns = config['output'] + "/process/data/eyetracking/%s_%s.csv"%(subject,session)
df_single = pd.read_csv(path_sns, float_precision='high')
#rename
df_single = df_single.rename(columns={"LEmotion":"left_mood","REmotion":"right_mood"})
#exclude columns
df_single = df_single[['participant','session','subsession','TrialNum','timestamp','trialType','isCongruent',
                        'left_mood','right_mood','monitorSize.px',
                        'x','y','marker','sg_x','sg_y','sg_class',
                        'sg_fix_all','sg_fix_index',
                        'sg_all_bounds','sg_fix_bounds','fix_num',
                        'left_bound','right_bound','dwell']]

#get rois
stim_bounds, roi_bounds = processing.roi(filters=filters, flt=filters[0][1], df=df_single, manual=True)
#for each subject
flt = 'sg'
for idx in range(198):
    #subset data
    df_single_ = df_single[df_single['TrialNum'].isin([idx])].reset_index(drop=True)
    #draw plot
    bokeh_plot = plot.bokeh_trial(config=config, df=df_single_, stim_bounds=stim_bounds, roi_bounds=roi_bounds, flt='sg')
    ##get is_congruent
    isCongruent = "congruent" if df_single_['isCongruent'][0] == True else "incongruent"
    #html
    title = "(%s) Participant %s, session %s, "%(isCongruent, subject, session)
    html_path = config['output'] + "/analysis/html/trial/%s_%s_%s.html"%(subject,session,idx)
    html = plot.html(config=config, path=html_path, plots=bokeh_plot, source="bokeh",
                     display="trial", trial=idx, title=title)

del title, path_sns, bokeh_plot, df_single_, html_path, html, subject, session

#-------------------------------------------------single subject calibration
print(console['green'] + 'bokeh_calibration()' + console['ENDC'])
subject = 'shellie'
monitorSize = [1920,1080]
#for calibration/validation event 0,1,2
for cv_session in range(1,4):
    #data
    path_sns = config['output'] + "/analysis/calibration/%s_1_%s_calibration.csv"%(subject, cv_session)
    df_calibration = pd.read_csv(path_sns, float_precision='high')

    #calibration and validation
    for event, full in zip(['isCalibrating','isValidating'],['calibration','validation']):
        #subset data
        df_calibration_ = df_calibration.loc[df_calibration['event'] == event].reset_index(drop=True)
        #draw plot
        bokeh_plot = plot.bokeh_calibration(config=config, df=df_calibration_, monitorSize=monitorSize)
        #html
        title = "Participant %s, event %s,"%(subject, cv_session)
        html_path = config['output'] + "/analysis/html/cv/%s_%s_%s.html"%(subject, cv_session, full)
        html = plot.html(config=config, path=html_path, plots=bokeh_plot, source="bokeh", display="calibration",
                             trial=full, session=cv_session, title=title)

##### density plot

[ ]:
print(console['red'] + 'Step: density_plot()' + console['ENDC'])
#Computes and draws density plot (kernel density estimate), which is a smoothed version of the histogram.
#This is used as a gage for normality
df_density = df[['participant','trialType_','m_rt',
                 'm_diff_dotloc','m_diff_stim','luminance',
                 'rrs_brooding','cesd_score','cesd_group_',
                 'dp_bias','n_dp_valid','gaze_bias','n_gaze_valid',
                 'var_gaze_bias','final_gaze_bias']].loc[df['nested'] == 'subject']

#----exclude
df_density = df_density[~df_density['participant'].isin(exclude)]

#file
title = string.capwords('kernel density estimate')

#create images
density, html_plots = plot.density_plot(config=config, df=df_density, title=title)
#description of plots
intro = "The kernel density estimate (kde) is used here as a quick check of normality for each of the variables of interest in the model. All data here has been nested by subject. %s"%(config['def_exclude'])
#create html
html_path = config['output'] + "/analysis/html/density.html"
html = plot.html(config=config, path=html_path, plots=html_plots, source="plots", intro=intro)

##### correlation matrix

[ ]:
print(console['red'] + 'Step: corr_matrix()' + console['ENDC'])
#run correlation matrix
df_corr = df[['dp_bias','n_dp_valid','var_dp_bias',
              'gaze_bias','n_gaze_valid','var_gaze_bias','final_gaze_bias',
              'rrs_brooding','cesd_score',
              'm_rt','m_diff_dotloc','m_diff_stim',
              'luminance']].loc[df['nested'] == 'subject']

#file
file = 'corr_matrix'
method = 'spearman'
title = string.capwords('%s correlation coefficient matrix (p-value).'%(method))

path = config['output'] + "/analysis/html/%s.html"%(file)
corr_matrix = plot.corr_matrix(config=config, df=df_corr, path=path, title=title, method=method)

##### boxplot

[ ]:
print(console['red'] + 'Step: boxplot()' + console['ENDC'])
#----create temp df
df_box = df[['dp_bias','gaze_bias','dp_gaze_cor','final_gaze_bias','participant','trialType_','cesd_group_','nested']]
html_plots = []

#----exclude
df_box = df_box[~df_box['participant'].isin(exclude)]
df_box_error = df_error[~df_error['participant'].isin(exclude)]

#analysis--------------------------------------------------------------------------------------------------------
html_file = 'bias_boxplot'
y=['dp_bias','gaze_bias','dp_gaze_cor','final_gaze_bias']
#create plot
intro = 'This was done with two purposes.         1) To get general trends in the data.         2) To identify potential outliers in the data that might warrent investigation. Participants that         may need follow up are 999999, 111111, 314, 298, 256, 206, 218, 201, 193, 183, 140, 84, 60, 12.         Note: All data here has either been collapsed by subject or trialType.'
##-------cesd
df_box_ = df_box.loc[df['nested'] == 'subject']
file = 'boxplot_cesd'
x='cesd_group_'
cat='analysis'
title = 'Boxplots, CES-D Cutoff (N = %s, collapsed by subject)'%(subjects_used)
footnote = "Data collapsed by subject:trialType. %s"%(config['def_exclude'])
sns_path = config['output'] + "/analysis/html/img/%s.png"%(file)
plot.boxplot(config=config, df=df_box_, path=sns_path, x=x,y=y, cat=cat)
html_plots.append({"title":title,"file":"%s.png"%(file),"footnote":footnote})

##-------trialType
df_box_ = df_box.loc[df['nested'] == 'trialType']
file = 'boxplot_trial'
x='trialType_'
cat='analysis'
title = 'Boxplots, Trial Type (N = %s, collapsed by subject:trialType)'%(subjects_used)
footnote = "Data collapsed by subject:trialType. %s"%(config['def_exclude'])
sns_path = config['output'] + "/analysis/html/img/%s.png"%(file)
plot.boxplot(config=config, df=df_box_, path=sns_path, x=x, y=y, cat=cat)
html_plots.append({"title":title,"file":"%s.png"%(file),"footnote":footnote})

#save folders
html_path = config['output'] + "/analysis/html/%s.html"%(html_file)
html = plot.html(config=config, path=html_path, plots=html_plots, source="plots", display="boxplot", intro=intro)
del intro, html_file, file, title, sns_path, html_path, html, cat, x, y

#timing----------------------------------------------------------------------------------------------------------
html_plots = []
html_file = 'rt_boxplot'
x = ['race','gender','is_normalvision','os']
cat = 'demographics'

##-------response time
y = 'Key_Resp_rt'
intro = 'This was done to compare differences in response time between os, webcamsize, gender, race and other factors.'
footnote = "Data collapsed by subject. Participants identified 'race' as 'American Indian or Alaska Native', 'Two or more races', \
'Black or African American', 'None of the above' were excluded here for displaying purposes. %s"%(config['def_exclude'])
#create plot
file = 'rt_boxplot'
title = 'Boxplots, %s (N = %s)'%(y, subjects_used)
sns_path = config['output'] + "/analysis/html/img/%s.png"%(file)
plot.boxplot(config=config, df=df_box_error, path=sns_path, x=x, y=y, cat=cat)
html_plots.append({"title":title,"file":"%s.png"%(file),"footnote":footnote})

##-------diff_dotloc
y = 'diff_dotloc'
intro = 'This was done to compare differences between expected and true dotloc onset between os, webcamsize, gender, race and other factors.'
footnote = "Data collapsed by subject. Participants identified 'race' as 'American Indian or Alaska Native','Two or more races', \
'Black or African American', 'None of the above' were excluded here for displaying purposes. %s"%(config['def_exclude'])
#create plot
file = 'dotloc_boxplot'
title = 'Boxplots, %s (N = %s)'%(y, subjects_used)
sns_path = config['output'] + "/analysis/html/img/%s.png"%(file)
plot.boxplot(config=config, df=df_box_error, path=sns_path, x=x,y=y, cat=cat)
html_plots.append({"title":title,"file":"%s.png"%(file),"footnote":footnote})

##-------diff_stim
y = 'diff_stim'
intro = 'This was done to compare differences between expected and true stim onset between os, webcamsize, gender, race and other factors.'
footnote = "Data collapsed by subject. Participants identified 'race' as 'American Indian or Alaska Native','Two or more races', \
'Black or African American', 'None of the above' were excluded here for displaying purposes. %s"%(config['def_exclude'])
#create plot
file = 'stim_boxplot'
title = 'Boxplots, %s (N = %s)'%(y, subjects_used)
sns_path = config['output'] + "/analysis/html/img/%s.png"%(file)
plot.boxplot(config=config, df=df_box_error, path=sns_path, x=x,y=y, cat=cat)
html_plots.append({"title":title,"file":"%s.png"%(file),"footnote":footnote})

#-------save folders
html_path = config['output'] + "/analysis/html/%s.html"%(html_file)
html = plot.html(config=config, path=html_path, plots=html_plots, source="plots", display="boxplot", intro=intro)
del intro, html_file, file, title, html_path, html, cat, x, y, footnote