1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012 |
- import matplotlib.pyplot as plt
- import numpy as np
- import pandas as pd
- # from brian2.units import *
- from mpl_toolkits.axes_grid1 import make_axes_locatable
- from pypet import Trajectory
- from pypet.brian2 import Brian2MonitorResult
- from scipy.optimize import curve_fit
- from matplotlib.patches import Ellipse
- from matplotlib.patches import Polygon
- import matplotlib.legend as mlegend
- from matplotlib.patches import Rectangle
- from scripts.interneuron_placement import get_position_mesh, Pickle, get_correct_position_mesh
- from scripts.spatial_network.placement_jitter.run_entropy_maximisation_orientation_map_placement_jitter import DATA_FOLDER, TRAJ_NAME
- FIGURE_SAVE_PATH = '../../../figures/placement_jitter/'
- def tablelegend(ax, col_labels=None, row_labels=None, title_label="", *args, **kwargs):
- """
- Place a table legend on the axes.
- Creates a legend where the labels are not directly placed with the artists,
- but are used as row and column headers, looking like this:
- title_label | col_labels[1] | col_labels[2] | col_labels[3]
- -------------------------------------------------------------
- row_labels[1] |
- row_labels[2] | <artists go there>
- row_labels[3] |
- Parameters
- ----------
- ax : `matplotlib.axes.Axes`
- The artist that contains the legend table, i.e. current axes instant.
- col_labels : list of str, optional
- A list of labels to be used as column headers in the legend table.
- `len(col_labels)` needs to match `ncol`.
- row_labels : list of str, optional
- A list of labels to be used as row headers in the legend table.
- `len(row_labels)` needs to match `len(handles) // ncol`.
- title_label : str, optional
- Label for the top left corner in the legend table.
- ncol : int
- Number of columns.
- Other Parameters
- ----------------
- Refer to `matplotlib.legend.Legend` for other parameters.
- """
- #################### same as `matplotlib.axes.Axes.legend` #####################
- handles, labels, extra_args, kwargs = mlegend._parse_legend_args([ax], *args, **kwargs)
- if len(extra_args):
- raise TypeError('legend only accepts two non-keyword arguments')
- if col_labels is None and row_labels is None:
- ax.legend_ = mlegend.Legend(ax, handles, labels, **kwargs)
- ax.legend_._remove_method = ax._remove_legend
- return ax.legend_
- #################### modifications for table legend ############################
- else:
- ncol = kwargs.pop('ncol')
- handletextpad = kwargs.pop('handletextpad', 0 if col_labels is None else -2)
- title_label = [title_label]
- # blank rectangle handle
- extra = [Rectangle((0, 0), 1, 1, fc="w", fill=False, edgecolor='none', linewidth=0)]
- # empty label
- empty = [""]
- # number of rows infered from number of handles and desired number of columns
- nrow = len(handles) // ncol
- # organise the list of handles and labels for table construction
- if col_labels is None:
- assert nrow == len(row_labels), "nrow = len(handles) // ncol = %s, but should be equal to len(row_labels) = %s." % (nrow, len(row_labels))
- leg_handles = extra * nrow
- leg_labels = row_labels
- elif row_labels is None:
- assert ncol == len(col_labels), "ncol = %s, but should be equal to len(col_labels) = %s." % (ncol, len(col_labels))
- leg_handles = []
- leg_labels = []
- else:
- assert nrow == len(row_labels), "nrow = len(handles) // ncol = %s, but should be equal to len(row_labels) = %s." % (nrow, len(row_labels))
- assert ncol == len(col_labels), "ncol = %s, but should be equal to len(col_labels) = %s." % (ncol, len(col_labels))
- leg_handles = extra + extra * nrow
- leg_labels = title_label + row_labels
- for col in range(ncol):
- if col_labels is not None:
- leg_handles += extra
- leg_labels += [col_labels[col]]
- leg_handles += handles[col*nrow:(col+1)*nrow]
- leg_labels += empty * nrow
- # Create legend
- ax.legend_ = mlegend.Legend(ax, leg_handles, leg_labels, ncol=ncol+int(row_labels is not None), handletextpad=handletextpad, **kwargs)
- ax.legend_._remove_method = ax._remove_legend
- return ax.legend_
- def get_closest_correlation_length(traj, correlation_length):
- available_lengths = sorted(list(set(traj.f_get("correlation_length").f_get_range())))
- closest_length = available_lengths[np.argmin(np.abs(np.array(available_lengths) - correlation_length))]
- if closest_length != correlation_length:
- print("Warning: desired correlation length {:.1f} not available. Taking {:.1f} instead".format(
- correlation_length, closest_length))
- corr_len = closest_length
- return corr_len
- def gauss(x, *p):
- A, mu, sigma, B = p
- return A * np.exp(-(x - mu) ** 2 / (2. * sigma ** 2)) + B
- def plot_tuning_curve(traj, direction_idx, plot_run_names):
- seed_expl = traj.f_get('seed').f_get_range()
- label_expl = [traj.derived_parameters.runs[run_name].morphology.morph_label for run_name in traj.f_get_run_names()]
- label_range = set(label_expl)
- rate_frame = pd.Series(index=[seed_expl, label_expl])
- rate_frame.index.names = ["seed", "label"]
- dir_bins = np.linspace(-np.pi, np.pi, traj.input.number_of_directions, endpoint=False)
- rate_bins = [[] for i in range(len(dir_bins)-1)]
- for run_name, seed, label in zip(plot_run_names, seed_expl, label_expl):
- ex_tunings = traj.results.runs[run_name].ex_tunings
- binned_idx = np.digitize(ex_tunings, dir_bins)
- #TODO: Avareage over directions by recentering
- firing_rate_array = traj.results[run_name].firing_rate_array
- for bin_idx, rate in zip(binned_idx, firing_rate_array[:, direction_idx]):
- rate_bins[bin_idx].append(rate)
- rate_bins_mean = [np.mean(rate_bin) for rate_bin in rate_bins]
- rate_frame[seed, label] = firing_rate_array
- # TODO: Standart deviation also for the population
- rate_seed_mean = rate_frame.groupby(level=[1]).mean()
- rate_seed_std_dev = rate_frame.groupby(level=[1]).std()
- style_dict = {
- 'no conn': ['grey', 'dashed', '', 0],
- 'ellipsoid': ['blue', 'solid', 'x', 10.],
- 'circular': ['lightblue', 'solid', 'o', 8.]
- }
- fig, ax = plt.subplots(1, 1)
- for label in label_range:
- hdi_mean = rate_seed_mean[label]
- hdi_std = rate_seed_std_dev[label]
- ex_tunings = traj.results.runs[run_name].ex_tunings
- col, lin, mar, mar_size = style_dict[label]
- ax.plot(corr_len_range, hdi_mean, label=label, marker=mar, color=col, linestyle=lin, markersize=mar_size)
- plt.fill_between(corr_len_range, hdi_mean - hdi_std,
- hdi_mean + hdi_std, alpha=0.4, color=col)
- ax.set_xlabel('Correlation length')
- ax.set_ylabel('Head Direction Index')
- ax.axvline(206.9, color='k', linewidth=0.5)
- ax.set_ylim(0.0, 1.0)
- ax.set_xlim(0.0, 400.)
- ax.legend()
- fig, ax = plt.subplots(1, 1)
- for run_idx, run_name in enumerate(plot_run_names):
- label = traj.derived_parameters.runs[run_name].morphology.morph_label
- ex_tunings = traj.results.runs[run_name].ex_tunings
- coeff_list = []
- ex_tunings_plt = np.array(ex_tunings)
- sort_ids = ex_tunings_plt.argsort()
- ex_tunings_plt = ex_tunings_plt[sort_ids]
- firing_rate_array = traj.results[run_name].firing_rate_array
- # firing_rate_array = traj.f_get('firing_rate_array')
- rates_plt = firing_rate_array[:, direction_idx]
- rates_plt = rates_plt[sort_ids]
- ax.scatter(ex_tunings_plt, rates_plt / hertz, label=label, alpha=0.3)
- ax.legend()
- ax.set_xlabel("Angles (rad)")
- ax.set_ylabel("f (Hz)")
- ax.set_title('tuning curves', fontsize=16)
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'tuning_curve.png', dpi=200)
- def colorbar(mappable):
- from mpl_toolkits.axes_grid1 import make_axes_locatable
- import matplotlib.pyplot as plt
- last_axes = plt.gca()
- ax = mappable.axes
- fig = ax.figure
- divider = make_axes_locatable(ax)
- cax = divider.append_axes("right", size="5%", pad=0.05)
- cbar = fig.colorbar(mappable, cax=cax)
- plt.sca(last_axes)
- return cbar
- def plot_firing_rate_map_excitatory(traj, direction_idx, plot_run_names):
- max_val = 0
- for run_name in plot_run_names:
- fr_array = traj.results.runs[run_name].firing_rate_array
- f_rates = fr_array[:, direction_idx]
- run_max_val = np.max(f_rates)
- if run_max_val > max_val:
- # if traj.derived_parameters.runs[run_name].morphology.morph_label == 'ellipsoid':
- # n_id_max_rate = np.argmax(f_rates)
- max_val = run_max_val
- n_id_polar_plot = 609
- # Mark the neuron that is shown in Polar plot
- ex_positions = traj.results.runs[plot_run_names[0]].ex_positions
- polar_plot_x, polar_plot_y = ex_positions[n_id_polar_plot]
- # Vertices for the plotted triangle
- tr_scale = 13.
- tr_x = tr_scale * np.cos(2. * np.pi / 3. + np.pi / 2.)
- tr_y = tr_scale * np.sin(2. * np.pi / 3. + np.pi / 2.) + polar_plot_y
- tr_vertices = np.array([[polar_plot_x, polar_plot_y + tr_scale], [tr_x + polar_plot_x, tr_y], [-tr_x + polar_plot_x, tr_y]])
- height = 4.5
- # color_bar_size = 0.05 * height + 0.05
- # width = 3 * height + color_bar_size
- width = 13.5
- fig, axes = plt.subplots(1, 3, figsize=(width, height))
- for ax, run_name in zip(axes, plot_run_names[::-1]):
- label = traj.derived_parameters.runs[run_name].morphology.morph_label
- X, Y = get_correct_position_mesh(traj.results.runs[run_name].ex_positions)
- firing_rate_array = traj.results[run_name].firing_rate_array
- number_of_excitatory_neurons_per_row = int(np.sqrt(traj.N_E))
- c = ax.pcolor(X, Y, np.reshape(firing_rate_array[:, direction_idx], (number_of_excitatory_neurons_per_row,
- number_of_excitatory_neurons_per_row)),
- vmin=0, vmax=max_val, cmap='Reds')
- ax.set_title(label)
- # ax.add_artist(Ellipse((polar_plot_x, polar_plot_y), 20., 20., color='k', fill=False, lw=2.))
- # ax.add_artist(Ellipse((polar_plot_x, polar_plot_y), 20., 20., color='w', fill=False, lw=1.))
- ax.add_artist(Polygon(tr_vertices, closed=True, fill=False, lw=2.5, color='k'))
- ax.add_artist(Polygon(tr_vertices, closed=True, fill=False, lw=1.5, color='w'))
- # fig.suptitle('spatial firing rate map', fontsize=16)
- colorbar(c)
- fig.tight_layout()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'firing_rate_map.png', dpi=200)
- return n_id_polar_plot
- def plot_firing_rate_map_inhibitory(traj, direction_idx, plot_run_names):
- max_val = 0
- for run_name in plot_run_names:
- fr_array = traj.results.runs[run_name].inh_firing_rate_array
- f_rates = fr_array[:, direction_idx]
- run_max_val = np.max(f_rates)
- if run_max_val > max_val:
- max_val = run_max_val
- n_id_polar_plot = 52
- # Mark the neuron that is shown in Polar plot
- inhibitory_axonal_cloud_array = traj.results.runs[plot_run_names[1]].inhibitory_axonal_cloud_array
- polar_plot_x = inhibitory_axonal_cloud_array[n_id_polar_plot, 0]
- polar_plot_y = inhibitory_axonal_cloud_array[n_id_polar_plot, 1]
- plot_run_names_sorted = [plot_run_names[1], plot_run_names[0]]
- fig, axes = plt.subplots(1, 2, figsize=(9.0, 4.5))
- for ax, run_name in zip(axes, plot_run_names_sorted):
- label = traj.derived_parameters.runs[run_name].morphology.morph_label
- inhibitory_axonal_cloud_array = traj.results.runs[run_name].inhibitory_axonal_cloud_array
- inh_positions = [[p[0], p[1]] for p in inhibitory_axonal_cloud_array]
- X, Y = get_correct_position_mesh(inh_positions)
- inh_firing_rate_array = traj.results[run_name].inh_firing_rate_array
- number_of_inhibitory_neurons_per_row = int(np.sqrt(traj.N_I))
- c = ax.pcolor(X, Y, np.reshape(inh_firing_rate_array[:, direction_idx], (number_of_inhibitory_neurons_per_row,
- number_of_inhibitory_neurons_per_row)),
- vmin=0, vmax=max_val, cmap='Blues')
- ax.set_title(label)
- circle_r = 40.
- ax.add_artist(Ellipse((polar_plot_x, polar_plot_y), circle_r, circle_r, color='k', fill=False, lw=4.5))
- ax.add_artist(Ellipse((polar_plot_x, polar_plot_y), circle_r, circle_r, color='w', fill=False, lw=3))
- # fig.colorbar(c, ax=ax, label="f (Hz)")
- # fig.suptitle('spatial firing rate map', fontsize=16)
- colorbar(c)
- fig.tight_layout()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'inh_firing_rate_map.png', dpi=200)
- return n_id_polar_plot, max_val
- def plot_hdi_over_tuning(traj, plot_run_names):
- fig, ax = plt.subplots(1, 1)
- for run_idx, run_name in enumerate(plot_run_names):
- label = traj.derived_parameters.runs[run_name].morphology.morph_label
- ex_tunings = traj.results.runs[run_name].ex_tunings
- ex_tunings_plt = np.array(ex_tunings)
- sort_ids = ex_tunings_plt.argsort()
- ex_tunings_plt = ex_tunings_plt[sort_ids]
- head_direction_indices = traj.results[run_name].head_direction_indices
- hdi_plt = head_direction_indices
- hdi_plt = hdi_plt[sort_ids]
- ax.scatter(ex_tunings_plt, hdi_plt, label=label, alpha=0.3)
- ax.legend()
- ax.set_xlabel("Angles (rad)")
- ax.set_ylabel("head direction index")
- ax.set_title('hdi over input tuning', fontsize=16)
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'hdi_over_tuning.png', dpi=200)
- def plot_axonal_clouds(traj, plot_run_names):
- n_ex = int(np.sqrt(traj.N_E))
- fig, axes = plt.subplots(1, 3, figsize=(13.5, 4.5))
- for ax, run_name in zip(axes, plot_run_names[::-1]):
- traj.f_set_crun(run_name)
- label = traj.derived_parameters.runs[run_name].morphology.morph_label
- X, Y = get_correct_position_mesh(traj.results.runs[run_name].ex_positions)
- inhibitory_axonal_cloud_array = traj.results.runs[run_name].inhibitory_axonal_cloud_array
- axonal_clouds = [Pickle(p[0], p[1], traj.morphology.long_axis, traj.morphology.short_axis, p[2]) for p in
- inhibitory_axonal_cloud_array]
- head_dir_preference = np.array(traj.results.runs[run_name].ex_tunings).reshape((n_ex, n_ex))
- # TODO: Why was this transposed for plotting? (now changed)
- c = ax.pcolor(X, Y, head_dir_preference, vmin=-np.pi, vmax=np.pi, cmap='hsv')
- ax.set_title(label)
- # fig.colorbar(c, ax=ax, label="Tuning")
- if label != 'no conn' and axonal_clouds is not None:
- for i, p in enumerate(axonal_clouds):
- ell = p.get_ellipse()
- ax.add_artist(ell)
- # fig.suptitle('axonal cloud', fontsize=16)
- traj.f_restore_default()
- fig.tight_layout()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'axonal_clouds.png', dpi=200)
- def plot_orientation_maps_diff_scales(traj):
- n_ex = int(np.sqrt(traj.N_E))
- scale_run_names = []
- plot_scales = [0.0, 100.0, 200.0, 300.0]
- for scale in plot_scales:
- par_dict = {'seed': 1, 'correlation_length': get_closest_correlation_length(traj,scale), 'long_axis': 100.}
- scale_run_names.append(*filter_run_names_by_par_dict(traj, par_dict))
- fig, axes = plt.subplots(1, 4, figsize=(18., 4.5))
- for ax, run_name, scale in zip(axes, scale_run_names, plot_scales):
- traj.f_set_crun(run_name)
- X, Y = get_position_mesh(traj.results.runs[run_name].ex_positions)
- head_dir_preference = np.array(traj.results.runs[run_name].ex_tunings).reshape((n_ex, n_ex))
- # TODO: Why was this transposed for plotting? (now changed)
- c = ax.pcolor(X, Y, head_dir_preference, vmin=-np.pi, vmax=np.pi, cmap='twilight')
- ax.set_title('Correlation length: {}'.format(scale))
- fig.colorbar(c, ax=ax, label="Tuning")
- # fig.suptitle('axonal cloud', fontsize=16)
- traj.f_restore_default()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'orientation_maps_diff_scales.png', dpi=200)
- def plot_orientation_maps_diff_scales_with_ellipse(traj):
- n_ex = int(np.sqrt(traj.N_E))
- scale_run_names = []
- plot_scales = [0.0, 100.0, 200.0, 300.0, 400.0]
- for scale in plot_scales:
- par_dict = {'seed': 1, 'correlation_length': get_closest_correlation_length(traj,scale), 'long_axis': 100.}
- scale_run_names.append(*filter_run_names_by_par_dict(traj, par_dict))
- print(scale_run_names)
- fig, axes = plt.subplots(1, 5, figsize=(18., 4.5))
- for ax, run_name, scale in zip(axes, scale_run_names, plot_scales):
- traj.f_set_crun(run_name)
- X, Y = get_position_mesh(traj.results.runs[run_name].ex_positions)
- inhibitory_axonal_cloud_array = traj.results.runs[run_name].inhibitory_axonal_cloud_array
- axonal_clouds = [Pickle(p[0], p[1], traj.morphology.long_axis, traj.morphology.short_axis, p[2]) for p in
- inhibitory_axonal_cloud_array]
- head_dir_preference = np.array(traj.results.runs[run_name].ex_tunings).reshape((n_ex, n_ex))
- # TODO: Why was this transposed for plotting? (now changed)
- c = ax.pcolor(X, Y, head_dir_preference, vmin=-np.pi, vmax=np.pi, cmap='hsv')
- # ax.set_title('Correlation length: {}'.format(scale))
- # fig.colorbar(c, ax=ax, label="Tuning")
- ax.set_xticks([])
- ax.set_yticks([])
- p1 = axonal_clouds[44]
- ell = p1.get_ellipse()
- ell._linewidth = 5.
- ax.add_artist(ell)
- p2 = axonal_clouds[77]
- circ_r = 2 * np.sqrt(2500.)
- circ = Ellipse((p2.x, p2.y), circ_r, circ_r, fill=False, zorder=2, edgecolor='k')
- circ._linewidth = 5.
- ax.add_artist(circ)
- # fig.suptitle('axonal cloud', fontsize=16)
- traj.f_restore_default()
- fig.tight_layout()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'orientation_maps_diff_scales_with_ellipse.png', dpi=200)
- def plot_excitatory_condensed_polar_plot(traj, plot_run_names, polar_plot_id):
- directions = np.linspace(-np.pi, np.pi, traj.input.number_of_directions, endpoint=False)
- directions_plt = list(directions)
- directions_plt.append(directions[0])
- fig, ax = plt.subplots(1, 1, figsize=(3.5, 3.5), subplot_kw=dict(projection='polar'))
- # head_direction_indices = traj.results.runs[plot_run_names[0]].head_direction_indices
- # sorted_ids = np.argsort(head_direction_indices)
- # plot_n_idx = sorted_ids[-75]
- plot_n_idx = polar_plot_id
- line_styles = ['dotted', 'solid', 'dashed']
- colors = ['r', 'lightsalmon', 'grey']
- max_rate = 0.0
- for run_idx, run_name in enumerate(plot_run_names):
- label = traj.derived_parameters.runs[run_name].morphology.morph_label
- tuning_vectors = traj.results.runs[run_name].tuning_vectors
- rate_plot = [np.linalg.norm(v) for v in tuning_vectors[plot_n_idx]]
- run_max_rate = np.max(rate_plot)
- if run_max_rate > max_rate:
- max_rate = run_max_rate
- rate_plot.append(rate_plot[0])
- ax.plot(directions_plt, rate_plot, label=label, color=colors[run_idx], linestyle=line_styles[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
- ticks = [30., 60., 90.]
- ax.set_rticks(ticks)
- ax.set_rlabel_position(230)
- ax.legend(loc='upper center', bbox_to_anchor=(0.2, 1.05),
- fancybox=True, shadow=True)
- plt.tight_layout()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'condensed_polar_plot.png', dpi=200)
- def plot_inhibitory_condensed_polar_plot(traj, plot_run_names, polar_plot_id, max_rate):
- directions = np.linspace(-np.pi, np.pi, traj.input.number_of_directions, endpoint=False)
- directions_plt = list(directions)
- directions_plt.append(directions[0])
- fig, ax = plt.subplots(1, 1, figsize=(3.5, 3.5), subplot_kw=dict(projection='polar'))
- # head_direction_indices = traj.results.runs[plot_run_names[0]].inh_head_direction_indices
- # sorted_ids = np.argsort(head_direction_indices)
- # plot_n_idx = sorted_ids[-75]
- plot_n_idx = polar_plot_id
- line_styles = ['dotted', 'solid']
- colors = ['b', 'lightblue']
- for run_idx, run_name in enumerate(plot_run_names[:2]):
- # ax = axes[max_hdi_idx, run_idx]
- label = traj.derived_parameters.runs[run_name].morphology.morph_label
- tuning_vectors = traj.results.runs[run_name].inh_tuning_vectors
- rate_plot = [np.linalg.norm(v) for v in tuning_vectors[plot_n_idx]]
- rate_plot.append(rate_plot[0])
- ax.plot(directions_plt, rate_plot, label=label, color=colors[run_idx], linestyle=line_styles[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)]
- ticks = [40., 80., 120.]
- ax.set_rticks(ticks)
- ax.set_rlabel_position(230)
- ax.legend(loc='upper center', bbox_to_anchor=(0.2, 1.05),
- fancybox=True, shadow=True)
- plt.tight_layout()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'condensed_inhibitory_polar_plot.png', dpi=200)
- def plot_hdi_over_corr_len(traj, plot_run_names):
- corr_len_expl = traj.f_get('correlation_length').f_get_range()
- seed_expl = traj.f_get('seed').f_get_range()
- label_expl = [traj.derived_parameters.runs[run_name].morphology.morph_label for run_name in traj.f_get_run_names()]
- label_range = set(label_expl)
- hdi_frame = pd.Series(index=[corr_len_expl, seed_expl, label_expl])
- hdi_frame.index.names = ["corr_len", "seed", "label"]
- for run_name, corr_len, seed, label in zip(plot_run_names, corr_len_expl, seed_expl, label_expl):
- ex_tunings = traj.results.runs[run_name].ex_tunings
- head_direction_indices = traj.results[run_name].head_direction_indices
- hdi_frame[corr_len, seed, label] = np.mean(head_direction_indices)
- # TODO: Standart deviation also for the population
- hdi_exc_n_and_seed_mean = hdi_frame.groupby(level=[0, 2]).mean()
- hdi_exc_n_and_seed_std_dev = hdi_frame.groupby(level=[0, 2]).std()
- # Ellipsoid markers
- rx, ry = 5., 12.
- # area = rx * ry * np.pi * 2.
- area = 1.
- theta = np.arange(0, 2 * np.pi + 0.01, 0.1)
- verts = np.column_stack([rx / area * np.cos(theta), ry / area * np.sin(theta)])
- style_dict = {
- 'no conn': ['grey', 'dashed', '', 0],
- 'ellipsoid': ['blue', 'solid', verts, 10.],
- 'circular': ['lightblue', 'solid', 'o', 8.]
- }
- # colors = ['blue', 'grey', 'lightblue']
- # linestyles = ['solid', 'dashed', 'solid']
- # markers = [verts, '', 'o']
- fig, ax = plt.subplots(1, 1)
- for label in label_range:
- hdi_mean = hdi_exc_n_and_seed_mean[:, label]
- hdi_std = hdi_exc_n_and_seed_std_dev[:, label]
- corr_len_range = hdi_mean.keys().to_numpy()
- col, lin, mar, mar_size = style_dict[label]
- ax.plot(corr_len_range, hdi_mean, label=label, marker=mar, color=col, linestyle=lin, markersize=mar_size)
- plt.fill_between(corr_len_range, hdi_mean - hdi_std,
- hdi_mean + hdi_std, alpha=0.4, color=col)
- ax.set_xlabel('Correlation length')
- ax.set_ylabel('Head Direction Index')
- ax.axvline(206.9, color='k', linewidth=0.5)
- ax.set_ylim(0.0,1.0)
- ax.set_xlim(0.0,400.)
- ax.legend()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'hdi_over_corr_len_scaled.png', dpi=200)
- def plot_hdi_histogram_excitatory(traj, plot_run_names):
- labels = []
- hdis = []
- colors = ['black', 'red', 'green']
- for run_idx, run_name in enumerate(plot_run_names):
- label = traj.derived_parameters.runs[run_name].morphology.morph_label
- labels.append(label)
- head_direction_indices = traj.results.runs[run_name].head_direction_indices
- hdis.append(head_direction_indices)
- fig, ax = plt.subplots(1, 1, figsize=(6, 3))
- ax.hist(hdis, color=colors, label=labels, bins=30)
- for hdi, color in zip(hdis, colors):
- mean_hdi = np.mean(hdi)
- ax.axvline(mean_hdi, 0, 1, color=color, linestyle='--')
- ax.set_xlabel("HDI")
- ax.legend()
- fig.tight_layout()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'hdi_histogram_excitatory.png', dpi=200)
- def plot_hdi_violin_excitatory(traj, plot_run_names):
- labels = []
- hdis = []
- colors = ['black', 'red', 'green']
- no_conn_hdi = 0.
- for run_idx, run_name in enumerate(plot_run_names):
- label = traj.derived_parameters.runs[run_name].morphology.morph_label
- head_direction_indices = traj.results.runs[run_name].head_direction_indices
- if label == 'no conn':
- no_conn_hdi = np.mean(head_direction_indices)
- else:
- labels.append(label)
- hdis.append(sorted(head_direction_indices))
- fig, ax = plt.subplots(1, 1, figsize=(6, 3))
- # hdis = np.array(hdis)
- viol_plt = ax.violinplot(hdis, showmeans=True, showextrema=False)
- viol_plt['cmeans'].set_color('black')
- for pc in viol_plt['bodies']:
- pc.set_facecolor('red')
- pc.set_edgecolor('black')
- pc.set_alpha(0.7)
- ax.axhline(no_conn_hdi, color='black', linestyle='--')
- ax.annotate('no conn', xy=(0.45,0.48), xycoords='axes fraction')
- ax.set_xticks(np.arange(1, len(labels) + 1))
- ax.set_xticklabels(labels)
- ax.set_ylabel('HDI')
- fig.tight_layout()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'hdi_violin_excitatory.png', dpi=200)
- def plot_hdi_violin_inhibitory(traj, plot_run_names):
- labels = []
- hdis = []
- 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 conn':
- labels.append(label)
- head_direction_indices = traj.results.runs[run_name].inh_head_direction_indices
- hdis.append(sorted(head_direction_indices))
- fig, ax = plt.subplots(1, 1, figsize=(6, 3))
- viol_plt = ax.violinplot(hdis, showmeans=True, showextrema=False)
- viol_plt['cmeans'].set_color('black')
- for pc in viol_plt['bodies']:
- pc.set_facecolor('blue')
- pc.set_edgecolor('black')
- pc.set_alpha(0.7)
- ax.set_xticks(np.arange(1, len(labels) + 1))
- ax.set_xticklabels(labels)
- ax.set_ylabel('HDI')
- fig.tight_layout()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'hdi_violin_inhibitory.png', dpi=200)
- def plot_hdi_violin_combined(traj, plot_run_names):
- labels = []
- inh_hdis = []
- exc_hdis = []
- no_conn_hdi = 0.
- 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 conn':
- labels.append(label)
- inh_head_direction_indices = traj.results.runs[run_name].inh_head_direction_indices
- inh_hdis.append(sorted(inh_head_direction_indices))
- exc_head_direction_indices = traj.results.runs[run_name].head_direction_indices
- exc_hdis.append(sorted(exc_head_direction_indices))
- else:
- exc_head_direction_indices = traj.results.runs[run_name].head_direction_indices
- no_conn_hdi = np.mean(exc_head_direction_indices)
- fig, ax = plt.subplots(1, 1, figsize=(6, 3))
- inh_viol_plt = ax.violinplot(inh_hdis, showmeans=True, showextrema=False)
- # viol_plt['cmeans'].set_color('black')
- #
- # for pc in viol_plt['bodies']:
- # pc.set_facecolor('blue')
- # pc.set_edgecolor('black')
- # pc.set_alpha(0.7)
- for b in inh_viol_plt['bodies']:
- m = np.mean(b.get_paths()[0].vertices[:, 0])
- b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], m, np.inf)
- b.set_color('b')
- exc_viol_plt = ax.violinplot(exc_hdis, showmeans=True, showextrema=False)
- for b in exc_viol_plt['bodies']:
- m = np.mean(b.get_paths()[0].vertices[:, 0])
- b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], -np.inf, m)
- b.set_color('r')
- ax.axhline(no_conn_hdi, color='black', linestyle='--')
- ax.annotate('no conn', xy=(0.45, 0.48), xycoords='axes fraction')
- ax.set_xticks(np.arange(1, len(labels) + 1))
- ax.set_xticklabels(labels)
- ax.set_ylabel('HDI')
- fig.tight_layout()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'hdi_violin_combined.svg', dpi=200)
- def plot_hdi_violin_combined_and_overlayed(traj, plot_run_names):
- labels = []
- inh_hdis = []
- exc_hdis = []
- no_conn_hdi = 0.
- 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 conn':
- labels.append(label)
- inh_head_direction_indices = traj.results.runs[run_name].inh_head_direction_indices
- inh_hdis.append(sorted(inh_head_direction_indices))
- exc_head_direction_indices = traj.results.runs[run_name].head_direction_indices
- exc_hdis.append(sorted(exc_head_direction_indices))
- else:
- exc_head_direction_indices = traj.results.runs[run_name].head_direction_indices
- no_conn_hdi = np.mean(exc_head_direction_indices)
- fig, ax = plt.subplots(1, 1, figsize=(3.5, 4.5))
- inh_ell_viol_plt = ax.violinplot(inh_hdis[0], showmeans=True, showextrema=False)
- for b in inh_ell_viol_plt['bodies']:
- m = np.mean(b.get_paths()[0].vertices[:, 0])
- b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], m, np.inf)
- b.set_color('b')
- mean_line = inh_ell_viol_plt['cmeans']
- mean_line.set_color('b')
- mean_line.get_paths()[0].vertices[:, 0] = np.clip(mean_line.get_paths()[0].vertices[:, 0], m, np.inf)
- exc_ell_viol_plt = ax.violinplot(exc_hdis[0], showmeans=True, showextrema=False)
- for b in exc_ell_viol_plt['bodies']:
- m = np.mean(b.get_paths()[0].vertices[:, 0])
- b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], m, np.inf)
- b.set_color('r')
- mean_line = exc_ell_viol_plt['cmeans']
- mean_line.set_color('r')
- mean_line.get_paths()[0].vertices[:, 0] = np.clip(mean_line.get_paths()[0].vertices[:, 0], m, np.inf)
- inh_cir_viol_plt = ax.violinplot(inh_hdis[1], showmeans=True, showextrema=False)
- for b in inh_cir_viol_plt['bodies']:
- m = np.mean(b.get_paths()[0].vertices[:, 0])
- b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], -np.inf, m)
- b.set_color('b')
- mean_line = inh_cir_viol_plt['cmeans']
- mean_line.set_color('b')
- mean_line.get_paths()[0].vertices[:, 0] = np.clip(mean_line.get_paths()[0].vertices[:, 0], -np.inf, m)
- exc_cir_viol_plt = ax.violinplot(exc_hdis[1], showmeans=True, showextrema=False)
- for b in exc_cir_viol_plt['bodies']:
- m = np.mean(b.get_paths()[0].vertices[:, 0])
- b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], -np.inf, m)
- b.set_color('r')
- mean_line = exc_cir_viol_plt['cmeans']
- mean_line.set_color('r')
- mean_line.get_paths()[0].vertices[:, 0] = np.clip(mean_line.get_paths()[0].vertices[:, 0], -np.inf, m)
- ax.axhline(no_conn_hdi, 0.5, 1., color='black', linestyle='--')
- ax.axvline(1.0, color='k')
- ax.annotate('no conn', xy=(0.75, 0.415), xycoords='axes fraction')
- ax.set_xlim(0.5, 1.5)
- ax.set_ylim(0.0, 1.0)
- ax.set_xticks([0.75, 1.25])
- ax.set_xticklabels(['circular', 'ellipsoid'])
- ax.set_ylabel('HDI')
- fig.tight_layout()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'hdi_violin_combined_and_overlayed.svg', dpi=200)
- def plot_hdi_histogram_inhibitory(traj, plot_run_names):
- labels = []
- hdis = []
- 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 conn':
- labels.append(label)
- head_direction_indices = traj.results.runs[run_name].inh_head_direction_indices
- hdis.append(head_direction_indices)
- fig, ax = plt.subplots(1, 1, figsize=(6, 3))
- ax.hist(hdis, color=colors, label=labels, bins=30)
- for hdi, color in zip(hdis, colors):
- mean_hdi = np.mean(hdi)
- ax.axvline(mean_hdi, 0, 1, color=color, linestyle='--')
- ax.set_xlabel("HDI")
- ax.legend()
- fig.tight_layout()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'hdi_histogram_inhibitory.png', dpi=200)
- def filter_run_names_by_par_dict(traj, par_dict):
- run_name_list = []
- for run_idx, run_name in enumerate(traj.f_get_run_names()):
- traj.f_set_crun(run_name)
- paramters_equal = True
- for key, val in par_dict.items():
- if (traj.par[key] != val):
- paramters_equal = False
- if paramters_equal:
- run_name_list.append(run_name)
- traj.f_restore_default()
- return run_name_list
- def plot_exc_and_inh_hdi_over_corr_len(traj, plot_run_names):
- corr_len_expl = traj.f_get('correlation_length').f_get_range()
- seed_expl = traj.f_get('seed').f_get_range()
- label_expl = [traj.derived_parameters.runs[run_name].morphology.morph_label for run_name in traj.f_get_run_names()]
- label_range = set(label_expl)
- exc_hdi_frame = pd.Series(index=[corr_len_expl, seed_expl, label_expl])
- exc_hdi_frame.index.names = ["corr_len", "seed", "label"]
- inh_hdi_frame = pd.Series(index=[corr_len_expl, seed_expl, label_expl])
- inh_hdi_frame.index.names = ["corr_len", "seed", "label"]
- for run_name, corr_len, seed, label in zip(plot_run_names, corr_len_expl, seed_expl, label_expl):
- ex_tunings = traj.results.runs[run_name].ex_tunings
- head_direction_indices = traj.results[run_name].head_direction_indices
- #TODO: Actual correlation lengths
- # actual_corr_len = get_correlation_length(ex_tunings.reshape((30,30)), 450, 30)
- exc_hdi_frame[corr_len, seed, label] = np.mean(head_direction_indices)
- inh_head_direction_indices = traj.results[run_name].inh_head_direction_indices
- inh_hdi_frame[corr_len, seed, label] = np.mean(inh_head_direction_indices)
- # TODO: Standart deviation also for the population
- exc_hdi_n_and_seed_mean = exc_hdi_frame.groupby(level=[0, 2]).mean()
- exc_hdi_n_and_seed_std_dev = exc_hdi_frame.groupby(level=[0, 2]).std()
- inh_hdi_n_and_seed_mean = inh_hdi_frame.groupby(level=[0, 2]).mean()
- inh_hdi_n_and_seed_std_dev = inh_hdi_frame.groupby(level=[0, 2]).std()
- exc_style_dict = {
- 'no conn': ['grey', 'dashed', '', 0],
- 'ellipsoid': ['red', 'solid', '^', 8.],
- 'circular': ['lightsalmon', 'solid', '^', 8.]
- }
- inh_style_dict = {
- 'no conn': ['grey', 'dashed', '', 0],
- 'ellipsoid': ['blue', 'solid', 'o', 8.],
- 'circular': ['lightblue', 'solid', 'o', 8.]
- }
- fig, ax = plt.subplots(1, 1)
- for label in label_range:
- if label == 'no conn':
- ax.axhline(exc_hdi_n_and_seed_mean[0, label], color='grey', linestyle='--')
- ax.annotate('input', xy=(1.01, 0.44), xycoords='axes fraction')
- continue
- exc_hdi_mean = exc_hdi_n_and_seed_mean[:, label]
- exc_hdi_std = exc_hdi_n_and_seed_std_dev[:, label]
- inh_hdi_mean = inh_hdi_n_and_seed_mean[:, label]
- inh_hdi_std = inh_hdi_n_and_seed_std_dev[:, label]
- corr_len_range = exc_hdi_mean.keys().to_numpy()
- exc_col, exc_lin, exc_mar, exc_mar_size = exc_style_dict[label]
- inh_col, inh_lin, inh_mar, inh_mar_size = inh_style_dict[label]
- ax.plot(corr_len_range, exc_hdi_mean, label='exc., ' + label, marker=exc_mar, color=exc_col, linestyle=exc_lin, markersize=exc_mar_size, alpha=0.5)
- plt.fill_between(corr_len_range, exc_hdi_mean - exc_hdi_std,
- exc_hdi_mean + exc_hdi_std, alpha=0.3, color=exc_col)
- ax.plot(corr_len_range, inh_hdi_mean, label='inh., ' + label, marker=inh_mar, color=inh_col, linestyle=inh_lin, markersize=inh_mar_size, alpha=0.5)
- plt.fill_between(corr_len_range, inh_hdi_mean - inh_hdi_std,
- inh_hdi_mean + inh_hdi_std, alpha=0.3, color=inh_col)
- ax.set_xlabel('Correlation length')
- ax.set_ylabel('Head Direction Index')
- ax.axvline(206.9, color='k', linewidth=0.5)
- ax.set_ylim(0.0,1.0)
- ax.set_xlim(0.0,400.)
- tablelegend(ax, ncol=2, bbox_to_anchor=(1, 1),
- row_labels=['exc.', 'inh.'],
- col_labels=['ellipsoid', 'circular'],
- title_label='')
- # plt.legend()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'exc_and_inh_hdi_over_corr_len_scaled.png', dpi=200)
- def plot_inhibitory_condensed_polar_plot_with_input(traj, plot_run_names, polar_plot_id, max_rate):
- directions = np.linspace(-np.pi, np.pi, traj.input.number_of_directions, endpoint=False)
- directions_plt = list(directions)
- directions_plt.append(directions[0])
- fig, ax = plt.subplots(1, 1, figsize=(3.5, 3.5), subplot_kw=dict(projection='polar'))
- # head_direction_indices = traj.results.runs[plot_run_names[0]].inh_head_direction_indices
- # sorted_ids = np.argsort(head_direction_indices)
- # plot_n_idx = sorted_ids[-75]
- plot_n_idx = polar_plot_id
- line_styles = ['dotted', 'solid', 'dashed']
- colors = ['b', 'lightblue', 'k']
- for run_idx, run_name in enumerate(plot_run_names):
- # ax = axes[max_hdi_idx, run_idx]
- label = traj.derived_parameters.runs[run_name].morphology.morph_label
- tuning_vectors = traj.results.runs[run_name].inh_tuning_vectors
- rate_plot = [np.linalg.norm(v) for v in tuning_vectors[plot_n_idx]]
- rate_plot.append(rate_plot[0])
- ax.plot(directions_plt, rate_plot, label=label, color=colors[run_idx], linestyle=line_styles[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)]
- ticks = [40., 80., 120.]
- ax.set_rticks(ticks)
- ax.set_rlabel_position(230)
- ax.legend(loc='upper center', bbox_to_anchor=(0.2, 1.05),
- fancybox=True, shadow=True)
- plt.tight_layout()
- if save_figs:
- plt.savefig(FIGURE_SAVE_PATH + 'condensed_inhibitory_polar_plot.png', dpi=200)
- if __name__ == "__main__":
- traj = Trajectory(TRAJ_NAME, add_time=False, dynamic_imports=Brian2MonitorResult)
- NO_LOADING = 0
- FULL_LOAD = 2
- traj.f_load(filename=DATA_FOLDER + TRAJ_NAME + ".hdf5", load_parameters=FULL_LOAD, load_results=NO_LOADING)
- traj.v_auto_load = True
- save_figs = True
- plot_corr_len = get_closest_correlation_length(traj, 200.0)
- par_dict = {'seed': 1, 'correlation_length': plot_corr_len, 'placement_jitter': 0.0}
- plot_run_names = filter_run_names_by_par_dict(traj, par_dict)
- print(plot_run_names)
- direction_idx = 6
- dir_indices = [0, 3, 6, 9]
- plot_axonal_clouds(traj, plot_run_names)
- #
- ex_polar_plot_id = plot_firing_rate_map_excitatory(traj, direction_idx, plot_run_names)
- #
- in_polar_plot_id, in_max_rate = plot_firing_rate_map_inhibitory(traj, direction_idx, plot_run_names)
- #
- # plot_orientation_maps_diff_scales_with_ellipse(traj)
- #
- # plot_hdi_histogram_inhibitory(traj, plot_run_names)
- #
- # plot_hdi_histogram_excitatory(traj, plot_run_names)
- #
- # plot_hdi_over_corr_len(traj, traj.f_get_run_names())
- plot_excitatory_condensed_polar_plot(traj, plot_run_names, ex_polar_plot_id)
- plot_inhibitory_condensed_polar_plot_with_input(traj, plot_run_names, in_polar_plot_id, in_max_rate)
- # plot_hdi_violin_combined(traj, plot_run_names)
- #
- plot_hdi_violin_combined_and_overlayed(traj, plot_run_names)
- # plot_exc_and_inh_hdi_over_corr_len(traj, traj.f_get_run_names())
- # par_dict = {'correlation_length': plot_corr_len}
- # single_corr_len_run_names = filter_run_names_by_par_dict(traj, par_dict)
- # plot_tuning_curve(traj, direction_idx, single_corr_len_run_names)
- if not save_figs:
- plt.show()
- traj.f_restore_default()
|