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

Account for unidirectional spiketrain->segment links in synchrofact deletion #398

Merged
merged 4 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
51 changes: 28 additions & 23 deletions elephant/spike_train_synchrony.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""
from __future__ import division, print_function, unicode_literals

import warnings
from collections import namedtuple
from copy import deepcopy

Expand Down Expand Up @@ -341,33 +342,37 @@ def delete_synchrofacts(self, threshold, in_place=False, mode='delete'):
if mode == 'extract':
mask = np.invert(mask)
new_st = st[mask]
spiketrain_list[idx] = new_st
if in_place:
if in_place and st.segment is not None:
segment = st.segment
if segment is None:
continue

# replace link to spiketrain in segment
new_index = self._get_spiketrain_index(
segment.spiketrains, st)
segment.spiketrains[new_index] = new_st
try:
# replace link to spiketrain in segment
new_index = self._get_spiketrain_index(
segment.spiketrains, st)
segment.spiketrains[new_index] = new_st
except ValueError:
# st is not in this segment even though it points to it
warnings.warn(f"The SpikeTrain at index {idx} of the "
"input list spiketrains has a "
"unidirectional uplink to a segment in "
"whose segment.spiketrains list it does not "
"appear.")
dizcza marked this conversation as resolved.
Show resolved Hide resolved

block = segment.block
if block is None:
continue

# replace link to spiketrain in groups
for group in block.groups:
try:
idx = self._get_spiketrain_index(
group.spiketrains,
st)
except ValueError:
# st is not in this group, move to next group
continue

# st found in group, replace with new_st
group.spiketrains[idx] = new_st
if block is not None:
# replace link to spiketrain in groups
for group in block.groups:
try:
idx = self._get_spiketrain_index(
group.spiketrains,
st)
except ValueError:
# st is not in this group, move to next group
continue

# st found in group, replace with new_st
group.spiketrains[idx] = new_st
dizcza marked this conversation as resolved.
Show resolved Hide resolved
spiketrain_list[idx] = new_st

return spiketrain_list

Expand Down
53 changes: 53 additions & 0 deletions elephant/test/test_spike_train_synchrony.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,59 @@ def test_spread_0(self):
spread=0, mode='delete', in_place=True,
deletion_threshold=2)

def test_spiketrains_findable(self):

# same test as `test_spread_0` with the addition of
# a neo structure: we must not overwrite the spiketrain
# list of the segment before determining the index

sampling_rate = 1 / pq.s

segment = neo.Segment()

segment.spiketrains = [neo.SpikeTrain([1, 5, 9, 11, 16, 19] * pq.s,
t_stop=20*pq.s),
neo.SpikeTrain([1, 4, 8, 12, 16, 18] * pq.s,
t_stop=20*pq.s)]

segment.create_relationship()

correct_annotations = np.array([[2, 1, 1, 1, 2, 1],
[2, 1, 1, 1, 2, 1]])

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

def test_unidirectional_uplinks(self):

# same test as `test_spiketrains_findable` but the spiketrains
# are rescaled first
# the rescaled spiketrains have a unidirectional uplink to segment
# check that this does not cause an error
# check that a UserWarning is issued in this case

sampling_rate = 1 / pq.s

segment = neo.Segment()

segment.spiketrains = [neo.SpikeTrain([1, 5, 9, 11, 16, 19] * pq.s,
t_stop=20*pq.s),
neo.SpikeTrain([1, 4, 8, 12, 16, 18] * pq.s,
t_stop=20*pq.s)]

segment.create_relationship()

spiketrains = [st.rescale(pq.s) for st in segment.spiketrains]

correct_annotations = np.array([[2, 1, 1, 1, 2, 1],
[2, 1, 1, 1, 2, 1]])

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

def test_spread_1(self):

# test synchrofact search taking into account adjacent bins
Expand Down