From 87d35aed56422f146b79746dce56f205a74e2447 Mon Sep 17 00:00:00 2001 From: MANUEL lab <65401298+MarinManuel@users.noreply.github.com> Date: Sat, 1 Nov 2025 14:45:11 -0400 Subject: [PATCH 1/3] added names to ImageJ ROIs --- cellpose/io.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/cellpose/io.py b/cellpose/io.py index 0aa04f46..c06cea15 100644 --- a/cellpose/io.py +++ b/cellpose/io.py @@ -649,19 +649,16 @@ def save_rois(masks, file_name, multiprocessing=None): None """ outlines = utils.outlines_list(masks, multiprocessing=multiprocessing) - nonempty_outlines = [outline for outline in outlines if len(outline)!=0] - if len(outlines)!=len(nonempty_outlines): - print(f"empty outlines found, saving {len(nonempty_outlines)} ImageJ ROIs to .zip archive.") - rois = [ImagejRoi.frompoints(outline) for outline in nonempty_outlines] - file_name = os.path.splitext(file_name)[0] + '_rois.zip' - + rois = [] + for i,outline in enumerate(outlines): + if len(outline) > 0: + rois.append(ImagejRoi.frompoints(outline, name=str(i+1))) - # Delete file if it exists; the roifile lib appends to existing zip files. - # If the user removed a mask it will still be in the zip file - if os.path.exists(file_name): - os.remove(file_name) + if len(outlines) != len(rois): + print(f"empty outlines found, saving {len(rois)} ImageJ ROIs to .zip archive.") - roiwrite(file_name, rois) + file_name = os.path.splitext(file_name)[0] + '_rois.zip' + roiwrite(file_name, rois, mode='w') def save_masks(images, masks, flows, file_names, png=True, tif=False, channels=[0, 0], From c5c22249a865d114b825805754eacfde351d4940 Mon Sep 17 00:00:00 2001 From: Marin Manuel Date: Tue, 11 Nov 2025 02:25:54 +0000 Subject: [PATCH 2/3] updated save_rois to ensure labels match mask ids --- cellpose/io.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/cellpose/io.py b/cellpose/io.py index c06cea15..66143472 100644 --- a/cellpose/io.py +++ b/cellpose/io.py @@ -638,21 +638,28 @@ def save_to_png(images, masks, flows, file_names): save_masks(images, masks, flows, file_names, png=True) -def save_rois(masks, file_name, multiprocessing=None): +def save_rois(masks, file_name, multiprocessing=None, prefix='', pad=False): """ save masks to .roi files in .zip archive for ImageJ/Fiji + When opened in ImageJ, the ROIs will be named [prefix][0000]n where n is 1,2,... corresponding to the masks label Args: masks (np.ndarray): masks output from Cellpose.eval, where 0=NO masks; 1,2,...=mask labels file_name (str): name to save the .zip file to + multiprocessing (bool, optional): Flag to enable multiprocessing. Defaults to None (disabled). + prefix (str, optional): prefix to add at the beginning of the ROI labels in ImageJ. Defaults to no prefix + pad (bool, optional): Whether to pad the numerical part of the label with zeros so that all labels have the same length Returns: None """ outlines = utils.outlines_list(masks, multiprocessing=multiprocessing) + + n_digits = int(np.floor(np.log10(masks.max()))+1) if pad else 0 + fmt = f'{{prefix}}{{:0{n_digits}d}}' rois = [] - for i,outline in enumerate(outlines): + for n,outline in zip(np.unique(masks)[1:], outlines): if len(outline) > 0: - rois.append(ImagejRoi.frompoints(outline, name=str(i+1))) + rois.append(ImagejRoi.frompoints(outline, name=fmt.format(n))) if len(outlines) != len(rois): print(f"empty outlines found, saving {len(rois)} ImageJ ROIs to .zip archive.") From f4c8d53f6ac16099e480b32523a59a8dfa5d251d Mon Sep 17 00:00:00 2001 From: Marin Manuel Date: Tue, 11 Nov 2025 02:25:54 +0000 Subject: [PATCH 3/3] updated save_rois to ensure labels match mask ids --- cellpose/io.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/cellpose/io.py b/cellpose/io.py index c06cea15..5d081bb2 100644 --- a/cellpose/io.py +++ b/cellpose/io.py @@ -638,21 +638,28 @@ def save_to_png(images, masks, flows, file_names): save_masks(images, masks, flows, file_names, png=True) -def save_rois(masks, file_name, multiprocessing=None): +def save_rois(masks, file_name, multiprocessing=None, prefix='', pad=False): """ save masks to .roi files in .zip archive for ImageJ/Fiji + When opened in ImageJ, the ROIs will be named [prefix][0000]n where n is 1,2,... corresponding to the masks label Args: masks (np.ndarray): masks output from Cellpose.eval, where 0=NO masks; 1,2,...=mask labels file_name (str): name to save the .zip file to + multiprocessing (bool, optional): Flag to enable multiprocessing. Defaults to None (disabled). + prefix (str, optional): prefix to add at the beginning of the ROI labels in ImageJ. Defaults to no prefix + pad (bool, optional): Whether to pad the numerical part of the label with zeros so that all labels have the same length Returns: None """ outlines = utils.outlines_list(masks, multiprocessing=multiprocessing) + + n_digits = int(np.floor(np.log10(masks.max()))+1) if pad else 0 + fmt = f'{{prefix}}{{id:0{n_digits}d}}' rois = [] - for i,outline in enumerate(outlines): + for n,outline in zip(np.unique(masks)[1:], outlines): if len(outline) > 0: - rois.append(ImagejRoi.frompoints(outline, name=str(i+1))) + rois.append(ImagejRoi.frompoints(outline, name=fmt.format(prefix=prefix, id=n))) if len(outlines) != len(rois): print(f"empty outlines found, saving {len(rois)} ImageJ ROIs to .zip archive.")