Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use headless ("Agg") matplotlib backend in CI #1133

Merged
merged 3 commits into from May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/run_tests.yml
Expand Up @@ -19,6 +19,11 @@ env:
# (TravisCI quietly defined this on all their platforms, but we have to give it manually on GithubCI.)
DEBIAN_FRONTEND: 'noninteractive'
HDF5_USE_FILE_LOCKING: 'FALSE'
# Skip to the headless matplotlib renderer, which is less
# bug-prone in the constrained environment of CI
# Tip from a matplotlib dev: https://github.com/spinalcordtoolbox/spinalcordtoolbox/issues/3388#issuecomment-846091012
# Ref: https://matplotlib.org/stable/users/explain/backends.html
MPLBACKEND: 'Agg'
joshuacwnewton marked this conversation as resolved.
Show resolved Hide resolved

jobs:
ultra_matrix_test:
Expand Down
17 changes: 2 additions & 15 deletions ivadomed/scripts/visualize_and_compare_testing_models.py
Expand Up @@ -9,6 +9,7 @@
###########################################################################################################

import matplotlib
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
import itertools
Expand All @@ -21,21 +22,7 @@

matplotlib.rcParams['toolbar'] = 'None' # Remove buttons

gui_env = ['TKAgg', 'GTKAgg', 'Qt4Agg', 'WXAgg']
selected_gui_env = []
for gui in gui_env:
try:
matplotlib.use(gui)
from matplotlib import pyplot as plt

selected_gui_env = gui
break
except:
continue
# If none works
if selected_gui_env == []:
from matplotlib import pyplot as plt

if matplotlib.get_backend() == "agg":
Comment on lines 24 to +25
Copy link
Contributor Author

@kousu kousu May 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this loop is redundant: https://matplotlib.org/stable/users/explain/backends.html

Without a backend explicitly set, Matplotlib automatically detects a usable backend based on what is available on your system and on whether a GUI event loop is already running. The first usable backend in the following list is selected: MacOSX, QtAgg, GTK4Agg, Gtk3Agg, TkAgg, WxAgg, Agg.

and calling use() in the middle of tests is messing the tests up by leaking state from one test to another, because use() is setting an internal global variable and hence causing tests to subtly change their behaviour depending on the order they happen to run in.

Especially because because none of the options were 'Agg', that might explain why the tests were being forced to try to load TkAgg even though it was unnecessary and in fact temporarily broken.

logger.warning("No backend can be used - Visualization will fail")
else:
logger.info(f"Using: {matplotlib.get_backend()} gui")
Expand Down
1 change: 0 additions & 1 deletion ivadomed/utils.py
Expand Up @@ -222,7 +222,6 @@ def plot_transformed_sample(before, after, list_title=None, fname_out="", cmap="
if fname_out:
plt.savefig(fname_out)
else:
matplotlib.use('TkAgg')
plt.show()


Expand Down