From 2a1db6d1c2332d81b1789852c7c3fdd88d2041d9 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Tue, 25 May 2021 21:39:25 -0400 Subject: [PATCH 01/23] Start work on function to plot connectivity matrix with weights --- hnn_core/viz.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/hnn_core/viz.py b/hnn_core/viz.py index 520590965..c2e461cd8 100644 --- a/hnn_core/viz.py +++ b/hnn_core/viz.py @@ -569,6 +569,75 @@ def plot_cell_morphology(cell, ax, show=True): return ax +def plot_connectivity_weights(net, conn_idx, ax=None, show=True): + """ Plot connectivity matrix with color bar for synaptic weights + Parameters + ---------- + net : Instance of Network object + The Network object + conn_idx : int + Index of _Connectivity object to be visualized + from `net.connectivity` + ax : instance of Axes3D + Matplotlib 3D axis + show : bool + If True, show the plot + + Returns + ------- + fig : instance of matplotlib Figure + The matplotlib figure handle. + """ + import matplotlib.pyplot as plt + from .network import Network + + if not isinstance(net, Network): + raise TypeError('conn must be instance of _Connectivity') + if ax is None: + _, ax = plt.subplots(1, 1) + + # Load objects for distance calculation + conn = net.connectivity[conn_idx] + nc_dict = conn['nc_dict'] + src_type = conn['src_type'] + target_type = conn['target_type'] + src_type_pos = net.pos_dict[src_type] + target_type_pos = net.pos_dict[target_type] + + src_range = np.array(conn['src_range']) + target_range = np.array(conn['target_range']) + connectivity_matrix = np.zeros((len(src_range), len(target_range))) + + for src_gid, target_src_pair in conn['gid_pairs'].items(): + src_idx = np.where(src_range == src_gid)[0][0] + target_indeces = np.where(np.in1d(target_range, target_src_pair))[0] + for target_idx in target_indeces: + src_pos = src_type_pos[src_idx] + target_pos = target_type_pos[target_idx] + + # Identical calculation used in Cell.par_connect_from_src() + dx = src_pos[0] - target_pos[0] + dy = src_pos[1] - target_pos[1] + d = np.sqrt(dx**2 + dy**2) + + weight = nc_dict['A_weight'] * \ + np.exp(-(d**2) / (nc_dict['lamtha']**2)) + + connectivity_matrix[src_idx, target_idx] = weight + + ax.imshow(connectivity_matrix, cmap='Greys', interpolation='none') + ax.set_xlabel(f"{conn['target_type']} target gids " + f"({target_range[0]}-{target_range[-1]})") + ax.set_xticklabels(list()) + ax.set_ylabel(f"{conn['src_type']} source gids " + f"({src_range[0]}-{src_range[-1]})") + ax.set_yticklabels(list()) + ax.set_title(f"{conn['src_type']} -> {conn['target_type']} " + f"({conn['loc']}, {conn['receptor']})") + + return ax.get_figure() + + def plot_connectivity_matrix(conn, ax=None, show=True): """Plot connectivity matrix for instance of _Connectivity object. @@ -576,6 +645,10 @@ def plot_connectivity_matrix(conn, ax=None, show=True): ---------- conn : Instance of _Connectivity object The _Connectivity object + ax : instance of Axes3D + Matplotlib 3D axis + show : bool + If True, show the plot Returns ------- From 31f2f4ce2ce3e66ccf1b91cd13edbdb4a196a57d Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Tue, 25 May 2021 21:39:56 -0400 Subject: [PATCH 02/23] Add plot_connectivity_weights to example --- examples/plot_connectivity.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/plot_connectivity.py b/examples/plot_connectivity.py index 5c7824e98..67b287da1 100644 --- a/examples/plot_connectivity.py +++ b/examples/plot_connectivity.py @@ -38,10 +38,16 @@ # Instantiating the network comes with a predefined set of connections that # reflect the canonical neocortical microcircuit. ``net.connectivity`` # is a list of dictionaries which detail every cell-cell, and drive-cell -# connection. -print(len(net_erp.connectivity)) +# connection. The weights of these connections can be visualized with +# :func:`~hnn_core.viz.plot_connectivity_weights` +from hnn_core.viz import plot_connectivity_weights +n_connections = len(net_erp.connectivity) +print(n_connections) print(net_erp.connectivity[0:2]) +# Plot the last connection added to the network +plot_connectivity_weights(net_erp, conn_idx=n_connections) + ############################################################################### # Data recorded during simulations are stored under # :class:`~hnn_core.Cell_Response`. Spiking activity can be visualized after @@ -118,6 +124,7 @@ net_sparse.connectivity[-2].plot() net_sparse.connectivity[-1].plot() + ############################################################################### # Using the sparse connectivity pattern produced a lot more spiking in # the L5 pyramidal cells. Nevertheless there appears to be some rhythmicity From e54f8bada2591ecba581af5c2e157f48638d4f09 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Tue, 25 May 2021 21:45:28 -0400 Subject: [PATCH 03/23] Better plots --- examples/plot_connectivity.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/plot_connectivity.py b/examples/plot_connectivity.py index 67b287da1..640e76de0 100644 --- a/examples/plot_connectivity.py +++ b/examples/plot_connectivity.py @@ -8,7 +8,7 @@ # Author: Nick Tolley -# sphinx_gallery_thumbnail_number = 5 +# sphinx_gallery_thumbnail_number = 1 import os.path as op @@ -41,12 +41,13 @@ # connection. The weights of these connections can be visualized with # :func:`~hnn_core.viz.plot_connectivity_weights` from hnn_core.viz import plot_connectivity_weights -n_connections = len(net_erp.connectivity) -print(n_connections) -print(net_erp.connectivity[0:2]) +print(len(net_erp.connectivity)) -# Plot the last connection added to the network -plot_connectivity_weights(net_erp, conn_idx=n_connections) +print(net_erp.connectivity[15]) +plot_connectivity_weights(net_erp, conn_idx=15) + +print(net_erp.connectivity[20]) +plot_connectivity_weights(net_erp, conn_idx=20) ############################################################################### # Data recorded during simulations are stored under From a27426158d14aa6aeb6e585169f13e85c4467a58 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Tue, 25 May 2021 21:45:54 -0400 Subject: [PATCH 04/23] Show plots --- hnn_core/viz.py | 1 + 1 file changed, 1 insertion(+) diff --git a/hnn_core/viz.py b/hnn_core/viz.py index c2e461cd8..0a7b67add 100644 --- a/hnn_core/viz.py +++ b/hnn_core/viz.py @@ -635,6 +635,7 @@ def plot_connectivity_weights(net, conn_idx, ax=None, show=True): ax.set_title(f"{conn['src_type']} -> {conn['target_type']} " f"({conn['loc']}, {conn['receptor']})") + plt_show(show) return ax.get_figure() From 9e548efef7ed4e7c6a11a6647aebfcb31d5410f8 Mon Sep 17 00:00:00 2001 From: Nick Tolley <55253912+ntolley@users.noreply.github.com> Date: Thu, 27 May 2021 16:27:51 -0400 Subject: [PATCH 05/23] Update hnn_core/viz.py Co-authored-by: Mainak Jas --- hnn_core/viz.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hnn_core/viz.py b/hnn_core/viz.py index 0a7b67add..75f536537 100644 --- a/hnn_core/viz.py +++ b/hnn_core/viz.py @@ -570,7 +570,8 @@ def plot_cell_morphology(cell, ax, show=True): def plot_connectivity_weights(net, conn_idx, ax=None, show=True): - """ Plot connectivity matrix with color bar for synaptic weights + """Plot connectivity matrix with color bar for synaptic weights + Parameters ---------- net : Instance of Network object From b5e32a5e4639d843d1e5e819d4a2517590c00a74 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Thu, 27 May 2021 17:22:33 -0400 Subject: [PATCH 06/23] Move gaussian decay calculations into separate functions --- hnn_core/cell.py | 77 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 14 deletions(-) diff --git a/hnn_core/cell.py b/hnn_core/cell.py index d1d6dff2a..d910e4c38 100644 --- a/hnn_core/cell.py +++ b/hnn_core/cell.py @@ -25,6 +25,66 @@ def _get_cos_theta(p_secs, sec_name_apical): return cos_thetas +def _calculate_gaussian(x_val, height, lamtha): + """Return height of gaussian at x_val.and + + Parameters + ---------- + x_val : float + Value on x-axis to query height of gaussian curve. + height : float + Height of the gaussian curve at zero. + lamtha : float + Space constant. + + Returns + ------- + x_height : float + Height of gaussian at x_val. + + Notes + ----- + Gaussian curve is centered at zero and has a fixed peak height + such the _calculate_gaussian(0, lamtha) returns 1 for all lamtha. + """ + x_height = height * np.exp(-(x_val**2) / (lamtha**2)) + + return x_height + + +def _get_gaussian_connection(nc_dict, target_pos): + """Calculate distance dependent connection properties. + + Parameters + ---------- + nc_dict : dict + Dictionary with keys: pos_src, A_weight, A_delay, lamtha + Defines the connection parameters + target_pos : float + Position of target cell. + + Returns + ------- + weight : float + Weight of the synaptic connection. + delay : float + Delay of synaptic connection. + + Notes + ----- + Distance in xy plane is used for gaussian decay. + """ + x_dist = target_pos[0] - nc_dict['pos_src'][0] + y_dist = target_pos[1] - nc_dict['pos_src'][1] + cell_dist = np.sqrt(x_dist**2 + y_dist**2) + + weight = _calculate_gaussian( + cell_dist, nc_dict['A_weight'], nc_dict['lamtha']) + delay = _calculate_gaussian( + cell_dist, nc_dict['A_delay'], nc_dict['lamtha']) + return weight, delay + + class _ArtificialCell: """The ArtificialCell class for initializing a NEURON feed source. @@ -477,24 +537,13 @@ def parconnect_from_src(self, gid_presyn, nc_dict, postsyn): from .network_builder import _PC nc = _PC.gid_connect(gid_presyn, postsyn) - # calculate distance between cell positions with pardistance() - d = self._pardistance(nc_dict['pos_src']) - # set props here + + # set props here. nc.threshold = nc_dict['threshold'] - nc.weight[0] = nc_dict['A_weight'] * \ - np.exp(-(d**2) / (nc_dict['lamtha']**2)) - nc.delay = nc_dict['A_delay'] / \ - (np.exp(-(d**2) / (nc_dict['lamtha']**2))) + nc.weight[0], nc.delay = _get_gaussian_connection(nc_dict, self.pos) return nc - # pardistance function requires pre position, since it is - # calculated on POST cell - def _pardistance(self, pos_pre): - dx = self.pos[0] - pos_pre[0] - dy = self.pos[1] - pos_pre[1] - return np.sqrt(dx**2 + dy**2) - def plot_morphology(self, ax=None, cell_types=None, show=True): """Plot the cell morphology. From 8444d11969541ef5d3214fcfe85fd1e15b13a54f Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Thu, 27 May 2021 17:31:37 -0400 Subject: [PATCH 07/23] Update function viz to use gausian connection function --- hnn_core/cell.py | 15 +++++++++------ hnn_core/viz.py | 10 +++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/hnn_core/cell.py b/hnn_core/cell.py index d910e4c38..e6d883c79 100644 --- a/hnn_core/cell.py +++ b/hnn_core/cell.py @@ -52,16 +52,18 @@ def _calculate_gaussian(x_val, height, lamtha): return x_height -def _get_gaussian_connection(nc_dict, target_pos): +def _get_gaussian_connection(src_pos, target_pos, nc_dict): """Calculate distance dependent connection properties. Parameters ---------- + src_pos : float + Position of source cell. + target_pos : float + Position of target cell. nc_dict : dict Dictionary with keys: pos_src, A_weight, A_delay, lamtha Defines the connection parameters - target_pos : float - Position of target cell. Returns ------- @@ -74,8 +76,8 @@ def _get_gaussian_connection(nc_dict, target_pos): ----- Distance in xy plane is used for gaussian decay. """ - x_dist = target_pos[0] - nc_dict['pos_src'][0] - y_dist = target_pos[1] - nc_dict['pos_src'][1] + x_dist = target_pos[0] - src_pos[0] + y_dist = target_pos[1] - src_pos[1] cell_dist = np.sqrt(x_dist**2 + y_dist**2) weight = _calculate_gaussian( @@ -540,7 +542,8 @@ def parconnect_from_src(self, gid_presyn, nc_dict, postsyn): # set props here. nc.threshold = nc_dict['threshold'] - nc.weight[0], nc.delay = _get_gaussian_connection(nc_dict, self.pos) + nc.weight[0], nc.delay = _get_gaussian_connection( + nc_dict['pos_src'], self.pos, nc_dict) return nc diff --git a/hnn_core/viz.py b/hnn_core/viz.py index 75f536537..528d1f194 100644 --- a/hnn_core/viz.py +++ b/hnn_core/viz.py @@ -577,7 +577,7 @@ def plot_connectivity_weights(net, conn_idx, ax=None, show=True): net : Instance of Network object The Network object conn_idx : int - Index of _Connectivity object to be visualized + Index of connection to be visualized from `net.connectivity` ax : instance of Axes3D Matplotlib 3D axis @@ -591,6 +591,7 @@ def plot_connectivity_weights(net, conn_idx, ax=None, show=True): """ import matplotlib.pyplot as plt from .network import Network + from .cell import _get_gaussian_connection if not isinstance(net, Network): raise TypeError('conn must be instance of _Connectivity') @@ -617,12 +618,7 @@ def plot_connectivity_weights(net, conn_idx, ax=None, show=True): target_pos = target_type_pos[target_idx] # Identical calculation used in Cell.par_connect_from_src() - dx = src_pos[0] - target_pos[0] - dy = src_pos[1] - target_pos[1] - d = np.sqrt(dx**2 + dy**2) - - weight = nc_dict['A_weight'] * \ - np.exp(-(d**2) / (nc_dict['lamtha']**2)) + weight, _ = _get_gaussian_connection(src_pos, target_pos, nc_dict) connectivity_matrix[src_idx, target_idx] = weight From 21ab5397c683aa7630b81d217a0bf97298bc9df8 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Thu, 27 May 2021 17:35:09 -0400 Subject: [PATCH 08/23] doc fix --- hnn_core/cell.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hnn_core/cell.py b/hnn_core/cell.py index e6d883c79..cf153ec1b 100644 --- a/hnn_core/cell.py +++ b/hnn_core/cell.py @@ -26,7 +26,7 @@ def _get_cos_theta(p_secs, sec_name_apical): def _calculate_gaussian(x_val, height, lamtha): - """Return height of gaussian at x_val.and + """Return height of gaussian at x_val. Parameters ---------- From 9a60eb8e919592449c9e3f61964d29e321880781 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Thu, 27 May 2021 18:12:09 -0400 Subject: [PATCH 09/23] Fix delay calculation --- hnn_core/cell.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hnn_core/cell.py b/hnn_core/cell.py index cf153ec1b..f5464e1be 100644 --- a/hnn_core/cell.py +++ b/hnn_core/cell.py @@ -82,8 +82,8 @@ def _get_gaussian_connection(src_pos, target_pos, nc_dict): weight = _calculate_gaussian( cell_dist, nc_dict['A_weight'], nc_dict['lamtha']) - delay = _calculate_gaussian( - cell_dist, nc_dict['A_delay'], nc_dict['lamtha']) + delay = nc_dict['A_delay'] / _calculate_gaussian( + cell_dist, 1, nc_dict['lamtha']) return weight, delay From c63747865b18e2898fada22edb81f2efd36fa854 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Thu, 27 May 2021 21:11:22 -0400 Subject: [PATCH 10/23] Fix type checks --- hnn_core/viz.py | 60 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/hnn_core/viz.py b/hnn_core/viz.py index 528d1f194..872b03542 100644 --- a/hnn_core/viz.py +++ b/hnn_core/viz.py @@ -5,6 +5,7 @@ import numpy as np from itertools import cycle +from .externals.mne import _validate_type def _get_plot_data(dpl, layer, tmin, tmax): @@ -593,8 +594,8 @@ def plot_connectivity_weights(net, conn_idx, ax=None, show=True): from .network import Network from .cell import _get_gaussian_connection - if not isinstance(net, Network): - raise TypeError('conn must be instance of _Connectivity') + _validate_type(net, Network, 'net', 'Network') + _validate_type(conn_idx, int, 'conn_idx', 'int') if ax is None: _, ax = plt.subplots(1, 1) @@ -657,8 +658,7 @@ def plot_connectivity_matrix(conn, ax=None, show=True): import matplotlib.pyplot as plt from.network import _Connectivity - if not isinstance(conn, _Connectivity): - raise TypeError('conn must be instance of _Connectivity') + _validate_type(conn, _Connectivity, 'conn', '_Connectivity') if ax is None: _, ax = plt.subplots(1, 1) @@ -680,5 +680,57 @@ def plot_connectivity_matrix(conn, ax=None, show=True): ax.set_title(f"{conn['src_type']} -> {conn['target_type']} " f"({conn['loc']}, {conn['receptor']})") + plt_show(show) + return ax.get_figure() + + +def plot_cell_connectivity(net, conn_idx, gid, ax=None, show=True): + """Plot synaptic weight + + Parameters + ---------- + net : Instance of Network object + The Network object + conn_idx : int + Index of connection to be visualized + from `net.connectivity` + gid : int + Each cell in a network is uniquely identified by it's "global ID": GID. + ax : instance of Axes3D + Matplotlib 3D axis + show : bool + If True, show the plot + + Returns + ------- + fig : instance of matplotlib Figure + The matplotlib figure handle. + """ + import matplotlib.pyplot as plt + from .network import Network + from .cell import _get_gaussian_connection + + _validate_type(net, Network, 'net', 'Network') + _validate_type(conn_idx, int, 'conn_idx', 'int') + _validate_type(gid, int, 'gid', 'int') + if ax is None: + _, ax = plt.subplots(1, 1) + + # Load objects for distance calculation + conn = net.connectivity[conn_idx] + nc_dict = conn['nc_dict'] + src_type = conn['src_type'] + target_type = conn['target_type'] + src_type_pos = net.pos_dict[src_type] + target_type_pos = net.pos_dict[target_type] + + # Assumes min/max values occur in the first/last pos_dict items. + + src_range = np.array(conn['src_range']) + target_range = np.array(conn['target_range']) + connectivity_matrix = np.zeros((len(src_range), len(target_range))) + + + plt_show(show) return ax.get_figure() From cc56dfa622c91917913a513cdd2fa3ffc0007e3b Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Thu, 27 May 2021 22:26:15 -0400 Subject: [PATCH 11/23] WIP: Start single gid connectivity plot code --- hnn_core/viz.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/hnn_core/viz.py b/hnn_core/viz.py index 872b03542..2cee89b55 100644 --- a/hnn_core/viz.py +++ b/hnn_core/viz.py @@ -724,13 +724,15 @@ def plot_cell_connectivity(net, conn_idx, gid, ax=None, show=True): src_type_pos = net.pos_dict[src_type] target_type_pos = net.pos_dict[target_type] - # Assumes min/max values occur in the first/last pos_dict items. - + # Assumes max value occurs in the last pos_dict items. src_range = np.array(conn['src_range']) target_range = np.array(conn['target_range']) - connectivity_matrix = np.zeros((len(src_range), len(target_range))) + connectivity_matrix = np.zeros((target_type_pos[-1][1], + (target_type_pos[-1][0]))) + # src_idx = np.where() + # conn['gid_pairs'] - plt_show(show) - return ax.get_figure() + # plt_show(show) + # return ax.get_figure() From ceb1cfebb8fa6510e9e2b4d0086d9806cef19901 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Wed, 2 Jun 2021 17:02:31 -0400 Subject: [PATCH 12/23] Add cell weight connectivity plot --- examples/plot_connectivity.py | 15 ++++++++----- hnn_core/viz.py | 41 +++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/examples/plot_connectivity.py b/examples/plot_connectivity.py index 640e76de0..806809a01 100644 --- a/examples/plot_connectivity.py +++ b/examples/plot_connectivity.py @@ -39,15 +39,18 @@ # reflect the canonical neocortical microcircuit. ``net.connectivity`` # is a list of dictionaries which detail every cell-cell, and drive-cell # connection. The weights of these connections can be visualized with -# :func:`~hnn_core.viz.plot_connectivity_weights` -from hnn_core.viz import plot_connectivity_weights +# :func:`~hnn_core.viz.plot_connectivity_weights` as well as +# :func:`~hnn_core.viz.plot_cell_connectivity` +from hnn_core.viz import plot_connectivity_weights, plot_cell_connectivity print(len(net_erp.connectivity)) -print(net_erp.connectivity[15]) -plot_connectivity_weights(net_erp, conn_idx=15) +conn_idx = 15 +print(net_erp.connectivity[conn_idx]) +plot_connectivity_weights(net_erp, conn_idx) -print(net_erp.connectivity[20]) -plot_connectivity_weights(net_erp, conn_idx=20) +conn_idx, gid_idx = 20, 11 +src_gid = net_erp.connectivity[conn_idx]['src_range'][gid_idx] +fig, ax = plot_cell_connectivity(net_erp, conn_idx, src_gid) ############################################################################### # Data recorded during simulations are stored under diff --git a/hnn_core/viz.py b/hnn_core/viz.py index 2cee89b55..b9bbd36d2 100644 --- a/hnn_core/viz.py +++ b/hnn_core/viz.py @@ -684,8 +684,8 @@ def plot_connectivity_matrix(conn, ax=None, show=True): return ax.get_figure() -def plot_cell_connectivity(net, conn_idx, gid, ax=None, show=True): - """Plot synaptic weight +def plot_cell_connectivity(net, conn_idx, src_gid, ax=None, show=True): + """Plot synaptic weight of connections from a src_gid. Parameters ---------- @@ -694,7 +694,7 @@ def plot_cell_connectivity(net, conn_idx, gid, ax=None, show=True): conn_idx : int Index of connection to be visualized from `net.connectivity` - gid : int + src_gid : int Each cell in a network is uniquely identified by it's "global ID": GID. ax : instance of Axes3D Matplotlib 3D axis @@ -705,6 +705,8 @@ def plot_cell_connectivity(net, conn_idx, gid, ax=None, show=True): ------- fig : instance of matplotlib Figure The matplotlib figure handle. + im : Instance of matplotlib AxesImage + The matplotlib AxesImage handle. """ import matplotlib.pyplot as plt from .network import Network @@ -712,7 +714,7 @@ def plot_cell_connectivity(net, conn_idx, gid, ax=None, show=True): _validate_type(net, Network, 'net', 'Network') _validate_type(conn_idx, int, 'conn_idx', 'int') - _validate_type(gid, int, 'gid', 'int') + _validate_type(src_gid, int, 'src_gid', 'int') if ax is None: _, ax = plt.subplots(1, 1) @@ -724,15 +726,32 @@ def plot_cell_connectivity(net, conn_idx, gid, ax=None, show=True): src_type_pos = net.pos_dict[src_type] target_type_pos = net.pos_dict[target_type] - # Assumes max value occurs in the last pos_dict items. src_range = np.array(conn['src_range']) target_range = np.array(conn['target_range']) - connectivity_matrix = np.zeros((target_type_pos[-1][1], - (target_type_pos[-1][0]))) - # src_idx = np.where() + # Extract indeces to get position in network + # Index in gid range aligns with net.pos_dict + target_src_pair = conn['gid_pairs'][src_gid] + target_indeces = np.where(np.in1d(target_range, target_src_pair))[0] - # conn['gid_pairs'] + src_idx = np.where(src_range == src_gid)[0][0] + src_pos = src_type_pos[src_idx] - # plt_show(show) - # return ax.get_figure() + weights, x_pos, y_pos = list(), list(), list() + for target_idx in target_indeces: + target_pos = target_type_pos[target_idx] + x_pos.append(target_pos[0]) + y_pos.append(target_pos[1]) + weight, _ = _get_gaussian_connection(src_pos, target_pos, nc_dict) + weights.append(weight) + + ax.scatter(x_pos, y_pos, c=weights) + + ax.scatter(src_pos[0], src_pos[1], color='red', s=100) + ax.set_ylabel('Y Position') + ax.set_xlabel('X Position') + ax.set_title(f"{conn['src_type']} -> {conn['target_type']} " + f"({conn['loc']}, {conn['receptor']})") + + plt_show(show) + return ax.get_figure(), ax From 5e9184284e5ba0fbf90a5476a59d437cd3f7fde6 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Wed, 2 Jun 2021 17:14:26 -0400 Subject: [PATCH 13/23] Better title --- hnn_core/viz.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hnn_core/viz.py b/hnn_core/viz.py index b9bbd36d2..c6750e38a 100644 --- a/hnn_core/viz.py +++ b/hnn_core/viz.py @@ -750,8 +750,8 @@ def plot_cell_connectivity(net, conn_idx, src_gid, ax=None, show=True): ax.scatter(src_pos[0], src_pos[1], color='red', s=100) ax.set_ylabel('Y Position') ax.set_xlabel('X Position') - ax.set_title(f"{conn['src_type']} -> {conn['target_type']} " - f"({conn['loc']}, {conn['receptor']})") + ax.set_title(f"{conn['src_type']} (gid={src_gid}) -> {conn['target_type']}" + f" ({conn['loc']}, {conn['receptor']})") plt_show(show) return ax.get_figure(), ax From 69066a77ff1791bc17846049da492a4732c52847 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Thu, 3 Jun 2021 21:08:28 -0400 Subject: [PATCH 14/23] Remove redunant function --- examples/plot_connectivity.py | 19 ++++++----- hnn_core/network.py | 21 +----------- hnn_core/viz.py | 60 +++++++---------------------------- 3 files changed, 21 insertions(+), 79 deletions(-) diff --git a/examples/plot_connectivity.py b/examples/plot_connectivity.py index 806809a01..ca3aa41d4 100644 --- a/examples/plot_connectivity.py +++ b/examples/plot_connectivity.py @@ -41,14 +41,14 @@ # connection. The weights of these connections can be visualized with # :func:`~hnn_core.viz.plot_connectivity_weights` as well as # :func:`~hnn_core.viz.plot_cell_connectivity` -from hnn_core.viz import plot_connectivity_weights, plot_cell_connectivity +from hnn_core.viz import plot_connectivity_matrix, plot_cell_connectivity print(len(net_erp.connectivity)) -conn_idx = 15 +conn_idx = 20 print(net_erp.connectivity[conn_idx]) -plot_connectivity_weights(net_erp, conn_idx) +plot_connectivity_matrix(net_erp, conn_idx) -conn_idx, gid_idx = 20, 11 +gid_idx = 11 src_gid = net_erp.connectivity[conn_idx]['src_range'][gid_idx] fig, ax = plot_cell_connectivity(net_erp, conn_idx, src_gid) @@ -99,9 +99,7 @@ # activity is visible as vertical lines where several cells fire simultaneously # We can additionally use the ``probability``. argument to create a sparse # connectivity pattern instead of all-to-all. Let's try creating the same -# network with a 10% chance of cells connecting to each other. The resulting -# connectivity pattern can also be visualized with -# ``net.connectivity[idx].plot()`` +# network with a 10% chance of cells connecting to each other. probability = 0.1 net_sparse = default_network(params, add_drives_from_params=True) net_sparse.clear_connectivity() @@ -125,9 +123,10 @@ dpl_sparse = simulate_dipole(net_sparse, n_trials=1) net_sparse.cell_response.plot_spikes_raster() -net_sparse.connectivity[-2].plot() -net_sparse.connectivity[-1].plot() - +# Get index of most recently added connection +conn_idx = len(net_sparse.connectivity) +plot_connectivity_matrix(net_sparse, conn_idx, show_weight=False) +plot_connectivity_matrix(net_sparse, conn_idx - 1, show_weight=False) ############################################################################### # Using the sparse connectivity pattern produced a lot more spiking in diff --git a/hnn_core/network.py b/hnn_core/network.py index cb6ed0634..148ed1507 100644 --- a/hnn_core/network.py +++ b/hnn_core/network.py @@ -17,7 +17,7 @@ from .cells_default import pyramidal, basket from .cell_response import CellResponse from .params import _long_name, _short_name -from .viz import plot_cells, plot_connectivity_matrix +from .viz import plot_cells from .externals.mne import _validate_type, _check_option @@ -1268,25 +1268,6 @@ def __repr__(self): return entr - def plot(self, ax=None, show=True): - """Plot connectivity matrix for instance of _Connectivity object. - - Parameters - ---------- - ax : instance of matplotlib Axes3D | None - An axis object from matplotlib. If None, - a new figure is created. - show : bool - If True, show the figure. - - Returns - ------- - fig : instance of matplotlib Figure - The matplotlib figure handle. - """ - - return plot_connectivity_matrix(self, ax=ax, show=show) - class _NetworkDrive(dict): """A class for containing the parameters of external drives diff --git a/hnn_core/viz.py b/hnn_core/viz.py index c6750e38a..eda784633 100644 --- a/hnn_core/viz.py +++ b/hnn_core/viz.py @@ -570,7 +570,8 @@ def plot_cell_morphology(cell, ax, show=True): return ax -def plot_connectivity_weights(net, conn_idx, ax=None, show=True): +def plot_connectivity_matrix(net, conn_idx, show_weight=True, + ax=None, show=True): """Plot connectivity matrix with color bar for synaptic weights Parameters @@ -580,6 +581,9 @@ def plot_connectivity_weights(net, conn_idx, ax=None, show=True): conn_idx : int Index of connection to be visualized from `net.connectivity` + show_weight : bool + If True, visualize connectivity weights as gradient. + If False, each connection is visualized with a black square. ax : instance of Axes3D Matplotlib 3D axis show : bool @@ -596,6 +600,7 @@ def plot_connectivity_weights(net, conn_idx, ax=None, show=True): _validate_type(net, Network, 'net', 'Network') _validate_type(conn_idx, int, 'conn_idx', 'int') + _validate_type(show_weight, bool, 'show_weight', 'bool') if ax is None: _, ax = plt.subplots(1, 1) @@ -619,7 +624,11 @@ def plot_connectivity_weights(net, conn_idx, ax=None, show=True): target_pos = target_type_pos[target_idx] # Identical calculation used in Cell.par_connect_from_src() - weight, _ = _get_gaussian_connection(src_pos, target_pos, nc_dict) + if show_weight: + weight, _ = _get_gaussian_connection( + src_pos, target_pos, nc_dict) + else: + weight = 1.0 connectivity_matrix[src_idx, target_idx] = weight @@ -637,53 +646,6 @@ def plot_connectivity_weights(net, conn_idx, ax=None, show=True): return ax.get_figure() -def plot_connectivity_matrix(conn, ax=None, show=True): - """Plot connectivity matrix for instance of _Connectivity object. - - Parameters - ---------- - conn : Instance of _Connectivity object - The _Connectivity object - ax : instance of Axes3D - Matplotlib 3D axis - show : bool - If True, show the plot - - Returns - ------- - fig : instance of matplotlib Figure - The matplotlib figure handle. - - """ - import matplotlib.pyplot as plt - from.network import _Connectivity - - _validate_type(conn, _Connectivity, 'conn', '_Connectivity') - if ax is None: - _, ax = plt.subplots(1, 1) - - src_range = np.array(conn['src_range']) - target_range = np.array(conn['target_range']) - connectivity_matrix = np.zeros((len(src_range), len(target_range))) - for src_gid, target_src_pair in conn['gid_pairs'].items(): - src_idx = np.where(src_range == src_gid)[0][0] - target_indeces = np.in1d(target_range, target_src_pair) - connectivity_matrix[src_idx, :] = target_indeces - - ax.imshow(connectivity_matrix, cmap='Greys', interpolation='none') - ax.set_xlabel(f"{conn['target_type']} target gids " - f"({target_range[0]}-{target_range[-1]})") - ax.set_xticklabels(list()) - ax.set_ylabel(f"{conn['src_type']} source gids " - f"({src_range[0]}-{src_range[-1]})") - ax.set_yticklabels(list()) - ax.set_title(f"{conn['src_type']} -> {conn['target_type']} " - f"({conn['loc']}, {conn['receptor']})") - - plt_show(show) - return ax.get_figure() - - def plot_cell_connectivity(net, conn_idx, src_gid, ax=None, show=True): """Plot synaptic weight of connections from a src_gid. From 2f05e5e63510c07c5cb5ad14f805a61559078245 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Thu, 3 Jun 2021 21:20:18 -0400 Subject: [PATCH 15/23] Add simple type checks for plotting functions --- hnn_core/tests/test_viz.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/hnn_core/tests/test_viz.py b/hnn_core/tests/test_viz.py index 2f234c64a..fbb3c11e7 100644 --- a/hnn_core/tests/test_viz.py +++ b/hnn_core/tests/test_viz.py @@ -7,6 +7,7 @@ import hnn_core from hnn_core import read_params, default_network from hnn_core.viz import plot_cells, plot_dipole, plot_psd, plot_tfr_morlet +from hnn_core.viz import plot_connectivity_matrix, plot_cell_connectivity from hnn_core.dipole import simulate_dipole matplotlib.use('agg') @@ -24,6 +25,26 @@ def test_network_visualization(): ax = net.cell_types['L2_pyramidal'].plot_morphology() assert len(ax.lines) == 8 + conn_idx = 0 + with pytest.raises(TypeError, match='net must be an instance of'): + plot_connectivity_matrix('blah', conn_idx, show_weight=False) + + with pytest.raises(TypeError, match='conn_idx must be an instance of'): + plot_connectivity_matrix(net, 'blah', show_weight=False) + + with pytest.raises(TypeError, match='show_weight must be an instance of'): + plot_connectivity_matrix(net, conn_idx, show_weight='blah') + + src_gid = 5 + with pytest.raises(TypeError, match='net must be an instance of'): + plot_cell_connectivity('blah', conn_idx, src_gid=src_gid) + + with pytest.raises(TypeError, match='conn_idx must be an instance of'): + plot_cell_connectivity(net, 'blah', src_gid=src_gid) + + with pytest.raises(TypeError, match='src_gid must be an instance of'): + plot_cell_connectivity(net, conn_idx, src_gid='blah') + def test_dipole_visualization(): """Test dipole visualisations.""" From 498377ffbaad45fd914dd9cb07775caa1cac0e4d Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Thu, 3 Jun 2021 21:45:58 -0400 Subject: [PATCH 16/23] WIP: Start adding colorbar options --- hnn_core/viz.py | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/hnn_core/viz.py b/hnn_core/viz.py index eda784633..6a50e4c3a 100644 --- a/hnn_core/viz.py +++ b/hnn_core/viz.py @@ -571,7 +571,7 @@ def plot_cell_morphology(cell, ax, show=True): def plot_connectivity_matrix(net, conn_idx, show_weight=True, - ax=None, show=True): + colorbar=True, ax=None, show=True): """Plot connectivity matrix with color bar for synaptic weights Parameters @@ -595,6 +595,7 @@ def plot_connectivity_matrix(net, conn_idx, show_weight=True, The matplotlib figure handle. """ import matplotlib.pyplot as plt + from matplotlib.ticker import ScalarFormatter from .network import Network from .cell import _get_gaussian_connection @@ -632,7 +633,19 @@ def plot_connectivity_matrix(net, conn_idx, show_weight=True, connectivity_matrix[src_idx, target_idx] = weight - ax.imshow(connectivity_matrix, cmap='Greys', interpolation='none') + im = ax.imshow(connectivity_matrix, cmap='Greys', interpolation='none') + + ax.set_xlabel('Time (ms)') + ax.set_ylabel('Frequency (Hz)') + + if colorbar: + fig = ax.get_figure() + xfmt = ScalarFormatter() + xfmt.set_powerlimits((-2, 2)) + cbar = fig.colorbar(im, ax=ax, format=xfmt) + cbar.ax.yaxis.set_ticks_position('right') + cbar.ax.set_ylabel('Weight', rotation=-90, va="bottom") + ax.set_xlabel(f"{conn['target_type']} target gids " f"({target_range[0]}-{target_range[-1]})") ax.set_xticklabels(list()) @@ -642,11 +655,13 @@ def plot_connectivity_matrix(net, conn_idx, show_weight=True, ax.set_title(f"{conn['src_type']} -> {conn['target_type']} " f"({conn['loc']}, {conn['receptor']})") + plt.tight_layout() plt_show(show) return ax.get_figure() -def plot_cell_connectivity(net, conn_idx, src_gid, ax=None, show=True): +def plot_cell_connectivity(net, conn_idx, src_gid, colorbar=True, + ax=None, show=True): """Plot synaptic weight of connections from a src_gid. Parameters @@ -667,12 +682,12 @@ def plot_cell_connectivity(net, conn_idx, src_gid, ax=None, show=True): ------- fig : instance of matplotlib Figure The matplotlib figure handle. - im : Instance of matplotlib AxesImage - The matplotlib AxesImage handle. + """ import matplotlib.pyplot as plt from .network import Network from .cell import _get_gaussian_connection + from matplotlib.ticker import ScalarFormatter _validate_type(net, Network, 'net', 'Network') _validate_type(conn_idx, int, 'conn_idx', 'int') @@ -707,13 +722,22 @@ def plot_cell_connectivity(net, conn_idx, src_gid, ax=None, show=True): weight, _ = _get_gaussian_connection(src_pos, target_pos, nc_dict) weights.append(weight) - ax.scatter(x_pos, y_pos, c=weights) + im = ax.scatter(x_pos, y_pos, c=weights) ax.scatter(src_pos[0], src_pos[1], color='red', s=100) ax.set_ylabel('Y Position') ax.set_xlabel('X Position') - ax.set_title(f"{conn['src_type']} (gid={src_gid}) -> {conn['target_type']}" + ax.set_title(f"{conn['src_type']}-> {conn['target_type']}" f" ({conn['loc']}, {conn['receptor']})") + if colorbar: + fig = ax.get_figure() + xfmt = ScalarFormatter() + xfmt.set_powerlimits((-2, 2)) + cbar = fig.colorbar(im, ax=ax, format=xfmt) + cbar.ax.yaxis.set_ticks_position('right') + cbar.ax.set_ylabel('Weight', rotation=-90, va="bottom") + + plt.tight_layout() plt_show(show) return ax.get_figure(), ax From b295434d17ced23cb022e7b49bf23a8e3bff95b0 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Fri, 4 Jun 2021 11:42:05 -0400 Subject: [PATCH 17/23] Better viz and docs --- hnn_core/viz.py | 59 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/hnn_core/viz.py b/hnn_core/viz.py index 6a50e4c3a..4becb06f7 100644 --- a/hnn_core/viz.py +++ b/hnn_core/viz.py @@ -570,8 +570,9 @@ def plot_cell_morphology(cell, ax, show=True): return ax -def plot_connectivity_matrix(net, conn_idx, show_weight=True, - colorbar=True, ax=None, show=True): +def plot_connectivity_matrix(net, conn_idx, ax=None, show_weight=True, + colorbar=True, colormap='Greys', + show=True): """Plot connectivity matrix with color bar for synaptic weights Parameters @@ -581,11 +582,15 @@ def plot_connectivity_matrix(net, conn_idx, show_weight=True, conn_idx : int Index of connection to be visualized from `net.connectivity` - show_weight : bool - If True, visualize connectivity weights as gradient. - If False, each connection is visualized with a black square. ax : instance of Axes3D Matplotlib 3D axis + show_weight : bool + If True, visualize connectivity weights as gradient. + If False, all weights set to constant value. + colormap : str + The name of a matplotlib colormap. Default: 'Greys' + colorbar : bool + If True (default), adjust figure to include colorbar. show : bool If True, show the plot @@ -633,7 +638,7 @@ def plot_connectivity_matrix(net, conn_idx, show_weight=True, connectivity_matrix[src_idx, target_idx] = weight - im = ax.imshow(connectivity_matrix, cmap='Greys', interpolation='none') + im = ax.imshow(connectivity_matrix, cmap=colormap, interpolation='none') ax.set_xlabel('Time (ms)') ax.set_ylabel('Frequency (Hz)') @@ -660,9 +665,9 @@ def plot_connectivity_matrix(net, conn_idx, show_weight=True, return ax.get_figure() -def plot_cell_connectivity(net, conn_idx, src_gid, colorbar=True, - ax=None, show=True): - """Plot synaptic weight of connections from a src_gid. +def plot_cell_connectivity(net, conn_idx, src_gid, ax=None, colorbar=True, + colormap='viridis', show=True): + """Plot synaptic weight of connections originating from src_gid. Parameters ---------- @@ -675,6 +680,10 @@ def plot_cell_connectivity(net, conn_idx, src_gid, colorbar=True, Each cell in a network is uniquely identified by it's "global ID": GID. ax : instance of Axes3D Matplotlib 3D axis + colormap : str + The name of a matplotlib colormap. Default: 'viridis' + colorbar : bool + If True (default), adjust figure to include colorbar. show : bool If True, show the plot @@ -683,6 +692,15 @@ def plot_cell_connectivity(net, conn_idx, src_gid, colorbar=True, fig : instance of matplotlib Figure The matplotlib figure handle. + Notes + ----- + Target cells will be determined by the connection class given by + net.connectivity[conn_idx]. + If the target cell is not connected to src_gid, it will appear as a + smaller black circle. + src_gid is plotted as a red circle. src_gid will not be plotted if + the connection corresponds to a drive, ex: poisson, bursty, etc. + """ import matplotlib.pyplot as plt from .network import Network @@ -704,6 +722,11 @@ def plot_cell_connectivity(net, conn_idx, src_gid, colorbar=True, target_type_pos = net.pos_dict[target_type] src_range = np.array(conn['src_range']) + if src_gid not in src_range: + raise ValueError(f'src_gid not in the src type range of {src_type} ' + f'gids. Valid gids include {src_range[0]} -> ' + f'{src_range[-1]}') + target_range = np.array(conn['target_range']) # Extract indeces to get position in network @@ -714,17 +737,25 @@ def plot_cell_connectivity(net, conn_idx, src_gid, colorbar=True, src_idx = np.where(src_range == src_gid)[0][0] src_pos = src_type_pos[src_idx] - weights, x_pos, y_pos = list(), list(), list() + # Aggregate positions and weight of each connected target + weights, target_x_pos, target_y_pos = list(), list(), list() for target_idx in target_indeces: target_pos = target_type_pos[target_idx] - x_pos.append(target_pos[0]) - y_pos.append(target_pos[1]) + target_x_pos.append(target_pos[0]) + target_y_pos.append(target_pos[1]) weight, _ = _get_gaussian_connection(src_pos, target_pos, nc_dict) weights.append(weight) - im = ax.scatter(x_pos, y_pos, c=weights) + im = ax.scatter(target_x_pos, target_y_pos, c=weights, cmap=colormap) + + # Gather positions of all gids in target_type. + x_pos = [target_type_pos[idx][0] for idx in range(len(target_type_pos))] + y_pos = [target_type_pos[idx][1] for idx in range(len(target_type_pos))] + ax.scatter(x_pos, y_pos, color='k', zorder=-1, s=4) - ax.scatter(src_pos[0], src_pos[1], color='red', s=100) + # Only plot src_gid if proper cell type. + if src_type in net.cell_types: + ax.scatter(src_pos[0], src_pos[1], color='red', s=100) ax.set_ylabel('Y Position') ax.set_xlabel('X Position') ax.set_title(f"{conn['src_type']}-> {conn['target_type']}" From cf7584c5e4da378ecdd74e274f01acef0d43b9d5 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Fri, 4 Jun 2021 11:42:23 -0400 Subject: [PATCH 18/23] Smoke tests --- hnn_core/tests/test_viz.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/hnn_core/tests/test_viz.py b/hnn_core/tests/test_viz.py index fbb3c11e7..6c27db717 100644 --- a/hnn_core/tests/test_viz.py +++ b/hnn_core/tests/test_viz.py @@ -26,6 +26,7 @@ def test_network_visualization(): assert len(ax.lines) == 8 conn_idx = 0 + plot_connectivity_matrix(net, conn_idx, show=False) with pytest.raises(TypeError, match='net must be an instance of'): plot_connectivity_matrix('blah', conn_idx, show_weight=False) @@ -36,15 +37,19 @@ def test_network_visualization(): plot_connectivity_matrix(net, conn_idx, show_weight='blah') src_gid = 5 + plot_cell_connectivity(net, conn_idx, src_gid, show=False) with pytest.raises(TypeError, match='net must be an instance of'): plot_cell_connectivity('blah', conn_idx, src_gid=src_gid) with pytest.raises(TypeError, match='conn_idx must be an instance of'): - plot_cell_connectivity(net, 'blah', src_gid=src_gid) + plot_cell_connectivity(net, 'blah', src_gid) with pytest.raises(TypeError, match='src_gid must be an instance of'): plot_cell_connectivity(net, conn_idx, src_gid='blah') + with pytest.raises(ValueError, match='src_gid not in the'): + plot_cell_connectivity(net, conn_idx, src_gid=-1) + def test_dipole_visualization(): """Test dipole visualisations.""" From 4f952119adee77d0282738954403b7a0970a384d Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Fri, 4 Jun 2021 12:01:53 -0400 Subject: [PATCH 19/23] Update examples --- examples/plot_connectivity.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/examples/plot_connectivity.py b/examples/plot_connectivity.py index ca3aa41d4..8f15aa0bd 100644 --- a/examples/plot_connectivity.py +++ b/examples/plot_connectivity.py @@ -123,10 +123,16 @@ dpl_sparse = simulate_dipole(net_sparse, n_trials=1) net_sparse.cell_response.plot_spikes_raster() -# Get index of most recently added connection -conn_idx = len(net_sparse.connectivity) -plot_connectivity_matrix(net_sparse, conn_idx, show_weight=False) -plot_connectivity_matrix(net_sparse, conn_idx - 1, show_weight=False) +# Get index of most recently added connection, and a src_gid in src_range. +conn_idx, gid_idx = len(net_sparse.connectivity) - 1, 5 +src_gid = net_erp.connectivity[conn_idx]['src_range'][gid_idx] +plot_connectivity_matrix(net_sparse, conn_idx) +plot_cell_connectivity(net_sparse, conn_idx, src_gid) + +conn_idx, gid_idx = len(net_sparse.connectivity) - 2, 5 +src_gid = net_erp.connectivity[conn_idx]['src_range'][gid_idx] +plot_connectivity_matrix(net_sparse, conn_idx) +plot_cell_connectivity(net_sparse, conn_idx, src_gid) ############################################################################### # Using the sparse connectivity pattern produced a lot more spiking in From 0633e7ffe88c6ecdb3476c15995fed7118df8c45 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Fri, 4 Jun 2021 12:14:43 -0400 Subject: [PATCH 20/23] Fix example, update what_new.rst --- doc/whats_new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whats_new.rst b/doc/whats_new.rst index de686b49e..9ad3bf26a 100644 --- a/doc/whats_new.rst +++ b/doc/whats_new.rst @@ -24,6 +24,8 @@ Changelog - Add probability argument to :func:`~hnn_core.Network.add_connection`. Connectivity patterns can also be visualized with :func:`~hnn_core.viz.plot_connectivity_matrix`, by `Nick Tolley`_ in `#318 `_ +- Add function to visualize connections originating from individual cells :func:`~hnn_core.viz.plot_cell_connectivity`, by `Nick Tolley`_ in `#339 `_ + Bug ~~~ From 40d00849604ddbc7f3fe869351529cc78406080c Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Fri, 4 Jun 2021 12:39:14 -0400 Subject: [PATCH 21/23] example fix --- examples/plot_connectivity.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/plot_connectivity.py b/examples/plot_connectivity.py index 8f15aa0bd..c2b90f258 100644 --- a/examples/plot_connectivity.py +++ b/examples/plot_connectivity.py @@ -125,12 +125,12 @@ # Get index of most recently added connection, and a src_gid in src_range. conn_idx, gid_idx = len(net_sparse.connectivity) - 1, 5 -src_gid = net_erp.connectivity[conn_idx]['src_range'][gid_idx] +src_gid = net_sparse.connectivity[conn_idx]['src_range'][gid_idx] plot_connectivity_matrix(net_sparse, conn_idx) plot_cell_connectivity(net_sparse, conn_idx, src_gid) conn_idx, gid_idx = len(net_sparse.connectivity) - 2, 5 -src_gid = net_erp.connectivity[conn_idx]['src_range'][gid_idx] +src_gid = net_sparse.connectivity[conn_idx]['src_range'][gid_idx] plot_connectivity_matrix(net_sparse, conn_idx) plot_cell_connectivity(net_sparse, conn_idx, src_gid) From 3e5981ac142d5fa293d2ba029254ba6ec2f035aa Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Fri, 4 Jun 2021 18:48:44 -0400 Subject: [PATCH 22/23] Update docs, modify plot markers --- doc/whats_new.rst | 4 ++-- examples/plot_connectivity.py | 2 +- hnn_core/viz.py | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/whats_new.rst b/doc/whats_new.rst index 9ad3bf26a..9cb5ef4a2 100644 --- a/doc/whats_new.rst +++ b/doc/whats_new.rst @@ -24,8 +24,6 @@ Changelog - Add probability argument to :func:`~hnn_core.Network.add_connection`. Connectivity patterns can also be visualized with :func:`~hnn_core.viz.plot_connectivity_matrix`, by `Nick Tolley`_ in `#318 `_ -- Add function to visualize connections originating from individual cells :func:`~hnn_core.viz.plot_cell_connectivity`, by `Nick Tolley`_ in `#339 `_ - Bug ~~~ @@ -45,6 +43,8 @@ API - New API for network creation. The default network is now created with ``net = default_network(params)``, by `Nick Tolley`_ in `#318 `_ +- Add API for visualizing connections originating from individual cells :func:`~hnn_core.viz.plot_cell_connectivity`, by `Nick Tolley`_ in `#339 `_ + .. _0.1: 0.1 diff --git a/examples/plot_connectivity.py b/examples/plot_connectivity.py index c2b90f258..29884999b 100644 --- a/examples/plot_connectivity.py +++ b/examples/plot_connectivity.py @@ -8,7 +8,7 @@ # Author: Nick Tolley -# sphinx_gallery_thumbnail_number = 1 +# sphinx_gallery_thumbnail_number = 2 import os.path as op diff --git a/hnn_core/viz.py b/hnn_core/viz.py index 4becb06f7..fe1e15c07 100644 --- a/hnn_core/viz.py +++ b/hnn_core/viz.py @@ -746,16 +746,16 @@ def plot_cell_connectivity(net, conn_idx, src_gid, ax=None, colorbar=True, weight, _ = _get_gaussian_connection(src_pos, target_pos, nc_dict) weights.append(weight) - im = ax.scatter(target_x_pos, target_y_pos, c=weights, cmap=colormap) + im = ax.scatter(target_x_pos, target_y_pos, c=weights, s=50, cmap=colormap) # Gather positions of all gids in target_type. x_pos = [target_type_pos[idx][0] for idx in range(len(target_type_pos))] y_pos = [target_type_pos[idx][1] for idx in range(len(target_type_pos))] - ax.scatter(x_pos, y_pos, color='k', zorder=-1, s=4) + ax.scatter(x_pos, y_pos, color='k', marker='x', zorder=-1, s=20) # Only plot src_gid if proper cell type. if src_type in net.cell_types: - ax.scatter(src_pos[0], src_pos[1], color='red', s=100) + ax.scatter(src_pos[0], src_pos[1], marker='s', color='red', s=150) ax.set_ylabel('Y Position') ax.set_xlabel('X Position') ax.set_title(f"{conn['src_type']}-> {conn['target_type']}" From 9d1d93c2e33d8524cbc8350313ab7929a02c2d73 Mon Sep 17 00:00:00 2001 From: Nick Tolley Date: Fri, 4 Jun 2021 18:51:06 -0400 Subject: [PATCH 23/23] Update api.rst --- doc/api.rst | 2 ++ doc/whats_new.rst | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/api.rst b/doc/api.rst index 075bab4e1..650d19552 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -46,6 +46,8 @@ Visualization (:py:mod:`hnn_core.viz`): plot_cell_morphology plot_psd plot_tfr_morlet + plot_cell_connectivity + plot_connectivity_matrix Parallel backends (:py:mod:`hnn_core.parallel_backends`): --------------------------------------------------------- diff --git a/doc/whats_new.rst b/doc/whats_new.rst index 9cb5ef4a2..9ad3bf26a 100644 --- a/doc/whats_new.rst +++ b/doc/whats_new.rst @@ -24,6 +24,8 @@ Changelog - Add probability argument to :func:`~hnn_core.Network.add_connection`. Connectivity patterns can also be visualized with :func:`~hnn_core.viz.plot_connectivity_matrix`, by `Nick Tolley`_ in `#318 `_ +- Add function to visualize connections originating from individual cells :func:`~hnn_core.viz.plot_cell_connectivity`, by `Nick Tolley`_ in `#339 `_ + Bug ~~~ @@ -43,8 +45,6 @@ API - New API for network creation. The default network is now created with ``net = default_network(params)``, by `Nick Tolley`_ in `#318 `_ -- Add API for visualizing connections originating from individual cells :func:`~hnn_core.viz.plot_cell_connectivity`, by `Nick Tolley`_ in `#339 `_ - .. _0.1: 0.1