Skip to content

Better FSL modelgen parameter handling and a few more EV file parameters #1710

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

Closed
wants to merge 7 commits into from
Closed
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
68 changes: 42 additions & 26 deletions nipype/interfaces/fsl/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ class Level1DesignInputSpec(BaseInterfaceInputSpec):
traits.Dict(traits.Enum(
'dgamma'), traits.Dict(traits.Enum('derivs'), traits.Bool)),
traits.Dict(traits.Enum('gamma'), traits.Dict(
traits.Enum('derivs'), traits.Bool)),
traits.Dict(traits.Enum('none'), traits.Enum(None)),
traits.Enum('derivs', 'gammasigma', 'gammadelay'))),
traits.Dict(traits.Enum('none'), traits.Dict()),
mandatory=True,
desc=("name of basis function and options e.g., "
"{'dgamma': {'derivs': True}}"))
orthogonalization = traits.Dict(traits.Int, traits.Dict(traits.Int,
traits.Either(traits.Bool,traits.Int)),
mandatory=False,
desc=("which regressors to make orthogonal e.g., "
"{1: {0:0,1:0,2:0}, 2: {0:1,1:1,2:0}} to make the second "
"regressor in a 2-regressor model orthogonal to the first."),
default={})
model_serial_correlations = traits.Bool(
desc="Option to model serial correlations using an \
autoregressive estimator (order 1). Setting this option is only \
Expand Down Expand Up @@ -122,8 +129,8 @@ def _create_ev_file(self, evfname, evinfo):
f.close()

def _create_ev_files(
self, cwd, runinfo, runidx, usetd, contrasts,
do_tempfilter, basis_key):
self, cwd, runinfo, runidx, ev_parameters, orthogonalization,
contrasts, do_tempfilter, basis_key):
"""Creates EV files from condition and regressor information.

Parameters:
Expand All @@ -134,16 +141,27 @@ def _create_ev_files(
about events and other regressors.
runidx : int
Index to run number
usetd : int
Whether or not to use temporal derivatives for
conditions
ev_parameters : dict
A dictionary containing the model parameters for the
given design type.
orthogonalization : dict
A dictionary of dictionaries specifying orthogonal EVs.
contrasts : list of lists
Information on contrasts to be evaluated
"""
conds = {}
evname = []
if basis_key == "dgamma":
basis_key = "hrf"
elif basis_key == "gamma":
try:
_ = ev_parameters['gammasigma']
except KeyError:
ev_parameters['gammasigma'] = 3
try:
_ = ev_parameters['gammadelay']
except KeyError:
ev_parameters['gammadelay'] = 6
ev_template = load_template('feat_ev_'+basis_key+'.tcl')
ev_none = load_template('feat_ev_none.tcl')
ev_ortho = load_template('feat_ev_ortho.tcl')
Expand Down Expand Up @@ -174,22 +192,18 @@ def _create_ev_files(
evinfo.insert(j, [onset, cond['duration'][j], amp])
else:
evinfo.insert(j, [onset, cond['duration'][0], amp])
if basis_key == "none":
ev_txt += ev_template.substitute(
ev_num=num_evs[0],
ev_name=name,
tempfilt_yn=do_tempfilter,
cond_file=evfname)
ev_parameters['ev_num'] = num_evs[0]
ev_parameters['ev_name'] = name
ev_parameters['tempfilt_yn'] = do_tempfilter
ev_parameters['cond_file'] = evfname
try:
ev_parameters['temporalderiv'] = ev_parameters.pop('derivs')
except KeyError:
pass
else:
ev_txt += ev_template.substitute(
ev_num=num_evs[0],
ev_name=name,
tempfilt_yn=do_tempfilter,
temporalderiv=usetd,
cond_file=evfname)
if usetd:
evname.append(name + 'TD')
num_evs[1] += 1
ev_txt += ev_template.substitute(ev_parameters)
elif field == 'regress':
evinfo = [[j] for j in cond['val']]
ev_txt += ev_none.substitute(ev_num=num_evs[0],
Expand All @@ -202,7 +216,11 @@ def _create_ev_files(
# add ev orthogonalization
for i in range(1, num_evs[0] + 1):
for j in range(0, num_evs[0] + 1):
ev_txt += ev_ortho.substitute(c0=i, c1=j)
try:
orthogonal = int(orthogonalization[i][j])
except (KeyError, TypeError, ValueError, IndexError):
orthogonal = 0
ev_txt += ev_ortho.substitute(c0=i, c1=j, orthogonal=orthogonal)
ev_txt += "\n"
# add contrast info to fsf file
if isdefined(contrasts):
Expand Down Expand Up @@ -297,10 +315,8 @@ def _run_interface(self, runtime):
prewhiten = 0
if isdefined(self.inputs.model_serial_correlations):
prewhiten = int(self.inputs.model_serial_correlations)
usetd = 0
basis_key = list(self.inputs.bases.keys())[0]
if basis_key in ['dgamma', 'gamma']:
usetd = int(self.inputs.bases[basis_key]['derivs'])
ev_parameters = dict(self.inputs.bases[basis_key])
session_info = self._format_session_info(self.inputs.session_info)
func_files = self._get_func_files(session_info)
n_tcon = 0
Expand All @@ -316,8 +332,9 @@ def _run_interface(self, runtime):
do_tempfilter = 1
if info['hpf'] == np.inf:
do_tempfilter = 0
num_evs, cond_txt = self._create_ev_files(cwd, info, i, usetd,
num_evs, cond_txt = self._create_ev_files(cwd, info, i, ev_parameters,
self.inputs.contrasts,
self.inputs.orthogonalization,
do_tempfilter, basis_key)
nim = load(func_files[i])
(_, _, _, timepoints) = nim.shape
Expand Down Expand Up @@ -371,7 +388,6 @@ def _list_outputs(self):
os.path.join(cwd, evfname))
return outputs


class FEATInputSpec(FSLCommandInputSpec):
fsf_file = File(exists=True, mandatory=True, argstr="%s", position=0,
desc="File specifying the feat design spec file")
Expand Down
6 changes: 5 additions & 1 deletion nipype/interfaces/fsl/tests/test_Level1Design_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ def test_level1design():
contrasts = Undefined
usetd = False
do_tempfilter = False
orthogonalization = {}
ev_parameters = {"temporalderiv":False}
for key, val in [('hrf', 3), ('dgamma', 3), ('gamma', 2), ('none', 0)]:
output_num, output_txt = Level1Design._create_ev_files(l, os.getcwd(),
runinfo, runidx,
usetd, contrasts,
ev_parameters,
orthogonalization,
contrasts,
do_tempfilter,
key)
assert "set fmri(convolve1) {0}".format(val) in output_txt
4 changes: 2 additions & 2 deletions nipype/interfaces/script_templates/feat_ev_gamma.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ set fmri(deriv_yn$ev_num) $temporalderiv
set fmri(custom$ev_num) "$cond_file"

# Gamma sigma
set fmri(gammasigma$ev_num) 3
set fmri(gammasigma$ev_num) $gammasigma

# Gamma delay
set fmri(gammadelay$ev_num) 6
set fmri(gammadelay$ev_num) $gammadelay
2 changes: 1 addition & 1 deletion nipype/interfaces/script_templates/feat_ev_none.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set fmri(evtitle$ev_num) "$ev_name"
# 3 : Custom (3 column format)
# 4 : Interaction
# 10 : Empty (all zeros)
set fmri(shape$ev_num) 2
set fmri(shape$ev_num) 3

# Convolution
# 0 : None
Expand Down
2 changes: 1 addition & 1 deletion nipype/interfaces/script_templates/feat_ev_ortho.tcl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Orthogonalise EV $c0 wrt EV $c1
set fmri(ortho$c0.$c1) 0
set fmri(ortho$c0.$c1) $orthogonal