Skip to content

Commit

Permalink
Merge 026ff35 into 1883499
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-l-kong committed May 9, 2022
2 parents 1883499 + 026ff35 commit 3e4a3a0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
21 changes: 16 additions & 5 deletions ark/utils/plot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ def plot_pixel_cell_cluster_overlay(img_xr, fovs, cluster_id_to_name_path, metac
# need to add black to denote a pixel with no clusters
mc_colors = [(0.0, 0.0, 0.0)] + list(metacluster_id_to_name['color'].values)

# map each metacluster_id_to_name to its index + 1
# NOTE: explicitly needed to ensure correct colormap colors are drawn and colorbar
# is indexed correctly when plotted
metacluster_to_index = {}
for index, row in metacluster_id_to_name.reset_index(drop=True).iterrows():
metacluster_to_index[row['metacluster']] = index + 1

# generate the colormap
cmap = colors.ListedColormap(mc_colors)
norm = colors.BoundaryNorm(
Expand All @@ -151,14 +158,18 @@ def plot_pixel_cell_cluster_overlay(img_xr, fovs, cluster_id_to_name_path, metac
# retrieve the image associated with the FOV
fov_img = img_xr[img_xr[fov_col] == fov].values

# get the unique cluster ids associated with the FOV
unique_clusters = np.sort(np.unique(fov_img))

# assign any metacluster id not in metacluster_id_to_name to 0 (not including 0 itself)
# done as a precaution, should not usually happen
acceptable_cluster_ids = [0] + list(unique_clusters)
acceptable_cluster_ids = [0] + list(metacluster_id_to_name['metacluster'])
fov_img[~np.isin(fov_img, acceptable_cluster_ids)] = 0

# explicitly relabel each value in fov_img with its index in mc_colors
# to ensure proper indexing into colormap
for mc, mc_index in metacluster_to_index.items():
fov_img[fov_img == mc] = mc_index

names, counts = np.unique(fov_img, return_counts=True)

# define the figure
fig = plt.figure(figsize=figsize)

Expand All @@ -167,7 +178,7 @@ def plot_pixel_cell_cluster_overlay(img_xr, fovs, cluster_id_to_name_path, metac

# display the image
overlay = plt.imshow(
img_xr[img_xr[fov_col] == fov].values.squeeze(),
fov_img.squeeze(),
cmap=cmap,
norm=norm,
origin='upper'
Expand Down
Loading

0 comments on commit 3e4a3a0

Please sign in to comment.