Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions MetricsReloaded/utility/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ def border_map2(self):
return border

def foreground_component(self):
return ndimage.label(self.binary_map)
structure = ndimage.generate_binary_structure(
self.binary_map.ndim, self.connectivity
)
return ndimage.label(self.binary_map, structure=structure)

def list_foreground_component(self):
labels, _ = self.foreground_component()
Expand Down Expand Up @@ -536,4 +539,4 @@ def trapezoidal_integration(x, fx):
Reference
https://en.wikipedia.org/w/index.php?title=Trapezoidal_rule&oldid=1104074899#Non-uniform_grid
"""
return np.sum((fx[:-1] + fx[1:])/2 * (x[1:] - x[:-1]))
return np.sum((fx[:-1] + fx[1:])/2 * (x[1:] - x[:-1]))
15 changes: 13 additions & 2 deletions test/test_utility/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
import numpy as np
from numpy.testing import assert_allclose, assert_array_equal
from MetricsReloaded.utility.utils import intersection_boxes, guess_input_style, com_from_box, point_in_box, point_in_mask, area_box, compute_box, compute_center_of_mass, compute_skeleton, combine_df, distance_transform_edt, one_hot_encode, median_heuristic, box_ior, box_iou, union_boxes, max_x_at_y_less, min_x_at_y_less, skeletonize, trapezoidal_integration
from MetricsReloaded.utility.utils import intersection_boxes, guess_input_style, com_from_box, point_in_box, point_in_mask, area_box, compute_box, compute_center_of_mass, compute_skeleton, combine_df, distance_transform_edt, one_hot_encode, median_heuristic, box_ior, box_iou, union_boxes, max_x_at_y_less, min_x_at_y_less, skeletonize, trapezoidal_integration, MorphologyOps


box3 = [2,3, 4,4]
Expand Down Expand Up @@ -80,6 +80,17 @@ def test_compute_center_of_mass():
mask[4:6,4:5] = 1
assert_array_equal(compute_center_of_mass(mask),np.asarray([3.125, 4.9375]))

def test_foreground_component_uses_connectivity():
mask = np.zeros([2, 2])
mask[0, 0] = 1
mask[1, 1] = 1

_, nlabels_connectivity_1 = MorphologyOps(mask, 1).foreground_component()
_, nlabels_connectivity_2 = MorphologyOps(mask, 2).foreground_component()

assert nlabels_connectivity_1 == 2
assert nlabels_connectivity_2 == 1

def test_box_ior():
box1 = [3,5,5,7]
box2 = [3,4,4,6]
Expand Down Expand Up @@ -121,4 +132,4 @@ def test_min_x_at_y_less():
x = [1, 2, 1, 3, 4, 0, 1, 4, 5]
y = [1, 2, 3, 4, 5, 6, 7 ,8, 9]
assert min_x_at_y_less(np.asarray(x), np.asarray(y),6) == 0
#compute_skeleton, combine_df, distance_transform_edt, one_hot_encode, median_heuristic, max_x_at_y_less, min_x_at_y_less, skeletonize, trapezoidal_integration
#compute_skeleton, combine_df, distance_transform_edt, one_hot_encode, median_heuristic, max_x_at_y_less, min_x_at_y_less, skeletonize, trapezoidal_integration