xcp_d.utils.filemanip module

Miscellaneous file manipulation functions.

xcp_d.utils.filemanip.binarize_img(mask_file)[source]

Binarize mask file.

xcp_d.utils.filemanip.check_binary_mask(mask_file)[source]

Check if the mask is binary.

TODO: Fix non-binary mask bug in nibabies 22.1.3

xcp_d.utils.filemanip.copyfile(originalfile, newfile, copy=False, create_new=False, hashmethod=None, use_hardlink=False, copy_related_files=True)[source]

Copy or link originalfile to newfile.

If use_hardlink is True, and the file can be hard-linked, then a link is created, instead of copying the file.

If a hard link is not created and copy is False, then a symbolic link is created.

Parameters:
  • originalfile (str) – full path to original file

  • newfile (str) – full path to new file

  • copy (bool) – specifies whether to copy or symlink files (default=False) but only for POSIX systems

  • use_hardlink (bool) – specifies whether to hard-link files, when able (Default=False), taking precedence over copy

  • copy_related_files (bool) – specifies whether to also operate on related files, as defined in related_filetype_sets

Returns:

newfile – The full path to the new file.

Return type:

str

xcp_d.utils.filemanip.copyfiles(filelist, dest, copy=False, create_new=False)[source]

Copy or symlink files in filelist to dest directory.

Parameters:
  • filelist (list of str) – List of files to copy.

  • dest (str or list of str) – full path to destination. If it is a list of length greater than 1, then it assumes that these are the names of the new files.

  • copy (str) – specifies whether to copy or symlink files (default=False) but only for posix systems

Returns:

newfiles – List of new copied files.

Return type:

list of str

xcp_d.utils.filemanip.ensure_list(filename)[source]

Return a list given either a string or a list.

xcp_d.utils.filemanip.fname_presuffix(fname, prefix='', suffix='', newpath=None, use_ext=True)[source]

Manipulate path and name of input filename.

Parameters:
  • fname (string) – A filename (may or may not include path)

  • prefix (string) – Characters to prepend to the filename

  • suffix (string) – Characters to append to the filename

  • newpath (string) – Path to replace the path of the input fname

  • use_ext (boolean) – If True (default), appends the extension of the original file to the output name.

Returns:

Absolute path of the modified filename

Return type:

str

Examples

>>> from nipype.utils.filemanip import fname_presuffix
>>> fname = 'foo.nii.gz'
>>> fname_presuffix(fname,'pre','post','/tmp')
'/tmp/prefoopost.nii.gz'
>>> from nipype.interfaces.base import Undefined
>>> fname_presuffix(fname, 'pre', 'post', Undefined) ==             fname_presuffix(fname, 'pre', 'post')
True

Return a list of related files, as defined in related_filetype_sets, for a filename.

For example, Nifti-Pair, Analyze (SPM), and AFNI files.

Parameters:
  • filename (str) – File name to find related filetypes of.

  • include_this_file (bool) – If true, output includes the input filename.

Returns:

related_files – List of file related to filename.

Return type:

list of str

xcp_d.utils.filemanip.hash_infile(afile, chunk_len=8192, crypto=<built-in function openssl_md5>, raise_notfound=False)[source]

Compute hash of a file using ‘crypto’ module.

Examples

>>> hash_infile('smri_ants_registration_settings.json')
'f225785dfb0db9032aa5a0e4f2c730ad'
>>> hash_infile('surf01.vtk')
'fdf1cf359b4e346034372cdeb58f9a88'
>>> hash_infile('spminfo')
'0dc55e3888c98a182dab179b976dfffc'
>>> hash_infile('fsl_motion_outliers_fd.txt')
'defd1812c22405b1ee4431aac5bbdd73'
xcp_d.utils.filemanip.hash_timestamp(afile)[source]

Compute md5 hash of the timestamp of a file.

xcp_d.utils.filemanip.on_cifs(fname)[source]

Check whether a file path is on a CIFS filesystem mounted in a POSIX host.

On Windows, Docker mounts host directories into containers through CIFS shares, which has support for Minshall+French symlinks, or text files that the CIFS driver exposes to the OS as symlinks. We have found that under concurrent access to the filesystem, this feature can result in failures to create or read recently-created symlinks, leading to inconsistent behavior and FileNotFoundError.

This check is written to support disabling symlinks on CIFS shares.

Parameters:

fname (str) – The file to be checked.

Returns:

Either returns “cifs” if the file is on a CIFS filesystem or False if not.

Return type:

bool or str

xcp_d.utils.filemanip.relpath(path, start=None)[source]

Return a relative version of a path.

Parameters:
  • path (str) – Path to reformat.

  • start (None or str, optional) – The starting location for the relative path. If None, use the current working directory. Default is None.

Returns:

Relative version of the path.

Return type:

str

xcp_d.utils.filemanip.split_filename(fname)[source]

Split a filename into parts: path, base filename and extension.

Parameters:

fname (str) – file or path name

Returns:

  • pth (str) – base path from fname

  • fname (str) – filename from fname, without extension

  • ext (str) – file extension from fname

Examples

>>> from nipype.utils.filemanip import split_filename
>>> pth, fname, ext = split_filename('/home/data/subject.nii.gz')
>>> pth
'/home/data'
>>> fname
'subject'
>>> ext
'.nii.gz'
xcp_d.utils.filemanip.which(cmd, env=None, pathext=None)[source]

Return the path to an executable which would be run if the given cmd was called.

If no cmd would be called, return None.

Code for Python < 3.3 is based on a code snippet from http://orip.org/2009/08/python-checking-if-executable-exists-in.html