Skip to content

Commit

Permalink
Update to return p-values
Browse files Browse the repository at this point in the history
  • Loading branch information
TomDonoghue committed Jan 11, 2020
1 parent 54355e7 commit cf69446
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
41 changes: 23 additions & 18 deletions bratios/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,17 @@ def get_wave_params(band_label):
return [curr_band + "_" + feat for feat in FEATURE_LABELS]


def print_aperiodic_correlation(ratio_type, corr, show=True):
def print_aperiodic_correlation(ratio_type, corr, ps=None, show=True):
"""Prints out the correlation between ratio and aperiodic parameters.
Parameters
----------
ratio_type : string
Which specific ratio measure to report (ex. "TBR").
corr : list of floats
corr : list of float
Correlation r-values for aperiodic params.
ps : list of float
P-values for the correlations.
show : boolean
Whether to display the print out.
Expand All @@ -105,7 +107,8 @@ def print_aperiodic_correlation(ratio_type, corr, show=True):

if show:
for ind, param in enumerate(["Exp", "Off", "Age"]):
print("The corr of {} to {} is {:1.2f}".format(ratio_type, param, corr[ind]))
p_txt = "\t with a p-value of {:1.4f}".format(ps[ind]) if isinstance(ps, np.ndarray) else ""
print(("The corr of {} to {} is {:1.2f}" + p_txt).format(ratio_type, param, corr[ind]))


def get_non_ratio_band(ratio_type):
Expand Down Expand Up @@ -139,8 +142,7 @@ def param_ratio_corr(df, ratio_type, ch_inds, func=nan_corr_pearson):
df : 2D DataFrame
Container which holds ratio, channels, and peak values.
ratio_type : string
Ratio measure to run correlations across.
ex) "TBR"
Ratio measure to run correlations across. ex: "TBR".
ch_inds : list of ints
Channels to run correlations over.
func : callable
Expand All @@ -163,23 +165,27 @@ def param_ratio_corr(df, ratio_type, ch_inds, func=nan_corr_pearson):
alpha = get_wave_params("A")
beta = get_wave_params("B")

theta_corrs = np.zeros(3)
alpha_corrs = np.zeros(3)
beta_corrs = np.zeros(3)
# Initialize variables to store periodic & aperiodic correlation results
per_corrs = np.zeros([3, 3])
per_ps = np.zeros([3, 3])
ap_corrs = np.zeros(3)
ap_ps = np.zeros(3)

# Ratio vs spectral params correlation
for ind in range(3):
theta_corrs[ind] = func(rel_df[theta[ind]], rel_df[ratio_type])[0]
alpha_corrs[ind] = func(rel_df[alpha[ind]], rel_df[ratio_type])[0]
beta_corrs[ind] = func(rel_df[beta[ind]], rel_df[ratio_type])[0]
per_corrs[0, ind], per_ps[0, ind] = \
func(rel_df[theta[ind]], rel_df[ratio_type])
per_corrs[1, ind], per_ps[1, ind] = \
func(rel_df[alpha[ind]], rel_df[ratio_type])
per_corrs[2, ind], per_ps[2, ind] = \
func(rel_df[beta[ind]], rel_df[ratio_type])

# Ratio vs aperiodic params correlation
ap_corrs[0] = func(rel_df["Exp"], rel_df[ratio_type])[0]
ap_corrs[1] = func(rel_df["Off"], rel_df[ratio_type])[0]
ap_corrs[2] = func(rel_df["Age"], rel_df[ratio_type])[0]
ap_corrs[0], ap_ps[0] = func(rel_df["Exp"], rel_df[ratio_type])
ap_corrs[1], ap_ps[1] = func(rel_df["Off"], rel_df[ratio_type])
ap_corrs[2], ap_ps[2] = func(rel_df["Age"], rel_df[ratio_type])

return np.stack((theta_corrs, alpha_corrs,beta_corrs)), ap_corrs
return per_corrs, ap_corrs, per_ps, ap_ps


def _add_params(curr_row, theta_params, beta_params, alpha_params, ap):
Expand Down Expand Up @@ -361,8 +367,7 @@ def param_corr(df, corr_label_1, corr_label_2, chan_inds, func):
Returns
-------
float describing results from correlation function.
R-value and p-value from the correlation function.
"""

# Select relevant rows from df
Expand All @@ -371,4 +376,4 @@ def param_corr(df, corr_label_1, corr_label_2, chan_inds, func):
# Average across selected channels per subject
rel_df = rel_df.groupby("Subj_ID").mean()

return func(rel_df[corr_label_1], rel_df[corr_label_2])[0]
return func(rel_df[corr_label_1], rel_df[corr_label_2])
37 changes: 20 additions & 17 deletions notebooks/5-EEG-Analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@
"outputs": [],
"source": [
"# Calculate the correlation between ratios and spectral features\n",
"tbr_per, tbr_ap = param_ratio_corr(df, \"TBR\", all_chans, func=nan_corr_spearman)"
"tbr_per, tbr_ap, tbr_per_ps, tbr_ap_ps = \\\n",
" param_ratio_corr(df, \"TBR\", all_chans, func=nan_corr_spearman)"
]
},
{
Expand Down Expand Up @@ -494,7 +495,7 @@
"outputs": [],
"source": [
"# Frontal Channels\n",
"tbr_per_fr, tbr_ap_fr = param_ratio_corr(df, \"TBR\", ch_inds_frontal, func=nan_corr_spearman)\n",
"tbr_per_fr, tbr_ap_fr, _, _ = param_ratio_corr(df, \"TBR\", ch_inds_frontal, func=nan_corr_spearman)\n",
"print_aperiodic_correlation(\"TBR\", tbr_ap_fr, show=SHOW_SUB)\n",
"\n",
"plot_param_ratio_corr(tbr_per_fr, show=SHOW_SUB,\n",
Expand All @@ -510,7 +511,7 @@
"outputs": [],
"source": [
"# Central channels\n",
"tbr_per_ce, tbr_ap_ce = param_ratio_corr(df, \"TBR\", ch_inds_central, func=nan_corr_spearman)\n",
"tbr_per_ce, tbr_ap_ce, _, _ = param_ratio_corr(df, \"TBR\", ch_inds_central, func=nan_corr_spearman)\n",
"print_aperiodic_correlation(\"TBR\", tbr_ap_ce, show=SHOW_SUB)\n",
"\n",
"plot_param_ratio_corr(tbr_per_ce, show=SHOW_SUB,\n",
Expand All @@ -526,7 +527,7 @@
"outputs": [],
"source": [
"# Parietal channels\n",
"tbr_per_pa, tbr_ap_pa = param_ratio_corr(df, \"TBR\", ch_inds_parietal, func=nan_corr_spearman)\n",
"tbr_per_pa, tbr_ap_pa, _, _ = param_ratio_corr(df, \"TBR\", ch_inds_parietal, func=nan_corr_spearman)\n",
"print_aperiodic_correlation(\"TBR\", tbr_ap_pa, show=SHOW_SUB)\n",
"\n",
"plot_param_ratio_corr(tbr_per_pa, show=SHOW_SUB,\n",
Expand Down Expand Up @@ -560,7 +561,8 @@
"outputs": [],
"source": [
"# Calculate the correlation between ratios and spectral features\n",
"tar_per, tar_ap = param_ratio_corr(df, \"TAR\", all_chans, func=nan_corr_spearman)"
"tar_per, tar_ap, tar_per_ps, tar_ap_ps = \\\n",
" param_ratio_corr(df, \"TAR\", all_chans, func=nan_corr_spearman)"
]
},
{
Expand Down Expand Up @@ -627,7 +629,7 @@
"outputs": [],
"source": [
"# Frontal Channels\n",
"tar_per_fr, tar_ap_fr = param_ratio_corr(df, \"TAR\", ch_inds_frontal, func=nan_corr_spearman)\n",
"tar_per_fr, tar_ap_fr, _, _ = param_ratio_corr(df, \"TAR\", ch_inds_frontal, func=nan_corr_spearman)\n",
"print_aperiodic_correlation(\"TAR\", tar_ap_fr, show=SHOW_SUB)\n",
"\n",
"plot_param_ratio_corr(tar_per_fr, show=SHOW_SUB,\n",
Expand All @@ -643,7 +645,7 @@
"outputs": [],
"source": [
"# Central Channels\n",
"tar_per_ce, tar_ap_ce = param_ratio_corr(df, \"TAR\", ch_inds_central, func=nan_corr_spearman)\n",
"tar_per_ce, tar_ap_ce, _, _ = param_ratio_corr(df, \"TAR\", ch_inds_central, func=nan_corr_spearman)\n",
"print_aperiodic_correlation(\"TAR\", tar_ap_ce, show=SHOW_SUB)\n",
"\n",
"plot_param_ratio_corr(tar_per_ce, show=SHOW_SUB,\n",
Expand All @@ -659,7 +661,7 @@
"outputs": [],
"source": [
"# Parietal Channels\n",
"tar_per_pa, tar_ap_pa = param_ratio_corr(df, \"TAR\", ch_inds_parietal, func=nan_corr_spearman)\n",
"tar_per_pa, tar_ap_pa, _, _ = param_ratio_corr(df, \"TAR\", ch_inds_parietal, func=nan_corr_spearman)\n",
"print_aperiodic_correlation(\"TAR\", tar_ap_pa, show=SHOW_SUB)\n",
"\n",
"plot_param_ratio_corr(tar_per_pa, show=SHOW_SUB,\n",
Expand Down Expand Up @@ -693,7 +695,8 @@
"outputs": [],
"source": [
"# Calculate the correlation between ratios and spectral features\n",
"abr_per, abr_ap = param_ratio_corr(df, \"ABR\", all_chans, func=nan_corr_spearman)"
"abr_per, abr_ap, abr_per_ps, abr_ap_ps = \\\n",
" param_ratio_corr(df, \"ABR\", all_chans, func=nan_corr_spearman)"
]
},
{
Expand Down Expand Up @@ -760,7 +763,7 @@
"outputs": [],
"source": [
"# Frontal Channels\n",
"abr_per_fr, abr_ap_fr = param_ratio_corr(df, \"ABR\", ch_inds_frontal, func=nan_corr_spearman)\n",
"abr_per_fr, abr_ap_fr, _, _ = param_ratio_corr(df, \"ABR\", ch_inds_frontal, func=nan_corr_spearman)\n",
"print_aperiodic_correlation(\"ABR\", abr_ap_fr, show=SHOW_SUB)\n",
"\n",
"plot_param_ratio_corr(abr_per_fr, show=SHOW_SUB,\n",
Expand All @@ -776,7 +779,7 @@
"outputs": [],
"source": [
"# Central Channels\n",
"abr_per_ce, abr_ap_ce = param_ratio_corr(df, \"ABR\", ch_inds_central, func=nan_corr_spearman)\n",
"abr_per_ce, abr_ap_ce, _, _ = param_ratio_corr(df, \"ABR\", ch_inds_central, func=nan_corr_spearman)\n",
"print_aperiodic_correlation(\"ABR\", abr_ap_ce, show=SHOW_SUB)\n",
"\n",
"plot_param_ratio_corr(abr_per_ce, show=SHOW_SUB,\n",
Expand All @@ -792,7 +795,7 @@
"outputs": [],
"source": [
"# Parietal Channels\n",
"abr_per_pa, abr_ap_pa = param_ratio_corr(df, \"ABR\", ch_inds_parietal, func=nan_corr_spearman)\n",
"abr_per_pa, abr_ap_pa, _, _ = param_ratio_corr(df, \"ABR\", ch_inds_parietal, func=nan_corr_spearman)\n",
"print_aperiodic_correlation(\"ABR\", abr_ap_pa, show=SHOW_SUB)\n",
"\n",
"plot_param_ratio_corr(abr_per_pa, show=SHOW_SUB,\n",
Expand Down Expand Up @@ -825,8 +828,8 @@
],
"source": [
"# Check correlation across all channels\n",
"print('Age-EXP corr global: \\t\\t {:1.4f}'.format(\n",
" param_corr(df, \"Age\", \"Exp\", all_chans, nan_corr_spearman)))"
"age_exp_corr, age_exp_p = param_corr(df, \"Age\", \"Exp\", all_chans, nan_corr_spearman)\n",
"print('Age-EXP corr global: \\t\\t {:1.4f}'.format(age_exp_corr))"
]
},
{
Expand All @@ -847,11 +850,11 @@
"source": [
"# Check correlation of age and exponent within channel sub-selections\n",
"print('Age-EXP corr frontal: \\t\\t {:1.4f}'.format(\n",
" param_corr(df, \"Age\", \"Exp\", ch_inds_frontal, nan_corr_spearman)))\n",
" param_corr(df, \"Age\", \"Exp\", ch_inds_frontal, nan_corr_spearman)[0]))\n",
"print('Age-EXP corr central: \\t\\t {:1.4f}'.format(\n",
" param_corr(df, \"Age\", \"Exp\", ch_inds_central, nan_corr_spearman)))\n",
" param_corr(df, \"Age\", \"Exp\", ch_inds_central, nan_corr_spearman)[0]))\n",
"print('Age-EXP corr parietal: \\t {:1.4f}'.format(\n",
" param_corr(df, \"Age\", \"Exp\", ch_inds_parietal, nan_corr_spearman)))"
" param_corr(df, \"Age\", \"Exp\", ch_inds_parietal, nan_corr_spearman)[0]))"
]
}
],
Expand Down
11 changes: 6 additions & 5 deletions notebooks/6-EEG-Topographies.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"outputs": [],
"source": [
"# Settings\n",
"SAVE_FIG = True"
"SAVE_FIG = False"
]
},
{
Expand Down Expand Up @@ -568,11 +568,12 @@
"outputs": [],
"source": [
"corr_mat = np.zeros((3, 4))\n",
"ps_mat = np.zeros((3, 4))\n",
"for ind, ratio in enumerate([tbrs, tars, abrs]):\n",
" corr_mat[ind][0] = spearmanr(ratio, theta_pw)[0]\n",
" corr_mat[ind][1] = spearmanr(ratio, alpha_pw)[0]\n",
" corr_mat[ind][2] = spearmanr(ratio, beta_pw)[0]\n",
" corr_mat[ind][3] = spearmanr(ratio, exps)[0]"
" corr_mat[ind][0], ps_mat[ind][0] = spearmanr(ratio, theta_pw)\n",
" corr_mat[ind][1], ps_mat[ind][1] = spearmanr(ratio, alpha_pw)\n",
" corr_mat[ind][2], ps_mat[ind][2] = spearmanr(ratio, beta_pw)\n",
" corr_mat[ind][3], ps_mat[ind][3] = spearmanr(ratio, exps)"
]
},
{
Expand Down

0 comments on commit cf69446

Please sign in to comment.