示例#1
0
def CreateFreeSurferWorkflow_custom(projectid, subjectid, sessionid, WFname, CLUSTER_QUEUE, CLUSTER_QUEUE_LONG, RunAllFSComponents=True, RunMultiMode=True, constructed_FS_SUBJECTS_DIR='/never_use_this'):
    freesurferWF = pe.Workflow(name=GenerateWFName(projectid, subjectid, sessionid, WFname))

    inputsSpec = pe.Node(interface=IdentityInterface(fields=['subj_session_id', 'T1_files', 'T2_files', 'subjects_dir',
                                                             'wm_prob', 'label_file', 'mask_file']), name='inputspec')
    outputsSpec = pe.Node(interface=IdentityInterface(fields=['full_path_FS_output', 'processed_output_name', 'cnr_optimal_image']), name='outputspec')

    ### HACK: the nipype interface requires that this environmental variable is set before running
    print "HACK SETTING SUBJECTS_DIR {0}".format(constructed_FS_SUBJECTS_DIR)
    os.environ['SUBJECTS_DIR'] = constructed_FS_SUBJECTS_DIR
    inputsSpec.inputs.subjects_dir = constructed_FS_SUBJECTS_DIR  # HACK

    if RunMultiMode:
        mergeT1T2 = pe.Node(interface=Merge(2), name="Merge_T1T2")
        freesurferWF.connect(inputsSpec, 'T1_files', mergeT1T2, 'in1')
        freesurferWF.connect(inputsSpec, 'T2_files', mergeT1T2, 'in2')

        # Some constants based on assumpts about the label_file from BRAINSABC
        white_label = 1
        grey_label = 2

        msLDA_GenerateWeights = pe.Node(interface=MS_LDA(), name="MS_LDA")
        MSLDA_sge_options_dictionary = {'qsub_args': '-S /bin/bash -pe smp 4 -l h_vmem=12G,mem_free=2G -o /dev/null -e /dev/null ' + CLUSTER_QUEUE, 'overwrite': True}
        msLDA_GenerateWeights.plugin_args = MSLDA_sge_options_dictionary
        msLDA_GenerateWeights.inputs.lda_labels = [white_label, grey_label]
        msLDA_GenerateWeights.inputs.weight_file = 'weights.txt'
        msLDA_GenerateWeights.inputs.use_weights = False
        msLDA_GenerateWeights.inputs.vol_synth_file = 'synth_out.nii.gz'
        # msLDA_GenerateWeights.inputs.vol_synth_file = 'synth_out.nii.gz'
        # msLDA_GenerateWeights.inputs.shift = 0 # value to shift by

        freesurferWF.connect(mergeT1T2, 'out', msLDA_GenerateWeights, 'images')
        # freesurferWF.connect(inputsSpec,'subjects_dir',  msLDA_GenerateWeights,'subjects_dir')
        freesurferWF.connect(inputsSpec, 'label_file', msLDA_GenerateWeights, 'label_file')
        # freesurferWF.connect(inputsSpec,'mask_file',  msLDA_GenerateWeights,'mask_file') ## Mask file MUST be unsigned char
        freesurferWF.connect(msLDA_GenerateWeights, 'vol_synth_file', outputsSpec, 'cnr_optimal_image')

    if RunAllFSComponents == True:
        print("""Run FreeSurfer ReconAll at""")
        fs_reconall = pe.Node(interface=fswrap.FSScript(), name="FS52_cross_" + str(sessionid))
        freesurfer_sge_options_dictionary = {'qsub_args': '-S /bin/bash -pe smp 4 -l h_vmem=10G,mem_free=10G ' + CLUSTER_QUEUE, 'overwrite': True}
        fs_reconall.plugin_args = freesurfer_sge_options_dictionary
        fs_reconall.inputs.subcommand = 'autorecon'
        # fs_reconall.inputs.directive = 'all'
        # fs_reconall.inputs.fs_env_script = '' # NOTE: NOT NEEDED HERE 'FreeSurferEnv.sh'
        # fs_reconall.inputs.fs_home = ''       # NOTE: NOT NEEDED HERE
        freesurferWF.connect(inputsSpec, 'subj_session_id', fs_reconall, 'subj_session_id')
        if RunMultiMode:
            ## Use the output of the synthesized T1 with maximized contrast
            ## HACK:  REMOVE FOR NOW - NEEDS FURTHER TESTING
            ## freesurferWF.connect(msLDA_GenerateWeights, 'vol_synth_file', fs_reconall, 'T1_files')
            freesurferWF.connect(inputsSpec, 'T1_files', fs_reconall, 'T1_files')
            ## END HACK
        else:
            ## Use the output of the T1 only image
            freesurferWF.connect(inputsSpec, 'T1_files', fs_reconall, 'T1_files')

        freesurferWF.connect(inputsSpec, 'label_file', fs_reconall, 'brainmask')
        freesurferWF.connect(inputsSpec, 'subjects_dir', fs_reconall, 'subjects_dir')
        freesurferWF.connect(fs_reconall, 'outDir', outputsSpec, 'full_path_FS_output')
        freesurferWF.connect(fs_reconall, 'processed_output_name', outputsSpec, 'processed_output_name')
    return freesurferWF
def create_free_surfer_workflow_custom(
    projectid,
    subjectid,
    sessionid,
    WFname,
    CLUSTER_QUEUE,
    CLUSTER_QUEUE_LONG,
    RunAllFSComponents=True,
    RunMultiMode=True,
    constructed_FS_SUBJECTS_DIR="/never_use_this",
):
    """
    This function...

    :param projectid:
    :param subjectid:
    :param sessionid:
    :param WFname:
    :param CLUSTER_QUEUE:
    :param CLUSTER_QUEUE_LONG:
    :param RunAllFSComponents: True
    :param RunMultiMode: True
    :param constructed_FS_SUBJECTS_DIR: '/never_use_this'
    :return:
    """
    freesurferWF = pe.Workflow(
        name=generate_wf_name(projectid, subjectid, sessionid, WFname))

    inputsSpec = pe.Node(
        interface=IdentityInterface(fields=[
            "subj_session_id",
            "T1_files",
            "T2_files",
            "subjects_dir",
            "wm_prob",
            "label_file",
            "mask_file",
        ]),
        name="inputspec",
    )
    outputsSpec = pe.Node(
        interface=IdentityInterface(fields=[
            "full_path_FS_output", "processed_output_name", "cnr_optimal_image"
        ]),
        name="outputspec",
    )

    ### HACK: the nipype interface requires that this environmental variable is set before running
    print(
        ("HACK SETTING SUBJECTS_DIR {0}".format(constructed_FS_SUBJECTS_DIR)))
    os.environ["SUBJECTS_DIR"] = constructed_FS_SUBJECTS_DIR
    inputsSpec.inputs.subjects_dir = constructed_FS_SUBJECTS_DIR  # HACK

    if RunMultiMode:
        mergeT1T2 = pe.Node(interface=Merge(2), name="Merge_T1T2")
        freesurferWF.connect(inputsSpec, "T1_files", mergeT1T2, "in1")
        freesurferWF.connect(inputsSpec, "T2_files", mergeT1T2, "in2")

        # Some constants based on assumpts about the label_file from BRAINSABC
        white_label = 1
        grey_label = 2

        msLDA_GenerateWeights = pe.Node(interface=MS_LDA(), name="MS_LDA")
        MSLDA_sge_options_dictionary = {
            "qsub_args": modify_qsub_args(CLUSTER_QUEUE, 2, 1, 1),
            "overwrite": True,
        }
        msLDA_GenerateWeights.plugin_args = MSLDA_sge_options_dictionary
        msLDA_GenerateWeights.inputs.lda_labels = [white_label, grey_label]
        msLDA_GenerateWeights.inputs.weight_file = "weights.txt"
        msLDA_GenerateWeights.inputs.use_weights = False
        msLDA_GenerateWeights.inputs.vol_synth_file = "synth_out.nii.gz"
        # msLDA_GenerateWeights.inputs.vol_synth_file = 'synth_out.nii.gz'
        # msLDA_GenerateWeights.inputs.shift = 0 # value to shift by

        freesurferWF.connect(mergeT1T2, "out", msLDA_GenerateWeights, "images")
        # freesurferWF.connect(inputsSpec,'subjects_dir',  msLDA_GenerateWeights,'subjects_dir')
        freesurferWF.connect(inputsSpec, "label_file", msLDA_GenerateWeights,
                             "label_file")
        # freesurferWF.connect(inputsSpec,'mask_file',  msLDA_GenerateWeights,'mask_file') ## Mask file MUST be unsigned char
        freesurferWF.connect(msLDA_GenerateWeights, "vol_synth_file",
                             outputsSpec, "cnr_optimal_image")

    if RunAllFSComponents == True:
        print("""Run FreeSurfer ReconAll at""")
        fs_reconall = pe.Node(interface=fswrap.FSScript(),
                              name="FS52_cross_" + str(sessionid))
        freesurfer_sge_options_dictionary = {
            "qsub_args": modify_qsub_args(CLUSTER_QUEUE, 8, 4, 4),
            "overwrite": True,
        }
        fs_reconall.plugin_args = freesurfer_sge_options_dictionary
        fs_reconall.inputs.subcommand = "autorecon"
        # fs_reconall.inputs.directive = 'all'
        # fs_reconall.inputs.fs_env_script = '' # NOTE: NOT NEEDED HERE 'FreeSurferEnv.sh'
        # fs_reconall.inputs.fs_home = ''       # NOTE: NOT NEEDED HERE
        freesurferWF.connect(inputsSpec, "subj_session_id", fs_reconall,
                             "subj_session_id")
        if RunMultiMode:
            ## Use the output of the synthesized T1 with maximized contrast
            ## HACK:  REMOVE FOR NOW - NEEDS FURTHER TESTING
            ## freesurferWF.connect(msLDA_GenerateWeights, 'vol_synth_file', fs_reconall, 'T1_files')
            freesurferWF.connect(inputsSpec, "T1_files", fs_reconall,
                                 "T1_files")
            ## END HACK
        else:
            ## Use the output of the T1 only image
            freesurferWF.connect(inputsSpec, "T1_files", fs_reconall,
                                 "T1_files")

        freesurferWF.connect(inputsSpec, "label_file", fs_reconall,
                             "brainmask")
        freesurferWF.connect(inputsSpec, "subjects_dir", fs_reconall,
                             "subjects_dir")
        freesurferWF.connect(fs_reconall, "outDir", outputsSpec,
                             "full_path_FS_output")
        freesurferWF.connect(fs_reconall, "processed_output_name", outputsSpec,
                             "processed_output_name")
    return freesurferWF