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

Propose less elegant but more specific comparison #8

Merged
merged 7 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 19 additions & 10 deletions phys2bids/physio_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,9 @@ def auto_trigger_selection(self):
"""
Find a trigger index matching the channels with a regular expresion.

It compares the channel name with the the regular expressions stored
in TRIGGER_NAMES.

Parameters
----------
self
Expand All @@ -576,16 +579,22 @@ def auto_trigger_selection(self):
Automatically retrieved trigger index
"""
LGR.info('Running automatic trigger detection.')
no_numbers = [re.sub(r'\d+', '', x) for x in self.ch_name]
results = [re.search('|'.join(re.split(r'(\W)', case)),
'§'.join(TRIGGER_NAMES), re.IGNORECASE) for case in no_numbers]
indexes = [i for i, v in enumerate(results) if v]
if len(indexes) == 1:
self.trigger_idx = indexes[0]
LGR.info(f'{self.ch_name[self.trigger_idx]} selected as trigger channel')
elif len(indexes) > 1:
raise Exception('More than one possible trigger channel was automatically found. '
'Please run phys2bids specifying the -chtrig argument.')
joint_match = '§'.join(TRIGGER_NAMES)
indexes = []
for n, case in enumerate(self.ch_name):
name = re.split(r'(\W+|\d|_|\s)', case)
name = list(filter(None, name))

if re.search('|'.join(name), joint_match, re.IGNORECASE):
indexes = indexes + [n]

if indexes:
if len(indexes) > 1:
raise Exception('More than one possible trigger channel was automatically found. '
'Please run phys2bids specifying the -chtrig argument.')
else:
self.trigger_idx = indexes[0]
LGR.info(f'{self.ch_name[self.trigger_idx]} selected as trigger channel')
else:
raise Exception('No trigger channel automatically found. Please run phys2bids '
'specifying the -chtrig argument.')
Expand Down
12 changes: 6 additions & 6 deletions phys2bids/tests/test_physio_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ def test_auto_trigger_selection(caplog):
test_trigger = np.array([0, 1, 2, 3, 4])
test_half = np.array([0, 1, 2])
test_twice = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
test_timeseries = [test_time, test_trigger, test_half, test_twice]
test_freq = [1, 1, 0.5, 2]
test_chn_name = ['time', 'trigger', 'half', 'CO2']
test_units = ['s', 'V', 'V', 'V']
test_timeseries = [test_time, test_trigger, test_half, test_twice, test_twice, test_twice]
test_freq = [1, 1, 0.5, 2, 2, 2]
test_chn_name = ['time', 'trigger', 'half', 'CO2', 'CO 2', 'strigose']
test_units = ['s', 'V', 'V', 'V', 'V', 'V']

# test when trigger is not the name of the channel
test_chtrig = 2
Expand All @@ -284,13 +284,13 @@ def test_auto_trigger_selection(caplog):
test_units, test_chtrig)
assert phys_in.trigger_idx == 1
# test when no trigger is found
test_chn_name = ['time', 'dummy', 'half', 'CO2']
test_chn_name = ['time', 'TRIGGAH', 'half', 'CO2', 'CO 2', 'strigose']
with raises(Exception) as errorinfo:
phys_in = po.BlueprintInput(test_timeseries, test_freq, test_chn_name,
test_units, test_chtrig)
assert 'No trigger channel automatically found' in str(errorinfo.value)
# test when no trigger is found
test_chn_name = ['time', 'trigger', 'TRIGGER', 'CO2']
test_chn_name = ['time', 'trigger', 'TRIGGER', 'CO2', 'CO 2', 'strigose']
with raises(Exception) as errorinfo:
phys_in = po.BlueprintInput(test_timeseries, test_freq, test_chn_name,
test_units, test_chtrig)
Expand Down