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

[Bug fix] Fix bug of synchrotool index out of bounds #612

Closed
Closed
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
a8f15bd
change index shift to avoid error when synchrofacts occur in the last…
morales-gregorio Dec 26, 2023
3995a83
Undo changes, this is the wrong place for such a fix, since the error…
morales-gregorio Feb 5, 2024
3613ac3
Fix last bin error from Jonas and Sven
morales-gregorio Feb 5, 2024
74dc07f
fix erroneous indentation
morales-gregorio Apr 3, 2024
9ae6e5b
Merge branch 'master' of github.com:NeuralEnsemble/elephant into sync…
morales-gregorio Apr 3, 2024
f7c91b0
simplify binning error test
morales-gregorio Apr 4, 2024
d7846f4
add test for synchrofact in last bin where indexing error used to occur
morales-gregorio Apr 4, 2024
7293669
Alternative method to avoid indexing error in last bin
morales-gregorio Apr 4, 2024
ee383b8
fix new error for metadata update with spiketrainlist
morales-gregorio Apr 4, 2024
c638628
make t_stop extension only when necessary, include note to docstring
morales-gregorio Apr 4, 2024
2ff0015
remove warning
morales-gregorio Apr 4, 2024
51c02e7
undo spiketrainlist problem
morales-gregorio Apr 4, 2024
3ec7b21
newer neo is required for synchrotool indexing not to fail or have a …
morales-gregorio Apr 4, 2024
728c698
ensure t_stop of original spiketrains is conserved
morales-gregorio Apr 4, 2024
3dc224f
fix PEP8 issue
morales-gregorio Apr 4, 2024
876e83b
Merge branch 'master' of github.com:NeuralEnsemble/elephant into sync…
morales-gregorio Apr 4, 2024
c2be5c5
update neo requirements also in txt
morales-gregorio Apr 25, 2024
cc58286
Update elephant/statistics.py
morales-gregorio Apr 25, 2024
a945178
Merge branch 'synchrotool_fix' of github.com:morales-gregorio/elephan…
morales-gregorio Apr 25, 2024
02e6219
Merge branch 'master' of github.com:NeuralEnsemble/elephant into sync…
morales-gregorio Apr 25, 2024
3c25002
update author affiliations
morales-gregorio Apr 25, 2024
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
2 changes: 2 additions & 0 deletions doc/authors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ contribution, and may not be the current affiliation of a contributor.
* Florian Porrmann [13]
* Sarah Pilz [13]
* Oliver Kloß [1]
* Jonas Oberste-Frielinghaus [1]
* Sven Krausse [1]
Moritz-Alexander-Kern marked this conversation as resolved.
Show resolved Hide resolved
* Felician Richter [12]

1. Institute of Neuroscience and Medicine (INM-6) and Institute for Advanced Simulation (IAS-6) and JARA-Institute Brain Structure-Function Relationships (INM-10), Jülich Research Centre, Jülich, Germany
Expand Down
15 changes: 15 additions & 0 deletions elephant/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,11 @@ class Complexity(object):
bin edge into the following bin. This can be adjusted using the tolerance
parameter and turned off by setting `tolerance=None`.

Due to the rounding error correction an indexing error would occur if
spikes were in the last bin. To avoid the t_stop of the original spike
trains is modified to add one more bin in the cases where a spike is found
at the last time bin.

See also
--------
elephant.conversion.BinnedSpikeTrain
Expand Down Expand Up @@ -1445,6 +1450,15 @@ def __init__(self, spiketrains,
if bin_size is None and sampling_rate is not None:
self.bin_size = 1 / self.sampling_rate

# Check if spikes happen in the last bin
for st in self.input_spiketrains:
# Extend t_stop to avoid indexing problems
if np.isclose(self.t_stop.magnitude, st.times[-1].magnitude):
self.t_stop += self.bin_size
for st in self.input_spiketrains:
st.t_stop = self.t_stop
break

if spread == 0:
self.time_histogram, self.complexity_histogram = \
self._histogram_no_spread()
Expand Down Expand Up @@ -1555,6 +1569,7 @@ def _epoch_no_spread(self):
if left_edges[0] < self.t_start:
left_edges[0] = self.t_start
durations[0] -= bin_shift

Moritz-Alexander-Kern marked this conversation as resolved.
Show resolved Hide resolved
morales-gregorio marked this conversation as resolved.
Show resolved Hide resolved
else:
warnings.warn('No sampling rate specified. '
'Note that using the complexity epoch to get '
Expand Down
37 changes: 32 additions & 5 deletions elephant/test/test_spike_train_synchrony.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ def _test_template(self, spiketrains, correct_complexities, sampling_rate,
spread, deletion_threshold=2, mode='delete',
in_place=False, binary=True):

intial_t_stop = spiketrains[0].t_stop.magnitude

synchrofact_obj = Synchrotool(
spiketrains,
sampling_rate=sampling_rate,
Expand Down Expand Up @@ -233,6 +235,9 @@ def _test_template(self, spiketrains, correct_complexities, sampling_rate,
cleaned_spike_times):
assert_array_almost_equal(cleaned_st, correct_st)

assert_array_almost_equal(spiketrains[0].t_stop.magnitude,
intial_t_stop)

def test_no_synchrofacts(self):

# nothing to find here
Expand Down Expand Up @@ -390,16 +395,16 @@ def test_binning_for_input_with_rounding_errors(self):

sampling_rate = 30000 / pq.s

spiketrains = [neo.SpikeTrain(np.arange(1000) * pq.s / 30000,
spiketrains = [neo.SpikeTrain(np.arange(10) * pq.s / 30000,
t_stop=.1 * pq.s),
neo.SpikeTrain(np.arange(2000, step=2) * pq.s / 30000,
neo.SpikeTrain(np.arange(20, step=2) * pq.s / 30000,
t_stop=.1 * pq.s)]

first_annotations = np.ones(1000)
first_annotations = np.ones(10)
first_annotations[::2] = 2

second_annotations = np.ones(1000)
second_annotations[:500] = 2
second_annotations = np.ones(10)
second_annotations[:5] = 2

correct_annotations = np.array([first_annotations,
second_annotations])
Expand All @@ -408,6 +413,28 @@ def test_binning_for_input_with_rounding_errors(self):
spread=0, mode='delete', in_place=True,
deletion_threshold=2)

def test_binning_indexing_last_bin_synchrofact(self):

# a test with inputs divided by 30000 which leads to rounding errors
# these errors have to be accounted for by proper binning;
# check if we still get the correct result
# If there is a synchrofact in the last bin there was an indexing
# error due to the rounding error correction

sampling_rate = 30000 / pq.s

st = neo.SpikeTrain(np.arange(10) * pq.s / 30000, t_stop=.1 * pq.s)

spiketrains = [st, st]

annotations = 2*np.ones(10)

correct_annotations = np.array([annotations, annotations])

self._test_template(spiketrains, correct_annotations, sampling_rate,
spread=0, mode='delete', in_place=True,
deletion_threshold=2)

def test_correct_transfer_of_spiketrain_attributes(self):

# for delete=True the spiketrains in the block are changed,
Expand Down
2 changes: 1 addition & 1 deletion requirements/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ dependencies:
- statsmodels
- jinja2
- pip:
- neo>=0.10.0
- neo>=0.13.0
Moritz-Alexander-Kern marked this conversation as resolved.
Show resolved Hide resolved
- viziphant
# neo, viziphant can be removed once it is integrated into requirements-tutorials.txt
Loading