model_mutations_2022/Code/csv_generation/SA_collection.py
2022-10-27 11:06:25 -04:00

297 lines
17 KiB
Python

import numpy as np
import h5py
import pandas as pd
import os
from ast import literal_eval
import json
import scipy.stats as stats
# folder
top_dir = '../Sensitivity_Analysis'
#%% create JSON files for each alt type
for dir_name in next(os.walk(top_dir))[1]:
folder = os.path.join(top_dir, dir_name)
print(folder)
# for each alt_type create pd df
shift_AUC = pd.DataFrame()
shift_rheo = pd.DataFrame()
shift_fI = pd.DataFrame(dtype=object)
shift_I_mag = pd.DataFrame(dtype=object)
slope_AUC = pd.DataFrame()
slope_rheo = pd.DataFrame()
slope_fI = pd.DataFrame(dtype=object)
slope_I_mag = pd.DataFrame(dtype=object)
g_AUC = pd.DataFrame()
g_rheo = pd.DataFrame()
g_fI = pd.DataFrame(dtype=object)
g_I_mag = pd.DataFrame(dtype=object)
for root, dirs, files in os.walk(folder):
for file in files:
if file.endswith('.hdf5'):
with h5py.File(os.path.join(folder, file), "r+") as f:
alt = f['data'].attrs['alteration']
test = f['data'].attrs['alteration_info'].replace(' ', ',')
alt_info = literal_eval(test)
var = alt_info[0]
alt_type = alt_info[1]
if alt_type == 'shift':
shift_AUC.loc[alt, var] = f['analysis']['AUC'][()]
try:
shift_rheo.loc[alt, var] = f['analysis']['rheobase'][()]
except:
print('shift', var, alt)
shift_fI.loc[alt, var] = 0
shift_fI = shift_fI.astype(object)
shift_fI.at[alt, var] = f['analysis']['F_inf'][:].tolist()
shift_I_mag.loc[alt, var] = 0
shift_I_mag = shift_I_mag.astype(object)
shift_I_mag.at[alt, var] = ((np.arange(f['data'].attrs['I_low'], f['data'].attrs['I_high'],
(f['data'].attrs['I_high'] - f['data'].attrs['I_low']) /
f['data'].attrs['stim_num'])) * 1000).tolist() #nA
elif alt_type == 'slope':
slope_AUC.loc[alt, var] = f['analysis']['AUC'][()]
try:
slope_rheo.loc[alt, var] = f['analysis']['rheobase'][()]
except:
print('slope', var, alt)
slope_fI.loc[alt, var] = 0
slope_fI = slope_fI.astype(object)
slope_fI.at[alt, var] = f['analysis']['F_inf'][:].tolist()
slope_I_mag.loc[alt, var] = 0
slope_I_mag = slope_I_mag.astype(object)
slope_I_mag.at[alt, var] = ((np.arange(f['data'].attrs['I_low'], f['data'].attrs['I_high'],
(f['data'].attrs['I_high'] - f['data'].attrs['I_low']) /
f['data'].attrs['stim_num'])) * 1000).tolist()
elif alt_type == 'g':
g_AUC.loc[alt, var] = f['analysis']['AUC'][()]
try:
g_rheo.loc[alt, var] = f['analysis']['rheobase'][()]
except:
print('g', var, alt)
g_fI.loc[alt, var] = 0
g_fI = g_fI.astype(object)
g_fI.at[alt, var] = f['analysis']['F_inf'][:].tolist()
g_I_mag.loc[alt, var] = 0
g_I_mag = g_I_mag.astype(object)
g_I_mag.at[alt, var] = ((np.arange(f['data'].attrs['I_low'], f['data'].attrs['I_high'],
(f['data'].attrs['I_high'] - f['data'].attrs['I_low']) /
f['data'].attrs['stim_num'])) * 1000).tolist()
else:
print(file, 'Unknown alteration type')
#save df with folder+alt_type
save_folder = os.path.join(top_dir, 'SA_summary_df')
if not os.path.isdir(save_folder):
os.makedirs(save_folder)
shift_AUC.to_json(os.path.join(save_folder, '{}_shift_AUC.json'.format(dir_name)))
shift_rheo.to_json(os.path.join(save_folder, '{}_shift_rheo.json'.format(dir_name)))
shift_fI.to_json(os.path.join(save_folder, '{}_shift_fI.json'.format(dir_name)))
shift_I_mag.to_json(os.path.join(save_folder, '{}_shift_I_mag.json'.format(dir_name)))
slope_AUC.to_json(os.path.join(save_folder, '{}_slope_AUC.json'.format(dir_name)))
slope_rheo.to_json(os.path.join(save_folder, '{}_slope_rheo.json'.format(dir_name)))
slope_fI.to_json(os.path.join(save_folder, '{}_slope_fI.json'.format(dir_name)))
slope_I_mag.to_json(os.path.join(save_folder, '{}_slope_I_mag.json'.format(dir_name)))
g_AUC.to_json(os.path.join(save_folder, '{}_g_AUC.json'.format(dir_name)))
g_rheo.to_json(os.path.join(save_folder, '{}_g_rheo.json'.format(dir_name)))
g_fI.to_json(os.path.join(save_folder, '{}_g_fI.json'.format(dir_name)))
g_I_mag.to_json(os.path.join(save_folder, '{}_g_I_mag.json'.format(dir_name)))
#%% AUC Correlation analysis
alt_dict = {}
alt_dict['m'] = 'Na activation'
alt_dict['h'] = 'Na inactivation'
alt_dict['n'] = 'K activation'
alt_dict['s'] = '$K_V1.1$ activation'
alt_dict['u'] = '$K_V1.1$inactivation'
alt_dict['a'] = 'A activation'
alt_dict['b'] = 'A inactivation'
alt_dict['n_A'] = 'A activation'
alt_dict['h_A'] = 'A inactivation'
alt_dict['Na'] = 'Na'
alt_dict['Kd'] = 'K'
alt_dict['Kv'] = '$K_V1.1$'
alt_dict['A'] = 'A'
alt_dict['Leak'] = 'Leak'
models =['RS_pyramidal', 'RS_inhib', 'FS', 'IB','Cb_stellate','Cb_stellate_Kv','Cb_stellate_Kv_only','STN','STN_Kv','STN_Kv_only']
model_names = ['RS pyramidal', 'RS inhibitory', 'FS', 'IB','Cb stellate','Cb stellate +$K_V1.1$','Cb stellate $\Delta$$K_V1.1$','STN','STN +$K_V1.1$','STN $\Delta$$K_V1.1$']
shift_df = pd.DataFrame(columns=['model', 'corr', 'p_value', 'local corr', 'local p_value', 'ratio', '$\Delta V_{1/2}$', 'color']) # for boxplots
for mod in range(len(models)):
with open('./SA_summary_df/{}_shift_AUC.json'.format(models[mod])) as json_file:
df = pd.read_json(json_file)
df.sort_index(inplace=True)
df.replace(0., np.NaN, inplace=True)
df = (df - df.loc[0, :]) / df.loc[0, :]
zero_ind = np.argwhere(df.index == 0)[0][0]
ind = [df.index[zero_ind - 1], df.index[zero_ind], df.index[zero_ind + 1]]
df2 = df.loc[ind, :]
for c in df.keys():
tau, p = stats.kendalltau(df.index, df[c], nan_policy='omit')
tau_l, p_l = stats.kendalltau(df2.index, df2[c], nan_policy='omit')
ratio_tau = tau_l / tau
shift_df = shift_df.append(pd.Series([model_names[mod], tau, p,tau_l, p_l, ratio_tau, alt_dict[c],clr_dict[models[mod]]], index=shift_df.columns), ignore_index=True)
models =['RS_pyramidal', 'RS_inhib', 'FS', 'IB','Cb_stellate','Cb_stellate_Kv','Cb_stellate_Kv_only','STN','STN_Kv','STN_Kv_only']
model_names = ['RS pyramidal', 'RS inhibitory', 'FS', 'IB','Cb stellate','Cb stellate +$K_V1.1$','Cb stellate $\Delta$$K_V1.1$','STN','STN +$K_V1.1$','STN $\Delta$$K_V1.1$']
slope_df = pd.DataFrame(columns=['model', 'corr', 'p_value','local corr', 'local p_value', 'ratio', 'Slope (k)', 'color']) # for boxplots
for mod in range(len(models)):
with open('./SA_summary_df/{}_slope_AUC.json'.format(models[mod])) as json_file:
df = pd.read_json(json_file, convert_dates=False, convert_axes=False)
df.index = df.index.map(float)
df.sort_index(inplace=True)
df.replace(0., np.NaN, inplace=True)
df = (df - df.loc[1.0, :]) / df.loc[1.0, :]
zero_ind = np.argwhere(df.index == 1)[0][0]
ind = [df.index[zero_ind - 1], df.index[zero_ind], df.index[zero_ind + 1]]
df2 = df.loc[ind, :]
for c in df.keys():
tau, p = stats.kendalltau(df.index, df[c], nan_policy='omit')
tau_l, p_l = stats.kendalltau(df2.index, df2[c], nan_policy='omit')
ratio_tau = tau_l/tau
slope_df = slope_df.append(pd.Series([model_names[mod], tau, p, tau_l, p_l, ratio_tau, alt_dict[c],clr_dict[models[mod]]], index=slope_df.columns), ignore_index=True)
models =['RS_pyramidal', 'RS_inhib', 'FS', 'IB','Cb_stellate','Cb_stellate_Kv','Cb_stellate_Kv_only','STN','STN_Kv','STN_Kv_only']
model_names = ['RS pyramidal', 'RS inhibitory', 'FS', 'IB','Cb stellate','Cb stellate +$K_V1.1$','Cb stellate $\Delta$$K_V1.1$','STN','STN +$K_V1.1$','STN $\Delta$$K_V1.1$']
g_df = pd.DataFrame(columns=['model', 'corr', 'p_value', 'local corr', 'local p_value', 'ratio', 'g', 'color']) # for boxplots
for mod in range(len(models)):
with open('./SA_summary_df/{}_g_AUC.json'.format(models[mod])) as json_file:
df = pd.read_json(json_file, convert_dates=False, convert_axes=False)
df.index = df.index.map(float)
df.sort_index(inplace=True)
df.replace(0., np.NaN, inplace=True)
df = (df - df.loc[1.0, :]) / df.loc[1.0, :]
zero_ind = np.argwhere(df.index == 1)[0][0]
ind = [df.index[zero_ind - 1], df.index[zero_ind], df.index[zero_ind + 1]]
df2 = df.loc[ind, :]
for c in df.keys():
tau, p = stats.kendalltau(df.index, df[c], nan_policy='omit')
tau_l, p_l = stats.kendalltau(df2.index, df2[c], nan_policy='omit')
ratio_tau = tau_l / tau
g_df = g_df.append(pd.Series([model_names[mod], tau, p,tau_l, p_l, ratio_tau, alt_dict[c],clr_dict[models[mod]]], index=g_df.columns), ignore_index=True)
shift_df.to_json('./Sensitivity_analysis/shift_box_kendall_corr.json')
slope_df.to_json('./Sensitivity_analysis/slope_box_kendall_corr.json')
g_df.to_json('./Sensitivity_analysis/g_box_kendall_corr.json')
#%% rheobase correlation analysis
models =['RS_pyramidal', 'RS_inhib', 'FS', 'IB','Cb_stellate','Cb_stellate_Kv','Cb_stellate_Kv_only','STN','STN_Kv','STN_Kv_only']
model_names = ['RS pyramidal', 'RS inhibitory', 'FS', 'IB','Cb stellate','Cb stellate +$K_V1.1$','Cb stellate $\Delta$$K_V1.1$','STN','STN +$K_V1.1$','STN $\Delta$$K_V1.1$']
shift_df = pd.DataFrame(columns=['model', 'corr', 'p_value', 'local corr', 'local p_value', 'ratio', '$\Delta V_{1/2}$', 'color']) # for boxplots
for mod in range(len(models)):
with open('./SA_summary_df/{}_shift_rheo.json'.format(models[mod])) as json_file:
df = pd.read_json(json_file)
df.sort_index(inplace=True)
df.replace(0., np.NaN, inplace=True)
df = (df - df.loc[0, :]) / df.loc[0, :]
zero_ind = np.argwhere(df.index == 0)[0][0]
ind = [df.index[zero_ind - 1], df.index[zero_ind], df.index[zero_ind + 1]]
df2 = df.loc[ind, :]
for c in df.keys():
tau, p = stats.kendalltau(df.index, df[c], nan_policy='omit')
tau_l, p_l = stats.kendalltau(df2.index, df2[c], nan_policy='omit')
ratio_tau = tau_l / tau
shift_df = shift_df.append(pd.Series([model_names[mod], tau, p,tau_l, p_l, ratio_tau, alt_dict[c],clr_dict[models[mod]]], index=shift_df.columns), ignore_index=True)
models =['RS_pyramidal', 'RS_inhib', 'FS', 'IB','Cb_stellate','Cb_stellate_Kv','Cb_stellate_Kv_only','STN','STN_Kv','STN_Kv_only']
model_names = ['RS pyramidal', 'RS inhibitory', 'FS', 'IB','Cb stellate','Cb stellate +$K_V1.1$','Cb stellate $\Delta$$K_V1.1$','STN','STN +$K_V1.1$','STN $\Delta$$K_V1.1$']
slope_df = pd.DataFrame(columns=['model', 'corr', 'p_value','local corr', 'local p_value', 'ratio', 'Slope (k)', 'color']) # for boxplots
for mod in range(len(models)):
with open('./SA_summary_df/{}_slope_rheo.json'.format(models[mod])) as json_file:
df = pd.read_json(json_file, convert_dates=False, convert_axes=False)
df.index = df.index.map(float)
df.sort_index(inplace=True)
df.replace(0., np.NaN, inplace=True)
df = (df - df.loc[1.0, :]) / df.loc[1.0, :]
zero_ind = np.argwhere(df.index == 1)[0][0]
ind = [df.index[zero_ind - 1], df.index[zero_ind], df.index[zero_ind + 1]]
df2 = df.loc[ind, :]
for c in df.keys():
tau, p = stats.kendalltau(df.index, df[c], nan_policy='omit')
tau_l, p_l = stats.kendalltau(df2.index, df2[c], nan_policy='omit')
ratio_tau = tau_l/tau
slope_df = slope_df.append(pd.Series([model_names[mod], tau, p, tau_l, p_l, ratio_tau, alt_dict[c],clr_dict[models[mod]]], index=slope_df.columns), ignore_index=True)
models =['RS_pyramidal', 'RS_inhib', 'FS', 'IB','Cb_stellate','Cb_stellate_Kv','Cb_stellate_Kv_only','STN','STN_Kv','STN_Kv_only']
model_names = ['RS pyramidal', 'RS inhibitory', 'FS', 'IB','Cb stellate','Cb stellate +$K_V1.1$','Cb stellate $\Delta$$K_V1.1$','STN','STN +$K_V1.1$','STN $\Delta$$K_V1.1$']
g_df = pd.DataFrame(columns=['model', 'corr', 'p_value', 'local corr', 'local p_value', 'ratio', 'g', 'color']) # for boxplots
for mod in range(len(models)):
with open('./SA_summary_df/{}_g_rheo.json'.format(models[mod])) as json_file:
df = pd.read_json(json_file, convert_dates=False, convert_axes=False)
df.index = df.index.map(float)
df.sort_index(inplace=True)
df.replace(0., np.NaN, inplace=True)
df = (df - df.loc[1.0, :]) / df.loc[1.0, :]
zero_ind = np.argwhere(df.index == 1)[0][0]
ind = [df.index[zero_ind - 1], df.index[zero_ind], df.index[zero_ind + 1]]
df2 = df.loc[ind, :]
for c in df.keys():
tau, p = stats.kendalltau(df.index, df[c], nan_policy='omit')
tau_l, p_l = stats.kendalltau(df2.index, df2[c], nan_policy='omit')
ratio_tau = tau_l / tau
g_df = g_df.append(pd.Series([model_names[mod], tau, p,tau_l, p_l, ratio_tau, alt_dict[c],clr_dict[models[mod]]], index=g_df.columns), ignore_index=True)
shift_df.to_json('./Sensitivity_analysis/rheo_shift_box_kendall_corr.json')
slope_df.to_json('./Sensitivity_analysis/rheo_slope_box_kendall_corr.json')
g_df.to_json('./Sensitivity_analysis/rheo_g_box_kendall_corr.json')
#%% todo: CREATE CSV FILES ###################################################################################################
# from ./Code/From_Neuronal_models/tau_analysis/AUC_corr_df_gen.py
AUC_shift_df = pd.read_json('./Sensitivity_analysis/shift_box_kendall_corr_rel.json', orient='records')
AUC_slope_df = pd.read_json('./Sensitivity_analysis/slope_box_kendall_corr_rel.json', orient='records') #, lines=True)
AUC_g_df = pd.read_json('./Sensitivity_analysis/g_box_kendall_corr_rel.json', orient='records')
# for i in np.array(['FS', 'IB', 'RS inhibitory', 'RS pyramidal']):
# AUC_shift_df.loc[AUC_shift_df['model'] == i, 'model'] = i + ' +$K_V1.1$' # ''+$\mathrm{K}_{\mathrm{V}}\mathrm{1.1}$'
# AUC_slope_df.loc[AUC_slope_df['model'] == i, 'model'] = i + ' +$K_V1.1$' #' +$\mathrm{K}_{\mathrm{V}}\mathrm{1.1}$'
# AUC_g_df.loc[AUC_g_df['model'] == i, 'model'] = i + ' +$K_V1.1$' #' +$\mathrm{K}_{\mathrm{V}}\mathrm{1.1}$'
AUC_shift_df_posp = pd.read_json('./Sensitivity_analysis/shift_box_kendall_corr_rel_pospischil.json', orient='records')
AUC_slope_df_posp = pd.read_json('./Sensitivity_analysis/slope_box_kendall_corr_rel_pospischil.json', orient='records') #, lines=True)
AUC_g_df_posp = pd.read_json('./Sensitivity_analysis/g_box_kendall_corr_rel_pospischil.json', orient='records')
AUC_shift_df = AUC_shift_df.append(AUC_shift_df_posp)
AUC_slope_df = AUC_slope_df.append(AUC_slope_df_posp)
AUC_g_df = AUC_g_df.append(AUC_g_df_posp)
AUC_shift_df.to_csv('AUC_shift_corr.csv')
AUC_slope_df.to_csv('AUC_scale_corr.csv')
AUC_g_df.to_csv('AUC_g_corr.csv')
#% rheo
rheo_shift_df = pd.read_json('./Sensitivity_analysis/rheo_shift_box_kendall_corr.json', orient='records')
rheo_slope_df = pd.read_json('./Sensitivity_analysis/rheo_slope_box_kendall_corr.json', orient='records') #, lines=True)
rheo_g_df = pd.read_json('./Sensitivity_analysis/rheo_g_box_kendall_corr.json', orient='records')
for i in np.array(['FS', 'IB', 'RS inhibitory', 'RS pyramidal']):
rheo_shift_df.loc[rheo_shift_df['model'] == i, 'model'] = i + ' +$K_V1.1$' #' +$\mathrm{K}_{\mathrm{V}}\mathrm{1.1}$'
rheo_slope_df.loc[rheo_slope_df['model'] == i, 'model'] = i + ' +$K_V1.1$' #' +$\mathrm{K}_{\mathrm{V}}\mathrm{1.1}$'
rheo_g_df.loc[rheo_g_df['model'] == i, 'model'] = i + ' +$K_V1.1$' #' +$\mathrm{K}_{\mathrm{V}}\mathrm{1.1}$'
rheo_shift_df_posp = pd.read_json('./Sensitivity_analysis/rheo_shift_box_kendall_corr_pospischil.json', orient='records')
rheo_slope_df_posp = pd.read_json('./Sensitivity_analysis/rheo_slope_box_kendall_corr_pospischil.json', orient='records') #, lines=True)
rheo_g_df_posp = pd.read_json('./Sensitivity_analysis/rheo_g_box_kendall_corr_pospischil.json', orient='records')
rheo_shift_df = rheo_shift_df.append(rheo_shift_df_posp)
rheo_slope_df = rheo_slope_df.append(rheo_slope_df_posp)
rheo_g_df = rheo_g_df.append(rheo_g_df_posp)
rheo_shift_df.to_csv('rheo_shift_corr.csv')
rheo_slope_df.to_csv('rheo_scale_corr.csv')
rheo_g_df.to_csv('rheo_g_corr.csv')