dmriprep.utils.images module

Utilities to handle images.

dmriprep.utils.images.extract_b0(in_file, b0_ixs, out_path=None)View on GitHub

Extract the b0 volumes from a DWI dataset.

Parameters:
  • in_file (str) – DWI NIfTI file.

  • b0_ixs (list) – List of B0 indices in in_file.

  • out_path (str) – Optionally specify an output path.

Returns:

out_path – 4D NIfTI file consisting of B0’s.

Return type:

str

Examples

>>> os.chdir(tmpdir)
>>> b0_ixs = np.where(np.loadtxt(str(data_dir / 'bval')) <= 50)[0].tolist()[:2]
>>> in_file = str(data_dir / 'dwi.nii.gz')
>>> out_path = extract_b0(in_file, b0_ixs)
>>> assert os.path.isfile(out_path)
dmriprep.utils.images.get_list_data(file_list, dtype=<class 'numpy.float32'>)View on GitHub

Load 3D volumes from a list of file paths into a 4D array.

Parameters:

file_list (str) – A list of file paths to 3D NIFTI images.

Return type:

Nibabel image object

Examples

>>> os.chdir(tmpdir)
>>> in_file = str(dipy_datadir / "HARDI193.nii.gz")
>>> out_files = save_4d_to_3d(in_file)
>>> assert len(out_files) == get_list_data(out_files).shape[-1]
dmriprep.utils.images.match_transforms(dwi_files, transforms, b0_ixs)View on GitHub

Arrange the order of a list of transforms.

This is a helper function for EMC. Sorts the input list of affine transforms to correspond with that of each individual dwi volume file, accounting for the indices of \(b = 0\) volumes.

Parameters:
  • dwi_files (list) – A list of file paths to 3D diffusion-weighted NIFTI volumes.

  • transforms (list) – A list of ndarrays.

  • b0_ixs (list) – List of B0 indices.

Returns:

nearest_affines – A list of affine file paths that correspond to each of the split dwi volumes.

Return type:

list

Examples

>>> os.chdir(tmpdir)
>>> from dmriprep.utils.vectors import DiffusionGradientTable
>>> dwi_file = str(dipy_datadir / "HARDI193.nii.gz")
>>> check = DiffusionGradientTable(
...     dwi_file=dwi_file,
...     bvecs=str(dipy_datadir / "HARDI193.bvec"),
...     bvals=str(dipy_datadir / "HARDI193.bval"))
>>> check.generate_rasb()
>>> # Conform to the orientation of the image:
>>> affines = np.zeros((check.gradients.shape[0], 4, 4))
>>> transforms = []
>>> for ii, aff in enumerate(affines):
...     aff_file = f'aff_{ii}.npy'
...     np.save(aff_file, aff)
...     transforms.append(aff_file)
>>> dwi_files = save_4d_to_3d(dwi_file)
>>> b0_ixs = np.where((check.bvals) <= 50)[0].tolist()[:2]
>>> nearest_affines = match_transforms(dwi_files, transforms, b0_ixs)
>>> assert sum([os.path.isfile(i) for i in nearest_affines]) == len(nearest_affines)
>>> assert len(nearest_affines) == len(dwi_files)
dmriprep.utils.images.rescale_b0(in_file, mask_file, out_path=None)View on GitHub

Rescale the input volumes using the median signal intensity.

Parameters:
  • in_file (str) – A NIfTI file consisting of one or more B0’s.

  • mask_file (str) – A B0 mask NIFTI file.

  • out_path (str) – Optionally specify an output path.

Returns:

out_path – A rescaled B0 NIFTI file.

Return type:

str

Examples

>>> os.chdir(tmpdir)
>>> mask_file = str(data_dir / 'dwi_mask.nii.gz')
>>> in_file = str(data_dir / 'dwi_b0.nii.gz')
>>> out_path, drifts = rescale_b0(in_file, mask_file)
>>> assert os.path.isfile(out_path)
dmriprep.utils.images.save_3d_to_4d(in_files)View on GitHub

Concatenate a list of 3D volumes into a 4D output.

Parameters:

in_files (list) – A list of file paths to 3D NIFTI images.

Returns:

out_file – A file path to a 4d NIFTI image of concatenated 3D volumes.

Return type:

str

Examples

>>> os.chdir(tmpdir)
>>> in_file = str(dipy_datadir / "HARDI193.nii.gz")
>>> threeD_files = save_4d_to_3d(in_file)
>>> out_file = save_3d_to_4d(threeD_files)
>>> assert len(threeD_files) == nb.load(out_file).shape[-1]
dmriprep.utils.images.save_4d_to_3d(in_file)View on GitHub

Split a 4D dataset along the last dimension into multiple 3D volumes.

Parameters:

in_file (str) – DWI NIfTI file.

Returns:

out_files – A list of file paths to 3d NIFTI images.

Return type:

list

Examples

>>> os.chdir(tmpdir)
>>> in_file = str(dipy_datadir / "HARDI193.nii.gz")
>>> out_files = save_4d_to_3d(in_file)
>>> assert len(out_files) == nb.load(in_file).shape[-1]
dmriprep.utils.images.summarize_images(in_file, method=<function median>, dtype=None, out_path=None)View on GitHub

Summarize a 4D dataset across the last dimension using a callable method.

Parameters:
  • in_file (str) – A NIfTI file consisting of one or more 3D images.

  • method (callable) – A numpy function such as np.mean or np.median.

  • dtype (str) – Optioally specify a datatype (e.g. ‘float32’).

  • out_path (str) – Optionally specify an output path for out_path.

Returns:

out_path – A 3D NIFTI image file.

Return type:

str

Examples

>>> os.chdir(tmpdir)
>>> in_file = str(dipy_datadir / "HARDI193.nii.gz")
>>> # Median case
>>> out_path = summarize_images(in_file)
>>> assert os.path.isfile(out_path)
>>> # Mean case
>>> out_path = summarize_images(in_file, method=np.mean)
>>> assert os.path.isfile(out_path)