susceptibility1/model_full.py
2024-03-08 11:02:12 +01:00

385 lines
18 KiB
Python

from utils_suseptibility import *
def model_full(c1=10, mult_type='_multsorted2_', devs=['05'], save=True, end='all', chose_score='mean_nrs',
detections=['MeanTrialsIndexPhaseSort'], sorted_on='LocalReconst0.2NormAm',ylim = [-1.25, 1.25], dfs = ['m1', 'm2']):
plot_style()
default_figsize(column=2, length=2.3)
grid = gridspec.GridSpec(1, 3, wspace=0.6, bottom = 0.1, width_ratios = [2,1,1], hspace=0.15, top=0.95, left=0.075, right=0.87)
axes = []
##################################
# model part
ls = '--'
lw = 0.5
ax = plt.subplot(grid[0])
axes.append(ax)
perc,im,stack_final = plt_model_big(ax, ls = ls, lw = 0.5)
fr_waves = 139
fr_noise = 120
f1 = 33
f2 = 139
#embed()
ax.plot(fr_noise * f1/fr_waves, fr_noise*f2/fr_waves, 'o', ms = 5, markeredgecolor = 'orange', markerfacecolor="None")
ax.plot(-fr_noise * f1 / fr_waves, fr_noise * f2 / fr_waves, 'o', ms = 5, markeredgecolor='pink', markerfacecolor="None")
# if len(cbar) > 0:
###############################
# data part
data_extra = False
if data_extra:
ax = plt.subplot(grid[0])
axes.append(ax)
cell = '2012-07-03-ak-invivo-1'
mat_rev,stack_final_rev = load_stack_data_susept(cell, save_name = version_final(), end = '_revQuadrant_')
mat, stack = load_stack_data_susept(cell, save_name=version_final(), end = '')
#embed()
#try:
full_matrix = create_full_matrix2(np.array(mat),np.array(mat_rev))
#except:
# print('full matrix something')
# embed()
stack_final = get_axis_on_full_matrix(full_matrix, mat)
abs_matrix = np.abs(stack_final)
#embed()
#if np.
abs_matrix, add_nonlin_title, resize_val = rescale_colorbar_and_values(abs_matrix)
ax.axhline(0, color = 'white', linestyle = ls, linewidth = lw)
ax.axvline(0, color='white', linestyle = ls, linewidth = lw)
im = plt_RAM_perc(ax, perc, abs_matrix)
cbar, left, bottom, width, height = colorbar_outside(ax, im, add=5, width=0.01)
set_clim_same_here([im], mats=[abs_matrix], lim_type='up', nr_clim='perc', clims='', percnr=95)
#clim = im.get_clim()
#if clim[1]> 1000:
#todo: change clim values with different Hz values
#embed()
cbar.set_label(nonlin_title(add_nonlin_title = ' ['+add_nonlin_title), rotation=90, labelpad=8)
set_ylabel_arrow(ax, xpos = -0.07, ypos = 0.97)
set_xlabel_arrow(ax, xpos=1, ypos=-0.07)
''' eod_fr, stack_spikes = plt_data_suscept_single(ax, cbar_label, cell, cells, f, fig, file_names_exclude, lp, title,
width)'''
cbar, left, bottom, width, height = colorbar_outside(ax, im, add=5, width=0.01)
#################
# power spectra data
# mean_type = '_MeanTrialsIndexPhaseSort_Min0.25sExcluded_'
extract = ''
datasets, data_dir = find_all_dir_cells()
cells = ['2022-01-28-ah-invivo-1'] # , '2022-01-28-af-invivo-1', '2022-01-28-ab-invivo-1',
# '2022-01-27-ab-invivo-1', ] # ,'2022-01-28-ah-invivo-1', '2022-01-28-af-invivo-1', ]
append_others = 'apend_others' # '#'apend_others'#'apend_others'#'apend_others'##'apend_others'
autodefine = '_DFdesired_'
autodefine = 'triangle_diagonal_fr' # ['triangle_fr', 'triangle_diagonal_fr', 'triangle_df2_fr','triangle_df2_eodf''triangle_df1_eodf', ] # ,'triangle_df2_fr''triangle_df1_fr','_triangle_diagonal__fr',]
DF2_desired = [-33]
DF1_desired = [133]
autodefine = '_dfchosen_closest_'
autodefine = '_dfchosen_closest_first_'
cells = ['2021-08-03-ac-invivo-1'] ##'2021-08-03-ad-invivo-1',,[10, ][5 ]
# c1s = [10] # 1, 10,
# c2s = [10]
minsetting = 'Min0.25sExcluded'
c2 = 10
# detections = ['MeanTrialsIndexPhaseSort'] # ['AllTrialsIndex'] # ,'MeanTrialsIndexPhaseSort''DetectionAnalysis''_MeanTrialsPhaseSort'
# detections = ['AllTrialsIndex'] # ['_MeanTrialsIndexPhaseSort_Min0.25sExcluded_extended_eod_loc_synch']
extend_trials = '' # 'extended'#''#'extended'#''#'extended'#''#'extended'#''#'extended'#''#'extended'# ok kein Plan was das hier ist
# phase_sorting = ''#'PhaseSort'
eodftype = '_psdEOD_'
concat = '' # 'TrialsConcat'
indices = ['_allindices_']
chirps = [
''] # '_ChirpsDelete3_',,'_ChirpsDelete3_'','','',''#'_ChirpsDelete3_'#''#'_ChirpsDelete3_'#'#'_ChirpsDelete2_'#''#'_ChirpsDelete_'#''#'_ChirpsDelete_'#''#'_ChirpsDelete_'#''#'_ChirpsCache_'
extract = '' # '_globalmax_'
devs_savename = ['original', '05'] # ['05']#####################
# control = pd.read_pickle(
# load_folder_name(
# 'calc_model') + '/modell_all_cell_no_sinz3_afe0.1__afr1__afj0.1__length1.5_adaptoffsetallall2___stepefish' + step + 'Hz_ratecorrrisidual35__modelbigfit_nfft4096.pkl')
if len(cells) < 1:
data_dir, cells = load_cells_three(end, data_dir=data_dir, datasets=datasets)
cells, p_units_cells, pyramidals = restrict_cell_type(cells, 'p-units')
# default_settings(fs=8)
start = 'min' #
cells = ['2021-08-03-ac-invivo-1']
tag_cells = []
for c, cell in enumerate(cells):
counter_pic = 0
contrasts = [c2]
tag_cell = []
for c, contrast in enumerate(contrasts):
contrast_small = 'c2'
contrast_big = 'c1'
contrasts1 = [c1]
for contrast1 in contrasts1:
for devname_orig in devs:
datapoints = [1000]
for d in datapoints:
################################
# prepare DF1 desired
# chose_score = 'auci02_012-auci_base_01'
# hier muss das halt stimmen mit der auswahl
# hier wollen wir eigntlich kein autodefine
# sondern wir wollen so ein diagonal ding haben
divergnce, fr, pivot_chosen, max_val, max_x, max_y, mult, DF1_desired, DF2_desired, min_y, min_x, min_val, diff_cut = chose_mat_max_value(
DF1_desired, DF2_desired, '', mult_type, eodftype, indices, cell, contrast_small,
contrast_big, contrast1, dfs, start, devname_orig, contrast, autodefine=autodefine,
cut_matrix='cut', chose_score=chose_score) # chose_score = 'auci02_012-auci_base_01'
DF1_desired = DF1_desired # [::-1]
DF2_desired = DF2_desired # [::-1]
# embed()
#######################################
# ROC part
# fr, celltype = get_fr_from_info(cell, data_dir[c])
version_comp, subfolder, mod_name_slash, mod_name, subfolder_path = find_code_vs_not()
b = load_b_public(c, cell, data_dir)
mt_sorted = predefine_grouping_frame(b, eodftype=eodftype, cell_name=cell)
counter_waves = 0
mt_sorted = mt_sorted[(mt_sorted['c2'] == c2) & (mt_sorted['c1'] == c1)]
for gg in range(len(DF1_desired)):
# embed()
# try:
grid0 = gridspec.GridSpecFromSubplotSpec(len(DF1_desired), 1, wspace=0.15, hspace=0.35,
subplot_spec=grid[1])
t3 = time.time()
# except:
# print('time thing')
# embed()
ax_w = []
###################
# all trials in one
grouped = mt_sorted.groupby(
['c1', 'c2', 'm1, m2'],
as_index=False)
# try:
grouped_mean = chose_certain_group(DF1_desired[gg],
DF2_desired[gg], grouped,
several=True, emb=False,
concat=True)
# except:
# print('grouped thing')
# embed()
###################
# groups sorted by repro tag
# todo: evnetuell die tuples gleich hier umspeichern vom csv ''
# embed()
grouped = mt_sorted.groupby(
['c1', 'c2', 'm1, m2', 'repro_tag_id'],
as_index=False)
grouped_orig = chose_certain_group(DF1_desired[gg],
DF2_desired[gg],
grouped,
several=True)
gr_trials = len(grouped_orig)
###################
groups_variants = [grouped_mean]
group_mean = [grouped_orig[0][0], grouped_mean]
for d, detection in enumerate(detections):
mean_type = '_' + detection # + '_' + minsetting + '_' + extend_trials + concat
##############################################################
# load plotting arrays
arrays, arrays_original, spikes_pure = save_arrays_susept(
data_dir, cell, c, b, chirps, devs, extract, group_mean, mean_type, plot_group=0,
rocextra=False, sorted_on=sorted_on)
####################################################
####################################################
# hier checken wir ob für diesen einen Punkt das funkioniert mit der standardabweichung
# embed()
try:
check_var_substract_method(spikes_pure)
except:
print('var checking not possible')
# fig = plt.figure()
# grid = gridspec.GridSpec(2, 1, wspace=0.7, left=0.05, top=0.95, bottom=0.15,
# right=0.98)
##########################################################################
# part with the power spectra
xlim = [0, 100]
# plt.savefig(r'C:\Users\alexi\OneDrive - bwedu\Präsentations\latex\experimental_protocol.pdf')
# embed()
fr_end = divergence_title_add_on(group_mean, fr[gg], autodefine)
###########################################
stimulus_length = 0.3
deltat = 1 / 40000
eodf = np.mean(group_mean[1].eodf)
eod_fr = eodf
# embed()
a_fr = 1
# embed()
eod_fe = eodf + np.mean(
group_mean[1].DF2) # data.eodf.iloc[0] + 10 # cell_model.eode.iloc[0]
a_fe = group_mean[0][1] / 100
eod_fj = eodf + np.mean(
group_mean[1].DF1) # data.eodf.iloc[0] + 50 # cell_model.eodj.iloc[0]
a_fj = group_mean[0][0] / 100
variant_cell = 'no' # 'receiver_emitter_jammer'
print('f0' + str(eod_fr))
print('f1'+str(eod_fe))
print('f2' + str(eod_fj))
eod_fish_j, time_array, time_fish_r, eod_fish_r, time_fish_e, eod_fish_e, time_fish_sam, eod_fish_sam, stimulus_am, stimulus_sam = extract_waves(
variant_cell, '',
stimulus_length, deltat, eod_fr, a_fr, a_fe, [eod_fe], 0, eod_fj, a_fj)
jammer_name = 'female'
cocktail_names = False
if cocktail_names:
titles = ['receiver ',
'+' + 'intruder ',
'+' + jammer_name,
'+' + jammer_name + '+intruder',
[]] ##'receiver + ' + 'receiver + receiver
else:
titles = title_motivation()
gs = [0, 1, 2, 3, 4]
waves_presents = [['receiver', '', '', 'all'],
['receiver', 'emitter', '', 'all'],
['receiver', '', 'jammer', 'all'],
['receiver', 'emitter', 'jammer', 'all'],
] # ['', '', '', ''],['receiver', '', '', 'all'],
# ['receiver', '', 'jammer', 'all'],
# ['receiver', 'emitter', '', 'all'],'receiver', 'emitter', 'jammer',
symbols = [''] # '$+$', '$-$', '$-$', '$=$',
symbols = ['', '', '', '', '']
time_array = time_array * 1000
color0 = 'green'
color0_burst = 'darkgreen'
color01 = 'green'
color02 = 'red'
color012 = 'orange'
color01_2 = 'purple'
colors_am = ['black', 'black', 'black', 'black'] # color01, color02, color012]
extracted = [False, True, True, True]
extracted2 = [False, False, False, False]
printing = True
if printing:
print(time.time() - t3)
# embed()
##########################################
# spike response
array_chosen = 1
if d == 0: #
# embed()
# plot the psds
ax00 = plt.subplot(grid0[gg])
p_means_all = {}
names = ['0', '02', '01', '012']
for j in range(len(arrays)):
########################################
# get the corresponding psds
# hier kann man aussuchen welches power spektrum machen haben will
f, nfft = get_psds_ROC(array_chosen, arrays, arrays_original, j, mean_type,
names, p_means_all)
#ax_as.append(ax_a)
ps = {}
p_means = {}
ax_ps = []
color012_minus = 'purple',
colors_p = [color0, color02, color01, color012, color02, color01, color012_minus,
color0_burst, color0_burst,
color0, color0]
ax00, fr_isi = plt_psds_ROC(arrays, ax00, ax_ps, cell, colors_p, f, grid0, group_mean, nfft, p_means, p_means_all,ps, 4, spikes_pure,time_array)
# arrays, ax00, ax_ps, cell, colors_p, f, grid0, group_mean, nfft, p_means, p_means_all, ps, row,spikes_pure, time_array,
axes = []
axes.append(ax_w)
#################
# power spectra model
#print('finished model_full')
fig = plt.gcf()
#axes = plt.gca()
#fig.tag(axes[::-1], xoffs=-4.5, yoffs=0.4) # ax_ams[3],
save_visualization()
def load_stack_data_susept(cell, save_name, end = ''):
load_name = load_folder_name('calc_RAM') + '/' + save_name+end
add = '_cell' + cell +end# str(f) # + '_amp_' + str(amp)
#embed()
stack_cell = load_data_susept(load_name + '_' + cell + '.pkl', load_name + '_' + cell, add=add,
load_version='csv')
file_names_exclude = get_file_names_exclude()
stack_cell = stack_cell[~stack_cell['file_name'].isin(file_names_exclude)]
# if len(stack_cell):
file_names = stack_cell.file_name.unique()
#embed()
file_names = exclude_file_name_short(file_names)
cut_off_nr = get_cutoffs_nr(file_names)
try:
maxs = list(map(float, cut_off_nr))
except:
embed()
file_names = file_names[np.argmax(maxs)]
#embed()
stack_file = stack_cell[stack_cell['file_name'] == file_names]
amps = [np.min(stack_file.amp.unique())]
amps = restrict_punits(cell, amps)
amp = np.min(amps)#[0]
# for amp in amps:
stack_amps = stack_file[stack_file['amp'] == amp]
lengths = stack_amps.stimulus_length.unique()
trial_nr_double = stack_amps.trial_nr.unique()
trial_nr = np.max(trial_nr_double)
stack_final = stack_amps[
(stack_amps['stimulus_length'] == np.max(lengths)) & (stack_amps.trial_nr == trial_nr)]
mat, new_keys = get_mat_susept(stack_final)
return mat,stack_final
if __name__ == '__main__':
model_full()