Commit 668b4e15 authored by Leo Pound Singer's avatar Leo Pound Singer Committed by Leo Pound Singer

Add context manager and decorator to close Matplotlib figures

parent d07e6c0f
......@@ -7,6 +7,9 @@ Changelog
- Pin celery to version 4.4.2 because version 4.4.4 breaks the GWCelery unit
tests. (See https://git.ligo.org/emfollow/gwcelery/-/issues/348)
- Close Matplotlib figures that are created during tasks to avoid leaking
references and memory.
0.13.0 (2020-06-03)
-------------------
......
......@@ -35,6 +35,7 @@ from . import gracedb
from ..import app
from ..import _version
from ..jinja import env
from ..util import closing_figures
__author__ = 'Geoffrey Mo <geoffrey.mo@ligo.org>'
......@@ -98,6 +99,7 @@ def create_cache(ifo, start, end):
@app.task(shared=False)
@closing_figures()
def make_omegascan(ifo, t0, durs):
"""Helper function to create a single omegascan image, with
multiple durations.
......
......@@ -10,7 +10,8 @@ from celery.utils.log import get_task_logger
from ..import app
from . import gracedb, lvalert
from .p_astro import _format_prob
from ..util import NamedTemporaryFile, PromiseProxy, read_pickle
from ..util import (closing_figures, NamedTemporaryFile, PromiseProxy,
read_pickle)
NS_CLASSIFIER = PromiseProxy(
read_pickle, ('ligo.data', 'knn_ns_classifier.pkl'))
......@@ -49,6 +50,7 @@ def handle(alert):
@app.task(shared=False)
@closing_figures()
def plot(contents):
"""Make a visualization of the source properties.
......
......@@ -12,7 +12,7 @@ import numpy as np
from . import gracedb, lvalert
from .. import app
from ..util import PromiseProxy, read_json
from ..util import closing_figures, PromiseProxy, read_json
MEAN_VALUES_DICT = PromiseProxy(
read_json, ('ligo.data', 'H1L1V1-mean_counts-1126051217-61603201.json'))
......@@ -104,6 +104,7 @@ def _format_prob(prob):
@app.task(shared=False)
@closing_figures()
def plot(contents):
"""Make a visualization of the source classification.
......
......@@ -19,6 +19,7 @@ from . import lvalert
from ..import app
from ..jinja import env
from ..util.cmdline import handling_system_exit
from ..util.matplotlib import closing_figures
from ..util.tempfile import NamedTemporaryFile
......@@ -84,6 +85,7 @@ def fits_header(filecontents, filename):
@app.task(shared=False)
@closing_figures()
def plot_allsky(filecontents, ra=None, dec=None):
"""Plot a Mollweide projection of a sky map using the command-line tool
:doc:`ligo-skymap-plot <ligo/skymap/tool/ligo_skymap_plot>`.
......@@ -104,6 +106,7 @@ def plot_allsky(filecontents, ra=None, dec=None):
@app.task(priority=1, queue='openmp', shared=False)
@closing_figures()
def plot_volume(filecontents):
"""Plot a 3D volume rendering of a sky map using the command-line tool
:doc:`ligo-skymap-plot-volume <ligo/skymap/tool/ligo_skymap_plot_volume>`.
......@@ -233,6 +236,7 @@ def plot_bayes_factor(logb,
@app.task(shared=False)
@closing_figures()
def plot_coherence(filecontents):
"""LVAlert handler to plot the coherence Bayes factor.
......
"""Matplotlib environment management."""
from contextlib import contextmanager
from matplotlib import pyplot as plt
__all__ = ('closing_figures',)
@contextmanager
def closing_figures():
"""Close figure that are created in a with: statement."""
old_fignums = set(plt.get_fignums())
try:
yield
finally:
new_fignums = set(plt.get_fignums())
for fignum in new_fignums - old_fignums:
plt.close(fignum)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment