Skip to content

Commit ea96591

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

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

nipype/algorithms/modelgen.py

+9-6
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
):
@@ -171,11 +172,17 @@ def bids_gen_info(bids_event_files,
171172
list of Bunch
172173
"""
173174
info = []
175+
info = []
174176
for bids_event_file in bids_event_files:
175177
with open(bids_event_file) as f:
176178
f_events = csv.DictReader(f, skipinitialspace=True, delimiter='\t')
177179
events = [{k: v for k, v in row.items()} for row in f_events]
180+
if not condition_column:
181+
condition_column = '_trial_type'
182+
for i in events:
183+
i.update({condition_column:'ev0'})
178184
conditions = list(set([i[condition_column] for i in events]))
185+
conditions = sorted(conditions)
179186
runinfo = Bunch(conditions=[], onsets=[], durations=[], amplitudes=[])
180187
for condition in conditions:
181188
selected_events = [i for i in events if i[condition_column]==condition]
@@ -185,10 +192,7 @@ def bids_gen_info(bids_event_files,
185192
decimals = math.ceil(-math.log10(time_repetition))
186193
onsets = [np.round(i, decimals) for i in onsets]
187194
durations = [np.round(i ,decimals) for i in durations]
188-
if condition:
189-
runinfo.conditions.append(condition)
190-
else:
191-
runinfo.conditions.append('e0')
195+
runinfo.conditions.append(condition)
192196
runinfo.onsets.append(onsets)
193197
runinfo.durations.append(durations)
194198
try:
@@ -199,7 +203,6 @@ def bids_gen_info(bids_event_files,
199203
info.append(runinfo)
200204
return info
201205

202-
203206
def gen_info(run_event_files):
204207
"""Generate subject_info structure from a list of event files
205208
"""

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 = '../../testing/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)