Repository: spacetelescope/jwql

Issue:
Bad thumbnail images
Looks like we are still seeing some bad thumbnail images even after #949 . We need to investigate whether these are left over bad pixels from before the fix, or if there are still bad thumbnails being produced.


Discussion:
NIRCam program 1602 still has 9 bad thumbnails. The preview images look ok though.
@bhilbert4 What server is this on? What was the creation date for the thumbnails?
This is the dev server. I'll log in and check the creation dates.
Looks like May 7
that was the same day that had the errors that I found in 01039



Resolving patch:
diff --git a/jwql/jwql_monitors/generate_preview_images.py b/jwql/jwql_monitors/generate_preview_images.py
--- a/jwql/jwql_monitors/generate_preview_images.py
+++ b/jwql/jwql_monitors/generate_preview_images.py
@@ -679,6 +679,10 @@ def process_program(program):
 
     # remove specific "ignored" suffix files (currently "original" and "stream")
     filenames = [filename for filename in filenames if os.path.splitext(filename.split('_')[-1])[0] not in IGNORED_SUFFIXES]
+
+    # Remove guiding files, as these are not currently visible in JWQL anyway
+    filenames = [filename for filename in filenames if 'guider_mode' not in filename_parser(filename)]
+
     logging.info('Found {} filenames'.format(len(filenames)))
     logging.info('')
 
diff --git a/jwql/utils/preview_image.py b/jwql/utils/preview_image.py
--- a/jwql/utils/preview_image.py
+++ b/jwql/utils/preview_image.py
@@ -167,6 +167,11 @@ def find_limits(self, data, pixmap, clipperc):
         """
         nelem = np.sum(pixmap)
         numclip = np.int(clipperc * nelem)
+
+        # Ignore any pixels that are NaN
+        finite = np.isfinite(data)
+        pixmap = pixmap & finite
+
         sorted = np.sort(data[pixmap], axis=None)
         minval = sorted[numclip]
         maxval = sorted[-numclip - 1]
@@ -295,20 +300,21 @@ def make_figure(self, image, integration_number, min_value, max_value,
 
             # If making a thumbnail, make a figure with no axes
             if thumbnail:
-                fig = plt.imshow(shiftdata,
-                                 norm=colors.LogNorm(vmin=shiftmin,
-                                                     vmax=shiftmax),
-                                 cmap=self.cmap)
+                self.fig, ax = plt.subplots(figsize=(3, 3))
+                cax = ax.imshow(shiftdata,
+                                norm=colors.LogNorm(vmin=shiftmin,
+                                                    vmax=shiftmax),
+                                cmap=self.cmap)
                 # Invert y axis
                 plt.gca().invert_yaxis()
 
                 plt.axis('off')
-                fig.axes.get_xaxis().set_visible(False)
-                fig.axes.get_yaxis().set_visible(False)
+                cax.axes.get_xaxis().set_visible(False)
+                cax.axes.get_yaxis().set_visible(False)
 
             # If preview image, add axes and colorbars
             else:
-                fig, ax = plt.subplots(figsize=(xsize, ysize))
+                self.fig, ax = plt.subplots(figsize=(xsize, ysize))
                 cax = ax.imshow(shiftdata,
                                 norm=colors.LogNorm(vmin=shiftmin,
                                                     vmax=shiftmax),
@@ -335,7 +341,7 @@ def make_figure(self, image, integration_number, min_value, max_value,
                     dig = 2
                 format_string = "%.{}f".format(dig)
                 tlabelstr = [format_string % number for number in tlabelflt]
-                cbar = fig.colorbar(cax, ticks=tickvals)
+                cbar = self.fig.colorbar(cax, ticks=tickvals)
 
                 # This seems to correctly remove the ticks and labels we want to remove. It gives a warning that
                 # it doesn't work on log scales, which we don't care about. So let's ignore that warning.
@@ -355,9 +361,12 @@ def make_figure(self, image, integration_number, min_value, max_value,
                 plt.rcParams.update({'xtick.labelsize': maxsize * 5. / 4})
 
         elif scale == 'linear':
-            fig, ax = plt.subplots(figsize=(xsize, ysize))
+            self.fig, ax = plt.subplots(figsize=(xsize, ysize))
             cax = ax.imshow(image, clim=(min_value, max_value), cmap=self.cmap)
 
+            # Invert y axis
+            plt.gca().invert_yaxis()
+
             if not thumbnail:
                 cbar = fig.colorbar(cax)
                 ax.set_xlabel('Pixels')
@@ -423,7 +432,7 @@ def make_image(self, max_img_size=8.0, create_thumbnail=False):
             self.make_figure(frame, i, minval, maxval, self.scaling.lower(),
                              maxsize=max_img_size, thumbnail=False)
             self.save_image(outfile, thumbnail=False)
-            plt.close()
+            plt.close(self.fig)
             self.preview_images.append(outfile)
 
             # Create thumbnail image matplotlib object, only for the
@@ -437,9 +446,10 @@ def make_image(self, max_img_size=8.0, create_thumbnail=False):
                 self.make_figure(frame, i, minval, maxval, self.scaling.lower(),
                                  maxsize=max_img_size, thumbnail=True)
                 self.save_image(outfile, thumbnail=True)
-                plt.close()
+                plt.close(self.fig)
                 self.thumbnail_images.append(self.thumbnail_filename)
 
+
     def save_image(self, fname, thumbnail=False):
         """
         Save an image in the requested output format and sets the
@@ -457,7 +467,6 @@ def save_image(self, fname, thumbnail=False):
             True if saving a thumbnail image, false for the full
             preview image.
         """
-
         plt.savefig(fname, bbox_inches='tight', pad_inches=0)
         permissions.set_permissions(fname)
 


Test patch: