<jupyter_start><jupyter_text>Compute Laterality Indices  import modules<jupyter_code>%matplotlib inline
import os
import sys

import pandas as pd
import numpy as np

import seaborn as sns
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>get absolute directory of project<jupyter_code># after converstion to .py, we can use __file__ to get the module folder
try:
    thisDir = os.path.realpath(__file__)
# in notebook form, we take the current working directory (we need to be in 'notebooks/' for this!)
except:
    thisDir = '.'
# convert relative path into absolute path, so this will work with notebooks and py modules
supDir = os.path.abspath(os.path.join(os.path.dirname(thisDir), '..'))

supDir<jupyter_output><empty_output><jupyter_text>get data<jupyter_code>arrayDf = pd.DataFrame(['../data/interim/csv/%s'%x for x in os.listdir('../data/interim/csv/') if x.startswith('roiPercentiles_pat')])
arrayDf.columns = ['roiPc']
arrayDf.index = [x.split('roiPercentiles_')[-1].split('.')[0] for x in arrayDf['roiPc']]
arrayDf.sort_index(inplace=True)
arrayDf.tail()
arrayDf.shape<jupyter_output><empty_output><jupyter_text>get stored data<jupyter_code>def getRawData(csvFile):
    
    rawDf = pd.read_csv(csvFile,index_col=[0,1])
    rawDf.columns = rawDf.columns.map(float)
    rawDf.sort_index(axis=1,inplace=True)
    
    return rawDf
pName = arrayDf.iloc[69].name
pName
rawDf = getRawData( arrayDf.loc[pName].roiPc )
rawDf.tail()<jupyter_output><empty_output><jupyter_text>The deconstruction into numerator and denominator:<jupyter_code>def makeDiff(df):
    return pd.DataFrame( (df['left']-df['right'])/100. )
def makeAdd(df):
    return pd.DataFrame( (df['left']+df['right'])/100. )
def makeBigDf(rawDf):
    bigDf = pd.DataFrame()

    for i in rawDf.index.levels[0]:

        thisDf = rawDf.loc[i].T

        diffDf = makeDiff(thisDf)
        diffDf.columns = ['L-R']

        addDf = makeAdd(thisDf)
        addDf.columns = ['L+R']

        liDf = pd.DataFrame( diffDf['L-R'] / addDf['L+R'] )
        liDf.columns = ['LI']

        pcDf = pd.DataFrame(pd.concat([addDf, diffDf, liDf], axis=1).stack(dropna=False)).T
        pcDf.index = [i]

        bigDf = pd.concat([bigDf, pcDf], axis=0)

    return bigDf
bigDf = makeBigDf(rawDf)
bigDf.tail()
pDf = rawDf.loc[50].T
fig,ax1 = plt.subplots(1,1,figsize=(8,6))
for side in ['left','right']:
    ax1.plot(pDf.loc[-5.0:15.0][side],linewidth=10,alpha=0.8,label=side)
    ax1.legend(loc='best',fontsize=24)
ax1.set_ylabel('% voxel above threshold',fontsize=24)
ax1.set_xlabel('t value for thresholding',fontsize=24)
ax1.set_xticks(range(-5,16,5))
ax1.set_xticklabels(range(-5,16,5),fontsize=22)
ax1.set_yticks(range(0,101,20))
ax1.set_yticklabels(range(0,101,20),fontsize=22)
ax1.set_ylim(-1,102)
ax1.set_title('example a, voxel count',fontsize=28)
sns.despine()
plt.show()
myDf = bigDf.loc[50].reorder_levels([1,0])
liDf = myDf['LI']
twoDf = pd.DataFrame( myDf ).unstack(0)[50]
fig,ax1 = plt.subplots(1,1,figsize=(8,6))

ax1.plot( liDf,label='(L-R)/(L+R)',linewidth=10,alpha=0.8,color='r' )
ax1.plot( twoDf['L-R'],label='L-R',linewidth=10,alpha=0.8 ,color='c')

ax1.set_xlim(-8,17)
ax1.set_xlabel('t value for thresholding',fontsize=24)
ax1.set_ylabel('laterality scores',fontsize=24)

ax1.set_xticks(range(-5,16,5))
ax1.set_xticklabels(range(-5,16,5),fontsize=22)

ax1.set_yticks(np.arange(0,1.1,.2))
ax1.set_yticklabels(np.arange(0,1.1,.2),fontsize=22)

ax2 = ax1.twinx()
ax2.plot( twoDf['L+R'],label='L+R',linewidth=10,alpha=0.8 ,color='y')
ax2.tick_params('y')
ax2.set_ylabel('strength score',fontsize=24)

ax2.set_yticks(np.arange(0,2.1,.5))
ax2.set_yticklabels(np.arange(0,2.1,.5),fontsize=22)

#ax1.legend(loc='upper right',bbox_to_anchor=(1.03,0.65),fontsize=24)
#ax2.legend(loc='upper right',bbox_to_anchor=(0.857,0.45),fontsize=24)
ax1.legend(loc='upper right',bbox_to_anchor=(1.03,1),fontsize=24)
ax2.legend(loc='upper right',bbox_to_anchor=(0.857,0.8),fontsize=24)

ax1.set_ylim(-0.01,1.02)
ax2.set_ylim(-0.02,2.04)
ax1.set_title('example b, laterality indices',fontsize=28)
sns.despine(right=False)
#ax1.axhline(0,linewidth=3,color='k',linestyle='dashed')

plt.show()<jupyter_output><empty_output><jupyter_text>do all for one participant<jupyter_code>def makeP(pName,arrayDf=arrayDf):
    rawDf = getRawData( arrayDf.loc[pName].roiPc )
    bigDf = makeBigDf(rawDf)
    bigDf.to_csv( '../data/interim/csv/roiLaterality_%s.csv'%pName )
    return bigDf
#def makeP(pFolder, pName):
#    rawDf = getRawData('%s/roiPercentiles_%s.csv' % (pFolder, pName))
#    bigDf = makeBigDf(rawDf)
#    bigDf.to_csv('%s/roiLaterality_%s.csv' % (pFolder, pName))
#    return bigDf
bigDf = makeP(pName)<jupyter_output><empty_output><jupyter_text>for all participants<jupyter_code>doneList = [
    x.split('Laterality_')[-1].split('.')[0]
    for x in os.listdir('%s/data/interim/csv/' % supDir)
    if x.startswith('roiLaterality')
]
len(doneList)
for pName in arrayDf.index:
    if pName not in doneList:
        print pName
        bigDf = makeP(pName)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17387868010274649, 'fraction_numerical': 0.04228413357044063, 'mean_word_length': 5.514800514800514, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '235374', 'license': 'MIT', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2165, 'n_words': 309, 'path': 'notebooks/05-mw-compute-2d-and-li.ipynb', 'repo_name': 'mwegrzyn/laterality-index-deconstruction', 'size': '5061'}