susceptibility1/trialnr.py
2024-05-28 17:59:52 +02:00

293 lines
12 KiB
Python

import os
import numpy as np
import pandas as pd
from IPython import embed
from matplotlib import pyplot as plt
from plotstyle import plot_style
from threefish.core_plot_labels import title_find_cell_add
from threefish.defaults import default_figsize, default_settings
from threefish.core_filenames import overlap_cells
from threefish.core_load import resave_small_files, save_visualization
from threefish.core_reformat_RAM import get_stack, load_model_susept
from threefish.core import find_folder_name
from threefish.utils1_RAM import trial_nrs_ram_model
import itertools as it
##from update_project import *
def table_printen(table):
print(table.keys())
for l in range(len(table)):
list_here = np.array(table.iloc[l])
l1 = "& ".join(list_here)
print(l1)
def trialnr(nffts=['whole'], powers=[1], contrasts=[0], noises_added=[''], D_extraction_method=['additiv_cv_adapt_factor_scaled'],
internal_noise=['RAM'], external_noise=['RAM'], level_extraction=[''], receiver_contrast=[1],
dendrids=[''], ref_types=[''], adapt_types=[''], c_noises=[0.1], c_signal=[0.9], cut_offs1=[300]): # ['eRAM']
# plot_style()#['_RAMscaled']'_RAMscaled'
duration_noise = '_short',
formula = 'code' ##'formula'
# ,int(2 ** 16) int(2 ** 16), int(2 ** 15),
stimulus_length = 1 # 20#550 # 30 # 15#45#0.5#1.5 15 45 100
trials_nrs = [1] # [100, 500, 1000, 3000, 10000, 100000, 1000000] # 500
stimulus_type = '_StimulusOrig_' # ,#
# ,3]#, 3, 1, 1.5, 0.5, ] # ,1,1.5, 0.5] #[1,1.5, 0.5] # 1.5,0.5]3, 1,
variant = 'sinz'
mimick = 'no'
cell_recording_save_name = ''
trans = 1 # 5
rep = 1000000 # 500000#0
repeats = [20, rep] # 250000
aa = 0
good_data, remaining = overlap_cells()
cells_all = [good_data[0]]
plot_style()
default_figsize(column=2, length=3.1) #.254.75 0.75
#grid = gridspec.GridSpec(1, 1, wspace=0.95, bottom=0.09,
# hspace=0.25, width_ratios = [1,0,1,1,1], left=0.09, right=0.93, top=0.9)
a = 0
maxs = []
mins = []
mats = []
ims = []
perc05 = []
perc95 = []
iternames = [D_extraction_method, external_noise,
internal_noise, powers, nffts, dendrids, cut_offs1, trials_nrs, c_signal,
c_noises,
ref_types, adapt_types, noises_added, level_extraction, receiver_contrast, contrasts, ]
nr = '2'
# cell_contrasts = ["2013-01-08-aa-invivo-1"]
# cells_triangl_contrast = np.concatenate([cells_all,cell_contrasts])
# cells_triangl_contrast = 1
# cell_contrasts = 1
rows = len(cells_all) # len(good_data)+len(cell_contrasts)
perc = 'perc'
lp = 2
label_model = r'Nonlinearity $\frac{1}{S}$'
for all in it.product(*iternames):
var_type, stim_type_afe, stim_type_noise, power, nfft, dendrid, cut_off1, trial_nrs, c_sig, c_noise, ref_type, adapt_type, noise_added, extract, a_fr, a_fe = all
# print(trials_stim,stim_type_noise, power, nfft, a_fe,a_fr, dendrid, var_type, cut_off1,trial_nrs)
fig = plt.figure()
hs = 0.45
##################################
# model part
trial_nr = 500000
cell = '2013-01-08-aa-invivo-1'
cell = '2012-07-03-ak-invivo-1'
print('cell'+str(cell))
cells_given = [cell]
save_name_rev = find_folder_name(
'calc_model') + '/' + 'calc_RAM_model-2__nfft_whole_power_1_RAM_additiv_cv_adapt_factor_scaled_cNoise_0.1_cSig_0.9_cutoff1_300_cutoff2_300no_sinz_length1_TrialsStim_' + str(
trial_nr) + '_a_fr_1__trans1s__TrialsNr_1_fft_o_forward_fft_i_forward_Hz_mV_revQuadrant_'
# for trial in trials:#.009
trial_nr = 1000000#1000000
trial_nrs_here = trial_nrs_ram_model()
stacks = []
perc95 = []
vars = []
cv_stims = []
fr_stims = []
perc05 = []
median = []
stacks_wo_norm = []
perc95_wo_norm = []
perc05_wo_norm = []
median_wo_norm = []
for tr in trial_nrs_here:
save_names = [
'calc_RAM_model-2__nfft_whole_power_1_RAM_additiv_cv_adapt_factor_scaled_cNoise_0.1_cSig_0.9_cutoff1_300_cutoff2_300no_sinz_length1_TrialsStim_'+str(tr)+'_a_fr_1__trans1s__TrialsNr_1_fft_o_forward_fft_i_forward_Hz_mV',]
#'calc_RAM_model-2__nfft_whole_power_1_RAM_additiv_cv_adapt_factor_scaled_cNoise_0.1_cSig_0.9_cutoff1_300_cutoff2_300no_sinz_length1_TrialsStim_5000000_a_fr_1__trans1s__TrialsNr_1_fft_o_forward_fft_i_forward_Hz_mV'
#'calc_RAM_model-2__nfft_whole_power_1_afe_0.009_RAM_RAM_additiv_cv_adapt_factor_scaled_cNoise_0.1_cSig_0.9_cutoff1_300_cutoff2_300no_sinz_length1_TrialsStim_11_a_fr_1__trans1s__TrialsNr_1_fft_o_forward_fft_i_forward_Hz_mV',
#'calc_RAM_model-2__nfft_whole_power_1_afe_0.009_RAM_RAM_additiv_cv_adapt_factor_scaled_cNoise_0.1_cSig_0.9_cutoff1_300_cutoff2_300no_sinz_length1_TrialsStim_500000_a_fr_1__trans1s__TrialsNr_1_fft_o_forward_fft_i_forward_Hz_mV',
nrs_s = [3, 4, 8, 9]#, 10, 11
#embed()
tr_name = trial_nr/1000000
if tr_name == 1:
tr_name = 1
ax_model = []
for s, sav_name in enumerate(save_names):
save_name = find_folder_name('calc_model') + '/' + sav_name
cell_add, cells_save = title_find_cell_add(cells_given)
perc = 'perc'
path = save_name + '.pkl' # '../'+
# stack = get_stack_one_quadrant(cell, cell_add, cells_save, path, save_name)
# full_matrix = create_full_matrix2(np.array(stack), np.array(stack_rev))
# stack_final = get_axis_on_full_matrix(full_matrix, stack)
# im = plt_RAM_perc(ax, perc, np.abs(stack))
#
stack = load_model_susept(path, cells_save, save_name.split(r'/')[-1] + cell_add)
if len(stack)> 0:
model_show, stack_plot, stack_plot_wo_norm = get_stack(cell, stack)
stacks.append(stack_plot)
#em
cv_stims.append(stack['cv_stim_mean'].iloc[0])
fr_stims.append(stack['fr_stim_mean'].iloc[0])
vars.append(stack['var_RAM'].iloc[0])
perc95.append(np.percentile(stack_plot,99.99))
perc05.append(np.percentile(stack_plot, 10))
median.append(np.percentile(stack_plot, 50))
stacks_wo_norm.append(stack_plot_wo_norm)
perc95_wo_norm.append(np.percentile(stack_plot_wo_norm,99.99))
perc05_wo_norm.append(np.percentile(stack_plot_wo_norm, 10))
median_wo_norm.append(np.percentile(stack_plot_wo_norm, 50))
else:
vars.append(float('nan'))
cv_stims.append(float('nan'))
fr_stims.append(float('nan'))
stacks.append([])
perc95.append(float('nan'))
perc05.append(float('nan'))
median.append(float('nan'))
stacks_wo_norm.append([])
perc95_wo_norm.append(float('nan'))
perc05_wo_norm.append(float('nan'))
median_wo_norm.append(float('nan'))
#embed()
test = False
if test:
fig, ax = plt.subplots(1,3)
ax[0].plot(trial_nrs_here,cv_stims/trial_nrs_here)
ax[1].plot(trial_nrs_here, fr_stims / trial_nrs_here)
ax[2].plot(trial_nrs_here, vars / trial_nrs_here)
plt.show()
# vars, cv_stims, fr_stims
fig, ax = plt.subplots(1,2)
#ax.plot(trial_nrs_here, perc05, color = 'grey')
ax[0].plot(trial_nrs_here, perc95, color = 'grey', clip_on = False, label = '99.99th percentile')
#ax.plot(trial_nrs_here, median, color = 'black', label = 'median')
#ax.scatter(trial_nrs_here, perc05, color = 'grey')
ax[0].scatter(trial_nrs_here, perc95, color = 'black', clip_on = False)
ax[0].set_xscale('log')
ax[0].set_yscale('log')
ax[0].set_xlabel('Trials [$N$]')
ax[0].set_ylabel('$\chi_{2}$\,[Hz]')
ax[0].legend()
############################################
ax[1].plot(trial_nrs_here, perc05, color='grey', clip_on=False, label = '10th percentile')
ax[1].scatter(trial_nrs_here, perc05, color='black', clip_on=False)
ax[1].set_xscale('log')
ax[1].set_yscale('log')
ax[1].set_xlabel('Trials [$N$]')
ax[1].set_ylabel('$\chi_{2}$\,[Hz]')
ax[1].legend()
''' ax = plt.subplot(1,3,2)
ax.plot(trial_nrs_here, perc05_wo_norm, color = 'grey')
ax.plot(trial_nrs_here, perc95_wo_norm, color = 'grey')
ax.plot(trial_nrs_here, median_wo_norm, color = 'black', label = 'median')
ax.fill_between(trial_nrs_here, perc05_wo_norm, perc95_wo_norm, color='grey')
#ax.scatter(trial_nrs_here, median, color='black', label='measured points')
ax = plt.subplot(1,3,3)
ax.plot(trial_nrs_here, perc05_wo_norm, color = 'grey')
ax.plot(trial_nrs_here, perc95_wo_norm, color = 'grey')
ax.plot(trial_nrs_here, median_wo_norm, color = 'black', label = 'median')
ax.fill_between(trial_nrs_here, perc05_wo_norm, perc95_wo_norm, color='grey')
#ax.scatter(trial_nrs_here, median, color='black', label='measured points')
ax.legend()
ax.set_xscale('log')
ax.set_yscale('log')
'''
plt.subplots_adjust(left = 0.1, right = 0.9, bottom = 0.2, top = 0.95)
#plt.set_scale
#embed()
#embed()
#fig.tag([axes[0:3]], xoffs=-3, yoffs=1.6) # ax_ams[3],
#fig.tag([[axes[4]]], xoffs=-3, yoffs=1.6, minor_index=0) # ax_ams[3],
#fig.tag([axes[3:6]], xoffs=-3, yoffs=1.6) #, major_index = 1, minor_index = 2 ax_ams[3],
#fig.tag([[axes[7]]], xoffs=-3, yoffs=1.6, major_index=2,minor_index=0) # ax_ams[3],
#fig.tag([axes[8::]], xoffs=-3, yoffs=1.6, major_index=2, minor_index=2) # ax_ams[3],
#fig.tag([axes[7::]], xoffs=-3, yoffs=1.6) # ax_ams[3],
#fig.tag([ax_ams[0],ax_data[0],axes[2], axes[3]], xoffs=-3, yoffs=1.6)#ax_ams[3],
save_visualization(pdf=True)
def start_pos_modeldata():
return 1.03
def signal_component_name():
return r'$\xi_{signal}$'#signal noise'
def noise_component_name():#$\xi_{noise}$noise_name =
return r'$\xi_{noise}$'#'Noise component'#'intrinsic noise'
def ypos_x_modelanddata():
return -0.45
if __name__ == '__main__':
model = resave_small_files("models_big_fit_d_right.csv", load_folder='calc_model_core')
cells = model.cell.unique()
params = {'cells': cells}
show = True
# if show == False:
# low CV: cells = ['2012-07-03-ak-invivo-1']
plot_style()
default_settings(lw=0.5, column=2, length=3.35) #8.5
redo = False
D_extraction_method = ['additiv_cv_adapt_factor_scaled']
# D_extraction_method = ['additiv_visual_d_4_scaled']
##########################
# hier printen wir die table Werte zum kopieren in den Text
path = 'print_table_suscept-model_params_suscept_table.csv'
if os.path.exists(path):
table = pd.read_csv(path)
table_printen(table)
path = 'print_table_all-model_params_suscept_table.csv'
if os.path.exists(path):
table = pd.read_csv()
print('model big')
table_printen(table)
#embed()
##########################
#embed()
trialnr(D_extraction_method=D_extraction_method) #r'$\frac{1}{mV^2S}$'