Skip to content

Commit eaeeb45

Browse files
committed
BF: tests and unambiguous conditions order
If this is not done, condition order for orthogonalizarion is unpredictable.
1 parent ce65afb commit eaeeb45

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

nipype/algorithms/modelgen.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from builtins import range, str, bytes, int
1717

1818
from copy import deepcopy
19+
import csv
1920
import os
2021

2122
from nibabel import load
@@ -145,7 +146,7 @@ def scale_timings(timelist, input_units, output_units, time_repetition):
145146
return timelist
146147

147148
def bids_gen_info(bids_event_files,
148-
condition_column='trial_type',
149+
condition_column='',
149150
amplitude_column=None,
150151
time_repetition=False,
151152
):
@@ -175,7 +176,11 @@ def bids_gen_info(bids_event_files,
175176
with open(bids_event_file) as f:
176177
f_events = csv.DictReader(f, skipinitialspace=True, delimiter='\t')
177178
events = [{k: v for k, v in row.items()} for row in f_events]
178-
conditions = list(set([i[condition_column] for i in events]))
179+
if not condition_column:
180+
condition_column = '_trial_type'
181+
for i in events:
182+
i.update({condition_column:'ev0'})
183+
conditions = sorted(set([i[condition_column] for i in events]))
179184
runinfo = Bunch(conditions=[], onsets=[], durations=[], amplitudes=[])
180185
for condition in conditions:
181186
selected_events = [i for i in events if i[condition_column]==condition]
@@ -185,10 +190,7 @@ def bids_gen_info(bids_event_files,
185190
decimals = math.ceil(-math.log10(time_repetition))
186191
onsets = [np.round(i, decimals) for i in onsets]
187192
durations = [np.round(i ,decimals) for i in durations]
188-
if condition:
189-
runinfo.conditions.append(condition)
190-
else:
191-
runinfo.conditions.append('e0')
193+
runinfo.conditions.append(condition)
192194
runinfo.onsets.append(onsets)
193195
runinfo.durations.append(durations)
194196
try:
@@ -199,7 +201,6 @@ def bids_gen_info(bids_event_files,
199201
info.append(runinfo)
200202
return info
201203

202-
203204
def gen_info(run_event_files):
204205
"""Generate subject_info structure from a list of event files
205206
"""

nipype/algorithms/tests/test_modelgen.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@
1212
import pytest
1313
import numpy.testing as npt
1414
from nipype.interfaces.base import Bunch, TraitError
15-
from nipype.algorithms.modelgen import (SpecifyModel, SpecifySparseModel,
16-
SpecifySPMModel)
15+
from nipype.algorithms.modelgen import (bids_gen_info, SpecifyModel,
16+
SpecifySparseModel, SpecifySPMModel)
1717

1818

19+
def test_bids_gen_info():
20+
fname = os.path.realpath('data/events.tsv')
21+
res = bids_gen_info([fname])
22+
assert res[0].onsets == [[183.75, 313.75, 483.75, 633.75, 783.75, 933.75, 1083.75, 1233.75]]
23+
assert res[0].durations == [[20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0]]
24+
assert res[0].amplitudes ==[[1, 1, 1, 1, 1, 1, 1, 1]]
25+
assert res[0].conditions == ['ev0']
26+
1927
def test_modelgen1(tmpdir):
2028
filename1 = tmpdir.join('test1.nii').strpath
2129
filename2 = tmpdir.join('test2.nii').strpath

nipype/testing/data/events.tsv

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
onset duration frequency pulse_width amplitude
2+
183.75 20.0 20.0 0.005 1.0
3+
313.75 20.0 20.0 0.005 1.0
4+
483.75 20.0 20.0 0.005 1.0
5+
633.75 20.0 20.0 0.005 1.0
6+
783.75 20.0 20.0 0.005 1.0
7+
933.75 20.0 20.0 0.005 1.0
8+
1083.75 20.0 20.0 0.005 1.0
9+
1233.75 20.0 20.0 0.005 1.0

0 commit comments

Comments
 (0)