ScalarImage
ScalarImage (*args, **kwargs)
Image whose pixel values represent scalars.
See :class:~torchio.Image
for more information.
Image Writers
write_image
write_image (data, file_path, dimension_order='TCZYX')
Writes an image to a file.
:param data: Image data (numpy array, tensor, or AICSImage) :param file_path: Path to save the image :param format: Format to save the image in (default is png)
# Example usage:
numpy_array = np.random.rand(3 , 100 , 100 )
write_image(numpy_array, './data_examples/output_from_numpy.tiff' )
tensor = torch.rand(3 , 100 , 100 )
write_image(tensor, './data_examples/output_from_tensor.tiff' )
aics_image = AICSImage('./data_examples/example_tiff.tiff' )
write_image(aics_image, './data_examples/output_from_tiff.png' )
Image successfully saved to ./data_examples/output_from_numpy.tiff
Image successfully saved to ./data_examples/output_from_tensor.tiff
Image successfully saved to ./data_examples/output_from_tiff.png
Image Readers
It uses helper functions such as _load_and_preprocess and _multi_sequence to load and preprocess biological images.
tiff2torch
tiff2torch (file_path:str)
Load tiff into pytorch tensor
string2dict
string2dict (input_string:str)
split_path
split_path (file_path,
exts:(<class'fastcore.foundation.L'>,<class'list'>)=['.ome.ti
ff', '.tiff', '.tif', '.png'])
file_path
The path to the file to split
exts
(<class ‘fastcore.foundation.L’>, <class ‘list’>)
[‘.ome.tiff’, ‘.tiff’, ‘.tif’, ‘.png’]
List of filename extensions
aics_image_reader
aics_image_reader (ind_dict=None)
Initialize self. See help(type(self)) for accurate signature.
ind_dict
NoneType
None
Dictionary indicating the channels to load
file_path = 'data_examples/example_tiff.tiff'
test_img, _ = aics_image_reader({'Z' : 0 })(file_path)
test_img.shape
split_hdf_path
split_hdf_path (file_path,
hdf5_exts:(<class'fastcore.foundation.L'>,<class'list'>)=
['.h5', '.hdf5'])
file_path
The path to the HDF5 file to split
hdf5_exts
(<class ‘fastcore.foundation.L’>, <class ‘list’>)
[‘.h5’, ‘.hdf5’]
List of filename extensions
hdf5_reader
hdf5_reader (dataset=None, patch=0,
hdf5_exts:(<class'fastcore.foundation.L'>,<class'list'>)=['.
h5', '.hdf5'])
Initialize self. See help(type(self)) for accurate signature.
dataset
NoneType
None
The dataset to load
patch
int
0
The patch to load from the dataset
hdf5_exts
(<class ‘fastcore.foundation.L’>, <class ‘list’>)
[‘.h5’, ‘.hdf5’]
List of filename extensions
Images can be loaded by explicitly writing dataset name and path number…
from bioMONAI.visualize import plot_image
file_path = './data_examples/0450_1.hdf5'
dataset_name= 'clean'
patch_num= 10
im , _ = hdf5_reader(dataset= dataset_name, patch= patch_num)(file_path)
plot_image(im[0 ])
… or enconding them in the path, where datasets are subfolders and patches the image files. The latter being compatible with image_reader
syntaxis.
f = file_path + '/' + dataset_name + '/' + ' %d ' % (patch_num)
im , _ = hdf5_reader()(f)
plot_image(im[0 ])
Load and preprocess
org_img, _, _ = _load_and_preprocess(f)
test_eq(org_img.data[0 ].shape, im.shape)
Read multichannel data
t = _multi_sequence([f], only_tensor= True );
test_eq(t[0 ].shape, im.shape)
torch.Size([1, 1, 96, 96])
image_reader
image_reader (file_path:(<class'str'>,<class'pathlib.Path'>,<class'fastco
re.foundation.L'>,<class'list'>), dtype=<class
'torch.Tensor'>, only_tensor:bool=True, **kwargs)
Loads and preprocesses a medical image.
Args: file_path: Path to the image. Can be a string, Path object or a list. dtype: Datatype for the return value. Defaults to torchTensor. reorder: Whether to reorder the data to be closest to canonical (RAS+) orientation. Defaults to False. resample: Whether to resample image to different voxel sizes and image dimensions. Defaults to None. only_tensor: To return only an image tensor. Defaults to True.
Returns: The preprocessed image. Returns only the image tensor if only_tensor is True, otherwise returns original image, preprocessed image, and original size.
file_path
(<class ‘str’>, <class ‘pathlib.Path’>, <class ‘fastcore.foundation.L’>, <class ‘list’>)
Path to the image
dtype
_TensorMeta
Tensor
Datatype for the return value. Defaults to torchTensor
only_tensor
bool
True
To return only an image tensor
kwargs
test_eq(image_reader(f)[0 ].shape, im.shape)