3
d|B                 @   s   d Z ddlZddlZddlmZ ddlZddlZddlmZ ddl	m
Z
 ddlmZmZmZmZmZmZmZmZmZmZ G d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdd ZdS )zKdcm2nii converts images from the proprietary scanner DICOM format to NIfTI.    N)deepcopy)iglob   )split_filename   )
CommandLineCommandLineInputSpecInputMultiPathtraitsTraitedSpecOutputMultiPath	isdefinedFile	DirectoryPackageInfoc               @   s    e Zd ZdZdZedd ZdS )Infoz#Handle dcm2niix version informationdcm2niixc             C   s    t jd| }|r|j d S d S )Nzversion (\S+)r   )researchgroups)Zraw_infom r   ;/tmp/pip-build-7vycvbft/nipype/nipype/interfaces/dcm2nii.pyparse_version   s    zInfo.parse_versionN)__name__
__module____qualname____doc__Zversion_cmdstaticmethodr   r   r   r   r   r      s   r   c               @   sB  e Zd Zeedddd.dddgdZeddd/ddgd	Zej	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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Zej	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Zej	d"d#d$Zej	d%d&gd'd(Zej	dd)dd*dZej	dd+dd,dZd-S )0Dcm2niiInputSpecT)existsz%sr   F
source_dir)argstrpositioncopyfile	mandatoryxorsource_names)r    r"   r#   r%   r&   z-azRemove identifying information)r"   
usedefaultdescz-b %sz$Load settings from specified inifile)r    r"   Zgenfiler)   z-czCollapse input foldersz-dzDate in filenamez-ezEvents (series/acq) in filenamez-fzSource filenamez-gzGzip output (.gz)z-izID  in filenamez-nz+Save as .nii - if no, create .hdr/.img pairz-o %sz5Output dir - if unspecified, source directory is usedz-pzProtocol in filenamez-rz$Reorient image to nearest orthogonal)r"   r)   z-s
nii_outputzSPM2/Analyze not SPM5/NIfTI)r"   r&   r)   z-vz Convert every image in directoryz-xzReorient and crop 3D imagesNr+   )r   r   r   r	   r   r'   r   r!   r
   Bool	anonymizeconfig_filecollapse_foldersdate_in_filenameevents_in_filenamesource_in_filenamegzip_outputid_in_filenamer*   
output_dirprotocol_in_filenamereorientspm_analyzeconvert_all_parsreorient_and_cropr   r   r   r   r   $   s^   
r   c               @   sR   e Z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 )Dcm2niiOutputSpecT)r    N)
r   r   r   r   r   converted_filesreoriented_filesreoriented_and_cropped_filesbvecsbvalsr   r   r   r   r;   f   s
   r;   c                   sP   e Zd ZdZeZeZdZ fddZ	 fddZ
dd Zd	d
 Zdd Z  ZS )Dcm2niia  Uses MRIcron's dcm2nii to convert dicom files

    Examples
    ========

    >>> from nipype.interfaces.dcm2nii import Dcm2nii
    >>> converter = Dcm2nii()
    >>> converter.inputs.source_names = ['functional_1.dcm', 'functional_2.dcm']
    >>> converter.inputs.gzip_output = True
    >>> converter.inputs.output_dir = '.'
    >>> converter.cmdline  # doctest: +ELLIPSIS
    'dcm2nii -a y -c y -b config.ini -v y -d y -e y -g y -i n -n y -o . -p y -x n -f n functional_1.dcm'Zdcm2niic                s`   |dkr6t |}|r$| jd7  _n| jd7  _d}|dkrL|j|d  S tt| j|||S )Nr-   r/   r0   r1   r2   r3   r4   r*   r6   r7   r8   r9   r:   z yz nTr'   r   )r-   r/   r0   r1   r2   r3   r4   r*   r6   r7   r8   r9   r:   )r   r"   superrA   _format_arg)selfoptspecval)	__class__r   r   rC      s,                zDcm2nii._format_argc                sJ   d| _ tt| j|}| j|j\| _| _| _| _	| _
| j rFtjd |S )NFz
config.ini)_config_createdrB   rA   _run_interface_parse_stdoutstdoutoutput_filesr=   r>   r?   r@   osremove)rD   runtimeZnew_runtime)rH   r   r   rJ      s     
zDcm2nii._run_interfacec             C   s  g }g }g }g }g }d}d }x|j dD ]p}	|sd }
|	jdrX|	tdd  }
n|	jdr|	tdd  }t|rtjj|d |d d kr|j  t| jj	r| jj	}n
| j
d}tjjtjj||}
n|	jdr*|rt|\}}}|jtjj||d	  |jtjj||d
  n|	jdr|x||fD ]6}tjjtjj|d dtjj|d f |d< q@W nhtjd|	rtjd|	}|j d }t| jj	r| jj	}n
| j
d}tjj||}tjj|r|}
|
r|
|kr*|j|
 |
}q*|	jdr,|j|	tdd   d}q*nl|	jdrtjj |	tdd  \}}d| }tjjtjj||s~| jjr|jtjj|| d}q*d}q*W |||||fS )NF
zSaving zGZip...r      r5   zNumber of diffusion directions z.bvecz.bvalzRemoved DWI from DTI scanzx%sz.*->(.*)r   zReorienting as TzCropping NIfTI/Analyze image cr+   r+   r+   r+   )split
startswithlenrN   pathbasenamepopr   inputsr5   _gen_filenameabspathjoinr   appenddirnamer   r   r   r    r:   )rD   rL   filesr=   r>   r?   r@   skipZlast_added_filelineout_filefnamer5   basefilenameextlrG   r   r   r   rK      sv    

$

2



zDcm2nii._parse_stdoutc             C   sB   | j  j }| j|d< | j|d< | j|d< | j|d< | j|d< |S )Nr<   r=   r>   r?   r@   )output_specgetrM   r=   r>   r?   r@   )rD   outputsr   r   r   _list_outputs   s    




zDcm2nii._list_outputsc          	   C   sJ   |dkrt j S |dkrFd| _d}t|d}|jd W d Q R X |S d S )Nr5   r.   Tz
config.iniwz[BOOL]
ManualNIfTIConv=0
)rN   getcwdrI   openwrite)rD   namer.   fr   r   r   r\      s    zDcm2nii._gen_filename)r   r   r   r   r   
input_specr;   rj   _cmdrC   rJ   rK   rm   r\   __classcell__r   r   )rH   r   rA   n   s   N	rA   c               @   s`  e Zd Zeedddd?ddddgdZeddd@dd	d
gdZej	dddZ
eddddddZejdddddZejddgddZejd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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/d0d1d2d3dZej	d4d5dZejd6d7dZeej	 d8d9dZejd:d;dZejd<d=dZd>S )ADcm2niixInputSpecT)r    z%sr   FzA set of filenames to be converted. Note that the current version (1.0.20180328) of dcm2niix converts any files in the directory. To only convert specific files they should be in an isolated directoryr!   )r"   r#   r$   r%   r)   r&   z2A directory containing dicom files to be convertedr'   )r    r"   r#   r%   r)   r&   z-f %sa;  Output filename template (%a=antenna (coil) number, %c=comments, %d=description, %e=echo number, %f=folder name, %i=ID of patient, %j=seriesInstanceUID, %k=studyInstanceUID, %m=manufacturer, %n=name of patient, %p=protocol, %s=series number, %t=time, %u=acquisition number, %v=vendor, %x=study ID; %z=sequence name))r"   r)   .z-o %szOutput directory)r(   r    r"   r)   z-bzCreate a BIDS sidecar file)r"   r(   r)   z-babids_formatzAnonymize BIDS)r"   requiresr)   yin3z-z %sz:Gzip compress images - [y=pigz, i=internal, n=no, 3=no,3D]z-mz merge 2D slices from same seriesz-szSingle file modez-vzVerbose outputz-xzCrop 3D T1 acquisitionsz-tz,Text notes including private patient detailsr   rR                  	   z-%dz,Gz compression level (1=fastest, 9=smallest)z-c %sz Comment stored as NIfTI aux_filez-iz'Ignore derived, localizer and 2D imagesz-n %s...zASelectively convert by series number - can be used up to 16 timesz-pz+Philips precise float (not display) scalingz-ezExport as NRRD instead of NIfTINr+   r+   )r   r   r   r	   r   r'   r   r!   r
   StrZout_filenamer5   r,   ry   	anon_bidsEnumcompress
merge_imgssingle_fileverbosecrophas_privatecompressioncommentignore_derivZseries_numbersphilips_floatto_nrrdr   r   r   r   rw     s   



rw   c               @   sD   e Zd ZeeddZeeddZeeddZeeddZdS )Dcm2niixOutputSpecT)r    N)	r   r   r   r   r   r<   r?   r@   bidsr   r   r   r   r   v  s   r   c                   s\   e Zd ZdZeZeZdZe	dd Z
 fddZ fddZd	d
 Zdd Zdd Z  ZS )Dcm2niixa  Uses Chris Rorden's dcm2niix to convert dicom files

    Examples
    ========

    >>> from nipype.interfaces.dcm2nii import Dcm2niix
    >>> converter = Dcm2niix()
    >>> converter.inputs.source_dir = 'dicomdir'
    >>> converter.inputs.compression = 5
    >>> converter.inputs.output_dir = 'ds005'
    >>> converter.cmdline
    'dcm2niix -b y -z y -5 -x n -t n -m n -o ds005 -s n -v n dicomdir'
    >>> converter.run() # doctest: +SKIP

    In the example below, we note that the current version of dcm2niix
    converts any files in the directory containing the files in the list. We
    also do not support nested filenames with this option. **Thus all files
    must have a common root directory.**

    >>> converter = Dcm2niix()
    >>> converter.inputs.source_names = ['functional_1.dcm', 'functional_2.dcm']
    >>> converter.inputs.compression = 5
    >>> converter.inputs.output_dir = 'ds005'
    >>> converter.cmdline
    'dcm2niix -b y -z y -5 -x n -t n -m n -o ds005 -s n -v n .'
    >>> converter.run() # doctest: +SKIP
    r   c             C   s   t j S )N)r   version)rD   r   r   r   r     s    zDcm2niix.versionc          
      s   ddddddddd	d
g
}||krNt |}|r<| jd7  _n| jd7  _d}|dkrp|jtjj|d pld S tt| j|||S )Nry   r   r   r   r   r   r   r   r   r   z yz nTr'   r   rx   )r   r"   rN   rX   r`   rB   r   rC   )rD   rE   rF   rG   Zbools)rH   r   r   rC     s&    zDcm2niix._format_argc                s*   t t| j|dd}| j| j|j |S )Nr   r   )Zcorrect_return_codes)r   r   )rB   r   rJ   _parse_filesrK   rL   )rD   rP   )rH   r   r   rJ     s    

zDcm2niix._run_interfacec             C   sN   g }xD|j dD ]6}|jdrttjd|jd}|jtjj	| qW |S )NrQ   zConvert z\S+/\S+r   )
rU   rV   strr   r   groupr_   rN   rX   r]   )rD   rL   	filenamesrc   re   r   r   r   rK     s    
zDcm2niix._parse_stdoutc       	      C   s   g g g g f\}}}}ddddg}| j jr8|dddg7 }n|dd	g7 }x|D ]}xt||D ]}|jds|jd
s|jds|jdr|j| qZ|jdr|j| qZ|jdr|j| qZ|jds|jdrZ|j| qZW qJW || _|| _|| _|| _d S )Nz.bvalz.bvecz.jsonz.txtz.nrrdz.nhdrz.raw.gzz.niiz.nii.gzz.gz)	r[   r   search_filesendswithr_   rM   r?   r@   r   )	rD   r   outfilesr@   r?   r   outtypesrg   flr   r   r   r     s,    






zDcm2niix._parse_filesc             C   s8   | j  j }| j|d< | j|d< | j|d< | j|d< |S )Nr<   r?   r@   r   )rj   rk   rM   r?   r@   r   )rD   rl   r   r   r   rm     s    



zDcm2niix._list_outputs)r   r   r   r   rw   rt   r   rj   ru   propertyr   rC   rJ   rK   r   rm   rv   r   r   )rH   r   r   }  s   r   c                s   t jj fdd|D S )Nc             3   s    | ]}t tj | V  qd S )N)r   globescape).0Zouttype)prefixr   r   	<genexpr>  s    zsearch_files.<locals>.<genexpr>)itchainfrom_iterable)r   r   r   )r   r   r     s    r   )r   rN   r   copyr   	itertoolsr   r   r   Zutils.filemanipr   rf   r   r   r	   r
   r   r   r   r   r   r   r   r   r;   rA   rw   r   r   r   r   r   r   r   <module>   s"   0B jt