3
d                @   s  d Z ddlZddlmZ ddlZddlmZmZm	Z	m
Z
 ddlmZmZmZmZmZmZmZmZ dd	lmZmZmZmZmZmZ d
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#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,G d/d0 d0eZ-G d1d2 d2eZ.G d3d4 d4eZ/G d5d6 d6eZ0G d7d8 d8eZ1G d9d: d:eZ2G d;d< d<eZ3G d=d> d>eZ4G d?d@ d@eZ5G dAdB dBeZ6G dCdD dDeZ7G dEdF dFeZ8G dGdH dHeZ9G dIdJ dJeZ:G dKdL dLeZ;G dMdN dNeZ<G dOdP dPeZ=G dQdR dReZ>G dSdT dTeZ?G dUdV dVeZ@G dWdX dXeZAG dYdZ dZeZBG d[d\ d\eZCG d]d^ d^eZDG d_d` d`eZEG dadb dbeZFG dcdd ddeZGG dedf dfeZHG dgdh dheZIG didj djeZJG dkdl dleZKG dmdn dneZLG dodp dpeZMdS )qz$SPM wrappers for preprocessing data
    N)deepcopy   )fname_presuffixensure_listsimplify_listsplit_filename   )OutputMultiPathTraitedSpec	isdefinedtraitsInputMultiPathInputMultiObjectFileStr   )
SPMCommandscans_for_fname
func_is_3dscans_for_fnamesSPMCommandInputSpecImageFileSPMZrestructuredtextc               @   s  e Zd ZejdddddZedddddd	Zedddd
dd	Zej	ej
ej
ddddZejdddddZejddIddddZej
ddddZejdddddZejdddddZejddddddd Zejd!d"dd#d$d%Zejd!d!dd&d'd%Zejddd(d)dZeddd*d+d,Zejd!d-dd.d$d%Zejd!d/dd0d1d%Zejd!d2dd3d1d%Zej
d4dd5d6dZej
d7dd8d9dZedddd:d;d<Zejddd=d>dZed?dd@dAdZ ejdddBdCdZ!edddDdEd,Z"ejdddFdGdZ#dHS )JFieldMapInputSpeccalculatevdmTz1.9.0zAMust be 'calculatevdm'; to apply VDM, use the ApplyVDM interface.)
usedefault
deprecateddescFzsubj.data.presubphasemag.phasezpresubstracted phase file)	mandatoryexistscopyfilefieldr   z"subj.data.presubphasemag.magnitudezpresubstracted magnitude filezsubj.defaults.defaultsval.etzshort and long echo times)r   r    r   z#subj.defaults.defaultsval.maskbrainz"masking or no masking of the brain)r   r    r   r   z!subj.defaults.defaultsval.blipdirz"polarity of the phase-encode blipszsubj.defaults.defaultsval.tertztotal EPI readout timezsubj.defaults.defaultsval.epifmzepi-based field mapzsubj.defaults.defaultsval.ajmzjacobian modulationZMark3DZMark2DZ	Huttonishz!One of: Mark3D, Mark2D, Huttonishz'subj.defaults.defaultsval.uflags.method)r   r   r    r   
   z%subj.defaults.defaultsval.uflags.fwhmzgaussian smoothing kernel width)lowvaluer   r    r   z$subj.defaults.defaultsval.uflags.padzpadding kernel widthz#subj.defaults.defaultsval.uflags.wszweighted smoothingz)subj.defaults.defaultsval.mflags.templatez template image for brain masking)r   r   r    r      z%subj.defaults.defaultsval.mflags.fwhmr   z'subj.defaults.defaultsval.mflags.nerodeznumber of erosions   z(subj.defaults.defaultsval.mflags.ndilateg      ?z'subj.defaults.defaultsval.mflags.threshz7threshold used to create brain mask from segmented datag{Gz?z$subj.defaults.defaultsval.mflags.regz-regularization value used in the segmentationzsubj.session.epizEPI to unwarp)r   r   r   r    r   zsubj.matchvdmzmatch VDM to EPIz_run-zsubj.sessnamezVDM filename extensionzsubj.writeunwarpedzwrite unwarped EPIz	subj.anatzanatomical image for comparisonzsubj.matchanatzmatch anatomical image to EPIN)$__name__
__module____qualname__r   Enumjobtyper   
phase_filemagnitude_fileTupleFloatZ
echo_timesBoolZ	maskbrainZblip_directionZtotal_readout_timeZepifmZjacobian_modulationmethodRangeZunwarp_fwhmpadwstemplateZ	mask_fwhmZnerodeZndilateZthreshregepi_fileZmatchvdmr   ZsessnameZwriteunwarped	anat_fileZ	matchanat r9   r9   B/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/spm/preprocess.pyr   )   s   r   c               @   s   e Zd ZedddZdS )FieldMapOutputSpecTzvoxel difference map)r   r   N)r'   r(   r)   r   vdmr9   r9   r9   r:   r;      s   r;   c                   sD   e Zd ZdZeZeZdZdZ	 fddZ
 fddZdd	 Z  ZS )
FieldMapa  Use the fieldmap toolbox from spm to calculate the voxel displacement map (VDM).

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=173

    .. important::

        This interface does not deal with real/imag magnitude images nor
        with the two phase files case.

    Examples
    --------
    >>> from nipype.interfaces.spm import FieldMap
    >>> fm = FieldMap()
    >>> fm.inputs.phase_file = 'phase.nii'
    >>> fm.inputs.magnitude_file = 'magnitude.nii'
    >>> fm.inputs.echo_times = (5.19, 7.65)
    >>> fm.inputs.blip_direction = 1
    >>> fm.inputs.total_readout_time = 15.6
    >>> fm.inputs.epi_file = 'epi.nii'
    >>> fm.run() # doctest: +SKIP

    toolsfieldmapc                s(   |dkrt t|S tt| j|||S )z+Convert input to appropriate format for spmr,   r-   r8   r7   )r,   r-   r8   r7   )r   r   superr=   _format_arg)selfoptspecval)	__class__r9   r:   rA      s    zFieldMap._format_argc                s   t t| j }d|d igS )z3validate spm fieldmap options if set to None ignorer   r   )r@   r=   _parse_inputs)rB   einputs)rF   r9   r:   rG      s    zFieldMap._parse_inputsc             C   s,   | j  j }| jj}t| jjdd|d< |S )NZvdm5_sc)prefixr<   )_outputsgetinputsr+   r   r,   )rB   outputsr+   r9   r9   r:   _list_outputs   s    zFieldMap._list_outputs)r'   r(   r)   __doc__r   
input_specr;   output_spec_jobtype_jobnamerA   rG   rN   __classcell__r9   r9   )rF   r:   r=      s   	r=   c               @   s   e Zd ZeedddddddZedddddZej	d	d
dddZ
ejd	dgdd	d	dddZejddddddZejej	 dddddZejdddZejdddd d!Zd"S )#ApplyVDMInputSpecT)r   z
data.scansz%list of filenames to apply the vdm to)r    r   r   r   zdata.vdmfilezVoxel displacement map to use)r    r   r   r   r   zroptions.pedirz9phase encode direction input data have been acquired with)r    r   r   r   zroptions.whichzjIf the first value is non-zero, reslice all images. If the second value is non-zero, reslice a mean image.)r    minlenmaxlenr   r   r%   r      zroptions.rinterpz)degree of b-spline used for interpolation)r#   r"   highr    r   r   zroptions.wrapz-Check if interpolation should wrap in [x,y,z])rV   rW   r    r   zroptions.maskz"True/False mask time series images)r    r   uzroptions.prefixz fieldmap corrected output prefix)r    r   r   N)r'   r(   r)   r   r   in_filesr   vdmfiler   IntZdistortion_directionListIntwrite_whichr2   interpolationList
write_wrapr0   
write_maskString
out_prefixr9   r9   r9   r:   rU     sT   
rU   c               @   s>   e Zd ZeejejeddeddddZedddZ	dS )ApplyVDMOutputSpecT)r   z+These will be the fieldmap corrected files.)r   z
Mean image)r   r   N)
r'   r(   r)   r	   r   Eitherra   r   	out_files
mean_imager9   r9   r9   r:   rf   8  s   rf   c                   sD   e Zd ZdZeZeZdZdZ	 fddZ
 fddZdd	 Z  ZS )
ApplyVDMa'  Use the fieldmap toolbox from spm to apply the voxel displacement map (VDM) to some epi files.

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=173

    .. important::

        This interface does not deal with real/imag magnitude images nor
        with the two phase files case.

    r>   r?   c                s(   |dkrt t|S tt| j|||S )z+Convert input to appropriate format for spmr[   r\   )r[   r\   )r   r   r@   rj   rA   )rB   rC   rD   rE   )rF   r9   r:   rA   Q  s    zApplyVDM._format_argc                s   t t| j }d|d igS )z3validate spm fieldmap options if set to None ignoreZapplyvdmr   )r@   rj   rG   )rB   rH   )rF   r9   r:   rG   X  s    zApplyVDM._parse_inputsc             C   s   | j  j }| jjd dk}| jjd dk}|rpt| jjd trT| jjd d }n| jjd }t|dd|d< |rg |d< x~tt	| jjD ]j\}}g }t|trxDtt	|D ]"\}}	t|	| jj
d}
|j|
 qW nt|| jj
d}|d j| qW |S )Nr   r   meanu)rI   ri   rh   )rJ   rK   rL   r_   
isinstancer[   listr   	enumerater   re   append)rB   rM   resliced_allresliced_meanfirst_imageidximgfZappliedvdm_runi
inner_imgfnewfiler9   r9   r:   rN   _  s*    
zApplyVDM._list_outputs)r'   r(   r)   rO   rU   rP   rf   rQ   rR   rS   rA   rG   rN   rT   r9   r9   )rF   r:   rj   @  s   
rj   c               @   s   e Zd ZeejejeddedddddddZej	dddd	Z
ejd
ddd	Zejdddd	Zejejej	 ej dddd	Zejej	 ej dddd	ZejdddddZdS )SliceTimingInputSpecT)r   scansz'list of filenames to apply slice timingF)r    r   r   r   Znslicesznumber of slices in a volume)r    r   r   trz6time between volume acquisitions (start to start time)tazDtime of volume acquisition. usually calculated as TR-(TR/num_slices)soz;1-based order or onset (in ms) in which slices are acquiredZrefslicez^1-based Number of the reference slice or reference time point if slice_order is in onsets (ms)arI   zslicetimed output prefix)r    r   r   N)r'   r(   r)   r   r   rg   ra   r   r[   r]   Z
num_slicesr/   Ztime_repetitionZtime_acquisitionZslice_orderZ	ref_slicerd   re   r9   r9   r9   r:   rx   |  s<   rx   c               @   s2   e Zd ZeejejeddeddddZdS )SliceTimingOutputSpecT)r   zslice time corrected files)r   N)	r'   r(   r)   r	   r   rg   ra   r   timecorrected_filesr9   r9   r9   r:   r~     s   r~   c                   s8   e Zd ZdZeZeZdZdZ	 fddZ
dd Z  ZS )SliceTiminga  Use spm to perform slice timing correction.

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=19

    Examples
    --------

    >>> from nipype.interfaces.spm import SliceTiming
    >>> st = SliceTiming()
    >>> st.inputs.in_files = 'functional.nii'
    >>> st.inputs.num_slices = 32
    >>> st.inputs.time_repetition = 6.0
    >>> st.inputs.time_acquisition = 6. - 6./32.
    >>> st.inputs.slice_order = list(range(32,0,-1))
    >>> st.inputs.ref_slice = 1
    >>> st.run() # doctest: +SKIP

    Ztemporalstc                s.   |dkrt t|dddS tt| j|||S )z+Convert input to appropriate format for spmr[   FT)keep4dseparate_sessions)r   r   r@   r   rA   )rB   rC   rD   rE   )rF   r9   r:   rA     s    zSliceTiming._format_argc                sn    j  j }g |d< t jj}xH|D ]@}t|trH fdd|D }nt| jjd}|d j	| q&W |S )Nr   c                s   g | ]}t | jjd qS ))rI   )r   rL   re   ).0in_f)rB   r9   r:   
<listcomp>  s    z-SliceTiming._list_outputs.<locals>.<listcomp>)rI   )
rJ   rK   r   rL   r[   rl   rm   r   re   ro   )rB   rM   filelistfrunr9   )rB   r:   rN     s    

zSliceTiming._list_outputs)r'   r(   r)   rO   rx   rP   r~   rQ   rR   rS   rA   rN   rT   r9   r9   )rF   r:   r     s   r   c               @   s(  e Zd ZeejeddejedddddddZej	dddd	dd
Z
ejdddddZejddddZejddddZejdddZeddddZejdddddZejej ddd d!d"Zejd#d$gd%d#d#dd&d'Zejddd(ddZejej ddd)d!d"Zejd*d+dZejd,d-dd.d/Zd0S )1RealignInputSpecT)r   datazlist of filenames to realign)r    r   r   r   estwriteestimatewritez!one of: estimate, write, estwrite)r   r   g        g      ?zeoptions.qualityz0.1 = fast, 1.0 = precise)r"   rY   r    r   zeoptions.fwhmzgaussian smoothing kernel width)r"   r    r   zeoptions.sepzsampling separation in mmzeoptions.rtmz6Indicate whether realignment is done to the mean image)r    r   zeoptions.weightzfilename of weighting image)r   r    r   r   rX   zeoptions.interpz)degree of b-spline used for interpolationr   zeoptions.wrapz-Check if interpolation should wrap in [x,y,z])rV   rW   r    r   r   r   zroptions.whichz"determines which images to reslice)r    rV   rW   r   r   zroptions.interpzroptions.wrapzroptions.maskzTrue/False mask output imagerzroptions.prefixzrealigned output prefix)r    r   r   N)r'   r(   r)   r   r   rg   r   ra   r[   r*   r+   r2   qualityfwhm
separationr0   register_to_meanr   
weight_imginterpr]   wrapr^   r_   write_interprb   rc   rd   re   r9   r9   r9   r:   r     sp   r   c               @   sv   e Zd ZedddZeejejeddeddddZ	eejejeddeddddZ
eeddddZd	S )
RealignOutputSpecTz$Mean image file from the realignment)r   r   )r   zCopies of all files passed to in_files. Headers will have been modified to align all images with the first, or optionally to first do that, extract a mean image, and re-align to that mean image.)r   zIf jobtype is write or estwrite, these will be the resliced files. Otherwise, they will be copies of in_files that have had their headers rewritten.z-Estimated translation and rotation parametersN)r'   r(   r)   r   ri   r	   r   rg   ra   modified_in_filesrealigned_filesrealignment_parametersr9   r9   r9   r:   r   )  s   	r   c                   sD   e Zd ZdZeZeZdZdZ	 fddZ
 fddZdd	 Z  ZS )
Realignaw  Use spm_realign for estimating within modality rigid body alignment

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=25

    Examples
    --------

    >>> import nipype.interfaces.spm as spm
    >>> realign = spm.Realign()
    >>> realign.inputs.in_files = 'functional.nii'
    >>> realign.inputs.register_to_mean = True
    >>> realign.run() # doctest: +SKIP

    spatialZrealignc                s@   |dkr,| j jdkrd}nd}t|d|dS tt| j|||S )z+Convert input to appropriate format for spmr[   r   FT)r   r   )rL   r+   r   r@   r   rA   )rB   rC   rD   rE   r   )rF   r9   r:   rA   \  s    zRealign._format_argc                s$   t t| j }d| jj |d igS )z2validate spm realign options if set to None ignorez%sr   )r@   r   rG   rL   r+   )rB   rH   )rF   r9   r:   rG   h  s    zRealign._parse_inputsc             C   s  | j  j }| jjd dk}| jjd dk}| jjdkrt| jjrLg |d< xX| jjD ]L}t|trn|d }n|}|d j	t
|dddd t|t rVt|rVP qVW | jjd	kr| jj|d
< | jjd	ks| jjdkr| jj|d< | jjdks| jjdkrt| jjd tr"| jjd d }n| jjd }|rDt
|dd|d< |rg |d
< xtt| jjD ]p\}}g }t|trxFtt|D ]$\}	}
t
|
| jjd}|j	| qW nt
|| jjd}|d
 j	| qdW |S )Nr   r   r   r   rp_z.txtF)rI   suffixuse_extr   r   r   r   Zmean)rI   ri   )rJ   rK   rL   r_   r+   r   r[   rl   rm   ro   r   r   rn   r   re   )rB   rM   rp   rq   rt   tmp_imgfrr   rs   realigned_runru   rv   rw   r9   r9   r:   rN   m  sL    

zRealign._list_outputs)r'   r(   r)   rO   r   rP   r   rQ   rR   rS   rA   rG   rN   rT   r9   r9   )rF   r:   r   F  s   r   c               @   s  e Zd ZeejeddejedddddddZe	dddd	Z
ejd
ddddZejd
dddZejd
dddZejdddZe	ddddZejdddddZejej dddd d!Zejej d"d"d#d$d!Zejddd%d&dZejd'gd(d)d)dd*d+Zejd,d-dZejej d)d.d/d0d!Zejej d)d.d1d2d!Zejd
d3d4dZejd5d6dZejd7gd8d)d)dd9d+Zejd:d;dd<d=Z ejd"d)gd>d"d"dd?d+Z!ejddd@ddZ"ejej dddAd d!Z#ejdBdCdZ$ejdDdEddFd=Z%dGS )HRealignUnwarpInputSpecT)r   z
data.scansz'list of filenames to realign and unwarp)r    r   r   r   zdata.pmscanzuVoxel displacement map to use in unwarping. Unlike SPM standard behaviour, the same map will be used for all sessionsF)r    r   r   g        g      ?zeoptions.qualityz0.1 = fast, 1.0 = precise)r"   rY   r    r   zeoptions.fwhmzgaussian smoothing kernel width)r"   r    r   zeoptions.sepzsampling separation in mmzeoptions.rtmz6Indicate whether realignment is done to the mean image)r    r   zeoptions.weightzfilename of weighting image)r   r    r   r   rX   zeoptions.einterpz)degree of b-spline used for interpolationr   zeoptions.ewrapz-Check if interpolation should wrap in [x,y,z])rV   rW   r    r   r   zuweoptions.basfcnz3Number of basis functions to use for each dimensionzuweoptions.regorderzThis parameter determines how to balance the compromise between likelihood maximization and smoothness maximization of the estimated field.i zuweoptions.lambdar   z0Regularisation factor. Default: 100000 (medium).)r    rV   rW   r   r   zuweoptions.jmzcJacobian deformations. In theory a good idea to include them,  in practice a bad idea. Default: No.   zuweoptions.fotzDFirst order effects should only depend on pitch and roll, i.e. [4 5]zuweoptions.sotz:List of second order terms to model second derivatives of.zuweoptions.uwfwhmz*gaussian smoothing kernel width for unwarpzuweoptions.remz<Re-estimate movement parameters at each unwarping iteration.r$   zuweoptions.noizNumber of iterations.ZAveragezuweoptions.exproundz;Point in position space to perform Taylor-expansion around.)r    r   r   zuwroptions.uwwhichz"determines which images to reslicezuwroptions.rinterpzuwroptions.wrapzuwroptions.maskzTrue/False mask output imagerZ   zuwroptions.prefixz$realigned and unwarped output prefixN)&r'   r(   r)   r   r   rg   r   ra   r[   r   	phase_mapr2   r   r   r   r0   r   r   r   r]   r   Zest_basis_funcZest_reg_orderr^   Zest_reg_factorZest_jacobian_deformationsZest_first_order_effectsZest_second_order_effectsZest_unwarp_fwhmZest_re_est_mov_parZest_num_of_iterationsrd   Zest_taylor_expansion_pointreslice_whichZreslice_interpZreslice_wrapZreslice_maskre   r9   r9   r9   r:   r     s   
r   c               @   sv   e Zd ZedddZeejejeddeddddZ	eejejeddeddddZ
eeddddZd	S )
RealignUnwarpOutputSpecTz0Mean image file from the realignment & unwarping)r   r   )r   zCopies of all files passed to in_files. Headers will have been modified to align all images with the first, or optionally to first do that, extract a mean image, and re-align to that mean image.)r   z-Realigned and unwarped files written to disc.z-Estimated translation and rotation parametersN)r'   r(   r)   r   ri   r	   r   rg   ra   r   realigned_unwarped_filesr   r9   r9   r9   r:   r   2  s   
	r   c                   sH   e Zd ZdZeZeZdZdZ	 fddZ
f f fdd	Zdd	 Z  ZS )
RealignUnwarpa  Use spm_uw_estimate for estimating within subject registration and unwarping
    of time series. Function accepts only one single field map. If in_files is a
    list of files they will be treated as separate sessions but associated to the
    same fieldmap.

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=31

    Examples
    --------

    >>> import nipype.interfaces.spm as spm
    >>> realignUnwarp = spm.RealignUnwarp()
    >>> realignUnwarp.inputs.in_files = ['functional.nii', 'functional2.nii']
    >>> realignUnwarp.inputs.phase_map = 'voxeldisplacemap.vdm'
    >>> realignUnwarp.inputs.register_to_mean = True
    >>> realignUnwarp.run() # doctest: +SKIP

    r   Zrealignunwarpc                s.   |dkrt t|dddS tt| j|||S )z+Convert input to appropriate format for spmr[   FT)r   r   )r   r   r@   r   rA   )rB   rC   rD   rE   )rF   r9   r:   rA   e  s    zRealignUnwarp._format_argc                s   t t| jf dd }t| jjr0|d d  nd t| jjrt| jjtrj fdd|d d D }nt	|d d  d	g}||d< |gS )
N)skipr   r   pmscan c                s   g | ]}t | d qS ))ry   r   )dict)r   sess)r   r9   r:   r   y  s    z/RealignUnwarp._parse_inputs.<locals>.<listcomp>ry   )ry   r   )
r@   r   rG   r   rL   r   r[   rl   rm   r   )rB   r   Zspmdictr   )rF   )r   r:   rG   m  s    zRealignUnwarp._parse_inputsc             C   st  | j  j }| jjd dk}| jjd dk}t| jjr@g |d< xX| jjD ]L}t|trb|d }n|}|d jt	|dddd t|t rJt
|rJP qJW t| jjd tr| jjd d }n| jjd }|rt	|dd	|d
< |rpg |d< xtt| jjD ]n\}}g }t|trNxFtt|D ]$\}	}
t	|
| jjd	}|j| q$W nt	|| jjd	}|d j| qW |S )Nr   r   r   r   z.txtF)rI   r   r   rk   )rI   ri   r   )rJ   rK   rL   r   r   r[   rl   rm   ro   r   r   rn   r   re   )rB   rM   rp   rq   rt   r   rr   rs   r   ru   rv   rw   r9   r9   r:   rN     s<    

zRealignUnwarp._list_outputs)r'   r(   r)   rO   r   rP   r   rQ   rR   rS   rA   rG   rN   rT   r9   r9   )rF   r:   r   K  s   r   c               @   s   e Zd ZeddddddZeeddddddd	Zejd
dddddZ	ee
ddddddZejdddddddZejej dddddZejej dddZejej dd dZejd!d"d#d$d%Zejej d&d&d'd(dZejd)d*dZejd+d,dd-d.Zd/S )0CoregisterInputSpecTrefzreference file to register toF)r   r   r    r   r   )r   sourcezfile to register to target)r    r   r   r   r   r   r   z!one of: estimate, write, estwrite)r   r   otherz files to apply transformation to)r    r   r   miZnmiZeccZncczeoptions.cost_funzcost function, one of:
                    'mi' - Mutual Information,
                    'nmi' - Normalised Mutual Information,
                    'ecc' - Entropy Correlation Coefficient,
                    'ncc' - Normalised Cross Correlation)r    r   r   zeoptions.fwhmz$gaussian smoothing kernel width (mm))rV   rW   r    r   zeoptions.sepzsampling separation in mmzeoptions.tolz*acceptable tolerance for each of 12 paramsr   rX   zroptions.interpz)degree of b-spline used for interpolation)r"   rY   r    r   r   zroptions.wrapz-Check if interpolation should wrap in [x,y,z]zroptions.maskzTrue/False mask output imager   zroptions.prefixzcoregistered output prefix)r    r   r   N)r'   r(   r)   r   targetr   r   r   r*   r+   r   apply_to_filesZcost_functionra   r/   r   r   Z	tolerancer2   r   r]   rb   r0   rc   rd   re   r9   r9   r9   r:   r     sp   r   c               @   s0   e Zd ZeeddddZeeddddZdS )CoregisterOutputSpecT)r   zCoregistered source files)r   zCoregistered other filesN)r'   r(   r)   r	   r   coregistered_sourcecoregistered_filesr9   r9   r9   r:   r     s   r   c                   sD   e Zd ZdZeZeZdZdZ	 fddZ
 fddZdd	 Z  ZS )

Coregisterao  Use spm_coreg for estimating cross-modality rigid body alignment

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=39

    Examples
    --------

    >>> import nipype.interfaces.spm as spm
    >>> coreg = spm.Coregister()
    >>> coreg.inputs.target = 'functional.nii'
    >>> coreg.inputs.source = 'structural.nii'
    >>> coreg.run() # doctest: +SKIP

    r   Zcoregc                s   |dks|dkr,| j jdkr,tt|ddS |dkrFtjt|tdS |dkr~| j jdkr~t| j jrvt|| j j S t|S t	t
| j|||S )z+Convert input to appropriate format for spmr   r   r   T)r   r   )dtype)rL   r+   r   r   nparrayobjectr   r   r@   r   rA   )rB   rC   rD   rE   )rF   r9   r:   rA     s    zCoregister._format_argc                sL   | j jdkr tt| jdd}ntt| jdd}| j j}d| |d igS )z5validate spm coregister options if set to None ignorer   r+   r   )r   z%sr   )r+   r   )rL   r+   r@   r   rG   )rB   rH   r+   )rF   r9   r:   rG   !  s    

zCoregister._parse_inputsc             C   s   | j  j }| jjdkr>t| jjr0| jj|d< | jj|d< n| jjdksV| jjdkrt| jjrg |d< x.t| jjD ]}|d jt	|| jj
d qxW g |d< x.t| jjD ]}|d jt	|| jj
d qW |S )Nr   r   r   r   r   )rI   )rJ   rK   rL   r+   r   r   r   r   ro   r   re   )rB   rM   rt   r9   r9   r:   rN   ,  s     zCoregister._list_outputs)r'   r(   r)   rO   r   rP   r   rQ   rR   rS   rA   rG   rN   rT   r9   r9   )rF   r:   r     s   r   c               @   s  e Zd ZedddddgddZeeddddgd	ddd
Zej	ddddddZ
eejeddejeddddddZeddddgdddZeddddZeddddZejdddZejdd dZej	d!d"d#d$d%dZejd&d'dZejd(d)dZejd*d+dZejd,d-dZejejej d.d.d/d0d1d1d2d3Zejej d4d.d.d5d3Zejd6d7d8d9d:Zejej d;d<dZej d=d>dd?d@Z!dAS )BNormalizeInputSpecTzeoptions.templateztemplate file to normalize toparameter_fileF)r   r    r   r   xorr   )r   zsubj.sourcezfile to normalize to template)r    r   r   r   r   r   estr   zEstimate, Write or do both)r   r   zsubj.resamplez files to apply transformation to)r    r   r   zsubj.matnamer   r5   z$normalization parameter file*_sn.mat)r    r   r   r   r   z
subj.wtsrcz"name of weighting image for sourcezeoptions.weightz$name of weighting image for templatezeoptions.smosrczsource smoothing)r    r   zeoptions.smorefztemplate smoothingmnisizenonezeoptions.regtypezmni, size, nonezeoptions.cutoffzCutoff of for DCT baseszeoptions.nitsz)Number of iterations of nonlinear warpingzeoptions.regzLthe amount of the regularization for the nonlinear part of the normalizationzroptions.preservez&True/False warped images are modulatedr   )rV   rW   zroptions.bbr   z3x2-element list of lists)r    rV   rW   r   zroptions.voxz3-element listr   rX   zroptions.interpz)degree of b-spline used for interpolation)r"   rY   r    r   zroptions.wrapz=Check if interpolation should wrap in [x,y,z] - list of boolswzroptions.prefixznormalized output prefix)r    r   r   N)"r'   r(   r)   r   r5   r   r   r   r   r*   r+   rg   ra   r   r   Zsource_weightZtemplate_weightr/   Zsource_image_smoothingZtemplate_image_smoothingaffine_regularization_typeZDCT_period_cutoffr]   Znonlinear_iterationsnonlinear_regularizationr0   write_preservewrite_bounding_boxwrite_voxel_sizesr2   r   rb   rd   re   r9   r9   r9   r:   r   H  s   




r   c               @   sB   e Zd ZeeddddZeeddddZeeddddZdS )NormalizeOutputSpecT)r   z1MAT files containing the normalization parameters)r   zNormalized source fileszNormalized other filesN)r'   r(   r)   r	   r   normalization_parametersnormalized_sourcenormalized_filesr9   r9   r9   r:   r     s
   r   c                   sD   e Zd ZdZeZeZdZdZ	 fddZ
 fddZdd	 Z  ZS )
	Normalizea0  use spm_normalise for warping an image to a template

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=203

    Examples
    --------
    >>> import nipype.interfaces.spm as spm
    >>> norm = spm.Normalize()
    >>> norm.inputs.source = 'functional.nii'
    >>> norm.run() # doctest: +SKIP

    r   	normalisec                s   |dkrt t|S |dkr(t t|S |dkr<tt|S |dkrXtjt|gtdS |d	krxt|dkrxtd| t	t
| j|||S )
z+Convert input to appropriate format for spmr5   r   r   r   )r   rb   r   z%s must have 3 elements)rb   )r   r   r   r   r   r   r   len
ValueErrorr@   r   rA   )rB   rC   rD   rE   )rF   r9   r:   rA     s    zNormalize._format_argc                s   t t| jd
d}t| jjrXt| jj}t| jjrD|j| jj t	||d d d< | jj
}|dkrt| jjst| jjrt| jj|d d d< d	| |d igS )z4Validate spm normalize options if set to None ignorer+   r   )r   r   subjresampler   r   z%s)r+   r   )r   r   )r@   r   rG   r   rL   r   r   r   extendr   r+   r   )rB   rH   
inputfilesr+   )rF   r9   r:   rG     s    
zNormalize._parse_inputsc                s  | j  j }| jj}|jdrdg |d< x,t| jjD ]}|d jt|ddd q4W t	|d |d< | jjdkrt
| jjr| jj|d< | jj|d< nd	| jjkrt
| jjr| jjrd
jd| jjg n| jj g |d< t
| jjr@t| jj}xH|D ]@}t|tr  fdd|D }nt| dg}|d j| qW t
| jjrg |d< x,t| jjD ]}|d jt| d qdW |S )Nr   r   z_sn.matF)r   r   r   r   r   r   r   mc                s   g | ]}t | d qS ))rI   )r   )r   r   )
prefixNormr9   r:   r     s    z+Normalize._list_outputs.<locals>.<listcomp>)rI   )rJ   rK   rL   r+   
startswithr   r   ro   r   r   r   r   r   joinre   rl   rm   r   )rB   rM   r+   rt   r   r   r   r9   )r   r:   rN     s>    

zNormalize._list_outputs)r'   r(   r)   rO   r   rP   r   rQ   rR   rS   rA   rG   rN   rT   r9   r9   )rF   r:   r     s   r   c               @   sz  e Zd ZeddddgdddZeejeddejedddddd	Z	ed
dddgdddZ
ejddddddZejddddddddddd
Zejd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/dZedd0d1dgdd2Zejd3d4d5d6d7dZejej d8d9d9d:d;Zejd<d=dZejd>d?dZejejej d@d@dAdBdCdCdDd;Zejej dEd@d@dFd;ZejddGdHdIdJZejdKdLddMdNZdOS )PNormalize12InputSpecTzsubj.volz.file to estimate normalization parameters withdeformation_file)r   r    r   r   r   r   )r   zsubj.resamplez files to apply transformation to)r    r   r   zsubj.defimage_to_aligntpmFzXfile y_*.nii containing 3 deformation fields for the deformation in x, y and z dimension)r    r   r   r   r   r   r   r   zEstimate, Write or do Both)r   r   r   gh㈵>g-C6?gMbP?g{Gz?g?r   r!   zeoptions.biasregzno(0) - extremely heavy (10))r    r      (   2   <   F   P   Z   d   n   x            Infzeoptions.biasfwhmz#FWHM of Gaussian smoothness of biaszeoptions.tpmz;template in form of tissue probablitiy maps to normalize to)r   r    r   r   r   r   r   r   zeoptions.affregzmni, size, nonezeoptions.regr$   z,controls balance between parameters and data)r    rV   rW   r   zeoptions.fwhmz5value (in mm) to smooth the data before normalizationzeoptions.sampz2Sampling distance on data for parameter estimationr   )rV   rW   zwoptions.bbr   zM3x2-element list of lists representing the bounding box (in mm) to be writtenzwoptions.voxzT3-element list representing the voxel sizes (in mm) of the written normalised imagesrX   zwoptions.interpz)degree of b-spline used for interpolation)r"   rY   r    r   r   zwoptions.prefixzNormalized output prefix)r    r   r   N)r'   r(   r)   r   r   r   r   rg   ra   r   r   r*   r+   bias_regularization	bias_fwhmr   r   r   r/   warping_regularizationZ
smoothnesssampling_distancer   r   r2   r   rd   re   r9   r9   r9   r:   r     s   r   c               @   sB   e Zd ZeeddddZeeddddZeeddddZdS )Normalize12OutputSpecT)r   zVNIfTI file containing 3 deformation fields for the deformation in x, y and z dimension)r   z)Normalized file that needed to be alignedzNormalized other filesN)r'   r(   r)   r	   r   deformation_fieldnormalized_imager   r9   r9   r9   r:   r     s   r   c                   sH   e Zd ZdZeZeZdZdZ	 fddZ
f f fdd	Zdd	 Z  ZS )
Normalize12a  uses SPM12's new Normalise routine for warping an image to a template.
    Spatial normalisation is now done via the segmentation routine (which was
    known as ``New Segment`` in SPM8). Note that the normalisation in SPM12
    is done towards a file containing multiple tissue probability maps, which
    was not the case in SPM8.

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=49

    Examples
    --------
    >>> import nipype.interfaces.spm as spm
    >>> norm12 = spm.Normalize12()
    >>> norm12.inputs.image_to_align = 'structural.nii'
    >>> norm12.inputs.apply_to_files = 'functional.nii'
    >>> norm12.run() # doctest: +SKIP

    r   r   c                s   |dkrt t|S |dkr(t t|S |dkr<tt|S |dkrXtjt|gtdS |d	krxt|dkrxtd| t	t
| j|||S )
z+Convert input to appropriate format for spmr   r   r   r   )r   r   r$   z%s must have 5 elements)r   )r   r   r   r   r   r   r   r   r   r@   r   rA   )rB   rC   rD   rE   )rF   r9   r:   rA     s    zNormalize12._format_argc                s   t t| jd
d}t| jjrZt| jj}t| jjrF|j| jjg t	||d d d< | jj
}|dkrt| jjst| jjrt| jj|d d d< d	| |d igS )z4validate spm normalize options if set to None ignorer+   r   )r   r   r   r   r   r   z%s)r+   r   )r   r   )r@   r   rG   r   rL   r   r   r   r   r   r+   r   )rB   r   rH   r   r+   )rF   r9   r:   rG     s    
zNormalize12._parse_inputsc             C   s6  | j  j }| jj}|jdrbg |d< x*t| jjD ]}|d jt|dd q4W t	|d |d< | jjdkrt
| jjr| jj|d< t| jjdd|d< nd	| jjkr2g |d< t
| jjrt| jj}xB|D ]:}t|trd
d |D }nt|ddg}|d j| qW t
| jjr2t| jjdd|d< |S )Nr   r   Zy_)rI   r   r   r   r   r   c             S   s   g | ]}t |d dqS )r   )rI   )r   )r   r   r9   r9   r:   r     s    z-Normalize12._list_outputs.<locals>.<listcomp>)rJ   rK   rL   r+   r   r   r   ro   r   r   r   r   rl   rm   r   )rB   rM   r+   rt   r   r   r   r9   r9   r:   rN     s2    


zNormalize12._list_outputs)r'   r(   r)   rO   r   rP   r   rQ   rR   rS   rA   rG   rN   rT   r9   r9   )rF   r:   r     s   r   c               @   sL  e Zd ZeedddddddZejej dddd	d
Z	ejej ddddd
Z
ejej ddddd
ZejdddZejddddddZejedddddZejej dddZejdddddd d!dZejd"d#dZejd$d%dZejd&d'd(d)d*d+d,d-d.d/d
Zejd0d1d2d3d4d5d6d7d8d9d:d;d<d=dZejd>d?dZedd@dAdBZdCS )DSegmentInputSpecT)r   r   zone scan per subjectF)r    r   r   r   r   z	output.GMa
  Options to produce grey matter images: c1*.img, wc1*.img and mwc1*.img.
            None: [False,False,False],
            Native Space: [False,False,True],
            Unmodulated Normalised: [False,True,False],
            Modulated Normalised: [True,False,False],
            Native + Unmodulated Normalised: [False,True,True],
            Native + Modulated Normalised: [True,False,True],
            Native + Modulated + Unmodulated: [True,True,True],
            Modulated + Unmodulated Normalised: [True,True,False])rV   rW   r    r   z	output.WMa  
            Options to produce white matter images: c2*.img, wc2*.img and mwc2*.img.
            None: [False,False,False],
            Native Space: [False,False,True],
            Unmodulated Normalised: [False,True,False],
            Modulated Normalised: [True,False,False],
            Native + Unmodulated Normalised: [False,True,True],
            Native + Modulated Normalised: [True,False,True],
            Native + Modulated + Unmodulated: [True,True,True],
            Modulated + Unmodulated Normalised: [True,True,False]z
output.CSFa  
            Options to produce CSF images: c3*.img, wc3*.img and mwc3*.img.
            None: [False,False,False],
            Native Space: [False,False,True],
            Unmodulated Normalised: [False,True,False],
            Modulated Normalised: [True,False,False],
            Native + Unmodulated Normalised: [False,True,True],
            Native + Modulated Normalised: [True,False,True],
            Native + Modulated + Unmodulated: [True,True,True],
            Modulated + Unmodulated Normalised: [True,True,False]zoutput.biascorz)True/False produce a bias corrected image)r    r   nolightthoroughzoutput.cleanupz:clean using estimated brain mask ('no','light','thorough')zopts.tpmz&list of gray, white & csf prob. (opt,)z
opts.ngausz,num Gaussians capture intensity distributionr   easternr   r   r   zopts.regtypezcPossible options: "mni", "eastern", "subj", "none" (no reguralisation), "" (no affine registration)zopts.warpregz,Controls balance between parameters and datazopts.warpcozCutoff of DCT basesr   gh㈵>g-C6?gMbP?g{Gz?g?r   r!   zopts.biasregzno(0) - extremely heavy (10)r   r   r   r   r   r   r   r   r   r   r   r   zopts.biasfwhmz#FWHM of Gaussian smoothness of biasz	opts.sampz2Sampling distance on data for parameter estimationzopts.mskz.Binary image to restrict parameter estimation )r   r    r   N)r'   r(   r)   r   r   r   r   ra   r0   Zgm_output_typeZwm_output_typeZcsf_output_typesave_bias_correctedr*   clean_masksr   tissue_prob_mapsr]   gaussians_per_classaffine_regularizationr/   r   Zwarp_frequency_cutoffr   r   r   
mask_imager9   r9   r9   r:   r     s   	





r   c               @   s   e Zd ZeddZeddZeddZeddZeddZeddZ	eddZ
ed	dZed
dZeddddZeddZedddZedddZdS )SegmentOutputSpecz!native space grey probability map)r   znormalized grey probability mapz*modulated, normalized grey probability mapz"native space white probability mapz normalized white probability mapz+modulated, normalized white probability mapz native space csf probability mapznormalized csf probability mapz)modulated, normalized csf probability mapz0.10bias_corrected_imagez%bias-corrected version of input image)r   new_namer   TzNormalization transformation)r   r   zInverse normalization infoN)r'   r(   r)   r   Znative_gm_imageZnormalized_gm_imageZmodulated_gm_imageZnative_wm_imageZnormalized_wm_imageZmodulated_wm_imageZnative_csf_imageZnormalized_csf_imageZmodulated_csf_imageZmodulated_input_imager  transformation_matinverse_transformation_matr9   r9   r9   r:   r   u  s    









r   c                   s8   e Zd ZdZeZeZdd Z fddZ	dd Z
  ZS )SegmentaE  use spm_segment to separate structural images into different
    tissue classes.

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=209

    Examples
    --------
    >>> import nipype.interfaces.spm as spm
    >>> seg = spm.Segment()
    >>> seg.inputs.data = 'structural.nii'
    >>> seg.run() # doctest: +SKIP

    c             K   s@   t  j}|r"d|kr"d| _d| _nd| _d| _t j| f| d S )Nz12.r>   Zoldsegr   preproc)r   versionrR   rS   __init__)rB   rL   _local_versionr9   r9   r:   r    s    zSegment.__init__c                sx   dddd}|dkr.t |tr&t|S t|S d|krDdd	 |D S |d
krTt|S |dkrd|| S tt| j|||S )z+Convert input to appropriate format for spmr   r   r   )r   r   r   r   r   output_typec             S   s   g | ]}t |qS r9   )int)r   vr9   r9   r:   r     s    z'Segment._format_arg.<locals>.<listcomp>r   r   )r   r   )rl   rm   r   r   r@   r  rA   )rB   rC   rD   rE   Zclean_masks_dict)rF   r9   r:   rA     s    
zSegment._format_argc             C   s   | j  j }| jjd }xtdddgD ]x\}}d| }tt| j|r(xVtdddgD ]D\}\}}t| j|| rXd||f }	t|d||d f d||	< qXW q(W t| jjr| jjrt|dd|d< t|ddd}
|
|d< t|ddd}||d< |S )Nr   gmwmcsfz%s_output_type	modulatedmw
normalizedr   nativer   z%s_%s_imagez%sc%dr   )rI   r   r  z_seg_sn.matF)r   r   r  z_seg_inv_sn.matr  )r  r  )r  r   )r  r   )	rJ   rK   rL   r   rn   r   getattrr   r   )rB   rM   r   ZtidxtissueZouttypers   imagerI   ZoutfieldZt_matZinvt_matr9   r9   r:   rN     s&    "zSegment._list_outputs)r'   r(   r)   rO   r   rP   r   rQ   r  rA   rN   rT   r9   r9   )rF   r:   r    s   r  c               @   s   e Zd ZeedddddddZejej ej ejej	ej	dddZ
ejejejeddej ej ejej	ej	ejej	ej	d	d
dZejdddddddZejejej dddej dddZejdddZejej	 dddddZdS )NewSegmentInputSpecT)r   zA list of files to be segmentedchannelF)r   r   r    r   zA tuple with the following fields:
            - bias reguralisation (0-10)
            - FWHM of Gaussian smoothness of bias
            - which maps to save (Field, Corrected) - a tuple of two boolean values)r   r    aL  A list of tuples (one per tissue) with the following fields:
            - tissue probability map (4D), 1-based index to frame
            - number of gaussians
            - which maps to save [Native, DARTEL] - a tuple of two boolean values
            - which maps to save [Unmodulated, Modulated] - a tuple of two boolean valuesr  r   r   r   r   zwarp.affregzmni, eastern, subj, none )r    r   r$   )rV   rW   zwarp.regzfWarping regularization parameter(s). Accepts float or list of floats (the latter is required by SPM12)z	warp.sampz2Sampling distance on data for parameter estimationr   z
warp.writez4Which deformation fields to write:[Inverse, Forward])rV   rW   r    r   N)r'   r(   r)   r   r   channel_filesr   r.   r/   r0   channel_infora   r]   tissuesr*   r   rg   r   r   write_deformation_fieldsr9   r9   r9   r:   r    sP   
r  c               @   s   e Zd ZejejeddddZejejeddddZejejeddddZejejeddddZ	e
eddddZe
eddd	dZe
eddd
dZe
eddZe
eddZdS )NewSegmentOutputSpecT)r   znative space probability maps)r   zdartel imported class imagesznormalized class imagesz!modulated+normalized class imageszNormalization transformationzbias corrected imageszbias field imagesN)r'   r(   r)   r   ra   r   native_class_imagesdartel_input_imagesnormalized_class_imagesmodulated_class_imagesr	   r  bias_corrected_imagesbias_field_imagesforward_deformation_fieldinverse_deformation_fieldr9   r9   r9   r:   r    s   r  c                   s8   e Zd ZdZeZeZdd Z fddZ	dd Z
  ZS )
NewSegmenta  Use spm_preproc8 (New Segment) to separate structural images into
    different tissue classes. Supports multiple modalities.

    NOTE: This interface currently supports single channel input only

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=43

    Examples
    --------
    >>> import nipype.interfaces.spm as spm
    >>> seg = spm.NewSegment()
    >>> seg.inputs.channel_files = 'structural.nii'
    >>> seg.inputs.channel_info = (0.0001, 60, (True, True))
    >>> seg.run() # doctest: +SKIP

    For VBM pre-processing [http://www.fil.ion.ucl.ac.uk/~john/misc/VBMclass10.pdf],
    TPM.nii should be replaced by /path/to/spm8/toolbox/Seg/TPM.nii

    >>> seg = NewSegment()
    >>> seg.inputs.channel_files = 'structural.nii'
    >>> tissue1 = (('TPM.nii', 1), 2, (True,True), (False, False))
    >>> tissue2 = (('TPM.nii', 2), 2, (True,True), (False, False))
    >>> tissue3 = (('TPM.nii', 3), 2, (True,False), (False, False))
    >>> tissue4 = (('TPM.nii', 4), 2, (False,False), (False, False))
    >>> tissue5 = (('TPM.nii', 5), 2, (False,False), (False, False))
    >>> seg.inputs.tissues = [tissue1, tissue2, tissue3, tissue4, tissue5]
    >>> seg.run() # doctest: +SKIP

    c             K   s@   t  j}|r"d|kr"d| _d| _nd| _d| _t j| f| d S )Nz12.r   r  r>   preproc8)r   r  rR   rS   r  )rB   rL   r	  r9   r9   r:   r  L  s    zNewSegment.__init__c       	         sp  |dkrri }t | jj|d< t| jjrl| jj}|d |d< |d |d< t|d d t|d d g|d	< |gS |d
kr&g }x|D ]}i }tjdj|d d t	|d d ggt
d|d< |d |d< t|d d t|d d g|d< t|d d t|d d g|d< |j| qW |S |dkrXtt| j||t|d t|d gS tt| j|||S dS )z+Convert input to appropriate format for spmr  r  volsr   biasregr   biasfwhmr   r   r  ,)r   r   ngausr  r   warpedr  N)r  r  )r   rL   r  r   r  r  r   r   r   strr   ro   r@   r&  rA   )	rB   rC   rD   rE   new_channelinfonew_tissuesr  
new_tissue)rF   r9   r:   rA   W  s0    $

.$$

zNewSegment._format_argc       	      C   s  | j  j }g |d< g |d< g |d< g |d< g |d< g |d< g |d< g |d< g |d	< d
}t| jjrpt| jj}xHt|D ]<}|d jg  |d jg  |d jg  |d jg  qzW x@| jjD ]2}t	|\}}}t| jjrxt
| jjD ]\}}|d d r4|d | jtjj|d|d |f  |d d rj|d | jtjj|d|d |f  |d d r|d | jtjj|d|d |f  |d d r|d | jtjj|d|d |f  qW n<x:t|D ].}|d | jtjj|d|d |f  qW |d jtjj|d|  t| jjr| jjd rj|d jtjj|d|  | jjd r|d	 jtjj|d|  t| jjr| jjd d r|d jtjj|d|  | jjd d r|d jtjj|d|  qW |S )Nr  r  r   r!  r  r"  r#  r%  r$  r$   r   r   z	c%d%s.niir   z
rc%d%s.niir   z
wc%d%s.niizmwc%d%s.niiz%s_seg8.matz	iy_%s.niizy_%s.niizBiasField_%s.niizm%s.nii)rJ   rK   r   rL   r  r   rangero   r  r   rn   ospathr   r  r  )	rB   rM   	n_classesru   filenamepthbaseextr  r9   r9   r:   rN   w  sl    ""zNewSegment._list_outputs)r'   r(   r)   rO   r  rP   r  rQ   r  rA   rN   rT   r9   r9   )rF   r:   r&  *  s    r&  c            
   @   s  e Zd Zejejeedddddddejej ej ejej	ej	dddd	ddZ
ejejejeddej ej ejej	ej	ejej	ej	d
ddZejdddddddZejejej dddej dddZejdddZejej	 dddddZdS )MultiChannelNewSegmentInputSpecT)r   zA list of files to be segmentedr  F)r   r   r    r   zA tuple with the following fields:
                    - bias reguralisation (0-10)
                    - FWHM of Gaussian smoothness of bias
                    - which maps to save (Field, Corrected) - a tuple of two boolean values)r   r    a  A list of tuples (one per each channel) with the following fields:
            - a list of channel files (only 1rst channel files will be segmented)
            - a tuple with the following channel-specific info fields:
              - bias reguralisation (0-10)
              - FWHM of Gaussian smoothness of bias
              - which maps to save (Field, Corrected) - a tuple of two boolean valuesaL  A list of tuples (one per tissue) with the following fields:
            - tissue probability map (4D), 1-based index to frame
            - number of gaussians
            - which maps to save [Native, DARTEL] - a tuple of two boolean values
            - which maps to save [Unmodulated, Modulated] - a tuple of two boolean valuesr  r   r   r   r   zwarp.affregzmni, eastern, subj, none )r    r   r$   )rV   rW   zwarp.regzfWarping regularization parameter(s). Accepts float or list of floats (the latter is required by SPM12)z	warp.sampz2Sampling distance on data for parameter estimationr   z
warp.writez4Which deformation fields to write:[Inverse, Forward])rV   rW   r    r   N)r'   r(   r)   r   ra   r.   r   r   r/   r0   channelsr]   r  r*   r   rg   r   r   r  r9   r9   r9   r:   r;    sX   
r;  c               @   s   e Zd ZejejeddddZejejeddddZejejeddddZejejeddddZ	e
eddddZe
eddd	dZe
eddd
dZe
eddZe
eddZdS ) MultiChannelNewSegmentOutputSpecT)r   znative space probability maps)r   zdartel imported class imagesznormalized class imagesz!modulated+normalized class imageszNormalization transformationzbias corrected imageszbias field imagesN)r'   r(   r)   r   ra   r   r  r  r   r!  r	   r  r"  r#  r$  r%  r9   r9   r9   r:   r=  
  s   r=  c                   s8   e Zd ZdZeZeZdd Z fddZ	dd Z
  ZS )MultiChannelNewSegmenta  Use spm_preproc8 (New Segment) to separate structural images into
    different tissue classes. Supports multiple modalities and multichannel inputs.

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=45

    Examples
    --------
    >>> import nipype.interfaces.spm as spm
    >>> seg = spm.MultiChannelNewSegment()
    >>> seg.inputs.channels = [('structural.nii',(0.0001, 60, (True, True)))]
    >>> seg.run() # doctest: +SKIP

    For VBM pre-processing [http://www.fil.ion.ucl.ac.uk/~john/misc/VBMclass10.pdf],
    TPM.nii should be replaced by /path/to/spm8/toolbox/Seg/TPM.nii

    >>> seg = MultiChannelNewSegment()
    >>> channel1= ('T1.nii',(0.0001, 60, (True, True)))
    >>> channel2= ('T2.nii',(0.0001, 60, (True, True)))
    >>> seg.inputs.channels = [channel1, channel2]
    >>> tissue1 = (('TPM.nii', 1), 2, (True,True), (False, False))
    >>> tissue2 = (('TPM.nii', 2), 2, (True,True), (False, False))
    >>> tissue3 = (('TPM.nii', 3), 2, (True,False), (False, False))
    >>> tissue4 = (('TPM.nii', 4), 2, (False,False), (False, False))
    >>> tissue5 = (('TPM.nii', 5), 2, (False,False), (False, False))
    >>> seg.inputs.tissues = [tissue1, tissue2, tissue3, tissue4, tissue5]
    >>> seg.run() # doctest: +SKIP

    c             K   s@   t  j}|r"d|kr"d| _d| _nd| _d| _t j| f| d S )Nz12.r   r  r>   r'  )r   r  rR   rS   r  )rB   rL   r	  r9   r9   r:   r  C  s    zMultiChannelNewSegment.__init__c                s  |dkrg }xz|D ]r}i }t |d |d< t|d rz|d }|d |d< |d |d< t|d d t|d d g|d< |j| qW |S |d	kr@g }x|D ]}	i }
tjd
j|	d d t|	d d ggtd|
d< |	d |
d< t|	d d t|	d d g|
d< t|	d d t|	d d g|
d< |j|
 qW |S |dkrrt	t
| j||t|d t|d gS t	t
| j|||S dS )z+Convert input to appropriate format for spmr<  r   r(  r   r)  r*  r   r   r  r+  )r   r   r,  r  r   r-  r  N)r   r   r  ro   r   r   r   r.  r   r@   r>  rA   )rB   rC   rD   rE   Znew_channelsr  r/  r0  r1  r  r2  )rF   r9   r:   rA   N  s6    
$

.$$

z"MultiChannelNewSegment._format_argc       
      C   s@  | j  j }g |d< g |d< g |d< g |d< g |d< g |d< g |d< g |d< g |d	< d
}t| jjrpt| jj}xHt|D ]<}|d jg  |d jg  |d jg  |d jg  qzW x| jjd d D ]}t	|\}}}t| jjrxt
| jjD ]\}}|d d r<|d | jtjj|d|d |f  |d d rr|d | jtjj|d|d |f  |d d r|d | jtjj|d|d |f  |d d r|d | jtjj|d|d |f  qW n<x:t|D ].}|d | jtjj|d|d |f  qW |d jtjj|d|  t| jjr| jjd rp|d jtjj|d|  | jjd r|d	 jtjj|d|  qW x| jjD ]}	x|	d D ]~}t	|\}}}t|	d r|	d d d r|d jtjj|d|  |	d d d r|d jtjj|d|  qW qW |S )Nr  r  r   r!  r  r"  r#  r%  r$  r$   r   r   z	c%d%s.niir   z
rc%d%s.niir   z
wc%d%s.niizmwc%d%s.niiz%s_seg8.matz	iy_%s.niizy_%s.niizBiasField_%s.niizm%s.nii)rJ   rK   r   rL   r  r   r3  ro   r<  r   rn   r4  r5  r   r  )
rB   rM   r6  ru   r7  r8  r9  r:  r  r  r9   r9   r:   rN   q  sr    "" z$MultiChannelNewSegment._list_outputs)r'   r(   r)   rO   r;  rP   r=  rQ   r  rA   rN   rT   r9   r9   )rF   r:   r>  "  s   #r>  c               @   sx   e Zd ZeedddddddZejejej	 dddej	 d	d
dZ
ejdddZejdddZejdddddZdS )SmoothInputSpecT)r   r   zlist of files to smoothF)r    r   r   r   r   )rV   rW   r   z!3-list of fwhm for each dimension)r    r   r   zData type of the output imagesZimz*A mask implied by a particular voxel valuesrI   zsmoothed output prefix)r    r   r   N)r'   r(   r)   r   r   r[   r   rg   ra   r/   r   r]   Z	data_typer0   Zimplicit_maskingrd   re   r9   r9   r9   r:   r?    s    
r?  c               @   s   e Zd ZeeddddZdS )SmoothOutputSpecT)r   zsmoothed files)r   N)r'   r(   r)   r	   r   smoothed_filesr9   r9   r9   r:   rA    s   rA  c                   s8   e Zd ZdZeZeZdZdZ	 fddZ
dd Z  ZS )Smootha`  Use spm_smooth for 3D Gaussian smoothing of image volumes.

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=55

    Examples
    --------
    >>> import nipype.interfaces.spm as spm
    >>> smooth = spm.Smooth()
    >>> smooth.inputs.in_files = 'functional.nii'
    >>> smooth.inputs.fwhm = [4, 4, 4]
    >>> smooth.run() # doctest: +SKIP
    r   Zsmoothc                st   |dkrt t|S |dkr`t|ts0|||gS t|tr`t|dkr\|d |d |d gS |S tt| j|||S )Nr[   r   r   r   )r[   )r   r   rl   rm   r   r@   rC  rA   )rB   rC   rD   rE   )rF   r9   r:   rA     s    


zSmooth._format_argc             C   sH   | j  j }g |d< x.t| jjD ]}|d jt|| jjd q"W |S )NrB  )rI   )rJ   rK   r   rL   r[   ro   r   re   )rB   rM   rt   r9   r9   r:   rN     s    zSmooth._list_outputs)r'   r(   r)   rO   r?  rP   rA  rQ   rR   rS   rA   rN   rT   r9   r9   )rF   r:   rC    s   rC  c               @   s   e Zd ZejejedddddddZejdddd	d
Zej	ddddddZ
ejejejddejejejejej	dddddddddd
ej	ddddddddddd d!d"Zejejejddejddd#d$dZd%S )&DARTELInputSpecT)r   zA list of files to be segmentedzwarp.imagesF)r   r    r   r   Templatezwarp.settings.templatezPrefix for template)r   r    r   LinearMembraneBendingzwarp.settings.rformz"Form of regularization energy term)r    r   r   r!   r   r%             @         i   r   g      ?r      zwarp.settings.paramzList of tuples for each iteration

  * Inner iterations
  * Regularization parameters
  * Time points for deformation model
  * smoothing parameter

)rV   rW   r    r   zwarp.settings.optimzpOptimization settings a tuple:

  * LM regularization
  * cycles of multigrid solver
  * relaxation iterations

N)r'   r(   r)   r   ra   r   image_filesr   template_prefixr*   regularization_formr.   r2   r/   iteration_parametersoptimization_parametersr9   r9   r9   r:   rD  	  sB   
	

rD  c               @   s@   e Zd ZedddZejeddddZejeddddZdS )	DARTELOutputSpecTzfinal DARTEL template)r   r   )r   z,Templates from different stages of iteration)r   zDARTEL flow fieldsN)	r'   r(   r)   r   final_template_filer   ra   template_filesdartel_flow_fieldsr9   r9   r9   r:   rU  >	  s   rU  c                   s8   e Zd ZdZeZeZdZdZ	 fddZ
dd Z  ZS )DARTELa]  Use spm DARTEL to create a template and flow fields

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=185

    Examples
    --------
    >>> import nipype.interfaces.spm as spm
    >>> dartel = spm.DARTEL()
    >>> dartel.inputs.image_files = [['rc1s1.nii','rc1s2.nii'],['rc2s1.nii', 'rc2s2.nii']]
    >>> dartel.run() # doctest: +SKIP

    r>   dartelc                s   |dkrt |dddS |dkr2dddd}|| S |d	krg }xN|D ]F}i }|d |d
< t|d |d< |d |d< |d |d< |j| qDW |S |dkri }|d |d< |d |d< |d |d
< |gS tt| j|||S dS )z+Convert input to appropriate format for spmrP  T)r   r   rR  r   r   r   )rF  rG  rH  rS  ZitsZrparamKr   ZslamrT  ZlmregZcycN)rP  )r   rm   ro   r@   rY  rA   )rB   rC   rD   rE   Zmapperparamsparam	new_param)rF   r9   r:   rA   Y	  s,    
zDARTEL._format_argc             C   s   | j  j }g |d< x6tdD ]*}|d jtjjd| jj|d f  qW tjjd| jj |d< g |d< xF| jj	d D ]6}t
|\}}}|d jtjjd	|| jj|f  qzW |S )
NrW  r   z	%s_%d.niir   z%s_6.niirV  rX  r   z	u_%s_%s%s)rJ   rK   r3  ro   r4  r5  realpathrL   rQ  rP  r   )rB   rM   ru   r7  r8  r9  r:  r9   r9   r:   rN   t	  s    " zDARTEL._list_outputs)r'   r(   r)   rO   rD  rP   rU  rQ   rR   rS   rA   rN   rT   r9   r9   )rF   r:   rY  F	  s   rY  c            
   @   s   e Zd ZeddddddZeedddddd	Zeeddd
ddddZej	ej
ej
ej
dddZej	ej
ej
ej
ej
ej
ej
dddZejdddZejejej
 dddej
 dddZdS )DARTELNorm2MNIInputSpecTFzDARTEL templatezmni_norm.template)r   r   r   r   r    )r   zDARTEL flow fields u_rc1*zmni_norm.data.subjs.flowfields)r   r   r    zFiles to apply the transform tozmni_norm.data.subjs.images)r   r    r   r   zVoxel sizes for output filezmni_norm.vox)r   r    zmni_norm.bbzmni_norm.preservez<Modulate out images - no modulation preserves concentrations)r    r   r   )rV   rW   zmni_norm.fwhmz!3-list of fwhm for each dimensionN)r'   r(   r)   r   template_filer   flowfield_filesr   r   r.   r/   
voxel_sizebounding_boxr0   modulaterg   ra   r   r9   r9   r9   r:   r`  	  sP   r`  c               @   s*   e Zd ZeeddddZedddZdS )DARTELNorm2MNIOutputSpecT)r   zNormalized files in MNI space)r   z!Transform parameters to MNI space)r   r   N)r'   r(   r)   r	   r   r   normalization_parameter_filer9   r9   r9   r:   rf  	  s   rf  c                   s8   e Zd ZdZeZeZdZdZ	 fddZ
dd Z  ZS )DARTELNorm2MNIa  Use spm DARTEL to normalize data to MNI space

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=188

    Examples
    --------
    >>> import nipype.interfaces.spm as spm
    >>> nm = spm.DARTELNorm2MNI()
    >>> nm.inputs.template_file = 'Template_6.nii'
    >>> nm.inputs.flowfield_files = ['u_rc1s1_Template.nii', 'u_rc1s3_Template.nii']
    >>> nm.inputs.apply_to_files = ['c1s1.nii', 'c1s3.nii']
    >>> nm.inputs.modulate = True
    >>> nm.run() # doctest: +SKIP

    r>   rZ  c                s   |dkrt j|gtdS |dkr,t|ddS |dkrBt|dddS |dkrRt|S |d	krbt|S |d
krt|trx|S |||gS ntt| j|||S dS )z+Convert input to appropriate format for spmra  )r   rb  T)r   r   )r   r   rc  rd  r   N)ra  )rb  )r   )	r   r   r   r   rm   rl   r@   rh  rA   )rB   rC   rD   rE   )rF   r9   r:   rA   	  s    
zDARTELNorm2MNI._format_argc             C   s   | j  j }t| jj\}}}tjj|d |d< g |d< d}t| jj	rZ| jj	rZd| }t| jj
 st| jj
dkr|d| }x>| jjD ]2}t|\}}}|d jtjjd|||f  qW |S )	Nz	_2mni.matrg  r   r   r   r   r@  z%s%s%s)rJ   rK   r   rL   ra  r4  r5  r_  r   re  r   r   ro   )rB   rM   r8  r9  r:  rI   r7  r9   r9   r:   rN   	  s    zDARTELNorm2MNI._list_outputs)r'   r(   r)   rO   r`  rP   rf  rQ   rR   rS   rA   rN   rT   r9   r9   )rF   r:   rh  	  s   rh  c               @   sn   e Zd ZeedddddddZeeddddddd	Zejd
ddddZ	ejd
ddddZ
ejdddZdS )CreateWarpedInputSpecT)r   zA list of files to be warpedzcrt_warped.imagesF)r   r   r    r   zDARTEL flow fields u_rc1*zcrt_warped.flowfields)r   r   r    r   r   	   z4The number of iterations: log2(number of time steps)zcrt_warped.K)r"   rY   r   r    rX   zcrt_warped.interpz)degree of b-spline used for interpolation)r"   rY   r    r   zcrt_warped.jactransfzModulate images)r    r   N)r'   r(   r)   r   r   rP  rb  r   r2   Z
iterationsr   r0   re  r9   r9   r9   r:   ri  	  s.   ri  c               @   s   e Zd ZejedddZdS )CreateWarpedOutputSpecTzfinal warped files)r   r   N)r'   r(   r)   r   ra   r   warped_filesr9   r9   r9   r:   rk  
  s   rk  c                   s8   e Zd ZdZeZeZdZdZ	 fddZ
dd Z  ZS )CreateWarpeda  Apply a flow field estimated by DARTEL to create warped images

    http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#page=190

    Examples
    --------
    >>> import nipype.interfaces.spm as spm
    >>> create_warped = spm.CreateWarped()
    >>> create_warped.inputs.image_files = ['rc1s1.nii', 'rc1s2.nii']
    >>> create_warped.inputs.flowfield_files = ['u_rc1s1_Template.nii', 'u_rc1s2_Template.nii']
    >>> create_warped.run() # doctest: +SKIP

    r>   rZ  c                sB   |dkrt |dddS |dkr*t |ddS tt| j|||S dS )	z+Convert input to appropriate format for spmrP  T)r   r   rb  )r   N)rP  )rb  )r   r@   rm  rA   )rB   rC   rD   rE   )rF   r9   r:   rA   5
  s
    zCreateWarped._format_argc             C   s   | j  j }g |d< xp| jjD ]d}t|\}}}t| jjrd| jjrd|d jtj	j
d||f  q|d jtj	j
d||f  qW |S )Nrl  zmw%s%szw%s%s)rJ   rK   rL   rP  r   r   re  ro   r4  r5  r_  )rB   rM   r7  r8  r9  r:  r9   r9   r:   rN   ?
  s     "zCreateWarped._list_outputs)r'   r(   r)   rO   ri  rP   rk  rQ   rR   rS   rA   rN   rT   r9   r9   )rF   r:   rm  !
  s   
rm  c               @   sN   e Zd ZeedddddZeddddZeddddZe	j
dd	d
ddZdS )ApplyDeformationFieldInputSpecT)r   fnames)r   r    zcomp{1}.def)r   r   r    zcomp{2}.id.spacer   rX   r   z)degree of b-spline used for interpolation)r"   rY   r    r   N)r'   r(   r)   r   r   r[   r   r   reference_volumer   r2   r   r9   r9   r9   r:   rn  K
  s   rn  c               @   s   e Zd ZeeddZdS )ApplyDeformationFieldOutputSpecT)r   N)r'   r(   r)   r	   r   rh   r9   r9   r9   r:   rq  V
  s   rq  c                   s4   e Zd ZeZeZdZdZ fddZ	dd Z
  ZS )ApplyDeformationsutilZdefsc                sR   |dkr|g}|d	kr$t |dddS |d
kr:t |dddS tt| j|||S dS )z+Convert input to appropriate format for spmr   rp  TF)r   r   r[   N)r   rp  )r   )r[   rp  )r   r@   rr  rA   )rB   rC   rD   rE   )rF   r9   r:   rA   a
  s    zApplyDeformations._format_argc             C   sT   | j  j }g |d< x:| jjD ].}tjj|\}}|d jtjjd|  qW |S )Nrh   zw%s)	rJ   rK   rL   r[   r4  r5  splitro   r_  )rB   rM   r7  _fnamer9   r9   r:   rN   n
  s    zApplyDeformations._list_outputs)r'   r(   r)   rn  rP   rq  rQ   rR   rS   rA   rN   rT   r9   r9   )rF   r:   rr  Z
  s   rr  c               @   sv  e Zd ZeedddddddZedddd	ZejdTej	 gd dddZ
ejddUddddZejddVd'dd(dZejddd)d*d+Zejddd,d-d+Zejd.d/dd0Zedd1d2Zejdd
d
dd3d4d+Zejd5dd6d7Zej	ddd8d9d+Zejddd:d7Zejddd;d7Zejddd<d7Zejdd
d
dd=d>d+Zejdd
ddd?d@d+ZejdddAd7ZejdddBd7Zejdd
d
ddCd>d+Zejdd
dddDd@d+Z ejdddEd7Z!ejdddFd7Z"ejdd
d
ddGd>d+Z#ejdd
dddHd@d+Z$ejdddId7Z%ejdddJd7Z&ejdddKd7Z'ejdddLd7Z(ejdddMd7Z)ejdd
dddNd@d+Z*ejdddOd7Z+ejdWejejddPdQd+Z,dRS )XVBMSegmentInputSpecT)r   zA list of files to be segmentedzestwrite.dataF)r   r    r   r   zestwrite.tpmztissue probability map)r   r    r   r   r   r%   r   z)number of gaussians for each tissue class)r   r   -C6?r   h㈵>MbP?{Gz?皙?r   r!   zestwrite.opts.biasregzno(0) - extremely heavy (10))r    r   r   r   r   r   r   r   r   r   r   r   r   r   r   zestwrite.opts.biasfwhmz#FWHM of Gaussian smoothness of biaszestwrite.opts.sampz2Sampling distance on data for parameter estimation)r   r    r   zestwrite.opts.warpregz,Controls balance between parameters and datarY   r"   )r   z.estwrite.extopts.dartelwarp.normhigh.darteltpm)r   r    zestwrite.extopts.sanlmz60=No denoising, 1=denoising,2=denoising multi-threadedg333333?zestwrite.extopts.mrf)r   r    zestwrite.extopts.cleanupz0=None,1=light,2=thoroughzestwrite.extopts.printzestwrite.output.GM.nativezestwrite.output.GM.warpedzestwrite.output.GM.modulatedz:0=none,1=affine+non-linear(SPM8 default),2=non-linear onlyzestwrite.output.GM.dartelz%0=None,1=rigid(SPM8 default),2=affinezestwrite.output.WM.nativezestwrite.output.WM.warpedzestwrite.output.WM.modulatedzestwrite.output.WM.dartelzestwrite.output.CSF.nativezestwrite.output.CSF.warpedzestwrite.output.CSF.modulatedzestwrite.output.CSF.dartelzestwrite.output.bias.nativezestwrite.output.bias.warpedzestwrite.output.bias.affinezestwrite.output.label.nativezestwrite.output.label.warpedzestwrite.output.label.dartelzestwrite.jacobian.warpedzestwrite.output.warpszforward and inverse fieldNr   r   r   r   r%   r   )r}  )r   ry  rx  rz  r{  r|  r   r!   )r   r   r   r   r   r   r   r   r   r   r   r   )r   r   )-r'   r(   r)   r   r   r[   r  r   r.   r]   r   r*   r   r   r/   r   r   spatial_normalizationdartel_templater2   Zuse_sanlm_denoising_filterZmrf_weightingZcleanup_partitionsr0   Zdisplay_resultsZ	gm_nativeZgm_normalizedZgm_modulated_normalizedZ	gm_dartelZ	wm_nativeZwm_normalizedZwm_modulated_normalizedZ	wm_dartelZ
csf_nativeZcsf_normalizedZcsf_modulated_normalizedZ
csf_dartelbias_corrected_nativebias_corrected_normalizedZbias_corrected_affinepve_label_nativepve_label_normalizedpve_label_darteljacobian_determinantr   r9   r9   r9   r:   rw  w
  s  

rw  c               @   s   e Zd ZejejeddddZejejeddddZejejeddddZejejeddddZ	e
eddddZe
eddd	dZe
eddd	dZe
eddZe
eddZe
eddZe
eddZe
eddZe
eddZd
S )VBMSegmentOuputSpecT)r   znative space probability maps)r   zdartel imported class imagesznormalized class imagesz!modulated+normalized class imageszNormalization transformationzbias corrected imagesN)r'   r(   r)   r   ra   r   r  r  r   r!  r	   r  r"   normalized_bias_corrected_imagespve_label_native_imagespve_label_normalized_imagespve_label_registered_imagesr$  r%  jacobian_determinant_imagesr9   r9   r9   r:   r  !  s(   r  c                   sD   e Zd ZdZeZeZdZdZ	dd Z
 fddZ fdd	Z  ZS )

VBMSegmentaE  Use VBM8 toolbox to separate structural images into different
    tissue classes.

    Example
    -------
    >>> import nipype.interfaces.spm as spm
    >>> seg = spm.VBMSegment()
    >>> seg.inputs.tissues = 'TPM.nii'
    >>> seg.inputs.dartel_template = 'Template_1_IXI550_MNI152.nii'
    >>> seg.inputs.bias_corrected_native = True
    >>> seg.inputs.gm_native = True
    >>> seg.inputs.wm_native = True
    >>> seg.inputs.csf_native = True
    >>> seg.inputs.pve_label_native = True
    >>> seg.inputs.deformation_field = (True, False)
    >>> seg.run() # doctest: +SKIP
    r>   Zvbm8c       
   	   C   s  | j  j }| jj}d}|r d}g g g g|d< g g g g|d< g g g g|d< g g g g|d< g |d< g |d< g |d	< g |d
< g |d< g |d< g |d< g |d< g |d< xJ| jjD ]<}t|\}}}|d jtjj	|d|  xt
dddgD ]|\}}	t| jd|	 r2|d | jtjj	|d|d |f  t| jd|	 dkrr|d | jtjj	|d|d |f  n>t| jd|	 dkr|d | jtjj	|d|d |f  t| jd|	 r|d | jtjj	|d||d |f  t| jd|	 dkr.|d | jtjj	|d||d |f  qt| jd|	 dkr|d | jtjj	|d||d |f  qW | jjr|d jtjj	|d |  | jjr|d jtjj	|d!||f  | jjdkr|d jtjj	|d"|  n*| jjdkr|d jtjj	|d#|  | jjr<|d jtjj	|d$|  | jjrf|d	 jtjj	|d%||f  | jjd& r|d jtjj	|d'||f  | jjd r|d
 jtjj	|d(||f  | jjr|r|d jtjj	|d)|  qW |S )*Nr   r   r  r  r   r!  r  r"  r  r%  r$  r  r  r  r  z%s_seg8.matr  r  r  z	%s_nativez	p%d%s.niir   z	%s_dartelz
rp%d%s.niir   zrp%d%s_affine.niiz%s_normalizedzw%sp%d%s.niiz%s_modulated_normalizedzmw%sp%d%s.niizm0w%sp%d%s.niizp0%s.niizw%sp0%s.niiz	rp0%s.niizrp0%s_affine.niizm%s.niiz
wm%s%s.niir   z
y_%s%s.niiziy_%s%s.niizjac_wrp1%s.nii)rJ   rK   rL   r~  r[   r   ro   r4  r5  r   rn   r  r  r  r  r  r  r   r  )
rB   rM   Z	do_dartelZ	dartel_pxr7  r8  r9  r:  ru   Ztisr9   r9   r:   rN   ]  s     "



zVBMSegment._list_outputsc                s   |dkrt |ddS |dkr.|dkrdg iS n\|dkrFtj|gtdS |dkrvtt| j||t|d
 t|d gS tt| j|||S dS )z+Convert input to appropriate format for spmr[   T)r   r~  r"   normlowr  )r   r   r   r   N)r[   )r~  )r  )r   )r   r   r   r   r@   r  rA   r  )rB   rC   rD   rE   )rF   r9   r:   rA     s    

zVBMSegment._format_argc                sP   | j jdkr:tt| jdd}ddi|d d d	 d
< |S tt| jddS d S )Nr"   r~  r  )r   r  r   r   r   ZextoptsZ
dartelwarp)r~  r  )rL   r~  r@   r  rG   )rB   rH   )rF   r9   r:   rG     s    
zVBMSegment._parse_inputs)r'   r(   r)   rO   rw  rP   r  rQ   rR   rS   rN   rA   rG   rT   r9   r9   )rF   r:   r  D  s   ir  )NrO   r4  copyr   numpyr   Zutils.filemanipr   r   r   r   r9  r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   Z__docformat__r   r;   r=   rU   rf   rj   rx   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r&  r;  r=  r>  r?  rA  rC  rD  rU  rY  r`  rf  rh  ri  rk  rm  rn  rq  rr  rw  r  r  r9   r9   r9   r:   <module>   s   (
 	 !56<+2IZ aJ	I^
]w\ I@ K ,9A2	=* +#