From 9e124f2814aa5ea172f65774d21a8143657598e8 Mon Sep 17 00:00:00 2001 From: Mirko Zanon Date: Fri, 6 Sep 2024 21:40:08 -0700 Subject: [PATCH] Update compute_tunings.py log plot also with numerosity 0 --- src/numan_plus/compute_tunings.py | 35 ++++++++++++++++++------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/numan_plus/compute_tunings.py b/src/numan_plus/compute_tunings.py index 0bea419..88a652f 100755 --- a/src/numan_plus/compute_tunings.py +++ b/src/numan_plus/compute_tunings.py @@ -75,33 +75,40 @@ def normalize_tuning(tuning_mat, tuning_err): return tuning_mat_exc, tuning_err_exc, tuning_mat_inh, tuning_err_inh def plot_tunings(tuning_mat, tuning_err, n_numerosities, colors_list, save_path=None, save_name=None): - # Plot population tuning curves on linear scale Qrange = np.arange(n_numerosities) + + # To handle log(0) issues, replace 0 with a small value + log_safe_Qrange = np.where(Qrange == 0, 1e-5, Qrange) + plt.figure(figsize=(9,4)) - plt.title(save_name) + plt.suptitle(save_name) + + # Linear scale plot plt.subplot(1,2,1) for i, (tc, err) in enumerate(zip(tuning_mat, tuning_err)): plt.errorbar(Qrange, tc, err, color=colors_list[i]) - plt.xticks(ticks=Qrange, labels=np.arange(n_numerosities)) + plt.xticks(ticks=Qrange, labels=np.arange(n_numerosities)) plt.xlabel('Numerosity') plt.ylabel('Normalized Neural Activity') - # Plot population tuning curves on log scale + + # Log scale plot plt.subplot(1,2,2) for i, (tc, err) in enumerate(zip(tuning_mat, tuning_err)): - plt.errorbar(np.arange(n_numerosities), tc, err, color=colors_list[i]) # offset x axis by one to avoid taking the log of zero + plt.errorbar(log_safe_Qrange, tc, err, color=colors_list[i]) # Use log_safe_Qrange to avoid log(0) plt.xscale('log', base=2) - #plt.gca().xaxis.set_major_formatter(ScalarFormatter()) - plt.xticks(ticks=Qrange, labels=Qrange) + plt.xticks(ticks=log_safe_Qrange, labels=np.arange(n_numerosities)) plt.xlabel('Numerosity') plt.ylabel('Normalized Neural Activity') - # save figure - if not (save_name is None): - if not (save_path is None): - plt.savefig(save_path + '/'+ save_name + '.svg') - plt.savefig(save_path + '/'+ save_name + '.png', dpi=900) + + # Save figure + if save_name: + if save_path: + plt.savefig(f'{save_path}/{save_name}.svg') + plt.savefig(f'{save_path}/{save_name}.png', dpi=900) else: - plt.savefig(save_name + '.svg') - plt.savefig(save_name + '.png', dpi=900) + plt.savefig(f'{save_name}.svg') + plt.savefig(f'{save_name}.png', dpi=900) + plt.show() def plot_selective_cells_histo(pref_num, n_numerosities, colors_list, excitatory_or_inhibitory=None, chance_lev=None, save_path=None, save_name=None):