Skip to content

Bugfix and Speedup for render_labels #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/spatialdata_plot/pl/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,11 +666,18 @@ def show(
# If any of the previous conditions are not met, generate random
# colors for each cell id

N_DISTINCT_FOR_RANDOM = 30

if sdata.table is not None:
# annoying case since number of cells in labels can be
# different from number of cells in table. So we just use
# the index and randomise colours for it

# add fake column for limiting the amount of different colors
sdata.table.obs["fake"] = np.random.randint(
0, N_DISTINCT_FOR_RANDOM, sdata.table.obs.shape[0]
)

# has a table, so it has a region key
region_key = _get_region_key(sdata)

Expand All @@ -681,7 +688,7 @@ def show(
region_key = _get_region_key(sdata)
instance_key = _get_instance_key(sdata)
params["instance_key"] = instance_key
params["color_key"] = instance_key
params["color_key"] = "fake"
params["add_legend"] = False
# TODO(ttreis) log the decision not to display a legend

Expand All @@ -693,7 +700,7 @@ def show(
cell_ids_per_label = {}
for key in list(sdata.labels.keys()):
cell_ids_per_label[key] = sdata.labels[key].values.max()

print(cell_ids_per_label)
region_key = "tmp_label_id"
instance_key = "tmp_cell_id"
params["instance_key"] = instance_key
Expand All @@ -708,10 +715,11 @@ def show(
}
)

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

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

elif sdata.table is None:
Expand Down
2 changes: 2 additions & 0 deletions src/spatialdata_plot/pl/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ def _render_labels(
ax.set_ylim(extent["y"][0], extent["y"][1])

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

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