3
d&                 @   s   d Z ddlZddl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 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dS )zLProvides interfaces to various longitudinal commands provided by freesurfer
    N   )logging   )TraitedSpecFiletraitsInputMultiPathOutputMultiPath	isdefined   )	FSCommandFSTraitedSpecFSCommandOpenMPFSTraitedSpecOpenMPZrestructuredtextznipype.interfacec               @   s  e Zd ZeeddddddZedddddd	Zejd
dgdddZ	ej
ddgdddZejeeddejdddZejddddZejeeddejdddZejdddZejdddddZejd d!dZejdd"d#dZejdd$d%dZeeddd&d'dZeeddd(d)dZd*S )+RobustTemplateInputSpecT)existsz--mov %szBinput movable volumes to be aligned to common mean/median template)	mandatoryargstrdesczmri_robust_template_out.mgzz--template %sz0output template volume (final mean/median image))r   Z
usedefaultr   r   z--satitoutlier_sensitivityzGauto-detect good sensitivity (recommended for head or full brain scans))r   xorr   r   z
--sat %.4fauto_detect_sensitivityz\set outlier sensitivity manually (e.g. "--sat 4.685" ). Higher values mean less sensitivity.Fz--lta %sz*output xforms to template (for each input))r   r   z--iscalez*allow also intensity scaling (default off))default_valuer   r   z--iscaleout %sz/final intensity scales (will activate --iscale)z--subsample %dz3subsample if dim > # on all axes (default no subs.)medianmeanz--average %dz3construct template from: 0 Mean, 1 Median (default)z--inittp %dz5use TP# for spacial init (default random), 0: no initz--fixtpz4map everthing to init TP# (init TP is not resampled)z--noitz*do not iterate, just create first templatez--ixforms %sz&use initial transforms (lta) on sourcez--iscalein %szuse initial intensity scalesN)__name__
__module____qualname__r   r   in_filesout_filer   ZBoolr   ZFloatr   ZEithertransform_outputsZintensity_scalingscaled_intensity_outputsZIntZsubsample_thresholdEnumaverage_metricZinitial_timepointZfixed_timepointZno_iterationZinitial_transformsZin_intensity_scales r$   r$   K/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/freesurfer/longitudinal.pyr      sx   r   c               @   s<   e Zd ZedddZeeddddZeeddddZdS )	RobustTemplateOutputSpecTz0output template volume (final mean/median image))r   r   )r   z*output xform files from moving to template)r   zoutput final intensity scalesN)r   r   r   r   r   r	   r    r!   r$   r$   r$   r%   r&   b   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 )RobustTemplatea  construct an unbiased robust template for longitudinal volumes

    Examples
    --------
    >>> from nipype.interfaces.freesurfer import RobustTemplate
    >>> template = RobustTemplate()
    >>> template.inputs.in_files = ['structural.nii', 'functional.nii']
    >>> template.inputs.auto_detect_sensitivity = True
    >>> template.inputs.average_metric = 'mean'
    >>> template.inputs.initial_timepoint = 1
    >>> template.inputs.fixed_timepoint = True
    >>> template.inputs.no_iteration = True
    >>> template.inputs.subsample_threshold = 200
    >>> template.cmdline  #doctest:
    'mri_robust_template --satit --average 0 --fixtp --mov structural.nii functional.nii --inittp 1 --noit --template mri_robust_template_out.mgz --subsample 200'
    >>> template.inputs.out_file = 'T1.nii'
    >>> template.cmdline  #doctest:
    'mri_robust_template --satit --average 0 --fixtp --mov structural.nii functional.nii --inittp 1 --noit --template T1.nii --subsample 200'

    >>> template.inputs.transform_outputs = ['structural.lta',
    ...                                      'functional.lta']
    >>> template.inputs.scaled_intensity_outputs = ['structural-iscale.txt',
    ...                                             'functional-iscale.txt']
    >>> template.cmdline    #doctest: +ELLIPSIS
    'mri_robust_template --satit --average 0 --fixtp --mov structural.nii functional.nii --inittp 1 --noit --template T1.nii --iscaleout .../structural-iscale.txt .../functional-iscale.txt --subsample 200 --lta .../structural.lta .../functional.lta'

    >>> template.inputs.transform_outputs = True
    >>> template.inputs.scaled_intensity_outputs = True
    >>> template.cmdline    #doctest: +ELLIPSIS
    'mri_robust_template --satit --average 0 --fixtp --mov structural.nii functional.nii --inittp 1 --noit --template T1.nii --iscaleout .../is1.txt .../is2.txt --subsample 200 --lta .../tp1.lta .../tp2.lta'

    >>> template.run()  #doctest: +SKIP

    References
    ----------
    [https://surfer.nmr.mgh.harvard.edu/fswiki/mri_robust_template]

    Zmri_robust_templatec                sD   |dkr|j ddd|  S |dkr0| j | }tt| j|||S )Nr#   r   r   )r   r   r    r!   )r    r!   )r   _list_outputssuperr'   _format_arg)selfnamespecvalue)	__class__r$   r%   r*      s
    zRobustTemplate._format_argc                s   | j  j }tjj| jj|d< t| jj}|dkr8dnd t	| jj
r| jj
}|dkrn fddt|D }dd |D |d	< t	| jjr| jj}|dkr fd
dt|D }dd |D |d< |S )Nr   	   z{}{:02d}.{}z	{}{:d}.{}Tc                s   g | ]} j d |d dqS )tpr   Zlta)format).0i)fmtr$   r%   
<listcomp>   s    z0RobustTemplate._list_outputs.<locals>.<listcomp>c             S   s   g | ]}t jj|qS r$   )ospathabspath)r3   xr$   r$   r%   r6      s    r    c                s   g | ]} j d |d dqS )isr   txt)r2   )r3   r4   )r5   r$   r%   r6      s    c             S   s   g | ]}t jj|qS r$   )r7   r8   r9   )r3   r:   r$   r$   r%   r6      s    r!   )output_specgetr7   r8   r9   inputsr   lenr   r
   r    ranger!   )r+   outputsZn_filesfnamesr$   )r5   r%   r(      s    zRobustTemplate._list_outputs)r   r   r   __doc___cmdr   
input_specr&   r=   r*   r(   __classcell__r$   r$   )r/   r%   r'   n   s   &r'   c               @   s   e Zd ZejddddZeej dddddZed	ddddZ	e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ZdS )FuseSegmentationsInputSpecz%sr   zsubject_id being processed)r   positionr   Tr   z)subject_ids or timepoints to be processed)r   r   rI   r   Fr   zoutput fused segmentation file)r   r   rI   r   )r   z-a %szmname of aseg file to use (default: aseg.mgz)         must include the aseg files for all the given timepoints)r   r   r   z-c %szname of aseg file w/o CC labels (default: aseg.auto_noCCseg.mgz)         must include the corresponding file for all the given timepointsz-n %sz-n <filename>  - name of norm file to use (default: norm.mgs)         must include the corresponding norm file for all given timepoints         as well as for the current subjectN)r   r   r   r   StringZ
subject_idr   Z
timepointsr   r   in_segmentationsin_segmentations_noCCin_normsr$   r$   r$   r%   rH      s2   rH   c               @   s   e Zd ZedddZdS )FuseSegmentationsOutputSpecFzoutput fused segmentation file)r   r   N)r   r   r   r   r   r$   r$   r$   r%   rQ      s   rQ   c                   s4   e Zd ZdZdZeZeZ fddZ	dd Z
  ZS )FuseSegmentationsa  fuse segmentations together from multiple timepoints

    Examples
    --------
    >>> from nipype.interfaces.freesurfer import FuseSegmentations
    >>> fuse = FuseSegmentations()
    >>> fuse.inputs.subject_id = 'tp.long.A.template'
    >>> fuse.inputs.timepoints = ['tp1', 'tp2']
    >>> fuse.inputs.out_file = 'aseg.fused.mgz'
    >>> fuse.inputs.in_segmentations = ['aseg.mgz', 'aseg.mgz']
    >>> fuse.inputs.in_segmentations_noCC = ['aseg.mgz', 'aseg.mgz']
    >>> fuse.inputs.in_norms = ['norm.mgz', 'norm.mgz', 'norm.mgz']
    >>> fuse.cmdline
    'mri_fuse_segmentations -n norm.mgz -a aseg.mgz -c aseg.mgz tp.long.A.template tp1 tp2'
    Zmri_fuse_segmentationsc                s2   |dkr|j tjj|d  S tt| j|||S )NrN   rO   rP   r   )rN   rO   rP   )r   r7   r8   basenamer)   rR   r*   )r+   r,   r-   r.   )r/   r$   r%   r*      s    zFuseSegmentations._format_argc             C   s$   | j  j }tjj| jj|d< |S )Nr   )r=   r>   r7   r8   r9   r?   r   )r+   rB   r$   r$   r%   r(      s    zFuseSegmentations._list_outputs)r   r   r   rD   rE   rH   rF   rQ   r=   r*   r(   rG   r$   r$   )r/   r%   rR      s   rR   )rD   r7    r   baser   r   r   r   r	   r
   r   r   r   r   Z__docformat__	getLoggerZifloggerr   r&   r'   rH   rQ   rR   r$   r$   r$   r%   <module>   s    
QF'