scverse / squidpy

Spatial Single Cell Analysis in Python
https://squidpy.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
401 stars 72 forks source link

Issues of mask_circle in function squidpy.im.calculate_image_features #751

Open ZiqianZheng opened 9 months ago

ZiqianZheng commented 9 months ago

Description

Sometimes, even when we set mask_circle=True in function squidpy.im.calculate_image_features, the features of segmentation is still calculated by a square mask rather than a circle mask.

Minimal reproducible example

The simplest reproducible example is this tutorial: https://squidpy.readthedocs.io/en/stable/notebooks/tutorials/tutorial_tangram.html

After using sq.im.calculate_image_features, if we check the distance between each spot center and the corresponding centroids, the maximum distance is larger than spot radius and less than sqrt(2)*spot radius. By using the following code, we can also easily tell that the mask is still square, not circle.

df = adata_st.obsm["image_features"]['segmentation_centroid']
spot_center = adata_st.obsm['spatial']
d_max = np.zeros(len(spot_center))
diff= []
for i in tqdm(range(len(spot_center))):
    d = 0
    for location in df[i]:
        if len(location) > 0 and location[0] is not np.nan:
            diff += [np.array(location)-spot_center[i]]
            d = max(d, np.sqrt(np.sum((diff[-1])**2)))
    d_max[i] = d
print(max(d_max)) # 52.3
diff = np.array(diff)
mpl.pyplot.scatter(diff[:, 0], diff[:, 1], s=1)
mpl.pyplot.axis('equal')

Another piece of evidence is the very last figure of this tutorial. If the mask is correct, then the centers of all dots should be in spots.