--- title: Utility functions keywords: fastai sidebar: home_sidebar summary: "Utility functions for deepflash2" description: "Utility functions for deepflash2" nb_path: "nbs/06_utils.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %}

Archive Extraction

{% raw %}

unzip[source]

unzip(path, zip_file)

Unzip and structure archive

{% endraw %} {% raw %}
{% endraw %}

Ensembling

{% raw %}

ensemble_results[source]

ensemble_results(res_dict, file, std=False)

Combines single model predictions.

{% endraw %} {% raw %}
{% endraw %} {% raw %}

plot_results[source]

plot_results(*args, df, unc_metric=None, figsize=(20, 20), **kwargs)

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

{% endraw %} {% raw %}
{% endraw %}

Pixelwise Analysis

{% raw %}

iou[source]

iou(a, b, threshold=0.5)

Computes the Intersection-Over-Union metric.

{% endraw %} {% raw %}
{% endraw %} {% raw %}
test_eq(iou(mask, mask), 1)
test_eq(iou(mask, empty_mask), 0)
{% endraw %}

ROI-wise Analysis

{% raw %}

label_mask[source]

label_mask(mask, threshold=0.5, min_pixel=15, do_watershed=False, exclude_border=False)

Analyze regions and return labels

{% endraw %} {% raw %}
{% endraw %} {% raw %}
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);
{% endraw %} {% raw %}
tst_lbl_b = label_mask(mask, min_pixel=150)
test_eq(tst_lbl_b.max(), 1)
plt.imshow(tst_lbl_b);
{% endraw %} {% raw %}

get_candidates[source]

get_candidates(labels_a, labels_b)

Get candiate masks for ROI-wise analysis

{% endraw %} {% raw %}
{% endraw %} {% raw %}

iou_mapping[source]

iou_mapping(labels_a, labels_b)

Compare masks using ROI-wise analysis

{% endraw %} {% raw %}
{% endraw %} {% raw %}
test_eq(iou_mapping(tst_lbl_a, tst_lbl_a), ([0., 1., 1], [0, 1, 2], [0, 1, 2], 2, 2))
test_eq(iou_mapping(tst_lbl_a, tst_lbl_b), ([0., 1.], [0, 2], [0, 1], 2, 1))
{% endraw %} {% raw %}

calculate_roi_measures[source]

calculate_roi_measures(*masks, iou_threshold=0.5, **kwargs)

Calculates precision, recall, and f1_score on ROI-level

{% endraw %} {% raw %}
{% endraw %} {% raw %}
test_eq(calculate_roi_measures(mask, mask), (1.0, 1.0, 1.0))
test_eq(calculate_roi_measures(mask, mask, min_pixel=150), (1.0, 1.0, 1.0))
{% endraw %}

Miscellaneous

{% raw %}

calc_iterations[source]

calc_iterations(n_iter, ds_length, bs)

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

{% endraw %} {% raw %}
{% endraw %} {% raw %}
test_eq(calc_iterations(100, 8, 4), 50)
{% endraw %} {% raw %}

get_label_fn[source]

get_label_fn(img_path, msk_dir_path)

Infers suffix from mask name and return label_fn

{% endraw %} {% raw %}
{% endraw %} {% raw %}

save_mask[source]

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

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

save_unc[source]

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

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