napari_cellseg3d.utils
Utilities functions, classes, and variables.
Classes:
| Name | Description |
|---|---|
Singleton |
Singleton class that can only be instantiated once at a time, with said unique instance always being accessed on call. |
Functions:
| Name | Description |
|---|---|
align_array_sizes |
Aligns the sizes of two arrays by adding zeros to the smaller one. |
channels_fraction_above_threshold |
Computes the fraction of pixels above a certain value in a 4D volume for each channel. |
correct_rotation |
Rotates the axes 0 and 2 in [DHW] section of image array. |
denormalize_y |
De-normalizes the values of an image array to be between [0;255] rather than [0;1]. |
dice_coeff |
Compute Dice-Sorensen coefficient between two numpy arrays. |
fill_list_in_between |
Fills a list with n * elem between each member of list. |
fraction_above_threshold |
Computes the fraction of pixels above a certain value in a volume. |
get_all_matching_files |
Returns a list of all files in a directory matching the pattern. |
get_date_time |
Get date and time in the following format : year_month_day_hour_minute_second. |
get_padding_dim |
Finds the nearest and superior power of two for each image dimension to zero-pad it for CNN processing. |
get_pretrained_weights_dir |
Returns the folder where pretrained weights are downloaded and loaded from. |
get_time |
Get time in the following format : hour:minute:second. NOT COMPATIBLE with file paths (saving with ":" is invalid). |
get_time_filepath |
Get time in the following format : hour_minute_second. Compatible with saving. |
load_images |
Loads the images in |
mkdir_from_str |
Creates a directory from a string path. |
normalize_max |
Normalizes an image using the max and min value. |
normalize_x |
Normalizes the values of an image array to be between [-1;1] rather than [0;255]. |
normalize_y |
Normalizes the values of an image array to be between [0;1] rather than [0;255]. |
optimize_labels_dtype |
Casts non-negative integer labels to the smallest unsigned dtype that still contains the max value. |
parse_default_path |
Returns a default path based on a vector of paths, some of which might be empty. |
quantile_normalization |
Normalizes an image using the quantiles. |
remap_image |
Normalizes a numpy array or Tensor using the max and min value. |
resize |
Resizes an image using the zoom_factors. |
save_folder |
Saves a list of images in a folder. |
save_layer |
Saves an image layer at the specified path, keeping its dtype. |
seek_best_dice_coeff_channel |
Compute Dice-Sorensen coefficient between unsupervised model output and ground truth labels; returns the channel with the highest dice coefficient. |
show_result |
Adds layers to a viewer to show result to user. |
sphericity_axis |
Computes the sphericity from volume semi major (a) and semi minor (b) axes. |
sphericity_volume_area |
Computes the sphericity from volume and area. |
time_difference |
Computes the time difference between two datetime objects. |
Singleton
Bases: type
Singleton class that can only be instantiated once at a time, with said unique instance always being accessed on call.
Should be used as a metaclass for classes without inheritance (object type).
Methods:
| Name | Description |
|---|---|
__call__ |
Call method for Singleton class. |
Source code in napari_cellseg3d/utils.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
__call__
__call__(*args, **kwargs)
Call method for Singleton class.
Ensures that only one instance of the class is created at a time, and that it is always the same instance that is returned.
Source code in napari_cellseg3d/utils.py
167 168 169 170 171 172 173 174 | |
align_array_sizes
align_array_sizes(array_shape, target_shape)
Aligns the sizes of two arrays by adding zeros to the smaller one.
Source code in napari_cellseg3d/utils.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
channels_fraction_above_threshold
channels_fraction_above_threshold(volume: array, threshold=0.5) -> list
Computes the fraction of pixels above a certain value in a 4D volume for each channel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
ndarray
|
Array of shape (C, H, W, D) containing the input volume |
required |
threshold
|
float
|
Threshold value to use for the computation |
0.5
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list
|
List of length C containing the fraction of pixels above the threshold for each channel |
Source code in napari_cellseg3d/utils.py
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | |
correct_rotation
correct_rotation(image)
Rotates the axes 0 and 2 in [DHW] section of image array.
Source code in napari_cellseg3d/utils.py
301 302 303 304 | |
denormalize_y
denormalize_y(image)
De-normalizes the values of an image array to be between [0;255] rather than [0;1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
array
|
Image to process |
required |
Returns:
| Name | Type | Description |
|---|---|---|
array |
de-normalized value for the image |
Source code in napari_cellseg3d/utils.py
454 455 456 457 458 459 460 461 462 463 | |
dice_coeff
dice_coeff(y_true: Union[Tensor, ndarray], y_pred: Union[Tensor, ndarray], smooth: float = 1.0) -> Union[torch.Tensor, np.float64]
Compute Dice-Sorensen coefficient between two numpy arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
Union[Tensor, ndarray]
|
Ground truth label |
required |
y_pred
|
Union[Tensor, ndarray]
|
Prediction label |
required |
smooth
|
float
|
Smoothing factor to avoid division by zero |
1.0
|
Returns: dice coefficient.
Source code in napari_cellseg3d/utils.py
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 | |
fill_list_in_between
fill_list_in_between(lst, n, fill_value)
Fills a list with n * elem between each member of list.
Example with list = [1,2,3], n=2, elem='&' : returns [1, &, &,2,&,&,3,&,&].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lst
|
list to fill |
required | |
n
|
number of elements to add |
required | |
fill_value
|
added n times after each element of list |
required |
Returns
Filled list
Source code in napari_cellseg3d/utils.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | |
fraction_above_threshold
fraction_above_threshold(volume: array, threshold=0.5) -> float
Computes the fraction of pixels above a certain value in a volume.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
ndarray
|
Array containing the input volume |
required |
threshold
|
float
|
Threshold value to use for the computation. |
0.5
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Fraction of pixels above the threshold |
Source code in napari_cellseg3d/utils.py
644 645 646 647 648 649 650 651 652 653 654 655 656 657 | |
get_all_matching_files
get_all_matching_files(path, pattern=None)
Returns a list of all files in a directory matching the pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
path to the directory containing the images |
required |
pattern
|
list
|
list of file extensions to match, defaults to [".tif", ".tiff"] if None |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list of all files in the directory matching the pattern, sorted alphabetically |
Source code in napari_cellseg3d/utils.py
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | |
get_date_time
get_date_time()
Get date and time in the following format : year_month_day_hour_minute_second.
Source code in napari_cellseg3d/utils.py
523 524 525 | |
get_padding_dim
get_padding_dim(image_shape, anisotropy_factor=None)
Finds the nearest and superior power of two for each image dimension to zero-pad it for CNN processing.
Accepts either 2D or 3D images shapes. E.g. an image size of 30x40x100 will result in a padding of 32x64x128. Shows a warning if the padding dimensions are very large.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_shape
|
size
|
an array of the dimensions of the image in D/H/W if 3D or H/W if 2D |
required |
anisotropy_factor
|
list
|
anisotropy factor for each dimension |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
array |
int
|
padding value for each dim |
Source code in napari_cellseg3d/utils.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | |
get_pretrained_weights_dir
get_pretrained_weights_dir() -> Path
Returns the folder where pretrained weights are downloaded and loaded from.
Uses the path set in the CELLSEG3D_WEIGHTS_DIR environment variable if defined, otherwise the pretrained folder inside the installed package. Read on each call, so the variable may be set after import.
Source code in napari_cellseg3d/utils.py
39 40 41 42 43 44 45 46 47 48 49 | |
get_time
get_time()
Get time in the following format : hour:minute:second. NOT COMPATIBLE with file paths (saving with ":" is invalid).
Source code in napari_cellseg3d/utils.py
528 529 530 | |
get_time_filepath
get_time_filepath()
Get time in the following format : hour_minute_second. Compatible with saving.
Source code in napari_cellseg3d/utils.py
533 534 535 | |
load_images
load_images(dir_or_path, filetype='', as_folder: bool = False)
Loads the images in directory, with different behaviour depending on filetype and as_folder.
- If
as_folderis False, will load the path as a single 3D .tif image. - If True, it will try to load a folder as stack of images. In this case
filetypemust be specified.
If True :
* For ``filetype == ".tif"`` : loads all tif files in the folder as a 3D dataset.
* For ``filetype == ".png"`` : loads all png files in the folder as a 3D dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dir_or_path
|
str
|
path to the directory containing the images or the images themselves |
required |
filetype
|
str
|
expected file extension of the image(s) in the directory, if as_folder is False |
''
|
as_folder
|
bool
|
Whether to load a folder of images as stack or a single 3D image |
False
|
Returns:
| Type | Description |
|---|---|
|
np.array: array with loaded images |
Source code in napari_cellseg3d/utils.py
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | |
mkdir_from_str
mkdir_from_str(path: str, exist_ok=True, parents=True)
Creates a directory from a string path.
Source code in napari_cellseg3d/utils.py
189 190 191 | |
normalize_max
normalize_max(image)
Normalizes an image using the max and min value.
Source code in napari_cellseg3d/utils.py
307 308 309 310 311 312 | |
normalize_x
normalize_x(image)
Normalizes the values of an image array to be between [-1;1] rather than [0;255].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
array
|
Image to process |
required |
Returns:
| Name | Type | Description |
|---|---|---|
array |
normalized value for the image |
Source code in napari_cellseg3d/utils.py
177 178 179 180 181 182 183 184 185 186 | |
normalize_y
normalize_y(image)
Normalizes the values of an image array to be between [0;1] rather than [0;255].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
array
|
Image to process |
required |
Returns:
| Name | Type | Description |
|---|---|---|
array |
normalized value for the image |
Source code in napari_cellseg3d/utils.py
194 195 196 197 198 199 200 201 202 203 | |
optimize_labels_dtype
optimize_labels_dtype(labels: ndarray) -> np.ndarray
Casts non-negative integer labels to the smallest unsigned dtype that still contains the max value.
Source code in napari_cellseg3d/utils.py
77 78 79 80 81 82 83 | |
parse_default_path
parse_default_path(possible_paths, check_existence=True)
Returns a default path based on a vector of paths, some of which might be empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
possible_paths
|
array of paths |
required | |
check_existence
|
whether to check if the path exists. |
True
|
Returns: the chosen default path
Source code in napari_cellseg3d/utils.py
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | |
quantile_normalization
quantile_normalization(image: Union[ndarray, Tensor], quantile_high=0.99, quantile_low=0.01)
Normalizes an image using the quantiles.
Source code in napari_cellseg3d/utils.py
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | |
remap_image
remap_image(image: Union[ndarray, Tensor], new_max=100, new_min=0, prev_max=None, prev_min=None)
Normalizes a numpy array or Tensor using the max and min value.
Source code in napari_cellseg3d/utils.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
resize
resize(image, zoom_factors)
Resizes an image using the zoom_factors.
Source code in napari_cellseg3d/utils.py
332 333 334 335 336 337 338 339 340 | |
save_folder
save_folder(results_path, folder_name, images, image_paths, exist_ok=False)
Saves a list of images in a folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results_path
|
Path to the folder containing results |
required | |
folder_name
|
Name of the folder containing results |
required | |
images
|
List of images to save |
required | |
image_paths
|
list of filenames of images |
required | |
exist_ok
|
whether to check for existing files. If False, will raise an error if the folder already exists. |
False
|
Source code in napari_cellseg3d/utils.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
save_layer
save_layer(results_path, image_name, image, labels=False)
Saves an image layer at the specified path, keeping its dtype.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results_path
|
path to folder containing result |
required | |
image_name
|
image name for saving |
required | |
image
|
data array containing image |
required | |
labels
|
if True, integer labels are saved with the smallest unsigned dtype that fits them |
False
|
Source code in napari_cellseg3d/utils.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
seek_best_dice_coeff_channel
seek_best_dice_coeff_channel(y_pred, y_true) -> torch.Tensor
Compute Dice-Sorensen coefficient between unsupervised model output and ground truth labels; returns the channel with the highest dice coefficient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
Ground truth label |
required | |
y_pred
|
Prediction label |
required |
Returns: best Dice coefficient channel.
Source code in napari_cellseg3d/utils.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
show_result
show_result(viewer, layer, image, name, existing_layer: Layer = None, colormap='bop orange', add_as_labels=False, add_as_image=False) -> napari.layers.Layer
Adds layers to a viewer to show result to user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
viewer
|
viewer to add layer in |
required | |
layer
|
original layer the operation was run on, to determine whether it should be an Image or Labels layer |
required | |
image
|
the data array containing the image |
required | |
name
|
name of the added layer |
required | |
existing_layer
|
Layer
|
existing layer to update, if any |
None
|
colormap
|
colormap to use for the layer |
'bop orange'
|
|
add_as_labels
|
whether to add the layer as a Labels layer. Overrides guessing from layer type. |
False
|
|
add_as_image
|
whether to add the layer as an Image layer. Overrides guessing from layer type. |
False
|
Returns:
| Type | Description |
|---|---|
Layer
|
napari.layers.Layer: the layer added to the viewer |
Source code in napari_cellseg3d/utils.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 | |
sphericity_axis
sphericity_axis(semi_major, semi_minor)
Computes the sphericity from volume semi major (a) and semi minor (b) axes.
.. math:: sphericity = \frac {2 \sqrt[3]{ab^2}} {a+ \frac {b^2} {\sqrt{a^2-b^2}}ln( \frac {a+ \sqrt{a^2-b^2}} {b} )}
Source code in napari_cellseg3d/utils.py
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 | |
sphericity_volume_area
sphericity_volume_area(volume, surface_area)
Computes the sphericity from volume and area.
.. math:: sphericity =\frac {\pi^\frac{1}{3} (6 V_{p})^\frac{2}{3}} {A_p}
Source code in napari_cellseg3d/utils.py
206 207 208 209 210 211 212 213 | |
time_difference
time_difference(time_start, time_finish, as_string=True)
Computes the time difference between two datetime objects.
Args: time_start (datetime): time to subtract to time_finish time_finish (datetime): time to add to subtract time_start to as_string (bool): if True, returns a string with the full time diff. Otherwise, returns as a list [hours,minutes,seconds].
Source code in napari_cellseg3d/utils.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |