示例#1
0
    def convert_to_nii(self, in_files: list, target_dir: Path):
        """
        Convert MRTrix's .mif format to NIfTI for compatability with FSL's
        functions.

        Parameters
        ----------
        in_files : list
            List of files to convert
        target_dir : Path
            Path of directory of converted files
        """
        target_dir = target_dir / "anatomical"
        target_dir.mkdir(exist_ok=True)
        out_files = []
        for in_file, session in zip(in_files, self.sessions):
            out_fname = f"{in_file.name.split('.')[0]}_{session}.nii.gz"
            out_file = target_dir / out_fname
            if out_file.exists():
                message = messages.FILE_EXISTS.format(fname=out_file)
                message = colored(message, "yellow")
                warnings.warn(message)
            else:
                executer = conversions.mrtrix_conversion({"nii": in_file},
                                                         out_file)
                message = messages.CONVERT_TO_NII.format(
                    in_file=in_file,
                    out_file=out_file,
                    command=executer.cmdline,
                )
                message = colored(message, "green")
                print(message)
                executer.run()
            out_files.append(out_file)
            self.registrations_dict[session]["anatomical"] = out_file
示例#2
0
def apply_xfm_to_mifs(
    in_file: Path,
    aff: Path,
    ref: Path,
    target_dir: Path,
):
    """
    Apply pre-calculated transformation matrix to numerous image files.
    Parameters
    ----------
    in_files : list
        A list of paths to images to be registered
    aff : Path
        Path to pre-calculated transformation matrix
    target_dir : Path
        Path to output directory
    keep_tmps : bool, optional
        Whether to keep intermidiate files, by default False
    """
    out_nii = target_dir / f"{in_file.name.split('.')[0]}_tmp.nii.gz"
    out_registered = target_dir / f"{in_file.name.split('.')[0]}.nii.gz"
    if not out_registered.exists():
        mrconvert = mrtrix_conversion({"nii": in_file}, out_nii)
        mrconvert.run()
        executer = apply_xfm(out_nii, ref, aff, out_registered)
    else:
        executer = None

    return executer, out_nii, out_registered
示例#3
0
    def convert_format(
        self,
        session: str,
        session_dict: dict,
        target_dir: Path,
    ):
        """
        Convert NIfTI files to .mif format for compatability with MRTrix3's
        commands.

        Parameters
        ----------
        session : str
            key representing a session within the dataset
        session_dict : dict
            Dictionary containing paths to session-relevant files
        target_dir : Path
            Path to user-defined session's output directory
        """
        for modality, vals in session_dict.items():
            target_file = target_dir / f"{modality}.mif"
            if target_file.exists():
                message = messages.FILE_EXISTS.format(fname=target_file)
                message = colored(message, "yellow")
                warnings.warn(message)
            else:
                converter = conversions.mrtrix_conversion(vals, target_file)
                message = messages.CONVERT_TO_MIF.format(
                    in_file=vals.get("nii"),
                    out_file=target_file,
                    command=converter.cmdline,
                )
                message = colored(message, "green")
                print(message)
                converter.run()

            self.output_dict[session][f"{modality}_mif"] = target_file