<commit_before>"""The fsl module provides classes for interfacing with the `FSL
<http://www.fmrib.ox.ac.uk/fsl/index.html>`_ command line tools.  This
was written to work with FSL version 4.1.4.

Examples
--------
See the docstrings of the individual classes for examples.

"""

import os
from glob import glob
import warnings

import numpy as np

from nipype.interfaces.fsl.base import FSLCommand,\
    FSLCommandInputSpec, Info
from nipype.interfaces.base import traits, TraitedSpec,\
    OutputMultiPath, File
from nipype.utils.misc import isdefined

warn = warnings.warn
warnings.filterwarnings('always', category=UserWarning)

class SmoothInputSpec(FSLCommandInputSpec):
    infile = File(exists=True, argstr="%s", position=0, mandatory=True)
    fwhm = traits.Float(argstr="-kernel gauss %f -fmean", position=1,
                            mandatory=True)
    outfile = File(argstr="%s", position=2, genfile=True)

class SmoothOutputSpec(TraitedSpec):
    smoothedimage = File(exists=True)

class Smooth(FSLCommand):
    '''Use fslmaths to smooth the image

    This is dumb, of course - we should use nipy for such things! But it is a
    step along the way to get the "standard" FSL pipeline in place.

    This is meant to be a throwaway class, so it's not currently very robust.
    Effort would be better spent integrating basic numpy into nipype'''
    
    input_spec = SmoothInputSpec
    output_spec = SmoothOutputSpec
    _cmd = 'fslmaths'

    def _gen_filename(self, name):
        if name == 'outfile':
            return self._list_outputs()['smoothedimage']
        return None

    def _list_outputs(self):
        outputs = self._outputs().get()
        outputs['smoothedimage'] = self.inputs.outfile
        if not isdefined(outputs['smoothedimage']):
            outputs['smoothedimage'] = self._gen_fname(self.inputs.infile,
                                              suffix = '_smooth')
        return outputs
    
    def _format_arg(self, name, trait_spec, value):
        if name == 'fwhm':
            # ohinds: convert fwhm to stddev
            return super(Smooth, self)._format_arg(name, trait_spec, float(value) / np.sqrt(8 * np.log(2)))
        else:
            return super(Smooth, self)._format_arg(name, trait_spec, value)

class MergeInputSpec(FSLCommandInputSpec):
    infiles = traits.List(File(exists=True), argstr="%s", position=2, mandatory=True)
    dimension = traits.Enum('t', 'x', 'y', 'z', argstr="-%s", position=0,
                            desc="dimension along which the file will be merged",
                            mandatory=True)
    outfile = File(argstr="%s", position=1, genfile=True)

class MergeOutputSpec(TraitedSpec):
    outfile = File(exists=True)

class Merge(FSLCommand):
    """Use fslmerge to concatenate images
    """

    _cmd = 'fslmerge'
    input_spec = MergeInputSpec
    output_spec = MergeOutputSpec

    def _list_outputs(self):
        outputs = self._outputs().get()
        outputs['outfile'] = self.inputs.outfile
        if not isdefined(outputs['outfile']):
            outputs['outfile'] = self._gen_fname(self.inputs.infile,
                                              suffix = '_merged')
        return outputs
    
    def _gen_filename(self, name):
        if name == 'outfile':
            return self._list_outputs()[name]
        return None


class ExtractRoiInputSpec(FSLCommandInputSpec):
    infile = File(exists=True, argstr="%s", position=0, desc="input file", mandatory=True)
    outfile = File(argstr="%s", position=1, desc="output file", genfile=True)<commit_msg>* modified merge to use the first infile<commit_after>"""The fsl module provides classes for interfacing with the `FSL
<http://www.fmrib.ox.ac.uk/fsl/index.html>`_ command line tools.  This
was written to work with FSL version 4.1.4.

Examples
--------
See the docstrings of the individual classes for examples.

"""

import os
from glob import glob
import warnings

import numpy as np

from nipype.interfaces.fsl.base import FSLCommand,\
    FSLCommandInputSpec, Info
from nipype.interfaces.base import traits, TraitedSpec,\
    OutputMultiPath, File
from nipype.utils.misc import isdefined

warn = warnings.warn
warnings.filterwarnings('always', category=UserWarning)

class SmoothInputSpec(FSLCommandInputSpec):
    infile = File(exists=True, argstr="%s", position=0, mandatory=True)
    fwhm = traits.Float(argstr="-kernel gauss %f -fmean", position=1,
                            mandatory=True)
    outfile = File(argstr="%s", position=2, genfile=True)

class SmoothOutputSpec(TraitedSpec):
    smoothedimage = File(exists=True)

class Smooth(FSLCommand):
    '''Use fslmaths to smooth the image

    This is dumb, of course - we should use nipy for such things! But it is a
    step along the way to get the "standard" FSL pipeline in place.

    This is meant to be a throwaway class, so it's not currently very robust.
    Effort would be better spent integrating basic numpy into nipype'''
    
    input_spec = SmoothInputSpec
    output_spec = SmoothOutputSpec
    _cmd = 'fslmaths'

    def _gen_filename(self, name):
        if name == 'outfile':
            return self._list_outputs()['smoothedimage']
        return None

    def _list_outputs(self):
        outputs = self._outputs().get()
        outputs['smoothedimage'] = self.inputs.outfile
        if not isdefined(outputs['smoothedimage']):
            outputs['smoothedimage'] = self._gen_fname(self.inputs.infile,
                                              suffix = '_smooth')
        return outputs
    
    def _format_arg(self, name, trait_spec, value):
        if name == 'fwhm':
            # ohinds: convert fwhm to stddev
            return super(Smooth, self)._format_arg(name, trait_spec, float(value) / np.sqrt(8 * np.log(2)))
        else:
            return super(Smooth, self)._format_arg(name, trait_spec, value)

class MergeInputSpec(FSLCommandInputSpec):
    infiles = traits.List(File(exists=True), argstr="%s", position=2, mandatory=True)
    dimension = traits.Enum('t', 'x', 'y', 'z', argstr="-%s", position=0,
                            desc="dimension along which the file will be merged",
                            mandatory=True)
    outfile = File(argstr="%s", position=1, genfile=True)

class MergeOutputSpec(TraitedSpec):
    outfile = File(exists=True)

class Merge(FSLCommand):
    """Use fslmerge to concatenate images
    """

    _cmd = 'fslmerge'
    input_spec = MergeInputSpec
    output_spec = MergeOutputSpec

    def _list_outputs(self):
        outputs = self._outputs().get()
        outputs['outfile'] = self.inputs.outfile
        if not isdefined(outputs['outfile']):
            outputs['outfile'] = self._gen_fname(self.inputs.infiles[0],
                                              suffix = '_merged')
        return outputs
    
    def _gen_filename(self, name):
        if name == 'outfile':
            return self._list_outputs()[name]
        return None


class ExtractRoiInputSpec(FSLCommandInputSpec):
    infile = File(exists=True, argstr="%s", position=0, desc="input file", mandatory=True)
    outfile = File(argstr="%s", position=1, desc="output file", genfile=True)
{'fraction_non_alphanumeric': 0.10922498956449145, 'fraction_numerical': 0.003756783080562126, 'mean_word_length': 2.915032679738562, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3372614', 'n_tokens_mistral': 2167, 'n_tokens_neox': 2129, 'n_words': 584}