Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] add include_t_stop flag to Synchrotool class, Issue 493 #637

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
718637a
add include_t_stop flag and implement False behavior
Moritz-Alexander-Kern Jul 16, 2024
9606903
add tests
Moritz-Alexander-Kern Jul 16, 2024
52fae9c
fix pep8
Moritz-Alexander-Kern Jul 16, 2024
f2e84b1
add parameter to docstring
Moritz-Alexander-Kern Jul 17, 2024
0ec2354
Merge branch 'master' into fix/index_error_synchrotool_493
Moritz-Alexander-Kern Jul 17, 2024
59f283a
implement changes
Moritz-Alexander-Kern Jul 25, 2024
805ad79
update docstring
Moritz-Alexander-Kern Jul 25, 2024
025e22b
add flag for tests
Moritz-Alexander-Kern Jul 25, 2024
05f0414
fix docstring
Moritz-Alexander-Kern Jul 25, 2024
a2d2a93
change t_stop back
Moritz-Alexander-Kern Jul 26, 2024
a02a0d8
add new flag ignore_shared_time to BinnedSpiketrain
Moritz-Alexander-Kern Aug 9, 2024
b0537b7
make use of new flag in Complexity class no_spread
Moritz-Alexander-Kern Aug 9, 2024
7f83ba7
remove unnecessary import
Moritz-Alexander-Kern Aug 9, 2024
a62f302
readd type check of for annotations
Moritz-Alexander-Kern Aug 9, 2024
88db22e
no need to pass parameter in other tests
Moritz-Alexander-Kern Aug 9, 2024
70b9e9e
pass t_start to time_histogram
Moritz-Alexander-Kern Aug 28, 2024
118d3fb
update docstring for ignore_shared_time
Moritz-Alexander-Kern Aug 28, 2024
f1e0202
add tests for ignore_shared_time_interval
Moritz-Alexander-Kern Aug 28, 2024
7d821f7
fix doctest
Moritz-Alexander-Kern Aug 28, 2024
3779ff5
fix pep8
Moritz-Alexander-Kern Aug 28, 2024
a65c14f
typo
Moritz-Alexander-Kern Sep 18, 2024
54e7e8c
edit typo
Moritz-Alexander-Kern Sep 18, 2024
6284e79
edit gitignore
Moritz-Alexander-Kern Sep 18, 2024
b2e32ab
add test checking correct binning
Moritz-Alexander-Kern Sep 30, 2024
1bc8752
Merge branch 'master' into fix/index_error_synchrotool_493
Moritz-Alexander-Kern Nov 14, 2024
589b276
Merge branch 'master' into fix/index_error_synchrotool_493
Moritz-Alexander-Kern Jan 8, 2025
935db4b
Merge branch 'master' into fix/index_error_synchrotool_493
Moritz-Alexander-Kern Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
implement changes
  • Loading branch information
Moritz-Alexander-Kern committed Jul 25, 2024
commit 59f283a7346d29c86f029b7c77cf33210f4ed117
54 changes: 20 additions & 34 deletions elephant/spike_train_synchrony.py
Original file line number Diff line number Diff line change
@@ -261,7 +261,7 @@ class Synchrotool(Complexity):
If True, the end of the spike train (`t_stop`) is included in the
analysis, ensuring that any spikes close to `t_stop` are properly
annotated.
Default is False.
Default is True.


See also
@@ -276,28 +276,19 @@ def __init__(self, spiketrains,
binary=True,
spread=0,
tolerance=1e-8,
include_t_stop=False):
include_t_stop=True):

self.annotated = False
self.include_t_stop = include_t_stop

super(Synchrotool, self).__init__(spiketrains=spiketrains,
bin_size=bin_size,
sampling_rate=sampling_rate,
binary=binary,
spread=spread,
tolerance=tolerance)

if self.include_t_stop:
self.t_stop += self.bin_size
if spread == 0:
self.time_histogram, self.complexity_histogram = \
self._histogram_no_spread()
self.epoch = self._epoch_no_spread()
else:
self.epoch = self._epoch_with_spread()
self.time_histogram, self.complexity_histogram = \
self._histogram_with_spread()

super(Synchrotool, self).__init__(
spiketrains=spiketrains,
bin_size=bin_size,
sampling_rate=sampling_rate,
binary=binary,
spread=spread,
tolerance=tolerance,
t_stop=spiketrains[0].t_stop + (1 / sampling_rate) if include_t_stop else None,
)

def delete_synchrofacts(self, threshold, in_place=False, mode='delete'):
"""
@@ -427,26 +418,21 @@ def annotate_synchrofacts(self):
self.epoch.times.units).magnitude.flatten()
)

for idx, st in enumerate(self.input_spiketrains):
for st in self.input_spiketrains:

# all indices of spikes that are within the half-open intervals
# defined by the boundaries
# note that every second entry in boundaries is an upper boundary
spike_to_epoch_idx = np.searchsorted(
right_edges,
st.times.rescale(self.epoch.times.units).magnitude.flatten())

# Initialize complexity_per_spike with NaNs
complexity_per_spike = np.full(spike_to_epoch_idx.shape, np.nan)
# Iterate through spike_to_epoch_idx and assign values or NaN
for i, idx in enumerate(spike_to_epoch_idx):
if 0 <= idx < len(epoch_complexities):
complexity_per_spike[i] = epoch_complexities[idx]
else:
warnings.warn(
"Some spikes in the input Spike Train are too close to t_stop and will be annotated with NaN."
"Consider setting include_t_stop=True in the Synchrotool class to address this."
)
try:
complexity_per_spike = epoch_complexities[spike_to_epoch_idx]
except IndexError:
raise ValueError(
"Some spikes in the input Spike Train may be too close or right at t_stop, they can not be binned "
"and therefore are not annotated. "
"Consider setting include_t_stop=True in the Synchrotool class to address this.")

st.array_annotate(complexity=complexity_per_spike)

11 changes: 8 additions & 3 deletions elephant/statistics.py
Original file line number Diff line number Diff line change
@@ -1423,7 +1423,10 @@ def __init__(self, spiketrains,
bin_size=None,
binary=True,
spread=0,
tolerance=1e-8):
tolerance=1e-8,
t_start=None,
t_stop=None,
):

check_neo_consistency(spiketrains, object_type=neo.SpikeTrain)

@@ -1434,8 +1437,10 @@ def __init__(self, spiketrains,
raise ValueError('Spread must be >=0')

self.input_spiketrains = spiketrains
self.t_start = spiketrains[0].t_start
self.t_stop = spiketrains[0].t_stop
self.t_start = spiketrains[0].t_start if t_start is None else t_start
self.t_stop = spiketrains[0].t_stop if t_stop is None else t_stop
for st in self.input_spiketrains:
st.t_stop = self.t_stop
self.sampling_rate = sampling_rate
self.bin_size = bin_size
self.binary = binary
20 changes: 3 additions & 17 deletions elephant/test/test_spike_train_synchrony.py
Original file line number Diff line number Diff line change
@@ -493,25 +493,12 @@ def test_regression_PR_612_index_out_of_bounds_raise_warning(self):
sampling_rate = 1/pq.ms
st = neo.SpikeTrain(np.arange(0, 11)*pq.ms, t_start=0*pq.ms, t_stop=10*pq.ms)

synchrotool_instance = Synchrotool([st, st], sampling_rate, spread=0)
synchrotool_instance = Synchrotool([st, st], sampling_rate, spread=0, include_t_stop=False)

with self.assertWarns(UserWarning) as cm:
with self.assertRaises(ValueError):
synchrotool_instance.annotate_synchrofacts()

self.assertIn("Some spikes in the input Spike Train are too close to t_stop", str(cm.warning))

def test_regression_PR_612_index_out_of_bounds_annotate_nan(self):
"""
https://github.com/NeuralEnsemble/elephant/pull/612
"""
sampling_rate = 1/pq.ms
st = neo.SpikeTrain(np.arange(0, 11)*pq.ms, t_start=0*pq.ms, t_stop=10*pq.ms)

synchrotool_instance = Synchrotool([st, st], sampling_rate, spread=0)
synchrotool_instance.annotate_synchrofacts()
self.assertTrue(np.isnan(st.array_annotations['complexity'][-1]))

def test_regression_PR_612_index_out_of_bounds_annotate_include_t_stop(self):
def test_regression_PR_612_index_out_of_bounds(self):
"""
https://github.com/NeuralEnsemble/elephant/pull/612
"""
@@ -520,7 +507,6 @@ def test_regression_PR_612_index_out_of_bounds_annotate_include_t_stop(self):

synchrotool_instance = Synchrotool([st, st], sampling_rate, spread=0, include_t_stop=True)
synchrotool_instance.annotate_synchrofacts()
self.assertFalse(np.isnan(st.array_annotations['complexity'][-1])) # non NaN
self.assertEqual(len(st.array_annotations['complexity']), len(st)) # all spikes annotated


Loading