paper_figures_spatial_head_direction_network_perlin_map.py 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. import itertools
  2. import os
  3. import matplotlib.legend as mlegend
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import pandas as pd
  7. from matplotlib.patches import Ellipse
  8. from matplotlib.patches import Polygon
  9. from matplotlib.patches import Rectangle
  10. from mpl_toolkits.axes_grid1 import ImageGrid
  11. from pypet import Trajectory
  12. from pypet.brian2 import Brian2MonitorResult
  13. from scripts.interneuron_placement import get_position_mesh, Pickle, get_correct_position_mesh
  14. from scripts.model_figure.plot_circular_colorbar import plot_colorbar
  15. from scripts.spatial_maps.simplex_input_tuning_correlation.correlation_length_distance_perlin import \
  16. correlation_length_fit_dict
  17. from scripts.spatial_network.figures_spatial_head_direction_network_orientation_map import plot_hdi_in_space
  18. from scripts.spatial_network.perlin.figure_utils import remove_frame, remove_ticks, add_length_scale, cm_per_inch, \
  19. panel_size, head_direction_input_colormap
  20. from scripts.spatial_network.perlin.run_entropy_maximisation_perlin_map import DATA_FOLDER, TRAJ_NAME, \
  21. get_input_head_directions, POLARIZED, CIRCULAR, NO_SYNAPSES
  22. plt.style.use('figures.mplstyle')
  23. FIGURE_SAVE_PATH = '../../../figures/figure_4_paper_perlin/svg_figures/'
  24. FIGURE_SAVE_FORMAT = '.pdf'
  25. save_figs = False
  26. def tablelegend(ax, col_labels=None, row_labels=None, title_label="", *args, **kwargs):
  27. """
  28. Place a table legend on the axes.
  29. Creates a legend where the labels are not directly placed with the artists,
  30. but are used as row and column headers, looking like this:
  31. title_label | col_labels[1] | col_labels[2] | col_labels[3]
  32. -------------------------------------------------------------
  33. row_labels[1] |
  34. row_labels[2] | <artists go there>
  35. row_labels[3] |
  36. Parameters
  37. ----------
  38. ax : `matplotlib.axes.Axes`
  39. The artist that contains the legend table, i.e. current axes instant.
  40. col_labels : list of str, optional
  41. A list of labels to be used as column headers in the legend table.
  42. `len(col_labels)` needs to match `ncol`.
  43. row_labels : list of str, optional
  44. A list of labels to be used as row headers in the legend table.
  45. `len(row_labels)` needs to match `len(handles) // ncol`.
  46. title_label : str, optional
  47. Label for the top left corner in the legend table.
  48. ncol : int
  49. Number of columns.
  50. Other Parameters
  51. ----------------
  52. Refer to `matplotlib.legend.Legend` for other parameters.
  53. """
  54. #################### same as `matplotlib.axes.Axes.legend` #####################
  55. handles, labels, extra_args, kwargs = mlegend._parse_legend_args([ax], *args, **kwargs)
  56. if len(extra_args):
  57. raise TypeError('legend only accepts two non-keyword arguments')
  58. if col_labels is None and row_labels is None:
  59. ax.legend_ = mlegend.Legend(ax, handles, labels, **kwargs)
  60. ax.legend_._remove_method = ax._remove_legend
  61. return ax.legend_
  62. #################### modifications for table legend ############################
  63. else:
  64. ncol = kwargs.pop('ncol')
  65. handletextpad = kwargs.pop('handletextpad', 0 if col_labels is None else -2)
  66. title_label = [title_label]
  67. # blank rectangle handle
  68. extra = [Rectangle((0, 0), 1, 1, fc="w", fill=False, edgecolor='none', linewidth=0)]
  69. # empty label
  70. empty = [""]
  71. # number of rows infered from number of handles and desired number of columns
  72. nrow = len(handles) // ncol
  73. # organise the list of handles and labels for table construction
  74. if col_labels is None:
  75. assert nrow == len(
  76. row_labels), "nrow = len(handles) // ncol = %s, but should be equal to len(row_labels) = %s." % (
  77. nrow, len(row_labels))
  78. leg_handles = extra * nrow
  79. leg_labels = row_labels
  80. elif row_labels is None:
  81. assert ncol == len(col_labels), "ncol = %s, but should be equal to len(col_labels) = %s." % (
  82. ncol, len(col_labels))
  83. leg_handles = []
  84. leg_labels = []
  85. else:
  86. assert nrow == len(
  87. row_labels), "nrow = len(handles) // ncol = %s, but should be equal to len(row_labels) = %s." % (
  88. nrow, len(row_labels))
  89. assert ncol == len(col_labels), "ncol = %s, but should be equal to len(col_labels) = %s." % (
  90. ncol, len(col_labels))
  91. leg_handles = extra + extra * nrow
  92. leg_labels = title_label + row_labels
  93. for col in range(ncol):
  94. if col_labels is not None:
  95. leg_handles += extra
  96. leg_labels += [col_labels[col]]
  97. leg_handles += handles[col * nrow:(col + 1) * nrow]
  98. leg_labels += empty * nrow
  99. # Create legend
  100. ax.legend_ = mlegend.Legend(ax, leg_handles, leg_labels, ncol=ncol + int(row_labels is not None),
  101. handletextpad=handletextpad, **kwargs)
  102. ax.legend_._remove_method = ax._remove_legend
  103. return ax.legend_
  104. def get_closest_correlation_length(traj, correlation_length):
  105. available_lengths = sorted(list(set(traj.f_get("correlation_length").f_get_range())))
  106. closest_length = available_lengths[np.argmin(np.abs(np.array(available_lengths) - correlation_length))]
  107. if closest_length != correlation_length:
  108. print("Warning: desired correlation length {:.1f} not available. Taking {:.1f} instead".format(
  109. correlation_length, closest_length))
  110. corr_len = closest_length
  111. return corr_len
  112. def get_closest_fit_correlation_length(traj, fit_corr_len):
  113. corr_len_fit_dict = correlation_length_fit_dict(traj, map_type='perlin', load=True)
  114. available_lengths = list(corr_len_fit_dict.values())
  115. closest_length = available_lengths[np.argmin(np.abs(np.array(available_lengths) - fit_corr_len))]
  116. closest_corresponding_scale = list(corr_len_fit_dict.keys())[list(corr_len_fit_dict.values()).index(closest_length)]
  117. if closest_length != fit_corr_len:
  118. print("Warning: desired fit correlation length {:.1f} not available. Taking {:.1f} instead".format(
  119. fit_corr_len, closest_length))
  120. return closest_corresponding_scale
  121. def colorbar(mappable):
  122. from mpl_toolkits.axes_grid1 import make_axes_locatable
  123. import matplotlib.pyplot as plt
  124. last_axes = plt.gca()
  125. ax = mappable.axes
  126. fig = ax.figure
  127. divider = make_axes_locatable(ax)
  128. cax = divider.append_axes("right", size="5%", pad=0.05)
  129. cbar = fig.colorbar(mappable, cax=cax)
  130. plt.sca(last_axes)
  131. return cbar
  132. def plot_firing_rate_map_excitatory(traj, direction_idx, plot_run_names, exemplary_excitatory_neuron_id):
  133. max_val = 0
  134. for run_name in plot_run_names:
  135. fr_array = traj.results.runs[run_name].firing_rate_array
  136. f_rates = fr_array[:, direction_idx]
  137. run_max_val = np.max(f_rates)
  138. if run_max_val > max_val:
  139. # if traj.derived_parameters.runs[run_name].morphology.morph_label == POLARIZED:
  140. # n_id_max_rate = np.argmax(f_rates)
  141. max_val = run_max_val
  142. n_id_polar_plot = exemplary_excitatory_neuron_id
  143. # Mark the neuron that is shown in polar plot
  144. ex_positions = traj.results.runs[plot_run_names[0]].ex_positions
  145. polar_plot_x, polar_plot_y = ex_positions[n_id_polar_plot]
  146. # Vertices for the plotted triangle
  147. tr_scale = 30.
  148. tr_x = tr_scale * np.cos(2. * np.pi / 3. + np.pi / 2.)
  149. tr_y = tr_scale * np.sin(2. * np.pi / 3. + np.pi / 2.) + polar_plot_y
  150. tr_vertices = np.array(
  151. [[polar_plot_x, polar_plot_y + tr_scale], [tr_x + polar_plot_x, tr_y], [-tr_x + polar_plot_x, tr_y]])
  152. height = 1 * panel_size
  153. width = 3 * panel_size
  154. fig = plt.figure(figsize=(width, height))
  155. axes = ImageGrid(fig, (0.05, 0.15, 0.85, 0.7), axes_pad=panel_size / 6.0, cbar_location="right",
  156. cbar_mode="single",
  157. cbar_size="7%", cbar_pad=panel_size / 10.0,
  158. nrows_ncols=(1, 3))
  159. for ax, run_name in zip(axes, [plot_run_names[i] for i in [2, 0, 1]]):
  160. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  161. X, Y = get_correct_position_mesh(traj.results.runs[run_name].ex_positions)
  162. firing_rate_array = traj.results[run_name].firing_rate_array
  163. number_of_excitatory_neurons_per_row = int(np.sqrt(traj.N_E))
  164. c = ax.pcolor(X, Y, np.reshape(firing_rate_array[:, direction_idx], (number_of_excitatory_neurons_per_row,
  165. number_of_excitatory_neurons_per_row)),
  166. vmin=0, vmax=max_val, cmap='Reds')
  167. # ax.set_title(adjust_label(label))
  168. # ax.add_artist(Ellipse((polar_plot_x, polar_plot_y), 20., 20., color='k', fill=False, lw=2.))
  169. # ax.add_artist(Ellipse((polar_plot_x, polar_plot_y), 20., 20., color='w', fill=False, lw=1.))
  170. ax.add_artist(Polygon(tr_vertices, closed=True, fill=False, lw=2.5, color='k'))
  171. ax.add_artist(Polygon(tr_vertices, closed=True, fill=False, lw=1.5, color='w'))
  172. for spine in ax.spines.values():
  173. spine.set_edgecolor("grey")
  174. spine.set_linewidth(1)
  175. remove_ticks(ax)
  176. # fig.suptitle('spatial firing rate map', fontsize=16)
  177. ax.cax.colorbar(c)
  178. ax.cax.annotate("fr (Hz)", xy=(1, 1), xytext=(3, 3), xycoords="axes fraction", textcoords="offset points")
  179. # fig.tight_layout()
  180. if save_figs:
  181. plt.savefig(FIGURE_SAVE_PATH + 'C_firing_rate_map_excitatory' + FIGURE_SAVE_FORMAT, dpi=300)
  182. plt.close(fig)
  183. def plot_firing_rate_map_inhibitory(traj, direction_idx, plot_run_names, selected_inhibitory_neuron):
  184. max_val = 0
  185. for run_name in plot_run_names:
  186. fr_array = traj.results.runs[run_name].inh_firing_rate_array
  187. f_rates = fr_array[:, direction_idx]
  188. run_max_val = np.max(f_rates)
  189. if run_max_val > max_val:
  190. max_val = run_max_val
  191. n_id_polar_plot = selected_inhibitory_neuron
  192. # Mark the neuron that is shown in Polar plot
  193. inhibitory_axonal_cloud_array = traj.results.runs[plot_run_names[1]].inhibitory_axonal_cloud_array
  194. polar_plot_x = inhibitory_axonal_cloud_array[n_id_polar_plot, 0]
  195. polar_plot_y = inhibitory_axonal_cloud_array[n_id_polar_plot, 1]
  196. width = 3 * panel_size
  197. height = panel_size
  198. fig = plt.figure(figsize=(width, height))
  199. axes = ImageGrid(fig, (0.05, 0.15, 0.85, 0.7), axes_pad=panel_size / 6.0, cbar_location="right",
  200. cbar_mode="single",
  201. cbar_size="7%", cbar_pad=panel_size / 10.0,
  202. nrows_ncols=(1, 3))
  203. for ax, run_name in zip(axes, [plot_run_names[i] for i in [2, 0, 1]]):
  204. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  205. inhibitory_axonal_cloud_array = traj.results.runs[run_name].inhibitory_axonal_cloud_array
  206. inh_positions = [[p[0], p[1]] for p in inhibitory_axonal_cloud_array]
  207. X, Y = get_correct_position_mesh(inh_positions)
  208. inh_firing_rate_array = traj.results[run_name].inh_firing_rate_array
  209. number_of_inhibitory_neurons_per_row = int(np.sqrt(traj.N_I))
  210. c = ax.pcolor(X, Y, np.reshape(inh_firing_rate_array[:, direction_idx], (number_of_inhibitory_neurons_per_row,
  211. number_of_inhibitory_neurons_per_row)),
  212. vmin=0, vmax=max_val, cmap='Blues')
  213. # ax.set_title(adjust_label(label))
  214. circle_r = 40.
  215. ax.add_artist(Ellipse((polar_plot_x, polar_plot_y), circle_r, circle_r, color='k', fill=False, lw=4.5))
  216. ax.add_artist(Ellipse((polar_plot_x, polar_plot_y), circle_r, circle_r, color='w', fill=False, lw=3))
  217. for spine in ax.spines.values():
  218. spine.set_edgecolor("grey")
  219. spine.set_linewidth(0.5)
  220. remove_ticks(ax)
  221. # fig.colorbar(c, ax=ax, label="f (Hz)")
  222. # fig.suptitle('spatial firing rate map', fontsize=16)
  223. cbar = ax.cax.colorbar(c)
  224. cbar_ticks = range(0,151,30)
  225. cbar.ax.set_yticks(cbar_ticks)
  226. ax.cax.annotate("fr (Hz)", xy=(1, 1), xytext=(3, 3), xycoords="axes fraction", textcoords="offset points")
  227. # fig.tight_layout()
  228. if save_figs:
  229. plt.savefig(FIGURE_SAVE_PATH + 'C_firing_rate_map_inhibitory' + FIGURE_SAVE_FORMAT, dpi=300)
  230. plt.close(fig)
  231. return max_val
  232. def plot_hdi_over_tuning(traj, plot_run_names):
  233. fig, ax = plt.subplots(1, 1)
  234. for run_idx, run_name in enumerate(plot_run_names):
  235. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  236. ex_tunings = traj.results.runs[run_name].ex_tunings
  237. ex_tunings_plt = np.array(ex_tunings)
  238. sort_ids = ex_tunings_plt.argsort()
  239. ex_tunings_plt = ex_tunings_plt[sort_ids]
  240. head_direction_indices = traj.results[run_name].head_direction_indices
  241. hdi_plt = head_direction_indices
  242. hdi_plt = hdi_plt[sort_ids]
  243. ax.scatter(ex_tunings_plt, hdi_plt, label=label, alpha=0.3)
  244. ax.legend()
  245. ax.set_xlabel("Angles (rad)")
  246. ax.set_ylabel("head direction index")
  247. ax.set_title('hdi over input tuning', fontsize=16)
  248. if save_figs:
  249. plt.savefig(FIGURE_SAVE_PATH + 'hdi_over_tuning' + FIGURE_SAVE_FORMAT)
  250. plt.close(fig)
  251. def normal_labels(label):
  252. if label == POLARIZED:
  253. label = 'polarized'
  254. elif label == NO_SYNAPSES:
  255. label = 'no interneurons'
  256. return label
  257. def short_labels(label):
  258. if label == POLARIZED:
  259. label = 'pol.'
  260. elif label == CIRCULAR:
  261. label = 'cir.'
  262. elif label == "no conn":
  263. label = "no inh."
  264. return label
  265. def plot_input_map(traj, run_name, figsize=(panel_size, panel_size), figname='input_map' + FIGURE_SAVE_FORMAT):
  266. n_ex = int(np.sqrt(traj.N_E))
  267. width, height = figsize
  268. fig = plt.figure(figsize=(width, height))
  269. axes = ImageGrid(fig, (0.15, 0.1, 0.7, 0.8), axes_pad=panel_size / 3.0, cbar_location="right", cbar_mode=None,
  270. nrows_ncols=(1, 1))
  271. ax = axes[0]
  272. traj.f_set_crun(run_name)
  273. X, Y = get_correct_position_mesh(traj.results.runs[run_name].ex_positions)
  274. head_dir_preference = np.array(traj.results.runs[run_name].ex_tunings).reshape((n_ex, n_ex))
  275. scale_length = traj.morphology.long_axis * 2
  276. traj.f_restore_default()
  277. ax.pcolor(X, Y, head_dir_preference, vmin=-np.pi, vmax=np.pi, cmap=head_direction_input_colormap)
  278. for spine in ax.spines.values():
  279. spine.set_edgecolor("grey")
  280. spine.set_linewidth(0.5)
  281. remove_ticks(ax)
  282. start_scale_x = 100
  283. end_scale_x = start_scale_x + scale_length
  284. start_scale_y = -70
  285. end_scale_y = start_scale_y
  286. add_length_scale(ax, scale_length, start_scale_x, end_scale_x, start_scale_y, end_scale_y)
  287. ax.cax.set_visible(False)
  288. if save_figs:
  289. plt.savefig(FIGURE_SAVE_PATH + figname)
  290. plt.close(fig)
  291. def plot_example_input_maps(traj, figsize=(panel_size, panel_size), figname='perlin_example_input_maps' + FIGURE_SAVE_FORMAT):
  292. n_ex = int(np.sqrt(traj.N_E))
  293. width, height = figsize
  294. fig = plt.figure(figsize=(width, height))
  295. axes = ImageGrid(fig, (0.1, 0.1, 0.85, 0.85), axes_pad=panel_size / 12.0, nrows_ncols=(3, 3))
  296. fit_corr_len_list = [20, 50, 100]
  297. seed_list = [1, 2, 3]
  298. corr_and_seed = itertools.product(fit_corr_len_list, seed_list)
  299. corr_len_fit_dict = correlation_length_fit_dict(traj, map_type='perlin', load=True)
  300. for idx, (ax, (fit_corr_len, seed)) in enumerate(zip(axes, corr_and_seed)):
  301. corresp_scale = get_closest_fit_correlation_length(traj, fit_corr_len)
  302. par_dict = {'seed': seed, 'correlation_length': corresp_scale}
  303. run_name = filter_run_names_by_par_dict(traj, par_dict)[0]
  304. print(run_name)
  305. traj.f_set_crun(run_name)
  306. X, Y = get_correct_position_mesh(traj.results.runs[run_name].ex_positions)
  307. head_dir_preference = np.array(traj.results.runs[run_name].ex_tunings).reshape((n_ex, n_ex))
  308. scale_length = traj.morphology.long_axis * 2
  309. traj.f_restore_default()
  310. ax.pcolor(X, Y, head_dir_preference, vmin=-np.pi, vmax=np.pi, cmap=head_direction_input_colormap)
  311. for spine in ax.spines.values():
  312. spine.set_edgecolor("grey")
  313. spine.set_linewidth(0.5)
  314. remove_ticks(ax)
  315. ax.set_ylabel('{:3.0f} um'.format(corr_len_fit_dict[corresp_scale]))
  316. inhibitory_axonal_cloud_array = traj.results.runs[run_name].inhibitory_axonal_cloud_array
  317. plt_polar_id = 255
  318. plt_polar_coordinates = inhibitory_axonal_cloud_array[plt_polar_id]
  319. plt_polar_neuron = Pickle(plt_polar_coordinates[0],
  320. plt_polar_coordinates[1],
  321. traj.morphology.long_axis,
  322. traj.morphology.short_axis,
  323. plt_polar_coordinates[2])
  324. circ_radius = np.sqrt(traj.morphology.long_axis * traj.morphology.short_axis)
  325. plt_circ_id = 65
  326. plt_circ_coordinates = inhibitory_axonal_cloud_array[plt_circ_id]
  327. plt_circ_neuron = Pickle(plt_circ_coordinates[0],
  328. plt_circ_coordinates[1],
  329. circ_radius,
  330. circ_radius,
  331. plt_circ_coordinates[2])
  332. plt_interneurons = [plt_polar_neuron, plt_circ_neuron]
  333. for p in plt_interneurons:
  334. ell = p.get_ellipse()
  335. edgecolor = 'black'
  336. alpha = 1
  337. zorder = 10
  338. linewidth = 1.
  339. ell.set_edgecolor(edgecolor)
  340. ell.set_alpha(alpha)
  341. ell.set_zorder(zorder)
  342. ell.set_linewidth(linewidth)
  343. ax.add_artist(ell)
  344. if idx == 8:
  345. start_scale_x = 100
  346. end_scale_x = start_scale_x + scale_length
  347. start_scale_y = -70
  348. end_scale_y = start_scale_y
  349. add_length_scale(ax, scale_length, start_scale_x, end_scale_x, start_scale_y, end_scale_y)
  350. ax.cax.set_visible(False)
  351. if save_figs:
  352. plt.savefig(FIGURE_SAVE_PATH + figname)
  353. plt.close(fig)
  354. def plot_axonal_clouds(traj, plot_run_names):
  355. n_ex = int(np.sqrt(traj.N_E))
  356. height = 1 * panel_size
  357. width = 3 * panel_size
  358. cluster_positions = [(250, 250), (750, 200), (450, 600)]
  359. cluster_sizes = [4, 4, 4]
  360. selected_neurons = []
  361. inhibitory_positions = traj.results.runs[plot_run_names[0]].inhibitory_axonal_cloud_array[:, :2]
  362. for cluster_position, number_of_neurons_in_cluster in zip(cluster_positions, cluster_sizes):
  363. selection = get_neurons_close_to_given_position(cluster_position, number_of_neurons_in_cluster,
  364. inhibitory_positions)
  365. selected_neurons.extend(selection)
  366. fig = plt.figure(figsize=(width, height))
  367. axes = ImageGrid(fig, 111, axes_pad=0.15, cbar_location="right", cbar_mode="single", cbar_size="7%",
  368. nrows_ncols=(1, 3))
  369. for ax, run_name in zip(axes, [plot_run_names[i] for i in [2, 0, 1]]):
  370. traj.f_set_crun(run_name)
  371. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  372. X, Y = get_correct_position_mesh(traj.results.runs[run_name].ex_positions)
  373. inhibitory_axonal_cloud_array = traj.results.runs[run_name].inhibitory_axonal_cloud_array
  374. axonal_clouds = [Pickle(p[0], p[1], traj.morphology.long_axis, traj.morphology.short_axis, p[2]) for p in
  375. inhibitory_axonal_cloud_array]
  376. head_dir_preference = np.array(traj.results.runs[run_name].ex_tunings).reshape((n_ex, n_ex))
  377. c = ax.pcolor(X, Y, head_dir_preference, vmin=-np.pi, vmax=np.pi, cmap=head_direction_input_colormap)
  378. # ax.set_title(normal_labels(label))
  379. ax.set_aspect('equal')
  380. remove_frame(ax)
  381. remove_ticks(ax)
  382. # fig.colorbar(c, ax=ax, label="Tuning")
  383. if label != NO_SYNAPSES and axonal_clouds is not None:
  384. for i, p in enumerate(axonal_clouds):
  385. ell = p.get_ellipse()
  386. if i in selected_neurons:
  387. edgecolor = 'black'
  388. alpha = 1
  389. zorder = 10
  390. linewidth = 1
  391. else:
  392. edgecolor = 'gray'
  393. alpha = 0.5
  394. zorder = 1
  395. linewidth = 0.3
  396. ell.set_edgecolor(edgecolor)
  397. ell.set_alpha(alpha)
  398. ell.set_zorder(zorder)
  399. ell.set_linewidth(linewidth)
  400. ax.add_artist(ell)
  401. traj.f_set_crun(plot_run_names[0])
  402. scale_length = traj.morphology.long_axis * 2
  403. traj.f_restore_default()
  404. start_scale_x = 100
  405. end_scale_x = start_scale_x + scale_length
  406. start_scale_y = -70
  407. end_scale_y = start_scale_y
  408. add_length_scale(axes[0], scale_length, start_scale_x, end_scale_x, start_scale_y, end_scale_y)
  409. axes[1].set_yticklabels([])
  410. axes[2].set_yticklabels([])
  411. axes[0].cax.set_visible(False)
  412. traj.f_restore_default()
  413. if save_figs:
  414. plt.savefig(FIGURE_SAVE_PATH + 'B_i_axonal_clouds' + FIGURE_SAVE_FORMAT)
  415. plt.close(fig)
  416. def get_neurons_close_to_given_position(cluster_position, number_of_neurons_in_cluster, positions):
  417. position = np.array(cluster_position)
  418. distance_vectors = positions - np.expand_dims(position, 0).repeat(positions.shape[0],
  419. axis=0)
  420. distances = np.linalg.norm(distance_vectors, axis=1)
  421. selection = list(np.argpartition(distances, number_of_neurons_in_cluster)[:number_of_neurons_in_cluster])
  422. return selection
  423. def plot_orientation_maps_diff_scales(traj):
  424. n_ex = int(np.sqrt(traj.N_E))
  425. scale_run_names = []
  426. plot_scales = [0.0, 100.0, 200.0, 300.0]
  427. for scale in plot_scales:
  428. par_dict = {'seed': 1, 'correlation_length': get_closest_correlation_length(traj, scale), 'long_axis': 100.}
  429. scale_run_names.append(*filter_run_names_by_par_dict(traj, par_dict))
  430. fig, axes = plt.subplots(1, 4, figsize=(18., 4.5))
  431. for ax, run_name, scale in zip(axes, scale_run_names, plot_scales):
  432. traj.f_set_crun(run_name)
  433. X, Y = get_position_mesh(traj.results.runs[run_name].ex_positions)
  434. head_dir_preference = np.array(traj.results.runs[run_name].ex_tunings).reshape((n_ex, n_ex))
  435. # TODO: Why was this transposed for plotting? (now changed)
  436. c = ax.pcolor(X, Y, head_dir_preference, vmin=-np.pi, vmax=np.pi, cmap='twilight')
  437. ax.set_title('Correlation length: {}'.format(scale))
  438. fig.colorbar(c, ax=ax, label="Tuning")
  439. # fig.suptitle('axonal cloud', fontsize=16)
  440. traj.f_restore_default()
  441. if save_figs:
  442. plt.savefig(FIGURE_SAVE_PATH + 'orientation_maps_diff_scales' + FIGURE_SAVE_FORMAT)
  443. plt.close(fig)
  444. def plot_orientation_maps_diff_scales_with_ellipse(traj):
  445. n_ex = int(np.sqrt(traj.N_E))
  446. scale_run_names = []
  447. plot_scales = [0.0, 400.0, 800.0]
  448. real_scales = [get_closest_correlation_length(traj, scale) for scale in plot_scales]
  449. for scale in plot_scales:
  450. par_dict = {'seed': 1, 'correlation_length': get_closest_correlation_length(traj, scale), 'long_axis': 100.}
  451. scale_run_names.append(*filter_run_names_by_par_dict(traj, par_dict))
  452. print(scale_run_names)
  453. inhibitory_positions = traj.results.runs[scale_run_names[1]].inhibitory_axonal_cloud_array[:, :2]
  454. selected_polar_neuron = get_neurons_close_to_given_position((300, 300), 1, inhibitory_positions)[0]
  455. print(selected_polar_neuron)
  456. selected_circular_neuron = get_neurons_close_to_given_position((600, 600), 1, inhibitory_positions)[0]
  457. width = panel_size
  458. height = 1.5 * panel_size
  459. fig = plt.figure(figsize=(width, height))
  460. axes = ImageGrid(fig, 111, axes_pad=0.15,
  461. nrows_ncols=(len(plot_scales), 1))
  462. for ax, run_name, scale in zip(axes, scale_run_names, real_scales):
  463. traj.f_set_crun(run_name)
  464. X, Y = get_position_mesh(traj.results.runs[run_name].ex_positions)
  465. inhibitory_axonal_cloud_array = traj.results.runs[run_name].inhibitory_axonal_cloud_array
  466. axonal_clouds = [Pickle(p[0], p[1], traj.morphology.long_axis, traj.morphology.short_axis, p[2]) for p in
  467. inhibitory_axonal_cloud_array]
  468. head_dir_preference = np.array(traj.results.runs[run_name].ex_tunings).reshape((n_ex, n_ex))
  469. # TODO: Why was this transposed for plotting? (now changed)
  470. c = ax.pcolor(X, Y, head_dir_preference, vmin=-np.pi, vmax=np.pi, cmap=head_direction_input_colormap)
  471. # ax.set_title('Correlation length: {}'.format(scale))
  472. # fig.colorbar(c, ax=ax, label="Tuning")
  473. ax.set_xticks([])
  474. ax.set_yticks([])
  475. p1 = axonal_clouds[selected_polar_neuron]
  476. ell = p1.get_ellipse()
  477. ell._linewidth = 2.
  478. ax.add_artist(ell)
  479. p2 = axonal_clouds[selected_circular_neuron]
  480. circ_r = 2 * np.sqrt(p2.a * p2.b)
  481. circ = Ellipse((p2.x, p2.y), circ_r, circ_r, fill=False, zorder=2, edgecolor='k')
  482. circ._linewidth = 2.
  483. ax.add_artist(circ)
  484. ax.annotate("{:.0f}".format(scale), xy=(1.0, 0.5), xytext=(2, 0), xycoords="axes fraction", textcoords="offset "
  485. "points",
  486. va="center", ha="left")
  487. remove_frame(ax)
  488. # fig.suptitle('axonal cloud', fontsize=16)
  489. traj.f_restore_default()
  490. add_length_scale(axes[1], 200, -300, -100, 50, 50)
  491. axes[0].annotate("input maps", xy=(-0.5, 1.0), xycoords="axes fraction", rotation=90, ha="left", va="top")
  492. fig.tight_layout()
  493. if save_figs:
  494. plt.savefig(FIGURE_SAVE_PATH + 'F_orientation_maps_diff_scales_with_ellipse' + FIGURE_SAVE_FORMAT)
  495. plt.close(fig)
  496. def plot_polar_plot_excitatory(traj, plot_run_names, selected_neuron_idx,
  497. figname=FIGURE_SAVE_PATH + 'D_polar_plot_excitatory' + FIGURE_SAVE_FORMAT):
  498. directions = np.linspace(-np.pi, np.pi, traj.input.number_of_directions, endpoint=False)
  499. directions_plt = list(directions)
  500. directions_plt.append(directions[0])
  501. height = panel_size
  502. width = panel_size
  503. fig, ax = plt.subplots(1, 1, figsize=(height, width), subplot_kw=dict(projection='polar'))
  504. # head_direction_indices = traj.results.runs[plot_run_names[0]].head_direction_indices
  505. # sorted_ids = np.argsort(head_direction_indices)
  506. # plot_n_idx = sorted_ids[-75]
  507. plot_n_idx = selected_neuron_idx
  508. line_styles = ['dotted', 'solid', 'dashed']
  509. colors = ['r', 'lightsalmon', 'grey']
  510. labels = ['pol. ', 'cir. ', 'no inh.']
  511. line_widths = [1.5, 1.5, 1]
  512. zorders = [10, 2, 1]
  513. max_rate = 0.0
  514. ax.plot([], [], color='white',label=' ')
  515. hdis = []
  516. for run_idx, run_name in enumerate(plot_run_names):
  517. # label = traj.derived_parameters.runs[run_name].morphology.morph_label
  518. label = labels[run_idx]
  519. hdi = traj.results.runs[run_name].head_direction_indices[selected_neuron_idx]
  520. hdis.append(hdi)
  521. tuning_vectors = traj.results.runs[run_name].tuning_vectors
  522. rate_plot = [np.linalg.norm(v) for v in tuning_vectors[plot_n_idx]]
  523. run_max_rate = np.max(rate_plot)
  524. if run_max_rate > max_rate:
  525. max_rate = run_max_rate
  526. rate_plot.append(rate_plot[0])
  527. # plt_label = '{:s} {:.2f}'.format(short_labels(label), hdi)
  528. plt_label = '{:s}'.format(short_labels(label))
  529. ax.plot(directions_plt, rate_plot, linewidth=line_widths[run_idx],
  530. label=plt_label, color=colors[run_idx], linestyle=line_styles[run_idx], zorder=zorders[run_idx])
  531. # ax.set_title('Firing Rate')
  532. # ax.plot([0.0, 0.0], [0.0, 1.05 * max_rate], color='red', alpha=0.25, linewidth=4.)
  533. # TODO: Set ticks for polar
  534. ticks = [40., 80.]
  535. ax.set_rgrids(ticks, labels=["{:.0f} Hz".format(ticklabel) if idx == len(ticks) - 1 else "" for idx, ticklabel in
  536. enumerate(ticks)], angle=60)
  537. ax.set_thetagrids([0, 90, 180, 270], labels=[])
  538. ax.xaxis.grid(linewidth=0.4)
  539. ax.yaxis.grid(linewidth=0.4)
  540. leg = ax.legend(loc="lower right", bbox_to_anchor=(1.15, -0.2), handlelength=1, fontsize="medium")
  541. leg.get_frame().set_linewidth(0.0)
  542. hdi_box_x, hdi_box_y = (0.86, -0.09)
  543. hdi_box_dy = 0.14
  544. hdi_box = ax.text(hdi_box_x, hdi_box_y + 3 * hdi_box_dy, 'HDI', fontsize='medium', transform=ax.transAxes,zorder=9.)
  545. hdi_box = ax.text(hdi_box_x, hdi_box_y + 2 * hdi_box_dy, '{:.2f}'.format(hdis[0]), fontsize='medium',transform=ax.transAxes, zorder=9.)
  546. hdi_box = ax.text(hdi_box_x, hdi_box_y + hdi_box_dy, '{:.2f}'.format(hdis[1]), fontsize='medium', transform=ax.transAxes,zorder=9.)
  547. hdi_box = ax.text(hdi_box_x, hdi_box_y, '{:.2f}'.format(hdis[2]), fontsize='medium', transform=ax.transAxes,zorder=9.)
  548. ax.axes.spines["polar"].set_visible(False)
  549. if save_figs:
  550. plt.savefig(figname)
  551. plt.close(fig)
  552. def plot_polar_plot_inhibitory(traj, plot_run_names, selected_neuron_idx, figname=FIGURE_SAVE_PATH +
  553. 'D_polar_plot_inhibitory' + FIGURE_SAVE_FORMAT):
  554. directions = np.linspace(-np.pi, np.pi, traj.input.number_of_directions, endpoint=False)
  555. directions_plt = list(directions)
  556. directions_plt.append(directions[0])
  557. height = panel_size
  558. width = panel_size
  559. fig, ax = plt.subplots(1, 1, figsize=(height, width), subplot_kw=dict(projection='polar'))
  560. # head_direction_indices = traj.results.runs[plot_run_names[0]].inh_head_direction_indices
  561. # sorted_ids = np.argsort(head_direction_indices)
  562. # plot_n_idx = sorted_ids[-75]
  563. plot_n_idx = selected_neuron_idx
  564. line_styles = ['dotted', 'solid']
  565. colors = ['b', 'lightblue']
  566. labels = ['pol. ', 'cir. ']
  567. line_widths = [1.5, 1.5]
  568. zorders = [10, 2]
  569. ax.plot([], [], color='white',label=' ')
  570. hdis = []
  571. for run_idx, run_name in enumerate(plot_run_names[:2]):
  572. # ax = axes[max_hdi_idx, run_idx]
  573. # label = traj.derived_parameters.runs[run_name].morphology.morph_label
  574. label = labels[run_idx]
  575. hdi = traj.results.runs[run_name].inh_head_direction_indices[selected_neuron_idx]
  576. hdis.append(hdi)
  577. tuning_vectors = traj.results.runs[run_name].inh_tuning_vectors
  578. rate_plot = [np.linalg.norm(v) for v in tuning_vectors[plot_n_idx]]
  579. rate_plot.append(rate_plot[0])
  580. # plt_label = '{:s} {:.2f}'.format(short_labels(label), hdi)
  581. plt_label = '{:s}'.format(short_labels(label))
  582. ax.plot(directions_plt, rate_plot, linewidth=line_widths[run_idx],
  583. label=plt_label,
  584. color=colors[run_idx], linestyle=line_styles[run_idx], zorder=zorders[run_idx])
  585. # ax.set_title('Inh. Firing Rate')
  586. # TODO: Set ticks for polar
  587. # ticks = [np.round(max_rate / 3.), np.round(max_rate * 2. / 3.), np.round(max_rate)]
  588. ticks = [40., 80., 120.]
  589. ax.set_rgrids(ticks, labels=["{:.0f} Hz".format(ticklabel) if idx == len(ticks) - 1 else "" for idx, ticklabel in
  590. enumerate(ticks)], angle=60)
  591. ax.set_thetagrids([0, 90, 180, 270], labels=[])
  592. ax.xaxis.grid(linewidth=0.4)
  593. ax.yaxis.grid(linewidth=0.4)
  594. leg = ax.legend(loc="lower right", bbox_to_anchor=(1.15, -0.15), handlelength=1, fontsize="medium")
  595. leg.get_frame().set_linewidth(0.0)
  596. hdi_box_x, hdi_box_y = (0.86, -0.04)
  597. hdi_box_dy = 0.14
  598. hdi_box = ax.text(hdi_box_x, hdi_box_y+2*hdi_box_dy, 'HDI', fontsize='medium', transform=ax.transAxes, zorder=9.)
  599. hdi_box = ax.text(hdi_box_x, hdi_box_y+hdi_box_dy, '{:.2f}'.format(hdis[0]), fontsize='medium', transform=ax.transAxes, zorder=9.)
  600. hdi_box = ax.text(hdi_box_x, hdi_box_y, '{:.2f}'.format(hdis[1]), fontsize='medium', transform=ax.transAxes, zorder=9.)
  601. ax.axes.spines["polar"].set_visible(False)
  602. if save_figs:
  603. plt.savefig(figname)
  604. plt.close(fig)
  605. def plot_hdi_over_corr_len(traj, plot_run_names):
  606. corr_len_expl = traj.f_get('correlation_length').f_get_range()
  607. seed_expl = traj.f_get('seed').f_get_range()
  608. label_expl = [traj.derived_parameters.runs[run_name].morphology.morph_label for run_name in traj.f_get_run_names()]
  609. label_range = set(label_expl)
  610. hdi_frame = pd.Series(index=[corr_len_expl, seed_expl, label_expl])
  611. hdi_frame.index.names = ["corr_len", "seed", "label"]
  612. for run_name, corr_len, seed, label in zip(plot_run_names, corr_len_expl, seed_expl, label_expl):
  613. ex_tunings = traj.results.runs[run_name].ex_tunings
  614. head_direction_indices = traj.results[run_name].head_direction_indices
  615. hdi_frame[corr_len, seed, label] = np.mean(head_direction_indices)
  616. # TODO: Standart deviation also for the population
  617. hdi_exc_n_and_seed_mean = hdi_frame.groupby(level=[0, 2]).mean()
  618. hdi_exc_n_and_seed_std_dev = hdi_frame.groupby(level=[0, 2]).std()
  619. # Ellipsoid markers
  620. rx, ry = 5., 12.
  621. # area = rx * ry * np.pi * 2.
  622. area = 1.
  623. theta = np.arange(0, 2 * np.pi + 0.01, 0.1)
  624. verts = np.column_stack([rx / area * np.cos(theta), ry / area * np.sin(theta)])
  625. style_dict = {
  626. NO_SYNAPSES: ['grey', 'dashed', '', 0],
  627. POLARIZED: ['blue', 'solid', verts, 10.],
  628. CIRCULAR: ['lightblue', 'solid', 'o', 8.]
  629. }
  630. # colors = ['blue', 'grey', 'lightblue']
  631. # linestyles = ['solid', 'dashed', 'solid']
  632. # markers = [verts, '', 'o']
  633. fig, ax = plt.subplots(1, 1)
  634. for label in label_range:
  635. hdi_mean = hdi_exc_n_and_seed_mean[:, label]
  636. hdi_std = hdi_exc_n_and_seed_std_dev[:, label]
  637. corr_len_range = hdi_mean.keys().to_numpy()
  638. col, lin, mar, mar_size = style_dict[label]
  639. ax.plot(corr_len_range, hdi_mean, label=label, marker=mar, color=col, linestyle=lin, markersize=mar_size)
  640. plt.fill_between(corr_len_range, hdi_mean - hdi_std,
  641. hdi_mean + hdi_std, alpha=0.4, color=col)
  642. ax.set_xlabel('Correlation length')
  643. ax.set_ylabel('Head Direction Index')
  644. ax.axvline(206.9, color='k', linewidth=0.5)
  645. ax.set_ylim(0.0, 1.0)
  646. ax.set_xlim(0.0, 400.)
  647. ax.legend()
  648. if save_figs:
  649. plt.savefig(FIGURE_SAVE_PATH + 'hdi_over_corr_len_scaled' + FIGURE_SAVE_FORMAT, dpi=200)
  650. plt.close(fig)
  651. def plot_hdi_histogram_excitatory(traj, plot_run_names):
  652. labels = []
  653. hdis = []
  654. colors = ['black', 'red', 'green']
  655. for run_idx, run_name in enumerate(plot_run_names):
  656. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  657. labels.append(label)
  658. head_direction_indices = traj.results.runs[run_name].head_direction_indices
  659. hdis.append(head_direction_indices)
  660. fig, ax = plt.subplots(1, 1, figsize=(6, 3))
  661. ax.hist(hdis, color=colors, label=labels, bins=30)
  662. for hdi, color in zip(hdis, colors):
  663. mean_hdi = np.mean(hdi)
  664. ax.axvline(mean_hdi, 0, 1, color=color, linestyle='--')
  665. ax.set_xlabel("HDI")
  666. ax.legend()
  667. fig.tight_layout()
  668. if save_figs:
  669. plt.savefig(FIGURE_SAVE_PATH + 'hdi_histogram_excitatory' + FIGURE_SAVE_FORMAT, dpi=200)
  670. plt.close(fig)
  671. def plot_hdi_violin_excitatory(traj, plot_run_names):
  672. labels = []
  673. hdis = []
  674. colors = ['black', 'red', 'green']
  675. no_conn_hdi = 0.
  676. for run_idx, run_name in enumerate(plot_run_names):
  677. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  678. head_direction_indices = traj.results.runs[run_name].head_direction_indices
  679. if label == NO_SYNAPSES:
  680. no_conn_hdi = np.mean(head_direction_indices)
  681. else:
  682. labels.append(label)
  683. hdis.append(sorted(head_direction_indices))
  684. fig, ax = plt.subplots(1, 1, figsize=(6, 3))
  685. # hdis = np.array(hdis)
  686. viol_plt = ax.violinplot(hdis, showmeans=True, showextrema=False)
  687. viol_plt['cmeans'].set_color('black')
  688. for pc in viol_plt['bodies']:
  689. pc.set_facecolor('red')
  690. pc.set_edgecolor('black')
  691. pc.set_alpha(0.7)
  692. ax.axhline(no_conn_hdi, color='black', linestyle='--')
  693. ax.annotate(NO_SYNAPSES, xy=(0.45, 0.48), xycoords='axes fraction')
  694. ax.set_xticks(np.arange(1, len(labels) + 1))
  695. ax.set_xticklabels(labels)
  696. ax.set_ylabel('HDI')
  697. fig.tight_layout()
  698. if save_figs:
  699. plt.savefig(FIGURE_SAVE_PATH + 'hdi_violin_excitatory' + FIGURE_SAVE_FORMAT, dpi=200)
  700. plt.close(fig)
  701. def plot_hdi_violin_inhibitory(traj, plot_run_names):
  702. labels = []
  703. hdis = []
  704. colors = ['black', 'red']
  705. for run_idx, run_name in enumerate(plot_run_names):
  706. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  707. if label != NO_SYNAPSES:
  708. labels.append(label)
  709. head_direction_indices = traj.results.runs[run_name].inh_head_direction_indices
  710. hdis.append(sorted(head_direction_indices))
  711. fig, ax = plt.subplots(1, 1, figsize=(6, 3))
  712. viol_plt = ax.violinplot(hdis, showmeans=True, showextrema=False)
  713. viol_plt['cmeans'].set_color('black')
  714. for pc in viol_plt['bodies']:
  715. pc.set_facecolor('blue')
  716. pc.set_edgecolor('black')
  717. pc.set_alpha(0.7)
  718. ax.set_xticks(np.arange(1, len(labels) + 1))
  719. ax.set_xticklabels(labels)
  720. ax.set_ylabel('HDI')
  721. fig.tight_layout()
  722. if save_figs:
  723. plt.savefig(FIGURE_SAVE_PATH + 'hdi_violin_inhibitory' + FIGURE_SAVE_FORMAT, dpi=200)
  724. plt.close(fig)
  725. def plot_hdi_violin_combined(traj, plot_run_names):
  726. labels = []
  727. inh_hdis = []
  728. exc_hdis = []
  729. no_conn_hdi = 0.
  730. colors = ['black', 'red']
  731. for run_idx, run_name in enumerate(plot_run_names):
  732. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  733. if label != NO_SYNAPSES:
  734. labels.append(label)
  735. inh_head_direction_indices = traj.results.runs[run_name].inh_head_direction_indices
  736. inh_hdis.append(sorted(inh_head_direction_indices))
  737. exc_head_direction_indices = traj.results.runs[run_name].head_direction_indices
  738. exc_hdis.append(sorted(exc_head_direction_indices))
  739. else:
  740. exc_head_direction_indices = traj.results.runs[run_name].head_direction_indices
  741. no_conn_hdi = np.mean(exc_head_direction_indices)
  742. fig, ax = plt.subplots(1, 1, figsize=(6, 3))
  743. inh_viol_plt = ax.violinplot(inh_hdis, showmeans=True, showextrema=False)
  744. # viol_plt['cmeans'].set_color('black')
  745. #
  746. # for pc in viol_plt['bodies']:
  747. # pc.set_facecolor('blue')
  748. # pc.set_edgecolor('black')
  749. # pc.set_alpha(0.7)
  750. for b in inh_viol_plt['bodies']:
  751. m = np.mean(b.get_paths()[0].vertices[:, 0])
  752. b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], m, np.inf)
  753. b.set_color('b')
  754. exc_viol_plt = ax.violinplot(exc_hdis, showmeans=True, showextrema=False)
  755. for b in exc_viol_plt['bodies']:
  756. m = np.mean(b.get_paths()[0].vertices[:, 0])
  757. b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], -np.inf, m)
  758. b.set_color('r')
  759. ax.axhline(no_conn_hdi, color='black', linestyle='--')
  760. ax.annotate(NO_SYNAPSES, xy=(0.45, 0.48), xycoords='axes fraction')
  761. ax.set_xticks(np.arange(1, len(labels) + 1))
  762. ax.set_xticklabels(labels)
  763. ax.set_ylabel('HDI')
  764. fig.tight_layout()
  765. if save_figs:
  766. plt.savefig(FIGURE_SAVE_PATH + 'hdi_violin_combined.svg', dpi=200)
  767. plt.close(fig)
  768. def plot_hdi_violin_combined_and_overlayed(traj, plot_run_names, ex_polar_plot_id, in_polar_plot_id):
  769. labels = []
  770. inh_hdis = []
  771. exc_hdis = []
  772. no_conn_hdi = 0.
  773. in_polar_plot_hdi = []
  774. ex_polar_plot_hdi = []
  775. colors = ['black', 'red']
  776. for run_idx, run_name in enumerate(plot_run_names):
  777. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  778. if label != NO_SYNAPSES:
  779. labels.append(label)
  780. inh_head_direction_indices = traj.results.runs[run_name].inh_head_direction_indices
  781. inh_hdis.append(sorted(inh_head_direction_indices))
  782. in_polar_plot_hdi.append(inh_head_direction_indices[in_polar_plot_id])
  783. exc_head_direction_indices = traj.results.runs[run_name].head_direction_indices
  784. exc_hdis.append(sorted(exc_head_direction_indices))
  785. ex_polar_plot_hdi.append(exc_head_direction_indices[ex_polar_plot_id])
  786. else:
  787. exc_head_direction_indices = traj.results.runs[run_name].head_direction_indices
  788. no_conn_hdi = np.mean(exc_head_direction_indices)
  789. ex_polar_plot_hdi.append(exc_head_direction_indices[ex_polar_plot_id])
  790. fig, ax = plt.subplots(1, 1, figsize=(3.5, 4.5))
  791. inh_ell_viol_plt = ax.violinplot(inh_hdis[0], showmeans=True, showextrema=False)
  792. for b in inh_ell_viol_plt['bodies']:
  793. m = np.mean(b.get_paths()[0].vertices[:, 0])
  794. b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], m, np.inf)
  795. b.set_color('b')
  796. mean_line = inh_ell_viol_plt['cmeans']
  797. mean_line.set_color('b')
  798. mean_line.get_paths()[0].vertices[:, 0] = np.clip(mean_line.get_paths()[0].vertices[:, 0], m, np.inf)
  799. exc_ell_viol_plt = ax.violinplot(exc_hdis[0], showmeans=True, showextrema=False)
  800. for b in exc_ell_viol_plt['bodies']:
  801. m = np.mean(b.get_paths()[0].vertices[:, 0])
  802. b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], m, np.inf)
  803. b.set_color('r')
  804. mean_line = exc_ell_viol_plt['cmeans']
  805. mean_line.set_color('r')
  806. mean_line.get_paths()[0].vertices[:, 0] = np.clip(mean_line.get_paths()[0].vertices[:, 0], m, np.inf)
  807. inh_cir_viol_plt = ax.violinplot(inh_hdis[1], showmeans=True, showextrema=False)
  808. for b in inh_cir_viol_plt['bodies']:
  809. m = np.mean(b.get_paths()[0].vertices[:, 0])
  810. b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], -np.inf, m)
  811. b.set_color('b')
  812. mean_line = inh_cir_viol_plt['cmeans']
  813. mean_line.set_color('b')
  814. mean_line.get_paths()[0].vertices[:, 0] = np.clip(mean_line.get_paths()[0].vertices[:, 0], -np.inf, m)
  815. exc_cir_viol_plt = ax.violinplot(exc_hdis[1], showmeans=True, showextrema=False)
  816. for b in exc_cir_viol_plt['bodies']:
  817. m = np.mean(b.get_paths()[0].vertices[:, 0])
  818. b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], -np.inf, m)
  819. b.set_color('r')
  820. mean_line = exc_cir_viol_plt['cmeans']
  821. mean_line.set_color('r')
  822. mean_line.get_paths()[0].vertices[:, 0] = np.clip(mean_line.get_paths()[0].vertices[:, 0], -np.inf, m)
  823. ax.axhline(no_conn_hdi, 0.5, 1., color='black', linestyle='--')
  824. ax.axvline(1.0, color='k')
  825. ax.annotate(NO_SYNAPSES, xy=(0.75, 0.415), xycoords='axes fraction')
  826. ax.set_xlim(0.5, 1.5)
  827. ax.set_ylim(0.0, 1.0)
  828. ax.set_xticks([0.75, 1.25])
  829. ax.set_xticklabels([CIRCULAR, POLARIZED])
  830. ax.set_ylabel('HDI')
  831. fig.tight_layout()
  832. if save_figs:
  833. plt.savefig(FIGURE_SAVE_PATH + 'hdi_violin_combined_and_overlayed.svg', dpi=200)
  834. plt.close(fig)
  835. return ex_polar_plot_hdi, in_polar_plot_hdi
  836. def plot_hdi_histogram_combined_and_overlayed(traj, plot_run_names, ex_polar_plot_id, in_polar_plot_id, cut_off_dist):
  837. labels = []
  838. inh_hdis = []
  839. exc_hdis = []
  840. no_conn_hdi = 0.
  841. for run_idx, run_name in enumerate(plot_run_names):
  842. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  843. if label != NO_SYNAPSES:
  844. labels.append(normal_labels(label))
  845. inh_head_direction_indices = traj.results.runs[run_name].inh_head_direction_indices
  846. inh_axonal_cloud = traj.results.runs[run_name].inhibitory_axonal_cloud_array
  847. inh_cut_off_ids = (inh_axonal_cloud[:, 0] >= cut_off_dist) & \
  848. (inh_axonal_cloud[:, 0] <= traj.parameters.map.sheet_size - cut_off_dist) & \
  849. (inh_axonal_cloud[:, 1] >= cut_off_dist) & \
  850. (inh_axonal_cloud[:, 1] <= traj.parameters.map.sheet_size - cut_off_dist)
  851. # print(inh_positions)
  852. inh_hdis.append(sorted(inh_head_direction_indices[inh_cut_off_ids]))
  853. exc_head_direction_indices = traj.results.runs[run_name].head_direction_indices
  854. ex_positions = traj.results.runs[run_name].ex_positions
  855. # print(ex_positions)
  856. exc_cut_off_ids = (ex_positions[:, 0] >= cut_off_dist) & \
  857. (ex_positions[:, 0] <= traj.parameters.map.sheet_size - cut_off_dist) & \
  858. (ex_positions[:, 1] >= cut_off_dist) & \
  859. (ex_positions[:, 1] <= traj.parameters.map.sheet_size - cut_off_dist)
  860. exc_hdis.append(sorted(exc_head_direction_indices[exc_cut_off_ids]))
  861. else:
  862. exc_head_direction_indices = traj.results.runs[run_name].head_direction_indices
  863. no_conn_hdi = np.mean(exc_head_direction_indices)
  864. # Look for a representative excitatory neuron
  865. hdi_mean_dict = {}
  866. excitatory_hdi_means = [np.mean(hdis) for hdis in exc_hdis]
  867. hdi_mean_dict["polar_exc"] = excitatory_hdi_means[0]
  868. hdi_mean_dict["circular_exc"] = excitatory_hdi_means[1]
  869. inhibitory_hdi_means = [np.mean(hdis) for hdis in inh_hdis]
  870. hdi_mean_dict["polar_inh"] = inhibitory_hdi_means[0]
  871. hdi_mean_dict["circular_inh"] = inhibitory_hdi_means[1]
  872. # # fig = plt.figure(figsize=(3.5, 3.5))
  873. # # gs1 = gridspec.GridSpec(1, 2)
  874. #
  875. # fig = plt.figure(constrained_layout=True)
  876. # gs = gridspec.GridSpec(ncols=1, nrows=2, hspace=0.0, wspace=0.0, figure=fig)
  877. # # gs.update(wspace=0.0, hspace=0.0) # set the spacing between axes.
  878. # # f2_ax1 = fig.add_subplot(gs[0, 0])
  879. # # f2_ax2 = fig.add_subplot(gs[0, 1])
  880. # print(gs.get_subplot_params())
  881. width = 2 * panel_size
  882. height = 1.2 * panel_size
  883. fig, axes = plt.subplots(2, 1, figsize=(width, height))
  884. plt.subplots_adjust(wspace=0, hspace=0.1)
  885. bins = np.linspace(0.0, 1.0, 21, endpoint=True)
  886. max_density = 0
  887. for i in range(2):
  888. # i = i + 1 # grid spec indexes from 0
  889. # ax = fig.add_subplot(gs[i])
  890. ax = axes[i]
  891. density_e, _, _ = ax.hist(exc_hdis[i], color='r', edgecolor='r', alpha=0.3, bins=bins, density=True)
  892. density_i, _, _ = ax.hist(inh_hdis[i], color='b', edgecolor='b', alpha=0.3, bins=bins, density=True)
  893. max_density = np.max([max_density, np.max(density_e), np.max(density_i)])
  894. ax.axvline(np.mean(exc_hdis[i]), color='r')
  895. ax.axvline(np.mean(inh_hdis[i]), color='b')
  896. ax.axvline(no_conn_hdi, color='dimgrey', linestyle='--', linewidth=1.5)
  897. ax.set_ylabel(labels[i], rotation='vertical')
  898. # plt.axis('on')
  899. if i == 0:
  900. ax.set_xticklabels([])
  901. else:
  902. ax.set_xlabel('HDI')
  903. remove_frame(ax, ["top", "right", "bottom"])
  904. max_density = 1.2 * max_density
  905. fig.subplots_adjust(left=0.2, right=0.95, bottom=0.2)
  906. axes[0].annotate('% cells', (0, 1.0), xycoords='axes fraction', va="bottom", ha="right")
  907. axes[1].annotate("no ihn.\n{:.2f}".format(no_conn_hdi), xy=(no_conn_hdi, max_density),
  908. xytext=(-2, 0), xycoords="data",
  909. textcoords="offset points",
  910. va="top", ha="right", color="dimgrey")
  911. for i, ax in enumerate(axes):
  912. ax.annotate("{:.2f}".format(np.mean(exc_hdis[i])), xy=(np.mean(exc_hdis[i]), max_density),
  913. xytext=(2, 0), xycoords="data",
  914. textcoords="offset points",
  915. va="top", ha="left", color="r")
  916. # i_ha = "left" if i == 1 else "right"
  917. # i_offset = 2 if i == 1 else -2
  918. i_ha = "right"
  919. i_offset = -1
  920. ax.annotate("{:.2f}".format(np.mean(inh_hdis[i])), xy=(np.mean(inh_hdis[i]), max_density),
  921. xytext=(i_offset, 0), xycoords="data",
  922. textcoords="offset points",
  923. va="top", ha=i_ha, color="b")
  924. for ax in axes:
  925. ax.set_ylim(0, max_density)
  926. # plt.annotate('probability density', (-0.2,1.5), xycoords='axes fraction', rotation=90, fontsize=18)
  927. if save_figs:
  928. plt.savefig(FIGURE_SAVE_PATH + 'E_hdi_histogram_combined_and_overlayed_cutoff_{}um' + FIGURE_SAVE_FORMAT.format(cut_off_dist))
  929. plt.close(fig)
  930. return hdi_mean_dict
  931. def get_neurons_with_given_hdi(polar_hdi, circular_hdi, max_number_of_suggestions, plot_run_names, traj, type):
  932. polar_run_name = plot_run_names[0]
  933. circular_run_name = plot_run_names[1]
  934. polar_ex_hdis = traj.results.runs[polar_run_name].head_direction_indices if type == "ex" else traj.results.runs[
  935. polar_run_name].inh_head_direction_indices
  936. circular_ex_hdis = traj.results.runs[circular_run_name].head_direction_indices if type == "ex" else \
  937. traj.results.runs[
  938. polar_run_name].inh_head_direction_indices
  939. neuron_indices = get_indices_of_closest_values(polar_ex_hdis, polar_hdi,
  940. circular_ex_hdis,
  941. circular_hdi, 0.1 * np.abs(
  942. polar_hdi - circular_hdi), max_number_of_suggestions)
  943. return neuron_indices
  944. def get_indices_of_closest_values(first_list, first_value, second_list, second_value, absolute_tolerance_list_one,
  945. number_of_indices):
  946. is_close_in_list_one = np.abs(first_list - first_value) < absolute_tolerance_list_one
  947. indices_close_in_list_one = np.where(is_close_in_list_one)[0]
  948. indices_closest_in_list_two = indices_close_in_list_one[np.argpartition(np.abs(second_list[
  949. indices_close_in_list_one] - second_value),
  950. number_of_indices)]
  951. return indices_closest_in_list_two[:number_of_indices]
  952. def plot_hdi_histogram_inhibitory(traj, plot_run_names, in_polar_plot_id):
  953. labels = []
  954. hdis = []
  955. colors = ['black', 'red']
  956. for run_idx, run_name in enumerate(plot_run_names):
  957. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  958. if label != NO_SYNAPSES:
  959. labels.append(label)
  960. head_direction_indices = traj.results.runs[run_name].inh_head_direction_indices
  961. print('inh {}: {}'.format(label, head_direction_indices[in_polar_plot_id]))
  962. hdis.append(head_direction_indices)
  963. fig, ax = plt.subplots(1, 1, figsize=(6, 3))
  964. ax.hist(hdis, color=colors, label=labels, bins=30)
  965. for hdi, color in zip(hdis, colors):
  966. mean_hdi = np.mean(hdi)
  967. ax.axvline(mean_hdi, 0, 1, color=color, linestyle='--')
  968. ax.set_xlabel("HDI")
  969. ax.legend()
  970. fig.tight_layout()
  971. if save_figs:
  972. plt.savefig(FIGURE_SAVE_PATH + 'hdi_histogram_inhibitory' + FIGURE_SAVE_FORMAT, dpi=200)
  973. plt.close(fig)
  974. def filter_run_names_by_par_dict(traj, par_dict):
  975. run_name_list = []
  976. for run_idx, run_name in enumerate(traj.f_get_run_names()):
  977. traj.f_set_crun(run_name)
  978. paramters_equal = True
  979. for key, val in par_dict.items():
  980. if (traj.par[key] != val):
  981. paramters_equal = False
  982. if paramters_equal:
  983. run_name_list.append(run_name)
  984. traj.f_restore_default()
  985. return run_name_list
  986. def plot_exc_and_inh_hdi_over_simplex_grid_scale(traj, plot_run_names, cut_off_dist):
  987. corr_len_expl = traj.f_get('correlation_length').f_get_range()
  988. seed_expl = traj.f_get('seed').f_get_range()
  989. label_expl = [traj.derived_parameters.runs[run_name].morphology.morph_label for run_name in traj.f_get_run_names()]
  990. label_range = set(label_expl)
  991. exc_hdi_frame = pd.Series(index=[corr_len_expl, seed_expl, label_expl])
  992. exc_hdi_frame.index.names = ["corr_len", "seed", "label"]
  993. inh_hdi_frame = pd.Series(index=[corr_len_expl, seed_expl, label_expl])
  994. inh_hdi_frame.index.names = ["corr_len", "seed", "label"]
  995. for run_name, corr_len, seed, label in zip(plot_run_names, corr_len_expl, seed_expl, label_expl):
  996. ex_tunings = traj.results.runs[run_name].ex_tunings
  997. inh_hdis = []
  998. exc_hdis = []
  999. inh_head_direction_indices = traj.results.runs[run_name].inh_head_direction_indices
  1000. inh_axonal_cloud = traj.results.runs[run_name].inhibitory_axonal_cloud_array
  1001. inh_cut_off_ids = (inh_axonal_cloud[:, 0] >= cut_off_dist) & \
  1002. (inh_axonal_cloud[:, 0] <= traj.parameters.map.sheet_size - cut_off_dist) & \
  1003. (inh_axonal_cloud[:, 1] >= cut_off_dist) & \
  1004. (inh_axonal_cloud[:, 1] <= traj.parameters.map.sheet_size - cut_off_dist)
  1005. inh_hdis.append(sorted(inh_head_direction_indices[inh_cut_off_ids]))
  1006. exc_head_direction_indices = traj.results.runs[run_name].head_direction_indices
  1007. ex_positions = traj.results.runs[run_name].ex_positions
  1008. exc_cut_off_ids = (ex_positions[:, 0] >= cut_off_dist) & \
  1009. (ex_positions[:, 0] <= traj.parameters.map.sheet_size - cut_off_dist) & \
  1010. (ex_positions[:, 1] >= cut_off_dist) & \
  1011. (ex_positions[:, 1] <= traj.parameters.map.sheet_size - cut_off_dist)
  1012. exc_hdis.append(sorted(exc_head_direction_indices[exc_cut_off_ids]))
  1013. exc_hdi_frame[corr_len, seed, label] = np.mean(exc_hdis)
  1014. inh_hdi_frame[corr_len, seed, label] = np.mean(inh_hdis)
  1015. # TODO: Standard deviation also for the population
  1016. exc_hdi_n_and_seed_mean = exc_hdi_frame.groupby(level=[0, 2]).mean()
  1017. exc_hdi_n_and_seed_std_dev = exc_hdi_frame.groupby(level=[0, 2]).std()
  1018. inh_hdi_n_and_seed_mean = inh_hdi_frame.groupby(level=[0, 2]).mean()
  1019. inh_hdi_n_and_seed_std_dev = inh_hdi_frame.groupby(level=[0, 2]).std()
  1020. markersize = 4.
  1021. exc_style_dict = {
  1022. NO_SYNAPSES: ['dimgrey', 'dashed', '', 0],
  1023. POLARIZED: ['red', 'solid', '^', markersize],
  1024. CIRCULAR: ['lightsalmon', 'solid', '^', markersize]
  1025. }
  1026. inh_style_dict = {
  1027. NO_SYNAPSES: ['dimgrey', 'dashed', '', 0],
  1028. POLARIZED: ['blue', 'solid', 'o', markersize],
  1029. CIRCULAR: ['lightblue', 'solid', 'o', markersize]
  1030. }
  1031. # colors = ['blue', 'grey', 'lightblue']
  1032. # linestyles = ['solid', 'dashed', 'solid']
  1033. # markers = [verts, '', 'o']
  1034. # corr_len_fit_dict = correlation_length_fit_dict(traj, load=True)
  1035. width = 2 * panel_size
  1036. height = 1.2 * panel_size
  1037. fig, ax = plt.subplots(1, 1, figsize=(width, height))
  1038. for label in sorted(label_range, reverse=True):
  1039. if label == NO_SYNAPSES:
  1040. no_conn_hdi = exc_hdi_n_and_seed_mean[1, label]
  1041. ax.axhline(no_conn_hdi, color='grey', linestyle='--')
  1042. ax.annotate(short_labels(label), xy=(1.0, no_conn_hdi), xytext=(0, -2), xycoords='axes fraction',
  1043. textcoords="offset points",
  1044. va="top", \
  1045. ha="right",
  1046. color="dimgrey")
  1047. continue
  1048. exc_hdi_mean = exc_hdi_n_and_seed_mean[:, label]
  1049. exc_hdi_std = exc_hdi_n_and_seed_std_dev[:, label]
  1050. inh_hdi_mean = inh_hdi_n_and_seed_mean[:, label]
  1051. inh_hdi_std = inh_hdi_n_and_seed_std_dev[:, label]
  1052. corr_len_range = exc_hdi_mean.keys().to_numpy()
  1053. print(label)
  1054. for corr_len, ex_hdi, in_hdi in zip(corr_len_range, exc_hdi_mean, inh_hdi_mean):
  1055. print("length: {:.2f} um, ex hdi: {:.2f} and in hdi {:.2f}".format(corr_len, ex_hdi, in_hdi))
  1056. exc_col, exc_lin, exc_mar, exc_mar_size = exc_style_dict[label]
  1057. inh_col, inh_lin, inh_mar, inh_mar_size = inh_style_dict[label]
  1058. simplex_grid_scale = corr_len_range * np.sqrt(2)
  1059. ax.plot(simplex_grid_scale, exc_hdi_mean, label='exc., ' + label, marker=exc_mar, color=exc_col, linestyle=exc_lin,
  1060. markersize=exc_mar_size, alpha=0.5)
  1061. plt.fill_between(simplex_grid_scale, exc_hdi_mean - exc_hdi_std,
  1062. exc_hdi_mean + exc_hdi_std, alpha=0.3, color=exc_col)
  1063. ax.plot(simplex_grid_scale, inh_hdi_mean, label='inh., ' + label, marker=inh_mar, color=inh_col, linestyle=inh_lin,
  1064. markersize=inh_mar_size, alpha=0.5)
  1065. plt.fill_between(simplex_grid_scale, inh_hdi_mean - inh_hdi_std,
  1066. inh_hdi_mean + inh_hdi_std, alpha=0.3, color=inh_col)
  1067. ax.set_xlabel('simplex grid scale')
  1068. ax.set_ylabel('HDI')
  1069. ax.axvline(get_closest_correlation_length(traj, 200.0) * np.sqrt(2), color='k', linewidth=0.5, zorder=0)
  1070. ax.set_ylim(0.0, 1.0)
  1071. # ax.set_xlim(0.0, np.max(corr_len_range))
  1072. remove_frame(ax, ["right", "top"])
  1073. tablelegend(ax, ncol=2, bbox_to_anchor=(1.1, 1.1), loc="upper right",
  1074. row_labels=None,
  1075. col_labels=[short_labels(label) for label in sorted(label_range - {"no conn"}, reverse=True)],
  1076. title_label='', borderaxespad=0, handlelength=2, edgecolor='white')
  1077. fig.subplots_adjust(bottom=0.2, left=0.2)
  1078. # plt.legend()
  1079. if save_figs:
  1080. plt.savefig(FIGURE_SAVE_PATH + 'F_hdi_over_grid_scale' + FIGURE_SAVE_FORMAT)
  1081. plt.close(fig)
  1082. def plot_exc_and_inh_hdi_over_fit_corr_len(traj, plot_run_names, cut_off_dist):
  1083. corr_len_expl = traj.f_get('correlation_length').f_get_range()
  1084. seed_expl = traj.f_get('seed').f_get_range()
  1085. label_expl = [traj.derived_parameters.runs[run_name].morphology.morph_label for run_name in traj.f_get_run_names()]
  1086. label_range = set(label_expl)
  1087. exc_hdi_frame = pd.Series(index=[corr_len_expl, seed_expl, label_expl])
  1088. exc_hdi_frame.index.names = ["corr_len", "seed", "label"]
  1089. inh_hdi_frame = pd.Series(index=[corr_len_expl, seed_expl, label_expl])
  1090. inh_hdi_frame.index.names = ["corr_len", "seed", "label"]
  1091. for run_name, corr_len, seed, label in zip(plot_run_names, corr_len_expl, seed_expl, label_expl):
  1092. ex_tunings = traj.results.runs[run_name].ex_tunings
  1093. inh_hdis = []
  1094. exc_hdis = []
  1095. inh_head_direction_indices = traj.results.runs[run_name].inh_head_direction_indices
  1096. inh_axonal_cloud = traj.results.runs[run_name].inhibitory_axonal_cloud_array
  1097. inh_cut_off_ids = (inh_axonal_cloud[:, 0] >= cut_off_dist) & \
  1098. (inh_axonal_cloud[:, 0] <= traj.parameters.map.sheet_size - cut_off_dist) & \
  1099. (inh_axonal_cloud[:, 1] >= cut_off_dist) & \
  1100. (inh_axonal_cloud[:, 1] <= traj.parameters.map.sheet_size - cut_off_dist)
  1101. inh_hdis.append(sorted(inh_head_direction_indices[inh_cut_off_ids]))
  1102. exc_head_direction_indices = traj.results.runs[run_name].head_direction_indices
  1103. ex_positions = traj.results.runs[run_name].ex_positions
  1104. exc_cut_off_ids = (ex_positions[:, 0] >= cut_off_dist) & \
  1105. (ex_positions[:, 0] <= traj.parameters.map.sheet_size - cut_off_dist) & \
  1106. (ex_positions[:, 1] >= cut_off_dist) & \
  1107. (ex_positions[:, 1] <= traj.parameters.map.sheet_size - cut_off_dist)
  1108. exc_hdis.append(sorted(exc_head_direction_indices[exc_cut_off_ids]))
  1109. exc_hdi_frame[corr_len, seed, label] = np.mean(exc_hdis)
  1110. inh_hdi_frame[corr_len, seed, label] = np.mean(inh_hdis)
  1111. # TODO: Standard deviation also for the population
  1112. exc_hdi_n_and_seed_mean = exc_hdi_frame.groupby(level=[0, 2]).mean()
  1113. exc_hdi_n_and_seed_std_dev = exc_hdi_frame.groupby(level=[0, 2]).std()
  1114. inh_hdi_n_and_seed_mean = inh_hdi_frame.groupby(level=[0, 2]).mean()
  1115. inh_hdi_n_and_seed_std_dev = inh_hdi_frame.groupby(level=[0, 2]).std()
  1116. markersize = 4.
  1117. exc_style_dict = {
  1118. NO_SYNAPSES: ['dimgrey', 'dashed', '', 0],
  1119. POLARIZED: ['red', 'solid', '^', markersize],
  1120. CIRCULAR: ['lightsalmon', 'solid', '^', markersize]
  1121. }
  1122. inh_style_dict = {
  1123. NO_SYNAPSES: ['dimgrey', 'dashed', '', 0],
  1124. POLARIZED: ['blue', 'solid', 'o', markersize],
  1125. CIRCULAR: ['lightblue', 'solid', 'o', markersize]
  1126. }
  1127. # colors = ['blue', 'grey', 'lightblue']
  1128. # linestyles = ['solid', 'dashed', 'solid']
  1129. # markers = [verts, '', 'o']
  1130. corr_len_fit_dict = correlation_length_fit_dict(traj, map_type='perlin', load=True)
  1131. width = 2 * panel_size
  1132. height = 1.2 * panel_size
  1133. fig, ax = plt.subplots(1, 1, figsize=(width, height))
  1134. for label in sorted(label_range, reverse=True):
  1135. if label == NO_SYNAPSES:
  1136. no_conn_hdi = exc_hdi_n_and_seed_mean[1, label]
  1137. ax.axhline(no_conn_hdi, color='grey', linestyle='--')
  1138. ax.annotate(short_labels(label), xy=(1.0, no_conn_hdi), xytext=(0, -2), xycoords='axes fraction',
  1139. textcoords="offset points",
  1140. va="top", \
  1141. ha="right",
  1142. color="dimgrey")
  1143. continue
  1144. exc_hdi_mean = exc_hdi_n_and_seed_mean[:, label]
  1145. exc_hdi_std = exc_hdi_n_and_seed_std_dev[:, label]
  1146. inh_hdi_mean = inh_hdi_n_and_seed_mean[:, label]
  1147. inh_hdi_std = inh_hdi_n_and_seed_std_dev[:, label]
  1148. corr_len_range = exc_hdi_mean.keys().to_numpy()
  1149. print(label)
  1150. for corr_len, ex_hdi, in_hdi in zip(corr_len_range, exc_hdi_mean, inh_hdi_mean):
  1151. print("length: {:.2f} um, ex hdi: {:.2f} and in hdi {:.2f}".format(corr_len, ex_hdi, in_hdi))
  1152. exc_col, exc_lin, exc_mar, exc_mar_size = exc_style_dict[label]
  1153. inh_col, inh_lin, inh_mar, inh_mar_size = inh_style_dict[label]
  1154. fit_corr_len = [corr_len_fit_dict[corr_len] for corr_len in corr_len_range]
  1155. ax.plot(fit_corr_len, exc_hdi_mean, label='exc., ' + label, marker=exc_mar, color=exc_col, linestyle=exc_lin,
  1156. markersize=exc_mar_size, alpha=0.5)
  1157. plt.fill_between(fit_corr_len, exc_hdi_mean - exc_hdi_std,
  1158. exc_hdi_mean + exc_hdi_std, alpha=0.3, color=exc_col)
  1159. ax.plot(fit_corr_len, inh_hdi_mean, label='inh., ' + label, marker=inh_mar, color=inh_col, linestyle=inh_lin,
  1160. markersize=inh_mar_size, alpha=0.5)
  1161. plt.fill_between(fit_corr_len, inh_hdi_mean - inh_hdi_std,
  1162. inh_hdi_mean + inh_hdi_std, alpha=0.3, color=inh_col)
  1163. ax.set_xlabel('correlation length (um)')
  1164. ax.set_ylabel('HDI')
  1165. print('The used correlation length is ', corr_len_fit_dict[get_closest_correlation_length(traj, 200.0)])
  1166. ax.axvline(corr_len_fit_dict[get_closest_correlation_length(traj, 200.0)], color='k', linewidth=0.5, zorder=0)
  1167. ax.set_ylim(0.0, 1.0)
  1168. # ax.set_xlim(0.0, np.max(corr_len_range))
  1169. remove_frame(ax, ["right", "top"])
  1170. tablelegend(ax, ncol=2, bbox_to_anchor=(1.1, 1.1), loc="upper right",
  1171. row_labels=None,
  1172. col_labels=[short_labels(label) for label in sorted(label_range - {"no conn"}, reverse=True)],
  1173. title_label='', borderaxespad=0, handlelength=2, edgecolor='white')
  1174. fig.subplots_adjust(bottom=0.2, left=0.2)
  1175. # plt.legend()
  1176. if save_figs:
  1177. plt.savefig(FIGURE_SAVE_PATH + 'F_hdi_over_corr_len_scaled' + FIGURE_SAVE_FORMAT)
  1178. plt.close(fig)
  1179. def plot_in_degree_map(traj, plot_run_names):
  1180. n_ex = int(np.sqrt(traj.N_E))
  1181. max_degree = 0
  1182. for run_name in plot_run_names:
  1183. ie_adjacency = traj.results.runs[run_name].ie_adjacency
  1184. exc_degree = np.sum(ie_adjacency, axis=0)
  1185. run_max_degree = np.max(exc_degree)
  1186. if run_max_degree > max_degree:
  1187. max_degree = run_max_degree
  1188. fig, axes = plt.subplots(1, 2, figsize=(9., 4.5))
  1189. for ax, run_name in zip(axes, plot_run_names[:-1]):
  1190. traj.f_set_crun(run_name)
  1191. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  1192. X, Y = get_position_mesh(traj.results.runs[run_name].ex_positions)
  1193. number_of_excitatory_neurons_per_row = int(np.sqrt(traj.N_E))
  1194. ie_adjacency = traj.results.runs[run_name].ie_adjacency
  1195. exc_degree = np.sum(ie_adjacency, axis=0)
  1196. c = ax.pcolor(X, Y, np.reshape(exc_degree, (number_of_excitatory_neurons_per_row,
  1197. number_of_excitatory_neurons_per_row)), vmin=0, vmax=max_degree,
  1198. cmap='hot')
  1199. ax.set_title(label)
  1200. fig.colorbar(c, ax=ax, label="in/out-degree")
  1201. fig.suptitle('in/out-degree', fontsize=16)
  1202. traj.f_restore_default()
  1203. if save_figs:
  1204. plt.savefig(FIGURE_SAVE_PATH + 'in_degree_map' + FIGURE_SAVE_FORMAT, dpi=200)
  1205. plt.close(fig)
  1206. def plot_spatial_hdi_map(traj, plot_run_names):
  1207. max_val = 0
  1208. for run_name in plot_run_names:
  1209. hdis = traj.results.runs[run_name].head_direction_indices
  1210. run_max_val = np.max(hdis)
  1211. if run_max_val > max_val:
  1212. max_val = run_max_val
  1213. fig, axes = plt.subplots(1, 3, figsize=(13.5, 4.5))
  1214. for ax, run_name in zip(axes, plot_run_names):
  1215. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  1216. positions = traj.results.runs[run_name].ex_positions
  1217. head_direction_indices = traj.results[run_name].head_direction_indices
  1218. print('Mean {}-HDI = {}'.format(label, np.mean(head_direction_indices)))
  1219. c = plot_hdi_in_space(ax, positions, head_direction_indices, max_val)
  1220. ax.set_title(label)
  1221. fig.colorbar(c, ax=ax, label="HDI")
  1222. fig.suptitle('spatial HDI map', fontsize=16)
  1223. if save_figs:
  1224. plt.savefig(FIGURE_SAVE_PATH + 'spatial_hdi_map' + FIGURE_SAVE_FORMAT, dpi=200)
  1225. plt.close(fig)
  1226. def plot_exc_spatial_hdi_map(traj, plot_run_names):
  1227. max_val = 0
  1228. for run_name in plot_run_names:
  1229. hdis = traj.results.runs[run_name].head_direction_indices
  1230. run_max_val = np.max(hdis)
  1231. if run_max_val > max_val:
  1232. max_val = run_max_val
  1233. fig, axes = plt.subplots(1, 3, figsize=(13.5, 4.5))
  1234. for ax, run_name in zip(axes, plot_run_names):
  1235. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  1236. positions = traj.results.runs[run_name].ex_positions
  1237. head_direction_indices = traj.results[run_name].head_direction_indices
  1238. print('Mean {}-HDI = {}'.format(label, np.mean(head_direction_indices)))
  1239. c = plot_hdi_in_space(ax, positions, head_direction_indices, max_val)
  1240. ax.set_title(label)
  1241. fig.colorbar(c, ax=ax, label="HDI")
  1242. fig.suptitle('spatial exc. HDI map', fontsize=16)
  1243. if save_figs:
  1244. plt.savefig(FIGURE_SAVE_PATH + 'spatial_exc_hdi_map' + FIGURE_SAVE_FORMAT, dpi=200)
  1245. plt.close(fig)
  1246. def plot_inh_spatial_hdi_map(traj, plot_run_names):
  1247. max_val = 0
  1248. for run_name in plot_run_names:
  1249. hdis = traj.results.runs[run_name].inh_head_direction_indices
  1250. run_max_val = np.max(hdis)
  1251. if run_max_val > max_val:
  1252. max_val = run_max_val
  1253. fig, axes = plt.subplots(1, 3, figsize=(13.5, 4.5))
  1254. for ax, run_name in zip(axes, plot_run_names):
  1255. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  1256. ax_cloud = traj.results.runs[run_name].inhibitory_axonal_cloud_array
  1257. positions = [[x, y] for x, y, phi in ax_cloud]
  1258. head_direction_indices = traj.results[run_name].inh_head_direction_indices
  1259. print('Mean {}-HDI = {}'.format(label, np.mean(head_direction_indices)))
  1260. c = plot_hdi_in_space(ax, positions, head_direction_indices, max_val)
  1261. ax.set_title(label)
  1262. fig.colorbar(c, ax=ax, label="HDI")
  1263. fig.suptitle('spatial inh. HDI map', fontsize=16)
  1264. if save_figs:
  1265. plt.savefig(FIGURE_SAVE_PATH + 'spatial_inh_hdi_map' + FIGURE_SAVE_FORMAT, dpi=200)
  1266. plt.close(fig)
  1267. def get_phase_difference(total_difference):
  1268. """
  1269. Map accumulated phase difference to shortest possible difference
  1270. :param total_difference:
  1271. :return: relative_difference
  1272. """
  1273. return (total_difference + np.pi) % (2 * np.pi) - np.pi
  1274. def plot_firing_rate_similar_vs_diff_tuning(traj, plot_run_names, figsize=(9, 9)):
  1275. # The plot that Imre wanted
  1276. n_bins = traj.parameters.input.number_of_directions
  1277. fig, ax = plt.subplots(1, 1, figsize=figsize)
  1278. dir_bins = np.linspace(-np.pi, np.pi, n_bins + 1)
  1279. print(dir_bins)
  1280. plot_fr_array = []
  1281. labels = []
  1282. similarity_threshold = np.pi / 6.
  1283. directions = np.linspace(-np.pi, np.pi, traj.input.number_of_directions, endpoint=False)
  1284. for run_idx, run_name in enumerate(plot_run_names):
  1285. label = traj.derived_parameters.runs[run_name].morphology.morph_label
  1286. labels.append(short_labels(label))
  1287. fr_similar_tunings = []
  1288. fr_different_tunings = []
  1289. ex_tunings = traj.results.runs[run_name].ex_tunings
  1290. firing_rate_array = traj.results[run_name].firing_rate_array
  1291. for tuning, firing_rates in zip(ex_tunings, firing_rate_array):
  1292. for idx, dir in enumerate(directions):
  1293. if np.abs(get_phase_difference(tuning - dir)) <= similarity_threshold:
  1294. fr_similar_tunings.append(firing_rates[idx])
  1295. elif np.abs(get_phase_difference(tuning + np.pi - dir)) <= similarity_threshold:
  1296. fr_different_tunings.append(firing_rates[idx])
  1297. plot_fr_array.append([np.mean(fr_similar_tunings), np.mean(fr_different_tunings)])
  1298. x = np.arange(3) # the label locations
  1299. width = 0.35 # the width of the bars
  1300. plot_fr_array = np.array(plot_fr_array)
  1301. # rects1 = ax.bar(x - width / 2, plot_fr_array[:, 0], width,
  1302. # label=r'$\theta_{pref} \pm 30°$')
  1303. # rects2 = ax.bar(x + width / 2, plot_fr_array[:, 1], width,
  1304. # label=r'$\theta_{opp} \pm 30°$')
  1305. rects1 = ax.bar(x - width / 2, plot_fr_array[:, 0], width,
  1306. label=r'sim.')
  1307. rects2 = ax.bar(x + width / 2, plot_fr_array[:, 1], width,
  1308. label=r'diff.')
  1309. ax.set_xticks(x)
  1310. ax.set_xticklabels(labels)
  1311. ax.spines['right'].set_visible(False)
  1312. ax.spines['top'].set_visible(False)
  1313. # ax.set_title('Mean firing rate for tunings similar and different to input')
  1314. # ax.set_ylabel('Mean firing rate')
  1315. ax.annotate(r'$\overline{\mathrm{fr}}$ (Hz)', (0.05, 1.0), xycoords='axes fraction', va="bottom", ha="right")
  1316. leg = ax.legend(loc="upper left", bbox_to_anchor=(0.0, 1.2), handlelength=1, fontsize="medium")
  1317. leg.get_frame().set_linewidth(0.0)
  1318. def autolabel(rects):
  1319. """Attach a text label above each bar in *rects*, displaying its height."""
  1320. for rect in rects:
  1321. height = rect.get_height()
  1322. ax.annotate('{}'.format(np.round(height)),
  1323. xy=(rect.get_x() + rect.get_width() / 2, height),
  1324. xytext=(0, 3), # 3 points vertical offset
  1325. textcoords="offset points",
  1326. ha='center', va='bottom')
  1327. autolabel(rects1)
  1328. autolabel(rects2)
  1329. fig.tight_layout()
  1330. if save_figs:
  1331. plt.savefig(FIGURE_SAVE_PATH + 'firing_rate_similar_vs_diff_tuning' + FIGURE_SAVE_FORMAT, dpi=200)
  1332. plt.close(fig)
  1333. def get_firing_rates_along_preferred_axis(traj, run_name, neuron_idx):
  1334. firing_rates = traj.results[run_name].firing_rate_array[neuron_idx, :]
  1335. tuning = traj.results[run_name].ex_tunings[neuron_idx]
  1336. anti_tuning = tuning + np.pi if tuning + np.pi < np.pi else tuning - np.pi
  1337. tuning_idx = np.argmin(np.abs(directions - tuning))
  1338. anti_tuning_idx = np.argmin(np.abs(directions - anti_tuning))
  1339. firing_at_the_preferred_direction = firing_rates[tuning_idx]
  1340. firing_at_the_opposite_direction = firing_rates[anti_tuning_idx]
  1341. return firing_at_the_preferred_direction, firing_at_the_opposite_direction
  1342. def get_hdi(traj, run_name, neuron_idx, type):
  1343. return traj.results.runs[run_name].head_direction_indices[neuron_idx] if type=="ex" else traj.results.runs[
  1344. run_name].inh_head_direction_indices[neuron_idx]
  1345. if __name__ == "__main__":
  1346. traj = Trajectory(TRAJ_NAME, add_time=False, dynamic_imports=Brian2MonitorResult)
  1347. NO_LOADING = 0
  1348. FULL_LOAD = 2
  1349. traj.f_load(filename=os.path.join(DATA_FOLDER, TRAJ_NAME + ".hdf5"), load_parameters=FULL_LOAD,
  1350. load_results=NO_LOADING)
  1351. traj.v_auto_load = True
  1352. save_figs = True
  1353. print("# Plotting script polarized interneurons")
  1354. print()
  1355. map_length_scale = 200.0
  1356. map_seed = 1
  1357. exemplary_head_direction = 0
  1358. # corr_len_fit_dict = correlation_length_fit_dict(traj, map_type='perlin', load=True)
  1359. # plt.plot(corr_len_fit_dict.keys(),corr_len_fit_dict.values())
  1360. # plt.show()
  1361. # abbrechen
  1362. print("## Map specifications")
  1363. print("\tcorrelation length: {:.1f} um".format(map_length_scale))
  1364. print("\tmap seed: {:d}".format(map_seed))
  1365. print()
  1366. print("## Input specification")
  1367. print("\tselected head direction: {:.0f}°".format(exemplary_head_direction))
  1368. print()
  1369. print("## Selected simulations")
  1370. plot_corr_len = get_closest_correlation_length(traj, map_length_scale)
  1371. par_dict = {'seed': map_seed, 'correlation_length': plot_corr_len}
  1372. plot_run_names = filter_run_names_by_par_dict(traj, par_dict)
  1373. run_name_dict = {}
  1374. for run_name in plot_run_names:
  1375. traj.f_set_crun(run_name)
  1376. run_name_dict[traj.derived_parameters.runs[run_name].morphology.morph_label] = run_name
  1377. for network_type, run_name in run_name_dict.items():
  1378. print("{:s}: {:s}".format(network_type, run_name))
  1379. directions = get_input_head_directions(traj)
  1380. direction_idx = np.argmin(np.abs(np.array(directions) - np.deg2rad(exemplary_head_direction)))
  1381. selected_neuron_excitatory = 1052
  1382. selected_inhibitory_neuron = 28
  1383. print("## Figure specification")
  1384. print("\tpanel size: {:.2f} cm".format(panel_size * cm_per_inch))
  1385. print()
  1386. plot_colorbar(figsize=(0.8 * panel_size, 0.8 * panel_size), figname=FIGURE_SAVE_PATH + "A_i_colormap.svg")
  1387. plot_input_map(traj, run_name_dict[POLARIZED], figname="A_i_exemplary_input_map"+FIGURE_SAVE_FORMAT,
  1388. figsize=(panel_size, panel_size))
  1389. plot_example_input_maps(traj, figsize=(2 * panel_size, 2 * panel_size))
  1390. plot_axonal_clouds(traj, plot_run_names)
  1391. #
  1392. plot_firing_rate_map_excitatory(traj, direction_idx, plot_run_names, selected_neuron_excitatory)
  1393. in_max_rate = plot_firing_rate_map_inhibitory(traj, direction_idx, plot_run_names, selected_inhibitory_neuron)
  1394. #
  1395. hdi_means = plot_hdi_histogram_combined_and_overlayed(
  1396. traj, plot_run_names,
  1397. selected_neuron_excitatory,
  1398. selected_inhibitory_neuron,
  1399. cut_off_dist=100.)
  1400. #
  1401. number_of_suggestions = 20
  1402. representative_excitatory_neuron_indices = get_neurons_with_given_hdi(hdi_means["polar_exc"], hdi_means[
  1403. "circular_exc"],
  1404. number_of_suggestions, plot_run_names,
  1405. traj, "ex")
  1406. representative_inhibitory_neuron_indices = get_neurons_with_given_hdi(hdi_means["polar_inh"],
  1407. hdi_means["circular_inh"],
  1408. number_of_suggestions, plot_run_names,
  1409. traj, "in")
  1410. plot_polar_plot_excitatory(traj, plot_run_names, selected_neuron_excitatory)
  1411. for suggested_excitatory_neuron in representative_excitatory_neuron_indices:
  1412. preferred_polar, opposite_polar = get_firing_rates_along_preferred_axis(
  1413. traj, run_name_dict["ellipsoid"], suggested_excitatory_neuron)
  1414. preferred_circular, opposite_circular = get_firing_rates_along_preferred_axis(
  1415. traj, run_name_dict["circular"], suggested_excitatory_neuron)
  1416. plot_polar_plot_excitatory(traj, plot_run_names, suggested_excitatory_neuron,
  1417. figname=FIGURE_SAVE_PATH + "X_polar_plot_excitatory_neuron_signal_increase_{"
  1418. ":0>2.0f}_noise_decrease_{"
  1419. ":0>2.0f}_neuron_id_{"
  1420. ":d}.png".format(
  1421. preferred_polar-preferred_circular,
  1422. opposite_circular-opposite_polar, suggested_excitatory_neuron))
  1423. plot_polar_plot_inhibitory(traj, plot_run_names, selected_inhibitory_neuron)
  1424. for suggested_inhibitory_neuron in representative_inhibitory_neuron_indices:
  1425. polar_hdi = get_hdi(traj, run_name_dict["ellipsoid"], suggested_inhibitory_neuron, "in")
  1426. circular_hdi = get_hdi(traj, run_name_dict["circular"], suggested_inhibitory_neuron, "in")
  1427. plot_polar_plot_inhibitory(traj, plot_run_names, suggested_inhibitory_neuron,
  1428. figname=FIGURE_SAVE_PATH + "X_polar_plot_inhibitory_neuron_polarized_{"
  1429. ":.2f}_circular_{:.2f}_neuron_id_{"
  1430. ":d}.png".format(polar_hdi, circular_hdi,
  1431. suggested_inhibitory_neuron))
  1432. plot_firing_rate_similar_vs_diff_tuning(traj, plot_run_names, figsize=(1.2*panel_size, 1.2*panel_size))
  1433. plot_orientation_maps_diff_scales_with_ellipse(traj)
  1434. # plot_exc_and_inh_hdi_over_simplex_grid_scale(traj, traj.f_get_run_names(), cut_off_dist=100.)
  1435. plot_exc_and_inh_hdi_over_fit_corr_len(traj, traj.f_get_run_names(), cut_off_dist=100.)
  1436. if not save_figs:
  1437. plt.show()
  1438. traj.f_restore_default()