Skip to content

Commit 376a891

Browse files
authored
Merge pull request #55 from HiDiHlabs/dev
Fix inconsistent VSI plotting
2 parents 381c33b + a83fba1 commit 376a891

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

ovrlpy/_plotting.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from matplotlib.axes import Axes
88
from matplotlib.colors import Colormap, LinearSegmentedColormap
99
from matplotlib.figure import Figure
10+
from matplotlib.image import AxesImage
1011
from matplotlib.patches import Rectangle
1112
from matplotlib.text import Text
1213
from matplotlib_scalebar.scalebar import ScaleBar
@@ -30,6 +31,7 @@
3031
)
3132

3233
VSI = "vertical signal integrity"
34+
_SIGNAL_THRESHOLD = 2
3335

3436

3537
def _plot_scalebar(ax: Axes, dx: float = 1, units="um", **kwargs):
@@ -249,10 +251,24 @@ def plot_pseudocells(
249251
return fig
250252

251253

254+
def _plot_signal_integrity(
255+
ax: Axes,
256+
integrity: np.ndarray,
257+
signal: np.ndarray,
258+
threshold: float,
259+
*,
260+
cmap: Colormap = BIH_CMAP,
261+
) -> AxesImage:
262+
# fade out for pixels with signal < threshold
263+
alpha = (signal / threshold).clip(0, 1) ** 2
264+
img = ax.imshow(integrity, cmap=cmap, alpha=alpha, vmin=0, vmax=1, origin="lower")
265+
return img
266+
267+
252268
def plot_signal_integrity(
253269
ovrlp: Ovrlp,
254270
*,
255-
signal_threshold: float = 2.0,
271+
signal_threshold: float = _SIGNAL_THRESHOLD,
256272
cmap: str | Colormap = BIH_CMAP,
257273
histogram: bool = True,
258274
scalebar: dict | None = SCALEBAR_PARAMS,
@@ -268,6 +284,7 @@ def plot_signal_integrity(
268284
signal_threshold : float, optional
269285
Threshold below which the signal is faded out in the plot,
270286
to avoid displaying noisy areas with low predictive confidence.
287+
Pixels below the threshold are not counted in the histogram.
271288
cmap : str | matplotlib.colors.Colormap, optional
272289
Colormap for display.
273290
histogram : bool, optional
@@ -301,14 +318,10 @@ def plot_signal_integrity(
301318

302319
assert isinstance(ax_im, Axes)
303320

304-
img = ax_im.imshow(
305-
integrity,
306-
cmap=cmap,
307-
alpha=((signal / signal_threshold).clip(0, 1) ** 2),
308-
vmin=0,
309-
vmax=1,
321+
img = _plot_signal_integrity(
322+
ax_im, integrity, signal, signal_threshold, cmap=cmap
310323
)
311-
ax_im.invert_yaxis()
324+
312325
ax_im.spines[["top", "right"]].set_visible(False)
313326

314327
if scalebar is not None:
@@ -343,7 +356,7 @@ def plot_region_of_interest(
343356
y: float,
344357
*,
345358
window_size: int = 30,
346-
signal_threshold: float = 5.0,
359+
signal_threshold: float = _SIGNAL_THRESHOLD,
347360
scalebar: dict | None = SCALEBAR_PARAMS,
348361
figsize: tuple[float, float] = (12, 8),
349362
**kwargs,
@@ -362,7 +375,8 @@ def plot_region_of_interest(
362375
window_size : int, optional
363376
Size of the window to display.
364377
signal_threshold : float, optional
365-
Threshold for the signal plot.
378+
Threshold below which the signal is faded out in the VSI plot,
379+
to avoid displaying noisy areas with low predictive confidence.
366380
scalebar : dict[str, typing.Any] | None
367381
If `None` no scalebar will be plotted. Otherwise a dictionary with
368382
additional kwargs for ``matplotlib_scalebar.scalebar.ScaleBar``.
@@ -393,18 +407,12 @@ def plot_region_of_interest(
393407
# integrity map
394408
ax_integrity = fig.add_subplot(gs[0, 2], label="signal_integrity", facecolor="k")
395409

396-
img = ax_integrity.imshow(
397-
integrity,
398-
cmap=BIH_CMAP,
399-
alpha=(signal / signal_threshold).clip(0, 1),
400-
vmin=0,
401-
vmax=1,
410+
img = _plot_signal_integrity(
411+
ax_integrity, integrity, signal, signal_threshold, cmap=BIH_CMAP
402412
)
403-
404-
ax_integrity.set_title("ROI, signal integrity")
405-
ax_integrity.invert_yaxis()
406413
fig.colorbar(img, label=VSI)
407414

415+
ax_integrity.set_title("ROI, signal integrity")
408416
ax_integrity.set_xlim(x - window_size, x + window_size)
409417
ax_integrity.set_ylim(y - window_size, y + window_size)
410418

0 commit comments

Comments
 (0)