gustaveroussy / sopa

Technology-invariant pipeline for spatial omics analysis (Xenium / Visium HD / MERSCOPE / CosMx / PhenoCycler / MACSima / ...) that scales to millions of cells
https://gustaveroussy.github.io/sopa/
BSD 3-Clause "New" or "Revised" License
129 stars 15 forks source link

Adding shapes to exisiting Spatialdata #95

Closed pakiessling closed 3 months ago

pakiessling commented 3 months ago

Hi,

I am tinkering with a new conflict resolution strategy.

I wrote a cli that takes as input a Spatialdata folder and a directory of .parquet segmentation files.

After resolving overlaps I try to save the shapes to the .zarr store similiarly to sopa resolve

def add_shape_df(sdata, geo_df, image_key, shapes_key):
    image = get_spatial_image(sdata, image_key)
    geo_df.index = image_key + geo_df.index.astype(str)
    geo_df = ShapesModel.parse(
            geo_df, transformations=get_transformation(image, get_all=True).copy()
        )
    sdata.shapes[shapes_key] = geo_df
    if sdata.is_backed():
            sdata.write_element(shapes_key, overwrite=True)
    log.info(f"Added {len(geo_df)} cell boundaries in sdata['{shapes_key}']")

Unfortunately, I always run into:

ValueError: Cannot overwrite. The target path of the write operation is in use. Please save the data to a different location. 
Details: the target path in which to save an element is a subfolder of the current Zarr store.
Workaround: please see discussion here https://github.com/scverse/spatialdata/discussions/520 .

Could you point me to where you accomplish this without writing to a new zarr store?

quentinblampey commented 3 months ago

Hi,

You already have existing shapes that are stored on-disk, so you need to remove them before saving new shapes:

if sdata.is_backed():
      sdata.delete_element_from_disk(shapes_key)
      sdata.write_element(shapes_key, overwrite=True)

If you prefer, you can also use another shapes_key (i.e. a name of shapes that is not already written on disk)

Hope this helps!

pakiessling commented 3 months ago

Ah, now I understand ty!