Skip to content

Commit 5f9917c

Browse files
Bugfix and Speedup for render_labels (#38)
* bugfix and speedup * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * typo --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 48227ca commit 5f9917c

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/spatialdata_plot/pl/basic.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,18 @@ def show(
666666
# If any of the previous conditions are not met, generate random
667667
# colors for each cell id
668668

669+
N_DISTINCT_FOR_RANDOM = 30
670+
669671
if sdata.table is not None:
670672
# annoying case since number of cells in labels can be
671673
# different from number of cells in table. So we just use
672674
# the index and randomise colours for it
673675

676+
# add fake column for limiting the amount of different colors
677+
sdata.table.obs["fake"] = np.random.randint(
678+
0, N_DISTINCT_FOR_RANDOM, sdata.table.obs.shape[0]
679+
)
680+
674681
# has a table, so it has a region key
675682
region_key = _get_region_key(sdata)
676683

@@ -681,7 +688,7 @@ def show(
681688
region_key = _get_region_key(sdata)
682689
instance_key = _get_instance_key(sdata)
683690
params["instance_key"] = instance_key
684-
params["color_key"] = instance_key
691+
params["color_key"] = "fake"
685692
params["add_legend"] = False
686693
# TODO(ttreis) log the decision not to display a legend
687694

@@ -693,7 +700,7 @@ def show(
693700
cell_ids_per_label = {}
694701
for key in list(sdata.labels.keys()):
695702
cell_ids_per_label[key] = sdata.labels[key].values.max()
696-
703+
print(cell_ids_per_label)
697704
region_key = "tmp_label_id"
698705
instance_key = "tmp_cell_id"
699706
params["instance_key"] = instance_key
@@ -708,10 +715,11 @@ def show(
708715
}
709716
)
710717

718+
tmp_table["fake"] = np.random.randint(0, N_DISTINCT_FOR_RANDOM, len(tmp_table))
711719
distinct_cells = max(list(cell_ids_per_label.values()))
712720

713721
if sdata.table is not None:
714-
print("Plotting a lot of cells with random colors, might take a while...")
722+
# print("Plotting a lot of cells with random colors, might take a while...")
715723
sdata.table.uns[f"{instance_key}_colors"] = _get_random_hex_colors(distinct_cells)
716724

717725
elif sdata.table is None:

src/spatialdata_plot/pl/render.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ def _render_labels(
165165
ax.set_ylim(extent["y"][0], extent["y"][1])
166166

167167
for group in groups:
168+
# Getting cell ids belonging to group and casting them to int for later numpy comparisons
168169
vaid_cell_ids = table[table[params["color_key"]] == group][params["instance_key"]].values
170+
vaid_cell_ids = [int(id) for id in vaid_cell_ids]
169171

170172
# define all out-of-group cells as background
171173
in_group_mask = segmentation.copy()

0 commit comments

Comments
 (0)