##from update_project import ** import numpy as np from threefish.core_cell_choice import find_all_dir_cells from threefish.core_reformat import chose_mat_max_value, load_b_public from threefish.core_plot_suscept import plt_coherences from threefish.defaults import default_figsize from threefish.core_time import cr_spikes_mat from threefish.core import find_code_vs_not import time from threefish.utils1_suscept import check_var_substract_method, chose_certain_group, circle_plot, extract_waves, load_cells_three, predefine_grouping_frame, restrict_cell_type, save_arrays_susept, ws_nonlin_systems from threefish.core_plot_labels import title_motivation from threefish.core_plot_subplots import colors_suscept_paper_dots, plot_arrays_ROC_psd_single3, plot_shemes4 from threefish.core_load import save_visualization #from matplotlib import gridspec as gridspec, gridspec, mlab as ml, pyplot as plt, ticker as ticker from matplotlib import pyplot as plt from matplotlib import gridspec from scipy.ndimage import gaussian_filter from plotstyle import plot_style def motivation_all_small(dev_desired = '1', ylim=[-1.25, 1.25], c1=10, dfs=['m1', 'm2'], mult_type='_multsorted2_', devs=['2'], figsize=None, save=True, end='0', chose_score='mean_nrs', detections=['AllTrialsIndex'], sorted_on='LocalReconst0.2Norm'): autodefines = [ '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',] cells = ['2021-08-03-ac-invivo-1'] ##'2021-08-03-ad-invivo-1',,[10, ][5 ] c1s = [10] # 1, 10, c2s = [10] plot_style() default_figsize(column=2, length=4.3) #6.7 ts=12, ls=12, fs=12 show = True DF2_desired = [0.8] DF1_desired = [0.87] DF2_desired = [-0.23] DF1_desired = [0.94] # 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 = [] ax_s = [] 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 extra_f_calculatoin = False if extra_f_calculatoin: 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 = [1.2]#DF1_desired # [::-1] DF2_desired = [0.95]#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)): # try: 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 '' 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, chirps, devs, extract, group_mean, mean_type, plot_group=0, rocextra=False, sorted_on=sorted_on, dev_desired = dev_desired) #################################################### #################################################### # hier checken wir ob für diesen einen Punkt das funkioniert mit der standardabweichung 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) if figsize: fig = plt.figure(figsize=figsize) else: fig = plt.figure() grid = gridspec.GridSpec(2, 1, wspace=0.7, hspace=0.15, left=0.055, top=0.96, bottom=0.15, right=0.935, height_ratios=[0.5, 5.3]) # height_ratios=[1, 2], height_ratios = [1,6]bottom=0.25, top=0.8, hr = [1, 0.35, 1.2, 0, 3, ] # 1 ########################################################################## # several coherence plot # frame_psd = pd.read_pickle(load_folder_name('calc_RAM')+'/noise_data11_nfft1sec_original__StimPreSaved4__first__CutatBeginning_0.05_s_NeurDelay_0.005_s_2021-08-03-ab-invivo-1.pkl') # frame_psd = pd.read_pickle(load_folder_name('calc_RAM') + '/noise_data11_nfft1sec_original__StimPreSaved4__first__CutatBeginning_0.05_s_NeurDelay_0.005_s_2021-08-03-ab-invivo-1.pkl') coh = False if coh: ax_w, d, data_dir, devs = plt_coherences(ax_w, d, devs, grid) # ax_cohs = plt.subplot(grid[0,1]) ########################################################################## # shemes ########################################################################## # part with the power spectra grid00 = gridspec.GridSpecFromSubplotSpec(1, 4, wspace=0.15, hspace=0.05, subplot_spec=grid[0, :]) texts1 = ['', '$s_{1}(t)$', '$s_{2}(t)$', '$s_{1} +s_{2}(t)$'] texts2 = ['$r_{0}$', '$r_{0} +r_{1}(t)$', '$r_{0} +r_{2}(t)$', r'$r_{t} \neq r_{0}+r_{1}(t)+r_{2}(t)$'] for g in range(4): horizontal = True if horizontal: grid000 = gridspec.GridSpecFromSubplotSpec(1, 4, wspace=0, hspace=0, subplot_spec=grid00[g], width_ratios = [2,0.7,2,1.6]) else: grid000 = gridspec.GridSpecFromSubplotSpec(3, 1, wspace=0, hspace=0, subplot_spec=grid00[g]) ax0 = plt.subplot(grid000[0]) color = 'black' # color_beats() #ax0.plot(time_array, sine, color=color, clip_on=False) ax0.show_spines('') #ax0.set_title('$s(t)$') # xy=(0.2, 0.2), ax0.show_spines('') # xytext=(0.8, 0.8), lw = 0.5 ws = ws_nonlin_systems() fs = 8 middle = 0.5 if horizontal: start = 0.7 if texts1[g] != '': ax0.annotate('', ha='center', xycoords='axes fraction', xy=(1, middle), textcoords='axes fraction', xytext=(start, middle), arrowprops={"arrowstyle": "->", "linestyle": "-", "linewidth": lw, "color": 'black'}, zorder=1, annotation_clip=False, transform=ax0.transAxes, ) ax0.text(start, 0.5, texts1[g], transform=ax0.transAxes, ha='right', va='center', fontsize = fs) else: start = 1.5 if g != texts1[g]: ax0.annotate('', ha='center', xycoords='axes fraction', xy=(middle, start), textcoords='axes fraction', xytext=(middle, 0), arrowprops={"arrowstyle": "<-", "linestyle": "-", "linewidth": lw, "color": 'black'}, zorder=1, annotation_clip=False, transform=ax0.transAxes, ) ax0.text(0.5, start, texts1[g], transform=ax0.transAxes, ha = 'center', va = 'center') ax_s.append(ax0) # embed() # fig.texts.append(ax[0].texts.pop()) ################################### ax1 = plt.subplot(grid000[1]) circle_plot(ax1, ax0, ws, lw) ax1.show_spines('') #ax1.set_xlim(-20, 40) #ax1.set_ylim(0, 20) ax1.set_xlim(0, 20) ax1.set_ylim(-20, 40) ####################################texts1[g]texts2[g] ax2 = plt.subplot(grid000[2]) if horizontal: end = 0.3 ax2.annotate('', ha='center', xycoords='axes fraction', xy=(end, middle), textcoords='axes fraction', xytext=(0, middle), arrowprops={"arrowstyle": "->", "linestyle": "-", "linewidth": lw, "color": 'black'}, zorder=1, annotation_clip=False, transform=ax2.transAxes, ) ax2.text(end, 0.5, texts2[g], transform=ax2.transAxes, ha='left', va='center', fontsize = fs) else: end = -0.5 ax2.annotate('', ha='center', xycoords='axes fraction', xy=(middle, end), textcoords='axes fraction', xytext=(middle, 1), arrowprops={"arrowstyle": "->", "linestyle": "-", "linewidth": lw, "color": 'black'}, zorder=1, annotation_clip=False, transform=ax2.transAxes, ) ax2.text(middle, end, texts2[g], transform=ax2.transAxes, ha='center', va='center') ax2.show_spines('') ########################################################################## # part with the power spectra grid0 = gridspec.GridSpecFromSubplotSpec(5, 4, wspace=0.15, hspace=0.35, subplot_spec=grid[1, :], height_ratios=hr) xlim = [0, 100] ########################################### stimulus_length = 0.3 deltat = 1 / 40000 eodf = np.mean(group_mean[1].eodf) eod_fr = eodf a_fr = 1 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 color01, color012, color01_2, color02, color0_burst, color0 = colors_suscept_paper_dots() colors_am = ['black', 'black', 'black', 'black'] # color01, color02, color012] extracted = [False, True, True, True] extracted2 = [False, False, False, False] for i in range(len(waves_presents)): ax = plot_shemes4(eod_fish_r, eod_fish_e, eod_fish_j, grid0, time_array, g=gs[i], title_top=True, eod_fr=eod_fr, waves_present=waves_presents[i], ylim=ylim, xlim=xlim, color_am=colors_am[i], color_am2 = color01_2, extracted=extracted[i], extracted2=extracted2[i], title=titles[i], add = 0.1) # 'intruder','receiver'#jammer_name ax_w.append(ax) if ax: ax.text(1.1, 0.45, symbols[i], fontsize=35, transform=ax.transAxes) bar = False if bar: if i == 0: ax.plot([0, 20], [ylim[0] + 0.01, ylim[0] + 0.01], color='black') ax.text(0, -0.16, '20 ms', va='center', fontsize=10, transform=ax.transAxes) printing = True if printing: print(time.time() - t3) ########################################## # spike response array_chosen = 1 if d == 0: # #embed() frs = [] for i in range(len(spikes_pure['base_0'])): #duration = spikes_pure['base_0'][i][-1] / 1000 duration = 0.5 fr = len(spikes_pure['base_0'][i])/duration frs.append(fr) fr = np.mean(frs) #embed() base_several = False if base_several: spikes_new = [] for i in range(len(spikes_pure['base_0'])): duration = 100 duration_full = 101#501 dur = np.arange(0, duration_full, duration) for d_nr in range(len(dur) - 1): #embed() spikes_new.append(np.array(spikes_pure['base_0'][i][ (spikes_pure['base_0'][i] > dur[d_nr]) & ( spikes_pure['base_0'][i] < dur[ d_nr + 1])])/1000-dur[d_nr]/1000) # spikes_pure['base_0'] = spikes_new sampling_rate = 1/np.diff(time_array) sampling_rate = int(sampling_rate[0]*1000) spikes_mats = [] smoothed05 = [] for i in range(len(spikes_new)): spikes_mat = cr_spikes_mat(spikes_new[i], sampling_rate, int(sampling_rate*duration/1000)) spikes_mats.append(spikes_mat) smoothed05.append(gaussian_filter(spikes_mat, sigma=(float(dev_desired)/1000) * sampling_rate)) smoothed_base = np.mean(smoothed05, axis=0) mat_base = np.mean(spikes_mats, axis=0) else: smoothed_base = arrays[0][0] mat_base = arrays_original[0][0] #embed()#arrays[0]v fr_isi, ax_ps, ax_as = plot_arrays_ROC_psd_single3( [[smoothed_base], arrays[2], arrays[1], arrays[3]], [[mat_base], arrays_original[2], arrays_original[1], arrays_original[3]], spikes_pure, cell, grid0, mean_type, group_mean, xlim=xlim, row=1 + d * 3, array_chosen=array_chosen, color0_burst=color0_burst, color01=color01, color02=color02,ylim_log=(-15, 3), color012=color012,color012_minus = color01_2,color0=color0) ########################################################################## individual_tag = 'DF1' + str(DF1_desired[gg]) + 'DF2' + str( DF2_desired[gg]) + cell + '_c1_' + str(c1) + '_c2_' + str(c2) + mean_type # save_all(individual_tag, show, counter_contrast=0, savename='') # print('individual_tag') axes = [] axes.append(ax_w) # axes.extend(np.transpose(ax_as)) # axes.append(np.transpose(ax_ps)) # np.transpose(axes) #fig.tag(ax_w[0:3], xoffs=-2.3, yoffs=1.7) #fig.tag(ax_w[3::], xoffs=-1.9, yoffs=1.4) fig.tag(ax_s, xoffs=-1.9, yoffs=1.2) #fig.tag(ax_w, xoffs=-1.9, yoffs=1.4) # ax_w, np.transpose(ax_as), ax_ps if save: save_visualization(individual_tag=individual_tag, show=show, pdf=True) # fig = plt.gcf() # fig.savefig # plt.show() if __name__ == '__main__':#2.5 motivation_all_small(dev_desired = '1', c1=10, mult_type='_multsorted2_', devs=['05'], save=True, end='all', chose_score='mean_nrs', detections=['MeanTrialsIndexPhaseSort'], sorted_on='LocalReconst0.2NormAm')#