Exemplo n.º 1
0
def test_EstimateContrast_inputs():
    input_map = dict(ignore_exception=dict(nohash=True,
    usedefault=True,
    ),
    nvbeta=dict(mandatory=True,
    ),
    s2=dict(mandatory=True,
    ),
    dof=dict(mandatory=True,
    ),
    mask=dict(),
    beta=dict(mandatory=True,
    ),
    reg_names=dict(mandatory=True,
    ),
    contrasts=dict(mandatory=True,
    ),
    constants=dict(mandatory=True,
    ),
    axis=dict(mandatory=True,
    ),
    )
    inputs = EstimateContrast.input_spec()

    for key, metadata in input_map.items():
        for metakey, value in metadata.items():
            yield assert_equal, getattr(inputs.traits()[key], metakey), value
def test_EstimateContrast_inputs():
    input_map = dict(axis=dict(mandatory=True,
    ),
    beta=dict(mandatory=True,
    ),
    constants=dict(mandatory=True,
    ),
    contrasts=dict(mandatory=True,
    ),
    dof=dict(mandatory=True,
    ),
    ignore_exception=dict(nohash=True,
    usedefault=True,
    ),
    mask=dict(),
    nvbeta=dict(mandatory=True,
    ),
    reg_names=dict(mandatory=True,
    ),
    s2=dict(mandatory=True,
    ),
    )
    inputs = EstimateContrast.input_spec()

    for key, metadata in input_map.items():
        for metakey, value in metadata.items():
            yield assert_equal, getattr(inputs.traits()[key], metakey), value
Exemplo n.º 3
0
def test_EstimateContrast_outputs():
    output_map = dict(p_maps=dict(),
    stat_maps=dict(),
    z_maps=dict(),
    )
    outputs = EstimateContrast.output_spec()

    for key, metadata in output_map.items():
        for metakey, value in metadata.items():
            yield assert_equal, getattr(outputs.traits()[key], metakey), value
def test_EstimateContrast_outputs():
    output_map = dict(p_maps=dict(),
    stat_maps=dict(),
    z_maps=dict(),
    )
    outputs = EstimateContrast.output_spec()

    for key, metadata in output_map.items():
        for metakey, value in metadata.items():
            yield assert_equal, getattr(outputs.traits()[key], metakey), value
Exemplo n.º 5
0
modelspec.inputs.input_units = 'secs'
modelspec.inputs.output_units = 'secs'
modelspec.inputs.time_repetition = 3.
modelspec.inputs.high_pass_filter_cutoff = 120
"""Fit the GLM model using nipy and ordinary least square method
"""

model_estimate = pe.Node(interface=FitGLM(), name="model_estimate")
model_estimate.inputs.TR = 3.
model_estimate.inputs.model = "spherical"
model_estimate.inputs.method = "ols"
"""Estimate the contrasts. The format of the contrasts definition is the same as
for FSL and SPM
"""

contrast_estimate = pe.Node(interface=EstimateContrast(),
                            name="contrast_estimate")
cont1 = ('Task>Baseline', 'T', ['Task-Odd', 'Task-Even'], [0.5, 0.5])
cont2 = ('Task-Odd>Task-Even', 'T', ['Task-Odd', 'Task-Even'], [1, -1])
contrast_estimate.inputs.contrasts = [cont1, cont2]
"""
Setup the pipeline
------------------

The nodes created above do not describe the flow of data. They merely
describe the parameters used for each function. In this section we
setup the connections between the nodes such that appropriate outputs
from nodes are piped into appropriate inputs of other nodes.

Use the :class:`nipype.pipeline.engine.Pipeline` to create a
graph-based execution pipeline for first level analysis. The config
def create_model_fit_pipeline(high_pass_filter_cutoff=128,
                              nipy=False,
                              ar1=True,
                              name="model",
                              save_residuals=False):
    inputnode = pe.Node(interface=util.IdentityInterface(fields=[
        'outlier_files', "realignment_parameters", "functional_runs", "mask",
        'conditions', 'onsets', 'durations', 'TR', 'contrasts', 'units',
        'sparse'
    ]),
                        name="inputnode")

    modelspec = pe.Node(interface=model.SpecifySPMModel(), name="modelspec")
    if high_pass_filter_cutoff:
        modelspec.inputs.high_pass_filter_cutoff = high_pass_filter_cutoff

    create_subject_info = pe.Node(interface=util.Function(
        input_names=['conditions', 'onsets', 'durations'],
        output_names=['subject_info'],
        function=create_subject_inf),
                                  name="create_subject_info")

    modelspec.inputs.concatenate_runs = True
    #modelspec.inputs.input_units             = units
    modelspec.inputs.output_units = "secs"
    #modelspec.inputs.time_repetition         = tr
    #modelspec.inputs.subject_info = subjectinfo

    model_pipeline = pe.Workflow(name=name)

    model_pipeline.connect([
        (inputnode, create_subject_info, [('conditions', 'conditions'),
                                          ('onsets', 'onsets'),
                                          ('durations', 'durations')]),
        (inputnode, modelspec, [('realignment_parameters',
                                 'realignment_parameters'),
                                ('functional_runs', 'functional_runs'),
                                ('outlier_files', 'outlier_files'),
                                ('units', 'input_units'),
                                ('TR', 'time_repetition')]),
        (create_subject_info, modelspec, [('subject_info', 'subject_info')]),
    ])

    if nipy:
        model_estimate = pe.Node(interface=FitGLM(), name="level1estimate")
        model_estimate.inputs.TR = tr
        model_estimate.inputs.normalize_design_matrix = True
        model_estimate.inputs.save_residuals = save_residuals
        if ar1:
            model_estimate.inputs.model = "ar1"
            model_estimate.inputs.method = "kalman"
        else:
            model_estimate.inputs.model = "spherical"
            model_estimate.inputs.method = "ols"

        model_pipeline.connect([
            (modelspec, model_estimate, [('session_info', 'session_info')]),
            (inputnode, model_estimate, [('mask', 'mask')])
        ])

        if contrasts:
            contrast_estimate = pe.Node(interface=EstimateContrast(),
                                        name="contrastestimate")
            contrast_estimate.inputs.contrasts = contrasts
            model_pipeline.connect([
                (model_estimate, contrast_estimate,
                 [("beta", "beta"), ("nvbeta", "nvbeta"), ("s2", "s2"),
                  ("dof", "dof"), ("axis", "axis"), ("constants", "constants"),
                  ("reg_names", "reg_names")]),
                (inputnode, contrast_estimate, [('mask', 'mask')]),
            ])
    else:
        level1design = pe.Node(interface=spm.Level1Design(),
                               name="level1design")
        level1design.inputs.bases = {'hrf': {'derivs': [0, 0]}}
        if ar1:
            level1design.inputs.model_serial_correlations = "AR(1)"
        else:
            level1design.inputs.model_serial_correlations = "none"

        level1design.inputs.timing_units = modelspec.inputs.output_units

        #level1design.inputs.interscan_interval = modelspec.inputs.time_repetition
        #        if sparse:
        #            level1design.inputs.microtime_resolution = n_slices*2
        #        else:
        #            level1design.inputs.microtime_resolution = n_slices
        #level1design.inputs.microtime_onset = ref_slice

        microtime_resolution = pe.Node(interface=util.Function(
            input_names=['volume', 'sparse'],
            output_names=['microtime_resolution'],
            function=_get_microtime_resolution),
                                       name="microtime_resolution")

        level1estimate = pe.Node(interface=spm.EstimateModel(),
                                 name="level1estimate")
        level1estimate.inputs.estimation_method = {'Classical': 1}

        contrastestimate = pe.Node(interface=spm.EstimateContrast(),
                                   name="contrastestimate")
        #contrastestimate.inputs.contrasts = contrasts

        threshold = pe.MapNode(interface=spm.Threshold(),
                               name="threshold",
                               iterfield=['contrast_index', 'stat_image'])
        #threshold.inputs.contrast_index = range(1,len(contrasts)+1)

        threshold_topo_ggmm = neuroutils.CreateTopoFDRwithGGMM(
            "threshold_topo_ggmm")
        #threshold_topo_ggmm.inputs.inputnode.contrast_index = range(1,len(contrasts)+1)

        model_pipeline.connect([
            (modelspec, level1design, [('session_info', 'session_info')]),
            (inputnode, level1design, [('mask', 'mask_image'),
                                       ('TR', 'interscan_interval'),
                                       (("functional_runs", get_ref_slice),
                                        "microtime_onset")]),
            (inputnode, microtime_resolution, [("functional_runs", "volume"),
                                               ("sparse", "sparse")]),
            (microtime_resolution, level1design, [("microtime_resolution",
                                                   "microtime_resolution")]),
            (level1design, level1estimate, [('spm_mat_file', 'spm_mat_file')]),
            (inputnode, contrastestimate, [('contrasts', 'contrasts')]),
            (level1estimate, contrastestimate,
             [('spm_mat_file', 'spm_mat_file'), ('beta_images', 'beta_images'),
              ('residual_image', 'residual_image')]),
            (contrastestimate, threshold, [('spm_mat_file', 'spm_mat_file'),
                                           ('spmT_images', 'stat_image')]),
            (inputnode, threshold, [(('contrasts', _get_contrast_index),
                                     'contrast_index')]),
            (level1estimate, threshold_topo_ggmm, [('mask_image',
                                                    'inputnode.mask_file')]),
            (contrastestimate, threshold_topo_ggmm,
             [('spm_mat_file', 'inputnode.spm_mat_file'),
              ('spmT_images', 'inputnode.stat_image')]),
            (inputnode, threshold_topo_ggmm,
             [(('contrasts', _get_contrast_index), 'inputnode.contrast_index')
              ]),
        ])

    return model_pipeline