Utility functions for deepflash2

Data Download and Archive Extraction

unzip[source]

unzip(path, zip_file)

Unzip and structure archive

download_sample_data[source]

download_sample_data(base_url, name, dest, extract=False, timeout=4, show_progress=True)

Install packages on demand

install_package[source]

install_package(package, version=None)

import_package[source]

import_package(package, version=None)

compose_albumentations[source]

compose_albumentations(gamma_limit_lower=0, gamma_limit_upper=0, CLAHE_clip_limit=0.0, brightness_limit=0, contrast_limit=0.0, distort_limit=0.0)

Compose albumentations augmentations

Plotting functions

clean_show[source]

clean_show(ax, msk, title, cmap, cbar=None, ticks=None, **kwargs)

plot_results[source]

plot_results(*args, df, hastarget=False, num_classes=2, model=None, instance_labels=False, metric_name='dice_score', unc_metric=None, figsize=(20, 20), msk_cmap='viridis', **kwargs)

Plot images, (masks), predictions and uncertainties side-by-side.

Patch to show metrics in Learner

Recorder.plot_metrics[source]

Recorder.plot_metrics(nrows=None, ncols=None, figsize=None, sharex=False, sharey=False, squeeze=True, width_ratios=None, height_ratios=None, subplot_kw=None, gridspec_kw=None)

Pixelwise Analysis

multiclass_dice_score[source]

multiclass_dice_score(y_true, y_pred, average='macro', **kwargs)

Computes the Sørensen–Dice coefficient for multiclass segmentations.

binary_dice_score[source]

binary_dice_score(y_true, y_pred)

Compute the Sørensen–Dice coefficient for binary segmentations.

dice_score[source]

dice_score(y_true, y_pred, average='macro', num_classes=2, **kwargs)

Computes the Sørensen–Dice coefficient.

test_eq(dice_score(mask, mask), 1)
test_eq(dice_score(mask, empty_mask), 0)

# Todo: add multiclass tests https://scikit-learn.org/stable/modules/generated/sklearn.metrics.jaccard_score.html

ROI-wise Analysis

label_mask[source]

label_mask(mask, threshold=0.5, connectivity=4, min_pixel=0, do_watershed=False, exclude_border=False)

Analyze regions and return labels

tst_lbl_a = label_mask(mask, min_pixel=0)
test_eq(tst_lbl_a.max(), 2)
test_eq(tst_lbl_a.min(), 0)
plt.imshow(tst_lbl_a);
tst_lbl_b = label_mask(mask, min_pixel=150)
test_eq(tst_lbl_b.max(), 1)
plt.imshow(tst_lbl_b);

get_instance_segmentation_metrics[source]

get_instance_segmentation_metrics(a, b, is_binary=False, thresholds=None, **kwargs)

Computes instance segmentation metric based on cellpose/stardist implementation. https://cellpose.readthedocs.io/en/latest/api.html#cellpose.metrics.average_precision

ap, tp, fp, fn = get_instance_segmentation_metrics(mask, mask, is_binary=True)
test_eq(len(ap),10)
test_eq(tp[0],2)
ap, tp, fp, fn = get_instance_segmentation_metrics(mask, empty_mask, is_binary=True, thresholds=[.5])
test_eq(len(ap),1)
test_eq(fn[0],2)
2022-06-08 12:01:32,630 [INFO] WRITING LOG OUTPUT TO /home/magr/.cellpose/run.log

ROI Export to ImageJ

export_roi_set[source]

export_roi_set(mask, intensity_image=None, instance_labels=False, name='RoiSet', path=Path('.'), ascending=True, min_pixel=0)

EXPERIMENTAL: Export mask regions to imageJ ROI Set

path = export_roi_set(mask)
path.unlink()

Miscellaneous

calc_iterations[source]

calc_iterations(n_iter, ds_length, bs)

Calculate the number of required epochs for 'n_iter' iterations.

test_eq(calc_iterations(100, 8, 4), 50)

get_label_fn[source]

get_label_fn(img_path, msk_dir_path)

Infers suffix from mask name and return label_fn

save_mask[source]

save_mask(mask, path, filetype='.png')

def save_mask(mask, path, filetype='.png'):
    mask = mask.astype(np.uint8) if np.max(mask)>1 else (mask*255).astype(np.uint8)
    imageio.imsave(path.with_suffix(filetype), mask)

save_unc[source]

save_unc(unc, path, filetype='.png')

def save_unc(unc, path, filetype='.png'):
    unc = (unc/unc.max()*255).astype(np.uint8)
    imageio.imsave(path.with_suffix(filetype), unc)