niftynet.io.misc_io module¶
Utilities functions for file and path management
-
infer_ndims_from_file
(file_path, loader=None)[source]¶ Get spatial rank of the image file.
Parameters: - file_path –
- loader –
Returns:
-
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]¶ Check image object header’s format, update the object if necessary
Parameters: img – Returns:
-
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:
-
compute_orientation
(init_axcodes, final_axcodes)[source]¶ A thin wrapper around
nib.orientations.ornt_transform
Parameters: - init_axcodes – Initial orientation codes
- final_axcodes – Target orientation codes
Returns: orientations array, start_ornt, end_ornt
-
do_resampling_idx
(idx_array, init_pixdim, fin_pixdim)[source]¶ Performs the transformation of indices (for csv sampler) when resampling is required (change of resolution enforced :param idx_array: array of indices to modify :param init_pixdim: initial pixdim :param fin_pixdim: target pixdim :return: new_idx transformed array of indices according to resampling
-
do_reorientation_idx
(idx_array, init_axcodes, final_axcodes, init_spatial_size)[source]¶ Perform the indices change based on the the orientation transformation :param idx_array: array of indices to transform when reorienting :param init_axcodes: initial orientation codes :param final_axcodes: target orientation codes :param init_spatial_size: initial image spatial size :return: new_idx the array of transformed indices and orientation transform
-
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 Perform the resampling of the data array given the initial and final pixel dimensions and the interpolation order this function assumes the same interp_order for multi-modal images
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_csv_array
(filefolder, filename, array_to_save)[source]¶ Save a np array as a csv :param filefolder: Path to the folder where to save :param filename: Name of the file to save :param array_to_save: Array to save :return:
-
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.
Parameters: - filefolder –
- filename –
- array_to_save –
- image_object –
- interp_order –
- reshape –
Returns:
-
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, if the final dimensions of the 5D array are 1’s, save the lower dimensional image to disk by squeezing the trailing single dimensional spaces away.
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]¶ split file_name into folder path name, basename, and extension name.
Parameters: file_name – Returns:
-
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]¶ Interpret module_dir_str as an absolute folder path. create the folder if create_new
Parameters: - module_dir_str –
- create_new –
Returns:
-
to_absolute_path
(input_path, model_root)[source]¶ Convert input_path into a relative path to model_root (model_root/input_path) if input_path is not an absolute one.
Parameters: - input_path –
- model_root –
Returns:
-
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]¶ Find the abosolute path of checkpoint_name
For now only supports checkpoint_name where checkpoint_name.index is in the file system eventually will support checkpoint names that can be referenced in a path file.
Parameters: checkpoint_name – Returns:
-
get_latest_subfolder
(parent_folder, create_new=False)[source]¶ Automatically determine the latest folder n if there are n folders in parent_folder, and create the (n+1)th folder if required. This is used in accessing/creating log folders of multiple runs.
Parameters: - parent_folder –
- create_new –
Returns:
-
image3
(name, tensor, max_out=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]¶ Create 2D image summary in the sagittal view.
Parameters: - name –
- tensor –
- max_outputs –
- collections –
Returns:
-
image3_coronal
(name, tensor, max_outputs=3, collections=('summaries', ))[source]¶ Create 2D image summary in the coronal view.
Parameters: - name –
- tensor –
- max_outputs –
- collections –
Returns:
-
image3_axial
(name, tensor, max_outputs=3, collections=('summaries', ))[source]¶ Create 2D image summary in the axial view.
Parameters: - name –
- tensor –
- max_outputs –
- collections –
Returns: