Data input and output
Dealing with volumetric data can be done by qim3d
for the most common image formats available.
Currently, it is possible to directly load tiff
, h5
, nii
,txm
, vol
and common PIL
formats using one single function.
qim3d.io
qim3d.io.Downloader
Class for downloading large data files available on the QIM data repository.
Attributes:
Name | Type | Description |
---|---|---|
folder_name |
str
|
Folder class with the name of the folder in https://data.qim.dk/ |
Syntax for downloading and loading a file is qim3d.io.Downloader().{folder_name}.{file_name}(load_file=True)
Overview of available data
Below is a table of the available folders and files on the QIM data repository.
Folder name | File name | File size |
---|---|---|
Coal |
CoalBrikett CoalBrikett_Zoom CoalBrikettZoom_DOWNSAMPLED |
2.23 GB 3.72 GB 238 MB |
Corals |
Coral_1 Coral_2 Coral2_DOWNSAMPLED MexCoral |
2.26 GB 2.38 GB 162 MB 2.23 GB |
Cowry_Shell |
Cowry_Shell Cowry_DOWNSAMPLED |
1.82 GB 116 MB |
Crab |
HerrmitCrab OkinawaCrab |
2.38 GB 1.86 GB |
Deer_Mandible |
Animal_Mandible DeerMandible_DOWNSAMPLED |
2.79 GB 638 MB |
Foam |
Foam Foam_DOWNSAMPLED Foam_2 Foam_2_zoom |
3.72 GB 238 MB 3.72 GB 3.72 GB |
Hourglass |
Hourglass Hourglass_4X_80kV_Air_9s_1_97um Hourglass_longexp_rerun |
3.72 GB 1.83 GB 3.72 GB |
Kiwi |
Kiwi |
2.86 GB |
Loofah |
Loofah Loofah_DOWNSAMPLED |
2.23 GB 143 MB |
Marine_Gastropods |
MarineGatropod_1 MarineGastropod1_DOWNSAMPLED MarineGatropod_2 MarineGastropod2_DOWNSAMPLED |
2.23 GB 143 MB 2.60 GB 166 MB |
Mussel |
ClosedMussel1 ClosedMussel1_DOWNSAMPLED |
2.23 GB 143 MB |
Oak_Branch |
Oak_branch OakBranch_DOWNSAMPLED |
2.38 GB 152 MB |
Okinawa_Forams |
Okinawa_Foram_1 Okinawa_Foram_2 |
1.84 GB 1.84 GB |
Physalis |
Physalis Physalis_DOWNSAMPLED |
3.72 GB 238 MB |
Raspberry |
Raspberry2 Raspberry2_DOWNSAMPLED |
2.97 GB 190 MB |
Rope |
FibreRope1 FibreRope1_DOWNSAMPLED |
1.82 GB 686 MB |
Sea_Urchin |
SeaUrchin Cordatum_Shell Cordatum_Spine |
2.60 GB 1.85 GB 183 MB |
Snail |
Escargot |
2.60 GB |
Sponge |
Sponge |
1.11 GB |
Example
Source code in qim3d/io/downloader.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
qim3d.io.load
load(path, virtual_stack=False, dataset_name=None, return_metadata=False, contains=None, progress_bar=True, force_load=False, dim_order=(2, 1, 0), **kwargs)
Load data from the specified file or directory.
Supported formats:
Tiff
(including file stacks)HDF5
TXRM
/TXM
/XRM
NIfTI
PIL
(including file stacks)VOL
/VGI
DICOM
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str or PathLike
|
The path to the file or directory. |
required |
virtual_stack |
bool
|
Specifies whether to use virtual stack when loading files. Default is False. |
False
|
dataset_name |
str
|
Specifies the name of the dataset to be loaded in case multiple dataset exist within the same file. Default is None (only for HDF5 files) |
None
|
return_metadata |
bool
|
Specifies whether to return metadata or not. Default is False (only for HDF5 and TXRM/TXM/XRM files) |
False
|
contains |
str
|
Specifies a part of the name that is common for the TIFF file stack to be loaded (only for TIFF stacks). Default is None. |
None
|
progress_bar |
bool
|
Displays tqdm progress bar. Useful for large files. So far works only for linux. Default is False. |
True
|
force_load |
bool
|
If the file size exceeds available memory, a MemoryError is raised. If force_load is True, the error is changed to warning and the loader tries to load it anyway. Default is False. |
False
|
dim_order |
tuple
|
The order of the dimensions in the volume for .vol files. Default is (2,1,0) which corresponds to (z,y,x) |
(2, 1, 0)
|
**kwargs |
Additional keyword arguments to be passed |
{}
|
Returns:
Name | Type | Description |
---|---|---|
vol |
(ndarray, memmap, Dataset, ArrayProxy or tuple)
|
The loaded volume If |
Raises:
Type | Description |
---|---|
MemoryError
|
if the given file size exceeds available memory |
Source code in qim3d/io/loading.py
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 |
|
qim3d.io.load_mesh
Load a mesh from an .obj file using trimesh.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
The path to the .obj file. |
required |
Returns:
Name | Type | Description |
---|---|---|
mesh |
A trimesh object containing the mesh data (vertices and faces). |
Source code in qim3d/io/loading.py
qim3d.io.save
save(path, data, replace=False, compression=False, basename=None, sliced_dim=0, chunk_shape='auto', **kwargs)
Save data to a specified file path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The path to save file to. File format is chosen based on the extension. Supported extensions are: '.tif', '.tiff', '.nii', '.nii.gz', '.h5', '.vol', '.vgi', '.dcm', '.DCM', '.zarr', '.jpeg', '.jpg', '.png' |
required |
data |
ndarray
|
The data to be saved |
required |
replace |
bool
|
Specifies if an existing file with identical path should be replaced. Default is False. |
False
|
compression |
bool
|
Specifies if the file should be saved with Deflate compression (lossless). Default is False. |
False
|
basename |
str
|
Specifies the basename for a TIFF stack saved as several files (only relevant for TIFF stacks). Default is None |
None
|
sliced_dim |
int
|
Specifies the dimension that is sliced in case a TIFF stack is saved as several files (only relevant for TIFF stacks). Default is 0, i.e., the first dimension. |
0
|
**kwargs |
Additional keyword arguments to be passed to the DataSaver constructor |
{}
|
Raises:
Type | Description |
---|---|
ValueError
|
If the provided path is an existing directory and self.basename is not provided OR If the file format is not supported OR If the provided path does not exist and self.basename is not provided OR If a file extension is not provided OR if a file with the specified path already exists and replace=False. |
Example
import qim3d
# Generate synthetic blob
synthetic_blob = qim3d.generate.blob(noise_scale = 0.015)
qim3d.io.save("blob.tif", synthetic_blob, replace=True)
Volumes can also be saved with one file per slice:
Source code in qim3d/io/saving.py
qim3d.io.save_mesh
Save a trimesh object to an .obj file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
The name of the file to save the mesh. |
required | |
mesh |
A trimesh.Trimesh object representing the mesh. |
required |
Example
Source code in qim3d/io/saving.py
qim3d.io.export_ome_zarr
export_ome_zarr(path, data, chunk_size=256, downsample_rate=2, order=1, replace=False, method='scaleZYX', progress_bar=True, progress_bar_repeat_time='auto')
Export 3D image data to OME-Zarr format with pyramidal downsampling.
This function generates a multi-scale OME-Zarr representation of the input data, which is commonly used for large imaging datasets. The downsampled scales are calculated such that the smallest scale fits within the specified chunk_size
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The directory where the OME-Zarr data will be stored. |
required |
data |
ndarray or array
|
The 3D image data to be exported. Supports both NumPy and Dask arrays. |
required |
chunk_size |
int
|
The size of the chunks for storing data. This affects both the original data and the downsampled scales. Defaults to 256. |
256
|
downsample_rate |
int
|
The factor by which to downsample the data for each scale. Must be greater than 1. Defaults to 2. |
2
|
order |
int
|
The interpolation order to use when downsampling. Defaults to 1 (linear). Use 0 for a faster nearest-neighbor interpolation. |
1
|
replace |
bool
|
Whether to replace the existing directory if it already exists. Defaults to False. |
False
|
method |
str
|
The method used for downsampling. If set to "dask", Dask arrays are used for chunking and downsampling. Defaults to "scaleZYX". |
'scaleZYX'
|
progress_bar |
bool
|
Whether to display a progress bar during export. Defaults to True. |
True
|
progress_bar_repeat_time |
str or int
|
The repeat interval (in seconds) for updating the progress bar. Defaults to "auto". |
'auto'
|
Raises:
Type | Description |
---|---|
ValueError
|
If the directory already exists and |
ValueError
|
If |
Example
Returns:
Name | Type | Description |
---|---|---|
None |
This function writes the OME-Zarr data to the specified directory and does not return any value. |
Source code in qim3d/io/ome_zarr.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
qim3d.io.import_ome_zarr
Import image data from an OME-Zarr file.
This function reads OME-Zarr formatted volumetric image data and returns the specified scale. The image data can be lazily loaded (as Dask arrays) or fully computed into memory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The file path to the OME-Zarr data. |
required |
scale |
int or str
|
The scale level to load. If 'highest', loads the finest scale (scale 0). If 'lowest', loads the coarsest scale (last available scale). Defaults to 0. |
0
|
load |
bool
|
Whether to compute the selected scale into memory. If False, returns a lazy Dask array. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
np.ndarray or dask.array.Array: The requested image data, either as a NumPy array if |
|
or a Dask array if |
Raises:
Type | Description |
---|---|
ValueError
|
If the requested |