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

Conversation

kousu
Copy link
Contributor

@kousu kousu commented May 17, 2022

Checklist

GitHub

  • I've given this PR a concise, self-descriptive, and meaningful title
  • I've linked relevant issues in the PR body
  • I've applied the relevant labels to this PR
  • I've assigned a reviewer

PR contents

Description

Force Agg matplotlib backend to avoid problems in CI. CI should be headless anyway, there's no need to try the other six backends.

Linked issues

Fixes #1132, which is the most recent symptom, but it's not the only tricky thing that can come up when mixing and matching rendering backends and headless test machines.

@kousu kousu added the CI category: TravisCI, GitHub Actions, etc. label May 17, 2022
@kousu kousu requested review from cakester and kanishk16 May 17, 2022 23:57
@kousu
Copy link
Contributor Author

kousu commented May 18, 2022

We use the same solution in SCT, but there we set it as a pytest setting:

https://github.com/spinalcordtoolbox/spinalcordtoolbox/blob/20d2f599c6bccffbcc0a852ad6acd3b71d509457/testing/conftest.py#L28-L33

@joshuacwnewton and I had a debate about which file was better to set this variable in: spinalcordtoolbox/spinalcordtoolbox#3420 (comment) and he had a good point, so maybe this should be moved there to conftest.py; except ivadomed doesn't yet have a conftest.py so the pressure of the status quo holds me back.

@kousu
Copy link
Contributor Author

kousu commented May 18, 2022

Drat, it didn't work: https://github.com/ivadomed/ivadomed/actions/runs/2342071673

Screenshot 2022-05-17 at 20-20-54 Use headless ( Agg ) matplotlib backend by kousu · Pull Request #1133 · ivadomed_ivadomed

I don't get it, that should have worked.

@coveralls
Copy link

coveralls commented May 18, 2022

Pull Request Test Coverage Report for Build 2354062718

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.04%) to 78.994%

Files with Coverage Reduction New Missed Lines %
ivadomed/scripts/visualize_and_compare_testing_models.py 1 17.12%
Totals Coverage Status
Change from base Build 2259135716: -0.04%
Covered Lines: 4697
Relevant Lines: 5946

💛 - Coveralls

Comment on lines 23 to +24

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":
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.

@kousu kousu changed the title Use headless ("Agg") matplotlib backend Use headless ("Agg") matplotlib backend in CI May 18, 2022
@kousu
Copy link
Contributor Author

kousu commented May 18, 2022

@joshuacwnewton helped me make it work!

kousu and others added 2 commits May 18, 2022 15:07
Force Agg matplotlib backend

Fixes #1132
Leave the choice of backend to the user/environment;
trying to guess causes more trouble than it saves IMO.

Co-authored-by: Joshua Newton <joshuacwnewton@gmail.com>
Copy link
Contributor

@kanishk16 kanishk16 left a comment

Choose a reason for hiding this comment

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

Apart from just importing pyplot in visualize_and_compare_testing_models.py, LGTM!

@kousu
Copy link
Contributor Author

kousu commented May 19, 2022

Apart from just importing pyplot in visualize_and_compare_testing_models.py, LGTM!

Do you want

from matplotlib import pyplot as plt

to be moved to the top of the file instead of removed?

The tests passed without that, do you think it's really necessary?

@joshuacwnewton
Copy link
Member

joshuacwnewton commented May 19, 2022

The tests passed without that, do you think it's really necessary?

The visualize_and_compare_testing_models.py script is only ~16% covered (though, for some reason Coveralls isn't giving line-by-line coverage). So, I'd be skeptical of trusting the test suite here?

Is it possible that the onclick and visualize_and_compare_models functions (which is where plt is used) aren't tested at all?

@kanishk16
Copy link
Contributor

Apart from just importing pyplot in visualize_and_compare_testing_models.py, LGTM!

Do you want

from matplotlib import pyplot as plt

to be moved to the top of the file instead of removed?

Yup

The tests passed without that, do you think it's really necessary?

I guess that'd be necessary if someone runs it as a script

Usage example::
visualize_and_compare_testing_models.py --ofolders ~/logs/logs_T1w ~/logs/logs_T2w
--metric dice_class0 --metadata pathology ms

Moreover, I believe there are no tests yet for it.

@kousu
Copy link
Contributor Author

kousu commented May 19, 2022

Moreover, I believe there are no tests yet for it.

Oh true, I didn't notice that. I'll put it in then merge :)

@kousu kousu merged commit 1da019b into master May 19, 2022
@kousu kousu deleted the ng/agg-ci-backend branch May 19, 2022 19:52
kousu added a commit that referenced this pull request Jun 2, 2022
@dyt811 dyt811 added this to the new release milestone Jun 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI category: TravisCI, GitHub Actions, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CI failure: "RuntimeError: tk.h version (8.6) doesn't match libtk.a version (8.5)"
5 participants