3
d                 @   s   d dl mZ ddlmZmZmZmZmZ d dlm	Z	 G dd deZ
G dd deZG d	d
 d
eZdZedd eD ZG dd deZG dd deZG dd deZdd ZdS )   )fname_presuffix   )SimpleInterfaceTraitedSpecBaseInterfaceInputSpectraitsFile)LooseVersionc               @   sH   e Zd ZeddddZeddddZejddZej	ddddd	d
Z
dS )RescaleInputSpecTzSkull-stripped image to rescale)exists	mandatorydesczSkull-stripped reference imagez!Invert contrast of rescaled image)r   g        g      I@zPercentile to use for reference to allow for outliers - 1 indicates the 1st and 99th percentiles in the input file will be mapped to the 99th and 1st percentiles in the reference; 0 indicates minima and maxima will be mapped)lowhighvalue
usedefaultr   N)__name__
__module____qualname__r   in_fileref_filer   ZBoolinvertZRange
percentile r   r   9/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/image.pyr
   
   s   r
   c               @   s   e Zd ZedddZdS )RescaleOutputSpecTzRescaled image)r   r   N)r   r   r   r   out_filer   r   r   r   r      s   r   c               @   s    e Zd ZdZeZeZdd ZdS )RescaleaX  Rescale an image

    Rescales the non-zero portion of ``in_file`` to match the bounds of the
    non-zero portion of ``ref_file``.
    Reference values in the input and reference images are defined by the
    ``percentile`` parameter, and the reference values in each image are
    identified and the remaining values are scaled accordingly.
    In the case of ``percentile == 0``, the reference values are the maxima
    and minima of each image.
    If the ``invert`` parameter is set, the input file is inverted prior to
    rescaling.

    Examples
    --------

    To use a high-resolution T1w image as a registration target for a T2\*
    image, it may be useful to invert the T1w image and rescale to the T2\*
    range.
    Using the 1st and 99th percentiles may reduce the impact of outlier
    voxels.

    >>> from nipype.interfaces.image import Rescale
    >>> invert_t1w = Rescale(invert=True)
    >>> invert_t1w.inputs.in_file = 'structural.nii'
    >>> invert_t1w.inputs.ref_file = 'functional.nii'
    >>> invert_t1w.inputs.percentile = 1.
    >>> res = invert_t1w.run()  # doctest: +SKIP

    c             C   s  dd l }dd l}|j| jj}|j }|j| jjj }|dk}|dk}| jjd| jj g}	|j|| |	\}
}|j|| |	\}}|| ||
  }| jjr|| n||
 }||| |  }| jjrdnd}t	| jj||j
d}|j||j|jj| || jd< |S )N    g      Y@Z_invZ	_rescaled)suffixnewpathr   )numpynibabelloadinputsr   Z	get_fdatar   r   r   r   cwd	__class__affineheaderto_filename_results)selfruntimenpnbimgdataZref_dataZin_maskZref_maskqZin_lowZin_highZref_lowZref_highZscale_factorsignalZout_datar   r   r   r   r   _run_interfaceB   s&    
zRescale._run_interfaceN)	r   r   r   __doc__r
   
input_specr   output_specr3   r   r   r   r   r       s   r   RLAPSIc             c   s~   | ]v}t D ]l}t D ]b}||  ko2|  ko2|kn  rdD ]6}dD ],}dD ]"}dj|| || || fV  qLqDq<qq
qdS )r   r    N)r   r   )r   r   )r   r   )_axesjoin).0xyzijkr   r   r   	<genexpr>a   s   "rD   c               @   s*   e Zd ZeddddZejedddZdS )ReorientInputSpecTzInput image)r   r   r   zTarget axis orientation)r   r   N)	r   r   r   r   r   r   Enum_orientationsorientationr   r   r   r   rE   l   s   rE   c               @   s$   e Zd ZedddZedddZdS )ReorientOutputSpecTzReoriented image)r   r   z1Affine transform from input orientation to outputN)r   r   r   r   r   	transformr   r   r   r   rI   s   s   rI   c               @   s    e Zd ZdZeZeZdd ZdS )Reorienta  Conform an image to a given orientation

    Flips and reorder the image data array so that the axes match the
    directions indicated in ``orientation``.
    The default ``RAS`` orientation corresponds to the first axis being ordered
    from left to right, the second axis from posterior to anterior, and the
    third axis from inferior to superior.

    For oblique images, the original orientation is considered to be the
    closest plumb orientation.

    No resampling is performed, and thus the output image is not de-obliqued
    or registered to any other image or template.

    The effective transform is calculated from the original affine matrix to
    the reoriented affine matrix.

    Examples
    --------

    If an image is not reoriented, the original file is not modified

    .. testsetup::

        >>> def print_affine(matrix):
        ...     print(str(matrix).replace(']', ' ').replace('[', ' '))

    >>> import numpy as np
    >>> from nipype.interfaces.image import Reorient
    >>> reorient = Reorient(orientation='LPS')
    >>> reorient.inputs.in_file = 'segmentation0.nii.gz'
    >>> res = reorient.run()
    >>> res.outputs.out_file
    'segmentation0.nii.gz'

    >>> print_affine(np.loadtxt(res.outputs.transform))
    1.  0.  0.  0.
    0.  1.  0.  0.
    0.  0.  1.  0.
    0.  0.  0.  1.

    >>> reorient.inputs.orientation = 'RAS'
    >>> res = reorient.run()
    >>> res.outputs.out_file  # doctest: +ELLIPSIS
    '.../segmentation0_ras.nii.gz'

    >>> print_affine(np.loadtxt(res.outputs.transform))
    -1.   0.   0.  60.
     0.  -1.   0.  72.
     0.   0.   1.   0.
     0.   0.   0.   1.

    .. testcleanup::

        >>> import os
        >>> os.unlink(res.outputs.out_file)
        >>> os.unlink(res.outputs.transform)
    c             C   s   dd l }dd l}ddlm}m}m} | jj}|j|}|j	|j
}	|| jj}
||	|
}|||j}t|jtdkr|j|}n
t||}||k	rd| jjj  }t|||jd}|j| n|}t|d|jdd}|j||d	d
 || jd< || jd< |S )Nr   )axcodes2orntornt_transforminv_ornt_affz2.4.0_)r   r    z.matF)r   r    Zuse_extz%.08f)fmtr   rJ   )r!   r"   nibabel.orientationsrL   rM   rN   r$   r   r#   Zio_orientationr'   rH   shaper	   __version__Zas_reoriented_as_reoriented_backportlowerr   r%   r)   Zsavetxtr*   )r+   r,   r-   r.   rL   rM   rN   fnameZorig_imgZ	orig_orntZ	targ_orntrJ   Z
affine_xfm
reorientedr   Zout_nameZmat_namer   r   r   r3      s.    




zReorient._run_interfaceN)	r   r   r   r4   rE   r5   rI   r6   r3   r   r   r   r   rK   z   s   :rK   c       	         s   ddl }ddl}ddlm} |j ddgddgddggr>| S |j| j }| jj| | j	}| j
||| j}t||jr fdd| jj D }|jj|  |S )z1Backport of img.as_reoriented as of nibabel 2.4.0r   N)rN   r   r   c                s(   g | ] }|d krd nt  |df qS )Nr   )int)r=   Zorig_dim)orntr   r   
<listcomp>   s   z+_as_reoriented_backport.<locals>.<listcomp>)r!   r"   rQ   rN   Zarray_equalZapply_orientationZdataobjr'   dotrR   r&   r(   
isinstanceZ
Nifti1PairZget_dim_infoZset_dim_info)	r/   rY   r-   r.   rN   Zt_arrZnew_affrW   Znew_dimr   )rY   r   rT      s    
rT   N)r7   r8   r9   )Zutils.filemanipr   baser   r   r   r   r   r:   r	   r
   r   r   r;   tuplerG   rE   rI   rK   rT   r   r   r   r   <module>   s   ?

g