import numpy as np
import seaborn as sns
import pandas as pd
import dabest
from scipy.stats.stats import pearsonr
import matplotlib.pyplot as plt
# Create style
sns.set_style('ticks')
sns.set_context("talk")
df = pd.read_csv("/Users/philipb/surfdrive/PhD/BRAINLINKS/PILOT/COSY results/BL0P_DATA_MASTER.csv")
time = pd.read_csv("/Users/philipb/surfdrive/PhD/BRAINLINKS/PILOT/COSY results/timeseries/timeseries_parents.csv")
df['NAcc OtherWin Mother_vs_Stranger'] = df['MOTHER_OtherWin'] - df['STRANGER_OtherWin']
df['Like Winning Mother_vs_Stranger'] = df['LW_mother'] - df['LW_stranger']
df
# Melt the columns together to create a TIDY DATASET for easier analysis later on
tidy = pd.melt(df,
id_vars=['id','sex'],
value_vars=['MOTHER_SelfWin','MOTHER_OtherWin','MOTHER_BothWin',
'FATHER_SelfWin','FATHER_OtherWin','FATHER_BothWin',
'STRANGER_SelfWin','STRANGER_OtherWin','STRANGER_BothWin',],
value_name='NAcc')
# Rename the column that has just been created for all conditions & targets
# USE df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)
tidy.rename(columns={'variable': 'Condition'}, inplace=True)
# Duplicate the condition column to be able to later split it into TARGET & Condition as two variables/columns
tidy['Target'] = tidy['Condition']
# Delete duplicate information for both condition & target variables.
# USE: df['range'] = df['range'].str.replace(',','-')
# USE with Wildcard / Regex: df['email'] = df['email'].str.replace(r'@.+', '@newcompany.com')
# For Partner
tidy['Condition'] = tidy['Condition'].str.replace('MOTHER_BothWin','BothWin')
tidy['Condition'] = tidy['Condition'].str.replace('MOTHER_SelfWin','SelfWin')
tidy['Condition'] = tidy['Condition'].str.replace('MOTHER_OtherWin','OtherWin')
# For Child
tidy['Condition'] = tidy['Condition'].str.replace('FATHER_BothWin','BothWin')
tidy['Condition'] = tidy['Condition'].str.replace('FATHER_SelfWin','SelfWin')
tidy['Condition'] = tidy['Condition'].str.replace('FATHER_OtherWin','OtherWin')
# For Stranger
tidy['Condition'] = tidy['Condition'].str.replace('STRANGER_BothWin','BothWin')
tidy['Condition'] = tidy['Condition'].str.replace('STRANGER_SelfWin','SelfWin')
tidy['Condition'] = tidy['Condition'].str.replace('STRANGER_OtherWin','OtherWin')
# Replace all target strings with just the Target and not target + condition
tidy['Target'] = tidy['Target'].str.replace('MOTHER.*','MOTHER',regex=True)
tidy['Target'] = tidy['Target'].str.replace('FATHER.*','FATHER',regex=True)
tidy['Target'] = tidy['Target'].str.replace('STRANGER.*','STRANGER',regex=True)
tidy.head()
#time.head()
# We are loading the different groups into a variable using dabest for IOS and like winning
ios_mother_father = dabest.load(df, idx =("IOS_mother","IOS_father"),resamples = 10000)
ios_mother_stranger = dabest.load(df, idx = ("IOS_mother","IOS_stranger"),resamples = 10000)
ios_father_stranger = dabest.load(df, idx = ("IOS_father", "IOS_stranger"),resamples = 10000)
ios_all = dabest.load(df, idx = ("IOS_stranger", "IOS_mother", "IOS_father"),resamples = 10000)
like_winning_self_stranger = dabest.load(df, idx = ("LW_self", "LW_stranger"),resamples = 10000)
like_winning_all = dabest.load(df, idx = ("LW_self","LW_mother","LW_father","LW_stranger"),resamples = 10000)
# Create variables from pandas dataframe
IOS_mother = df.IOS_mother
IOS_father = df.IOS_father
IOS_stranger = df.IOS_stranger
LW_self = df.LW_self
LW_mother = df.LW_mother
LW_father = df.LW_father
LW_stranger = df.LW_stranger
ios_all
ios_all.mean_diff
ios_all.mean_diff.plot()
ios_mother_father.mean_diff.results
#ios_mother_father.mean_diff.plot()
ios_mother_stranger.mean_diff.results
#ios_mother_stranger.mean_diff.plot()
ios_father_stranger.mean_diff.results
#ios_father_stranger.mean_diff.plot()
like_winning_self_stranger.mean_diff.results
like_winning_all.mean_diff
like_winning_all.mean_diff.plot()
plt.figure(figsize=(10, 6))
fig2 = sns.regplot(data = df, x = "IOS_stranger", y = "LW_stranger", ci= 95, x_jitter = 0.05)
sns.despine()
# Calculate the statistics for the correlation regplot
pearsonr(x = IOS_stranger, y = LW_stranger)
# Calculate other correlational stats
pearsonr(x=IOS_mother, y=IOS_father)
# Plot mother father IOS results with some x axis jitter for clarity of visualization
plt.figure(figsize=(12,6))
fig3 = sns.regplot(data=df, x = IOS_mother, y = IOS_father, x_jitter = 0.2, ci)
sns.despine()
# Calculate mother stranger IOS correlation
pearsonr(x=IOS_mother, y=IOS_stranger)
# Plot mother father IOS results with some x axis jitter for clarity of visualization
plt.figure(figsize=(12,6))
fig3 = sns.regplot(data=df, x = IOS_mother, y = LW_mother, x_jitter = 0.2)
sns.despine()
pearsonr(x=IOS_mother, y=LW_mother)
# Plot it
plt.figure(figsize=(12,6))
fig4 = sns.regplot(data=df, x = IOS_mother, y = IOS_stranger, x_jitter = 0.1)
sns.despine()
pearsonr(x=IOS_father, y=IOS_stranger)
# Plot it
plt.figure(figsize=(12,6))
fig5 = sns.regplot(data=df,x = IOS_father, y = IOS_stranger, x_jitter=0.1)
sns.despine()
pearsonr(x=IOS_mother, y=LW_mother)
# Plot it with seaborn
plt.figure(figsize=(12,6),dpi=150)
fig6 = sns.regplot(data=df,x=IOS_mother, y=LW_mother, x_jitter=0.15)
plt.ylim(0,7.5)
sns.despine()
pearsonr(x=IOS_father, y=LW_father)
# Plot it
plt.figure(figsize=(12,8),dpi=600)
fig7 = sns.regplot(data=df,x = IOS_father, y = LW_father, x_jitter = 0.15, y_jitter = 0.15,color ='g')
plt.ylabel('Like winning father')
plt.ylim(0,7.5)
plt.xlim(0.8,7.5)
sns.despine()
# Plot mother father IOS results with some x axis jitter for clarity of visualization
plt.figure(figsize=(12,8),dpi=150)
fig3 = sns.regplot(data=df, x = IOS_stranger, y = LW_stranger, x_jitter = 0.15, y_jitter = 0.15)
plt.ylabel('Like Winning Stranger')
plt.ylim(0,7.5)
plt.xlim(0.8,7.5)
sns.despine()
pearsonr(x=LW_stranger,y=IOS_stranger)
# Plot it
plt.figure(figsize=(12,6))
fig8 = sns.regplot(data=df, x = LW_self, y = IOS_stranger, x_jitter=0.1)
sns.despine()
pearsonr(x=LW_self,y=IOS_stranger)
# Plot it
plt.figure(figsize=(12,6))
fig8 = sns.regplot(data=df, x = LW_mother, y = IOS_stranger, x_jitter=0.1)
sns.despine()
pearsonr(x=LW_mother,y=IOS_stranger)
# Plot it
plt.figure(figsize=(12,6))
fig8 = sns.regplot(data=df, x = LW_mother, y = IOS_stranger, x_jitter=0.1)
sns.despine()
# Plot with hue for sex/gender
fig10 = sns.lmplot(data = df, x = "LW_self", y = "IOS_stranger", hue = "sex", height = 8, x_jitter = 0.15)
sns.despine()
fig11 = sns.lmplot(data=df, x = '')
fig1 = sns.catplot(data=tidy, x='Target', y='NAcc',hue='Condition',kind='bar',ci=95,height=12)
(fig1.set_axis_labels('Condition','NAcc parameter estimates')
.set_titles("TEST"))
fig1.set(ylim=(-10,10))
# Same activation bar graph for LEFT & RIGHT NAcc ROI to validate our choice of averaging them into one result
fig1 = sns.catplot(data=tidy, x='Target', y='NAcc',hue='Condition',kind='bar',ci=95,height=12)
(fig1.set_axis_labels('Condition','NAcc parameter estimates')
.set_titles("TEST"))
fig2 = sns.catplot(data=df, y='Precuneus_FatherWin', kind='swarm',ci=95)
fig2 = sns.catplot(data=df, y='Precuneus_FatherNoWin', kind='swarm',ci=95,color='green')
fig2.set(ylim=(0,5))
sns.lmplot(data=df, x='IOS_father',y='Precuneus_FatherWin',ci=95,height=10, x_jitter=0.2)
sns.lmplot(data=df, x='LW_father',y='Precuneus_FatherWin',ci=95,height=10, x_jitter=0.2)
pearsonr(x=LW_father,y=df.Precuneus_FatherWin)
sns.lmplot(data=df, x='IOS_father',y='difference_fatherwin_father_nowin',ci=95,height=8, x_jitter=0.2)
pearsonr(x=df.IOS_father,y=df.difference_fatherwin_father_nowin)
sns.lmplot(data=df, x='LW_father',y='difference_fatherwin_father_nowin',ci=95,height=8, x_jitter=0.2)
pearsonr(x=df.LW_father,y=df.difference_fatherwin_father_nowin)
plt.figure(figsize=(15,10))
fig11 = sns.lineplot(data= df2, x = "time", y = "signal", hue = "condition", style = "condition",
markers = True, units = "subject", estimator = None)
fig11.set(xlim=(None,6))
plt.figure(figsize=(15,10))
fig11 = sns.lineplot(data= df2, x = "time", y = "signal", hue = "condition", style = "condition", markers = True, ci = 95)
fig11.set(xlim=(None,6))
# This is a quadratic regression line fit for the difference in NAcc scores and LW scores Mother - Stranger
# Plot it
plt.figure(figsize=(12,6),dpi=150)
fig12 = sns.regplot(data=df, x = 'Like Winning Mother_vs_Stranger', y = 'NAcc OtherWin Mother_vs_Stranger',order=2)
sns.despine()
# Let's run a residual plot for the data above to see if a linear regression is a best of fit model
sns.residplot(data=df, x = 'Like Winning Mother_vs_Stranger', y = 'NAcc OtherWin Mother_vs_Stranger',lowess=True)