Skip to content

Commit ca755f0

Browse files
YijieZhu15pre-commit-ci[bot]shaneahmed
authored
📝 Add utils.visualization Examples (#943)
- Add examples in docstrings for `utils.visualization`. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Shan E Ahmed Raza <[email protected]>
1 parent 6ac6f55 commit ca755f0

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

tiatoolbox/utils/visualization.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ def random_colors(num_colors: int, *, bright: bool) -> np.ndarray:
5656
np.ndarray:
5757
Array of (r, g, b) colors.
5858
59+
Examples:
60+
>>> from tiatoolbox.utils.visualization import random_colors
61+
>>> colors = random_colors(10, bright=True)
62+
5963
"""
6064
brightness = 1.0 if bright else 0.7
6165
hsv = [(i / num_colors, 1, brightness) for i in range(num_colors)]
@@ -75,6 +79,15 @@ def colourise_image(img: np.ndarray, cmap: str = "viridis") -> np.ndarray:
7579
7680
Returns:
7781
img(ndarray): An RGB image.
82+
83+
Examples:
84+
>>> from tiatoolbox.utils.visualization import colourise_image
85+
>>> import numpy as np
86+
>>> # Generate a random example; replace with your own data
87+
>>> img = np.random.rand(255, 255)
88+
>>> # Example usage of colourise_image
89+
>>> coloured_image = colourise_image(img, 'viridis')
90+
7891
"""
7992
if len(img.shape) == 2: # noqa: PLR2004
8093
# Single channel, make into rgb with colormap.
@@ -124,6 +137,30 @@ def overlay_prediction_mask(
124137
If return_ax is True, return the matplotlib ax object. Else,
125138
return the overlay array.
126139
140+
Examples:
141+
>>> from tiatoolbox.utils.visualization import overlay_prediction_mask
142+
>>> import numpy as np
143+
>>> from matplotlib import pyplot as plt
144+
>>> # Generate a random example; replace with your own data
145+
>>> img = np.random.randint(0, 256, size=(256, 256, 3), dtype=np.uint8)
146+
>>> prediction = np.random.randint(0, 3, size=(256, 256), dtype=np.uint8)
147+
>>> label_info = {
148+
... 0: ("Background", (0, 0, 0)),
149+
... 1: ("Tumor", (255, 0, 0)),
150+
... 2: ("Stroma", (0, 255, 0))
151+
... }
152+
>>> # Example usage of overlay_prediction_mask
153+
>>> ax = overlay_prediction_mask(
154+
... img=img,
155+
... prediction=prediction,
156+
... alpha=0.5,
157+
... label_info=label_info,
158+
... min_val=0.0,
159+
... ax=None,
160+
... return_ax=True
161+
... )
162+
>>> plt.show()
163+
127164
"""
128165
# Validate inputs
129166
if img.shape[:2] != prediction.shape[:2]:
@@ -310,6 +347,25 @@ def overlay_probability_map(
310347
If return_ax is True, return the matplotlib ax object. Else,
311348
return the overlay array.
312349
350+
Examples:
351+
>>> from tiatoolbox.utils.visualization import overlay_probability_map
352+
>>> import numpy as np
353+
>>> from matplotlib import pyplot as plt
354+
>>> # Generate a random example; replace with your own data
355+
>>> img = np.random.randint(0, 256, size=(256, 256, 3), dtype=np.uint8)
356+
>>> probability_map = np.random.rand(256, 256).astype(np.float32)
357+
>>> # Example usage of overlay_probability_map
358+
>>> ax = overlay_probability_map(
359+
... img=img,
360+
... prediction=probability_map,
361+
... alpha=0.35,
362+
... colour_map="jet",
363+
... min_val=0.0,
364+
... ax=None,
365+
... return_ax=True,
366+
... )
367+
>>> plt.show()
368+
313369
"""
314370
prediction = prediction.astype(np.float32)
315371
img = _validate_overlay_probability_map(img, prediction, min_val)
@@ -455,6 +511,40 @@ def overlay_prediction_contours(
455511
:class:`numpy.ndarray`:
456512
The overlaid image.
457513
514+
Examples:
515+
>>> from tiatoolbox.utils.visualization import overlay_prediction_contours
516+
>>> import numpy as np
517+
>>> from matplotlib import pyplot as plt
518+
>>> # Generate a random example; replace with your own data
519+
>>> canvas = np.zeros((256, 256, 3), dtype=np.uint8)
520+
>>> inst_dict = {
521+
... 1: {
522+
... "type": 0,
523+
... "contour": [[50, 50], [60, 45], [70, 50],
524+
... [70, 60], [60, 65], [50, 60]],
525+
... "centroid": [60, 55]
526+
... },
527+
... 2: {
528+
... "type": 1,
529+
... "contour": [[100, 100], [120, 100], [120, 120], [100, 120]],
530+
... "centroid": [110, 110]
531+
... }
532+
... }
533+
>>> type_colours = {
534+
... 0: ("Type A", (0, 255, 0)),
535+
... 1: ("Type B", (0, 0, 255))
536+
... }
537+
>>> # Example usage of overlay_prediction_contours
538+
>>> overlaid_canvas = overlay_prediction_contours(
539+
... canvas=canvas,
540+
... inst_dict=inst_dict,
541+
... type_colours=type_colours,
542+
... line_thickness=1,
543+
... draw_dot=True
544+
... )
545+
>>> plt.imshow(overlaid_canvas)
546+
>>> plt.show()
547+
458548
"""
459549
overlay = np.copy(canvas)
460550

@@ -531,6 +621,28 @@ def plot_graph(
531621
edge_size (int):
532622
Line width of the edge.
533623
624+
Examples:
625+
>>> from tiatoolbox.utils.visualization import plot_graph
626+
>>> import numpy as np
627+
>>> # Generate a random example; replace with your own data
628+
>>> canvas = np.zeros((256, 256, 3), dtype=np.uint8)
629+
>>> num_nodes = 10
630+
>>> nodes = np.random.randint(0, 255, size=(num_nodes, 2))
631+
>>> num_edges = 15
632+
>>> edges = np.random.randint(0, num_nodes, size=(num_edges, 2))
633+
>>> node_colors = np.random.randint(0, 256, size=(num_nodes, 3))
634+
>>> edge_colors = np.random.randint(0, 256, size=(num_edges, 3))
635+
>>> # Example usage of overlay_prediction_contours
636+
>>> overlaid_canvas = plot_graph(
637+
... canvas=canvas,
638+
... nodes=nodes,
639+
... edges=edges,
640+
... node_colors=node_colors,
641+
... node_size=8,
642+
... edge_colors=edge_colors,
643+
... edge_size=3
644+
... )
645+
534646
"""
535647
if isinstance(node_colors, tuple):
536648
node_colors_list = np.array([node_colors] * len(nodes))

0 commit comments

Comments
 (0)