Skip to content

Instantly share code, notes, and snippets.

@tsalo
Created December 6, 2017 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsalo/ece0b3cdd36eb78b8c5a4dd0b07611a1 to your computer and use it in GitHub Desktop.
Save tsalo/ece0b3cdd36eb78b8c5a4dd0b07611a1 to your computer and use it in GitHub Desktop.
from nipype.interfaces.base import Bunch
from nipype.algorithms import modelgen
from copy import deepcopy
def lss(bunch_list):
"""Takes input Bunch list and returns list of Bunch lists with LSS regressors.
"""
all_infos = []
other_items = ['durations', 'amplitudes',
'regressors', 'regressor_names']
# Take a list of Bunches, loop through runs
for i_run, run in enumerate(bunch_list):
# Grab conditions first to get number
conditions = run.get('conditions')
for i_cond, cond in enumerate(conditions):
# Loop through other items, which may be lists or Nones
onsets = run.get('onsets')
cond_onsets = onsets[i_cond]
for trial in range(len(cond_onsets)):
trial_name = '{0}_{1:04d}'.format(cond, trial)
trial_dict = {}
for item in other_items:
item_vals = run.get(item, None)
if item_vals is not None:
cond_vals = item_vals[i_cond]
if len(cond_vals) == len(cond_onsets):
val = cond_vals[trial]
new_vals = deepcopy(item_vals)
new_vals[i_cond].pop(trial)
trial_dict[item] = [[item_vals[i_cond][trial]]] + new_vals
else:
val = cond_vals[0]
trial_dict[item] = [[val]] + item_vals
else:
trial_dict[item] = item_vals
trial_dict['conditions'] = [trial_name] + conditions
new_onsets = deepcopy(onsets)
new_onsets[i_cond].pop(trial)
trial_dict['onsets'] = [[onsets[i_cond][trial]]] + new_onsets
trial_info = deepcopy(bunch_list)
trial_info[i_run] = Bunch(trial_dict)
all_infos.append(trial_info)
return all_infos
s = modelgen.SpecifyModel()
s.inputs.input_units = 'secs'
s.inputs.functional_runs = ['functional2.nii', 'functional3.nii']
s.inputs.time_repetition = 6
s.inputs.high_pass_filter_cutoff = 128.
evs_run2 = Bunch(conditions=['cond1', 'cond2'],
onsets=[[2, 50, 100, 180], [3, 51, 101, 116]],
durations=[[1, 2, 3, 4], [1, 2, 3, 4]])
evs_run3 = Bunch(conditions=['cond1'], onsets=[[30, 40, 100, 150]], durations=[[1]])
s.inputs.subject_info = [evs_run2, evs_run3]
list_of_bunch_lists = lss(s.inputs.subject_info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment