spatialdata-io icon indicating copy to clipboard operation
spatialdata-io copied to clipboard

Cosmx reader: CellOverlay is skipped

Open StevenWijnen opened this issue 3 months ago • 0 comments

Dear,

First, thank you for creating this package! It's been incredibly helpful in processing my CosMx data.

I wanted to share a quick observation in case it helps anyone else. When downloading raw CosMx data, there are not only CellComposite folders, but also CellOverlay folder. These overlays are part of the standard CosMx workflow, where cells are segmented and the images are included in the download. However, the current CosMx reader doesn’t load these CellOverlay images into the spatial data object.

To address this, I've added a few lines to cosmx.py to enable the reading of these images. While it’s not the cleanest solution, it works for now. Here’s the code snippet I used:

        for fname in os.listdir(path / cell_overlay):
            if fname.endswith(file_extensions):
                fov = str(int(pat.findall(fname)[0]))
                if fov in fovs_counts:
                    aff = affine_transforms_to_global[fov]
                    im = imread(path / cell_overlay / fname, **imread_kwargs).squeeze()
                    flipped_im = da.flip(im, axis=0)
                    parsed_im = Image2DModel.parse(
                        flipped_im,
                        transformations={
                            fov: Identity(),
                            "global": aff,
                            "global_only_image": aff,
                        },
                        dims=("y", "x", "c"),
                        rgb=None,
                        **image_models_kwargs,
                    )
                    images[f"{fov}_overlay"] = parsed_im
                else:
                    logger.warning(f"FOV {fov} not found in counts file. Skipping image {fname}.")

To implement this, simply add the code to line 208 in CosMx.py, and it should work.

I’m happy to help further or discuss any potential improvements to this solution.

Kind regards, Steven

StevenWijnen avatar Aug 11 '25 12:08 StevenWijnen