# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Py 3.7 (fooof & mne)
#     language: python
#     name: fooof
# ---

# %matplotlib inline
# %load_ext autoreload
# %config InlineBackend.figure_format = 'retina' # high res plotting
import numpy as np
import scipy
import matplotlib.pyplot as plt
import IPython.display as ipd
import sys


from scipy import stats
import pandas as pd

# +
# Import Seaborn

import seaborn as sns
# sns.set_style('whitegrid')

# +
import matplotlib
# plot settings
# font = {'family' : 'Bitstream Vera Sans',
#         'weight' : 'regular',
#         'size'   : 13}
font = {'weight' : 'regular',
        'size'   : 13}
figure = {'figsize' : (20,8)}

matplotlib.rc('font', **font)
matplotlib.rc('figure', **figure)

# +
import sys

#file structure for /src
package_folder = '/home/debrown/Songbird-LFP-Paper/'
sys.path.append(package_folder)
# -

# from src.analysis.chunk_channel_dropping_report import make_optimzed_channel_dropping_report
from src.analysis.chunk_when_analysis_naive import load_pandas_from_pickle
from src.analysis.chunk_syllable_phase_preference_analysis import syllable_phase_preference_path
from src.analysis.chunk_when_analysis_naive import all_when_label_instructions, motif_length_dict

# +
# Path to saving Figures

from src.utils.paths import FIGURES_DIR

# +
# Designate the High Yield Days for Each Bird

z020_days = ['day-2016-06-03', 'day-2016-06-05']
z007_days = ['day-2016-09-10','day-2016-09-11']
z017_days = ['day-2016-06-19', 'day-2016-06-21']
# -

# # Import Results of Syllable Preference Analysis

# ## z020

# +
z020_bird_id = 'z020'

z020_day1 = load_pandas_from_pickle(data_name="syllable_phase_preference", bird_id=z020_bird_id,
                                    source=syllable_phase_preference_path, session=z020_days[0], verbose=True)

z020_day2 = load_pandas_from_pickle(data_name="syllable_phase_preference", bird_id=z020_bird_id,
                                    source=syllable_phase_preference_path, session=z020_days[1], verbose=True)

# +
z020_day1['Session'] = "Day 1"
z020_day2['Session'] = "Day 2"

full_results_z020 = pd.concat([z020_day1, z020_day2], axis=0)

# -

z020_when_instruct = all_when_label_instructions[z020_bird_id]

# ## z007

# +
z007_bird_id = 'z007'

z007_day1 = load_pandas_from_pickle(data_name="syllable_phase_preference", bird_id=z007_bird_id,
                                    source=syllable_phase_preference_path, session=z007_days[0], verbose=True)

z007_day2 = load_pandas_from_pickle(data_name="syllable_phase_preference", bird_id=z007_bird_id,
                                    source=syllable_phase_preference_path, session=z007_days[1], verbose=True)

# +
z007_day1['Session'] = "Day 1"
z007_day2['Session'] = "Day 2"

full_results_z007 = pd.concat([z007_day1, z007_day2], axis=0)
# -

full_results_z007.head()

z007_when_instruct = all_when_label_instructions[z007_bird_id]

# ## z017

# +
z017_bird_id = 'z017'

z017_day1 = load_pandas_from_pickle(data_name="syllable_phase_preference", bird_id=z017_bird_id,
                                    source=syllable_phase_preference_path, session=z017_days[0], verbose=True)

z017_day2 = load_pandas_from_pickle(data_name="syllable_phase_preference", bird_id=z017_bird_id,
                                    source=syllable_phase_preference_path, session=z017_days[1], verbose=True)

# +
z017_day1['Session'] = "Day 1"
z017_day2['Session'] = "Day 2"

full_results_z017 = pd.concat([z017_day1, z017_day2], axis=0)
# -

z017_when_instruct = all_when_label_instructions[z017_bird_id]

# # Run Statistics on the Results

z020_day1.head()

z020_day1[z020_day1['Syllable']=='2']['Difference']

z020_when_instruct

statistics, p_value = stats.wilcoxon(flat_diff_z_results[5], alternative='greater')

# ## z020

the_p_values = []
for syll in z020_when_instruct:
    statistics, p_value = stats.wilcoxon(z020_day1[z020_day1['Syllable']==str(syll)]['Difference'],
                                         alternative='greater')
    the_p_values.append(p_value)
    print(p_value)

# +
freq_p_values = np.asarray(the_p_values)  # Copy a ndarray of the p_values
p_values_ordered = np.sort(the_p_values)
p_value_order = np.argsort(freq_p_values)

significance = np.zeros(np.shape(freq_p_values))

for index, p_value in enumerate(p_values_ordered):
    threshold = ((index+1)/len(freq_p_values))*.05
    if p_value > threshold:
        significance[p_value_order[index]] = 1
significance

# +
# b_h_fdr_result = []
# for all_freq_p_values in all_ranked_p_values:
#     freq_p_values = np.asarray(all_freq_p_values)  # Copy a ndarray of the p_values
#     p_values_ordered = np.sort(all_freq_p_values)
#     p_value_order = np.argsort(freq_p_values)
    
#     significance = np.zeros(np.shape(freq_p_values))
    
#     for index, p_value in enumerate(p_values_ordered):
#         threshold = ((index+1)/len(freq_p_values))*.05
#         if p_value > threshold:
#             significance[p_value_order[index]] = 1
#     b_h_fdr_result.append(significance)            

# b_h_fdr_result

# +
the_p_values = []

for syll in z020_when_instruct:
    statistics, p_value = stats.wilcoxon(z020_day2[z020_day2['Syllable']==str(syll)]['Difference'],
                                         alternative='greater')
    the_p_values.append(p_value)
    print(p_value)

# +
freq_p_values = np.asarray(the_p_values)  # Copy a ndarray of the p_values
p_values_ordered = np.sort(the_p_values)
p_value_order = np.argsort(freq_p_values)

significance = np.zeros(np.shape(freq_p_values))

for index, p_value in enumerate(p_values_ordered):
    threshold = ((index+1)/len(freq_p_values))*.05
    if p_value > threshold:
        significance[p_value_order[index]] = 1
significance
# -

# ## z007

# +
the_p_values = []

for syll in z007_when_instruct:
    statistics, p_value = stats.wilcoxon(z007_day1[z007_day1['Syllable']==str(syll)]['Difference'],
                                         alternative='greater')
    the_p_values.append(p_value)
    print(p_value)

# +
freq_p_values = np.asarray(the_p_values)  # Copy a ndarray of the p_values
p_values_ordered = np.sort(the_p_values)
p_value_order = np.argsort(freq_p_values)

significance = np.zeros(np.shape(freq_p_values))

for index, p_value in enumerate(p_values_ordered):
    threshold = ((index+1)/len(freq_p_values))*.05
    if p_value > threshold:
        significance[p_value_order[index]] = 1
significance

# +
the_p_values = []

for syll in z007_when_instruct:
    statistics, p_value = stats.wilcoxon(z007_day2[z007_day2['Syllable']==str(syll)]['Difference'],
                                         alternative='greater')
    the_p_values.append(p_value)
    print(p_value)

# +
freq_p_values = np.asarray(the_p_values)  # Copy a ndarray of the p_values
p_values_ordered = np.sort(the_p_values)
p_value_order = np.argsort(freq_p_values)

significance = np.zeros(np.shape(freq_p_values))

for index, p_value in enumerate(p_values_ordered):
    threshold = ((index+1)/len(freq_p_values))*.05
    if p_value > threshold:
        significance[p_value_order[index]] = 1
significance
# -

# ## z017

# +
the_p_values = []

for syll in z017_when_instruct:
    statistics, p_value = stats.wilcoxon(z017_day1[z017_day1['Syllable']==str(syll)]['Difference'],
                                         alternative='greater')
    the_p_values.append(p_value)
    print(p_value)

# +
freq_p_values = np.asarray(the_p_values)  # Copy a ndarray of the p_values
p_values_ordered = np.sort(the_p_values)
p_value_order = np.argsort(freq_p_values)

significance = np.zeros(np.shape(freq_p_values))

for index, p_value in enumerate(p_values_ordered):
    threshold = ((index+1)/len(freq_p_values))*.05
    if p_value > threshold:
        significance[p_value_order[index]] = 1
significance

# +
the_p_values = []

for syll in z017_when_instruct:
    statistics, p_value = stats.wilcoxon(z017_day2[z017_day2['Syllable']==str(syll)]['Difference'],
                                         alternative='greater')
    the_p_values.append(p_value)
    print(p_value)

# +
freq_p_values = np.asarray(the_p_values)  # Copy a ndarray of the p_values
p_values_ordered = np.sort(the_p_values)
p_value_order = np.argsort(freq_p_values)

significance = np.zeros(np.shape(freq_p_values))

for index, p_value in enumerate(p_values_ordered):
    threshold = ((index+1)/len(freq_p_values))*.05
    if p_value > threshold:
        significance[p_value_order[index]] = 1
significance
# -

# # Make Summary Figure

# +
# [left, bottom, width, height]
# -

0.117647 *2

# +
fig = plt.figure(figsize=(8.5, 7), dpi = 300)

syll_colors= ['#B66DFF', '#db6e00', '#009292', '#924900', '#006DDB', '#FFFF6D', 'white', '#B6DBFF' , '#490092']

line_width = .5
subsize = 8
bigsize = 12
ticksize = 8


z007_ax = fig.add_axes([0.05882, 0.392839, 0.235294, 0.2857]) 

z020_ax = fig.add_axes([0.367634, 0.392839, 0.235294, 0.2857]) # z017

z017_ax = fig.add_axes([0.676448, 0.392839, 0.235294, 0.2857])

# z007
# Day 1
## Box Plot
sns.boxplot(x='Session', y= 'Difference', hue="Syllable", 
            hue_order= ['2', '3', '4', '5'],
            data= full_results_z007,
            order=['Day 1','Day 2'],
            palette = syll_colors,
            fliersize=line_width, 
            linewidth=line_width,
            ax=z007_ax)

z007_ax.set_ylim(-60, 140)


z007_ax.set_ylabel('Time Relative to True Onset (ms)')
z007_ax.tick_params(axis='both', which='major', labelsize=ticksize)
# ax1.set_xticklabels([])
# syll_2_1.set_yticklabels([])
z007_ax.xaxis.label.set_visible(False)
z007_ax.yaxis.label.set_visible(False)

# overall_legend = syll_2_1.get_legend()
z007_ax.legend('',frameon=False)

# z007
# Day 1
## Box Plot
sns.boxplot(x='Session', y= 'Difference', hue="Syllable", 
            hue_order= ['2', '3', '4'],
            data= full_results_z020,
            order=['Day 1','Day 2'],
            palette = syll_colors,
            fliersize=line_width, 
            linewidth=line_width,
            ax=z020_ax)

z020_ax.set_ylim(-60, 140)


z020_ax.set_ylabel('Time Relative to True Onset (ms)')
z020_ax.tick_params(axis='both', which='major', labelsize=ticksize)
# ax1.set_xticklabels([])
# syll_2_1.set_yticklabels([])
z020_ax.xaxis.label.set_visible(False)
z020_ax.yaxis.label.set_visible(False)
# z020_ax.set_yticklabels([])


# overall_legend = syll_2_1.get_legend()
z020_ax.legend('',frameon=False)

# z017
# Day 1
## Box Plot
sns.boxplot(x='Session', y= 'Difference', hue="Syllable", 
            hue_order= ['2', '3', '4', '5', '6', '7'],
            data= full_results_z017,
            order=['Day 1','Day 2'],
            palette = syll_colors,
            fliersize=line_width, 
            linewidth=line_width,
            ax=z017_ax)

z017_ax.set_ylim(-60, 140)


z017_ax.set_ylabel('Time Relative to True Onset (ms)')
z017_ax.tick_params(axis='both', which='major', labelsize=ticksize)
# ax1.set_xticklabels([])
# syll_2_1.set_yticklabels([])
z017_ax.xaxis.label.set_visible(False)
z017_ax.yaxis.label.set_visible(False)
# z017_ax.set_yticklabels([])


# overall_legend = syll_2_1.get_legend()
z017_ax.legend('',frameon=False)

# Save the Figure
figure_name = 'Syllable_Phase_Preference' + '_Supplement_cb.svg'
figure_type_folder = FIGURES_DIR / 'Syllable_Phase_Preference'

# Check if Folder Path Exists
if not figure_type_folder.exists():
    figure_type_folder.mkdir(parents=True, exist_ok=True)

figure_location = figure_type_folder / figure_name

# fig.savefig(figure_location, format='png')
fig.savefig(figure_location, dpi=300, papertype=None, format='svg')





plt.show()


StarcoderdataJupyterScriptsDedupFiltered