3
dN                 @   s  d Z ddlZddlZddlmZ ddlmZmZ ddl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 d
Zej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(dS )0zLProvides interfaces to various longitudinal commands provided by freesurfer
    N   )logging)split_filenamecopyfile   )	FSCommandFSTraitedSpecFSScriptCommandFSScriptOutputSpecFSCommandOpenMPFSTraitedSpecOpenMP   )	isdefinedTraitedSpecFiletraits	DirectoryZrestructuredtextznipype.interfacec               @   s>   e Zd ZeddddddZejdddddZedddd	Z	d
S )MPRtoMNI305InputSpec TZTODO)exists	mandatory
usedefaultdesczinput atlas file)r   r   r   z%sz%the input file prefix for MPRtoMNI305)argstrr   r   N)
__name__
__module____qualname__r   reference_dirr   Stringtargetr   in_file r!   r!   K/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/freesurfer/registration.pyr      s
   r   c               @   s   e Zd ZedddZdS )MPRtoMNI305OutputSpecFz6The output file '<in_file>_to_<target>_t4_vox2vox.txt')r   r   N)r   r   r   r   out_filer!   r!   r!   r"   r#   (   s   r#   c                   sT   e Zd ZdZdZeZeZ fddZ	 fddZ
dd Zd	d
 Z fddZ  ZS )MPRtoMNI305a  
    For complete details, see FreeSurfer documentation

    Examples
    --------
    >>> from nipype.interfaces.freesurfer import MPRtoMNI305, Info
    >>> mprtomni305 = MPRtoMNI305()
    >>> mprtomni305.inputs.target = 'structural.nii'
    >>> mprtomni305.inputs.reference_dir = '.' # doctest: +SKIP
    >>> mprtomni305.cmdline # doctest: +SKIP
    'mpr2mni305 output'
    >>> mprtomni305.inputs.out_file = 'struct_out' # doctest: +SKIP
    >>> mprtomni305.cmdline # doctest: +SKIP
    'mpr2mni305 struct_out' # doctest: +SKIP
    >>> mprtomni305.inputs.environ['REFDIR'] == os.path.join(Info.home(), 'average') # doctest: +SKIP
    True
    >>> mprtomni305.inputs.environ['MPR2MNI305_TARGET'] # doctest: +SKIP
    'structural'
    >>> mprtomni305.run() # doctest: +SKIP

    Z
mpr2mni305c                s6   t t| jf | | jj| jd | jj| jd d S )Nr   r   )superr%   __init__inputsZon_trait_change_environ_update)selfr(   )	__class__r!   r"   r'   I   s    zMPRtoMNI305.__init__c                sV   |dkrdS |dkrBt |\}}}t|tjj|| ddd |S tt| j|||S )	Nr   r   r   r    Tcontent)copyZ
hashmethod)r   r   )r   r   ospathabspathr&   r%   _format_arg)r*   optspecval_retvalext)r+   r!   r"   r1   N   s    zMPRtoMNI305._format_argc             C   s,   | j j}| j j}|| j jd< || j jd< d S )NZMPR2MNI305_TARGETZREFDIR)r(   r   r   environ)r*   Zrefdirr   r!   r!   r"   r)   Z   s    zMPRtoMNI305._environ_updatec             C   s   t |d S )Nr   )r   )r*   fnamer!   r!   r"   
_get_fnamea   s    zMPRtoMNI305._get_fnamec                sD   t t| j }dj| j| jjd| jjddg}tj	j
||d< |S )Nr5   ZtoZt4zvox2vox.txtr$   )r&   r%   _list_outputsjoinr:   r(   r    r   r.   r/   r0   )r*   outputsfullname)r+   r!   r"   r;   d   s    zMPRtoMNI305._list_outputs)r   r   r   __doc___cmdr   
input_specr#   output_specr'   r1   r)   r:   r;   __classcell__r!   r!   )r+   r"   r%   .   s   r%   c               @   sT   e Zd ZeddddddZeddddddZeddddd	dZed
dddddZdS )RegisterAVItoTalairachInputSpecz%sTr   zThe input file)r   r   r   positionr   r   zThe target filer   zThe vox2vox fileztalairach.auto.xfmr   zThe transform output)r   r   rE   r   N)r   r   r   r   r    r   Zvox2voxr$   r!   r!   r!   r"   rD   s   s   rD   c               @   s   e Zd ZedddZdS ) RegisterAVItoTalairachOutputSpecFz*The output file for RegisterAVItoTalairach)r   r   N)r   r   r   r   r$   r!   r!   r!   r"   rF      s   rF   c               @   s$   e Zd ZdZdZeZeZdd Z	dS )RegisterAVItoTalairachaL  
    converts the vox2vox from talairach_avi to a talairach.xfm file

    This is a script that converts the vox2vox from talairach_avi to a
    talairach.xfm file. It is meant to replace the following cmd line:

    tkregister2_cmdl         --mov $InVol         --targ $FREESURFER_HOME/average/mni305.cor.mgz         --xfmout ${XFM}         --vox2vox talsrcimg_to_${target}_t4_vox2vox.txt         --noedit         --reg talsrcimg.reg.tmp.dat
    set targ = $FREESURFER_HOME/average/mni305.cor.mgz
    set subject = mgh-02407836-v2
    set InVol = $SUBJECTS_DIR/$subject/mri/orig.mgz
    set vox2vox = $SUBJECTS_DIR/$subject/mri/transforms/talsrcimg_to_711-2C_as_mni_average_305_t4_vox2vox.txt

    Examples
    ========

    >>> from nipype.interfaces.freesurfer import RegisterAVItoTalairach
    >>> register = RegisterAVItoTalairach()
    >>> register.inputs.in_file = 'structural.mgz'                         # doctest: +SKIP
    >>> register.inputs.target = 'mni305.cor.mgz'                          # doctest: +SKIP
    >>> register.inputs.vox2vox = 'talsrcimg_to_structural_t4_vox2vox.txt' # doctest: +SKIP
    >>> register.cmdline                                                   # doctest: +SKIP
    'avi2talxfm structural.mgz mni305.cor.mgz talsrcimg_to_structural_t4_vox2vox.txt talairach.auto.xfm'

    >>> register.run() # doctest: +SKIP
    Z
avi2talxfmc             C   s$   | j  j }tjj| jj|d< |S )Nr$   )rB   getr.   r/   r0   r(   r$   )r*   r=   r!   r!   r"   r;      s    z$RegisterAVItoTalairach._list_outputsN)
r   r   r   r?   r@   rD   rA   rF   rB   r;   r!   r!   r!   r"   rG      s
   rG   c            
   @   s   e Zd ZeddddddZeddddddZeddd	gd
dddddZejdddZ	eddddZ
ejdddZeddddZdS )EMRegisterInputSpecz%sTr   zin brain volume)r   r   r   rE   r   r   ztemplate gcaFr    z%s_transform.ltar   zoutput transform)r   r   name_sourcename_template
hash_fileskeep_extensionrE   r   z-skullz'align to atlas containing skull (uns=5))r   r   z-mask %szuse volume as a mask)r   r   r   z-uns %dzHalign to atlas containing skull setting unknown_nbr_spacing = nbrspacingz-t %szPreviously computed transformN)r   r   r   r   r    templater$   r   BoolZskullmaskIntZ
nbrspacingZ	transformr!   r!   r!   r"   rI      s&   rI   c               @   s   e Zd ZedddZdS )EMRegisterOutputSpecFzoutput transform)r   r   N)r   r   r   r   r$   r!   r!   r!   r"   rU      s   rU   c               @   s$   e Zd ZdZdZeZeZdd Z	dS )
EMRegistera  This program creates a tranform in lta format

    Examples
    ========
    >>> from nipype.interfaces.freesurfer import EMRegister
    >>> register = EMRegister()
    >>> register.inputs.in_file = 'norm.mgz'
    >>> register.inputs.template = 'aseg.mgz'
    >>> register.inputs.out_file = 'norm_transform.lta'
    >>> register.inputs.skull = True
    >>> register.inputs.nbrspacing = 9
    >>> register.cmdline
    'mri_em_register -uns 9 -skull norm.mgz aseg.mgz norm_transform.lta'
    Zmri_em_registerc             C   s$   | j  j }tjj| jj|d< |S )Nr$   )rB   rH   r.   r/   r0   r(   r$   )r*   r=   r!   r!   r"   r;      s    zEMRegister._list_outputsN)
r   r   r   r?   r@   rI   rA   rU   rB   r;   r!   r!   r!   r"   rV      s
   rV   c               @   st   e Zd ZedddddddZedddddd	Zedddd
dZeddddddZej	ddgddZ
eddddZdS )RegisterInputSpecz%sTr   z(Surface to register, often {hemi}.sphere)r   r   r   rE   r   r   r   z4The data to register to. In normal recon-all usage, z,this is a template file for average surface.)r   r   r   rE   r   zIUndocumented mandatory input file ${SUBJECTS_DIR}/surf/{hemisphere}.sulc )r   r   r   r   Fr   z+Output surface file to capture registration)r   r   rE   Zgenfiler   z-curvin_smoothwmz*Use smoothwm curvature for final alignment)r   requiresr   zCUndocumented input file ${SUBJECTS_DIR}/surf/{hemisphere}.smoothwm )r   r   r   NrN   rO   z`The data to register to. In normal recon-all usage, this is a template file for average surface.rP   )r   r   r   r   in_surfr   Zin_sulcr$   r   rR   curvrX   r!   r!   r!   r"   rW      sB    rW   c               @   s   e Zd ZedddZdS )RegisterOutputSpecFz+Output surface file to capture registration)r   r   N)r   r   r   r   r$   r!   r!   r!   r"   r\     s   r\   c                   s<   e Zd ZdZdZeZeZ fddZ	dd Z
dd Z  ZS )	Registera  This program registers a surface to an average surface template.

    Examples
    ========
    >>> from nipype.interfaces.freesurfer import Register
    >>> register = Register()
    >>> register.inputs.in_surf = 'lh.pial'
    >>> register.inputs.in_smoothwm = 'lh.pial'
    >>> register.inputs.in_sulc = 'lh.pial'
    >>> register.inputs.target = 'aseg.mgz'
    >>> register.inputs.out_file = 'lh.pial.reg'
    >>> register.inputs.curv = True
    >>> register.cmdline
    'mris_register -curv lh.pial aseg.mgz lh.pial.reg'
    Zmris_registerc                s"   |dkr|j S tt| j|||S )Nr[   )r   r&   r]   r1   )r*   r2   r3   r4   )r+   r!   r"   r1   4  s    zRegister._format_argc             C   s   |dkr| j  | S d S )Nr$   )r;   )r*   namer!   r!   r"   _gen_filename9  s    zRegister._gen_filenamec             C   sJ   | j  j }t| jjr.tjj| jj|d< ntjj| jjd |d< |S )Nr$   z.reg)	rB   rH   r   r(   r$   r.   r/   r0   rZ   )r*   r=   r!   r!   r"   r;   >  s
    zRegister._list_outputs)r   r   r   r?   r@   rW   rA   r\   rB   r1   r_   r;   rC   r!   r!   )r+   r"   r]     s   r]   c            
   @   sd   e Zd ZeddddddZeddddddZejd	d
ZejdddZ	eddddddgdddZ
dS )PaintInputSpecz%sTr   z1Surface file with grid (vertices) onto which the z+template data is to be sampled or 'painted')r   r   r   rE   r   r   zTemplate filez"Frame number of the input template)r   z-a %dzAverage curvature patterns)r   r   Fr   z%s.avg_curvrZ   z6File containing a surface-worth of per-vertex values, zsaved in 'curvature' format.)r   r   rE   rK   rL   rJ   rM   r   NrO   z\Surface file with grid (vertices) onto which the template data is to be sampled or 'painted'rN   rP   zRFile containing a surface-worth of per-vertex values, saved in 'curvature' format.)r   r   r   r   rZ   rQ   r   rT   template_paramZaveragesr$   r!   r!   r!   r"   r`   G  s*     r`   c               @   s   e Zd ZedddZdS )PaintOutputSpecFzRFile containing a surface-worth of per-vertex values, saved in 'curvature' format.)r   r   N)r   r   r   r   r$   r!   r!   r!   r"   rb   d  s   rb   c                   s4   e Zd ZdZdZeZeZ fddZ	dd Z
  ZS )Painta  
    This program is useful for extracting one of the arrays ("a variable")
    from a surface-registration template file. The output is a file
    containing a surface-worth of per-vertex values, saved in "curvature"
    format. Because the template data is sampled to a particular surface
    mesh, this conjures the idea of "painting to a surface".

    Examples
    ========
    >>> from nipype.interfaces.freesurfer import Paint
    >>> paint = Paint()
    >>> paint.inputs.in_surf = 'lh.pial'
    >>> paint.inputs.template = 'aseg.mgz'
    >>> paint.inputs.averages = 5
    >>> paint.inputs.out_file = 'lh.avg_curv'
    >>> paint.cmdline
    'mrisp_paint -a 5 aseg.mgz lh.pial lh.avg_curv'
    Zmrisp_paintc                sB   |dkr.t | jjr.|j|d t| jj  S tt| j|||S )NrQ   #)r   r(   ra   r   strr&   rc   r1   )r*   r2   r3   r4   )r+   r!   r"   r1     s    zPaint._format_argc             C   s$   | j  j }tjj| jj|d< |S )Nr$   )rB   rH   r.   r/   r0   r(   r$   )r*   r=   r!   r!   r"   r;     s    zPaint._list_outputs)r   r   r   r?   r@   r`   rA   rb   rB   r1   r;   rC   r!   r!   )r+   r"   rc   k  s   rc   c               @   s"  e Zd ZedddddZedddddgd	Zejded
ddddZejdedddZ	ejdedddZ
eddddZejddddgdgddZejddddddZejdej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d,dd!d-d.Zejejejejd/d0dZejejejejd1d2dZejejejejd3d4dZejejejejd5d6dZejd7d8dZ ej!dd9d:d;Z"ejd<d=dZ#ejd>d?Z$ej!d@dAdBdCdDZ%ejdEdFdZ&ejdGdHdZ'ejdIdJgdKdLZ(ejdMdJgdNdLZ)ejdOdPdZ*ejdQdRdZ+ejdSdTdZ,dUS )VMRICoregInputSpecz--mov %szsource file to be registeredTF)r   r   r   r   z--ref %szreference (target) file
subject_id)r   r   r   r   xorz--lta %sz%output registration file (LTA format))r   defaultr   r   z--regdat %sz%output registration file (REG format))r   r   z--params %szoutput parameters filez--sd %szFreeSurfer SUBJECTS_DIR)r   r   r   z--s %sr   reference_filesubjects_dirz_freesurfer subject ID (implies ``reference_mask == aparc+aseg.mgz`` unless otherwise specified))r   rE   r   rh   rY   r      	      z--dof %dz&number of transform degrees of freedomz--ref-mask %sr   z;mask reference volume with given mask, or None if ``False``)r   rE   r   z
--mov-maskz mask source file with given maskz--threads %dznumber of OpenMP threadsz--no-coord-ditherzturn off coordinate ditheringz--no-intensity-ditherzturn off intensity ditheringz--sep %s...z.set spatial scales, in voxels (default [2, 4]))r   Zminlenmaxlenr   z--trans %g %g %gz,initial translation in mm (implies no_cras0)z--rot %g %g %gzinitial rotation in degreesz--scale %g %g %gzinitial scalez--shear %g %g %gzinitial shear (Hxy, Hxz, Hyz)z
--no-cras0zPdo not set translation parameters to align centers of source and reference filesz--nitersmax %dzmaximum iterations (default: 4))lowr   r   z	--ftol %ez'floating-point tolerance (default=1e-7)z--linmintol %e)r   g        g      Y@z--sat %gz$saturation threshold (default=9.999))rp   highr   r   z
--conf-refz#conform reference without rescalingz--no-bfzdo not brute force searchz--bf-lim %gno_brute_forcez'constrain brute force search to +/- lim)r   rh   r   z--bf-nsamp %dz'number of samples in brute force searchz--no-smoothz9do not apply smoothing to either reference or source filez
--ref-fwhmz!apply smoothing to reference filez	--mov-oobz/count source voxels that are out-of-bounds as 0N)-r   r   r   r   Zsource_filerj   r   ZEitherout_lta_fileout_reg_fileout_params_filer   rk   Strrg   EnumZdofreference_maskZsource_maskrT   Znum_threadsrR   Zno_coord_ditheringZno_intensity_ditheringListsepTupleZFloatZinitial_translationZinitial_rotationZinitial_scaleZinitial_shearZno_cras0ZRangeZ	max_itersZftolZ	linmintolZsaturation_thresholdZconform_referencerr   Zbrute_force_limitZbrute_force_samplesZ	no_smoothZref_fwhmZ
source_oobr!   r!   r!   r"   rf     s   






rf   c               @   s0   e Zd ZedddZedddZedddZdS )MRICoregOutputSpecTzoutput registration file)r   r   z"output LTA-style registration filezoutput parameters fileN)r   r   r   r   rt   rs   ru   r!   r!   r!   r"   r|     s   r|   c                   s4   e Zd ZdZdZeZeZ fddZ	dd Z
  ZS )MRICoregaF  This program registers one volume to another

    mri_coreg is a C reimplementation of spm_coreg in FreeSurfer

    Examples
    ========
    >>> from nipype.interfaces.freesurfer import MRICoreg
    >>> coreg = MRICoreg()
    >>> coreg.inputs.source_file = 'moving1.nii'
    >>> coreg.inputs.reference_file = 'fixed1.nii'
    >>> coreg.inputs.subjects_dir = '.'
    >>> coreg.cmdline # doctest: +ELLIPSIS
    'mri_coreg --lta .../registration.lta --ref fixed1.nii --mov moving1.nii --sd .'

    If passing a subject ID, the reference mask may be disabled:

    >>> coreg = MRICoreg()
    >>> coreg.inputs.source_file = 'moving1.nii'
    >>> coreg.inputs.subjects_dir = '.'
    >>> coreg.inputs.subject_id = 'fsaverage'
    >>> coreg.inputs.reference_mask = False
    >>> coreg.cmdline # doctest: +ELLIPSIS
    'mri_coreg --s fsaverage --no-ref-mask --lta .../registration.lta --mov moving1.nii --sd .'

    Spatial scales may be specified as a list of one or two separations:

    >>> coreg.inputs.sep = [4]
    >>> coreg.cmdline # doctest: +ELLIPSIS
    'mri_coreg --s fsaverage --no-ref-mask --lta .../registration.lta --sep 4 --mov moving1.nii --sd .'

    >>> coreg.inputs.sep = [4, 5]
    >>> coreg.cmdline # doctest: +ELLIPSIS
    'mri_coreg --s fsaverage --no-ref-mask --lta .../registration.lta --sep 4 --sep 5 --mov moving1.nii --sd .'
    Z	mri_coregc                sF   |dkr|dkr| j  | }n|dkr2|dkr2dS tt| j|||S )	Nrt   rs   ru   Trx   Fz--no-ref-mask)rt   rs   ru   )r;   r&   r}   r1   )r*   r2   r3   r4   )r+   r!   r"   r1   I  s
    zMRICoreg._format_argc             C   s   | j  j }| jj}t|r8|dkr(d}tjj||d< | jj}t|rd|dkrTd}tjj||d< | jj	}t|r|dkrd}tjj||d< |S )NTzregistration.ltars   zregistration.datrt   zregistration.parru   )
rB   rH   r(   rs   r   r.   r/   r0   rt   ru   )r*   r=   rs   rt   ru   r!   r!   r"   r;   P  s"    zMRICoreg._list_outputs)r   r   r   r?   r@   rf   rA   r|   rB   r1   r;   rC   r!   r!   )r+   r"   r}   !  s   "r}   ))r?   r.   os.pathr   r   Zutils.filemanipr   r   baser   r   r	   r
   r   r   r   r   r   r   r   Z__docformat__	getLoggerZifloggerr   r#   r%   rD   rF   rG   rI   rU   rV   rW   r\   r]   r`   rb   rc   rf   r|   r}   r!   r!   r!   r"   <module>   s6    
E+,($ 