Skip to content

Custom Segmentation Error #407

Description

@hallumg

Hi Sopa team!

I am experiencing a strange issue while using the SOPA module for cell segmentation. Let me first describe my situation:

I have a Xenium experiment from which I only obtained DAPI staining onboard. I have aligned with post-hoc IF images on the same slides. I trained a highly accurate cellpose model for segmenting the cells with the cellpose GUI (v4), and then implemented this model with SOPA by specifying thusly:

sopa.make_image_patches(cropped_sdata, patch_width=2000, patch_overlap=50, image_key=selection_name)

sopa.segmentation.cellpose(cropped_sdata, channels='AF594', diameter=50, pretrained_model='/mount/models/cpsam_20251119_200959_sample1', gpu=True, image_key=selection_name, min_area=None, delete_cache=True, recover=False, flow_threshold=.5, cellprob_threshold=.5, clip_limit=0.2, clahe_kernel_size=None, gaussian_sigma=1, key_added='cellpose_1')

sopa.aggregate(cropped_sdata, aggregate_genes=True, aggregate_channels=True, image_key=selection_name)

cropped_sdata.write(f'{selection_name}_cropped_SOPAcellpose.zarr')

### XENIUM EXPLORER
sopa.io.explorer.write(f"{selection_name}_cropped_SOPAPIPE.explorer", cropped_sdata, image_key=selection_name, shapes_key='cellpose_1', pixel_size=0.2125, layer=None, polygon_max_vertices=13, lazy=True, ram_threshold_gb=4, mode=None, save_h5ad=True, run_name=None)

I am now trying to re-segment ONLY the nuclei from these slides. I have trained a somewhat accurate model, but replacing the pretrained model arguments above does not yield similar results. I can't seem to figure out why it is not reproducible via sopa.segmentation.cellpose, only that it is not.

Alternatively, using the cellpose GUI / cellpose API directly, I am able to segment with the model very well. I attempted to add in the .npy file with segmentation using only the 'masks' portion of the output (via this post: #361) , but got the below error when trying to save the explorer bundle after re-aggregating:

"File "/usr/local/analysis/sopa/2.1.1/venv/lib/python3.10/site-packages/zarr/indexing.py", line 167, in ceildiv return math.ceil(a / b) ZeroDivisionError: division by zero"

So my question is: Is there a better way to implement a custom cellpose model that I know performs optimally outside of SOPA? I have to use SOPA because it is the only method I have found that consistently works with the Xenium Explorer.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions