<jupyter_start><jupyter_text>Here, we need to analyse data from recordings. In particular, we should to measure amplitude ratio, decay time of 1 and 5 transporter<jupyter_code>from IPython import get_ipython
get_ipython().magic('reset -sf') 
import pandas as pd
import glob
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch
import seaborn as sns
import matplotlib.ticker as ticker
from statannot import add_stat_annotation<jupyter_output><empty_output><jupyter_text>Step 1. Import data from excel to pandas df<jupyter_code>def file_extraction(search_string, set): 
    filepaths = glob.glob(search_string) 
    df = pd.DataFrame()
    col_name_suffix = 1
    for find_files in filepaths:
        if set == 'ctrl':
            df = df.join (pd.read_excel(find_files, sheet_name='CTRL'), how='outer',lsuffix=col_name_suffix)
            col_name_suffix +=1
        else: 
            df = df.join (pd.read_excel(find_files, sheet_name='SE'), how='outer',lsuffix=col_name_suffix)
            col_name_suffix +=1
    return(df)
rec_count_CTRL= file_extraction('**/rec*.xlsx', set = 'ctrl')
rec_count_SE = file_extraction('**/rec*.xlsx', set = 'SE')
all_rec_count = pd.concat([rec_count_CTRL, rec_count_SE], ignore_index= True)
all_rec_count<jupyter_output><empty_output><jupyter_text>Step 2. Calculate amplitude ratio and decay time Calculate amplitude and decay time ratio as ratio IGluT(5) to IGluT(1)<jupyter_code>all_rec_count ['IGluT(5)/IGluT(1)'] = all_rec_count['IGluT(5)']/all_rec_count['IGluT(1)']
all_rec_count ['τdecay IGluT(5)/τdecay IGluT(1)'] = all_rec_count['τdecay IGluT(5)']/all_rec_count['τdecay IGluT(1)']
all_rec_count
def adjust_box_widths(g, fac): # from https://stackoverflow.com/questions/56838187/how-to-create-spacing-between-same-subgroup-in-seaborn-boxplot
    """
    Adjust the withs of a seaborn-generated boxplot.
    """

    # iterating through Axes instances
    for ax in g.axes:

        # iterating through axes artists:
        for c in ax.get_children():

            # searching for PathPatches
            if isinstance(c, PathPatch):
                # getting current width of box:
                p = c.get_path()
                verts = p.vertices
                verts_sub = verts[:-1]
                xmin = np.min(verts_sub[:, 0])
                xmax = np.max(verts_sub[:, 0])
                xmid = 0.5*(xmin+xmax)
                xhalf = 0.5*(xmax - xmin)

                # setting new width of box
                xmin_new = xmid-fac*xhalf
                xmax_new = xmid+fac*xhalf
                verts_sub[verts_sub[:, 0] == xmin, 0] = xmin_new
                verts_sub[verts_sub[:, 0] == xmax, 0] = xmax_new

                # setting new width of median line
                for l in ax.lines:
                    if np.all(l.get_xdata() == [xmin, xmax]):
                        l.set_xdata([xmin_new, xmax_new])
def stat_to_list(stat): 
    l = []
    for i in stat:
        l.append(str(i).split(','))
    for i in range(len(l)):
            l[i][1] = l[i].extend(l[i][1].split())
            l[i][1]= l[i].extend(l[i][2].split('='))
            l[i][1]= l[i].extend(l[i][3].split('='))
            del l[i][1]
            del l[i][1]
            del l[i][1]
    return l<jupyter_output><empty_output><jupyter_text>Transform df to create hue-groupped boxplot.<jupyter_code>all_rec_count.drop(all_rec_count[all_rec_count['τdecay IGluT(5)']< 8].index, inplace=True) #delete outliers

all_rec_count_ampl = pd.melt (all_rec_count, id_vars= ['Group'], 
                            value_vars=['IGluT(1)','IGluT(5)'],
                            var_name= '', value_name= 'Амплитуда, pA')
all_rec_count_decay = pd.melt (all_rec_count, id_vars= ['Group'], 
                            value_vars=['τdecay IGluT(1)','τdecay IGluT(5)'],
                            var_name= '', value_name= 'τdecay, мс')
all_rec_count_ampl
def boxplot_graph_hue(data, dots, fig_name, big_tick, ylim, xyhue, box_pairs): 
    fig_name = 'figs/recordings calculate/' + fig_name + '.tif'
    fig = plt.figure(figsize=(12,7)) # меняет размер графика
    custom_params = {"axes.spines.right": False, "axes.spines.top": False}
    sns.set_theme(font_scale=1.4, style="ticks",context="poster", rc= custom_params)
    plot = sns.boxplot(data = data, x = xyhue[0], y = xyhue[1], hue = xyhue[2], 
                                            palette="vlag", linewidth = 7, dodge=True)
    
    plot.spines['left'].set_linewidth(4)
    plot.spines['bottom'].set_linewidth(4)
    plot.yaxis.set_major_locator(ticker.MultipleLocator(big_tick))
    plt.ylim(ylim)
    stat = add_stat_annotation(plot, data=data,x = xyhue[0], y = xyhue[1], hue = xyhue[2],
                    box_pairs=box_pairs,loc = 'outside',
                    test='Mann-Whitney', text_format='star',  verbose=1, comparisons_correction=None, linewidth=4) #loc = 'outside'
    if dots == True:
        sns.swarmplot(x = xyhue[0], y = xyhue[1], hue = xyhue[2], data=data, 
                                            size = 15, palette = "Set2", linewidth=4 ,dodge = True)
    handles, labels = plot.get_legend_handles_labels()
    plt.legend(handles[0:2], labels[0:2], loc=2, bbox_to_anchor=(1.03, 1), borderaxespad=0)
    adjust_box_widths(fig, 0.9)
    plot = plot.get_figure()
    plt.tight_layout()                                         
    plot.savefig(fig_name, dpi=800)
    return stat
ax, stat  = boxplot_graph_hue(all_rec_count_ampl, dots = True, fig_name = 'амлитуды',big_tick = 25, ylim = [-100,0], 
                xyhue =['','Амплитуда, pA','Group'], box_pairs = [(('IGluT(1)', 'Контроль'),( 'IGluT(1)', 'ЭС')),
                                                                 (('IGluT(5)', 'Контроль'),('IGluT(5)', 'ЭС'))])
stat_list = []
stat_list.extend(stat_to_list(stat))   

ax, stat = boxplot_graph_hue(all_rec_count_decay, dots = True, fig_name = 'время затухания',big_tick = 2, ylim = [6,18], 
                xyhue =['','τdecay, мс','Group'], box_pairs = [(('τdecay IGluT(1)', 'Контроль'),( 'τdecay IGluT(1)', 'ЭС')),
                                                                 (('τdecay IGluT(5)', 'Контроль'),('τdecay IGluT(5)', 'ЭС'))])
stat_list.extend(stat_to_list(stat))  
all_rec_count.drop(all_rec_count[all_rec_count['IGluT(5)/IGluT(1)']< 0.55].index, inplace=True) #удалим выбросы

fig = plt.figure(figsize=(6,8)) # меняет размер графика
custom_params = {"axes.spines.right": False, "axes.spines.top": False}
sns.set_theme(font_scale=1.5, style="ticks",context="poster", rc= custom_params)
plot = sns.boxplot (data = all_rec_count, x=all_rec_count['Group'], y = all_rec_count['IGluT(5)/IGluT(1)'],  palette="vlag", linewidth = 7, dodge=True)
plot.spines['left'].set_linewidth(4)
plot.spines['bottom'].set_linewidth(4)
plot.yaxis.set_major_locator(ticker.MultipleLocator(0.25))
plt.ylim([0.75,2])
ax, stat  = add_stat_annotation(plot, data = all_rec_count, x=all_rec_count['Group'], y = all_rec_count['IGluT(5)/IGluT(1)'],
                    box_pairs=[("Контроль", "ЭС")],loc = 'outside',
                    test='Mann-Whitney', text_format='star',  verbose=1, comparisons_correction=None, linewidth=4) #loc = 'outside'
sns.stripplot(data=all_rec_count, x=all_rec_count['Group'], y = all_rec_count['IGluT(5)/IGluT(1)'],
              size=17, linewidth=4,palette = "Set2", jitter = True)
plot = plot.get_figure()
plt.tight_layout() 
plot.savefig('figs/recordings calculate/отношения амплитуд', dpi=800)

stat_list.extend(stat_to_list(stat))  
all_rec_count.drop(all_rec_count[all_rec_count['τdecay IGluT(5)/τdecay IGluT(1)']> 1.7].index, inplace=True) #удалим выбросы

fig = plt.figure(figsize=(6,8)) # меняет размер графика
custom_params = {"axes.spines.right": False, "axes.spines.top": False}
sns.set_theme(font_scale=1.5, style="ticks",context="poster", rc= custom_params)
plot = sns.boxplot (data = all_rec_count, x=all_rec_count['Group'], y = all_rec_count['τdecay IGluT(5)/τdecay IGluT(1)'],  palette="vlag", linewidth = 7, dodge=True)
plot.spines['left'].set_linewidth(4)
plot.spines['bottom'].set_linewidth(4)
plot.yaxis.set_major_locator(ticker.MultipleLocator(0.25))
plt.ylim([0.5,1.75])
ax, stat = add_stat_annotation(plot, data = all_rec_count, x=all_rec_count['Group'], y = all_rec_count['τdecay IGluT(5)/τdecay IGluT(1)'],
                    box_pairs=[("Контроль", "ЭС")],loc = 'outside',
                    test='Mann-Whitney', text_format='star',  verbose=1, comparisons_correction=None, linewidth=4) #loc = 'outside'
sns.stripplot(data=all_rec_count, x=all_rec_count['Group'], y = all_rec_count['τdecay IGluT(5)/τdecay IGluT(1)'],
              size=17, linewidth=4,palette = "Set2", jitter = True)
plot = plot.get_figure()
plt.tight_layout() 
plot.savefig('figs/recordings calculate/отношения времени затухания', dpi=800)

stat_list.extend(stat_to_list(stat))<jupyter_output>p-value annotation legend:
ns: 5.00e-02 < p <= 1.00e+00
*: 1.00e-02 < p <= 5.00e-02
**: 1.00e-03 < p <= 1.00e-02
***: 1.00e-04 < p <= 1.00e-03
****: p <= 1.00e-04

Контроль v.s. ЭС: Mann-Whitney-Wilcoxon test two-sided, P_val=8.225e-02 U_stat=5.000e+00<jupyter_text>Create p-value containing df to export to excel<jupyter_code>def df_pval_tramsform(stat_list):
    df_stat_out = pd.DataFrame(stat_list, columns = ['Test', 'feature', 'P-Val', 'feature2','U-stat'] ,index=['IGluT(5)', 'IGluT(1)', 'τdecay IGluT(5)', 'τdecay IGluT(1)','IGluT(5)/IGluT(1)', 'τd IGluT(5)/τd IGluT(1)'])
    df_stat_out.drop(['feature','feature2'], axis= 'columns', inplace= True)
    return df_stat_out

def df_out_tramsform(all_rec_count):
    mask_ctrl = all_rec_count['Group']=='Контроль'
    perc_cell_count_CTRL = all_rec_count[mask_ctrl].quantile([0.25, 0.50 , 0.75])
    mask_SE = all_rec_count['Group']=='ЭС'

    perc_cell_count_SE = all_rec_count[mask_SE].quantile([0.25, 0.50 , 0.75])
    perc_cell_count_CTRL ['Group'] = 'Контроль'
    perc_cell_count_SE ['Group'] = 'ЭС'
    perc_out = pd.concat([perc_cell_count_CTRL, perc_cell_count_SE])
    return perc_out 
df_stat_out = df_pval_tramsform(stat_list)
perc_out = df_out_tramsform(all_rec_count)
df_stat_out
perc_out
with pd.ExcelWriter('output_table/final_out_rec_count.xlsx') as writer:  
     perc_out.to_excel(writer, sheet_name='Percentiles', float_format="%.2f")
     df_stat_out.to_excel(writer, sheet_name='P-val, MW(U)', float_format="%.2f")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15513425079395632, 'fraction_numerical': 0.025214127610432104, 'mean_word_length': 3.5221932114882506, 'pattern_counts': {'":': 6, '<': 31, '<?xml version=': 0, '>': 21, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '457894', 'license': 'MIT', 'n_tokens_mistral': 4167, 'n_tokens_neox': 3963, 'n_words': 680, 'path': 'old_notebooks/3_recordings calculate.ipynb', 'repo_name': 'Nosa1323/ICH_table_analysis', 'size': '10391'}