Input/Output
All the input/output operations are handled by the plantseg.io
module. This module provides functions to read and write data in different formats. The supported formats are tiff
, h5
, and zarr
, jpeg
, png
.
Reading
plantseg.io.smart_load(path: Path, key: str | None = None, default=load_tiff) -> np.ndarray
Load a dataset from a file. The loader is chosen based on the file extension. Supported formats are: tiff, h5, zarr, and PIL images. If the format is not supported, a default loader can be provided (default: load_tiff).
Parameters:
-
path
(Path
) –path to the file to load.
-
key
(str
, default:None
) –key of the dataset to load (if h5 or zarr).
-
default
(callable
, default:load_tiff
) –default loader if the type is not understood.
Returns:
-
stack
(ndarray
) –numpy array with the image data.
Examples:
>>> data = smart_load('path/to/file.tif')
>>> data = smart_load('path/to/file.h5', key='raw')
Source code in plantseg/io/io.py
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 |
|
Writing
plantseg.io.create_tiff(path: Path, stack: np.ndarray, voxel_size: VoxelSize, layout: str = 'ZYX') -> None
Create a tiff file from a numpy array
Parameters:
-
path
(Path
) –path to save the tiff file
-
stack
(ndarray
) –numpy array to save as tiff
-
voxel_size
(list or tuple
) –tuple of the voxel size
-
voxel_size_unit
(str
) –units of the voxel size
Source code in plantseg/io/tiff.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
plantseg.io.create_h5(path: Path, stack: np.ndarray, key: str, voxel_size: Optional[VoxelSize] = None, mode: str = 'a') -> None
Create a dataset inside a h5 file from a numpy array.
Parameters:
-
path
(Path
) –path to the h5 file
-
stack
(ndarray
) –numpy array to save as dataset in the h5 file.
-
key
(str
) –key of the dataset in the h5 file.
-
voxel_size
(VoxelSize
, default:None
) –voxel size of the dataset.
-
mode
(str
, default:'a'
) –mode to open the h5 file ['w', 'a'].
Source code in plantseg/io/h5.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
plantseg.io.create_zarr(path: Path, stack: np.ndarray, key: str, voxel_size: VoxelSize, mode: str = 'a') -> None
Create a Zarr array from a NumPy array.
Parameters:
-
path
(Path
) –The path to the Zarr file.
-
stack
(ndarray
) –The NumPy array to save as a dataset.
-
key
(str
) –The internal key of the desired dataset.
-
voxel_size
(VoxelSize
) –The voxel size of the dataset.
-
mode
(str
, default:'a'
) –The mode to open the Zarr file ['w', 'a'].
Source code in plantseg/io/zarr.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
|
Tiff Utilities
plantseg.io.tiff.read_tiff_voxel_size(file_path: Path) -> VoxelSize
Returns the voxels size and the voxel units for imagej and ome style tiff (if absent returns [1, 1, 1], um)
Parameters:
-
file_path
(Path
) –path to the tiff file
Returns:
-
VoxelSize
(VoxelSize
) –voxel size and unit
Source code in plantseg/io/tiff.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
H5 Utilities
plantseg.io.h5.list_h5_keys(path: Path) -> list[str]
List all keys in a h5 file
Parameters:
-
path
(Path
) –path to the h5 file (Path object)
Returns:
Source code in plantseg/io/h5.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
plantseg.io.h5.del_h5_key(path: Path, key: str, mode: str = 'a') -> None
helper function to delete a dataset from a h5file
Parameters:
-
path
(Path
) –path to the h5 file (Path object)
-
key
(str
) –key of the dataset to delete
-
mode
(str
, default:'a'
) –mode to open the h5 file ['r', 'r+']
Source code in plantseg/io/h5.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
plantseg.io.h5.rename_h5_key(path: Path, old_key: str, new_key: str, mode='r+') -> None
Rename the 'old_key' dataset to 'new_key'
Parameters:
-
path
(Path
) –path to the h5 file (Path object)
-
old_key
(str
) –old key name
-
new_key
(str
) –new key name
-
mode
(str
, default:'r+'
) –mode to open the h5 file ['r', 'r+']
Source code in plantseg/io/h5.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
Zarr Utilities
plantseg.io.zarr.list_zarr_keys(path: Path) -> list[str]
List all keys in a Zarr file.
Parameters:
-
path
(Path
) –The path to the Zarr file.
Returns:
Source code in plantseg/io/zarr.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
plantseg.io.zarr.del_zarr_key(path: Path, key: str, mode: str = 'a') -> None
Delete a dataset from a Zarr file.
Parameters:
-
path
(Path
) –The path to the Zarr file.
-
key
(str
) –The internal key of the dataset to be deleted.
-
mode
(str
, default:'a'
) –The mode to open the Zarr file ['w', 'a'].
Source code in plantseg/io/zarr.py
189 190 191 192 193 194 195 196 197 198 199 200 201 |
|
plantseg.io.zarr.rename_zarr_key(path: Path, old_key: str, new_key: str, mode: str = 'r+') -> None
Rename a dataset in a Zarr file.
Parameters:
-
path
(Path
) –The path to the Zarr file.
-
old_key
(str
) –The current key of the dataset.
-
new_key
(str
) –The new key for the dataset.
-
mode
(str
, default:'r+'
) –The mode to open the Zarr file ['r+'].
Source code in plantseg/io/zarr.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|