The helpers module

helpers.bin_time_series(array, binsize, gaussian=True, **kwargs)

Helper function for windowing the time series into smaller chunks.

Parameters
  • array (2_D array) – n x t matrix where n is the number of neuorons and t is the length of the time series.

  • binsize (int) – Size of each window. This number needs to be smaller than t and a positive divider of t.

  • gaussian (bool (Default: True)) – If True, each spike is going to be multiplied by a 1-D gaussian of length sigma.

  • **kwargs

    sigma: float

    Size of the gaussian. See gaussian_filter.

Returns

A – Matrix of size l x n x t where l is the number of windows(=t/binsize), n is number of neurons and t is the length of the time series.

Return type

array

helpers.binarize(array, thresh=None)

Function for binarizing adjacency matrices.

Parameters
  • array (array like) – Cross-correlation matrix.

  • thresh (float (Default: None)) – If None, entries that are non-zero are going to be set to 1. If a value between [0,1] is given, then every entry smaller than thresh will be set to 0 and 1 otherwise.

Returns

binary_spikes – Binarized cross-correlation matrix of same size.

Return type

array like

helpers.community_consensus_iterative(C)

Function finding the consensus on the given set of partitions. See the paper:

‘Robust detection of dynamic community structure in networks’, Danielle S. Bassett, Mason A. Porter, Nicholas F. Wymbs, Scott T. Grafton, Jean M. Carlson et al.

We apply Leiden algorithm to maximize modularity.

Parameters

C (array) – Matrix of size parameter_space x (length * size) where each row is the community assignment of the corresponding parameters.

Returns

partition – See https://leidenalg.readthedocs.io/en/stable/

Return type

Leidenalg object

helpers.consensus_display(partition, n, t)

Helper function to visualize the consensus from community_consensus_iterative.

Parameters
Returns

  • comms (array) – Community membership array of size n x t.

  • cmap (matplotlib object) – Colormap used to plot the membership information.

  • color (list) – List of strings encoding the colors of the communities.

helpers.create_time_series(operation, community_sizes, spiking_rates, spy=True, windowsize=1000, k=5)

Main function for creating spike trains using Homogeneous Poisson Process.

Parameters
  • operation (grow, contract, merge or transient) – Community operation.

  • community_sizes (list or list of lists) – If the operation is grow (or contract), this should be a list indicating the number of neurons joining (or leaving from) the main community. If the operation is merge or transient, then this should be a list of lists([list1,list2,…]) where each list contains sizes of the communities in that layer.

  • spike_rates (list or list of lists) – This should be of same size and shape as community_sizes indicating the spike rates of the corresponding communities.

  • spy (bool (Deafult: True)) – Displays the time series if True.

  • windowsize (int (Default: 1000)) – Length of the window size for a new layer of events to be created.

  • k (int (Default: 5)) – Constant for jittering the spikes when creating new communities.

Returns

spikes – Matrix of size n x t where n is the number of neuorons and t is the length of the time series.

Return type

array

helpers.cross_correlation_matrix(data)

Main function to call for computing cross-correlation matrix of a time series.

Parameters

data (array) – n x t matrix where n is the number of neuorons and t is the length of the time series.

Returns

  • X_full (array) – n x n symmetric cross-correlation matrix.

  • X (array) – n x n upper triangular cross-correlation matrix.

  • lag (array) – n x n lag matrix.

helpers.display_truth(comm_sizes, community_operation, ax=None)

Function for displaying the ground truths.

Parameters
  • comm_sizes (list, or list of lists) – This will be passed to generate_ground_truth where pad is True by default.

  • community_operation (grow, contract, merge or transient) – Type of the community event which will also be passed to generate_ground_truth.

  • ax (matplotlib object (Default: None)) – If None, a new axis will be created, otherwise the ground truth will be plotted to the provided axis.

helpers.find_repeated(l)

Helper function for generating transient communities.

helpers.gaussian_filter(array, sigma)

Function that multiplies vectors with a gaussian.

Parameters
  • array (1_D array like) – Input vector.

  • sigma (float) – 1 spike turns into 3 non-zero spikes(one at each side of smaller magnitude) with sigma=0.25. 1 spike turns into 5 non-zero spikes(two at each side of smaller magnitude) with sigma=0.50. 1 spike turns into 9 non-zero spikes(four at each side of smaller magnitude) with sigma=1, and so on..

Returns

array – Gaussian vector.

Return type

1_D array like

helpers.generate_ground_truth(comm_sizes, method='scattered', pad=False, community_operation='grow')

Main function that generates ground truth labels for the experiments. Community labels according to two methods one in which the rest of the network except the planted communities are scattered i.e. they all have their own community or they are all in one community, integrated.

Parameters
  • comm_sizes (list, or list of lists) – If the community_operation is grow (or contract), this should be a list indicating the number of neurons joining (or leaving from) the main community. If the community_operation is merge or transient, then this should be a list of lists([list1,list2,…]) where each list contains sizes of the communities in that layer. For example, [[6,1,1,1,1],[6,4]] indicates a 6 neuron community in the first layer and additional 4 neurons, that are independently firing, merges into 1 community in the second layer.

  • method (scattered or integrated (Default: ‘scattered’)) – If the community_operation is grow (or contract), two types of ground truths can be prepared. Integrated is the one where independently firing neurons are grouped together into one single community and scattered is the one with independently firing neurons.

  • pad (bool (Default: False)) – If True, the truth will be padded from the beginning and the end by the exact same community membership.

  • community_operation (grow, contract, merge or transient (Default: ‘grow’)) – Type of community events that are available. Community expansion, community contraction, community merge and transient communities.

Returns

truth_labels – List of truth labels, of length n*t where n is the number of neuorons and t is the number of layers. If pad, length will be n*(t+2).

Return type

list

helpers.generate_transient(comm_per_layer)

Helper function for creating the time series for the transient communities.

Parameters

comm_per_layer (list of lists) – List of lists of length number of layers where each list contains the number of communities at that layer.

Returns

  • comm_sizes (list of lists) – Sizes of the communities in the corresponding layers which will be passed to create_time_series.

  • spike_rate (list of lists) – Randomly selected corresponding spike rates for the Homogeneous Poisson process that generates spike trains.

  • num_neurons (int) – Number of neurons.

helpers.getOverlap(a, b)

Helper function for generating transient communities. Finds repeated indices.

helpers.get_repeated_indices(l)

Helper function for generating transient communities.

helpers.information_recovery(pred_labels, comm_size, truth, interlayers, other_parameter, com_op)

Function for calculating the quality of the resulting partitions on a parameter plane and visualizes the quality landscape according to NMI, ARI and F1-Score.

Parameters
  • pred_labels (list) – List of truth labels appended in the order of layers. This should be the same length as the output of generate_ground_truth.

  • comm_size (list, or list of lists) – This will be passed to generate_ground_truth that assumes pad to be True.

  • truth (integrated or scattered) – Same as in generate_ground_truth.

  • interlayers (1_D array like) – To get the landscape information on a plane of parameters, we pass two array like object. This one is the y-axis one on the result.

  • other_parameter (1_D array like) – This is the x-axis array for quality.

  • com_op (grow, contract, merge or transient) – Same as in generate_ground_truth.

Returns

  • fig (matplotlib object) – Figure object for the plots.

  • ax (matplotlib object) – Axis objects for the plots.

helpers.jitter(spike, k)

Function for randomly jittering spikes when generating communities.

Parameters
  • spike (array) – Spike train to be jittered.

  • k (int) – Number of time frames, to the right or to the left, for a spike to be jittered.

Returns

jittered – Jittered spike train.

Return type

array

helpers.max_norm_cross_corr(x1, x2)

Function for computing maximum cross-correlation.

Parameters
  • x1 (1_D array like) – First vector.

  • x2 (1_D array like) – Second vector.

Returns

  • max_corr (int) – Maximum cross-correlation between the two input vectors.

  • lag (int) – Lag difference where the maximum cross-correlation occurs.

helpers.normalized_cross_corr(x, y)

Function to compute normalized cross-correlation between two vectors.

Parameters
  • x (1_D array like) – First vector.

  • y (1_D array like) – Second vector.

Returns

corr_array – Correlation array between x and y.

Return type

array

helpers.space_comms(comm_size)

Helper function for spacing the communities randomly for the transient communities.

helpers.spike_count(spikes, ax, num_bins=None, t_min=None, t_max=None)

Helper function to visualize the distribution of the number of spikes in a given spike train.

Parameters
  • spikes (array) – Spike train matrix of size n x t.

  • ax (matplotlib axis) – Axis for distribution to be plotted.

  • num_bins (int (Default: None)) – If None, this will be the difference between maximum and minimum number of spikes in a population.

  • t_min (int (Default: None)) – if None, this will be 0, otherwise spikes will be counted in the given range.

  • t_max (int (Default: None)) – if None, this will be t, otherwise spikes will be counted in the given range.

Returns

  • n (array) – The values of the histogram bins.

  • bins (array) – The edges of the bins.

helpers.threshold(array, thresh)

Function for thresholding the adjacency matrices.

Parameters
  • array (array like) – Cross-correlation matrix.

  • thresh (float) – Value in which every entry smaller than thresh will be set to 0 and entries greater than thresh will stay the same.

Returns

threhsolded_array – Thresholded cross-correlation matrix of same size.

Return type

array like