niftynet.io.misc_io module

infer_ndims_from_file(file_path, loader=None)[source]
dtype_casting(original_dtype, interp_order, as_tf=False)[source]

Making image dtype based on user specified interp order and best compatibility with Tensorflow.

(if interp_order > 1, all values are promoted to float32,

this avoids errors when the input images have different dtypes)

The image preprocessing steps such as normalising intensities to [-1, 1] will cast input into floats. We therefore cast almost everything to float32 in the reader. Potentially more complex casting rules are needed here.

Parameters:
  • original_dtype – an input datatype
  • interp_order – an integer of interpolation order
  • as_tf – boolean
Returns:

normalised numpy dtype if not as_tf else tensorflow dtypes

create_affine_pixdim(affine, pixdim)[source]

Given an existing affine transformation and the pixel dimension to apply, create a new affine matrix that satisfies the new pixel dimension.

Parameters:
  • affine – original affine matrix
  • pixdim – pixel dimensions to apply
Returns:

correct_image_if_necessary(img)[source]
rectify_header_sform_qform(img_nii)[source]

Look at the sform and qform of the nifti object and correct it if any incompatibilities with pixel dimensions

Parameters:img_nii
Returns:
do_reorientation(data_array, init_axcodes, final_axcodes)[source]

Performs the reorientation (changing order of axes)

Parameters:
  • data_array – 5D Array to reorient
  • init_axcodes – Initial orientation
  • final_axcodes – Target orientation
Return data_reoriented:
 

New data array in its reoriented form

do_resampling(data_array, pixdim_init, pixdim_fin, interp_order)[source]

Performs the resampling

Parameters:
  • data_array – 5D Data array to resample
  • pixdim_init – Initial pixel dimension
  • pixdim_fin – Targeted pixel dimension
  • interp_order – Interpolation order applied
Return data_resampled:
 

Array containing the resampled data

save_data_array(filefolder, filename, array_to_save, image_object=None, interp_order=3, reshape=True)[source]

write image data array to hard drive using image_object properties such as affine, pixdim and axcodes.

expand_to_5d(img_data)[source]

Expands an array up to 5d if it is not the case yet; The first three spatial dims are rearranged so that 1-d is always [X, 1, 1] 2-d is always [X, y, 1] :param img_data: :return:

save_volume_5d(img_data, filename, save_path, affine=array([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]]))[source]

Save the img_data to nifti image

Parameters:
  • img_data – 5d img to save
  • filename – filename under which to save the img_data
  • save_path
  • affine – an affine matrix.
Returns:

split_filename(file_name)[source]
squeeze_spatial_temporal_dim(tf_tensor)[source]

Given a tensorflow tensor, ndims==6 means:

[batch, x, y, z, time, modality]

this function removes x, y, z, and time dims if the length along the dims is one.

Returns:squeezed tensor
touch_folder(model_dir)[source]

This function returns the absolute path of model_dir if exists otherwise try to create the folder and returns the absolute path.

resolve_module_dir(module_dir_str, create_new=False)[source]
to_absolute_path(input_path, model_root)[source]
resolve_file_name(file_name, paths)[source]

check if file_name exists, if not, go though the list of [path + file_name for path in paths]. raises IOError if all options don’t exist

Parameters:
  • file_name
  • paths
Returns:

resolve_checkpoint(checkpoint_name)[source]
get_latest_subfolder(parent_folder, create_new=False)[source]
image3(name, tensor, max_outputs=3, collections=('summaries', ), animation_axes=(1, ), image_axes=(2, 3), other_indices=None)[source]

Summary for higher dimensional images

Parameters:
  • name – string name for the summary
  • tensor – tensor to summarize. Should be in the range 0..255. By default, assumes tensor is NDHWC, and animates (through D) HxW slices of the 1st channel.
  • collections – list of strings collections to add the summary to
  • animation_axes=[1],image_axes=[2,3]
image3_sagittal(name, tensor, max_outputs=3, collections=('summaries', ))[source]
image3_coronal(name, tensor, max_outputs=3, collections=('summaries', ))[source]
image3_axial(name, tensor, max_outputs=3, collections=('summaries', ))[source]
set_logger(file_name=None)[source]