From b9ff01fa1fdb55274d93c724ecc019cf04f42555 Mon Sep 17 00:00:00 2001 From: Moritz Kern <92092328+Moritz-Alexander-Kern@users.noreply.github.com> Date: Wed, 10 Nov 2021 16:33:43 +0100 Subject: [PATCH 1/3] Update spike_train_correlation.py Improvement of Spike Time Tiling Coefficient (STTC) calculation by utilizing numpy function. Details: -use numpy.where to find overlapping tiles in run_T -some comments elaborated/changed -changed name of time_A to time_max for maximum time covered by tiles This enhancement was inspired by Thierry Nieus code: https://github.com/thierrynieus/Spike-Time-Tiling-Coefficient- --- elephant/spike_train_correlation.py | 40 +++++++++++++++++------------ 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/elephant/spike_train_correlation.py b/elephant/spike_train_correlation.py index 3f5d21736..b48c743a8 100644 --- a/elephant/spike_train_correlation.py +++ b/elephant/spike_train_correlation.py @@ -924,29 +924,37 @@ def run_T(spiketrain): Calculate the proportion of the total recording time 'tiled' by spikes. """ N = len(spiketrain) - time_A = 2 * N * dt # maximum possible time + time_max = 2 * N * dt # maximum possible time - if N == 1: # for just one spike in train + if N == 1: # for only a single spike in the train + + # Check difference between start of recording and single spike if spiketrain[0] - spiketrain.t_start < dt: - time_A += -dt + spiketrain[0] - spiketrain.t_start - if spiketrain[0] + dt > spiketrain.t_stop: - time_A += -dt - spiketrain[0] + spiketrain.t_stop - else: # if more than one spike in train - # Vectorized loop of spike time differences + time_max = time_max - dt + spiketrain[0] - spiketrain.t_start + + # Check difference between single spike and end of recording + elif spiketrain[0] + dt > spiketrain.t_stop: + time_max = time_max - dt - spiketrain[0] + spiketrain.t_stop + + else: # if more than a single spike in the train + + # Calculate difference between consecutive spikes diff = np.diff(spiketrain) - diff_overlap = diff[diff < 2 * dt] - # Subtract overlap - time_A += -2 * dt * len(diff_overlap) + np.sum(diff_overlap) - # check if spikes are within dt of the start and/or end - # if so subtract overlap of first and/or last spike - if (spiketrain[0] - spiketrain.t_start) < dt: - time_A += spiketrain[0] - dt - spiketrain.t_start + # Find spikes whose tiles overlap + idx = np.where(diff < 2 * dt)[0] + # Subtract overlapping "2*dt" tiles and add differences instead + time_max = time_max - 2 * dt * len(idx) + diff[idx].sum() + # Check if spikes are within +/-dt of the start and/or end + # if so, subtract overlap of first and/or last spike + if (spiketrain[0] - spiketrain.t_start) < dt: + time_max = time_max + spiketrain[0] - dt - spiketrain.t_start if (spiketrain.t_stop - spiketrain[N - 1]) < dt: - time_A += -spiketrain[-1] - dt + spiketrain.t_stop + time_max = time_max - spiketrain[-1] - dt + spiketrain.t_stop - T = time_A / (spiketrain.t_stop - spiketrain.t_start) + # Calculate the proportion of total recorded time to "tiled" time + T = time_max / (spiketrain.t_stop - spiketrain.t_start) return T.simplified.item() # enforce simplification, strip units N1 = len(spiketrain_i) From e45d1a7fee5c896e905cb0c7bd9d483078334a6b Mon Sep 17 00:00:00 2001 From: Moritz Kern <92092328+Moritz-Alexander-Kern@users.noreply.github.com> Date: Tue, 23 Nov 2021 11:00:13 +0100 Subject: [PATCH 2/3] - changed back variable names (#85) --- elephant/spike_train_correlation.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/elephant/spike_train_correlation.py b/elephant/spike_train_correlation.py index b48c743a8..f68f4a7c5 100644 --- a/elephant/spike_train_correlation.py +++ b/elephant/spike_train_correlation.py @@ -924,17 +924,17 @@ def run_T(spiketrain): Calculate the proportion of the total recording time 'tiled' by spikes. """ N = len(spiketrain) - time_max = 2 * N * dt # maximum possible time + time_A = 2 * N * dt # maximum possible time if N == 1: # for only a single spike in the train # Check difference between start of recording and single spike if spiketrain[0] - spiketrain.t_start < dt: - time_max = time_max - dt + spiketrain[0] - spiketrain.t_start + time_A += spiketrain[0] - spiketrain.t_start # Check difference between single spike and end of recording elif spiketrain[0] + dt > spiketrain.t_stop: - time_max = time_max - dt - spiketrain[0] + spiketrain.t_stop + time_A += - dt - spiketrain[0] + spiketrain.t_stop else: # if more than a single spike in the train @@ -944,17 +944,17 @@ def run_T(spiketrain): # Find spikes whose tiles overlap idx = np.where(diff < 2 * dt)[0] # Subtract overlapping "2*dt" tiles and add differences instead - time_max = time_max - 2 * dt * len(idx) + diff[idx].sum() + time_A += - 2 * dt * len(idx) + diff[idx].sum() # Check if spikes are within +/-dt of the start and/or end # if so, subtract overlap of first and/or last spike if (spiketrain[0] - spiketrain.t_start) < dt: - time_max = time_max + spiketrain[0] - dt - spiketrain.t_start + time_A += spiketrain[0] - dt - spiketrain.t_start if (spiketrain.t_stop - spiketrain[N - 1]) < dt: - time_max = time_max - spiketrain[-1] - dt + spiketrain.t_stop + time_A += - spiketrain[-1] - dt + spiketrain.t_stop # Calculate the proportion of total recorded time to "tiled" time - T = time_max / (spiketrain.t_stop - spiketrain.t_start) + T = time_A / (spiketrain.t_stop - spiketrain.t_start) return T.simplified.item() # enforce simplification, strip units N1 = len(spiketrain_i) @@ -1074,4 +1074,4 @@ def spike_train_timescale(binned_spiketrain, max_tau): # Calculate the timescale using trapezoidal integration integr = (corrfct / corrfct[0]) ** 2 timescale = 2 * integrate.trapz(integr, dx=bin_size) - return pq.Quantity(timescale, units=binned_spiketrain.units, copy=False) + return pq.Quantity(timescale, units=binned_spiketrain.units, copy=False) \ No newline at end of file From 95d09b6b909e048580141734b45557ea7c9c0d32 Mon Sep 17 00:00:00 2001 From: Moritz-Alexander-Kern Date: Thu, 17 Feb 2022 10:33:02 +0100 Subject: [PATCH 3/3] fixed mistake introduced earlier --- elephant/spike_train_correlation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/elephant/spike_train_correlation.py b/elephant/spike_train_correlation.py index f68f4a7c5..780112637 100644 --- a/elephant/spike_train_correlation.py +++ b/elephant/spike_train_correlation.py @@ -930,7 +930,7 @@ def run_T(spiketrain): # Check difference between start of recording and single spike if spiketrain[0] - spiketrain.t_start < dt: - time_A += spiketrain[0] - spiketrain.t_start + time_A += - dt + spiketrain[0] - spiketrain.t_start # Check difference between single spike and end of recording elif spiketrain[0] + dt > spiketrain.t_stop: @@ -1074,4 +1074,4 @@ def spike_train_timescale(binned_spiketrain, max_tau): # Calculate the timescale using trapezoidal integration integr = (corrfct / corrfct[0]) ** 2 timescale = 2 * integrate.trapz(integr, dx=bin_size) - return pq.Quantity(timescale, units=binned_spiketrain.units, copy=False) \ No newline at end of file + return pq.Quantity(timescale, units=binned_spiketrain.units, copy=False)