<commit_before># emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
    The maths module provides higher-level interfaces to some of the operations
    that can be performed with the niftysegmaths (seg_maths) command-line program.
"""
import os
import numpy as np

from nipype.interfaces.niftyseg.base import NIFTYSEGCommand, NIFTYSEGCommandInputSpec, getNiftySegPath
from nipype.interfaces.base import (TraitedSpec, File, traits, InputMultiPath,
                                    isdefined)

class MathsInput(NIFTYSEGCommandInputSpec):
    
    in_file = File(position=2, argstr='%s', exists=True, mandatory=True,
                desc='image to operate on')
    out_file = File(genfile=True, position=-2, argstr='%s', desc='image to write')

    _dtypes = ['float', 'char', 'int', 'short', 'double', 'input']
    output_datatype = traits.Enum(*_dtypes,
                                  position=-3, argstr='-odt %s',
                                  desc='datatype to use for output (default uses input type)')
    
class MathsOutput(TraitedSpec):

    out_file = File(exists=True, desc='image written after calculations')

class MathsCommand(NIFTYSEGCommand):

    _cmd = getNiftySegPath('seg_maths')
    input_spec = MathsInput
    output_spec = MathsOutput
    _suffix = '_maths'

    def _list_outputs(self):
        outputs = self.output_spec().get()
        outputs['out_file'] = self.inputs.out_file
        if not isdefined(self.inputs.out_file):
            outputs['out_file'] = self._gen_fname(self.inputs.in_file, suffix=self._suffix)
        outputs['out_file'] = os.path.abspath(outputs['out_file'])
        return outputs

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

class UnaryMathsInput(MathsInput):

    operation = traits.Enum('exp', 'log', 'recip', 'sqrt', 'abs', 'bin', 
                            'otsu', 'lconcomp', 'fill', 'euc', 
                            'tpmax', 'tmean', 'tmin', 'tmax',
                            'splitlab',
                            'subsamp2', 'lcl', 'range',
                            argstr='-%s', position=4, mandatory=True,
                            desc='operation to perform')


class UnaryMaths(MathsCommand):

    """

    Use niftysegmaths (seg_maths) to perorm a variety of mathematical unary operations on an image.
    mandatory input specs is operation
    Examples
    --------
    from nipype.interfaces.niftyseg import UnaryMaths
    squarerooter = UnaryMaths()
    squarerooter.inputs.in_file = "T1.nii.gz"
    squarerooter.inputs.out_file = "T1-sqrt.nii.gz"
    squarerooter.inputs.operation = "sqrt"
    squarerooter.inputs.output_datatype = "float"
    squarerooter.cmdline
    seg_maths T1.nii.gz -sqrt -odt float T1-sqrt.nii.gz

    available operations:

	* * Operations on 3-D and 4-D images* *
	-sqrt 			Square root of the image.<commit_msg>I have change the scale flag for niftysseg, it was wrong<commit_after># emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
    The maths module provides higher-level interfaces to some of the operations
    that can be performed with the niftysegmaths (seg_maths) command-line program.
"""
import os
import numpy as np

from nipype.interfaces.niftyseg.base import NIFTYSEGCommand, NIFTYSEGCommandInputSpec, getNiftySegPath
from nipype.interfaces.base import (TraitedSpec, File, traits, InputMultiPath,
                                    isdefined)

class MathsInput(NIFTYSEGCommandInputSpec):
    
    in_file = File(position=2, argstr='%s', exists=True, mandatory=True,
                desc='image to operate on')
    out_file = File(genfile=True, position=-2, argstr='%s', desc='image to write')

    _dtypes = ['float', 'char', 'int', 'short', 'double', 'input']
    output_datatype = traits.Enum(*_dtypes,
                                  position=-3, argstr='-odt %s',
                                  desc='datatype to use for output (default uses input type)')
    
class MathsOutput(TraitedSpec):

    out_file = File(exists=True, desc='image written after calculations')

class MathsCommand(NIFTYSEGCommand):

    _cmd = getNiftySegPath('seg_maths')
    input_spec = MathsInput
    output_spec = MathsOutput
    _suffix = '_maths'

    def _list_outputs(self):
        outputs = self.output_spec().get()
        outputs['out_file'] = self.inputs.out_file
        if not isdefined(self.inputs.out_file):
            outputs['out_file'] = self._gen_fname(self.inputs.in_file, suffix=self._suffix)
        outputs['out_file'] = os.path.abspath(outputs['out_file'])
        return outputs

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

class UnaryMathsInput(MathsInput):

    operation = traits.Enum('exp', 'log', 'recip', 'sqrt', 'abs', 'bin', 
                            'otsu', 'lconcomp', 'fill', 'euc', 
                            'tpmax', 'tmean', 'tmin', 'tmax',
                            'splitlab',
                            'subsamp2', 'scl', 'range',
                            argstr='-%s', position=4, mandatory=True,
                            desc='operation to perform')


class UnaryMaths(MathsCommand):

    """

    Use niftysegmaths (seg_maths) to perorm a variety of mathematical unary operations on an image.
    mandatory input specs is operation
    Examples
    --------
    from nipype.interfaces.niftyseg import UnaryMaths
    squarerooter = UnaryMaths()
    squarerooter.inputs.in_file = "T1.nii.gz"
    squarerooter.inputs.out_file = "T1-sqrt.nii.gz"
    squarerooter.inputs.operation = "sqrt"
    squarerooter.inputs.output_datatype = "float"
    squarerooter.cmdline
    seg_maths T1.nii.gz -sqrt -odt float T1-sqrt.nii.gz

    available operations:

	* * Operations on 3-D and 4-D images* *
	-sqrt 			Square root of the image.
{'fraction_non_alphanumeric': 0.11742235509051653, 'fraction_numerical': 0.004982561036372696, 'mean_word_length': 2.578134284016637, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5198383', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1864, 'n_words': 455}