<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
ITK files handling
~~~~~~~~~~~~~~~~~~


"""
import os
import numpy as np
import nibabel as nb

from joblib import Parallel, delayed
from mimetypes import guess_type
from tempfile import TemporaryDirectory

from niworkflows.nipype.utils.filemanip import fname_presuffix
from niworkflows.nipype.interfaces.base import (
    traits, TraitedSpec, BaseInterfaceInputSpec, File, InputMultiPath, OutputMultiPath)

from niworkflows.nipype.interfaces.ants.resampling import ApplyTransformsInputSpec
from niworkflows.interfaces.base import SimpleInterface


class MCFLIRT2ITKInputSpec(BaseInterfaceInputSpec):
    in_files = InputMultiPath(File(exists=True), mandatory=True,
                              desc='list of MAT files from MCFLIRT')
    in_reference = File(exists=True, mandatory=True,
                        desc='input image for spatial reference')
    in_source = File(exists=True, mandatory=True,
                     desc='input image for spatial source')
    nprocs = traits.Int(1, usedefault=True, nohash=True,
                        desc='number of parallel processes')


class MCFLIRT2ITKOutputSpec(TraitedSpec):
    out_file = File(desc='the output ITKTransform file')


class MCFLIRT2ITK(SimpleInterface):

    """
    Convert a list of MAT files from MCFLIRT into an ITK Transform file.
    """
    input_spec = MCFLIRT2ITKInputSpec
    output_spec = MCFLIRT2ITKOutputSpec

    def _run_interface(self, runtime):
        tmp_folder = TemporaryDirectory()
        parallel = Parallel(n_jobs=self.inputs.nprocs)
        itk_outs = parallel(delayed(_mat2itk)(
            in_mat, self.inputs.in_reference, self.inputs.in_source, i,
            tmp_folder.name) for i, in_mat in enumerate(self.inputs.in_files))
        tmp_folder.cleanup()

        # Compose the collated ITK transform file and write
        tfms = '#Insight Transform File V1.0\n' + ''.join(
            [el[1] for el in sorted(itk_outs)])

        self._results['out_file'] = os.path.abspath('mat2itk.txt')
        with open(self._results['out_file'], 'w') as f:
            f.write(tfms)

        return runtime


class MultiApplyTransformsInputSpec(ApplyTransformsInputSpec):
    input_image = InputMultiPath(File(exists=True), mandatory=True,
                                 desc='list of MAT files from MCFLIRT')
    nprocs = traits.Int(1, usedefault=True, nohash=True,<commit_msg>final fixes<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
ITK files handling
~~~~~~~~~~~~~~~~~~


"""
import os
import numpy as np
import nibabel as nb

from joblib import Parallel, delayed
from mimetypes import guess_type
from tempfile import TemporaryDirectory

from niworkflows.nipype.utils.filemanip import fname_presuffix
from niworkflows.nipype.interfaces.base import (
    traits, TraitedSpec, BaseInterfaceInputSpec, File, InputMultiPath, OutputMultiPath)

from niworkflows.nipype.interfaces.ants.resampling import ApplyTransformsInputSpec
from niworkflows.interfaces.base import SimpleInterface


class MCFLIRT2ITKInputSpec(BaseInterfaceInputSpec):
    in_files = InputMultiPath(File(exists=True), mandatory=True,
                              desc='list of MAT files from MCFLIRT')
    in_reference = File(exists=True, mandatory=True,
                        desc='input image for spatial reference')
    in_source = File(exists=True, mandatory=True,
                     desc='input image for spatial source')
    nprocs = traits.Int(1, usedefault=True, nohash=True,
                        desc='number of parallel processes')


class MCFLIRT2ITKOutputSpec(TraitedSpec):
    out_file = File(desc='the output ITKTransform file')


class MCFLIRT2ITK(SimpleInterface):

    """
    Convert a list of MAT files from MCFLIRT into an ITK Transform file.
    """
    input_spec = MCFLIRT2ITKInputSpec
    output_spec = MCFLIRT2ITKOutputSpec

    def _run_interface(self, runtime):
        parallel = Parallel(n_jobs=self.inputs.nprocs)
        with TemporaryDirectory() as tmp_folder:
            itk_outs = parallel(delayed(_mat2itk)(
                in_mat, self.inputs.in_reference, self.inputs.in_source, i,
                tmp_folder.name) for i, in_mat in enumerate(self.inputs.in_files))

        # Compose the collated ITK transform file and write
        tfms = '#Insight Transform File V1.0\n' + ''.join(
            [el[1] for el in sorted(itk_outs)])

        self._results['out_file'] = os.path.abspath('mat2itk.txt')
        with open(self._results['out_file'], 'w') as f:
            f.write(tfms)

        return runtime


class MultiApplyTransformsInputSpec(ApplyTransformsInputSpec):
    input_image = InputMultiPath(File(exists=True), mandatory=True,
                                 desc='input time-series as a list of volumes after splitting'
                                      ' through the fourth dimension')
    nprocs = traits.Int(1, usedefault=True, nohash=True,
{'fraction_non_alphanumeric': 0.10011596443757248, 'fraction_numerical': 0.006571318129107074, 'mean_word_length': 3.1733870967741935, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '783937', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1527, 'n_words': 388}