{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ ".. _plots:\n", " \n", ".. title:: Plots\n", " \n", "#### Plots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Created on Sat May 1 15:12:38 2019 \n", " @author: 'Semeon Risom' \n", " @email: 'semeon.risom@gmail.com' \n", " @url: 'https://semeon.io/d/R33-analysis' \n", " @purpose: Hub for creating plots. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ##### imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#----local\n", "from imhr import plot, processing, raw, redcap\n", "import imhr.model as model\n", "import imhr.settings as settings\n", "\n", "#----check if required libraries are available\n", "is_library = False\n", "if is_library:\n", " settings.library()\n", "pass\n", "\n", "#----core\n", "from pdb import set_trace as breakpoint\n", "import pandas as pd\n", "import gc, glob, string, pytz\n", "from datetime import datetime" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ##### initalize" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "console = settings.console" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ##### single subject bokeh coordinates (all trials)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##### single subject\n", "'''\n", "Resources: \n", " - https://media.springernature.com/original/springer-static/image/art%3A10.3758%2Fs13428-017-0913-7/MediaObjects/13428_2017_913_Figa_HTML.gif\n", "'''\n", "print(console['red'] + 'Step: bokeh_trial()' + console['ENDC'])\n", "subject = 31\n", "session = 0\n", "#data\n", "path_sns = config['output'] + \"/process/data/eyetracking/%s_%s.csv\"%(subject,session)\n", "df_single = pd.read_csv(path_sns, float_precision='high')\n", "#rename\n", "df_single = df_single.rename(columns={\"LEmotion\":\"left_mood\",\"REmotion\":\"right_mood\"})\n", "#exclude columns\n", "df_single = df_single[['participant','session','subsession','TrialNum','timestamp','trialType','isCongruent',\n", " 'left_mood','right_mood','monitorSize.px',\n", " 'x','y','marker','sg_x','sg_y','sg_class',\n", " 'sg_fix_all','sg_fix_index',\n", " 'sg_all_bounds','sg_fix_bounds','fix_num',\n", " 'left_bound','right_bound','dwell']]\n", "\n", "#get rois\n", "stim_bounds, roi_bounds = processing.roi(filters=filters, flt=filters[0][1], df=df_single, manual=True)\n", "#for each subject\n", "flt = 'sg'\n", "for idx in range(198):\n", " #subset data\n", " df_single_ = df_single[df_single['TrialNum'].isin([idx])].reset_index(drop=True)\n", " #draw plot\n", " bokeh_plot = plot.bokeh_trial(config=config, df=df_single_, stim_bounds=stim_bounds, roi_bounds=roi_bounds, flt='sg')\n", " ##get is_congruent\n", " isCongruent = \"congruent\" if df_single_['isCongruent'][0] == True else \"incongruent\"\n", " #html\n", " title = \"(%s) Participant %s, session %s, \"%(isCongruent, subject, session)\n", " html_path = config['output'] + \"/analysis/html/trial/%s_%s_%s.html\"%(subject,session,idx)\n", " html = plot.html(config=config, path=html_path, plots=bokeh_plot, source=\"bokeh\", \n", " display=\"trial\", trial=idx, title=title)\n", "\n", "del title, path_sns, bokeh_plot, df_single_, html_path, html, subject, session\n", "\n", "#-------------------------------------------------single subject calibration\n", "print(console['green'] + 'bokeh_calibration()' + console['ENDC'])\n", "subject = 'shellie'\n", "monitorSize = [1920,1080]\n", "#for calibration/validation event 0,1,2\n", "for cv_session in range(1,4):\n", " #data\n", " path_sns = config['output'] + \"/analysis/calibration/%s_1_%s_calibration.csv\"%(subject, cv_session)\n", " df_calibration = pd.read_csv(path_sns, float_precision='high')\n", "\n", " #calibration and validation\n", " for event, full in zip(['isCalibrating','isValidating'],['calibration','validation']):\n", " #subset data\n", " df_calibration_ = df_calibration.loc[df_calibration['event'] == event].reset_index(drop=True)\n", " #draw plot\n", " bokeh_plot = plot.bokeh_calibration(config=config, df=df_calibration_, monitorSize=monitorSize)\n", " #html\n", " title = \"Participant %s, event %s,\"%(subject, cv_session)\n", " html_path = config['output'] + \"/analysis/html/cv/%s_%s_%s.html\"%(subject, cv_session, full)\n", " html = plot.html(config=config, path=html_path, plots=bokeh_plot, source=\"bokeh\", display=\"calibration\",\n", " trial=full, session=cv_session, title=title)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ##### density plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(console['red'] + 'Step: density_plot()' + console['ENDC'])\n", "#Computes and draws density plot (kernel density estimate), which is a smoothed version of the histogram. \n", "#This is used as a gage for normality\n", "df_density = df[['participant','trialType_','m_rt',\n", " 'm_diff_dotloc','m_diff_stim','luminance',\n", " 'rrs_brooding','cesd_score','cesd_group_',\n", " 'dp_bias','n_dp_valid','gaze_bias','n_gaze_valid',\n", " 'var_gaze_bias','final_gaze_bias']].loc[df['nested'] == 'subject'] \n", "\n", "#----exclude\n", "df_density = df_density[~df_density['participant'].isin(exclude)] \n", "\n", "#file\n", "title = string.capwords('kernel density estimate')\n", "\n", "#create images\n", "density, html_plots = plot.density_plot(config=config, df=df_density, title=title)\n", "#description of plots\n", "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'])\n", "#create html\n", "html_path = config['output'] + \"/analysis/html/density.html\"\n", "html = plot.html(config=config, path=html_path, plots=html_plots, source=\"plots\", intro=intro)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ##### correlation matrix" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(console['red'] + 'Step: corr_matrix()' + console['ENDC'])\n", "#run correlation matrix\n", "df_corr = df[['dp_bias','n_dp_valid','var_dp_bias',\n", " 'gaze_bias','n_gaze_valid','var_gaze_bias','final_gaze_bias',\n", " 'rrs_brooding','cesd_score',\n", " 'm_rt','m_diff_dotloc','m_diff_stim',\n", " 'luminance']].loc[df['nested'] == 'subject']\n", "\n", "#file\n", "file = 'corr_matrix'\n", "method = 'spearman'\n", "title = string.capwords('%s correlation coefficient matrix (p-value).'%(method))\n", "\n", "path = config['output'] + \"/analysis/html/%s.html\"%(file)\n", "corr_matrix = plot.corr_matrix(config=config, df=df_corr, path=path, title=title, method=method)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ##### boxplot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(console['red'] + 'Step: boxplot()' + console['ENDC'])\n", "#----create temp df\n", "df_box = df[['dp_bias','gaze_bias','dp_gaze_cor','final_gaze_bias','participant','trialType_','cesd_group_','nested']]\n", "html_plots = []\n", "\n", "#----exclude\n", "df_box = df_box[~df_box['participant'].isin(exclude)]\n", "df_box_error = df_error[~df_error['participant'].isin(exclude)]\n", "\n", "#analysis--------------------------------------------------------------------------------------------------------\n", "html_file = 'bias_boxplot'\n", "y=['dp_bias','gaze_bias','dp_gaze_cor','final_gaze_bias']\n", "#create plot\n", "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.'\n", "##-------cesd\n", "df_box_ = df_box.loc[df['nested'] == 'subject']\n", "file = 'boxplot_cesd'\n", "x='cesd_group_'\n", "cat='analysis'\n", "title = 'Boxplots, CES-D Cutoff (N = %s, collapsed by subject)'%(subjects_used) \n", "footnote = \"Data collapsed by subject:trialType. %s\"%(config['def_exclude'])\n", "sns_path = config['output'] + \"/analysis/html/img/%s.png\"%(file)\n", "plot.boxplot(config=config, df=df_box_, path=sns_path, x=x,y=y, cat=cat)\n", "html_plots.append({\"title\":title,\"file\":\"%s.png\"%(file),\"footnote\":footnote})\n", "\n", "##-------trialType\n", "df_box_ = df_box.loc[df['nested'] == 'trialType']\n", "file = 'boxplot_trial'\n", "x='trialType_'\n", "cat='analysis'\n", "title = 'Boxplots, Trial Type (N = %s, collapsed by subject:trialType)'%(subjects_used)\n", "footnote = \"Data collapsed by subject:trialType. %s\"%(config['def_exclude'])\n", "sns_path = config['output'] + \"/analysis/html/img/%s.png\"%(file)\n", "plot.boxplot(config=config, df=df_box_, path=sns_path, x=x, y=y, cat=cat)\n", "html_plots.append({\"title\":title,\"file\":\"%s.png\"%(file),\"footnote\":footnote})\n", "\n", "#save folders\n", "html_path = config['output'] + \"/analysis/html/%s.html\"%(html_file)\n", "html = plot.html(config=config, path=html_path, plots=html_plots, source=\"plots\", display=\"boxplot\", intro=intro)\n", "del intro, html_file, file, title, sns_path, html_path, html, cat, x, y\n", "\n", "#timing----------------------------------------------------------------------------------------------------------\n", "html_plots = []\n", "html_file = 'rt_boxplot'\n", "x = ['race','gender','is_normalvision','os']\n", "cat = 'demographics'\n", "\n", "##-------response time\n", "y = 'Key_Resp_rt'\n", "intro = 'This was done to compare differences in response time between os, webcamsize, gender, race and other factors.'\n", "footnote = \"Data collapsed by subject. Participants identified 'race' as 'American Indian or Alaska Native', 'Two or more races', \\\n", "'Black or African American', 'None of the above' were excluded here for displaying purposes. %s\"%(config['def_exclude'])\n", "#create plot\n", "file = 'rt_boxplot'\n", "title = 'Boxplots, %s (N = %s)'%(y, subjects_used)\n", "sns_path = config['output'] + \"/analysis/html/img/%s.png\"%(file)\n", "plot.boxplot(config=config, df=df_box_error, path=sns_path, x=x, y=y, cat=cat)\n", "html_plots.append({\"title\":title,\"file\":\"%s.png\"%(file),\"footnote\":footnote})\n", "\n", "##-------diff_dotloc\n", "y = 'diff_dotloc'\n", "intro = 'This was done to compare differences between expected and true dotloc onset between os, webcamsize, gender, race and other factors.'\n", "footnote = \"Data collapsed by subject. Participants identified 'race' as 'American Indian or Alaska Native','Two or more races', \\\n", "'Black or African American', 'None of the above' were excluded here for displaying purposes. %s\"%(config['def_exclude'])\n", "#create plot\n", "file = 'dotloc_boxplot'\n", "title = 'Boxplots, %s (N = %s)'%(y, subjects_used)\n", "sns_path = config['output'] + \"/analysis/html/img/%s.png\"%(file)\n", "plot.boxplot(config=config, df=df_box_error, path=sns_path, x=x,y=y, cat=cat)\n", "html_plots.append({\"title\":title,\"file\":\"%s.png\"%(file),\"footnote\":footnote})\n", "\n", "##-------diff_stim\n", "y = 'diff_stim'\n", "intro = 'This was done to compare differences between expected and true stim onset between os, webcamsize, gender, race and other factors.'\n", "footnote = \"Data collapsed by subject. Participants identified 'race' as 'American Indian or Alaska Native','Two or more races', \\\n", "'Black or African American', 'None of the above' were excluded here for displaying purposes. %s\"%(config['def_exclude'])\n", "#create plot\n", "file = 'stim_boxplot'\n", "title = 'Boxplots, %s (N = %s)'%(y, subjects_used)\n", "sns_path = config['output'] + \"/analysis/html/img/%s.png\"%(file)\n", "plot.boxplot(config=config, df=df_box_error, path=sns_path, x=x,y=y, cat=cat)\n", "html_plots.append({\"title\":title,\"file\":\"%s.png\"%(file),\"footnote\":footnote})\n", "\n", "#-------save folders\n", "html_path = config['output'] + \"/analysis/html/%s.html\"%(html_file)\n", "html = plot.html(config=config, path=html_path, plots=html_plots, source=\"plots\", display=\"boxplot\", intro=intro)\n", "del intro, html_file, file, title, html_path, html, cat, x, y, footnote" ] } ], "metadata": { "file_extension": ".py", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" }, "mimetype": "text/x-python", "name": "python", "npconvert_exporter": "python", "pygments_lexer": "ipython3", "version": 3 }, "nbformat": 4, "nbformat_minor": 2 }