Skip to content

Commit 4ebfe86

Browse files
authored
WTNR thumbnails: sigma clipping-based plotting (#576)
* for wntr thumbnails, set nans to the max value and use sigma clipping to get vmin and vmax
1 parent 1e6f89f commit 4ebfe86

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

kowalski/alert_brokers/alert_broker.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import pandas as pd
2323
import requests
2424
from astropy.io import fits
25+
from astropy.stats import sigma_clipped_stats
2526
from astropy.visualization import (
2627
AsymmetricPercentileInterval,
2728
ImageNormalize,
@@ -662,22 +663,36 @@ def make_thumbnail(
662663
xl = np.greater(np.abs(img), 1e20, where=~np.isnan(img))
663664
if img[xl].any():
664665
img[xl] = np.nan
665-
if np.isnan(img).any():
666-
median = float(np.nanmean(img.flatten()))
667-
img = np.nan_to_num(img, nan=median)
668-
669-
norm = ImageNormalize(
670-
img,
671-
stretch=LinearStretch()
672-
if alert_packet_type == "Difference"
673-
else LogStretch(),
674-
)
675-
img_norm = norm(img)
676-
normalizer = AsymmetricPercentileInterval(
677-
lower_percentile=1, upper_percentile=100
678-
)
679-
vmin, vmax = normalizer.get_limits(img_norm)
680-
ax.imshow(img_norm, cmap="bone", origin="lower", vmin=vmin, vmax=vmax)
666+
667+
if self.instrument == "WNTR":
668+
# replace nans with max value
669+
if np.isnan(img).any():
670+
max = float(np.nanmax(img.flatten()))
671+
img = np.nan_to_num(img, nan=max)
672+
673+
# get vmin and vmax with sigma clipping
674+
_, med, std = sigma_clipped_stats(img)
675+
vmin, vmax = med - 4 * std, med + 5 * std
676+
else:
677+
# replace nans with median
678+
if np.isnan(img).any():
679+
median = float(np.nanmean(img.flatten()))
680+
img = np.nan_to_num(img, nan=median)
681+
682+
# get vmin and vmax with ImageNormalize + AsymmetricPercentileInterval
683+
norm = ImageNormalize(
684+
img,
685+
stretch=LinearStretch()
686+
if alert_packet_type == "Difference"
687+
else LogStretch(),
688+
)
689+
img = norm(img)
690+
normalizer = AsymmetricPercentileInterval(
691+
lower_percentile=1, upper_percentile=100
692+
)
693+
vmin, vmax = normalizer.get_limits(img)
694+
695+
ax.imshow(img, cmap="bone", origin="lower", vmin=vmin, vmax=vmax)
681696
plt.savefig(buff, dpi=42)
682697

683698
buff.seek(0)

0 commit comments

Comments
 (0)