pymic.io package
Submodules
pymic.io.h5_dataset module
- class pymic.io.h5_dataset.H5DataSet(root_dir, sample_list_name, transform=None)
Bases:
Dataset
Dataset for loading images stored in h5 format. It generates 4D tensors with dimention order [C, D, H, W] for 3D images, and 3D tensors with dimention order [C, H, W] for 2D images
- Args:
root_dir (str): thr root dir of images.
sample_list_name (str): a file name for sample list.
tranform (list): A list of transform objects applied on a sample.
- class pymic.io.h5_dataset.TwoStreamBatchSampler(primary_indices, secondary_indices, batch_size, secondary_batch_size)
Bases:
Sampler
Iterate two sets of indices
An ‘epoch’ is one iteration through the primary indices. During the epoch, the secondary indices are iterated through as many times as needed.
- pymic.io.h5_dataset.grouper(iterable, n)
Collect data into fixed-length chunks or blocks
- pymic.io.h5_dataset.iterate_eternally(indices)
- pymic.io.h5_dataset.iterate_once(iterable)
pymic.io.image_read_write module
- pymic.io.image_read_write.load_image_as_nd_array(image_name)
Load an image and return a 4D array with shape [C, D, H, W], or 3D array with shape [C, H, W].
- Parameters:
filename – (str) The input file name
- Returns:
A dictionay storing data array, origin, spacing and direction.
- pymic.io.image_read_write.load_nifty_volume_as_4d_array(filename)
Read a nifty image and return a dictionay storing data array, origin, spacing and direction.
output[‘data_array’] 4D array with shape [C, D, H, W];
output[‘spacing’] A list of spacing in z, y, x axis;
output[‘direction’] A 3x3 matrix for direction.
- Parameters:
filename – (str) The input file name
- Returns:
A dictionay storing data array, origin, spacing and direction.
- pymic.io.image_read_write.load_rgb_image_as_3d_array(filename)
Read an RGB image and return a dictionay storing data array, origin, spacing and direction.
output[‘data_array’] 3D array with shape [D, H, W];
output[‘spacing’] a list of spacing in z, y, x axis;
output[‘direction’] a 3x3 matrix for direction.
- Parameters:
filename – (str) The input file name
- Returns:
A dictionay storing data array, origin, spacing and direction.
- pymic.io.image_read_write.rotate_nifty_volume_to_LPS(filename_or_image_dict, origin=None, direction=None)
Rotate the axis of a 3D volume to LPS
- Parameters:
filename_or_image_dict – (str) Filename of the nifty file (str) or image dictionary returned by load_nifty_volume_as_4d_array. If supplied with the former, the flipped image data will be saved to override the original file. If supplied with the later, only flipped image data will be returned.
origin – (list/tuple) The origin of the image.
direction – (list or tuple) The direction of the image.
- Returns:
A dictionary for image data and meta info, with
data_array
,origin
,direction
andspacing
.
- pymic.io.image_read_write.save_array_as_nifty_volume(data, image_name, reference_name=None)
Save a numpy array as nifty image
- Parameters:
data – (numpy.ndarray) A numpy array with shape [Depth, Height, Width].
image_name – (str) The ouput file name.
reference_name – (str) File name of the reference image of which meta information is used.
- pymic.io.image_read_write.save_array_as_rgb_image(data, image_name)
Save a numpy array as rgb image.
- Parameters:
data – (numpy.ndarray) A numpy array with shape [3, H, W] or [H, W, 3] or [H, W].
image_name – (str) The output file name.
- pymic.io.image_read_write.save_nd_array_as_image(data, image_name, reference_name=None)
Save a 3D or 2D numpy array as medical image or RGB image
- Parameters:
data – (numpy.ndarray) A numpy array with shape [3, H, W] or [H, W, 3] or [H, W].
reference_name – (str) File name of the reference image of which meta information is used.
pymic.io.nifty_dataset module
- class pymic.io.nifty_dataset.ClassificationDataset(root_dir, csv_file, modal_num=1, class_num=2, with_label=False, transform=None)
Bases:
NiftyDataset
Dataset for loading images for classification. It generates 4D tensors with dimention order [C, D, H, W] for 3D images, and 3D tensors with dimention order [C, H, W] for 2D images.
- Parameters:
root_dir – (str) Directory with all the images.
csv_file – (str) Path to the csv file with image names.
modal_num – (int) Number of modalities.
class_num – (int) Class number of the classificaiton task.
with_label – (bool) Load the data with segmentation ground truth or not.
transform – (list) List of transforms to be applied on a sample. The built-in transforms can listed in
pymic.transform.trans_dict
.
- class pymic.io.nifty_dataset.NiftyDataset(root_dir, csv_file, modal_num=1, with_label=False, transform=None)
Bases:
Dataset
Dataset for loading images for segmentation. It generates 4D tensors with dimention order [C, D, H, W] for 3D images, and 3D tensors with dimention order [C, H, W] for 2D images.
- Parameters:
root_dir – (str) Directory with all the images.
csv_file – (str) Path to the csv file with image names.
modal_num – (int) Number of modalities.
with_label – (bool) Load the data with segmentation ground truth or not.
transform – (list) List of transforms to be applied on a sample. The built-in transforms can listed in
pymic.transform.trans_dict
.