scverse / scverse/spatialdata

get_centroids() slow on on labels elements

Open
#936 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
394
Forks
95
Avg merge
4d 3h
Merged PRs (30d)
7

Description

When trying to get centroids of labels, the function get_centroids is really taking forever, even on small data.
This makes it very difficult to run on real life data.

For example, it takes 3 minutes on a 848x2540 image. I tested with a tqdm in _get_centroids_for_axis to check what was taking time:

Computing centroids along the y axis: 100%|██████████| 848/848 [00:50<00:00, 16.78it/s]
Computing centroids along the x axis: 100%|██████████| 2540/2540 [02:04<00:00, 20.47it/s]

A minimal example to reproduce:

from spatialdata import get_centroids
from spatialdata.datasets import blobs
import time

sdata = blobs()

t = time.process_time()
get_centroids(sdata["blobs_labels"])
print (time.process_time() - t)

That shows around 10 seconds for a 512x512 image.

For comparion, the following code that also computes centroids, takes about 0.01 second:

from spatialdata import get_centroids
from spatialdata.datasets import blobs
import time

sdata = blobs()

import numpy as np
from skimage.measure import regionprops
import pandas as pd

t = time.process_time()
label_image = sdata["blobs_labels"].values.T
props = regionprops(label_image)
centroids = pd.DataFrame(
    [p.centroid for p in props],
    columns=["x", "y"],
    index=[p.label for p in props]
)
print (time.process_time() - t)

As get_centroids() is used in to_legacy_anndata, it makes it also really slow on large datasets.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with get_centroids and _get_centroids_for_axis, then trace how to_legacy_anndata calls them. Run the minimal blobs example and compare its timing and centroid results with the regionprops approach; done means get_centroids preserves its results without the reported multi-minute slowdown.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.