Skip to content

Commit 8d1f11a

Browse files
Update the SGN background mask computation
1 parent 0d4ab4b commit 8d1f11a

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

flamingo_tools/measurements.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,26 @@ def compute_sgn_background_mask(
351351
segmentation_key: Optional[str] = None,
352352
threshold_percentile: float = 35.0,
353353
scale_factor: Tuple[int, int, int] = (16, 16, 16),
354+
n_threads: Optional[int] = None,
354355
) -> np.typing.ArrayLike:
355-
"""
356+
"""Compute the background mask for intensity measurements in the SGN segmentation.
357+
358+
This function computes a mask for determining the background signal in the rosenthal canal.
359+
It is computed by downsampling the image (PV) and segmentation (SGNs) internally,
360+
by thresholding the downsampled image, and by then intersecting this mask with the segmentation.
361+
This results in a mask that is positive for the background signal within the rosenthal canal.
356362
357363
Args:
358-
p
364+
image_path: The path to the image data with the PV channel.
365+
segmentation_path: The path to the SGN segmentation.
366+
image_key: Internal path for the image data, for zarr or similar file formats.
367+
segmentation_key: Internal path for the segmentation data, for zarr or similar file formats.
368+
threshold_percentile: The percentile threshold for separating foreground and background in the PV signal.
369+
scale_factor: The scale factor for internally downsampling the mask.
370+
n_threads: The number of threads for parallelizing the computation.
359371
360372
Returns:
361-
pass
373+
The mask for determining the background values.
362374
"""
363375
image = read_image_data(image_path, image_key)
364376
segmentation = read_image_data(segmentation_path, segmentation_key)
@@ -393,16 +405,11 @@ def _compute_block(block_id):
393405

394406
low_res_mask[bb] = this_mask
395407

396-
# TODO parallelize
397-
for block_id in range(n_blocks):
398-
_compute_block(block_id)
399-
400-
# stain_averaged = downscale_local_mean(stain, factors=(16, 16, 16))
401-
# # The 35th percentile seems to be a decent approximation for the background subtraction.
402-
# threshold = np.percentile(stain_averaged, 35)
403-
# mask = stain_averaged > threshold
404-
# mask = resize(mask, seg_extended.shape, order=0, anti_aliasing=False, preserve_range=True).astype(bool)
405-
# mask[seg_extended != 0] = 0
408+
n_threads = mp.cpu_count() if n_threads is None else n_threads
409+
with futures.ThreadPoolExecutor(n_threads) as tp:
410+
list(tqdm(
411+
tp.map(_compute_block, range(n_blocks)), total=n_blocks, desc="Compute background mask"
412+
))
406413

407414
mask = ResizedVolume(low_res_mask, shape=original_shape, order=0)
408415
return mask

0 commit comments

Comments
 (0)