Skip to content

ENH: Support for BIDS event files #2845

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

Merged
merged 1 commit into from
Jan 21, 2019
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
81 changes: 80 additions & 1 deletion nipype/algorithms/modelgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,61 @@ def scale_timings(timelist, input_units, output_units, time_repetition):
timelist = [np.max([0., _scalefactor * t]) for t in timelist]
return timelist

def bids_gen_info(bids_event_files,
condition_column='trial_type',
amplitude_column=None,
time_repetition=False,
):
"""Generate subject_info structure from a list of BIDS .tsv event files.

Parameters
----------

bids_event_files : list of str
Filenames of BIDS .tsv event files containing columns including:
'onset', 'duration', and 'trial_type' or the `condition_column` value.
condition_column : str
Column of files in `bids_event_files` based on the values of which
events will be sorted into different regressors
amplitude_column : str
Column of files in `bids_event_files` based on the values of which
to apply amplitudes to events. If unspecified, all events will be
represented with an amplitude of 1.

Returns
-------

list of Bunch
"""
info = []
for bids_event_file in bids_event_files:
with open(bids_event_file) as f:
f_events = csv.DictReader(f, skipinitialspace=True, delimiter='\t')
events = [{k: v for k, v in row.items()} for row in f_events]
conditions = list(set([i[condition_column] for i in events]))
runinfo = Bunch(conditions=[], onsets=[], durations=[], amplitudes=[])
for condition in conditions:
selected_events = [i for i in events if i[condition_column]==condition]
onsets = [float(i['onset']) for i in selected_events]
durations = [float(i['duration']) for i in selected_events]
if time_repetition:
decimals = math.ceil(-math.log10(time_repetition))
onsets = [np.round(i, decimals) for i in onsets]
durations = [np.round(i ,decimals) for i in durations]
if condition:
runinfo.conditions.append(condition)
else:
runinfo.conditions.append('e0')
runinfo.onsets.append(onsets)
runinfo.durations.append(durations)
try:
amplitudes = [float(i[amplitude_column]) for i in selected_events]
runinfo.amplitudes.append(amplitudes)
except KeyError:
runinfo.amplitudes.append([1] * len(onsets))
info.append(runinfo)
return info


def gen_info(run_event_files):
"""Generate subject_info structure from a list of event files
Expand Down Expand Up @@ -190,6 +245,23 @@ class SpecifyModelInputSpec(BaseInterfaceInputSpec):
desc='List of event description files 1, 2 or 3 '
'column format corresponding to onsets, '
'durations and amplitudes')
bids_event_file = InputMultiPath(
File(exists=True),
mandatory=True,
xor=['subject_info', 'event_files', 'bids_event_file'],
desc='TSV event file containing common BIDS fields: `onset`,'
'`duration`, and categorization and amplitude columns')
bids_condition_column = traits.Str(exists=True,
mandatory=False,
default_value='trial_type',
usedefault=True,
desc='Column of the file passed to `bids_event_file` to the '
'unique values of which events will be assigned'
'to regressors')
bids_amplitude_column = traits.Str(exists=True,
mandatory=False,
desc='Column of the file passed to `bids_event_file` '
'according to which to assign amplitudes to events')
realignment_parameters = InputMultiPath(
File(exists=True),
desc='Realignment parameters returned '
Expand Down Expand Up @@ -432,8 +504,15 @@ def _generate_design(self, infolist=None):
if infolist is None:
if isdefined(self.inputs.subject_info):
infolist = self.inputs.subject_info
else:
elif isdefined(self.inputs.event_files):
infolist = gen_info(self.inputs.event_files)
elif isdefined(self.inputs.bids_event_file):
infolist = bids_gen_info(
self.inputs.bids_event_file,
self.inputs.bids_condition_column,
self.inputs.bids_amplitude_column,
self.inputs.time_repetition,
)
self._sessinfo = self._generate_standard_design(
infolist,
functional_runs=self.inputs.functional_runs,
Expand Down
13 changes: 13 additions & 0 deletions nipype/algorithms/tests/test_auto_SpecifyModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@

def test_SpecifyModel_inputs():
input_map = dict(
bids_amplitude_column=dict(
exists=True,
mandatory=False,
),
bids_condition_column=dict(
exists=True,
mandatory=False,
usedefault=True,
),
bids_event_file=dict(
mandatory=True,
xor=['subject_info', 'event_files', 'bids_event_file'],
),
event_files=dict(
mandatory=True,
xor=['subject_info', 'event_files'],
Expand Down
13 changes: 13 additions & 0 deletions nipype/algorithms/tests/test_auto_SpecifySPMModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@

def test_SpecifySPMModel_inputs():
input_map = dict(
bids_amplitude_column=dict(
exists=True,
mandatory=False,
),
bids_condition_column=dict(
exists=True,
mandatory=False,
usedefault=True,
),
bids_event_file=dict(
mandatory=True,
xor=['subject_info', 'event_files', 'bids_event_file'],
),
concatenate_runs=dict(usedefault=True, ),
event_files=dict(
mandatory=True,
Expand Down
13 changes: 13 additions & 0 deletions nipype/algorithms/tests/test_auto_SpecifySparseModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@

def test_SpecifySparseModel_inputs():
input_map = dict(
bids_amplitude_column=dict(
exists=True,
mandatory=False,
),
bids_condition_column=dict(
exists=True,
mandatory=False,
usedefault=True,
),
bids_event_file=dict(
mandatory=True,
xor=['subject_info', 'event_files', 'bids_event_file'],
),
event_files=dict(
mandatory=True,
xor=['subject_info', 'event_files'],
Expand Down