Skip to content

Commit 68839d1

Browse files
committed
added contrasting color function to give text of centroids and circles gray or black border depending on its color. made text larger, so that the snapshots are better readable
1 parent e9deb56 commit 68839d1

1 file changed

Lines changed: 82 additions & 11 deletions

File tree

TPTBox/spine/snapshot2D/snapshot_modular.py

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from matplotlib.axes import Axes
1414
from matplotlib.colors import ListedColormap, Normalize
1515
from matplotlib.patches import Circle, FancyArrow
16+
from matplotlib.patheffects import withStroke
1617
from scipy import ndimage
1718
from scipy.interpolate import RegularGridInterpolator, interp1d
1819
from scipy.signal import savgol_filter
@@ -427,6 +428,14 @@ def make_isotropic2dpluscolor(arr3d, zms2d, msk=False):
427428
return img
428429

429430

431+
def get_contrasting_stroke_color(rgb):
432+
# Convert RGBA to RGB if necessary
433+
if len(rgb) == 4:
434+
rgb = rgb[:3]
435+
luminance = 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]
436+
return "gray" if luminance < 0.3 else "black"
437+
438+
430439
def create_figure(dpi, planes: list, has_title=True):
431440
fig_h = round(2 * planes[0].shape[0] / dpi, 2) + (0.5 if has_title else 0)
432441
plane_w = [p.shape[1] for p in planes]
@@ -461,11 +470,32 @@ def plot_sag_centroids(
461470
if show_these_subreg_poi is not None:
462471
ctd2 = ctd.extract_subregion(*show_these_subreg_poi)
463472
for k1, k2, v in ctd2.items():
473+
color = cmap((k1 - 1) % LABEL_MAX % cmap.N)
474+
backgroundcolor = get_contrasting_stroke_color(color)
464475
# print(k, v, (v[1] * zms[1], v[0] * zms[0]), zms)
465476
try:
466-
axs.add_patch(Circle((v[1] * zms[1], v[0] * zms[0]), 2, color=cmap((k1 - 1) % LABEL_MAX % cmap.N)))
477+
circle = Circle(
478+
(v[1] * zms[1], v[0] * zms[0]),
479+
3,
480+
edgecolor=backgroundcolor,
481+
facecolor=color,
482+
linewidth=0.5,
483+
)
484+
axs.add_patch(circle)
467485
if not hide_centroid_labels and k2 == curve_location.value and k1 in poi_labelmap:
468-
axs.text(4, v[0] * zms[0], poi_labelmap[k1], fontdict={"color": cmap(k1 - 1), "weight": "bold"})
486+
axs.text(
487+
4,
488+
v[0] * zms[0],
489+
poi_labelmap[k1],
490+
fontdict={
491+
"color": color,
492+
"weight": "bold",
493+
"fontsize": 18,
494+
},
495+
# bbox=dict(boxstyle="square,pad=0.2", facecolor="gray", edgecolor="none"),
496+
path_effects=[withStroke(linewidth=3.0, foreground=backgroundcolor)],
497+
zorder=2,
498+
)
469499
except Exception as e:
470500
print(e)
471501
if "line_segments_sag" in ctd.info:
@@ -478,6 +508,7 @@ def plot_sag_centroids(
478508
axs.add_patch(FancyArrow(v[1] * zms[1], v[0] * zms[0], c, d, color=cmap(color - 1 % LABEL_MAX % cmap.N)))
479509
if "text_sag" in ctd.info:
480510
for color, x in ctd.info["text_sag"]:
511+
backgroundcolor = get_contrasting_stroke_color(color)
481512
if not isinstance(color, int) and len(color) == 2:
482513
color, curve_location = color # noqa: PLW2901
483514
if isinstance(x, str) or len(x) == 1:
@@ -489,7 +520,19 @@ def plot_sag_centroids(
489520
elif len(x) == 2:
490521
(text, a) = x
491522
b = zms[0] * ctd[color, curve_location][0]
492-
axs.text(a, b, text, fontdict={"color": cmap(color - 1), "weight": "bold"})
523+
axs.text(
524+
a,
525+
b,
526+
text,
527+
fontdict={
528+
"color": color,
529+
"weight": "bold",
530+
"fontsize": 18,
531+
},
532+
# bbox=dict(boxstyle="square,pad=0.2", facecolor="gray", edgecolor="none"),
533+
path_effects=[withStroke(linewidth=3.0, foreground=backgroundcolor)],
534+
zorder=2,
535+
)
493536

494537

495538
def plot_cor_centroids(
@@ -508,16 +551,31 @@ def plot_cor_centroids(
508551

509552
# requires v_dict = dictionary of mask labels
510553
for k1, k2, v in ctd2.items():
554+
color = cmap((k1 - 1) % LABEL_MAX % cmap.N)
555+
backgroundcolor = get_contrasting_stroke_color(color)
511556
try:
512-
axs.add_patch(
513-
Circle(
514-
(v[2] * zms[2], v[0] * zms[0]),
515-
2,
516-
color=cmap((k1 - 1) % LABEL_MAX % cmap.N),
517-
)
557+
circle = Circle(
558+
(v[2] * zms[2], v[0] * zms[0]),
559+
3,
560+
edgecolor=backgroundcolor,
561+
facecolor=color,
562+
linewidth=0.5,
518563
)
564+
axs.add_patch(circle)
519565
if not hide_centroid_labels and k2 == curve_location.value and k1 in poi_labelmap:
520-
axs.text(4, v[0] * zms[0], poi_labelmap[k1], fontdict={"color": cmap(k1 - 1), "weight": "bold"})
566+
axs.text(
567+
4,
568+
v[0] * zms[0],
569+
poi_labelmap[k1],
570+
fontdict={
571+
"color": color,
572+
"weight": "bold",
573+
"fontsize": 18,
574+
},
575+
# bbox=dict(boxstyle="square,pad=0.2", facecolor="gray", edgecolor="none"),
576+
path_effects=[withStroke(linewidth=3.0, foreground=backgroundcolor)],
577+
zorder=2,
578+
)
521579
except Exception:
522580
pass
523581
if "line_segments_cor" in ctd.info:
@@ -529,6 +587,7 @@ def plot_cor_centroids(
529587
axs.add_patch(FancyArrow(v[2] * zms[2], v[0] * zms[0], c, d, color=cmap(color - 1 % LABEL_MAX % cmap.N)))
530588
if "text_cor" in ctd.info:
531589
for color, x in ctd.info["text_cor"]:
590+
backgroundcolor = get_contrasting_stroke_color(color)
532591
if isinstance(color, Sequence) and len(color) == 2:
533592
color, curve_location = color # noqa: PLW2901
534593
if isinstance(x, str) or len(x) == 1:
@@ -540,7 +599,19 @@ def plot_cor_centroids(
540599
elif len(x) == 2:
541600
(text, a) = x
542601
b = zms[0] * ctd[color, curve_location][0]
543-
axs.text(a, b, text, fontdict={"color": cmap(color - 1), "weight": "bold"})
602+
axs.text(
603+
a,
604+
b,
605+
text,
606+
fontdict={
607+
"color": color,
608+
"weight": "bold",
609+
"fontsize": 18,
610+
},
611+
# bbox=dict(boxstyle="square,pad=0.2", facecolor="gray", edgecolor="none"),
612+
path_effects=[withStroke(linewidth=3.0, foreground=backgroundcolor)],
613+
zorder=2,
614+
)
544615

545616

546617
def make_2d_slice(

0 commit comments

Comments
 (0)