예제 #1
0
    def _environment(self):
        """ Return a dictionary of the environment needed by FreeSurfer
        binaries.
        """
        # Check if FreeSurfer has already been configures
        env = os.environ.get("FREESURFER_CONFIGURED", None)

        # Configure FreeSurfer
        if env is None:

            # FreeSurfer home directory
            fs_home = os.environ.get("FREESURFER_HOME", None)
            env = {}
            if fs_home is not None:
                env["FREESURFER_HOME"] = fs_home

            # Parse configuration file
            env = environment(self.shfile, env)

            # Save the result
            os.environ["FREESURFER_CONFIGURED"] = json.dumps(env)

        # Load configuration
        else:
            env = json.loads(env)

        return env
예제 #2
0
    def _environment(self):
        """ Return a dictionary of the environment needed by FreeSurfer
        binaries.
        """
        # Check if FreeSurfer has already been configures
        env = os.environ.get("FREESURFER_CONFIGURED", None)

        # Configure FreeSurfer
        if env is None:

            # FreeSurfer home directory
            fs_home = os.environ.get("FREESURFER_HOME", None)
            env = {}
            if fs_home is not None:
                env["FREESURFER_HOME"] = fs_home

            # Parse configuration file
            env = environment(self.shfile, env)

            # Save the result
            os.environ["FREESURFER_CONFIGURED"] = json.dumps(env)

        # Load configuration
        else:
            env = json.loads(env)

        return env
예제 #3
0
def run_freesurfer_cmd(cmd, subjects_dir=None, add_fsl_env=False,
                       fsl_init="/etc/fsl/5.0/fsl.sh"):
    """
    To avoid repeating the code to run Freesurfer and check exitcode
    everywhere.
    Step:
        - add $SUBJECTS_DIR to the environment if requested
        - add FSL's environment if requested (some Freesurfer commands require
          FSL)
        - run the Freesurfer cmd
        - check exit code

    Parameters
    ----------
    cmd: list of str
        the command to run (subprocess like).
    subjects_dir: str, default None.
        To set the $SUBJECTS_DIR environment variable.
    add_fsl_env:  bool, default False
        To activate the FSL environment, required for commands like bbregister.
    fsl_init: str
        Path to the Bash script setting the FSL environment, if needed.
    """
    fsprocess = FSWrapper(cmd)

    if subjects_dir is not None:
        fsprocess.environment["SUBJECTS_DIR"] = subjects_dir

    if add_fsl_env:
        fsl_env = environment(fsl_init)
        for k, i in fsl_env.items():
            if k not in fsprocess.environment:
                fsprocess.environment[k] = i
            else:
                # A variable that exists in both FS and FSL environments
                if k == "PATH":
                    fsprocess.environment["PATH"] += ":" + fsl_env["PATH"]
                else:
                    pass  # ignore this variable

    fsprocess()  # Run
    if fsprocess.exitcode != 0:
        raise FreeSurferRuntimeError(cmd[0], " ".join(cmd[1:]))

    return fsprocess
예제 #4
0
    def _environment(self) :
        """ Return a dictionary of the environment needed by FSL binaries.
        """
        # Check if FSL has already been configures
        env = os.environ.get("FSL_CONFIGURED", None)
       
        # Configure FSL
        if env is None:

            # Parse configuration file
            env = environment(self.shfile)

            # Save the result
            os.environ["FSL_CONFIGURED"] = json.dumps(env)

        # Load configuration
        else:
            env = json.loads(env)

        return env
예제 #5
0
    def _environment(self):
        """ Return a dictionary of the environment needed by FSL binaries.
        """
        # Check if FSL has already been configures
        env = os.environ.get("FSL_CONFIGURED", None)

        # Configure FSL
        if env is None:

            # Parse configuration file
            env = environment(self.shfile)

            # Save the result
            os.environ["FSL_CONFIGURED"] = json.dumps(env)

        # Load configuration
        else:
            env = json.loads(env)

        return env
예제 #6
0
def register_diffusion_to_anatomy(outdir,
                                  nodif_brain,
                                  subject_id,
                                  fs_subjects_dir = None,
                                  subdir          = "diff_to_anat",
                                  fsl_init        = "/etc/fsl/5.0/fsl.sh"):
    """
    Register the diffusion to the anatomy (T1) using Freesurfer bbregister
    (boundary-based registration).
    <unit>
        <input name="outdir"          type="Directory"           />
        <input name="nodif_brain"     type="File"                />
        <input name="subject_id"      type="Str"                 />
        <input name="fs_subjects_dir" type="Directory"           />
        <input name="subdir"          type="Str"                 />
        <input name="fsl_init"        type="File"                />

        <output name="dif2anat_dat"   type="File" description="
            The .dat file created by tkregister/bbregister cmd." />
    </unit>
    """

    if fs_subjects_dir is None:
        if "SUBJECTS_DIR" in os.environ:
            fs_subjects_dir = os.environ["SUBJECTS_DIR"]
        else:
            raise ValueError("Missing <SUBJECTS_DIR>: set the $SUBJECTS_DIR "
                             "environment variable for Freesurfer or pass it "
                             "as an argument.")

    # If a subdirectory name has been passed, adapt outdir
    if subdir:
        outdir = os.path.join(outdir, subdir)

    # Create outdir if it does not exist
    if not os.path.isdir(outdir):
        os.makedirs(outdir)

    dif2anat_dat = os.path.join(outdir, "dif2anat.dat")
    cmd = ["bbregister", "--s", subject_id, "--mov", nodif_brain,
           "--reg", dif2anat_dat, "--dti", "--init-fsl"]
    fsprocess = FSWrapper(cmd)
    fsprocess.environment["SUBJECTS_DIR"] = fs_subjects_dir

    # bbregister requires FSL, so we have to add the FSL environment
    fsl_env = environment(fsl_init)
    for k, i in fsl_env.items():
        if k not in fsprocess.environment:
            fsprocess.environment[k] = i
        else:
            # A variable that exists in both FS and FSL environments
            if k == "PATH":
                fsprocess.environment["PATH"] += ":" + fsl_env["PATH"]
            else:
                pass  # ignore this variable

    fsprocess()  # Run
    if fsprocess.exitcode != 0:
        raise FreeSurferRuntimeError(cmd[0], " ".join(cmd[1:]))

    return dif2anat_dat