labsyspharm / scimap

Spatial Single-Cell Analysis Toolkit
https://scimap.xyz/
MIT License
72 stars 24 forks source link

Segmentation mask error in pl.image_viewer #25

Closed yerahko closed 2 years ago

yerahko commented 2 years ago

I am getting an error when i include a segmentation mask file:

# Pre- subset the anndata to prevent indexing error. Also, don't use `imageid` and `subset` args to image_viewer().
#selected = adata[adata.obs['Unique_ID'].isin(['PT7.ROI_1']), :]          # This doesn't work
selected = adata[adata.obs['Unique_ID'].isin(['PT7.ROI_1']), :].copy() # Making a copy makes it work

sm.pl.image_viewer(image_path=img_path,
                   seg_mask=seg_mask,   # seg_mask=None,
                   #adata=adata, imageid='Unique_ID', subset = 'PT7.ROI_1',  # Don't do this, Use pre-subsetted data instead
                   adata=selected,
                   overlay='spatial_kmeans', 
                   channel_names=layers_order,
                   x_coordinate='X', y_coordinate='Y', flip_y=False, point_size=4)

The segmentation mask file is readable, as in, I am able to display it in napari by running this chunk of code from sm.pl.image_viewer (minus the last two lines that I commented out—those break):

# Load the segmentation mask
        if seg_mask is not None:
            seg_m = tiff.imread(seg_mask)
            # if seg_m.shape[0] > 1: 
            #    seg_m = seg_m[0]   

followed by:

napari.view_image(seg_m)

However, I get the error below when trying to open it with image_viewer().

Click to expand error ``` --------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [198], in () 1 # Pre- subset the anndata to prevent indexing error. Also, don't use imageid and subset args to image_viewer(). 2 #selected = adata[adata.obs['Unique_ID'].isin(['PT7.ROI_1'])] # This doesn't work 3 selected = adata[adata.obs['Unique_ID'].isin(['PT7.ROI_1']), :].copy() # Making a copy makes it work ----> 5 viewer=sm.pl.image_viewer(image_path=img_path, 6 seg_mask=seg_path, 7 #adata=adata, imageid='Unique_ID', subset = 'PT7.ROI_1', # Don't do this, Use pre-subsetted data instead 8 adata=selected, 9 overlay='cellsimple2', # 'spatial_kmeans', # 10 channel_names=layers_order, 11 x_coordinate='X', y_coordinate='Y', flip_y=False, point_size=4) File ~/anaconda3/envs/napari-imc/lib/python3.9/site-packages/scimap/plotting/_image_viewer.py:184, in image_viewer(image_path, adata, overlay, flip_y, overlay_category, markers, channel_names, x_coordinate, y_coordinate, point_size, point_color, subset, imageid, seg_mask, **kwargs) 182 # Add the seg mask 183 if seg_mask is not None: --> 184 viewer.add_labels(seg_m, name='segmentation mask', visible=False) 186 # Add phenotype layer function 187 def add_phenotype_layer (adata, overlay, phenotype_layer,x,y,viewer,point_size,point_color): File ~/anaconda3/envs/napari-imc/lib/python3.9/site-packages/napari/components/viewer_model.py:4, in add_labels(self, data, num_colors, features, properties, color, seed, name, metadata, scale, translate, rotate, shear, affine, opacity, blending, rendering, depiction, visible, multiscale, cache, plane, experimental_clipping_planes) 1 from __future__ import annotations 3 import inspect ----> 4 import itertools 5 import os 6 import warnings File ~/anaconda3/envs/napari-imc/lib/python3.9/site-packages/napari/layers/labels/labels.py:259, in Labels.__init__(self, data, num_colors, features, properties, color, seed, name, metadata, scale, translate, rotate, shear, affine, opacity, blending, rendering, depiction, visible, multiscale, cache, plane, experimental_clipping_planes) 256 self._show_selected_label = False 257 self._contour = 0 --> 259 data = self._ensure_int_labels(data) 260 self._color_lookup_func = None 262 super().__init__( 263 data, 264 rgb=False, (...) 284 experimental_clipping_planes=experimental_clipping_planes, 285 ) File ~/anaconda3/envs/napari-imc/lib/python3.9/site-packages/napari/layers/labels/labels.py:554, in Labels._ensure_int_labels(self, data) 552 def _ensure_int_labels(self, data): 553 """Ensure data is integer by converting from bool if required, raising an error otherwise.""" --> 554 looks_multiscale, data = guess_multiscale(data) 555 if not looks_multiscale: 556 data = [data] File ~/anaconda3/envs/napari-imc/lib/python3.9/site-packages/napari/layers/image/_image_utils.py:76, in guess_multiscale(data) 72 consistent = bool(np.all(sizes[:-1] > sizes[1:])) 73 if np.all(sizes == sizes[0]): 74 # note: the individual array case should be caught by the first 75 # code line in this function, hasattr(ndim) and ndim > 1. ---> 76 raise ValueError( 77 trans._( 78 'Input data should be an array-like object, or a sequence of arrays of decreasing size. Got arrays of single shape: {shape}', 79 deferred=True, 80 shape=shapes[0], 81 ) 82 ) 83 if not consistent: 84 raise ValueError( 85 trans._( 86 'Input data should be an array-like object, or a sequence of arrays of decreasing size. Got arrays in incorrect order, shapes: {shapes}', (...) 89 ) 90 ) ValueError: Input data should be an array-like object, or a sequence of arrays of decreasing size. Got arrays of single shape: () ```

Thank you!

yerahko commented 2 years ago

Adding an example segmentation mask TIFF file that causes this error

https://cedars.box.com/s/1t47hs4exrocbhy6rhvi8eh3ln0ma0v7

Thank you!

ajitjohnson commented 2 years ago

Dear @yerahko Thank you for pointing out this issue. I have attempted to fix the bug, could you please update the package pip install scimap --upgrade and let me know if it fixes your issue? Thank you.

yerahko commented 2 years ago

Hi @ajitjohnson thanks for getting back to me so quickly. It does work now!