Skip to content

Commit ad66b2d

Browse files
YijieZhu15pre-commit-ci[bot]shaneahmed
authored
📝 Add Examples to utils/metrics.py (#921)
- Add examples to `utils/metrics.py`. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Shan E Ahmed Raza <13048456+shaneahmed@users.noreply.github.com>
1 parent 7ba7394 commit ad66b2d

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

tiatoolbox/utils/metrics.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ def pair_coordinates(
4141
- :class:`numpy.ndarray` - Unpaired B:
4242
Indices of unpaired points in set B.
4343
44+
45+
Examples:
46+
>>> from tiatoolbox.utils.metrics import pair_coordinates
47+
>>> # Generate two random example sets; replace with your own data
48+
>>> import numpy as np
49+
>>> np.random.seed(6)
50+
>>> set_a_num_points = np.random.randint(low=10, high=30)
51+
>>> set_b_num_points = np.random.randint(low=10, high=30)
52+
>>> set_a = np.random.randint(low=0, high=25, size=(set_a_num_points, 2))
53+
>>> set_b = np.random.randint(low=0, high=25, size=(set_b_num_points, 2))
54+
>>> radius = 2.0
55+
>>> # Example usage of pair_coordinates
56+
>>> pairing, unpaired_a, unpaired_b = pair_coordinates(set_a, set_b, radius)
57+
4458
"""
4559
# * Euclidean distance as the cost matrix
4660
pair_distance = distance.cdist(set_a, set_b, metric="euclidean")
@@ -65,7 +79,22 @@ def pair_coordinates(
6579

6680

6781
def f1_detection(true: np.ndarray, pred: np.ndarray, radius: float) -> float:
68-
"""Calculate the F1-score for predicted set of coordinates."""
82+
"""Calculate the F1-score for predicted set of coordinates.
83+
84+
Examples:
85+
>>> from tiatoolbox.utils.metrics import f1_detection
86+
>>> # Generate two random example sets; replace with your own data
87+
>>> import numpy as np
88+
>>> np.random.seed(6)
89+
>>> true_num_points = np.random.randint(low=10, high=30)
90+
>>> pred_num_points = np.random.randint(low=10, high=30)
91+
>>> true = np.random.randint(low=0, high=25, size=(true_num_points, 2))
92+
>>> pred = np.random.randint(low=0, high=25, size=(pred_num_points, 2))
93+
>>> radius = 2.0
94+
>>> # Example usage of f1_detection
95+
>>> f1_score = f1_detection(true, pred, radius)
96+
97+
"""
6998
(paired_true, unpaired_true, unpaired_pred) = pair_coordinates(true, pred, radius)
7099

71100
tp = len(paired_true)
@@ -94,6 +123,16 @@ def dice(gt_mask: np.ndarray, pred_mask: np.ndarray) -> float:
94123
:class:`float`:
95124
An estimate of Sørensen-Dice coefficient value.
96125
126+
Examples:
127+
>>> from tiatoolbox.utils.metrics import dice
128+
>>> # Generate two random example masks; replace with your own data
129+
>>> import numpy as np
130+
>>> np.random.seed(6)
131+
>>> gt_mask = (np.random.rand(256, 256) > 0.8).astype(np.uint8)
132+
>>> pred_mask = (np.random.rand(256, 256) > 0.8).astype(np.uint8)
133+
>>> # Example usage of dice
134+
>>> dice_score = dice(gt_mask, pred_mask)
135+
97136
"""
98137
if gt_mask.shape != pred_mask.shape:
99138
msg = f"{'Shape mismatch between the two masks.'}"

0 commit comments

Comments
 (0)