Skip to content

Commit 6c3b25c

Browse files
Add doc strings and type annotations for visualization
1 parent 7c736f6 commit 6c3b25c

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

micro_sam/visualization.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
import numpy as np
2+
23
from elf.segmentation.embeddings import embedding_pca
34
from nifty.tools import blocking
45
from skimage.transform import resize
56

7+
from .util import ImageEmbeddings
8+
69

710
#
811
# PCA visualization for the image embeddings
912
#
1013

11-
def compute_pca(embeddings):
14+
def compute_pca(embeddings: np.ndarray) -> np.ndarray:
1215
"""Compute the pca projection of the embeddings to visualize them as RGB image.
16+
17+
Args:
18+
embeddings: The embeddings. For example predicted by the SAM image encoder.
19+
20+
Returns:
21+
PCA of the embeddings, mapped to the pixels.
1322
"""
1423
if embeddings.ndim == 4:
1524
pca = embedding_pca(embeddings.squeeze()).transpose((1, 2, 0))
@@ -131,7 +140,18 @@ def _project_tiled_embeddings(image_embeddings):
131140
return embedding_vis, scale
132141

133142

134-
def project_embeddings_for_visualization(image_embeddings):
143+
def project_embeddings_for_visualization(
144+
image_embeddings: ImageEmbeddings
145+
) -> tuple[np.ndarray, tuple[float, ...]]:
146+
"""Project image embeddings to pixel-wise PCA.
147+
148+
Args:
149+
image_embeddings: The image embeddings.
150+
151+
Returns:
152+
The PCA of the embeddings.
153+
The scale factor for resizing to the original image size.
154+
"""
135155
is_tiled = image_embeddings["input_size"] is None
136156
if is_tiled:
137157
embedding_vis, scale = _project_tiled_embeddings(image_embeddings)

0 commit comments

Comments
 (0)