Skip to content

Commit

Permalink
create wrapper func for saving png files
Browse files Browse the repository at this point in the history
  • Loading branch information
bhilbert4 committed Sep 21, 2023
1 parent d85da1c commit 477072f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
5 changes: 2 additions & 3 deletions jwql/instrument_monitors/common_monitors/dark_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
from astropy.modeling import models
from astropy.stats import sigma_clipped_stats
from astropy.time import Time
from bokeh.io import export_png
from bokeh.models import ColorBar, ColumnDataSource, HoverTool, Legend
from bokeh.models import LinearColorMapper
from bokeh.plotting import figure
Expand All @@ -106,7 +105,7 @@
from jwql.utils.constants import JWST_INSTRUMENT_NAMES_MIXEDCASE, JWST_DATAPRODUCTS, RAPID_READPATTERNS
from jwql.utils.logging_functions import log_info, log_fail
from jwql.utils.permissions import set_permissions
from jwql.utils.utils import copy_files, ensure_dir_exists, get_config, filesystem_path
from jwql.utils.utils import copy_files, ensure_dir_exists, get_config, filesystem_path, save_png

THRESHOLDS_FILE = os.path.join(os.path.split(__file__)[0], 'dark_monitor_file_thresholds.txt')

Expand Down Expand Up @@ -350,7 +349,7 @@ def create_mean_slope_figure(self, image, num_files, hotxy=None, deadxy=None, no
self.plot.add_layout(legend, 'below')

# Save the plot in a png
export_png(self.plot, filename=output_filename)
save_png(self.plot, filename=output_filename)
set_permissions(output_filename)


Expand Down
23 changes: 22 additions & 1 deletion jwql/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from bokeh.plotting import figure
import numpy as np
from PIL import Image
from selenium import webdriver

from jwql.utils import permissions
from jwql.utils.constants import FILE_AC_CAR_ID_LEN, FILE_AC_O_ID_LEN, FILE_ACT_LEN, \
Expand Down Expand Up @@ -170,7 +171,7 @@ def create_png_from_fits(filename, outdir):

# Save the plot in a png
output_filename = os.path.join(outdir, os.path.basename(filename).replace('fits','png'))
export_png(plot, filename=output_filename)
save_png(plot, filename=output_filename)
permissions.set_permissions(output_filename)
return output_filename
else:
Expand Down Expand Up @@ -756,6 +757,26 @@ def read_png(filename):
return img


def save_png(fig, filename=''):
"""Starting with selenium version 4.10.0, our testing has shown that on the JWQL
servers, we need to specify an instance of a web driver when exporting a Bokeh
figure as a png. This is a wrapper function that creates the web driver instance
and calls Bokeh's export_png function.
Parameters
----------
fig : bokeh.plotting.figure
Bokeh figure to be saved as a png
filename : str
Filename to use for the png file
"""
options = webdriver.FirefoxOptions()
options.add_argument('-headless')
driver = webdriver.Firefox(options=options)
export_png(fig, filename=filename, webdriver=driver)


def grouper(iterable, chunksize):
"""
Take a list of items (iterable), and group it into chunks of chunksize, with the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from astropy.stats import sigma_clipped_stats
from astropy.time import Time
from bokeh.embed import components, file_html
from bokeh.io import export_png, show
from bokeh.io import show
from bokeh.layouts import layout
from bokeh.models import ColumnDataSource, DatetimeTickFormatter, HoverTool, Legend, LinearColorMapper, Panel, Tabs, Text, Title
from bokeh.plotting import figure
Expand All @@ -40,7 +40,7 @@
from jwql.utils.constants import BAD_PIXEL_MONITOR_MAX_POINTS_TO_PLOT, BAD_PIXEL_TYPES, DARKS_BAD_PIXEL_TYPES
from jwql.utils.constants import DETECTOR_PER_INSTRUMENT, FLATS_BAD_PIXEL_TYPES, JWST_INSTRUMENT_NAMES_MIXEDCASE
from jwql.utils.permissions import set_permissions
from jwql.utils.utils import filesystem_path, get_config, read_png
from jwql.utils.utils import filesystem_path, get_config, read_png, save_png

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
OUTPUT_DIR = get_config()['outputs']
Expand Down Expand Up @@ -593,7 +593,7 @@ def switch_to_png(self, filename, title):
Title to add to the Figure
"""
# Save the figure as a png
export_png(self.plot, filename=filename)
save_png(self.plot, filename=filename)
set_permissions(filename)

# Read in the png and insert into a replacement figure
Expand Down

0 comments on commit 477072f

Please sign in to comment.