Browse Source

extracted selection of polar plot candidates and added additional information in the filenames to ease selection

Paul Pfeiffer 3 years ago
parent
commit
de5bf988fc

+ 1 - 0
scripts/model_figure/plot_circular_colorbar.py

@@ -34,6 +34,7 @@ def plot_colorbar(figsize=(2, 2), figname=None):
     # plt.show()
     if figname is not None:
         plt.savefig(figname, transparent=True)
+        plt.close(fig)
 
 
 if __name__ == "__main__":

+ 102 - 44
scripts/spatial_network/perlin/paper_figures_spatial_head_direction_network_perlin_map.py

@@ -203,10 +203,11 @@ def plot_firing_rate_map_excitatory(traj, direction_idx, plot_run_names, exempla
         remove_ticks(ax)
     # fig.suptitle('spatial firing rate map', fontsize=16)
     ax.cax.colorbar(c)
-    ax.cax.annotate( "fr (Hz)", xy=(1, 1), xytext=(3,3), xycoords="axes fraction", textcoords="offset points")
+    ax.cax.annotate("fr (Hz)", xy=(1, 1), xytext=(3, 3), xycoords="axes fraction", textcoords="offset points")
     # fig.tight_layout()
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'C_firing_rate_map_excitatory.png', dpi=300)
+        plt.close(fig)
 
 
 def plot_firing_rate_map_inhibitory(traj, direction_idx, plot_run_names, selected_inhibitory_neuron):
@@ -265,6 +266,7 @@ def plot_firing_rate_map_inhibitory(traj, direction_idx, plot_run_names, selecte
     # fig.tight_layout()
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'C_firing_rate_map_inhibitory.png', dpi=300)
+        plt.close(fig)
     return max_val
 
 
@@ -291,7 +293,8 @@ def plot_hdi_over_tuning(traj, plot_run_names):
     ax.set_title('hdi over input tuning', fontsize=16)
 
     if save_figs:
-        plt.savefig(FIGURE_SAVE_PATH + 'hdi_over_tuning.png', dpi=200)
+        plt.savefig(FIGURE_SAVE_PATH + 'hdi_over_tuning.png')
+        plt.close(fig)
 
 
 def normal_labels(label):
@@ -347,6 +350,7 @@ def plot_input_map(traj, run_name, figsize=(panel_size, panel_size), figname='in
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + figname)
+        plt.close(fig)
 
 
 def plot_axonal_clouds(traj, plot_run_names):
@@ -426,6 +430,7 @@ def plot_axonal_clouds(traj, plot_run_names):
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'B_i_axonal_clouds.png')
+        plt.close(fig)
 
 
 def get_neurons_close_to_given_position(cluster_position, number_of_neurons_in_cluster, positions):
@@ -462,7 +467,8 @@ def plot_orientation_maps_diff_scales(traj):
     traj.f_restore_default()
 
     if save_figs:
-        plt.savefig(FIGURE_SAVE_PATH + 'orientation_maps_diff_scales.png', dpi=200)
+        plt.savefig(FIGURE_SAVE_PATH + 'orientation_maps_diff_scales.png')
+        plt.close(fig)
 
 
 def plot_orientation_maps_diff_scales_with_ellipse(traj):
@@ -528,6 +534,7 @@ def plot_orientation_maps_diff_scales_with_ellipse(traj):
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'F_orientation_maps_diff_scales_with_ellipse.png')
+        plt.close(fig)
 
 
 def plot_polar_plot_excitatory(traj, plot_run_names, selected_neuron_idx,
@@ -546,6 +553,7 @@ def plot_polar_plot_excitatory(traj, plot_run_names, selected_neuron_idx,
     line_styles = ['dotted', 'solid', 'dashed']
     colors = ['r', 'lightsalmon', 'grey']
     line_widths = [1.5, 1.5, 1]
+    zorders = [10, 2, 1]
 
     max_rate = 0.0
     for run_idx, run_name in enumerate(plot_run_names):
@@ -560,7 +568,7 @@ def plot_polar_plot_excitatory(traj, plot_run_names, selected_neuron_idx,
         rate_plot.append(rate_plot[0])
         ax.plot(directions_plt, rate_plot, linewidth=line_widths[run_idx],
                 label='{:s} {:.2f}'.format(short_labels(label), hdi),
-                color=colors[run_idx], linestyle=line_styles[run_idx])
+                color=colors[run_idx], linestyle=line_styles[run_idx], zorder=zorders[run_idx])
     # ax.set_title('Firing Rate')
     # ax.plot([0.0, 0.0], [0.0, 1.05 * max_rate], color='red', alpha=0.25, linewidth=4.)
     # TODO: Set ticks for polar
@@ -575,6 +583,7 @@ def plot_polar_plot_excitatory(traj, plot_run_names, selected_neuron_idx,
     ax.axes.spines["polar"].set_visible(False)
     if save_figs:
         plt.savefig(figname)
+        plt.close(fig)
 
 
 def plot_polar_plot_inhibitory(traj, plot_run_names, selected_neuron_idx, figname=FIGURE_SAVE_PATH +
@@ -593,6 +602,7 @@ def plot_polar_plot_inhibitory(traj, plot_run_names, selected_neuron_idx, fignam
     line_styles = ['dotted', 'solid']
     colors = ['b', 'lightblue']
     line_widths = [1.5, 1.5]
+    zorders = [10, 2]
 
     for run_idx, run_name in enumerate(plot_run_names[:2]):
         # ax = axes[max_hdi_idx, run_idx]
@@ -604,7 +614,7 @@ def plot_polar_plot_inhibitory(traj, plot_run_names, selected_neuron_idx, fignam
         rate_plot.append(rate_plot[0])
         ax.plot(directions_plt, rate_plot, linewidth=line_widths[run_idx],
                 label='{:s} {:.2f}'.format(short_labels(label), hdi),
-                color=colors[run_idx], linestyle=line_styles[run_idx])
+                color=colors[run_idx], linestyle=line_styles[run_idx], zorder=zorders[run_idx])
     # ax.set_title('Inh. Firing Rate')
     # TODO: Set ticks for polar
     # ticks = [np.round(max_rate / 3.), np.round(max_rate * 2. / 3.), np.round(max_rate)]
@@ -620,6 +630,7 @@ def plot_polar_plot_inhibitory(traj, plot_run_names, selected_neuron_idx, fignam
 
     if save_figs:
         plt.savefig(figname)
+        plt.close(fig)
 
 
 def plot_hdi_over_corr_len(traj, plot_run_names):
@@ -677,6 +688,7 @@ def plot_hdi_over_corr_len(traj, plot_run_names):
     ax.legend()
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'hdi_over_corr_len_scaled.png', dpi=200)
+        plt.close(fig)
 
 
 def plot_hdi_histogram_excitatory(traj, plot_run_names):
@@ -703,6 +715,7 @@ def plot_hdi_histogram_excitatory(traj, plot_run_names):
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'hdi_histogram_excitatory.png', dpi=200)
+        plt.close(fig)
 
 
 def plot_hdi_violin_excitatory(traj, plot_run_names):
@@ -742,6 +755,7 @@ def plot_hdi_violin_excitatory(traj, plot_run_names):
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'hdi_violin_excitatory.png', dpi=200)
+        plt.close(fig)
 
 
 def plot_hdi_violin_inhibitory(traj, plot_run_names):
@@ -774,6 +788,7 @@ def plot_hdi_violin_inhibitory(traj, plot_run_names):
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'hdi_violin_inhibitory.png', dpi=200)
+        plt.close(fig)
 
 
 def plot_hdi_violin_combined(traj, plot_run_names):
@@ -830,6 +845,7 @@ def plot_hdi_violin_combined(traj, plot_run_names):
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'hdi_violin_combined.svg', dpi=200)
+        plt.close(fig)
 
 
 def plot_hdi_violin_combined_and_overlayed(traj, plot_run_names, ex_polar_plot_id, in_polar_plot_id):
@@ -910,6 +926,7 @@ def plot_hdi_violin_combined_and_overlayed(traj, plot_run_names, ex_polar_plot_i
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'hdi_violin_combined_and_overlayed.svg', dpi=200)
+        plt.close(fig)
 
     return ex_polar_plot_hdi, in_polar_plot_hdi
 
@@ -920,10 +937,6 @@ def plot_hdi_histogram_combined_and_overlayed(traj, plot_run_names, ex_polar_plo
     exc_hdis = []
     no_conn_hdi = 0.
 
-    in_polar_plot_hdi = []
-    ex_polar_plot_hdi = []
-
-    colors = ['black', 'red']
     for run_idx, run_name in enumerate(plot_run_names):
         label = traj.derived_parameters.runs[run_name].morphology.morph_label
         if label != NO_SYNAPSES:
@@ -937,7 +950,6 @@ def plot_hdi_histogram_combined_and_overlayed(traj, plot_run_names, ex_polar_plo
                               (inh_axonal_cloud[:, 1] <= traj.parameters.map.sheet_size - cut_off_dist)
             # print(inh_positions)
             inh_hdis.append(sorted(inh_head_direction_indices[inh_cut_off_ids]))
-            in_polar_plot_hdi.append(inh_head_direction_indices[in_polar_plot_id])
 
             exc_head_direction_indices = traj.results.runs[run_name].head_direction_indices
             ex_positions = traj.results.runs[run_name].ex_positions
@@ -947,39 +959,20 @@ def plot_hdi_histogram_combined_and_overlayed(traj, plot_run_names, ex_polar_plo
                               (ex_positions[:, 1] >= cut_off_dist) & \
                               (ex_positions[:, 1] <= traj.parameters.map.sheet_size - cut_off_dist)
             exc_hdis.append(sorted(exc_head_direction_indices[exc_cut_off_ids]))
-            ex_polar_plot_hdi.append(exc_head_direction_indices[ex_polar_plot_id])
+
         else:
             exc_head_direction_indices = traj.results.runs[run_name].head_direction_indices
             no_conn_hdi = np.mean(exc_head_direction_indices)
-            ex_polar_plot_hdi.append(exc_head_direction_indices[ex_polar_plot_id])
 
     # Look for a representative excitatory neuron
-
+    hdi_mean_dict = {}
     excitatory_hdi_means = [np.mean(hdis) for hdis in exc_hdis]
-    polar_hdi_mean = excitatory_hdi_means[0]
-    circular_hdi_mean = excitatory_hdi_means[1]
-
-    polar_ex_hdis = traj.results.runs[plot_run_names[0]].head_direction_indices
-    circular_ex_hdis = traj.results.runs[plot_run_names[1]].head_direction_indices
-
-    number_of_suggestions = 5
-    representative_excitatory_neuron_indices = get_indices_of_closest_values(polar_ex_hdis, polar_hdi_mean,
-                                                                             circular_ex_hdis,
-                                                                             circular_hdi_mean, 0.1 * np.abs(
-            polar_hdi_mean - circular_hdi_mean), number_of_suggestions)
+    hdi_mean_dict["polar_exc"] = excitatory_hdi_means[0]
+    hdi_mean_dict["circular_exc"] = excitatory_hdi_means[1]
 
     inhibitory_hdi_means = [np.mean(hdis) for hdis in inh_hdis]
-    polar_hdi_mean = inhibitory_hdi_means[0]
-    circular_hdi_mean = inhibitory_hdi_means[1]
-
-    polar_in_hdis = traj.results.runs[plot_run_names[0]].inh_head_direction_indices
-    circular_in_hdis = traj.results.runs[plot_run_names[1]].inh_head_direction_indices
-
-    number_of_suggestions = 5
-    representative_inhibitory_neuron_indices = get_indices_of_closest_values(polar_in_hdis, polar_hdi_mean,
-                                                                             circular_in_hdis,
-                                                                             circular_hdi_mean, 0.1 * np.abs(
-            polar_hdi_mean - circular_hdi_mean), number_of_suggestions)
+    hdi_mean_dict["polar_inh"] = inhibitory_hdi_means[0]
+    hdi_mean_dict["circular_inh"] = inhibitory_hdi_means[1]
 
     # # fig = plt.figure(figsize=(3.5, 3.5))
     # # gs1 = gridspec.GridSpec(1, 2)
@@ -1050,8 +1043,24 @@ def plot_hdi_histogram_combined_and_overlayed(traj, plot_run_names, ex_polar_plo
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'E_hdi_histogram_combined_and_overlayed_cutoff_{}um.png'.format(cut_off_dist))
+        plt.close(fig)
+
+    return hdi_mean_dict
 
-    return ex_polar_plot_hdi, in_polar_plot_hdi, representative_excitatory_neuron_indices, representative_inhibitory_neuron_indices
+
+def get_neurons_with_given_hdi(polar_hdi, circular_hdi, max_number_of_suggestions, plot_run_names, traj, type):
+    polar_run_name = plot_run_names[0]
+    circular_run_name = plot_run_names[1]
+    polar_ex_hdis = traj.results.runs[polar_run_name].head_direction_indices if type == "ex" else traj.results.runs[
+        polar_run_name].inh_head_direction_indices
+    circular_ex_hdis = traj.results.runs[circular_run_name].head_direction_indices if type == "ex" else \
+        traj.results.runs[
+            polar_run_name].inh_head_direction_indices
+    neuron_indices = get_indices_of_closest_values(polar_ex_hdis, polar_hdi,
+                                                   circular_ex_hdis,
+                                                   circular_hdi, 0.1 * np.abs(
+            polar_hdi - circular_hdi), max_number_of_suggestions)
+    return neuron_indices
 
 
 def get_indices_of_closest_values(first_list, first_value, second_list, second_value, absolute_tolerance_list_one,
@@ -1090,6 +1099,7 @@ def plot_hdi_histogram_inhibitory(traj, plot_run_names, in_polar_plot_id):
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'hdi_histogram_inhibitory.png', dpi=200)
+        plt.close(fig)
 
 
 def filter_run_names_by_par_dict(traj, par_dict):
@@ -1206,6 +1216,7 @@ def plot_exc_and_inh_hdi_over_corr_len(traj, plot_run_names):
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'F_hdi_over_corr_len_scaled.png')
+        plt.close(fig)
 
 
 def plot_in_degree_map(traj, plot_run_names):
@@ -1238,6 +1249,7 @@ def plot_in_degree_map(traj, plot_run_names):
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'in_degree_map.png', dpi=200)
+        plt.close(fig)
 
 
 def plot_spatial_hdi_map(traj, plot_run_names):
@@ -1262,6 +1274,7 @@ def plot_spatial_hdi_map(traj, plot_run_names):
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'spatial_hdi_map.png', dpi=200)
+        plt.close(fig)
 
 
 def plot_exc_spatial_hdi_map(traj, plot_run_names):
@@ -1286,6 +1299,7 @@ def plot_exc_spatial_hdi_map(traj, plot_run_names):
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'spatial_exc_hdi_map.png', dpi=200)
+        plt.close(fig)
 
 
 def plot_inh_spatial_hdi_map(traj, plot_run_names):
@@ -1311,6 +1325,7 @@ def plot_inh_spatial_hdi_map(traj, plot_run_names):
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'spatial_inh_hdi_map.png', dpi=200)
+        plt.close(fig)
 
 
 def get_phase_difference(total_difference):
@@ -1385,7 +1400,25 @@ def plot_firing_rate_similar_vs_diff_tuning(traj, plot_run_names):
 
     if save_figs:
         plt.savefig(FIGURE_SAVE_PATH + 'firing_rate_similar_vs_diff_tuning.png', dpi=200)
+        plt.close(fig)
+
+
+def get_firing_rates_along_preferred_axis(traj, run_name, neuron_idx):
+    firing_rates = traj.results[run_name].firing_rate_array[neuron_idx, :]
+    tuning = traj.results[run_name].ex_tunings[neuron_idx]
+    anti_tuning = tuning + np.pi if tuning + np.pi < np.pi else tuning - np.pi
+
+    tuning_idx = np.argmin(np.abs(directions - tuning))
+    anti_tuning_idx = np.argmin(np.abs(directions - anti_tuning))
+
+    firing_at_the_preferred_direction = firing_rates[tuning_idx]
+    firing_at_the_opposite_direction = firing_rates[anti_tuning_idx]
 
+    return firing_at_the_preferred_direction, firing_at_the_opposite_direction
+
+def get_hdi(traj, run_name, neuron_idx, type):
+    return traj.results.runs[run_name].head_direction_indices[neuron_idx] if type=="ex" else traj.results.runs[
+        run_name].inh_head_direction_indices[neuron_idx]
 
 if __name__ == "__main__":
     traj = Trajectory(TRAJ_NAME, add_time=False, dynamic_imports=Brian2MonitorResult)
@@ -1435,7 +1468,7 @@ if __name__ == "__main__":
     print()
 
     #
-    plot_colorbar(figsize=(0.8*panel_size, 0.8*panel_size), figname=FIGURE_SAVE_PATH+"A_i_colormap.svg")
+    plot_colorbar(figsize=(0.8 * panel_size, 0.8 * panel_size), figname=FIGURE_SAVE_PATH + "A_i_colormap.svg")
     plot_input_map(traj, run_name_dict[POLARIZED], figname="A_i_exemplary_input_map.png",
                    figsize=(panel_size, panel_size))
     plot_axonal_clouds(traj, plot_run_names)
@@ -1444,25 +1477,50 @@ if __name__ == "__main__":
     plot_firing_rate_map_excitatory(traj, direction_idx, plot_run_names, selected_neuron_excitatory)
 
     in_max_rate = plot_firing_rate_map_inhibitory(traj, direction_idx, plot_run_names, selected_inhibitory_neuron)
+
     #
-    ex_polar_plot_hdi, in_polar_plot_hdi, suggestions_excitatory_neurons, \
-    suggestions_inhibitory_neurons = plot_hdi_histogram_combined_and_overlayed(
+    hdi_means = plot_hdi_histogram_combined_and_overlayed(
         traj, plot_run_names,
         selected_neuron_excitatory,
         selected_inhibitory_neuron,
         cut_off_dist=100.)
+    #
+    number_of_suggestions = 30
+    representative_excitatory_neuron_indices = get_neurons_with_given_hdi(hdi_means["polar_exc"], hdi_means[
+        "circular_exc"],
+                                                                          number_of_suggestions, plot_run_names,
+                                                                          traj, "ex")
+    representative_inhibitory_neuron_indices = get_neurons_with_given_hdi(hdi_means["polar_inh"],
+                                                                          hdi_means["circular_inh"],
+                                                                          number_of_suggestions, plot_run_names,
+                                                                          traj, "in")
 
     plot_polar_plot_excitatory(traj, plot_run_names, selected_neuron_excitatory)
 
-    for suggested_excitatory_neuron in suggestions_excitatory_neurons:
+    for suggested_excitatory_neuron in representative_excitatory_neuron_indices:
+        preferred_polar, opposite_polar = get_firing_rates_along_preferred_axis(
+            traj, run_name_dict["ellipsoid"], suggested_excitatory_neuron)
+        preferred_circular, opposite_circular = get_firing_rates_along_preferred_axis(
+                    traj, run_name_dict["circular"], suggested_excitatory_neuron)
+
         plot_polar_plot_excitatory(traj, plot_run_names, suggested_excitatory_neuron,
-                                   figname=FIGURE_SAVE_PATH + "X_polar_plot_excitatory_neuron_{:d}.png".format(
-                                       suggested_excitatory_neuron))
+                                   figname=FIGURE_SAVE_PATH + "X_polar_plot_excitatory_neuron_signal_increase_{"
+                                                              ":0>2.0f}_noise_decrease_{"
+                                                              ":0>2.0f}_neuron_id_{"
+                                                              ":d}.png".format(
+                                       preferred_polar-preferred_circular,
+                                       opposite_circular-opposite_polar, suggested_excitatory_neuron))
+
 
     plot_polar_plot_inhibitory(traj, plot_run_names, selected_inhibitory_neuron)
-    for suggested_inhibitory_neuron in suggestions_inhibitory_neurons:
+
+    for suggested_inhibitory_neuron in representative_inhibitory_neuron_indices:
+        polar_hdi = get_hdi(traj, run_name_dict["ellipsoid"], suggested_inhibitory_neuron, "in")
+        circular_hdi = get_hdi(traj, run_name_dict["circular"], suggested_inhibitory_neuron, "in")
         plot_polar_plot_inhibitory(traj, plot_run_names, suggested_inhibitory_neuron,
-                                   figname=FIGURE_SAVE_PATH + "X_polar_plot_inhibitory_neuron_{:d}.png".format(
+                                   figname=FIGURE_SAVE_PATH + "X_polar_plot_inhibitory_neuron_polarized_{"
+                                                              ":.2f}_circular_{:.2f}_neuron_id_{"
+                                                              ":d}.png".format(polar_hdi, circular_hdi,
                                        suggested_inhibitory_neuron))
 
     plot_firing_rate_similar_vs_diff_tuning(traj, plot_run_names)