3
Ydr#                 @   s   d dl Z d dlZd dlZddlmZmZ ddlm	Z	m
Z
 ddlmZmZmZmZmZmZmZ G dd	 d	eZG d
d deZG dd de	ZG dd deZG dd deZG dd de	ZG dd deZG dd deZG dd de	ZdS )    N   )split_filenamefname_presuffix   )NipyBaseInterface	have_nipy   )TraitedSpectraitsBaseInterfaceInputSpec	isdefinedFileInputMultiPathOutputMultiPathc               @   sJ   e Zd ZeddddZedddZejddZejddZ	ej
d	dZd
S )ComputeMaskInputSpecTz:mean EPI image, used to compute the threshold for the mask)exists	mandatorydesczTreference volume used to compute the mask. If none is give, the mean volume is used.)r   r   z/lower fraction of the histogram to be discarded)r   z/upper fraction of the histogram to be discardedz)Keep only the largest connected componentN)__name__
__module____qualname__r   mean_volumereference_volumer
   FloatmMZBoolcc r   r   S/var/www/html/virt/lib/python3.6/site-packages/nipype/interfaces/nipy/preprocess.pyr      s   r   c               @   s   e Zd ZeddZdS )ComputeMaskOutputSpecT)r   N)r   r   r   r   
brain_maskr   r   r   r   r   '   s   r   c               @   s$   e Zd ZeZeZdd Zdd ZdS )ComputeMaskc       
      C   s   ddl m} i }xVdd t| jj D D ]:}t| j|}t|r*|dkr\tjt	j
|j}|||< q*W |f |}t| jj\}}}	tjjd||	f | _t	jt	j|jtjtj| j |S )	Nr   )compute_maskc             S   s"   g | ]\}}|t  j kr|qS r   )r   Ztrait_names).0k_r   r   r   
<listcomp>4   s   z.ComputeMask._run_interface.<locals>.<listcomp>r   r   z
%s_mask.%s)r   r   )Znipy.labs.maskr"   listinputsitemsgetattrr   npZ
asanyarraynbloaddataobjr   r   ospathabspath_brain_mask_pathsaveNifti1ImageZastypeZuint8niiaffine)
selfruntimer"   argskeyvaluer    r%   nameextr   r   r   _run_interface/   s"    
zComputeMask._run_interfacec             C   s   | j  j }| j|d< |S )Nr    )_outputsgetr2   )r7   outputsr   r   r   _list_outputsH   s    
zComputeMask._list_outputsN)	r   r   r   r   
input_specr   output_specr>   rB   r   r   r   r   r!   +   s   r!   c               @   s   e Zd ZeeddddddZejddgdZej	ej
ej ejd	d
ddddddddZej	ejej
dddddgdZdS )SpaceTimeRealignerInputSpecT)r   z	0.4.0.devzFile to realign)r   Zmin_verr   zTR in secondsslice_times)r   requiresZ	asc_alt_2Zasc_alt_2_1Zasc_alt_halfZasc_alt_siemensZ	ascendingZ
desc_alt_2Zdesc_alt_halfZ
descendingzActual slice acquisition times.)r   r   )Zmin_lenmax_lena  Single integer or length 2 sequence If int, the axis in `images` that is the slice axis.  In a 4D image, this will often be axis = 2.  If a 2 sequence, then elements are ``(slice_axis, slice_direction)``, where ``slice_axis`` is the slice axis in the image as above, and ``slice_direction`` is 1 if the slices were acquired slice 0 first, slice -1 last, or -1 if acquired slice -1 first, slice 0 last.  If `slice_info` is an int, assume ``slice_direction`` == 1.N)r   r   r   r   r   in_filer
   r   trZEitherListEnumrF   Int
slice_infor   r   r   r   rE   N   s(   rE   c               @   s0   e Zd ZeeddddZeeddddZdS )SpaceTimeRealignerOutputSpecT)r   zRealigned files)r   z3Motion parameter files. Angles are not euler anglesN)r   r   r   r   r   out_filepar_filer   r   r   r   rO   w   s   rO   c               @   s0   e Zd ZdZeZeZddgZdd Z	dd Z
dS )	SpaceTimeRealignera|  Simultaneous motion and slice timing correction algorithm

    If slice_times is not specified, this algorithm performs spatial motion
    correction

    This interface wraps nipy's SpaceTimeRealign algorithm [Roche2011]_ or simply the
    SpatialRealign algorithm when timing info is not provided.

    Examples
    --------
    >>> from nipype.interfaces.nipy import SpaceTimeRealigner
    >>> #Run spatial realignment only
    >>> realigner = SpaceTimeRealigner()
    >>> realigner.inputs.in_file = ['functional.nii']
    >>> res = realigner.run() # doctest: +SKIP

    >>> realigner = SpaceTimeRealigner()
    >>> realigner.inputs.in_file = ['functional.nii']
    >>> realigner.inputs.tr = 2
    >>> realigner.inputs.slice_times = list(range(0, 3, 67))
    >>> realigner.inputs.slice_info = 2
    >>> res = realigner.run() # doctest: +SKIP


    References
    ----------
    .. [Roche2011] Roche A. A four-dimensional registration algorithm with        application to joint correction of motion and slice timing        in fMRI. IEEE Trans Med Imaging. 2011 Aug;30(8):1546-54. DOI_.

    .. _DOI: https://doi.org/10.1109/TMI.2011.2131152

    zslice timingzmotion correctionc                s|  ddl m}m   fdd| jjD }t| jjsHddlm} ||}n(ddl	m
} ||| jj| jj| jjd}|jd d |j }g | _g | _xt|D ]\}}	| jjtjjd	t| jj| d
   ||	| j|  | jjtjjdtjj| jj| d
   t| j| d}
|j| }xJt|D ]>\}}dd tj|j|jfD }dj|d }|
j| q*W |
j   qW |S )Nr   )
save_image
load_imagec                s   g | ]} |qS r   r   )r#   fname)rT   r   r   r&      s    z5SpaceTimeRealigner._run_interface.<locals>.<listcomp>)SpaceRealign)SpaceTimeRealign)rJ   rF   rN   )Zrefscanzcorr_%s.nii.gzr   z%s.parwc             S   s   g | ]}d | qS )z%.10fr   )r#   itemr   r   r   r&      s     
)!ZnipyrS   rT   r(   rI   r   rF   Z3nipy.algorithms.registration.groupwise_registrationrV   Znipy.algorithms.registrationrW   rJ   rN   ZestimateZresample_out_file_path_par_file_path	enumerateappendr/   r0   r1   r   splitopenZ_transformsr+   ZhstacktranslationZrotationjoinwriteclose)r7   r8   rS   Zall_imsrV   RrW   Zcorr_runjZcorrZmfileZmotionimoparamsstringr   )rT   r   r>      s<    
&
z!SpaceTimeRealigner._run_interfacec             C   s$   | j  j }| j|d< | j|d< |S )NrP   rQ   )r?   r@   r\   r]   )r7   rA   r   r   r   rB      s    

z SpaceTimeRealigner._list_outputsN)r   r   r   __doc__rE   rC   rO   rD   keywordsr>   rB   r   r   r   r   rR      s   !3rR   c               @   sT   e Zd ZeddddZejddddZejddddZedd	Z	ej
d
dddZdS )TrimInputSpecTzEPI image to trim)r   r   r   r   zfirst volume)Z
usedefaultr   z1last volume indexed as in python (and 0 for last)zoutput filename)r   Z_trimz2suffix for out_file to use if no out_file providedN)r   r   r   r   rI   r
   rM   begin_index	end_indexrP   Strsuffixr   r   r   r   rn      s   
rn   c               @   s   e Zd ZeddZdS )TrimOutputSpecT)r   N)r   r   r   r   rP   r   r   r   r   rs      s   rs   c               @   s(   e Zd ZdZeZeZdd Zdd Z	dS )TrimaH  Simple interface to trim a few volumes from a 4d fmri nifti file

    Examples
    --------
    >>> from nipype.interfaces.nipy.preprocess import Trim
    >>> trim = Trim()
    >>> trim.inputs.in_file = 'functional.nii'
    >>> trim.inputs.begin_index = 3 # remove 3 first volumes
    >>> res = trim.run() # doctest: +SKIP

    c             C   sz   | j  d }tj| jj}| jjdkr<t| jj|jd }nt| jj| jj}tj	|j
d|f |j|j}tj|| |S )NrP   r   r   .)rB   r,   r-   r(   rI   rp   slicero   shaper4   r.   r6   headerr3   )r7   r8   rP   r5   sZnii2r   r   r   r>     s    zTrim._run_interfacec             C   sZ   | j  j }| jj|d< t|d sBt| jjtj | jj	d|d< tj
j|d |d< |S )NrP   )newpathrr   )rD   r@   r(   rP   r   r   rI   r/   getcwdrr   r0   r1   )r7   rA   r   r   r   rB     s    zTrim._list_outputsN)
r   r   r   rl   rn   rC   rs   rD   r>   rB   r   r   r   r   rt      s
   rt   )r/   Znibabelr,   Znumpyr+   Zutils.filemanipr   r   baser   r   r	   r
   r   r   r   r   r   r   r   r!   rE   rO   rR   rn   rs   rt   r   r   r   r   <module>   s   $#)a